import { test, expect, describe } from "bun:test"; import { beginCloseoutJob, trackCloseoutJob, pendingCloseoutJobs, reviewCardWaitingOn, REPORT_START_GRACE_MS, } from "../src/closeout_jobs"; /** * What the review card waits for before it goes out. A false "ready" is the bug * this exists to fix — the reader taps the card and finds a half-built panel; * a false "not ready" only costs time, bounded by REVIEW_CARD_MAX_WAIT_MS. */ describe("review card: is this round's closeout done", () => { const T0 = 1_792_688_798_000; const base = { reportFresh: true, reportHtml: true, reportError: false, reportInFlight: false, reportGeneratedAt: T0, altGeneratedAt: T0 + 60_000, bilingual: true, sinceEntryMs: 3 * 60_000, jobs: [] as ("mr" | "media")[], }; test("report + translation in, no jobs running → send", () => { expect(reviewCardWaitingOn(base)).toEqual([]); }); test("report still being generated (previous round's body on the row) → wait", () => { expect(reviewCardWaitingOn({ ...base, reportFresh: false, reportInFlight: true })).toEqual(["report"]); }); test("first round, nothing on the row yet → wait", () => { expect(reviewCardWaitingOn({ ...base, reportFresh: true, reportHtml: false, reportInFlight: true })).toEqual(["report"]); }); test("translation still belongs to the previous round → wait for this one", () => { expect(reviewCardWaitingOn({ ...base, altGeneratedAt: T0 - 3_600_000 })).toEqual(["translation"]); expect(reviewCardWaitingOn({ ...base, altGeneratedAt: null })).toEqual(["translation"]); }); test("bilingual switched off → translation is not waited on", () => { expect(reviewCardWaitingOn({ ...base, altGeneratedAt: null, bilingual: false })).toEqual([]); }); test("MR / media still running → wait for them", () => { expect(reviewCardWaitingOn({ ...base, jobs: ["mr", "media"] })).toEqual(["mr", "media"]); }); test("generation failed for good → stop waiting for it and for its translation", () => { expect(reviewCardWaitingOn({ ...base, reportFresh: false, reportError: true, reportInFlight: false, altGeneratedAt: null, sinceEntryMs: REPORT_START_GRACE_MS + 1, })).toEqual([]); }); test("an old error right after entry is not a failure of this round — its run may not be registered yet", () => { expect(reviewCardWaitingOn({ ...base, reportFresh: false, reportError: true, reportInFlight: false, sinceEntryMs: 2_000, })).toEqual(["report"]); }); test("an old error while a new run is in flight → keep waiting", () => { expect(reviewCardWaitingOn({ ...base, reportFresh: false, reportError: true, reportInFlight: true, sinceEntryMs: 10 * 60_000, })).toEqual(["report"]); }); }); describe("closeout job registry", () => { test("tracked promise registers until it settles, success or failure", async () => { let resolve!: () => void; const p = trackCloseoutJob("bug", "a", "media", new Promise((r) => { resolve = r; })); expect(pendingCloseoutJobs("bug", "a")).toEqual(["media"]); resolve(); await p; expect(pendingCloseoutJobs("bug", "a")).toEqual([]); const failing = trackCloseoutJob("bug", "a", "mr", Promise.reject(new Error("bits down"))); expect(pendingCloseoutJobs("bug", "a")).toEqual(["mr"]); await failing.catch(() => {}); expect(pendingCloseoutJobs("bug", "a")).toEqual([]); }); test("overlapping runs of one job end only when the last one does; ending twice is harmless", () => { const a = beginCloseoutJob("feature", "b", "mr"); const b = beginCloseoutJob("feature", "b", "mr"); a(); a(); expect(pendingCloseoutJobs("feature", "b")).toEqual(["mr"]); b(); expect(pendingCloseoutJobs("feature", "b")).toEqual([]); }); test("items and kinds are kept apart", () => { const end = beginCloseoutJob("bug", "c", "media"); expect(pendingCloseoutJobs("feature", "c")).toEqual([]); expect(pendingCloseoutJobs("bug", "d")).toEqual([]); end(); }); });