import { describe, expect, it, vi } from "vitest"; import { runSequentialUploads } from "@/domain/work-orders/utils/run-sequential-uploads"; describe("runSequentialUploads", () => { it("keeps pending true across the full batch; second upload starts only after the first settles", async () => { const pendingLog: boolean[] = []; const setPending = (pending: boolean) => { pendingLog.push(pending); }; let resolveFirst: (() => void) | undefined; let resolveSecond: (() => void) | undefined; const first = new Promise((resolve) => { resolveFirst = resolve; }); const second = new Promise((resolve) => { resolveSecond = resolve; }); const upload = vi .fn() .mockImplementationOnce(() => first) .mockImplementationOnce(() => second); const done = runSequentialUploads(["large.bin", "small.bin"], upload, setPending); expect(pendingLog).toEqual([true]); expect(upload).toHaveBeenCalledTimes(1); expect(upload).toHaveBeenCalledWith("large.bin"); // First file still in flight — pending must stay true. await Promise.resolve(); expect(pendingLog).toEqual([true]); expect(upload).toHaveBeenCalledTimes(1); resolveFirst?.(); await Promise.resolve(); await Promise.resolve(); expect(upload).toHaveBeenCalledTimes(2); expect(upload).toHaveBeenNthCalledWith(2, "small.bin"); expect(pendingLog).toEqual([true]); resolveSecond?.(); await done; expect(pendingLog).toEqual([true, false]); }); it("clears pending when a middle upload rejects", async () => { const setPending = vi.fn(); const upload = vi .fn() .mockResolvedValueOnce(undefined) .mockRejectedValueOnce(new Error("upload failed")); await expect(runSequentialUploads(["a", "b", "c"], upload, setPending)).rejects.toThrow( "upload failed", ); expect(setPending.mock.calls.map((call) => call[0])).toEqual([true, false]); expect(upload).toHaveBeenCalledTimes(2); }); });