mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-11-12 22:29:53 -06:00
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, test } from "vitest";
|
|
import { weekNumberToDate, weekNumberToDateRange } from "./dates";
|
|
|
|
describe("weekNumberToDate", () => {
|
|
// the bug only manifests west of UTC
|
|
const originalTimezone = process.env.TZ;
|
|
beforeAll(() => {
|
|
process.env.TZ = "America/Los_Angeles";
|
|
});
|
|
afterAll(() => {
|
|
process.env.TZ = originalTimezone;
|
|
});
|
|
|
|
test("returns the Monday of the ISO week regardless of server timezone", () => {
|
|
// ISO week 1 of 2024 starts on Monday 2024-01-01
|
|
const start = weekNumberToDate({ week: 1, year: 2024 });
|
|
|
|
expect(start.toISOString().slice(0, 10)).toBe("2024-01-01");
|
|
});
|
|
|
|
test("returns the Sunday of the ISO week regardless of server timezone", () => {
|
|
// ISO week 1 of 2024 ends on Sunday 2024-01-07
|
|
const end = weekNumberToDate({ week: 1, year: 2024, position: "end" });
|
|
|
|
expect(end.toISOString().slice(0, 10)).toBe("2024-01-07");
|
|
});
|
|
});
|
|
|
|
describe("weekNumberToDateRange", () => {
|
|
// a timezone west of UTC observing DST
|
|
const originalTimezone = process.env.TZ;
|
|
beforeAll(() => {
|
|
process.env.TZ = "America/New_York";
|
|
});
|
|
afterAll(() => {
|
|
process.env.TZ = originalTimezone;
|
|
});
|
|
|
|
const SEVEN_DAYS_MS = 7 * 24 * 60 * 60 * 1000;
|
|
|
|
test("spans exactly seven days even when the week contains a DST transition", () => {
|
|
// US spring-forward Sunday 2025-03-09 falls inside ISO week 10 (Mon 03-03 .. Mon 03-10)
|
|
const { startTime, endTime } = weekNumberToDateRange({
|
|
week: 10,
|
|
year: 2025,
|
|
});
|
|
|
|
expect(endTime.getTime() - startTime.getTime()).toBe(SEVEN_DAYS_MS);
|
|
});
|
|
|
|
test("spans exactly seven days for an ordinary week", () => {
|
|
const { startTime, endTime } = weekNumberToDateRange({
|
|
week: 20,
|
|
year: 2025,
|
|
});
|
|
|
|
expect(endTime.getTime() - startTime.getTime()).toBe(SEVEN_DAYS_MS);
|
|
});
|
|
});
|