import { describe, expect, it } from 'vitest' import { currentQAWeek, qaDateTimeToISO, qaWeek, qaWeeksInYear, qaWeekQueryRange } from './qaWeeks' describe('QA natural-week query ranges', () => { it('caps the in-progress week at now but leaves completed weeks intact', () => { const now = new Date('2026-10-18T06:00:00Z') expect(qaWeekQueryRange(2026, 42, now).end).toBe('2026-10-18T14:00') expect(qaWeekQueryRange(2026, 40, now).end).toBe('2026-10-05T00:00') expect(qaWeekQueryRange(2026, 53, now).start).toBe('2026-12-28T00:00') }) it('resolves the current ISO week at the Beijing midnight boundary', () => { expect(currentQAWeek(new Date('2026-10-18T15:59:00Z'))).toMatchObject({ year: 2026, week: 42 }) expect(currentQAWeek(new Date('2026-10-18T16:01:00Z'))).toMatchObject({ year: 2026, week: 43 }) expect(currentQAWeek(new Date('2027-01-01T12:00:00Z'))).toMatchObject({ year: 2026, week: 53 }) }) it('converts Beijing form input to UTC without relying on the browser timezone', () => { expect(qaDateTimeToISO('2026-09-28T00:00')).toBe('2026-09-27T16:00:00.000Z') expect(qaDateTimeToISO('2026-10-05T00:00')).toBe('2026-10-04T16:00:00.000Z') for (const input of ['', '2026-02-30T00:00', '2026-10-04', '2026-10-04T24:00', 'invalid']) { expect(() => qaDateTimeToISO(input)).toThrow() } }) it('counts only the valid weeks for each year', () => { expect(qaWeeksInYear(2026)).toBe(53) expect(qaWeeksInYear(2027)).toBe(52) expect(qaWeeksInYear(2020)).toBe(53) expect(qaWeeksInYear(2021)).toBe(52) }) it('shows week dates inclusively and queries through the exclusive next Monday', () => { expect(qaWeek(2026, 40)).toEqual({ year: 2026, week: 40, start: '2026-09-28T00:00', end: '2026-10-05T00:00', label: '第40周(09-28 到 10-04)', }) }) it('rejects nonexistent weeks instead of spilling into the next year', () => { expect(() => qaWeek(2027, 53)).toThrow() for (const week of [0, -1, 54, 1.5, Number.NaN]) expect(() => qaWeek(2026, week)).toThrow() for (const year of [0, 2026.5, Number.NaN]) expect(() => qaWeek(year, 1)).toThrow() }) it('keeps the ISO week-year and shows years on crossing ranges', () => { expect(qaWeek(2026, 1)).toMatchObject({ start: '2025-12-29T00:00', end: '2026-01-05T00:00', label: '第1周(2025-12-29 到 2026-01-04)', }) expect(qaWeek(2026, 53)).toMatchObject({ start: '2026-12-28T00:00', end: '2027-01-04T00:00', label: '第53周(2026-12-28 到 2027-01-03)', }) expect(qaWeek(2024, 9)).toMatchObject({ start: '2024-02-26T00:00', end: '2024-03-04T00:00' }) }) })