import { describe, expect, it, vi } from "vitest"; import { executeOnce, useTestExecution } from "./execution"; describe("shared test execution", () => { it("shares a pending run across entry points and releases it after completion", async () => { let resolve!: (value: string) => void; const task = vi.fn(() => new Promise((r) => { resolve = r; })); const first = executeOnce("case-1", task); const second = executeOnce("case-1", task); expect(first).toBe(second); expect(useTestExecution.getState().running.has("case-1")).toBe(true); await Promise.resolve(); expect(task).toHaveBeenCalledTimes(1); resolve("report"); await expect(first).resolves.toBe("report"); expect(useTestExecution.getState().running.has("case-1")).toBe(false); await expect(executeOnce("case-1", async () => "new report")).resolves.toBe("new report"); }); it("clears failed runs and allows retry without clearing other active cases", async () => { let resolve!: () => void; const other = executeOnce("other", () => new Promise((r) => { resolve = r; })); await expect(executeOnce("failed", () => { throw new Error("transport unavailable"); })) .rejects.toThrow("transport unavailable"); expect([...useTestExecution.getState().running]).toEqual(["other"]); await expect(executeOnce("failed", async () => "retry")).resolves.toBe("retry"); resolve(); await other; expect(useTestExecution.getState().running.size).toBe(0); }); });