import { expect, test } from "vitest"; import { monthPeriod, quarterPeriod, type BillingPeriod } from "./period"; test("converts a Berlin month to an exclusive UTC range", () => { expect(monthPeriod(2026, 3)).toEqual({ fromUtc: new Date("2026-02-28T23:00:00.000Z"), toUtcExclusive: new Date("2026-03-31T22:00:00.000Z"), }); }); test("converts a Berlin month in full summer (CEST) to an exclusive UTC range", () => { expect(monthPeriod(2026, 8)).toEqual({ fromUtc: new Date("2026-07-31T22:00:00.000Z"), toUtcExclusive: new Date("2026-08-31T22:00:00.000Z"), }); }); test("converts a Berlin month in full winter (CET) to an exclusive UTC range", () => { expect(monthPeriod(2026, 1)).toEqual({ fromUtc: new Date("2025-12-31T23:00:00.000Z"), toUtcExclusive: new Date("2026-01-31T23:00:00.000Z"), }); }); test("converts a Berlin quarter spanning a DST transition to an exclusive UTC range", () => { // Q1 2026: Jan 1 to Apr 1. Start is CET (+1), end is CEST (+2) after Mar 29 switch. expect(quarterPeriod(2026, 1)).toEqual({ fromUtc: new Date("2025-12-31T23:00:00.000Z"), toUtcExclusive: new Date("2026-03-31T22:00:00.000Z"), }); }); test("converts a Berlin quarter fully in summer (CEST) to an exclusive UTC range", () => { // Q3 2026: Jul 1 to Oct 1. Both CEST (+2) until Oct 25 switch. Oct 1 is still CEST. expect(quarterPeriod(2026, 3)).toEqual({ fromUtc: new Date("2026-06-30T22:00:00.000Z"), toUtcExclusive: new Date("2026-09-30T22:00:00.000Z"), }); }); test("converts a Berlin quarter spanning the autumn DST transition", () => { // Q4 2026: Oct 1 to Jan 1 2027. Start is CEST (+2), end is CET (+1) after Oct 25 switch. expect(quarterPeriod(2026, 4)).toEqual({ fromUtc: new Date("2026-09-30T22:00:00.000Z"), toUtcExclusive: new Date("2026-12-31T23:00:00.000Z"), }); }); test("BillingPeriod has fromUtc and toUtcExclusive Date fields", () => { const period: BillingPeriod = monthPeriod(2026, 8); expect(period.fromUtc).toBeInstanceOf(Date); expect(period.toUtcExclusive).toBeInstanceOf(Date); expect(period.toUtcExclusive.getTime()).toBeGreaterThan(period.fromUtc.getTime()); }); test("rolls over the year boundary for December", () => { expect(monthPeriod(2026, 12)).toEqual({ fromUtc: new Date("2026-11-30T23:00:00.000Z"), toUtcExclusive: new Date("2026-12-31T23:00:00.000Z"), }); }); test("supports an explicit timezone override", () => { // America/New_York is UTC-5 in winter (EST), UTC-4 in summer (EDT). // March 2026: Mar 1 00:00 EST = Mar 1 05:00 UTC; Apr 1 00:00 EDT = Apr 1 04:00 UTC. expect(monthPeriod(2026, 3, "America/New_York")).toEqual({ fromUtc: new Date("2026-03-01T05:00:00.000Z"), toUtcExclusive: new Date("2026-04-01T04:00:00.000Z"), }); });