diff --git a/tests/composition/analysis-sessions/binarySessionCleanupRetry.test.ts b/tests/composition/analysis-sessions/binarySessionCleanupRetry.test.ts index a8c26d003b..94ef67ddbc 100644 --- a/tests/composition/analysis-sessions/binarySessionCleanupRetry.test.ts +++ b/tests/composition/analysis-sessions/binarySessionCleanupRetry.test.ts @@ -296,75 +296,60 @@ it("offers a close retry after a saved snapshot but not after a failed save", as expect(await session.status()).toMatchObject({ cleanup_pending: false }); }); -it("keeps a failed candidate for cleanup and preserves its startup failure", async () => { - const primary = new HopperStartError(); - const fixture = await retryFixture(() => Promise.resolve(err(primary))); - const result = await fixture.session.open(fixture.targets[0]); - expect(result).toMatchObject({ - ok: false, - error: { +it.each(["provider failure", "caller cancellation"] as const)( + "retains the failed startup candidate and its %s through cleanup retry", + async (outcome) => { + const controller = new AbortController(); + const reason = { stage: "startup health" }; + const startupFailure = new HopperStartError(); + const fixture = await retryFixture(async () => { + if (outcome === "provider failure") return err(startupFailure); + controller.abort(reason); + return ok(null); + }); + const result = await fixture.session.open(fixture.targets[0], { + signal: controller.signal, + }); + if (result.ok) throw new Error("Expected startup to fail"); + expect(result.error).toMatchObject({ cleanupIncomplete: true, cleanupResources: [fixture.runtime], - diagnostics: { - primary_error: projectAnalysisError(primary), - cleanup_error: projectAnalysisError(fixture.cleanupError), + diagnostics: + outcome === "provider failure" + ? { + primary_error: projectAnalysisError(startupFailure), + cleanup_error: projectAnalysisError(fixture.cleanupError), + } + : { primary_error: { code: "cancelled" } }, + }); + if (outcome === "caller cancellation") { + expect(result.error.cleanupPrimary).toBeInstanceOf( + AnalysisCancelledError, + ); + expect(result.error.cleanupPrimary?.cause).toBe(reason); + } + expect(fixture.session.activeTarget()).toBeUndefined(); + expect(await fixture.session.status()).toMatchObject({ + open: false, + cleanup_pending: true, + analysis_run: { + run_id: fixture.runIds[0], + process_lineage: { status: "snapshots" }, }, - }, - }); - expect(fixture.session.activeTarget()).toBeUndefined(); - expect(await fixture.session.status()).toMatchObject({ - open: false, - cleanup_pending: true, - analysis_run: { - run_id: fixture.runIds[0], - process_lineage: { status: "snapshots" }, - }, - }); - fixture.allowCleanup(); - expect((await fixture.session.close()).ok).toBe(true); - await expect(access(fixture.runtime)).rejects.toMatchObject({ - code: "ENOENT", - }); -}); + }); -it("retains cancelled startup cleanup and the cancellation reason", async () => { - const controller = new AbortController(); - const reason = { stage: "startup health" }; - const fixture = await retryFixture(() => { - controller.abort(reason); - return Promise.resolve(ok(null)); - }); - const result = await fixture.session.open(fixture.targets[0], { - signal: controller.signal, - }); - expect(result).toMatchObject({ - ok: false, - error: { - cleanupIncomplete: true, - cleanupResources: [fixture.runtime], - diagnostics: { primary_error: { code: "cancelled" } }, - }, - }); - if (result.ok) throw new Error("Expected startup cancellation"); - expect(result.error.cleanupPrimary).toBeInstanceOf(AnalysisCancelledError); - expect(result.error.cleanupPrimary?.cause).toBe(reason); - expect(await fixture.session.status()).toMatchObject({ - open: false, - cleanup_pending: true, - analysis_run: { - run_id: fixture.runIds[0], - process_lineage: { status: "snapshots" }, - }, - }); - fixture.allowCleanup(); - expect((await fixture.session.close()).ok).toBe(true); - await expect(access(fixture.runtime)).rejects.toMatchObject({ - code: "ENOENT", - }); - expect(result.error.cleanupIncomplete).toBe(false); - expect(result.error.cleanupPrimary?.cause).toBe(reason); - expect(projectAnalysisError(result.error).code).toBe("cancelled"); -}); + fixture.allowCleanup(); + expect((await fixture.session.close()).ok).toBe(true); + await expect(access(fixture.runtime)).rejects.toMatchObject({ + code: "ENOENT", + }); + expect(result.error.cleanupIncomplete).toBe(false); + if (outcome === "caller cancellation") { + expect(result.error.cleanupPrimary?.cause).toBe(reason); + expect(projectAnalysisError(result.error).code).toBe("cancelled"); + } + }, +); it("keeps cancellation as the primary when previous-client retirement fails after abort", async () => { const controller = new AbortController(); @@ -394,37 +379,35 @@ it("keeps cancellation as the primary when previous-client retirement fails afte expect(result.error.cleanupPrimary?.cause).toBe(reason); }); -it("preserves caller-selected document retention when retrying cleanup", async () => { - const fixture = await retryFixture(); - expect((await fixture.session.open(fixture.targets[0])).ok).toBe(true); - expect( - await fixture.session.close({ retainProviderDocuments: true }), - ).toEqual(err(fixture.cleanupError)); - fixture.allowCleanup(); - expect((await fixture.session.close()).ok).toBe(true); - await expect(access(fixture.runtime)).rejects.toMatchObject({ - code: "ENOENT", - }); - expect(await readFile(fixture.document, "utf8")).toBe( - "caller-retained document", - ); -}); - -it("honors an explicit retention choice on a cleanup retry", async () => { - const fixture = await retryFixture(); - expect((await fixture.session.open(fixture.targets[0])).ok).toBe(true); - expect(await fixture.session.close()).toEqual(err(fixture.cleanupError)); - fixture.allowCleanup(); - expect( - (await fixture.session.close({ retainProviderDocuments: true })).ok, - ).toBe(true); - await expect(access(fixture.runtime)).rejects.toMatchObject({ - code: "ENOENT", - }); - expect(await readFile(fixture.document, "utf8")).toBe( - "caller-retained document", - ); -}); +it.each([ + { initial: true, retry: undefined, retained: true }, + { initial: undefined, retry: true, retained: true }, + { initial: true, retry: false, retained: false }, +])( + "applies the document retention choice across cleanup attempts: %j", + async ({ initial, retry, retained }) => { + const fixture = await retryFixture(); + const close = (choice: boolean | undefined) => + fixture.session.close( + choice === undefined ? {} : { retainProviderDocuments: choice }, + ); + expect((await fixture.session.open(fixture.targets[0])).ok).toBe(true); + expect(await close(initial)).toEqual(err(fixture.cleanupError)); + fixture.allowCleanup(); + expect((await close(retry)).ok).toBe(true); + await expect(access(fixture.runtime)).rejects.toMatchObject({ + code: "ENOENT", + }); + if (retained) + expect(await readFile(fixture.document, "utf8")).toBe( + "caller-retained document", + ); + else + await expect(access(fixture.document)).rejects.toMatchObject({ + code: "ENOENT", + }); + }, +); it("keeps failed restoration resources available for a close retry", async () => { const parent = await createTestTempDirectory("rea-restore-cleanup-retry-"); diff --git a/tests/composition/directAnalysisFactories.test.ts b/tests/composition/directAnalysisFactories.test.ts index 8cb6204aeb..5d097e3622 100644 --- a/tests/composition/directAnalysisFactories.test.ts +++ b/tests/composition/directAnalysisFactories.test.ts @@ -25,12 +25,18 @@ import { createTestBinarySession, } from "../fixtures/binarySession.js"; +type SessionFactory = Exclude< + keyof DirectAnalysisDependencies, + "readConfiguration" +>; + const recordingFactories = ( failure?: Error, profile?: ReturnType, ) => { const sessions: BinarySession[] = []; let clientsClosed = 0; + const selectedFactories: SessionFactory[] = []; const createSession = () => { const session = createTestBinarySession( () => ({ @@ -54,15 +60,26 @@ const recordingFactories = ( sessions.push(session); return session; }; + const select = (name: SessionFactory) => () => { + selectedFactories.push(name); + return createSession(); + }; const dependencies: DirectAnalysisDependencies = { readConfiguration: () => parseConfig({}), - createBinarySession: createSession, - createArtifactBinarySession: createSession, - createNativeDispatchBinarySession: createSession, - createNativeBinarySession: createSession, - createManagedBinarySession: createSession, + createBinarySession: select("createBinarySession"), + createArtifactBinarySession: select("createArtifactBinarySession"), + createNativeDispatchBinarySession: select( + "createNativeDispatchBinarySession", + ), + createNativeBinarySession: select("createNativeBinarySession"), + createManagedBinarySession: select("createManagedBinarySession"), + }; + return { + dependencies, + sessions, + selectedFactories, + clientsClosed: () => clientsClosed, }; - return { dependencies, sessions, clientsClosed: () => clientsClosed }; }; /** Schema-valid read_bytes input so lifecycle tests reach the session. */ @@ -181,67 +198,63 @@ describe("one-shot analysis factory boundary", () => { expect(factories.sessions).toEqual([]); }, ); - - it.each([ - "inspect_macho", - "inspect_signature", - "list_architectures", - "inspect_plist", - "demangle_swift", - ] satisfies readonly NativeToolName[])( - "runs %s without reading deep-engine configuration or constructing its session", - async (tool) => { - const [path] = await createBinarySessionTargets(); - const factories = recordingFactories(); - const unexpected = (): never => { - throw new Error("unrelated session factory must stay unused"); - }; - const arguments_ = - tool === "demangle_swift" ? { symbols: ["$s4Test3fooyyF"] } : {}; - const result = await runProviderAnalysis( - { - ...factories.dependencies, - readConfiguration: unexpected, - createBinarySession: unexpected, - createArtifactBinarySession: unexpected, - createManagedBinarySession: unexpected, - }, - path, - tool, - arguments_, - ).then(projectCliAnalysisResult); - expect(result).toMatchObject({ - operation: tool, - normalized_result: { operation: tool }, - }); - expect(factories.clientsClosed()).toBe(1); - expect(factories.sessions[0]?.activeTarget()).toBeUndefined(); - }, - ); }); it.each([ - "observe_native_ui", - "capture_native_ui_scenario", - "observe_native_calls", -] satisfies readonly NativeToolName[])( - "keeps %s on the ordinary session route", - async (tool) => { - const [path] = await createBinarySessionTargets(); - const factories = recordingFactories(); - const result = await runProviderAnalysis( - { - ...factories.dependencies, - createNativeBinarySession: () => { - throw new Error("static inspection factory must stay unused"); - }, - }, - path, + { + factory: "createNativeBinarySession", + needsConfiguration: false, + cases: ( + [ + "inspect_macho", + "inspect_signature", + "list_architectures", + "inspect_plist", + "demangle_swift", + ] satisfies readonly NativeToolName[] + ).map((tool) => ({ tool, - admittedNativeSessionInput[tool], - ).then(projectCliAnalysisResult); - expect(result).toMatchObject({ operation: tool }); - expect(factories.clientsClosed()).toBe(1); + input: tool === "demangle_swift" ? { symbols: ["$s4Test3fooyyF"] } : {}, + })), + }, + { + factory: "createBinarySession", + needsConfiguration: true, + cases: ( + [ + "observe_native_ui", + "capture_native_ui_scenario", + "observe_native_calls", + ] as const + ).map((tool) => ({ tool, input: admittedNativeSessionInput[tool] })), + }, +])( + "routes one-shot native operations through $factory", + async ({ factory, needsConfiguration, cases }) => { + const [path] = await createBinarySessionTargets(); + const fixtures = recordingFactories(); + const readConfiguration = vi.fn(fixtures.dependencies.readConfiguration); + const dependencies = { ...fixtures.dependencies, readConfiguration }; + for (const { tool, input } of cases) { + const result = await runProviderAnalysis( + dependencies, + path, + tool, + input, + ).then(projectCliAnalysisResult); + expect(result, tool).toMatchObject({ + operation: tool, + normalized_result: { operation: tool }, + }); + expect(fixtures.selectedFactories.at(-1), tool).toBe(factory); + } + expect(fixtures.clientsClosed()).toBe(cases.length); + expect(readConfiguration).toHaveBeenCalledTimes( + needsConfiguration ? cases.length : 0, + ); + expect(fixtures.sessions.map((session) => session.activeTarget())).toEqual( + cases.map(() => undefined), + ); }, );