import {
  addUtcDays,
  dateInTimeZone,
  renewalWindow,
  RENEWAL_CADENCE,
} from "./renewals.service";

describe("renewal scheduling dates", () => {
  it("uses the America/Tijuana calendar date", () => {
    expect(dateInTimeZone(new Date("2026-08-01T05:00:00.000Z"))).toEqual(
      new Date("2026-07-31T00:00:00.000Z"),
    );
  });

  it("maps generations to 30 days, 15 days, and 7 days overdue", () => {
    const today = new Date("2026-08-01T00:00:00.000Z");

    expect(
      RENEWAL_CADENCE.map(({ generation, offsetDays }) => ({
        generation,
        target: addUtcDays(today, offsetDays).toISOString().slice(0, 10),
      })),
    ).toEqual([
      { generation: 1, target: "2026-08-31" },
      { generation: 2, target: "2026-08-16" },
      { generation: 3, target: "2026-07-25" },
    ]);
  });

  it("uses an inclusive catch-up window after a missed run", () => {
    const window = renewalWindow(
      new Date("2026-08-10T00:00:00.000Z"),
      30,
      new Date("2026-08-07T18:00:00.000Z"),
    );

    expect(window).toEqual({
      from: new Date("2026-09-07T00:00:00.000Z"),
      to: new Date("2026-09-09T00:00:00.000Z"),
    });
  });
});
