import { describe, it, expect, beforeEach, vi } from "vitest"; import { defaultCartesReferencePeriod, readCartesPeriodMode, CARTES_PERIOD_MODE_STORAGE_KEY, } from "./useCartes"; describe("defaultCartesReferencePeriod", () => { it("returns the month before the given date", () => { expect(defaultCartesReferencePeriod(new Date(2026, 3, 15))).toEqual({ year: 2026, month: 3, }); }); it("wraps around January to December of the previous year", () => { expect(defaultCartesReferencePeriod(new Date(2026, 0, 10))).toEqual({ year: 2025, month: 12, }); }); it("handles the last day of a month", () => { expect(defaultCartesReferencePeriod(new Date(2026, 5, 30))).toEqual({ year: 2026, month: 5, }); }); }); describe("readCartesPeriodMode (localStorage round-trip)", () => { const store = new Map(); const mockLocalStorage = { getItem: vi.fn((key: string) => store.get(key) ?? null), setItem: vi.fn((key: string, value: string) => { store.set(key, value); }), removeItem: vi.fn((key: string) => { store.delete(key); }), clear: vi.fn(() => store.clear()), key: vi.fn(), length: 0, }; beforeEach(() => { store.clear(); vi.stubGlobal("localStorage", mockLocalStorage); }); it("defaults to 'month' when nothing is persisted", () => { expect(readCartesPeriodMode()).toBe("month"); }); it("reads back a previously persisted 'month' value (round-trip)", () => { store.set(CARTES_PERIOD_MODE_STORAGE_KEY, "month"); expect(readCartesPeriodMode()).toBe("month"); }); it("reads back a previously persisted 'ytd' value (round-trip)", () => { store.set(CARTES_PERIOD_MODE_STORAGE_KEY, "ytd"); expect(readCartesPeriodMode()).toBe("ytd"); }); it("falls back to default for unknown/corrupted values", () => { store.set(CARTES_PERIOD_MODE_STORAGE_KEY, "bogus"); expect(readCartesPeriodMode()).toBe("month"); }); it("supports a custom fallback", () => { expect(readCartesPeriodMode(CARTES_PERIOD_MODE_STORAGE_KEY, "ytd")).toBe("ytd"); }); it("uses the expected storage key", () => { expect(CARTES_PERIOD_MODE_STORAGE_KEY).toBe("reports-cartes-period-mode"); }); });