import { describe, expect, it } from "vitest"; import { compareDateDesc, defaultPeriod, isValidPeriod, moscowDate, } from "./calendar"; import { calendarDate } from "./calendar.schema"; describe("calendar dates", () => { it.each([ ["0000-02-29", true], ["0001-01-01", true], ["0099-12-31", true], ["1900-02-29", false], ["2000-02-29", true], ["2024-02-29", true], ["2026-02-29", false], ["2026-99-99", false], ["2026-1-01", false], ["", false], ])("validates %s without Date normalization", (value, valid) => { expect(calendarDate.safeParse(value).success).toBe(valid); }); it("accepts open or single-day periods and rejects reversed ranges", () => { expect(isValidPeriod(null, null)).toBe(true); expect(isValidPeriod(null, "2026-09-01")).toBe(true); expect(isValidPeriod("2026-09-01", null)).toBe(true); expect(isValidPeriod("2026-09-01", "2026-09-01")).toBe(true); expect(isValidPeriod("2026-09-02", "2026-09-01")).toBe(false); expect(isValidPeriod("", "2026-09-01")).toBe(false); }); it("changes the calendar day at Moscow midnight across a year boundary", () => { expect(moscowDate(new Date("2026-12-31T20:59:59Z"))).toBe("2026-12-31"); expect(moscowDate(new Date("2026-12-31T21:00:00Z"))).toBe("2027-01-01"); }); it("defaults to the last 7 days including today", () => { const [from, to] = defaultPeriod(); expect(calendarDate.safeParse(from).success).toBe(true); expect(to).toBe(moscowDate(new Date())); expect((Date.parse(to) - Date.parse(from)) / 86_400_000).toBe(6); expect(from <= to).toBe(true); }); it("compares dates newest first and keeps dateless rows last", () => { const dates = [null, "2026-01-01", "2026-03-01", "2026-02-01"]; expect(dates.toSorted(compareDateDesc)).toEqual([ "2026-03-01", "2026-02-01", "2026-01-01", null, ]); expect(dates.toSorted((a, b) => compareDateDesc(b, a))).toEqual([ null, "2026-01-01", "2026-02-01", "2026-03-01", ]); }); });