import { afterEach, expect, it, vi } from "vitest"; import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceScope.js"; import { CliJavaScriptAnalysisHost } from "../../../src/cli/javascriptApplicationAnalysis.js"; import { loadCliRuntime } from "../../../src/cliLoader.js"; import { parseCliGlobalArguments } from "../../../src/cliBuiltinArguments.js"; const originalClose = ArtifactResourceScope.prototype.close; const originalHostClose = CliJavaScriptAnalysisHost.prototype.close; afterEach(() => { ArtifactResourceScope.prototype.close = originalClose; vi.restoreAllMocks(); }); it.each(["selected", "full"] as const)( "retains the %s runtime's invocation owner across failed outer cleanup", async (route) => { const failure = new Error("fixture cleanup denied"); let denied = true; let attempts = 0; let release = (): void => {}; const gate = new Promise((resolve) => { release = resolve; }); let invocation: CliJavaScriptAnalysisHost | undefined; let retained: ArtifactResourceScope | undefined; const hosts = vi .spyOn(CliJavaScriptAnalysisHost.prototype, "close") .mockImplementation(function (this: CliJavaScriptAnalysisHost) { if (this.callerLifetime === "invocation" && invocation === undefined) { invocation = this; retained = this.scope(); retained.addCloseHook(async () => { attempts += 1; await gate; if (denied) throw failure; }); } return originalHostClose.call(this); }); const runtime = await loadCliRuntime( route === "full" ? undefined : parseCliGlobalArguments([ "analyze-javascript-application", "fixture", "--json", ]), {}, undefined, { finalizeJavaScriptAnalysis: true }, ); try { const first = runtime.closeCliResources(); const rejected = expect(first).rejects.toBe(failure); expect(runtime.closeCliResources()).toBe(first); release(); await rejected; expect(invocation).toBeDefined(); expect(invocation?.scope()).toBe(retained); expect(attempts).toBe(1); await expect(runtime.closeCliResources()).rejects.toBe(failure); expect(attempts).toBe(2); denied = false; await runtime.closeCliResources(); expect(attempts).toBe(3); expect(invocation?.scope()).not.toBe(retained); } finally { release(); denied = false; await runtime.closeCliResources(); hosts.mockRestore(); } }, ); it("does not share a JavaScript worker scope or its in-flight close", async () => { let release = (): void => {}; const gate = new Promise((resolve) => { release = resolve; }); ArtifactResourceScope.prototype.close = function ( this: ArtifactResourceScope, ) { return gate.then(() => originalClose.call(this)); }; const first = new CliJavaScriptAnalysisHost(); const second = new CliJavaScriptAnalysisHost(); let firstClose: Promise | undefined; let secondClose: Promise | undefined; const firstScope = first.scope(); const secondScope = second.scope(); try { expect(firstScope).not.toBe(secondScope); firstClose = first.close(); expect(first.close()).toBe(firstClose); secondClose = second.close(); expect(secondClose).not.toBe(firstClose); } finally { release(); await Promise.allSettled([firstClose, secondClose]); } expect(first.scope()).not.toBe(firstScope); expect(second.scope()).not.toBe(secondScope); });