import assert from "node:assert/strict"; import { it } from "vitest"; import { lazy } from "../lazy.ts"; it("serializes different loads, shares a target, and continues after failure", async () => { let releaseFirst!: () => void; const firstFinished = new Promise((resolve) => { releaseFirst = resolve; }); const calls: string[] = []; const first = lazy(async () => { calls.push("first"); await firstFinished; throw new Error("failed"); }); const second = lazy(async () => { calls.push("second"); return 2; }); const firstAttempt = first(); assert.equal(first(), firstAttempt); const secondAttempt = second(); await Promise.resolve(); assert.deepEqual(calls, ["first"]); releaseFirst(); await assert.rejects(firstAttempt, /failed/); assert.equal(await secondAttempt, 2); assert.deepEqual(calls, ["first", "second"]); await assert.rejects(first(), /failed/); assert.deepEqual(calls, ["first", "second", "first"]); });