Files

59 lines
2.4 KiB
TypeScript

/**
* The relay's week.
*
* Half of these are boundary cases that occur twice a year at most, which is
* exactly why they are pinned here: a week-numbering bug is invisible for
* eleven months and then rejects every submission in the first days of January.
*/
import { describe, expect, it } from 'vitest';
import { currentWeekId, isWeekIdShape } from '../src/week.js';
describe('currentWeekId', () => {
it('numbers an ordinary mid-year week', () => {
// Wednesday 2026-08-19 — the week the fixtures use.
expect(currentWeekId(Date.UTC(2026, 7, 19, 12))).toBe('2026-W34');
});
it('keeps Monday and the following Sunday in the same week', () => {
const monday = Date.UTC(2026, 7, 17, 0, 0, 0);
const sunday = Date.UTC(2026, 7, 23, 23, 59, 59);
expect(currentWeekId(monday)).toBe(currentWeekId(sunday));
});
it('rolls over at Monday 00:00 UTC and not a millisecond earlier', () => {
const lastMs = Date.UTC(2026, 7, 23, 23, 59, 59, 999);
const firstMs = Date.UTC(2026, 7, 24, 0, 0, 0, 0);
expect(currentWeekId(lastMs)).toBe('2026-W34');
expect(currentWeekId(firstMs)).toBe('2026-W35');
});
it('puts early January into the PREVIOUS year when ISO says so', () => {
// 2027-01-01 is a Friday, so it belongs to the week that started
// 2026-12-28 — ISO week 53 of 2026. Deriving the year from the calendar
// year is the classic bug and would call this `2027-W53`.
expect(currentWeekId(Date.UTC(2027, 0, 1, 12))).toBe('2026-W53');
});
it('puts late December into the NEXT year when ISO says so', () => {
// 2025-12-29 is a Monday, and 2026-01-01 is a Thursday — so that week is
// week 01 of 2026 even though three of its days are in 2025.
expect(currentWeekId(Date.UTC(2025, 11, 29, 12))).toBe('2026-W01');
});
it('zero-pads single-digit weeks', () => {
expect(currentWeekId(Date.UTC(2026, 0, 8, 12))).toBe('2026-W02');
});
it('accepts only the padded YYYY-Www shape', () => {
expect(isWeekIdShape('2026-W34')).toBe(true);
expect(isWeekIdShape('2026-W01')).toBe(true);
expect(isWeekIdShape('2026-W53')).toBe(true);
expect(isWeekIdShape('2026-W1')).toBe(false);
expect(isWeekIdShape('2026-W00')).toBe(false);
expect(isWeekIdShape('2026-W54')).toBe(false);
expect(isWeekIdShape('2026-34')).toBe(false);
// Anchoring. An unanchored pattern accepts this, and a week id is a key.
expect(isWeekIdShape('x2026-W34y')).toBe(false);
});
});