/** * cutover-readiness-poll unit tests (node:test). * * Run from apps/web: * node --experimental-strip-types --test src/lib/cutover-readiness-poll.test.ts */ import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { CUTOVER_READINESS_POLL_MS, shouldRunCutoverReadinessFetch } from "./cutover-readiness-poll.ts"; describe("cutover-readiness-poll", () => { it("exports a 60s poll interval", () => { assert.equal(CUTOVER_READINESS_POLL_MS, 60_000); }); it("allows the first attempt when visible and idle", () => { assert.equal( shouldRunCutoverReadinessFetch({ nowMs: 1_000, lastAttemptAtMs: 0, minIntervalMs: CUTOVER_READINESS_POLL_MS, documentVisible: true, inFlight: false }), true ); }); it("blocks while a request is in flight", () => { assert.equal( shouldRunCutoverReadinessFetch({ nowMs: 1_000, lastAttemptAtMs: 0, minIntervalMs: CUTOVER_READINESS_POLL_MS, documentVisible: true, inFlight: true }), false ); }); it("blocks when the document is hidden", () => { assert.equal( shouldRunCutoverReadinessFetch({ nowMs: 1_000, lastAttemptAtMs: 0, minIntervalMs: CUTOVER_READINESS_POLL_MS, documentVisible: false, inFlight: false }), false ); }); it("throttles repeats inside the min interval", () => { assert.equal( shouldRunCutoverReadinessFetch({ nowMs: 30_000, lastAttemptAtMs: 1_000, minIntervalMs: CUTOVER_READINESS_POLL_MS, documentVisible: true, inFlight: false }), false ); }); it("allows a repeat after the min interval", () => { assert.equal( shouldRunCutoverReadinessFetch({ nowMs: 61_000, lastAttemptAtMs: 1_000, minIntervalMs: CUTOVER_READINESS_POLL_MS, documentVisible: true, inFlight: false }), true ); }); it("force bypasses the min interval but not hidden/in-flight", () => { assert.equal( shouldRunCutoverReadinessFetch({ nowMs: 2_000, lastAttemptAtMs: 1_000, minIntervalMs: CUTOVER_READINESS_POLL_MS, documentVisible: true, inFlight: false, force: true }), true ); assert.equal( shouldRunCutoverReadinessFetch({ nowMs: 2_000, lastAttemptAtMs: 1_000, minIntervalMs: CUTOVER_READINESS_POLL_MS, documentVisible: false, inFlight: false, force: true }), false ); assert.equal( shouldRunCutoverReadinessFetch({ nowMs: 2_000, lastAttemptAtMs: 1_000, minIntervalMs: CUTOVER_READINESS_POLL_MS, documentVisible: true, inFlight: true, force: true }), false ); }); });