import { describe, expect, test } from "vitest"; import { buildKwpArchiveDiscoveryEnqueue, buildKwpArchiveFileEnqueue } from "./kwp-archive-queue.js"; const projectPayload = { kind: "kwp.project-sync" as const, tenantId: "tenant_1", bindingId: "binding_1", dataConnectionId: "connection_1", cadenceBucket: "project-15-minute-bucket", }; describe("KWP archive queue policy", () => { test("deduplicates initial archive discovery into one six-hour window", () => { const first = buildKwpArchiveDiscoveryEnqueue({ payload: projectPayload, now: new Date("2026-08-01T05:59:59.000Z"), }); const second = buildKwpArchiveDiscoveryEnqueue({ payload: projectPayload, now: new Date("2026-08-01T01:00:00.000Z"), }); expect(first.descriptor.payload).toMatchObject({ kind: "kwp.archive-discovery", cadenceBucket: "2026-08-01T00:00:00.000Z", batchNumber: 0, }); expect(first.options.idempotencyKey).toBe(second.options.idempotencyKey); expect(first.options).toMatchObject({ attempts: 3, backoff: { type: "exponential", delay: 60_000 } }); expect(first.options.delay).toBeUndefined(); }); test("continues the same crawl after five seconds with a stable batch id", () => { const payload = { ...projectPayload, kind: "kwp.archive-discovery" as const, cadenceBucket: "2026-08-01T00:00:00.000Z", batchNumber: 3, }; const cursor = { pendingFolderIds: [20], visitedFolderIds: [0, 1, 2, 3, 4] }; const result = buildKwpArchiveDiscoveryEnqueue({ payload, cursor, now: new Date("2026-08-01T07:00:00.000Z") }); expect(result.descriptor.payload).toMatchObject({ batchNumber: 4, cursor }); expect(result.options).toMatchObject({ delay: 5_000, attempts: 3 }); }); test("retries each failed file only inside its six-hour cooldown bucket", () => { const payload = { ...projectPayload, kind: "kwp.archive-discovery" as const, cadenceBucket: "2026-08-01T00:00:00.000Z", batchNumber: 0, }; const archiveFile = { externalId: "1:2:3", headerId: 1, revisionId: 3, revision: 1, folderId: 0, displayName: "plan.pdf", documentType: null, providerFileType: "PDF", providerSizeBytes: 123, lastModifiedAt: null, }; const result = buildKwpArchiveFileEnqueue(payload, archiveFile); expect(result.options).toMatchObject({ idempotencyKey: "kwp-archive:tenant_1:connection_1:1:2:3:2026-08-01T00:00:00.000Z", attempts: 3, backoff: { type: "exponential", delay: 60_000 }, }); }); });