From 114698a71847acaee6c3c00c417f1af26609801a Mon Sep 17 00:00:00 2001 From: seedds Date: Wed, 11 Nov 2026 18:40:16 +0100 Subject: [PATCH] test(usage): pin checkpoint fixture clock after scan yields --- tests/usage/usage-ledger-scanner.test.ts | 60 +++++++++++++----------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/tests/usage/usage-ledger-scanner.test.ts b/tests/usage/usage-ledger-scanner.test.ts index f81a85d9d8b..e6af1574fc4 100644 --- a/tests/usage/usage-ledger-scanner.test.ts +++ b/tests/usage/usage-ledger-scanner.test.ts @@ -469,34 +469,40 @@ describe("usage ledger cooperative scanner", () => { const rewritten = line("new-checkpoint"); expect(Buffer.byteLength(rewritten)).toBe(Buffer.byteLength(original)); writeFileSync(usageLogPath(), original); - let abortChecks = 0; - const rewriteAfterVerification = { - get aborted() { - abortChecks += 1; - if (abortChecks === 4) writeFileSync(usageLogPath(), rewritten); - return false; - }, - } as AbortSignal; - - const ids: string[] = []; - const result = await scanUsageLedgerCooperatively({ - signal: rewriteAfterVerification, - onEntry: value => ids.push(value.requestId), - }); - const originalDigest = createHash("sha256").update(original).digest("hex"); - expect(abortChecks).toBeGreaterThanOrEqual(4); - expect(ids).toEqual(["old-checkpoint"]); - expect(result.processedThroughDigest).toBe(originalDigest); + // This fixture rewrites after verification; elapsed-time yields have separate coverage. + const clock = spyOn(performance, "now").mockReturnValue(0); + try { + let abortChecks = 0; + const rewriteAfterVerification = { + get aborted() { + abortChecks += 1; + if (abortChecks === 4) writeFileSync(usageLogPath(), rewritten); + return false; + }, + } as AbortSignal; + + const ids: string[] = []; + const result = await scanUsageLedgerCooperatively({ + signal: rewriteAfterVerification, + onEntry: value => ids.push(value.requestId), + }); + const originalDigest = createHash("sha256").update(original).digest("hex"); + expect(abortChecks).toBeGreaterThanOrEqual(4); + expect(ids).toEqual(["old-checkpoint"]); + expect(result.processedThroughDigest).toBe(originalDigest); - await expect(scanUsageLedgerCooperatively({ - startAtBytes: result.processedThroughBytes, - expectedIdentityKey: usageLogIdentityKey(result.revision), - expectedProcessedThroughDigest: result.processedThroughDigest, - onEntry: () => {}, - })).rejects.toMatchObject({ - code: "usage_ledger_rebuild_required", - reason: "content_changed", - }); + await expect(scanUsageLedgerCooperatively({ + startAtBytes: result.processedThroughBytes, + expectedIdentityKey: usageLogIdentityKey(result.revision), + expectedProcessedThroughDigest: result.processedThroughDigest, + onEntry: () => {}, + })).rejects.toMatchObject({ + code: "usage_ledger_rebuild_required", + reason: "content_changed", + }); + } finally { + clock.mockRestore(); + } }); test("rejects a shrink while the scanner is yielded", async () => {