From e1b9ffa84d152f719124e99d7fa3500515e29667 Mon Sep 17 00:00:00 2001 From: philluiz2323 Date: Fri, 24 Jul 2026 17:33:55 +0400 Subject: [PATCH] test(db): cover pruneExpiredRecords' two defensive ?? 0 arms (#8370) Neither the dry-run row?.n ?? 0 fallback (line 75) nor the delete-loop result.meta?.changes ?? 0 fallback (line 85) had direct test coverage, unlike the identical pattern on this file's sibling function dedupeSignalSnapshots, which already has dedicated tests for both. Mirrors that exact mocking approach: env.DB.prepare returning a row/meta shape missing the relevant field, asserting the function falls back to 0 rather than throwing or producing NaN. No production-code changes. --- test/unit/retention.test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/unit/retention.test.ts b/test/unit/retention.test.ts index 086758da92..8326126d66 100644 --- a/test/unit/retention.test.ts +++ b/test/unit/retention.test.ts @@ -140,6 +140,30 @@ describe("pruneExpiredRecords", () => { const rows = await env.DB.prepare("SELECT delivery_id FROM webhook_events").all<{ delivery_id: string }>(); expect(rows.results.map((row) => row.delivery_id)).toEqual(["wh-recent"]); }); + + it("dry-run falls back to 0 when the count query returns no row (defensive ?? 0 arm, #8370)", async () => { + const noRowEnv = { + DB: { + prepare: (_sql: string) => ({ + bind: (..._binds: unknown[]) => ({ first: async () => undefined }), // count query returns no row → `row?.n ?? 0` fallback fires + }), + }, + } as unknown as Env; + const results = await pruneExpiredRecords(noRowEnv, { dryRun: true, policy: [{ table: "webhook_events", column: "received_at", days: 1 }] }); + expect(results).toEqual([{ table: "webhook_events", column: "received_at", cutoff: expect.any(String), deleted: 0 }]); + }); + + it("falls back to 0 changes when a delete run() result lacks meta (defensive ?? 0 arm, #8370)", async () => { + const noMetaEnv = { + DB: { + prepare: (_sql: string) => ({ + bind: (..._binds: unknown[]) => ({ run: async () => ({}) }), // no meta → `result.meta?.changes ?? 0` fallback fires, so changes = 0 < batchSize + }), + }, + } as unknown as Env; + const results = await pruneExpiredRecords(noMetaEnv, { policy: [{ table: "webhook_events", column: "received_at", days: 1 }] }); + expect(results).toEqual([{ table: "webhook_events", column: "received_at", cutoff: expect.any(String), deleted: 0 }]); + }); }); describe("dedupeSignalSnapshots", () => {