import { createSharedStore } from "@toolctrl/shared-store"; import { beforeEach, describe, expect, it } from "vitest"; import { readExportProgressFromSharedStore } from "./export-progress.js"; import { __resetSharedStoreForTests, __setSharedStoreFactoryForTests } from "./shared-store.js"; describe("readExportProgressFromSharedStore", () => { beforeEach(() => { const store = createSharedStore({ namespace: "test-export-progress" }); __setSharedStoreFactoryForTests(() => store); __resetSharedStoreForTests(); }); it("returns null when no progress entry has been published", async () => { const snapshot = await readExportProgressFromSharedStore({ exportJobId: "missing", tenantId: "t" }); expect(snapshot).toBeNull(); }); it("decodes the worker payload schema", async () => { const { getSharedStore } = await import("./shared-store.js"); const store = getSharedStore(); await store.set( "export:progress:job-1", JSON.stringify({ processedRows: 4500, totalEstimate: 12000, updatedAt: "2026-05-17T10:00:00.000Z" }), { ttlSeconds: 60 }, ); const snapshot = await readExportProgressFromSharedStore({ exportJobId: "job-1", tenantId: "t" }); expect(snapshot).toEqual({ processedRows: 4500, totalEstimate: 12000, updatedAt: "2026-05-17T10:00:00.000Z", }); }); it("returns null when stored payload is malformed", async () => { const { getSharedStore } = await import("./shared-store.js"); await getSharedStore().set("export:progress:job-bad", "{not-json", { ttlSeconds: 60 }); const snapshot = await readExportProgressFromSharedStore({ exportJobId: "job-bad", tenantId: "t" }); expect(snapshot).toBeNull(); }); it("normalises missing optional fields", async () => { const { getSharedStore } = await import("./shared-store.js"); await getSharedStore().set( "export:progress:job-partial", JSON.stringify({ processedRows: 10 }), { ttlSeconds: 60 }, ); const snapshot = await readExportProgressFromSharedStore({ exportJobId: "job-partial", tenantId: "t" }); expect(snapshot).toEqual({ processedRows: 10, totalEstimate: null, updatedAt: null }); }); });