The classic ASP site prints a week number under the site title, and people quote it in conversation and on paperwork. Anyone with both sites open needs the two to agree, so this is a port of the old arithmetic rather than a fresh interpretation of what a fiscal week is: ISO 8601, week 1 contains 4 January, and the week's Thursday decides which year it belongs to. That rule is what makes late December and early January land in the right year, which is exactly where a naive day-of-year count goes wrong, so it is what the tests cover. Worth recording: a true GE fiscal calendar need not follow ISO weeks. Nobody has asked for a different rule, and inventing one here would silently disagree with the site people compare against. Computed in local time on purpose. The number people quote is the one on the wall where they stand, and a UTC week rolls over hours early in the evening at a US site. The sidebar re-checks every half hour; the shop-floor board picks it up with the clock it already ticks.
50 lines
1.8 KiB
JavaScript
50 lines
1.8 KiB
JavaScript
import { describe, it, expect } from 'vitest'
|
|
import { fiscalWeek, fiscalWeekYear } from './fiscalWeek'
|
|
|
|
/**
|
|
* The number has to match what the classic ASP site shows, because people will
|
|
* have both open. The awkward cases are all at the year boundary, which is
|
|
* exactly where a naive "day of year / 7" gets it wrong.
|
|
*/
|
|
describe('fiscalWeek', () => {
|
|
it('counts the week containing 4 January as week 1', () => {
|
|
expect(fiscalWeek(new Date(2026, 0, 4))).toBe(1)
|
|
})
|
|
|
|
it('puts a late-December date in week 1 of the NEXT year', () => {
|
|
// 2025-12-29 is a Monday; its Thursday falls in 2026, so it is 2026 week 1.
|
|
expect(fiscalWeek(new Date(2025, 11, 29))).toBe(1)
|
|
expect(fiscalWeekYear(new Date(2025, 11, 29))).toBe(2026)
|
|
})
|
|
|
|
it('puts an early-January date in the LAST week of the previous year', () => {
|
|
// 2027-01-01 is a Friday; its Thursday is 2026-12-31, so it is 2026 week 53.
|
|
expect(fiscalWeek(new Date(2027, 0, 1))).toBe(53)
|
|
expect(fiscalWeekYear(new Date(2027, 0, 1))).toBe(2026)
|
|
})
|
|
|
|
it('holds the same number all week, Monday through Sunday', () => {
|
|
const monday = new Date(2026, 7, 10)
|
|
const week = fiscalWeek(monday)
|
|
for (let i = 0; i < 7; i++) {
|
|
const day = new Date(2026, 7, 10 + i)
|
|
expect(fiscalWeek(day)).toBe(week)
|
|
}
|
|
})
|
|
|
|
it('increments the following Monday', () => {
|
|
expect(fiscalWeek(new Date(2026, 7, 17))).toBe(fiscalWeek(new Date(2026, 7, 10)) + 1)
|
|
})
|
|
|
|
it('gives a 53-week year its 53rd week', () => {
|
|
// 2026 starts on a Thursday, so it runs to 53 weeks.
|
|
expect(fiscalWeek(new Date(2026, 11, 31))).toBe(53)
|
|
})
|
|
|
|
it('is not fooled by a time of day', () => {
|
|
const morning = new Date(2026, 7, 12, 6, 0, 0)
|
|
const nearMidnight = new Date(2026, 7, 12, 23, 59, 0)
|
|
expect(fiscalWeek(morning)).toBe(fiscalWeek(nearMidnight))
|
|
})
|
|
})
|