const path = require("node:path"); const { pathToFileURL } = require("node:url"); let pendingOcr; describe("pending OCR readiness contract", () => { beforeAll(async () => { const scriptPath = path.resolve(__dirname, "../../../../scripts/qa/check-pending-ocr.mjs"); pendingOcr = await import(pathToFileURL(scriptPath).href); }); it("normalizes unique positive thresholds", () => { expect(pendingOcr.parseThresholds(["--thresholds=10,2,5,2,0,invalid"])).toEqual([2, 5, 10]); }); it("reports a non-clean SKIP when the database target is unavailable", async () => { const summary = await pendingOcr.buildPendingOcrSummary({ databaseReady: false, thresholds: [2, 5, 10], countPending: vi.fn(), now: new Date("2026-07-15T00:00:00.000Z"), }); expect(summary).toMatchObject({ status: "SKIP", findingCount: 1, findings: [{ code: "database_not_configured", severity: "manual_or_config" }], thresholds: [], }); }); it("reports PASS when no pending OCR rows exceed any threshold", async () => { const countPending = vi.fn(async () => 0); const summary = await pendingOcr.buildPendingOcrSummary({ databaseReady: true, thresholds: [2, 5], countPending, now: new Date("2026-07-15T00:00:00.000Z"), }); expect(summary).toMatchObject({ status: "PASS", findingCount: 0, findings: [] }); expect(summary.thresholds).toEqual([ { thresholdMinutes: 2, count: 0 }, { thresholdMinutes: 5, count: 0 }, ]); expect(countPending).toHaveBeenNthCalledWith(1, { status: "pending_ocr", created_at: { lte: new Date("2026-07-14T23:58:00.000Z") }, }); }); it("reports PASS_WITH_FINDINGS using the unique pending row upper bound", async () => { const counts = [2, 4, 7]; const summary = await pendingOcr.buildPendingOcrSummary({ databaseReady: true, thresholds: [2, 5, 10], countPending: vi.fn(async () => counts.shift()), now: new Date("2026-07-15T00:00:00.000Z"), }); expect(summary).toMatchObject({ status: "PASS_WITH_FINDINGS", findingCount: 7, findings: [{ code: "pending_ocr_detected", severity: "finding", count: 7 }], }); }); it("constructs and closes the shared Prisma 7 adapter without opening a database connection", async () => { const previousDatabaseUrl = process.env.DATABASE_URL; process.env.DATABASE_URL = "postgresql://local-user:local-password@127.0.0.1:1/hyb_oa_test"; try { await expect(pendingOcr.executePendingOcrCheck({ databaseReady: true, thresholds: [], })).resolves.toMatchObject({ status: "PASS", findingCount: 0 }); } finally { if (previousDatabaseUrl === undefined) { delete process.env.DATABASE_URL; } else { process.env.DATABASE_URL = previousDatabaseUrl; } } }); });