Files
beaver-calendar/tests/due.test.ts
T

58 lines
1.9 KiB
TypeScript

import { afterAll, beforeAll, describe, expect, it } from "vitest";
import {
daysFromToday,
dueLabel,
dueTone,
localKey,
monthGrid,
nextFriday,
} from "../src/lib/due";
describe("due helpers read the day they are given", () => {
it("labels against the given today, not the wall clock", () => {
expect(dueLabel("2026-08-23", "2026-08-23")).toBe("today");
expect(dueLabel("2026-08-24", "2026-08-23")).toBe("tomorrow");
expect(dueLabel("2026-08-22", "2026-08-23")).toBe("yesterday");
expect(dueLabel("2026-08-23", "2026-08-30")).toBe("Aug 23");
});
it("tones against the given today", () => {
expect(dueTone("2026-08-22", "2026-08-23")).toBe("overdue");
expect(dueTone("2026-08-24", "2026-08-23")).toBe("soon");
expect(dueTone("2026-09-24", "2026-08-23")).toBe("normal");
expect(daysFromToday("2026-09-02", "2026-08-23")).toBe(10);
});
it("finds the friday after the given day, never the same one", () => {
expect(nextFriday("2026-08-23")).toBe("2026-08-28");
expect(nextFriday("2026-08-28")).toBe("2026-09-04");
});
});
describe("monthGrid steps by calendar day", () => {
const zone = process.env.TZ;
beforeAll(() => {
process.env.TZ = "Europe/Berlin";
});
afterAll(() => {
process.env.TZ = zone;
});
it("keeps every day once across the autumn clock change", () => {
const keys = monthGrid(new Date(2026, 9, 1)).map(localKey);
expect(keys).toHaveLength(42);
expect(new Set(keys).size).toBe(42);
expect(keys[0]).toBe("2026-09-28");
expect(keys[27]).toBe("2026-10-25");
expect(keys[28]).toBe("2026-10-26");
expect(keys[41]).toBe("2026-11-08");
});
it("keeps every day once across the spring clock change", () => {
const keys = monthGrid(new Date(2027, 2, 1)).map(localKey);
expect(new Set(keys).size).toBe(42);
expect(keys).toContain("2027-03-28");
expect(keys).toContain("2027-03-29");
});
});