import { describe, expect, it } from "vitest"; import { decideWebhookRenewal, DEFAULT_RENEWAL_THRESHOLD_MS } from "./calendar-webhook-renewal.js"; const now = new Date("2026-07-01T12:00:00Z"); describe("decideWebhookRenewal", () => { it("renews when there is no subscription yet", () => { expect(decideWebhookRenewal({ expiresAt: null, now })).toEqual({ shouldRenew: true, reason: "no_subscription" }); }); it("renews when expiring within the threshold", () => { const expiresAt = new Date(now.getTime() + DEFAULT_RENEWAL_THRESHOLD_MS - 60_000); expect(decideWebhookRenewal({ expiresAt, now })).toEqual({ shouldRenew: true, reason: "expiring_soon" }); }); it("renews exactly at the threshold boundary", () => { const expiresAt = new Date(now.getTime() + DEFAULT_RENEWAL_THRESHOLD_MS); expect(decideWebhookRenewal({ expiresAt, now }).shouldRenew).toBe(true); }); it("leaves a still-valid subscription alone", () => { const expiresAt = new Date(now.getTime() + DEFAULT_RENEWAL_THRESHOLD_MS + 60 * 60_000); expect(decideWebhookRenewal({ expiresAt, now })).toEqual({ shouldRenew: false, reason: "still_valid" }); }); it("honours a custom threshold", () => { const expiresAt = new Date(now.getTime() + 2 * 60 * 60_000); // 2h left expect(decideWebhookRenewal({ expiresAt, now, thresholdMs: 60 * 60_000 }).shouldRenew).toBe(false); expect(decideWebhookRenewal({ expiresAt, now, thresholdMs: 3 * 60 * 60_000 }).shouldRenew).toBe(true); }); });