From 385e5d228f75894e616548371ceb3fccdb6e499b Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:22:53 -0500 Subject: [PATCH] test: own temporary directories through createTestTempDirectory in remaining tests/ (#12727) * test: own temporary directories through createTestTempDirectory in remaining tests/ Give per-test directories in the remaining tests/ suites to createTestTempDirectory so onTestFinished removes them through removeTestWorkspace. Keep suite-scoped roots and child-process directories whose removal is the assertion. * test: drop empty cleanup callbacks and keep the capture skip reason visible Remove the no-op withVerifierCleanup steps left after directory removal moved to createTestTempDirectory. Put the native capture skip explanation in the Linux sample test name, because it.skipIf only tests truthiness. Co-authored-by: morluto --------- Co-authored-by: Cursor Agent Co-authored-by: morluto --- .../browser/historicalWebCapture.test.ts | 9 +- tests/boundary/cli/cliJsonCallerPath.test.ts | 7 +- tests/boundary/cli/cutterCommands.test.ts | 15 +- tests/boundary/cli/fridaCli.test.ts | 295 +++--- tests/boundary/evm/evmCleanupRetry.test.ts | 40 +- .../filesystem/adbInputSnapshot.test.ts | 4 +- .../filesystem/ciCacheRetention.test.ts | 3 +- .../macHopperInstallOwnership.test.ts | 12 +- ....01-lists-the-complete-target-open.test.ts | 10 +- tests/boundary/mcp/fridaLifecycleMcp.test.ts | 4 +- tests/boundary/mcp/gdbCancellationMcp.test.ts | 28 +- .../mcp/gdbExecutableSelection.test.ts | 9 +- .../mcp/ghidraProfileRefusalMcp.test.ts | 4 +- ...investigationEvidenceReferencesMcp.test.ts | 4 +- .../mcp/processCleanupRetryMcp.test.ts | 9 +- ...reverseDebuggerMcpHarnessLifecycle.test.ts | 3 +- .../reverseEngineeringEnvironmentMcp.test.ts | 9 +- ...eEngineeringProviderEnvironmentMcp.test.ts | 91 +- .../mcp/rizinDebuggerCancellationMcp.test.ts | 7 +- .../mcp/toolErrorSchemaDelivery.test.ts | 4 +- .../modelArtifactPartialCancellation.test.ts | 10 +- .../native/pwntoolsArtifactCleanup.test.ts | 16 +- .../process/adbVerifierCleanup.test.ts | 11 +- .../artifactVerifierCleanupOwner.test.ts | 32 +- .../bunVerifierOwnerInitialization.test.ts | 114 +-- .../claudeVerifierSetupTargets.test.ts | 108 +- .../process/cliEnvironmentSeam.test.ts | 67 +- .../clientVerifierSetupTargets.test.ts | 93 +- .../commandcodeVerifierSkillBody.test.ts | 133 ++- .../darwinProcessRunTokenReader.test.ts | 320 +++--- .../process/deepseekVerifierSkillBody.test.ts | 109 +- .../process/geminiVerifierSkillBody.test.ts | 103 +- .../process/il2cppVerifierRetention.test.ts | 105 +- .../process/nativeUiVerifierActions.test.ts | 95 +- .../processCaptureNormalization.test.ts | 15 +- .../processCleanupRetryOwnership.test.ts | 11 +- .../processSnapshotHandleOwnership.test.ts | 8 +- .../process/qwenVerifierSkillBody.test.ts | 107 +- .../process/selectedExecutableCleanup.test.ts | 8 +- tests/composition/reverseEngineering.test.ts | 949 ++++++++---------- .../reverseEngineeringAdmission.test.ts | 24 +- .../reverseEngineeringCancellation.test.ts | 53 +- .../conformanceFixtureVerifier.test.ts | 10 +- .../ghidra/checkpointRefusal.test.ts | 4 +- .../analysisEfficiencyComparison.test.ts | 103 +- ...cessObservationCommandFinalization.test.ts | 50 +- 46 files changed, 1487 insertions(+), 1738 deletions(-) diff --git a/tests/boundary/browser/historicalWebCapture.test.ts b/tests/boundary/browser/historicalWebCapture.test.ts index 66e02156a3..8f0a5fe952 100644 --- a/tests/boundary/browser/historicalWebCapture.test.ts +++ b/tests/boundary/browser/historicalWebCapture.test.ts @@ -286,8 +286,7 @@ it("excludes declared binary lengths through CLI, MCP and retained capture pages }); it("reads actual upstream HAR packages in an owned process and retains selected producer order", async () => { - const root = await mkdtemp(join(tmpdir(), "rea-history-har-")); - onTestFinished(() => rm(root, { recursive: true, force: true })); + const root = await createTestTempDirectory("rea-history-har-"); const fixture = historicalHar(); const first = fixture.log.entries[0]; if (first === undefined) throw new Error("fixture missing"); @@ -314,8 +313,7 @@ it("reads actual upstream HAR packages in an owned process and retains selected }); it("keeps malformed JSON separate from absent external mitmproxy capability", async () => { - const root = await mkdtemp(join(tmpdir(), "rea-history-errors-")); - onTestFinished(() => rm(root, { recursive: true, force: true })); + const root = await createTestTempDirectory("rea-history-errors-"); const path = join(root, "capture.har"); await writeFile(path, "{malformed-private-json"); const malformed = await cli(path, "har"); @@ -333,8 +331,7 @@ it("keeps malformed JSON separate from absent external mitmproxy capability", as }); it("reports the actual HAR decoder nesting constraint as a typed selected-input issue", async () => { - const root = await mkdtemp(join(tmpdir(), "rea-history-depth-")); - onTestFinished(() => rm(root, { recursive: true, force: true })); + const root = await createTestTempDirectory("rea-history-depth-"); let extension: unknown = "leaf"; for (let depth = 0; depth < 66; depth++) extension = { child: extension }; const path = join(root, "deep.har"); diff --git a/tests/boundary/cli/cliJsonCallerPath.test.ts b/tests/boundary/cli/cliJsonCallerPath.test.ts index 528953b0ff..19d7a9202d 100644 --- a/tests/boundary/cli/cliJsonCallerPath.test.ts +++ b/tests/boundary/cli/cliJsonCallerPath.test.ts @@ -27,6 +27,7 @@ import { analysisViewJavaScriptEvidence, analysisViewLayoutEvidence, } from "../../fixtures/analysisView.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const providerIdentity = { id: "test.path-boundary", @@ -53,7 +54,7 @@ const serveJson = async ( it.skipIf(process.platform === "win32")( "keeps caller-selected JSON paths through the public CLI and provider boundaries", async () => { - const root = await mkdtemp(join(tmpdir(), "rea-cli-json-path-")); + const root = await createTestTempDirectory("rea-cli-json-path-"); const originalCwd = process.cwd(); const originalExitCode = process.exitCode; try { @@ -167,7 +168,6 @@ it.skipIf(process.platform === "win32")( } finally { process.chdir(originalCwd); process.exitCode = originalExitCode; - await rm(root, { recursive: true, force: true }); } }, ); @@ -175,7 +175,7 @@ it.skipIf(process.platform === "win32")( it.skipIf(process.platform === "win32")( "reads the caller-selected Evidence file through the registered CLI", async () => { - const root = await mkdtemp(join(tmpdir(), "rea-cli-evidence-path-")); + const root = await createTestTempDirectory("rea-cli-evidence-path-"); const originalCwd = process.cwd(); const originalExitCode = process.exitCode; try { @@ -220,7 +220,6 @@ it.skipIf(process.platform === "win32")( } finally { process.chdir(originalCwd); process.exitCode = originalExitCode; - await rm(root, { recursive: true, force: true }); } }, ); diff --git a/tests/boundary/cli/cutterCommands.test.ts b/tests/boundary/cli/cutterCommands.test.ts index 7db9c0d5ae..9a1547db23 100644 --- a/tests/boundary/cli/cutterCommands.test.ts +++ b/tests/boundary/cli/cutterCommands.test.ts @@ -12,6 +12,7 @@ import { spawnOwnedProviderProcess, ProviderProcessSupervisor, } from "../../../src/process/ProviderProcess.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const SESSION_ID = "27d3e3f1-f1e5-49ae-91ec-95af1f343a5a"; const listen = async ( @@ -31,14 +32,11 @@ describe("Cutter CLI commands", () => { afterEach(async () => { if (server !== undefined) await new Promise((resolve) => server?.close(() => resolve())); - if (directory !== undefined) - await rm(directory, { recursive: true, force: true }); - directory = undefined; server = undefined; }); it("lists sessions from the selected bridge directory", async () => { - directory = await mkdtemp(join(tmpdir(), "rea-cutter-cli-test-")); + directory = await createTestTempDirectory("rea-cutter-cli-test-"); const result = await serveCli(["list-cutter-sessions", "--json"], { REA_CUTTER_BRIDGE_DIR: directory, @@ -59,7 +57,7 @@ describe("Cutter CLI commands", () => { it.skipIf(process.platform === "win32")( "awaits discovery cancellation on SIGINT and returns its typed outcome with exit130", async () => { - directory = await mkdtemp(join(tmpdir(), "rea-cutter-cli-cancel-")); + directory = await createTestTempDirectory("rea-cutter-cli-cancel-"); const sockets = new Set(); let accepted = 0; server = createServer((socket) => { @@ -125,7 +123,7 @@ describe("Cutter CLI commands", () => { ); it("reports an unavailable selected session for a command", async () => { - directory = await mkdtemp(join(tmpdir(), "rea-cutter-cli-test-")); + directory = await createTestTempDirectory("rea-cutter-cli-test-"); const result = await serveCli( ["cutter-command", SESSION_ID, "0", "pdi", "--json"], @@ -157,14 +155,11 @@ describe("Cutter CLI command output flags", () => { afterEach(async () => { if (server !== undefined) await new Promise((resolve) => server?.close(() => resolve())); - if (directory !== undefined) - await rm(directory, { recursive: true, force: true }); - directory = undefined; server = undefined; }); it("returns Evidence when the selected Cutter session completes a command", async () => { - directory = await mkdtemp(join(tmpdir(), "rea-cutter-cli-test-")); + directory = await createTestTempDirectory("rea-cutter-cli-test-"); const token = "synthetic-cutter-cli-token-for-test"; let nextConnectionOrdinal = 0; const commandJsonModes: boolean[] = []; diff --git a/tests/boundary/cli/fridaCli.test.ts b/tests/boundary/cli/fridaCli.test.ts index fbd769b48c..ed9818f84e 100644 --- a/tests/boundary/cli/fridaCli.test.ts +++ b/tests/boundary/cli/fridaCli.test.ts @@ -10,6 +10,7 @@ import { FRIDA_TOOL_CONTRACTS } from "../../../src/contracts/fridaToolContracts. import type { FridaInstrumentationPort } from "../../../src/ports/frida/FridaInstrumentationPort.js"; import { AnalysisCapabilityUnavailableError } from "../../../src/domain/analysisErrorCore.js"; import { FridaInstrumentationManager } from "../../../src/frida/FridaInstrumentationManager.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; it.each([ { @@ -414,7 +415,7 @@ it("forwards every selected spawn argument through the CLI and shared service", }); it("keeps Frida script-file identity in original bytes when UTF-8 decoding replaces invalid bytes", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-frida-script-identity-")); + const directory = await createTestTempDirectory("rea-frida-script-identity-"); const files = [ { name: "ff.js", @@ -436,118 +437,114 @@ it("keeps Frida script-file identity in original bytes when UTF-8 decoding repla bytes: Buffer.from("// ordinary ASCII\nsend(1);", "ascii"), }, ]; - try { - await Promise.all( - files.map(({ name, bytes }) => writeFile(join(directory, name), bytes)), - ); - const loadedSources: string[] = []; - const deviceManager = { - async getDeviceById() { - return { - id: "local", - name: "Fixture device", - type: "local", - async getProcessByPid(pid: number) { - return { pid, name: "fixture" }; - }, - async attach() { - let detached = false; - return { - pid: 71, - detached: { connect() {}, disconnect() {} }, - isDetached: () => detached, - async detach() { - detached = true; - }, - async createScript(source: string) { - loadedSources.push(source); - return { - message: { connect() {}, disconnect() {} }, - async load() {}, - async unload() {}, - }; - }, - }; - }, - }; - }, - } as unknown as DeviceManager; + await Promise.all( + files.map(({ name, bytes }) => writeFile(join(directory, name), bytes)), + ); + const loadedSources: string[] = []; + const deviceManager = { + async getDeviceById() { + return { + id: "local", + name: "Fixture device", + type: "local", + async getProcessByPid(pid: number) { + return { pid, name: "fixture" }; + }, + async attach() { + let detached = false; + return { + pid: 71, + detached: { connect() {}, disconnect() {} }, + isDetached: () => detached, + async detach() { + detached = true; + }, + async createScript(source: string) { + loadedSources.push(source); + return { + message: { connect() {}, disconnect() {} }, + async load() {}, + async unload() {}, + }; + }, + }; + }, + }; + }, + } as unknown as DeviceManager; - for (const { name } of files) { - // The CLI resolves a relative script file against its working - // directory; the contract admits only the absolute path. A Windows - // temporary directory on another drive than the checkout has no - // relative spelling, so only there the absolute path is passed. - const relativeSpelling = relative(process.cwd(), join(directory, name)); - if (name === "ascii.js" && process.platform !== "win32") - expect(isAbsolute(relativeSpelling)).toBe(false); - const scriptFile = - name === "ascii.js" && !isAbsolute(relativeSpelling) - ? relativeSpelling - : join(directory, name); - const provider = new FridaInstrumentationManager({ deviceManager }); - let stdout = ""; - let exitCode = 0; - await createCli({}, undefined, provider).serve( - [ - "instrument-with-frida", - "attach", - "71", - "--script-file", - scriptFile, - "--duration-ms", - "0", - "--json", - ], - { - env: {}, - exit: (code) => { - exitCode = code; - }, - stdout: (text) => { - stdout += text; + for (const { name } of files) { + // The CLI resolves a relative script file against its working + // directory; the contract admits only the absolute path. A Windows + // temporary directory on another drive than the checkout has no + // relative spelling, so only there the absolute path is passed. + const relativeSpelling = relative(process.cwd(), join(directory, name)); + if (name === "ascii.js" && process.platform !== "win32") + expect(isAbsolute(relativeSpelling)).toBe(false); + const scriptFile = + name === "ascii.js" && !isAbsolute(relativeSpelling) + ? relativeSpelling + : join(directory, name); + const provider = new FridaInstrumentationManager({ deviceManager }); + let stdout = ""; + let exitCode = 0; + await createCli({}, undefined, provider).serve( + [ + "instrument-with-frida", + "attach", + "71", + "--script-file", + scriptFile, + "--duration-ms", + "0", + "--json", + ], + { + env: {}, + exit: (code) => { + exitCode = code; + }, + stdout: (text) => { + stdout += text; + }, + }, + ); + expect(exitCode).toBe(0); + const output = JSON.parse(stdout) as { + readonly normalized_result: { readonly source_path: string }; + }; + expect(isAbsolute(output.normalized_result.source_path)).toBe(true); + const bytes = files.find((file) => file.name === name)?.bytes; + if (bytes === undefined) throw new Error(`Missing fixture ${name}`); + const decodedSource = bytes.toString("utf8"); + expect(loadedSources.at(-1)).toBe(decodedSource); + const rawDigest = createHash("sha256").update(bytes).digest("hex"); + const loadedSourceDigest = createHash("sha256") + .update(decodedSource, "utf8") + .digest("hex"); + const outputText = JSON.stringify(output); + if (name === "ff.js" || name === "fe.js") { + expect(rawDigest).not.toBe(loadedSourceDigest); + expect(output).toMatchObject({ + normalized_result: { source_sha256: rawDigest }, + raw_result: { + source_representation: { + encoding: "utf8", + decoding: "replacement", + loaded_source_sha256: loadedSourceDigest, }, }, - ); - expect(exitCode).toBe(0); - const output = JSON.parse(stdout) as { - readonly normalized_result: { readonly source_path: string }; - }; - expect(isAbsolute(output.normalized_result.source_path)).toBe(true); - const bytes = files.find((file) => file.name === name)?.bytes; - if (bytes === undefined) throw new Error(`Missing fixture ${name}`); - const decodedSource = bytes.toString("utf8"); - expect(loadedSources.at(-1)).toBe(decodedSource); - const rawDigest = createHash("sha256").update(bytes).digest("hex"); - const loadedSourceDigest = createHash("sha256") - .update(decodedSource, "utf8") - .digest("hex"); - const outputText = JSON.stringify(output); - if (name === "ff.js" || name === "fe.js") { - expect(rawDigest).not.toBe(loadedSourceDigest); - expect(output).toMatchObject({ - normalized_result: { source_sha256: rawDigest }, - raw_result: { - source_representation: { - encoding: "utf8", - decoding: "replacement", - loaded_source_sha256: loadedSourceDigest, - }, - }, - limitations: expect.arrayContaining([ - expect.stringMatching(/replacement|original bytes/iu), - ]), - }); - } else { - expect(output).toMatchObject({ - normalized_result: { source_sha256: rawDigest }, - }); - expect(outputText).not.toContain("source_representation"); - } - await provider.closeAll(); + limitations: expect.arrayContaining([ + expect.stringMatching(/replacement|original bytes/iu), + ]), + }); + } else { + expect(output).toMatchObject({ + normalized_result: { source_sha256: rawDigest }, + }); + expect(outputText).not.toContain("source_representation"); } - } finally { - await rm(directory, { recursive: true, force: true }); + await provider.closeAll(); } }); @@ -705,53 +702,43 @@ it.each([ ); it("advances each Frida script source to the attach target error without attaching or spawning", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-frida-script-source-")); + const directory = await createTestTempDirectory("rea-frida-script-source-"); const scriptPath = join(directory, "source.js"); - try { - await writeFile(scriptPath, "send(1);\n"); - for (const scriptArguments of [ - ["--script", "send(1)"], - ["--script-file", scriptPath], - ]) { - let starts = 0; - const provider = { - async startSession() { - starts += 1; - throw new Error("Frida must not attach or spawn for an invalid PID"); + await writeFile(scriptPath, "send(1);\n"); + for (const scriptArguments of [ + ["--script", "send(1)"], + ["--script-file", scriptPath], + ]) { + let starts = 0; + const provider = { + async startSession() { + starts += 1; + throw new Error("Frida must not attach or spawn for an invalid PID"); + }, + } as unknown as FridaInstrumentationPort; + let stdout = ""; + await createCli({}, undefined, provider).serve( + ["instrument-with-frida", "attach", "abc", ...scriptArguments, "--json"], + { + env: {}, + exit: () => undefined, + stdout: (text) => { + stdout += text; }, - } as unknown as FridaInstrumentationPort; - let stdout = ""; - await createCli({}, undefined, provider).serve( - [ - "instrument-with-frida", - "attach", - "abc", - ...scriptArguments, - "--json", - ], - { - env: {}, - exit: () => undefined, - stdout: (text) => { - stdout += text; + }, + ); + expect(JSON.parse(stdout)).toMatchObject({ + code: "invalid_request", + details: { + issues: [ + { + path: ["target"], + reason: "invalid_value", + message: "attach mode needs a positive integer process ID.", }, - }, - ); - expect(JSON.parse(stdout)).toMatchObject({ - code: "invalid_request", - details: { - issues: [ - { - path: ["target"], - reason: "invalid_value", - message: "attach mode needs a positive integer process ID.", - }, - ], - }, - }); - expect(starts).toBe(0); - } - } finally { - await rm(directory, { recursive: true, force: true }); + ], + }, + }); + expect(starts).toBe(0); } }); diff --git a/tests/boundary/evm/evmCleanupRetry.test.ts b/tests/boundary/evm/evmCleanupRetry.test.ts index 1a3db4e198..437b9bd8c8 100644 --- a/tests/boundary/evm/evmCleanupRetry.test.ts +++ b/tests/boundary/evm/evmCleanupRetry.test.ts @@ -62,6 +62,7 @@ import { projectAnalysisError } from "../../../src/domain/analysisErrorProjectio import { waitForProviderProcessReady } from "../../fixtures/providerProcess.js"; import { EvmInterfaceService } from "../../../src/application/evm/EvmInterfaceService.js"; import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; let limitsWorkspace = ""; beforeAll(async () => { @@ -132,7 +133,7 @@ it.each(["completed", "invalid", "cancelled"] as const)( it.each(["pre-aborted", "admission", "close"] as const)( "preserves opaque caller reasons through service cancellation at %s", async (stage) => { - const workspace = await mkdtemp(join(tmpdir(), "rea-evm-artifact-cancel-")); + const workspace = await createTestTempDirectory("rea-evm-artifact-cancel-"); const input = join(workspace, "selected.hex"); await writeFile(input, "0x60006000f3\n"); const createRuntime = vi.fn(async () => { @@ -181,7 +182,6 @@ it.each(["pre-aborted", "admission", "close"] as const)( closeState.onClosed = undefined; closeState.onLimits = undefined; await provider.close(); - await rm(workspace, { recursive: true, force: true }); } }, ); @@ -194,7 +194,7 @@ it.each([ ] as const)( "updates a returned cleanup error after later root release: %s", async (scenario) => { - const workspace = await mkdtemp(join(tmpdir(), "rea-evm-cleanup-retry-")); + const workspace = await createTestTempDirectory("rea-evm-cleanup-retry-"); const input = join(workspace, "selected.hex"); await writeFile(input, "0x60006000f3\n"); let rootPath = ""; @@ -318,13 +318,12 @@ it.each([ } catch { // Preserve the assertion's original failure; the workspace cleanup follows. } - await rm(workspace, { recursive: true, force: true }); } }, ); it("keeps descriptor cleanup in close aggregates until the exact file lease releases", async () => { - const workspace = await mkdtemp(join(tmpdir(), "rea-evm-descriptor-retry-")); + const workspace = await createTestTempDirectory("rea-evm-descriptor-retry-"); const input = join(workspace, "selected.hex"); await writeFile(input, "0x60006000f3\n"); closeState.path = input; @@ -372,12 +371,11 @@ it("keeps descriptor cleanup in close aggregates until the exact file lease rele } catch { // Preserve the assertion's original failure while releasing test files. } - await rm(workspace, { recursive: true, force: true }); } }); it("keeps a mixed root and reply-descriptor aggregate pending until both owners release", async () => { - const workspace = await mkdtemp(join(tmpdir(), "rea-evm-mixed-owners-")); + const workspace = await createTestTempDirectory("rea-evm-mixed-owners-"); const input = join(workspace, "selected.hex"); await writeFile(input, "0x60006000f3\n"); closeState.path = ""; @@ -462,13 +460,12 @@ it("keeps a mixed root and reply-descriptor aggregate pending until both owners } catch { // Preserve the assertion's original failure while releasing test files. } - await rm(workspace, { recursive: true, force: true }); } }); it("releases a retained process independently while a root cleanup retry remains pending", async () => { - const workspace = await mkdtemp( - join(tmpdir(), "rea-evm-process-root-retry-"), + const workspace = await createTestTempDirectory( + "rea-evm-process-root-retry-", ); const input = join(workspace, "selected.hex"); await writeFile(input, "0x60006000f3\n"); @@ -575,12 +572,11 @@ it("releases a retained process independently while a root cleanup retry remains } catch { // Preserve the assertion's original failure while releasing test files. } - await rm(workspace, { recursive: true, force: true }); } }); it("preserves an active cancellation through denied owned-group cleanup and later retry", async () => { - const workspace = await mkdtemp(join(tmpdir(), "rea-evm-cancel-process-")); + const workspace = await createTestTempDirectory("rea-evm-cancel-process-"); const input = join(workspace, "selected.hex"); await writeFile(input, "0x60006000f3\n"); const controller = new AbortController(); @@ -675,7 +671,6 @@ it("preserves an active cancellation through denied owned-group cleanup and late } catch { // Preserve the assertion's original failure while releasing test files. } - await rm(workspace, { recursive: true, force: true }); } }); @@ -691,8 +686,8 @@ it.each([ ] as const)( "retains validated reply ownership through finalization: %s", async (scenario) => { - const workspace = await mkdtemp( - join(tmpdir(), "rea-evm-reply-finalization-"), + const workspace = await createTestTempDirectory( + "rea-evm-reply-finalization-", ); const input = join(workspace, "selected.hex"); await writeFile(input, "0x60006000f3\n"); @@ -825,7 +820,6 @@ it.each([ closeState.onClosing = undefined; closeState.readFailure = undefined; await provider.close(); - await rm(workspace, { recursive: true, force: true }); } }, ); @@ -833,8 +827,8 @@ it.each([ it.each(["physical-read", "completed-late-cancel"] as const)( "retains only completed caller carrier identity: %s", async (scenario) => { - const workspace = await mkdtemp( - join(tmpdir(), "rea-evm-carrier-finalization-"), + const workspace = await createTestTempDirectory( + "rea-evm-carrier-finalization-", ); const input = join(workspace, "selected.hex"); await writeFile(input, "0x60006000f3\n"); @@ -894,7 +888,6 @@ it.each(["physical-read", "completed-late-cancel"] as const)( closeState.onClosing = undefined; closeState.readFailure = undefined; await provider.close(); - await rm(workspace, { recursive: true, force: true }); } }, ); @@ -910,7 +903,7 @@ it.each([ ] as const)( "preserves exact reserved-exit marker receipt and cleanup: %s", async (scenario) => { - const workspace = await mkdtemp(join(tmpdir(), "rea-evm-marker-receipt-")); + const workspace = await createTestTempDirectory("rea-evm-marker-receipt-"); const input = join(workspace, "selected.hex"); await writeFile(input, "0x60006000f3\n"); let markerPath = ""; @@ -1009,13 +1002,12 @@ it.each([ closeState.onClosing = undefined; closeState.readFailure = undefined; await provider.close(); - await rm(workspace, { recursive: true, force: true }); } }, ); it("keeps the selected limiter and child environment together while later inspections reconfigure", async () => { - const workspace = await mkdtemp(join(tmpdir(), "rea-evm-environment-")); + const workspace = await createTestTempDirectory("rea-evm-environment-"); const input = join(workspace, "selected.hex"); const limiter = join(workspace, "selected-limiter"); const { symlink } = await import("node:fs/promises"); @@ -1060,7 +1052,6 @@ it("keeps the selected limiter and child environment together while later inspec } finally { closeState.onLimits = undefined; await provider.close(); - await rm(workspace, { recursive: true, force: true }); } }); @@ -1071,7 +1062,7 @@ it.each([ ] as const)( "counts %s from the worker launch as cancellation: %s", async (_label, thrown, cancelled) => { - const workspace = await mkdtemp(join(tmpdir(), "rea-evm-abort-owner-")); + const workspace = await createTestTempDirectory("rea-evm-abort-owner-"); const input = join(workspace, "selected.hex"); await writeFile(input, "0x60006000f3\n"); const controller = new AbortController(); @@ -1107,7 +1098,6 @@ it.each([ } else expect(result.error).not.toBeInstanceOf(AnalysisCancelledError); } finally { await provider.close(); - await rm(workspace, { recursive: true, force: true }); } }, ); diff --git a/tests/boundary/filesystem/adbInputSnapshot.test.ts b/tests/boundary/filesystem/adbInputSnapshot.test.ts index fd8af9f1cd..00912051b0 100644 --- a/tests/boundary/filesystem/adbInputSnapshot.test.ts +++ b/tests/boundary/filesystem/adbInputSnapshot.test.ts @@ -16,6 +16,7 @@ import { AdbInputSnapshotFailure, snapshotAdbInput, } from "../../../src/adb/AdbInputSnapshot.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; vi.mock("node:fs/promises", async (importOriginal) => { const actual = await importOriginal(); @@ -27,7 +28,7 @@ it("retains and retries a source handle that fails close during admission", asyn await vi.importActual( "node:fs/promises", ); - const parent = await mkdtemp(join(tmpdir(), "rea-adb-snapshot-test-")); + const parent = await createTestTempDirectory("rea-adb-snapshot-test-"); const root = await PrivateRuntimeRoot.create({ parent }); const resources = new ArtifactResourceScope(); const sourcePath = join(parent, "selected.apk"); @@ -75,6 +76,5 @@ it("retains and retries a source handle that fails close during admission", asyn if (source !== undefined && source.fd !== -1) await source.close(); vi.restoreAllMocks(); await root.close(); - await rm(parent, { recursive: true, force: true }); } }); diff --git a/tests/boundary/filesystem/ciCacheRetention.test.ts b/tests/boundary/filesystem/ciCacheRetention.test.ts index 270a9b3c5b..7b43c7d207 100644 --- a/tests/boundary/filesystem/ciCacheRetention.test.ts +++ b/tests/boundary/filesystem/ciCacheRetention.test.ts @@ -11,6 +11,7 @@ import { selectNpmDownloadDuplicateDeletions, selectPrefixDeletions, } from "../../../scripts/ci/cache-retention.mjs"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const cache = ( id: number, @@ -532,7 +533,7 @@ describe("repeated cache ids", () => { }); it("runs retention from the command line without deleting on an unsafe prefix", async () => { - const root = await mkdtemp(join(tmpdir(), "rea-cache-retention-")); + const root = await createTestTempDirectory("rea-cache-retention-"); const log = join(root, "gh.log"); const gh = join(root, "gh"); await writeFile( diff --git a/tests/boundary/filesystem/macHopperInstallOwnership.test.ts b/tests/boundary/filesystem/macHopperInstallOwnership.test.ts index cbd60110c7..c9765690b4 100644 --- a/tests/boundary/filesystem/macHopperInstallOwnership.test.ts +++ b/tests/boundary/filesystem/macHopperInstallOwnership.test.ts @@ -20,18 +20,10 @@ import { } from "../../../src/application/MacHopper.js"; import { SetupCleanupOwner } from "../../../src/application/SetupCleanupOwner.js"; import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js"; - -const roots: string[] = []; - -afterEach(async () => { - await Promise.all( - roots.splice(0).map((root) => rm(root, { recursive: true, force: true })), - ); -}); +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const temporaryRoot = async (): Promise => { - const root = await mkdtemp(join(tmpdir(), "rea-hopper-owned-stage-test-")); - roots.push(root); + const root = await createTestTempDirectory("rea-hopper-owned-stage-test-"); return root; }; diff --git a/tests/boundary/mcp/enhancedTools.01-lists-the-complete-target-open.test.ts b/tests/boundary/mcp/enhancedTools.01-lists-the-complete-target-open.test.ts index 9976ef91a7..828a28edff 100644 --- a/tests/boundary/mcp/enhancedTools.01-lists-the-complete-target-open.test.ts +++ b/tests/boundary/mcp/enhancedTools.01-lists-the-complete-target-open.test.ts @@ -18,6 +18,7 @@ import { ok as resultOk, err } from "../../../src/domain/result.js"; import { AnalysisCapabilityUnavailableError } from "../../../src/domain/analysisErrorCore.js"; import { nativeDispatchMetadataResultSchema } from "../../../src/domain/native/objcSwiftMetadata.js"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const targetObservation = (value: unknown) => resultOk( @@ -218,16 +219,10 @@ const fixturePort = (): AnalysisOperationPort => ({ }); const resources: Array<{ close(): Promise }> = []; -let decompileOutput: string | undefined; - afterEach(async () => { await Promise.all( resources.splice(0).map(async (resource) => resource.close()), ); - if (decompileOutput !== undefined) { - await rm(decompileOutput, { recursive: true, force: true }); - decompileOutput = undefined; - } }); const connect = async (analysis: AnalysisOperationPort = fixturePort()) => { @@ -268,8 +263,7 @@ describe("enhanced MCP tools", () => { // oxlint-disable-next-line max-lines-per-function -- one exhaustive registration test guards the public tool catalog. it("executes all enhanced tools through production registration", async () => { const client = await connect(); - const outputParent = await mkdtemp(join(tmpdir(), "rea-decompile-all-")); - decompileOutput = outputParent; + const outputParent = await createTestTempDirectory("rea-decompile-all-"); const calls = [ ["get_objc_classes", { pattern: "Fixture" }], ["get_objc_protocols", {}], diff --git a/tests/boundary/mcp/fridaLifecycleMcp.test.ts b/tests/boundary/mcp/fridaLifecycleMcp.test.ts index 2586468272..07c97fdbde 100644 --- a/tests/boundary/mcp/fridaLifecycleMcp.test.ts +++ b/tests/boundary/mcp/fridaLifecycleMcp.test.ts @@ -47,6 +47,7 @@ import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { phaseGate } from "../../support/lifecycleHarness.js"; import { waitFor } from "../../support/testWait.js"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const nativeFixture = () => { const created: string[] = []; @@ -1141,8 +1142,7 @@ it("retains cancellation source facts without claiming native creation when no s it("retains original malformed file identity and decoded representation through a rejected cancelled MCP load", async () => { const { fixture, id, held } = await scriptLoadFixture(); - const directory = await mkdtemp(join(tmpdir(), "rea-frida-file-cancel-")); - onTestFinished(() => rm(directory, { recursive: true, force: true })); + const directory = await createTestTempDirectory("rea-frida-file-cancel-"); const path = join(directory, "script.js"); const bytes = Buffer.from([0x2f, 0x2f, 0xff, 0x0a]); await writeFile(path, bytes); diff --git a/tests/boundary/mcp/gdbCancellationMcp.test.ts b/tests/boundary/mcp/gdbCancellationMcp.test.ts index c7e72aa719..1d2af74ab6 100644 --- a/tests/boundary/mcp/gdbCancellationMcp.test.ts +++ b/tests/boundary/mcp/gdbCancellationMcp.test.ts @@ -40,11 +40,12 @@ import { fakeGdbStartupPreamble, } from "../providers/gdbTestHelpers.js"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; it.skipIf(process.platform === "win32")( "keeps a failed GDB startup closed through MCP and retries its retained owner before console admission", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-gdb-mcp-startup-")); + const directory = await createTestTempDirectory("rea-gdb-mcp-startup-"); const provider = join(directory, "fake-gdb.mjs"); await writeFile( provider, @@ -135,11 +136,7 @@ process.stdin.on("data", (chunk) => { () => server.close(), async () => { await manager.closeAll(); - try { - await expectNoUnmodeledGdbCommands(join(directory, "unmodeled")); - } finally { - await rm(directory, { recursive: true, force: true }); - } + await expectNoUnmodeledGdbCommands(join(directory, "unmodeled")); }, ]); } @@ -149,7 +146,7 @@ process.stdin.on("data", (chunk) => { it.skipIf(process.platform === "win32")( "propagates MCP cancellation into a running GDB command", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-gdb-mcp-cancel-")); + const directory = await createTestTempDirectory("rea-gdb-mcp-cancel-"); const provider = join(directory, "fake-gdb.mjs"); const commandPath = join(directory, "command-started"); await writeFile( @@ -262,7 +259,6 @@ process.stdin.on("data", async (chunk) => { () => server.close(), async () => { await manager.closeAll(); - await rm(directory, { recursive: true, force: true }); }, ]); } @@ -272,7 +268,7 @@ process.stdin.on("data", async (chunk) => { it.skipIf(process.platform === "win32")( "accepts caller-selected inferior_tty and exposes verified sink status through MCP", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-gdb-mcp-tty-")); + const directory = await createTestTempDirectory("rea-gdb-mcp-tty-"); const provider = join(directory, "fake-gdb.mjs"); const tty = `${directory}/inferior-"terminal-Ω"`; await writeFile( @@ -347,11 +343,7 @@ process.stdin.on("data", (chunk) => { () => server.close(), async () => { await manager.closeAll(); - try { - await expectNoUnmodeledGdbCommands(join(directory, "unmodeled")); - } finally { - await rm(directory, { recursive: true, force: true }); - } + await expectNoUnmodeledGdbCommands(join(directory, "unmodeled")); }, ]); } @@ -361,7 +353,7 @@ process.stdin.on("data", (chunk) => { it.skipIf(process.platform === "win32")( "exposes lossy GDB MI decoding through the strict status MCP contract", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-gdb-mcp-mi-utf8-")); + const directory = await createTestTempDirectory("rea-gdb-mcp-mi-utf8-"); const provider = join(directory, "fake-gdb.mjs"); await writeFile( provider, @@ -419,11 +411,7 @@ process.stdin.on("data", (chunk) => { () => server.close(), async () => { await manager.closeAll(); - try { - await expectNoUnmodeledGdbCommands(join(directory, "unmodeled")); - } finally { - await rm(directory, { recursive: true, force: true }); - } + await expectNoUnmodeledGdbCommands(join(directory, "unmodeled")); }, ]); } diff --git a/tests/boundary/mcp/gdbExecutableSelection.test.ts b/tests/boundary/mcp/gdbExecutableSelection.test.ts index d169290139..c215ce73a2 100644 --- a/tests/boundary/mcp/gdbExecutableSelection.test.ts +++ b/tests/boundary/mcp/gdbExecutableSelection.test.ts @@ -29,6 +29,7 @@ import { fakeGdbModeledReplies, fakeGdbStartupPreamble, } from "../providers/gdbTestHelpers.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const selectedBytes = Buffer.from("caller-selected executable bytes\n"); const selectedDigest = createHash("sha256").update(selectedBytes).digest("hex"); @@ -141,7 +142,7 @@ const fixture = async ( "stopProcess" > = {}, ) => { - const root = await mkdtemp(join(tmpdir(), "rea-gdb-selected-file-")); + const root = await createTestTempDirectory("rea-gdb-selected-file-"); const owner = createGdbFixtureOwner(); const managers: GdbSessionManager[] = []; const connections: Array<() => void | Promise> = []; @@ -151,11 +152,7 @@ const fixture = async ( ...connections, () => owner.cleanup(managers, async () => { - try { - await expectNoUnmodeledGdbCommands(unmodeled); - } finally { - await rm(root, { recursive: true, force: true }); - } + await expectNoUnmodeledGdbCommands(unmodeled); }), ]), ); diff --git a/tests/boundary/mcp/ghidraProfileRefusalMcp.test.ts b/tests/boundary/mcp/ghidraProfileRefusalMcp.test.ts index eb926566c8..c0f0e72a88 100644 --- a/tests/boundary/mcp/ghidraProfileRefusalMcp.test.ts +++ b/tests/boundary/mcp/ghidraProfileRefusalMcp.test.ts @@ -13,12 +13,13 @@ import { createServer } from "../../../src/server/createServer.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { ghidraInstallationHost } from "./ghidraMcpHarness.js"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const REFUSAL = "Processor or compiler specification overrides cannot replace the admitted raw-binary import."; const openRawArm = async (languageId: string | undefined) => { - const directory = await mkdtemp(join(tmpdir(), "rea-profile-refusal-")); + const directory = await createTestTempDirectory("rea-profile-refusal-"); const path = join(directory, "image.bin"); await writeFile(path, Buffer.from([0, 0, 160, 225])); const config = parseConfig({ @@ -67,7 +68,6 @@ const openRawArm = async (languageId: string | undefined) => { } finally { await Promise.allSettled([mcp.close(), server.close()]); await session.close(); - await rm(directory, { recursive: true, force: true }); } }; diff --git a/tests/boundary/mcp/investigationEvidenceReferencesMcp.test.ts b/tests/boundary/mcp/investigationEvidenceReferencesMcp.test.ts index f29d2a9e22..ed7b5bc162 100644 --- a/tests/boundary/mcp/investigationEvidenceReferencesMcp.test.ts +++ b/tests/boundary/mcp/investigationEvidenceReferencesMcp.test.ts @@ -28,6 +28,7 @@ import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarnes import { createTestBinarySession } from "../../fixtures/binarySession.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const reference = (source: Evidence) => ({ kind: "retained-evidence" as const, @@ -256,8 +257,7 @@ it("reports missing sources for a retained dossier with a large link set", async evidenceLinks: links, }, ); - const directory = await mkdtemp(join(tmpdir(), "rea-large-links-")); - onTestFinished(() => rm(directory, { recursive: true, force: true })); + const directory = await createTestTempDirectory("rea-large-links-"); const path = join(directory, "bundle.json"); await writeFile(path, JSON.stringify(createEvidenceBundle([source]))); const imported = await client.callTool({ diff --git a/tests/boundary/mcp/processCleanupRetryMcp.test.ts b/tests/boundary/mcp/processCleanupRetryMcp.test.ts index 7f28f3694d..6605e1b480 100644 --- a/tests/boundary/mcp/processCleanupRetryMcp.test.ts +++ b/tests/boundary/mcp/processCleanupRetryMcp.test.ts @@ -24,9 +24,10 @@ import { silentLogger } from "../../../src/logger.js"; import { createServer } from "../../../src/server/createServer.js"; import { ToolResultDelivery } from "../../../src/server/ToolResultDelivery.js"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const createTemporaryRoot = (): Promise => - mkdtemp(join(tmpdir(), "rea-mcp-process-cleanup-retry-")); + createTestTempDirectory("rea-mcp-process-cleanup-retry-"); const cleanupOptions = ( root: string, @@ -166,9 +167,6 @@ it("projects the retained process root through close_binary and resolves its ret async () => { await closeSession(resources); }, - async () => { - await rm(root, { recursive: true, force: true }); - }, ]); }); @@ -269,9 +267,6 @@ it.each(["returned", "thrown", "synchronous"] as const)( async () => { await closeSession(resources); }, - async () => { - await rm(root, { recursive: true, force: true }); - }, ]); }, ); diff --git a/tests/boundary/mcp/reverseDebuggerMcpHarnessLifecycle.test.ts b/tests/boundary/mcp/reverseDebuggerMcpHarnessLifecycle.test.ts index d2e27bb976..4c388037f4 100644 --- a/tests/boundary/mcp/reverseDebuggerMcpHarnessLifecycle.test.ts +++ b/tests/boundary/mcp/reverseDebuggerMcpHarnessLifecycle.test.ts @@ -6,12 +6,13 @@ import { McpServer } from "@modelcontextprotocol/server"; import { expect, it, onTestFinished, vi } from "vitest"; import { createProviderEnvironmentMcpHarness } from "../../support/reverseDebuggerMcpTestHelpers.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const providerSource = `#!${process.execPath}\nprocess.stdout.write("selected provider\\n");\n`; const inputSource = "selected input bytes\n"; const selectedProviderFiles = async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-provider-owner-")); + const directory = await createTestTempDirectory("rea-provider-owner-"); const provider = join(directory, "selected-rizin"); const input = join(directory, "fixture.bin"); await Promise.all([ diff --git a/tests/boundary/mcp/reverseEngineeringEnvironmentMcp.test.ts b/tests/boundary/mcp/reverseEngineeringEnvironmentMcp.test.ts index 4b03e00eeb..d8300e5bfb 100644 --- a/tests/boundary/mcp/reverseEngineeringEnvironmentMcp.test.ts +++ b/tests/boundary/mcp/reverseEngineeringEnvironmentMcp.test.ts @@ -10,6 +10,7 @@ import { REVERSE_ENGINEERING_TOOL_CONTRACTS } from "../../../src/contracts/rever import { rizinCommandLimitations } from "../../../src/rizin/RizinCommand.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { createProviderEnvironmentMcpHarness } from "../../support/reverseDebuggerMcpTestHelpers.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const writeProvider = async (path: string, output: string): Promise => { await writeFile( @@ -49,7 +50,7 @@ const executeRizinCommand = async ( }; it("uses the captured server environment to resolve reverse-engineering providers", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-provider-environment-")); + const directory = await createTestTempDirectory("rea-provider-environment-"); const provider = join(directory, "selected-rizin"); const artifact = join(directory, "fixture.bin"); await writeFile(artifact, "fixture"); @@ -67,7 +68,7 @@ it("uses the captured server environment to resolve reverse-engineering provider }); it("prefers an explicit provider environment over the captured server environment", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-provider-override-")); + const directory = await createTestTempDirectory("rea-provider-override-"); const configured = join(directory, "captured-rizin"); const override = join(directory, "override-rizin"); const artifact = join(directory, "fixture.bin"); @@ -95,7 +96,7 @@ it("prefers an explicit provider environment over the captured server environmen }); it("snapshots an explicit provider environment when the server is created", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-provider-snapshot-")); + const directory = await createTestTempDirectory("rea-provider-snapshot-"); const configured = join(directory, "captured-rizin"); const override = join(directory, "explicit-rizin"); const artifact = join(directory, "fixture.bin"); @@ -125,7 +126,7 @@ it("snapshots an explicit provider environment when the server is created", asyn }); it("passes Rizin decoder and map inputs as flags before the artifact", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-rizin-load-mcp-")); + const directory = await createTestTempDirectory("rea-rizin-load-mcp-"); const provider = join(directory, "argv-rizin"); const artifact = join(directory, "flash.bin"); await writeFile(artifact, "flash"); diff --git a/tests/boundary/mcp/reverseEngineeringProviderEnvironmentMcp.test.ts b/tests/boundary/mcp/reverseEngineeringProviderEnvironmentMcp.test.ts index 4819f4d1ab..ea4b6e6e02 100644 --- a/tests/boundary/mcp/reverseEngineeringProviderEnvironmentMcp.test.ts +++ b/tests/boundary/mcp/reverseEngineeringProviderEnvironmentMcp.test.ts @@ -9,6 +9,7 @@ import { createServer } from "../../../src/server/createServer.js"; import { createAnalysisExecution } from "../../../src/ports/AnalysisProvider.js"; import { ok } from "../../../src/domain/result.js"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const analysis = { execute: async () => @@ -24,7 +25,7 @@ const writeRizin = async (directory: string, output: string): Promise => { }; it("uses the server-selected environment unless a provider environment overrides it", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-reverse-env-")); + const directory = await createTestTempDirectory("rea-reverse-env-"); const selectedPath = join(directory, "selected"); const overridePath = join(directory, "override"); const artifactPath = join(directory, "sample.bin"); @@ -35,54 +36,50 @@ it("uses the server-selected environment unless a provider environment overrides writeFile(artifactPath, "fixture"), ]); - try { - for (const scenario of [ + for (const scenario of [ + { + environment: selectedPath, + expected: "selected environment", + }, + { + environment: selectedPath, + provider: overridePath, + expected: "provider override", + }, + ]) { + const server = createServer( + { kind: "fixed", analysis }, { - environment: selectedPath, - expected: "selected environment", + environment: { ...process.env, PATH: scenario.environment }, + ...(scenario.provider === undefined + ? {} + : { + providerEnvironment: { + ...process.env, + PATH: scenario.provider, + }, + }), }, - { - environment: selectedPath, - provider: overridePath, - expected: "provider override", - }, - ]) { - const server = createServer( - { kind: "fixed", analysis }, - { - environment: { ...process.env, PATH: scenario.environment }, - ...(scenario.provider === undefined - ? {} - : { - providerEnvironment: { - ...process.env, - PATH: scenario.provider, - }, - }), - }, - ); - const client = new Client( - { name: "reverse-env-test", version: "1" }, - { jsonSchemaValidator: createMcpSchemaValidation() }, - ); - const [clientTransport, serverTransport] = - InMemoryTransport.createLinkedPair(); - try { - await server.connect(serverTransport); - await client.connect(clientTransport); - const result = await client.callTool({ - name: "execute_rizin_command", - arguments: { path: artifactPath, command: "iI" }, - }); - expect(result.isError).not.toBe(true); - expect(result.structuredContent).toMatchObject({ - raw_result: { stdout: `${scenario.expected}\n` }, - }); - } finally { - await Promise.all([client.close(), server.close()]); - } + ); + const client = new Client( + { name: "reverse-env-test", version: "1" }, + { jsonSchemaValidator: createMcpSchemaValidation() }, + ); + const [clientTransport, serverTransport] = + InMemoryTransport.createLinkedPair(); + try { + await server.connect(serverTransport); + await client.connect(clientTransport); + const result = await client.callTool({ + name: "execute_rizin_command", + arguments: { path: artifactPath, command: "iI" }, + }); + expect(result.isError).not.toBe(true); + expect(result.structuredContent).toMatchObject({ + raw_result: { stdout: `${scenario.expected}\n` }, + }); + } finally { + await Promise.all([client.close(), server.close()]); } - } finally { - await rm(directory, { recursive: true, force: true }); } }); diff --git a/tests/boundary/mcp/rizinDebuggerCancellationMcp.test.ts b/tests/boundary/mcp/rizinDebuggerCancellationMcp.test.ts index 2166cdfd79..cc321768af 100644 --- a/tests/boundary/mcp/rizinDebuggerCancellationMcp.test.ts +++ b/tests/boundary/mcp/rizinDebuggerCancellationMcp.test.ts @@ -30,11 +30,12 @@ import { waitForStartedFile, } from "../../support/reverseDebuggerMcpTestHelpers.js"; import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; it.skipIf(process.platform === "win32")( "reports Rizin launch provenance and lossy frame decoding through MCP", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-rizin-provenance-")); + const directory = await createTestTempDirectory("rea-rizin-provenance-"); const provider = join(directory, "fake-rizin-provenance.mjs"); await writeFile( provider, @@ -113,7 +114,6 @@ process.stdin.on("data", (chunk) => { () => server.close(), async () => { await manager.closeAll(); - await rm(directory, { recursive: true, force: true }); }, ]); } @@ -190,7 +190,7 @@ it("admits Rizin startup across a bound analysis session", async () => { it.skipIf(process.platform === "win32")( "propagates MCP cancellation into a running Rizin debugger command", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-rizin-mcp-cancel-")); + const directory = await createTestTempDirectory("rea-rizin-mcp-cancel-"); const provider = join(directory, "fake-rizin.mjs"); const commandPath = join(directory, "command-started"); await writeFile( @@ -254,7 +254,6 @@ process.stdin.on("data", async (chunk) => { () => server.close(), async () => { await manager.closeAll(); - await rm(directory, { recursive: true, force: true }); }, ]); } diff --git a/tests/boundary/mcp/toolErrorSchemaDelivery.test.ts b/tests/boundary/mcp/toolErrorSchemaDelivery.test.ts index edd220e9ea..f82e5b8254 100644 --- a/tests/boundary/mcp/toolErrorSchemaDelivery.test.ts +++ b/tests/boundary/mcp/toolErrorSchemaDelivery.test.ts @@ -9,10 +9,10 @@ import { z } from "zod"; import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js"; import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; it("delivers typed tool failures without violating the advertised success schema over stdio", async () => { - const root = await mkdtemp(join(tmpdir(), "rea-tool-error-schema-")); - onTestFinished(() => rm(root, { recursive: true, force: true })); + const root = await createTestTempDirectory("rea-tool-error-schema-"); const missing = join(root, "not-present.bin"); const { client, call } = await connectLocalToolsMcp(); const tools = (await client.listTools()).tools; diff --git a/tests/boundary/model/modelArtifactPartialCancellation.test.ts b/tests/boundary/model/modelArtifactPartialCancellation.test.ts index 0015779df0..c28d1248a0 100644 --- a/tests/boundary/model/modelArtifactPartialCancellation.test.ts +++ b/tests/boundary/model/modelArtifactPartialCancellation.test.ts @@ -6,6 +6,7 @@ import { ModelArtifactProvider } from "../../../src/model/ModelArtifactProvider. import { parseEvidence } from "../../../src/domain/evidence.js"; import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js"; import { ggufFixture, safetensorsFixture } from "../../fixtures/model/model.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const cancelledArrayFixture = (alignment?: number) => { const seed = ggufFixture(); @@ -38,7 +39,7 @@ const cancelledArrayFixture = (alignment?: number) => { it.each([undefined, 64])( "retains completed GGUF facts when cancellation interrupts a later value (alignment=%s)", async (alignment) => { - const root = await mkdtemp(join(tmpdir(), "rea-model-partial-cancel-")); + const root = await createTestTempDirectory("rea-model-partial-cancel-"); const path = join(root, "model.gguf"); const { fixture, count, arrayStart } = cancelledArrayFixture(alignment); await writeFile(path, fixture.bytes); @@ -126,13 +127,12 @@ it.each([undefined, 64])( } finally { readSpy.mockRestore(); await provider.close(); - await rm(root, { recursive: true, force: true }); } }, ); it("retains GGUF tensor declarations and layout limitations before later tensor bytes cancel", async () => { - const root = await mkdtemp(join(tmpdir(), "rea-model-tensor-partial-")); + const root = await createTestTempDirectory("rea-model-tensor-partial-"); const path = join(root, "model.gguf"); const tensorCount = 4000; const fixture = ggufFixture({ @@ -231,7 +231,6 @@ it("retains GGUF tensor declarations and layout limitations before later tensor } finally { readSpy.mockRestore(); await provider.close(); - await rm(root, { recursive: true, force: true }); } }); @@ -242,7 +241,7 @@ it.each([ ])( "retains parsed $format facts with alignment $alignment when full hashing is cancelled", async ({ format, alignment }) => { - const root = await mkdtemp(join(tmpdir(), "rea-model-hash-cancel-")); + const root = await createTestTempDirectory("rea-model-hash-cancel-"); const path = join(root, `model.${format}`); const fixture = format === "safetensors" @@ -347,7 +346,6 @@ it.each([ } finally { readSpy.mockRestore(); await provider.close(); - await rm(root, { recursive: true, force: true }); } }, ); diff --git a/tests/boundary/native/pwntoolsArtifactCleanup.test.ts b/tests/boundary/native/pwntoolsArtifactCleanup.test.ts index 4a277200a9..884edb7615 100644 --- a/tests/boundary/native/pwntoolsArtifactCleanup.test.ts +++ b/tests/boundary/native/pwntoolsArtifactCleanup.test.ts @@ -58,6 +58,7 @@ import { binaryLayoutFixture } from "../../fixtures/binaryDiagnostics/layout.js" import { PwntoolsDecoder } from "../../../src/native/pwntools/PwntoolsDecoder.js"; import { PWNTOOLS_PROVIDER_IDENTITY } from "../../../src/native/pwntools/PwntoolsProviderIdentity.js"; import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const makeDecoder = () => new PwntoolsDecoder( @@ -79,7 +80,7 @@ const makeDecoder = () => ); it("reports the read failure without stale incomplete-cleanup state when close retry succeeds", async () => { - const root = await mkdtemp(join(tmpdir(), "rea-pwntools-close-retry-")); + const root = await createTestTempDirectory("rea-pwntools-close-retry-"); const path = join(root, "selected.bin"); await writeFile(path, "selected evidence"); closeState.fail = false; @@ -113,12 +114,11 @@ it("reports the read failure without stale incomplete-cleanup state when close r closeState.fail = false; closeState.failOnce = false; await decoder.close(); - await rm(root, { recursive: true, force: true }); } }); it("retains a persistent failed input descriptor and refuses the next acquisition", async () => { - const root = await mkdtemp(join(tmpdir(), "rea-pwntools-held-close-")); + const root = await createTestTempDirectory("rea-pwntools-held-close-"); const path = join(root, "selected.bin"); await writeFile(path, "selected evidence"); closeState.fail = true; @@ -151,12 +151,11 @@ it("retains a persistent failed input descriptor and refuses the next acquisitio } catch { // The test already asserts persistent cleanup failure before releasing it. } - await rm(root, { recursive: true, force: true }); } }); it("retains selected target identity when cancellation arrives after its completed read", async () => { - const root = await mkdtemp(join(tmpdir(), "rea-pwntools-input-cancel-")); + const root = await createTestTempDirectory("rea-pwntools-input-cancel-"); const path = join(root, "selected.bin"); await writeFile(path, "selected evidence"); closeState.inputAbort = new AbortController(); @@ -179,14 +178,13 @@ it("retains selected target identity when cancellation arrives after its complet } finally { closeState.inputAbort = undefined; await decoder.close(); - await rm(root, { recursive: true, force: true }); } }); // Simulated decoder writes a reply; the selected object is never executed. it("retains admitted layout through reply close failure and cancellation, then resolves cleanup", async () => { await prepareProcessOwnershipInspection(); - const root = await mkdtemp(join(tmpdir(), "rea-pwntools-reply-close-")); + const root = await createTestTempDirectory("rea-pwntools-reply-close-"); const path = join(root, "selected.bin"); await writeFile(path, Buffer.alloc(128)); const runtime = await PrivateRuntimeRoot.create({ @@ -248,7 +246,6 @@ it("retains admitted layout through reply close failure and cancellation, then r closeState.replyFail = false; closeState.abort = undefined; await provider.close(); - await rm(root, { recursive: true, force: true }); } }); @@ -266,7 +263,7 @@ it.each([ "admits base core facts before optional debugger enrichment: %s", async (scenario) => { await prepareProcessOwnershipInspection(); - const root = await mkdtemp(join(tmpdir(), "rea-pwntools-core-reply-")); + const root = await createTestTempDirectory("rea-pwntools-core-reply-"); const path = join(root, "selected.core"); const value = recordedCrashFixture(path); await writeFile(path, recordedCrashFixtureBytes(value)); @@ -402,7 +399,6 @@ it.each([ closeState.replyFail = false; closeState.abort = undefined; await provider.close(); - await rm(root, { recursive: true, force: true }); } }, ); diff --git a/tests/boundary/process/adbVerifierCleanup.test.ts b/tests/boundary/process/adbVerifierCleanup.test.ts index be3fed58e0..37ece084f0 100644 --- a/tests/boundary/process/adbVerifierCleanup.test.ts +++ b/tests/boundary/process/adbVerifierCleanup.test.ts @@ -13,13 +13,7 @@ import { join } from "node:path"; import { afterEach, expect, it } from "vitest"; import { copyScriptsWithImports } from "../../support/scriptImportClosure.js"; - -const roots: string[] = []; -afterEach(async () => { - await Promise.all( - roots.splice(0).map((root) => rm(root, { recursive: true, force: true })), - ); -}); +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const runVerifier = async ( settings: { @@ -30,8 +24,7 @@ const runVerifier = async ( pull?: "error" | "throw"; } = {}, ) => { - const root = await mkdtemp(join(tmpdir(), "rea-adb-verifier-test-")); - roots.push(root); + const root = await createTestTempDirectory("rea-adb-verifier-test-"); await mkdir(join(root, "dist", "application", "adb"), { recursive: true }); await mkdir(join(root, "dist", "composition"), { recursive: true }); await copyScriptsWithImports(["scripts/verify-adb.mjs"], root); diff --git a/tests/boundary/process/artifactVerifierCleanupOwner.test.ts b/tests/boundary/process/artifactVerifierCleanupOwner.test.ts index 85271f9d8d..f8c0413245 100644 --- a/tests/boundary/process/artifactVerifierCleanupOwner.test.ts +++ b/tests/boundary/process/artifactVerifierCleanupOwner.test.ts @@ -8,9 +8,10 @@ import { createPackageVerifierCommands } from "../../../scripts/lib/verify-packa import { OwnedMcpStdioTransport } from "../../../src/process/OwnedMcpStdioTransport.js"; import { resolveSwiftcCompileTimeoutMs } from "../../../src/process/DarwinProcessRunTokenReader.js"; import { processHasStopped } from "../../support/process/observePosixProcess.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; it("retains the workspace until an owned protocol cleanup can be retried", async () => { - const path = await mkdtemp(join(tmpdir(), "rea-verifier-transport-")); + const path = await createTestTempDirectory("rea-verifier-transport-"); const process = Object.assign(new EventEmitter(), { pid: 1, stdout: new PassThrough(), @@ -50,21 +51,17 @@ it("retains the workspace until an owned protocol cleanup can be retried", async }, }); commands.adopt(transport, "protocol child"); - try { - await transport.start(); - // The group can still contain work after the leader's exit. - process.emit("exit", 0, null); - await expect(commands.close()).rejects.toMatchObject({ - path, - cleanupOwner: commands, - }); - await expect(access(path)).resolves.toBeUndefined(); - refuse = false; - await commands.close(); - await expect(access(path)).rejects.toMatchObject({ code: "ENOENT" }); - } finally { - await rm(path, { recursive: true, force: true }); - } + await transport.start(); + // The group can still contain work after the leader's exit. + process.emit("exit", 0, null); + await expect(commands.close()).rejects.toMatchObject({ + path, + cleanupOwner: commands, + }); + await expect(access(path)).resolves.toBeUndefined(); + refuse = false; + await commands.close(); + await expect(access(path)).rejects.toMatchObject({ code: "ENOENT" }); }); it( @@ -73,7 +70,7 @@ it( const { writeFile, readFile } = await import("node:fs/promises"); const { createOwnedMcpStdioTransport } = await import("../../../scripts/lib/owned-mcp-stdio.mjs"); - const path = await mkdtemp(join(tmpdir(), "rea-verifier-real-transport-")); + const path = await createTestTempDirectory("rea-verifier-real-transport-"); const fixture = join(path, "child.cjs"); const marker = join(path, "pids.json"); await writeFile( @@ -110,7 +107,6 @@ setInterval(()=>{},1000);`, for (const pid of pids) expect(await processHasStopped(pid)).toBe(true); } finally { await transport.close(); - await rm(path, { recursive: true, force: true }); } }, Math.min( diff --git a/tests/boundary/process/bunVerifierOwnerInitialization.test.ts b/tests/boundary/process/bunVerifierOwnerInitialization.test.ts index e546152ea4..95a901fa20 100644 --- a/tests/boundary/process/bunVerifierOwnerInitialization.test.ts +++ b/tests/boundary/process/bunVerifierOwnerInitialization.test.ts @@ -3,6 +3,7 @@ import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { expect, it } from "vitest"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; it.each([ ["initialization failure", "init", true], @@ -12,77 +13,70 @@ it.each([ ] as const)( "retains the verifier root correctly after %s", async (_name, failure, rootClosed) => { - const root = await mkdtemp(join(tmpdir(), "rea-bun-owner-test-")); - try { - await mkdir(join(root, "scripts", "lib"), { recursive: true }); - // VerifierFixture loads src/process/PrivateRuntimeRoot.ts beside its - // copied scripts/lib, so the stand-in root sits there. - await mkdir(join(root, "src", "process"), { recursive: true }); - await mkdir(join(root, "dist", "domain"), { recursive: true }); + const root = await createTestTempDirectory("rea-bun-owner-test-"); + await mkdir(join(root, "scripts", "lib"), { recursive: true }); + // VerifierFixture loads src/process/PrivateRuntimeRoot.ts beside its + // copied scripts/lib, so the stand-in root sits there. + await mkdir(join(root, "src", "process"), { recursive: true }); + await mkdir(join(root, "dist", "domain"), { recursive: true }); + await writeFile( + join(root, "scripts", "verify-bun-standalone.mjs"), + await readFile( + new URL("../../../scripts/verify-bun-standalone.mjs", import.meta.url), + ), + ); + for (const helper of [ + "verifier-cleanup.mjs", + "verifier-fixture.mjs", + "canonical-temporary-directory.mjs", + "verifier-source-loader.mjs", + ]) await writeFile( - join(root, "scripts", "verify-bun-standalone.mjs"), + join(root, "scripts", "lib", helper), await readFile( - new URL( - "../../../scripts/verify-bun-standalone.mjs", - import.meta.url, - ), + new URL(`../../../scripts/lib/${helper}`, import.meta.url), ), ); - for (const helper of [ - "verifier-cleanup.mjs", - "verifier-fixture.mjs", - "canonical-temporary-directory.mjs", - "verifier-source-loader.mjs", - ]) - await writeFile( - join(root, "scripts", "lib", helper), - await readFile( - new URL(`../../../scripts/lib/${helper}`, import.meta.url), - ), - ); - await writeFile( - join(root, "scripts", "lib", "verify-commands.mjs"), - `export async function createVerifierCommands() { + await writeFile( + join(root, "scripts", "lib", "verify-commands.mjs"), + `export async function createVerifierCommands() { if (process.env.FAULT.startsWith("init")) throw new Error("owner load refused"); return { async exec() { throw new Error("command refused"); }, async close() { if (process.env.FAULT === "close") throw new Error("owner close refused"); } }; }`, - ); - await writeFile( - join(root, "src", "process", "PrivateRuntimeRoot.ts"), - `import { writeFile } from "node:fs/promises"; + ); + await writeFile( + join(root, "src", "process", "PrivateRuntimeRoot.ts"), + `import { writeFile } from "node:fs/promises"; export class PrivateRuntimeRoot { static async create() { return { path: process.env.ROOT, async close() { if (process.env.FAULT === "init_close") throw new Error("root close refused"); await writeFile(process.env.CLOSED, "closed"); } }; } }`, - ); - await writeFile( - join(root, "dist", "domain", "evidence.js"), - `export function parseEvidence() { throw new Error("unexpected"); }`, - ); - const marker = join(root, "closed"); - const result = spawnSync( - process.execPath, - [join(root, "scripts", "verify-bun-standalone.mjs")], - { - encoding: "utf8", - timeout: 10_000, - env: { ...process.env, ROOT: root, CLOSED: marker, FAULT: failure }, - }, - ); - expect(result.status).not.toBe(0); - expect(result.stderr).toContain( - failure.startsWith("init") ? "owner load refused" : "command refused", - ); - if (failure === "close") - expect(result.stderr).toContain("owner close refused"); - if (failure === "init_close") - expect(result.stderr).toContain("root close refused"); - expect(await readFile(marker, "utf8").catch(() => null)).toBe( - rootClosed ? "closed" : null, - ); - } finally { - await rm(root, { recursive: true, force: true }); - } + ); + await writeFile( + join(root, "dist", "domain", "evidence.js"), + `export function parseEvidence() { throw new Error("unexpected"); }`, + ); + const marker = join(root, "closed"); + const result = spawnSync( + process.execPath, + [join(root, "scripts", "verify-bun-standalone.mjs")], + { + encoding: "utf8", + timeout: 10_000, + env: { ...process.env, ROOT: root, CLOSED: marker, FAULT: failure }, + }, + ); + expect(result.status).not.toBe(0); + expect(result.stderr).toContain( + failure.startsWith("init") ? "owner load refused" : "command refused", + ); + if (failure === "close") + expect(result.stderr).toContain("owner close refused"); + if (failure === "init_close") + expect(result.stderr).toContain("root close refused"); + expect(await readFile(marker, "utf8").catch(() => null)).toBe( + rootClosed ? "closed" : null, + ); }, ); diff --git a/tests/boundary/process/claudeVerifierSetupTargets.test.ts b/tests/boundary/process/claudeVerifierSetupTargets.test.ts index ae065868fe..337232be6e 100644 --- a/tests/boundary/process/claudeVerifierSetupTargets.test.ts +++ b/tests/boundary/process/claudeVerifierSetupTargets.test.ts @@ -4,6 +4,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { expect, it } from "vitest"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; it.each( ( @@ -21,14 +22,13 @@ it.each( )( "%s checks a %s setup-plan target before writing", async (client, kind, allowed) => { - const state = await mkdtemp(join(tmpdir(), "rea-claude-plan-")); - try { - const commands = join(state, "commands.mjs"); - const fixture = join(state, "fixture.mjs"); - const hook = join(state, "hook.mjs"); - await writeFile( - commands, - `import { writeFile } from 'node:fs/promises'; + const state = await createTestTempDirectory("rea-claude-plan-"); + const commands = join(state, "commands.mjs"); + const fixture = join(state, "fixture.mjs"); + const hook = join(state, "hook.mjs"); + await writeFile( + commands, + `import { writeFile } from 'node:fs/promises'; import { isAbsolute, join, relative } from 'node:path'; export async function createVerifierCommands(){return { async exec(command,args,options={}){ @@ -42,57 +42,51 @@ export async function createVerifierCommands(){return { } await writeFile(process.env.STATE+'/setup-advanced','yes'); throw Error('intentional stop before setup mutation'); }, async close(){} };}`, - ); - await writeFile( - fixture, - `export async function createAnthropicModelFixture(){return {baseUrl:'http://127.0.0.1:1',async close(){}};} export const createOpenAiModelFixture=createAnthropicModelFixture;`, - ); - await writeFile( - hook, - `import {registerHooks} from 'node:module';import{pathToFileURL}from'node:url'; + ); + await writeFile( + fixture, + `export async function createAnthropicModelFixture(){return {baseUrl:'http://127.0.0.1:1',async close(){}};} export const createOpenAiModelFixture=createAnthropicModelFixture;`, + ); + await writeFile( + hook, + `import {registerHooks} from 'node:module';import{pathToFileURL}from'node:url'; registerHooks({resolve(specifier,context,next){if(context.parentURL?.endsWith('/scripts/verify-'+process.env.CLIENT+'-client.mjs')){let f;if(specifier==='./lib/verify-commands.mjs')f='commands.mjs';if(specifier==='./verify/clients/anthropic-model-fixture.mjs'||specifier==='./verify/clients/model-fixture.mjs')f='fixture.mjs';if(f)return{url:pathToFileURL(process.env.STATE+'/'+f).href,shortCircuit:true};}return next(specifier,context);}});`, - ); - const result = spawnSync( - process.execPath, - [ - "--import", - hook, - fileURLToPath( - new URL( - `../../../scripts/verify-${client}-client.mjs`, - import.meta.url, - ), + ); + const result = spawnSync( + process.execPath, + [ + "--import", + hook, + fileURLToPath( + new URL( + `../../../scripts/verify-${client}-client.mjs`, + import.meta.url, ), - ], - { - encoding: "utf8", - cwd: state, - timeout: 10_000, - env: { - ...process.env, - STATE: state, - KIND: kind, - CLIENT: client, - TMPDIR: state, - TMP: state, - TEMP: state, - }, + ), + ], + { + encoding: "utf8", + cwd: state, + timeout: 10_000, + env: { + ...process.env, + STATE: state, + KIND: kind, + CLIENT: client, + TMPDIR: state, + TMP: state, + TEMP: state, }, - ); - expect(result.status).not.toBe(0); - const advanced = await readFile( - join(state, "setup-advanced"), - "utf8", - ).then( - () => true, - () => false, - ); - expect(advanced).toBe(allowed); - expect(result.stderr).toContain( - allowed ? "intentional stop before setup mutation" : "AssertionError", - ); - } finally { - await rm(state, { recursive: true, force: true }); - } + }, + ); + expect(result.status).not.toBe(0); + const advanced = await readFile(join(state, "setup-advanced"), "utf8").then( + () => true, + () => false, + ); + expect(advanced).toBe(allowed); + expect(result.stderr).toContain( + allowed ? "intentional stop before setup mutation" : "AssertionError", + ); }, ); diff --git a/tests/boundary/process/cliEnvironmentSeam.test.ts b/tests/boundary/process/cliEnvironmentSeam.test.ts index 0d46c96c4e..921bf2e15d 100644 --- a/tests/boundary/process/cliEnvironmentSeam.test.ts +++ b/tests/boundary/process/cliEnvironmentSeam.test.ts @@ -10,6 +10,7 @@ import { createDirectAnalysis } from "../../../src/composition/directAnalysis.js import { probeProcessCaptureCapability } from "../../../src/process/capture/ProcessCaptureCapability.js"; import { parseEvidence } from "../../../src/domain/evidence.js"; import { parseProcessCapture } from "../../../src/domain/process/processCaptureParsing.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; describe("the CLI takes its environment as an input", () => { it("resolves from injected PATH and inherits injected env with scenario overrides", async ({ @@ -21,46 +22,42 @@ describe("the CLI takes its environment as an input", () => { return; } - const root = await mkdtemp(join(tmpdir(), "rea-cli-env-process-")); + const root = await createTestTempDirectory("rea-cli-env-process-"); const bin = join(root, "bin"); const executable = join(bin, "rea-injected-process-probe"); const scenarioPath = join(root, "scenario.json"); - try { - await mkdir(bin); - await writeFile( - executable, - [ - "#!/bin/sh", - 'printf "%s|%s\\n" "$REA_CAPTURE_INJECTED_MARKER" "$REA_CAPTURE_SCENARIO_OVERRIDE"', - ].join("\n"), - ); - await chmod(executable, 0o755); - await writeFile( - scenarioPath, - JSON.stringify({ - executable: "rea-injected-process-probe", - working_directory: root, - environment: { REA_CAPTURE_SCENARIO_OVERRIDE: "scenario-value" }, - }), - ); + await mkdir(bin); + await writeFile( + executable, + [ + "#!/bin/sh", + 'printf "%s|%s\\n" "$REA_CAPTURE_INJECTED_MARKER" "$REA_CAPTURE_SCENARIO_OVERRIDE"', + ].join("\n"), + ); + await chmod(executable, 0o755); + await writeFile( + scenarioPath, + JSON.stringify({ + executable: "rea-injected-process-probe", + working_directory: root, + environment: { REA_CAPTURE_SCENARIO_OVERRIDE: "scenario-value" }, + }), + ); - const result = await captureProcessScenarioFile(scenarioPath, { - PATH: bin, - REA_CAPTURE_INJECTED_MARKER: "injected-value", - REA_CAPTURE_SCENARIO_OVERRIDE: "host-value", - }); - if (isCliOperationFailure(result)) - throw new Error( - `Process capture returned an error: ${JSON.stringify(result)}`, - ); - const evidence = parseEvidence(result); - const capture = parseProcessCapture(evidence.normalized_result); - expect(capture.frames.map(({ data }) => data).join("")).toContain( - "injected-value|scenario-value", + const result = await captureProcessScenarioFile(scenarioPath, { + PATH: bin, + REA_CAPTURE_INJECTED_MARKER: "injected-value", + REA_CAPTURE_SCENARIO_OVERRIDE: "host-value", + }); + if (isCliOperationFailure(result)) + throw new Error( + `Process capture returned an error: ${JSON.stringify(result)}`, ); - } finally { - await rm(root, { recursive: true, force: true }); - } + const evidence = parseEvidence(result); + const capture = parseProcessCapture(evidence.normalized_result); + expect(capture.frames.map(({ data }) => data).join("")).toContain( + "injected-value|scenario-value", + ); }); it("reports session status from a supplied environment", async () => { diff --git a/tests/boundary/process/clientVerifierSetupTargets.test.ts b/tests/boundary/process/clientVerifierSetupTargets.test.ts index eb2cd6357d..bb8bf9d321 100644 --- a/tests/boundary/process/clientVerifierSetupTargets.test.ts +++ b/tests/boundary/process/clientVerifierSetupTargets.test.ts @@ -4,6 +4,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { expect, it } from "vitest"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; it.each( ( @@ -30,14 +31,13 @@ it.each( )( "%s checks %s setup-plan targets before writing", async (client, kind, allowed) => { - const state = await mkdtemp(join(tmpdir(), "rea-client-plan-")); - try { - const commands = join(state, "commands.mjs"); - const fixture = join(state, "fixture.mjs"); - const hook = join(state, "hook.mjs"); - await writeFile( - commands, - `import {writeFile} from 'node:fs/promises'; import {join} from 'node:path'; + const state = await createTestTempDirectory("rea-client-plan-"); + const commands = join(state, "commands.mjs"); + const fixture = join(state, "fixture.mjs"); + const hook = join(state, "hook.mjs"); + await writeFile( + commands, + `import {writeFile} from 'node:fs/promises'; import {join} from 'node:path'; export async function createVerifierCommands(){return { async exec(command,args,options={}){ if(command==='git')return {stdout:'',stderr:''}; @@ -55,49 +55,40 @@ export async function createVerifierCommands(){return { } await writeFile(process.env.STATE+'/setup-advanced','yes'); throw Error('intentional stop before setup mutation'); }, async close(){} };}`, - ); - await writeFile( - fixture, - `export async function createOpenAiModelFixture(){return {baseUrl:'http://127.0.0.1:1',async close(){}};}`, - ); - await writeFile( - hook, - `import {registerHooks} from 'node:module';import{pathToFileURL}from'node:url'; + ); + await writeFile( + fixture, + `export async function createOpenAiModelFixture(){return {baseUrl:'http://127.0.0.1:1',async close(){}};}`, + ); + await writeFile( + hook, + `import {registerHooks} from 'node:module';import{pathToFileURL}from'node:url'; registerHooks({resolve(specifier,context,next){if(context.parentURL?.endsWith('/scripts/verify-'+process.env.CLIENT+'-client.mjs')){let f;if(specifier==='./lib/verify-commands.mjs')f='commands.mjs';if(specifier==='./verify/clients/model-fixture.mjs')f='fixture.mjs';if(f)return{url:pathToFileURL(process.env.STATE+'/'+f).href,shortCircuit:true};}return next(specifier,context);}});`, - ); - const script = fileURLToPath( - new URL( - `../../../scripts/verify-${client}-client.mjs`, - import.meta.url, - ), - ); - const result = spawnSync(process.execPath, ["--import", hook, script], { - encoding: "utf8", - timeout: 10_000, - env: { - ...process.env, - STATE: state, - CLIENT: client, - KIND: kind, - TMPDIR: state, - TMP: state, - TEMP: state, - }, - }); - expect(result.status).not.toBe(0); - const advanced = await readFile( - join(state, "setup-advanced"), - "utf8", - ).then( - () => true, - () => false, - ); - expect(advanced).toBe(allowed); - expect(result.stderr).toContain( - allowed ? "intentional stop before setup mutation" : "AssertionError", - ); - } finally { - await rm(state, { recursive: true, force: true }); - } + ); + const script = fileURLToPath( + new URL(`../../../scripts/verify-${client}-client.mjs`, import.meta.url), + ); + const result = spawnSync(process.execPath, ["--import", hook, script], { + encoding: "utf8", + timeout: 10_000, + env: { + ...process.env, + STATE: state, + CLIENT: client, + KIND: kind, + TMPDIR: state, + TMP: state, + TEMP: state, + }, + }); + expect(result.status).not.toBe(0); + const advanced = await readFile(join(state, "setup-advanced"), "utf8").then( + () => true, + () => false, + ); + expect(advanced).toBe(allowed); + expect(result.stderr).toContain( + allowed ? "intentional stop before setup mutation" : "AssertionError", + ); }, ); diff --git a/tests/boundary/process/commandcodeVerifierSkillBody.test.ts b/tests/boundary/process/commandcodeVerifierSkillBody.test.ts index 457f39c8a2..d2779ed418 100644 --- a/tests/boundary/process/commandcodeVerifierSkillBody.test.ts +++ b/tests/boundary/process/commandcodeVerifierSkillBody.test.ts @@ -4,6 +4,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { expect, it } from "vitest"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const posix = it.skipIf(process.platform === "win32"); posix.each([ @@ -16,15 +17,14 @@ posix.each([ ] as const)( "requires CommandCode skill proof for %s", async (mode, allowed) => { - const state = await mkdtemp(join(tmpdir(), "rea-commandcode-skill-")); - try { - const account = join(state, "account"); - await mkdir(account); - await writeFile( - join(account, ".rea-client-verification"), - "Disposable REA client verification account\n", - ); - const commands = String.raw` + const state = await createTestTempDirectory("rea-commandcode-skill-"); + const account = join(state, "account"); + await mkdir(account); + await writeFile( + join(account, ".rea-client-verification"), + "Disposable REA client verification account\n", + ); + const commands = String.raw` import { mkdir, readFile, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { TOOL_CONTRACTS } from REPLACE_CONTRACTS; @@ -74,66 +74,61 @@ export async function createVerifierCommands() { }; } `.replace( - "REPLACE_CONTRACTS", - JSON.stringify( - new URL("../../../dist/contracts/toolContracts.js", import.meta.url) - .href, - ), - ); - await writeFile(join(state, "commands.mjs"), commands); - await writeFile( - join(state, "fixture.mjs"), - "export async function createOpenAiModelFixture({onRequest}){globalThis.fakeRequest=onRequest;return {baseUrl:'http://127.0.0.1:1',async close(){}};}\n", - ); - await writeFile( - join(state, "hook.mjs"), - "import{registerHooks}from'node:module';import{pathToFileURL}from'node:url';registerHooks({resolve(specifier,context,next){if(context.parentURL?.endsWith('/scripts/verify-commandcode-client.mjs')){let f;if(specifier==='node:os')f='os.mjs';if(specifier==='./lib/verify-commands.mjs')f='commands.mjs';if(specifier==='./verify/clients/model-fixture.mjs')f='fixture.mjs';if(f)return{url:pathToFileURL(process.env.STATE+'/'+f).href,shortCircuit:true};}return next(specifier,context);}});\n", - ); - await writeFile( - join(state, "os.mjs"), - "export * from 'node:os';export function homedir(){return process.env.REA_VERIFY_COMMANDCODE_ACCOUNT_HOME;}\n", - ); - const script = fileURLToPath( - new URL( - "../../../scripts/verify-commandcode-client.mjs", - import.meta.url, - ), - ); - const result = spawnSync( - process.execPath, - ["--import", join(state, "hook.mjs"), script], - { - encoding: "utf8", - timeout: 10_000, - env: { - ...process.env, - STATE: state, - MODE: mode, - REA_VERIFY_COMMANDCODE_ACCOUNT_HOME: account, - }, + "REPLACE_CONTRACTS", + JSON.stringify( + new URL("../../../dist/contracts/toolContracts.js", import.meta.url) + .href, + ), + ); + await writeFile(join(state, "commands.mjs"), commands); + await writeFile( + join(state, "fixture.mjs"), + "export async function createOpenAiModelFixture({onRequest}){globalThis.fakeRequest=onRequest;return {baseUrl:'http://127.0.0.1:1',async close(){}};}\n", + ); + await writeFile( + join(state, "hook.mjs"), + "import{registerHooks}from'node:module';import{pathToFileURL}from'node:url';registerHooks({resolve(specifier,context,next){if(context.parentURL?.endsWith('/scripts/verify-commandcode-client.mjs')){let f;if(specifier==='node:os')f='os.mjs';if(specifier==='./lib/verify-commands.mjs')f='commands.mjs';if(specifier==='./verify/clients/model-fixture.mjs')f='fixture.mjs';if(f)return{url:pathToFileURL(process.env.STATE+'/'+f).href,shortCircuit:true};}return next(specifier,context);}});\n", + ); + await writeFile( + join(state, "os.mjs"), + "export * from 'node:os';export function homedir(){return process.env.REA_VERIFY_COMMANDCODE_ACCOUNT_HOME;}\n", + ); + const script = fileURLToPath( + new URL( + "../../../scripts/verify-commandcode-client.mjs", + import.meta.url, + ), + ); + const result = spawnSync( + process.execPath, + ["--import", join(state, "hook.mjs"), script], + { + encoding: "utf8", + timeout: 10_000, + env: { + ...process.env, + STATE: state, + MODE: mode, + REA_VERIFY_COMMANDCODE_ACCOUNT_HOME: account, }, - ); - expect(result.status).not.toBe(0); - const observed = await readFile( - join(state, "observed.json"), - "utf8", - ).then(JSON.parse, () => null); - expect(observed !== null).toBe(allowed); - if (allowed) - expect(observed.second.tool_calls[0].function.name).toBe( - "search_tools", - ); - expect(result.stderr).toContain( - allowed - ? "intentional stop before client output" - : mode === "empty" - ? "Installed skill must have authored frontmatter" - : mode === "empty body" - ? "Installed skill body must not be empty" - : "The model must receive the complete activated skill body", - ); - } finally { - await rm(state, { recursive: true, force: true }); - } + }, + ); + expect(result.status).not.toBe(0); + const observed = await readFile(join(state, "observed.json"), "utf8").then( + JSON.parse, + () => null, + ); + expect(observed !== null).toBe(allowed); + if (allowed) + expect(observed.second.tool_calls[0].function.name).toBe("search_tools"); + expect(result.stderr).toContain( + allowed + ? "intentional stop before client output" + : mode === "empty" + ? "Installed skill must have authored frontmatter" + : mode === "empty body" + ? "Installed skill body must not be empty" + : "The model must receive the complete activated skill body", + ); }, ); diff --git a/tests/boundary/process/darwinProcessRunTokenReader.test.ts b/tests/boundary/process/darwinProcessRunTokenReader.test.ts index 07fc708730..107972a986 100644 --- a/tests/boundary/process/darwinProcessRunTokenReader.test.ts +++ b/tests/boundary/process/darwinProcessRunTokenReader.test.ts @@ -850,8 +850,8 @@ it.skipIf(!onDarwin)( it.skipIf(process.platform === "win32")( "rethrows compile cancellation, removes its temporary root, and permits retry", async () => { - const directory = await mkdtemp( - join(tmpdir(), "rea-process-token-abort-test-"), + const directory = await createTestTempDirectory( + "rea-process-token-abort-test-", ); const fakeCompiler = await writeBlockingCompiler(directory); const reader = createDarwinProcessRunTokenReader({ @@ -894,7 +894,6 @@ it.skipIf(process.platform === "win32")( if (!controller.signal.aborted) controller.abort(); await compilation; await reader.close(); - await rm(directory, { recursive: true, force: true }); } }, ); @@ -994,8 +993,8 @@ it.skipIf(process.platform === "win32")( it.skipIf(process.platform === "win32").each(["first", "second"] as const)( "keeps shared compilation alive when the %s waiter cancels", async (cancelledWaiter) => { - const directory = await mkdtemp( - join(tmpdir(), "rea-process-token-shared-compile-test-"), + const directory = await createTestTempDirectory( + "rea-process-token-shared-compile-test-", ); const fakeCompiler = await writeBlockingCompiler(directory); const reader = createDarwinProcessRunTokenReader({ @@ -1048,7 +1047,6 @@ it.skipIf(process.platform === "win32").each(["first", "second"] as const)( if (!secondController.signal.aborted) secondController.abort(); await Promise.allSettled([firstWaiter, secondWaiterPromise]); await reader.close(); - await rm(directory, { recursive: true, force: true }); } }, ); @@ -1058,8 +1056,8 @@ it .each(["reader-close", "exact-root"] as const)( "retains a failed compilation cleanup for a later %s retry", async (retry) => { - const directory = await mkdtemp( - join(tmpdir(), "rea-process-token-cleanup-retry-test-"), + const directory = await createTestTempDirectory( + "rea-process-token-cleanup-retry-test-", ); const fakeCompiler = await writeBlockingCompiler(directory); const reader = createDarwinProcessRunTokenReader({ @@ -1118,7 +1116,6 @@ it if (compilationRoot !== undefined) await chmod(compilationRoot, 0o700).catch(() => undefined); await reader.close(); - await rm(directory, { recursive: true, force: true }); } }, ); @@ -1126,8 +1123,8 @@ it it.skipIf(process.platform === "win32")( "aborts and cleans an active compilation when the reader closes", async () => { - const directory = await mkdtemp( - join(tmpdir(), "rea-process-token-close-compile-test-"), + const directory = await createTestTempDirectory( + "rea-process-token-close-compile-test-", ); const fakeCompiler = await writeBlockingCompiler(directory); const reader = createDarwinProcessRunTokenReader({ @@ -1160,7 +1157,6 @@ it.skipIf(process.platform === "win32")( } finally { await Promise.allSettled([preparation]); await reader.close(); - await rm(directory, { recursive: true, force: true }); } }, ); @@ -1168,8 +1164,8 @@ it.skipIf(process.platform === "win32")( it.skipIf(process.platform === "win32")( "maps cancellation during native preparation before Electron launch", async () => { - const directory = await mkdtemp( - join(tmpdir(), "rea-electron-token-abort-test-"), + const directory = await createTestTempDirectory( + "rea-electron-token-abort-test-", ); const fakeCompiler = await writeBlockingCompiler(directory); const reader = createDarwinProcessRunTokenReader({ @@ -1215,7 +1211,6 @@ it.skipIf(process.platform === "win32")( prepareSpy.mockRestore(); launchSpy.mockRestore(); await reader.close(); - await rm(directory, { recursive: true, force: true }); } }, ); @@ -1223,8 +1218,8 @@ it.skipIf(process.platform === "win32")( it.skipIf(process.platform === "win32").each(["baseline", "tokens"] as const)( "preserves cancellation through system-host %s inspection", async (kind) => { - const directory = await mkdtemp( - join(tmpdir(), "rea-process-baseline-abort-test-"), + const directory = await createTestTempDirectory( + "rea-process-baseline-abort-test-", ); const fakeCompiler = await writeBlockingIdentityCompiler(directory); const host = createSystemProcessOwnershipHost("darwin", process.env, { @@ -1269,7 +1264,6 @@ it.skipIf(process.platform === "win32").each(["baseline", "tokens"] as const)( await host.close?.(); if (helperRoot !== undefined) await expect(access(helperRoot)).rejects.toThrow(); - await rm(directory, { recursive: true, force: true }); } }, ); @@ -1277,8 +1271,8 @@ it.skipIf(process.platform === "win32").each(["baseline", "tokens"] as const)( it.skipIf(process.platform === "win32")( "passes Electron cancellation into capture-local baseline inspection", async () => { - const directory = await mkdtemp( - join(tmpdir(), "rea-electron-baseline-abort-test-"), + const directory = await createTestTempDirectory( + "rea-electron-baseline-abort-test-", ); const fakeCompiler = await writeBlockingIdentityCompiler(directory); const reader = createDarwinProcessRunTokenReader({ @@ -1343,7 +1337,6 @@ it.skipIf(process.platform === "win32")( baselineSpy.mockRestore(); launchSpy.mockRestore(); await reader.close(); - await rm(directory, { recursive: true, force: true }); } }, ); @@ -1352,8 +1345,8 @@ it.skipIf(!onDarwin)( "parses exact NUL-delimited token records and rejects truncated argv", { tags: ["long-running"] }, async () => { - const root = await mkdtemp( - join(tmpdir(), "rea-process-token-parser-test-"), + const root = await createTestTempDirectory( + "rea-process-token-parser-test-", ); const executable = join(root, "parser-test"); const core = fileURLToPath( @@ -1368,116 +1361,112 @@ it.skipIf(!onDarwin)( import.meta.url, ), ); - try { - await execFileOutput( - "/usr/bin/xcrun", - ["swiftc", core, fixture, "-o", executable], - { timeout: 120_000, stopSignal: SWIFTC_INTERRUPT }, - ); - const { stdout } = await execFileAsync(executable, [], { - encoding: "utf8", - timeout: 10_000, - }); - expect(JSON.parse(stdout)).toEqual({ - argvDecoyFailsClosed: true, - readableEnvironmentWithoutToken: { - runId: null, - environmentEntryCount: 1, - }, - restrictedReadableEnvironmentWithoutToken: { - state: "readable", - reason: null, - }, - restrictedEmptyEnvironment: { - state: "unavailable", - reason: "restricted_process_environment_withheld", - }, - platformEmptyEnvironment: { + await execFileOutput( + "/usr/bin/xcrun", + ["swiftc", core, fixture, "-o", executable], + { timeout: 120_000, stopSignal: SWIFTC_INTERRUPT }, + ); + const { stdout } = await execFileAsync(executable, [], { + encoding: "utf8", + timeout: 10_000, + }); + expect(JSON.parse(stdout)).toEqual({ + argvDecoyFailsClosed: true, + readableEnvironmentWithoutToken: { + runId: null, + environmentEntryCount: 1, + }, + restrictedReadableEnvironmentWithoutToken: { + state: "readable", + reason: null, + }, + restrictedEmptyEnvironment: { + state: "unavailable", + reason: "restricted_process_environment_withheld", + }, + platformEmptyEnvironment: { + state: "unavailable", + reason: "platform_binary_environment_withheld", + }, + restrictedUnavailableEnvironment: { + state: "unavailable", + reason: "restricted_process_environment_withheld", + }, + restrictedAmbiguousEnvironment: { + state: "unavailable", + reason: "restricted_process_environment_withheld", + }, + ordinaryAmbiguousEnvironment: { + state: "unavailable", + reason: "ambiguous_environment_boundary", + }, + ordinaryUnavailableEnvironment: { + state: "unavailable", + reason: "environment_unavailable", + }, + alternatePaddingRead: true, + appleVectorTokenFailsClosed: true, + clearedAppleVectorTokenFailsClosed: true, + callerPfzBeforeTokenRead: true, + emptyLaterArgumentRead: true, + emptyArgv0Read: true, + emptyEnvironmentRecordsBeforeTokenFailClosed: true, + duplicateTokenFailsClosed: true, + invalidTokenEncodingFailsClosed: true, + finalAssignmentArgumentRead: true, + missingAppleBoundaryFailsClosed: true, + malformedBuffersRejected: true, + invalidPointerSizeFailsClosed: true, + incompleteRestrictedArguments: [ + "malformed_procargs_path_padding", + "malformed_procargs_arguments", + "apple_vector_unavailable", + ].map((reason) => [ + { state: "unavailable", reason: "platform_binary_environment_withheld", }, - restrictedUnavailableEnvironment: { + { state: "unavailable", reason: "restricted_process_environment_withheld", }, - restrictedAmbiguousEnvironment: { + { state: "unavailable", reason }, + ]), + signedReadableTokens: [true, true], + signedInvalidTokens: [ + "duplicate_run_token", + "invalid_run_token_encoding", + ].map((reason) => [ + { state: "unavailable", reason }, + { state: "unavailable", reason }, + ]), + malformedStages: [ + { state: "unavailable", reason: "malformed_procargs_header" }, + { state: "unavailable", reason: "malformed_procargs_argument_count" }, + { state: "unavailable", - reason: "restricted_process_environment_withheld", + reason: "malformed_procargs_executable_path", }, - ordinaryAmbiguousEnvironment: { + { state: "unavailable", reason: "malformed_procargs_pointer_size" }, + { state: "unavailable", reason: "malformed_procargs_path_padding" }, + { state: "unavailable", reason: "malformed_procargs_arguments" }, + { state: "unavailable", reason: "malformed_procargs_terminator" }, + { state: "unavailable", - reason: "ambiguous_environment_boundary", + reason: "malformed_procargs_trailing_entries", }, - ordinaryUnavailableEnvironment: { - state: "unavailable", - reason: "environment_unavailable", - }, - alternatePaddingRead: true, - appleVectorTokenFailsClosed: true, - clearedAppleVectorTokenFailsClosed: true, - callerPfzBeforeTokenRead: true, - emptyLaterArgumentRead: true, - emptyArgv0Read: true, - emptyEnvironmentRecordsBeforeTokenFailClosed: true, - duplicateTokenFailsClosed: true, - invalidTokenEncodingFailsClosed: true, - finalAssignmentArgumentRead: true, - missingAppleBoundaryFailsClosed: true, - malformedBuffersRejected: true, - invalidPointerSizeFailsClosed: true, - incompleteRestrictedArguments: [ - "malformed_procargs_path_padding", - "malformed_procargs_arguments", - "apple_vector_unavailable", - ].map((reason) => [ - { - state: "unavailable", - reason: "platform_binary_environment_withheld", - }, - { - state: "unavailable", - reason: "restricted_process_environment_withheld", - }, - { state: "unavailable", reason }, - ]), - signedReadableTokens: [true, true], - signedInvalidTokens: [ - "duplicate_run_token", - "invalid_run_token_encoding", - ].map((reason) => [ - { state: "unavailable", reason }, - { state: "unavailable", reason }, - ]), - malformedStages: [ - { state: "unavailable", reason: "malformed_procargs_header" }, - { state: "unavailable", reason: "malformed_procargs_argument_count" }, - { - state: "unavailable", - reason: "malformed_procargs_executable_path", - }, - { state: "unavailable", reason: "malformed_procargs_pointer_size" }, - { state: "unavailable", reason: "malformed_procargs_path_padding" }, - { state: "unavailable", reason: "malformed_procargs_arguments" }, - { state: "unavailable", reason: "malformed_procargs_terminator" }, - { - state: "unavailable", - reason: "malformed_procargs_trailing_entries", - }, - ], - malformedObservation: { - state: "unavailable", - reason: "malformed_procargs", - }, - nonAsciiPathRead: true, - realTokenRead: true, - reservedArgumentWithRealTokenFailsClosed: true, - titleOverwriteFailsClosed: true, - truncatedArgvFailsClosed: true, - zeroArgumentsRead: true, - }); - } finally { - await rm(root, { recursive: true, force: true }); - } + ], + malformedObservation: { + state: "unavailable", + reason: "malformed_procargs", + }, + nonAsciiPathRead: true, + realTokenRead: true, + reservedArgumentWithRealTokenFailsClosed: true, + titleOverwriteFailsClosed: true, + truncatedArgvFailsClosed: true, + zeroArgumentsRead: true, + }); }, ); @@ -1485,8 +1474,8 @@ it.skipIf(!onDarwin)( "preserves process-table failures and confirms run-token identity", { tags: ["long-running"] }, async () => { - const root = await mkdtemp( - join(tmpdir(), "rea-process-snapshot-retry-test-"), + const root = await createTestTempDirectory( + "rea-process-snapshot-retry-test-", ); const executable = join(root, "snapshot-probe"); const core = fileURLToPath( @@ -1501,48 +1490,44 @@ it.skipIf(!onDarwin)( import.meta.url, ), ); - try { - await execFileOutput( - "/usr/bin/xcrun", - ["swiftc", core, fixture, "-o", executable], - { timeout: 120_000, stopSignal: SWIFTC_INTERRUPT }, - ); - const { stdout } = await execFileAsync(executable, [], { - encoding: "utf8", - env: { ...process.env, REA_PROCESS_RUN_ID: "rea-snapshot-probe" }, - timeout: 10_000, - }); - expect(JSON.parse(stdout)).toEqual({ - preservedOtherErrno: true, - retriedAfterFreshSize: true, - openingRunTokenSnapshotReads: 1, - failedOpeningRunTokens: [42, 43].map((pid) => ({ - pid, + await execFileOutput( + "/usr/bin/xcrun", + ["swiftc", core, fixture, "-o", executable], + { timeout: 120_000, stopSignal: SWIFTC_INTERRUPT }, + ); + const { stdout } = await execFileAsync(executable, [], { + encoding: "utf8", + env: { ...process.env, REA_PROCESS_RUN_ID: "rea-snapshot-probe" }, + timeout: 10_000, + }); + expect(JSON.parse(stdout)).toEqual({ + preservedOtherErrno: true, + retriedAfterFreshSize: true, + openingRunTokenSnapshotReads: 1, + failedOpeningRunTokens: [42, 43].map((pid) => ({ + pid, + state: "unavailable", + reason: "process_table_failed_13", + })), + absentRunTokens: [ + { pid: 42, state: "unavailable", reason: "process_not_in_snapshot" }, + ], + failedConfirmingRunTokens: [ + { state: "unavailable", - reason: "process_table_failed_13", - })), - absentRunTokens: [ - { pid: 42, state: "unavailable", reason: "process_not_in_snapshot" }, - ], - failedConfirmingRunTokens: [ - { - state: "unavailable", - reason: "process_table_failed_5", - tokenPresent: false, - }, - ], - confirmedRunTokens: [{ state: "readable", matchesFixtureToken: true }], - successfulSnapshotMissingPidIsAbsent: true, - failedSnapshotMissingPidIsUnavailable: true, - invalidPidIsUnavailable: true, - zombieLifecycleIsSeparateFromIdentity: true, - effectiveUidComesFromNativeIdentity: true, - nativeRelationIsSeparateFromIdentity: true, - nativeNameIsSeparateFromIdentity: true, - }); - } finally { - await rm(root, { recursive: true, force: true }); - } + reason: "process_table_failed_5", + tokenPresent: false, + }, + ], + confirmedRunTokens: [{ state: "readable", matchesFixtureToken: true }], + successfulSnapshotMissingPidIsAbsent: true, + failedSnapshotMissingPidIsUnavailable: true, + invalidPidIsUnavailable: true, + zombieLifecycleIsSeparateFromIdentity: true, + effectiveUidComesFromNativeIdentity: true, + nativeRelationIsSeparateFromIdentity: true, + nativeNameIsSeparateFromIdentity: true, + }); }, ); @@ -1685,7 +1670,7 @@ it.skipIf(!onDarwin)( it.skipIf(!onDarwin)( "reads owned tokens across executable padding and distinguishes a real empty environment", async () => { - const root = await mkdtemp(join(tmpdir(), "rea-process-token-padding-")); + const root = await createTestTempDirectory("rea-process-token-padding-"); const host = createSystemProcessOwnershipHost("darwin"); const children: Awaited>[] = []; try { @@ -1726,7 +1711,6 @@ it.skipIf(!onDarwin)( } finally { for (const { child } of children) await stopNodeChild(child); await host.close?.(); - await rm(root, { recursive: true, force: true }); } }, ); diff --git a/tests/boundary/process/deepseekVerifierSkillBody.test.ts b/tests/boundary/process/deepseekVerifierSkillBody.test.ts index 2020878453..ec2ec434ad 100644 --- a/tests/boundary/process/deepseekVerifierSkillBody.test.ts +++ b/tests/boundary/process/deepseekVerifierSkillBody.test.ts @@ -4,6 +4,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { expect, it } from "vitest"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const posix = it.skipIf(process.platform === "win32"); posix.each([ @@ -20,64 +21,60 @@ posix.each([ ] as const)( "requires actual skill body when model receives %s content", async (body, allowed) => { - const state = await mkdtemp(join(tmpdir(), "rea-deepseek-skill-")); - try { - const commands = - "import {mkdir,readFile,writeFile} from 'node:fs/promises'; import {join} from 'node:path'; import {TOOL_CONTRACTS} from REPLACE_CONTRACTS;\nlet setup=0;\nexport async function createVerifierCommands(){return {async exec(command,args,options={}){\n if(command==='git')return {stdout:'',stderr:''}; if(args.includes('--version'))return {stdout:'fake',stderr:''};\n const profile=options.env.DSH_HOME;const skill=join(options.env.HOME,'.agents','skills','reverse-engineer-anything');const config=join(profile,'cordis.patch.yml');\n if(args.includes('setup')&&args.includes('--dry-run'))return {stdout:JSON.stringify({status:'planned',plannedActions:[{target:config},{target:skill}]}),stderr:''};\n if(args.includes('setup')){if(setup++===0){await writeFile(config+'.rea.backup',await readFile(config));await mkdir(skill,{recursive:true});await writeFile(join(skill,'SKILL.md'),('---\\nname: reverse-engineer-anything\\n---\\n'+(process.env.BODY==='complete tags'?'Literal and \\n':'')+'Essential \"authored\" instructions\\nKeep a \\\\ literal\\n').replaceAll('\\n',process.env.BODY==='complete CRLF'?'\\r\\n':'\\n'));return {stdout:JSON.stringify({status:'ready'}),stderr:''};}return {stdout:JSON.stringify({appliedActions:[]}),stderr:''};}\n if(args.includes('doctor'))return {stdout:JSON.stringify({healthy:true}),stderr:''};\n const tools=[...TOOL_CONTRACTS.map(t=>({name:'mcp__rea__'+t.name,parameters:{type:'object',properties:{v:{type:'string',pattern:'^x$'}}}})),{name:'skill'}];\n const first=await globalThis.fakeRequest({body:{messages:[]},tools,requestIndex:1});\n const body=((process.env.BODY==='complete tags'?'Literal and \\n':'')+'Essential \"authored\" instructions\\nKeep a \\\\ literal').replaceAll('\\n',process.env.BODY==='complete CRLF'?'\\r\\n':'\\n');\n const escapedSkill=skill.replaceAll('&','&').replaceAll('<','<').replaceAll('>','>'); const wrap='\\n\\n'+escapedSkill+(process.env.BODY==='closed early'?'':'')+(process.env.BODY==='inside resources'?body:'')+'\\n\\n\\n';\n const payload=wrap+(process.env.BODY==='closed wrapper'?'':'')+((process.env.BODY.startsWith('complete')||process.env.BODY==='closed wrapper'||process.env.BODY==='closed early')?'\\n'+body+'\\n\\n':'')+''+(process.env.BODY==='outside wrapper'?body:'');\n const messages=[{role:'tool',content:payload},...(process.env.BODY==='split messages'?[{role:'tool',content:body}]:[])];\n const second=await globalThis.fakeRequest({body:{messages},tools,requestIndex:2});\n await writeFile(process.env.STATE+'/observed.json',JSON.stringify({first,second,payload}));throw Error('intentional stop after empty-skill request');\n },async close(){}};}\n".replace( - "REPLACE_CONTRACTS", - JSON.stringify( - new URL("../../../dist/contracts/toolContracts.js", import.meta.url) - .href, - ), - ); - await writeFile(join(state, "commands.mjs"), commands); - await writeFile( - join(state, "fixture.mjs"), - "export async function createOpenAiModelFixture({onRequest}){globalThis.fakeRequest=onRequest;return {baseUrl:'http://127.0.0.1:1',async close(){}};}\n", + const state = await createTestTempDirectory("rea-deepseek-skill-"); + const commands = + "import {mkdir,readFile,writeFile} from 'node:fs/promises'; import {join} from 'node:path'; import {TOOL_CONTRACTS} from REPLACE_CONTRACTS;\nlet setup=0;\nexport async function createVerifierCommands(){return {async exec(command,args,options={}){\n if(command==='git')return {stdout:'',stderr:''}; if(args.includes('--version'))return {stdout:'fake',stderr:''};\n const profile=options.env.DSH_HOME;const skill=join(options.env.HOME,'.agents','skills','reverse-engineer-anything');const config=join(profile,'cordis.patch.yml');\n if(args.includes('setup')&&args.includes('--dry-run'))return {stdout:JSON.stringify({status:'planned',plannedActions:[{target:config},{target:skill}]}),stderr:''};\n if(args.includes('setup')){if(setup++===0){await writeFile(config+'.rea.backup',await readFile(config));await mkdir(skill,{recursive:true});await writeFile(join(skill,'SKILL.md'),('---\\nname: reverse-engineer-anything\\n---\\n'+(process.env.BODY==='complete tags'?'Literal and \\n':'')+'Essential \"authored\" instructions\\nKeep a \\\\ literal\\n').replaceAll('\\n',process.env.BODY==='complete CRLF'?'\\r\\n':'\\n'));return {stdout:JSON.stringify({status:'ready'}),stderr:''};}return {stdout:JSON.stringify({appliedActions:[]}),stderr:''};}\n if(args.includes('doctor'))return {stdout:JSON.stringify({healthy:true}),stderr:''};\n const tools=[...TOOL_CONTRACTS.map(t=>({name:'mcp__rea__'+t.name,parameters:{type:'object',properties:{v:{type:'string',pattern:'^x$'}}}})),{name:'skill'}];\n const first=await globalThis.fakeRequest({body:{messages:[]},tools,requestIndex:1});\n const body=((process.env.BODY==='complete tags'?'Literal and \\n':'')+'Essential \"authored\" instructions\\nKeep a \\\\ literal').replaceAll('\\n',process.env.BODY==='complete CRLF'?'\\r\\n':'\\n');\n const escapedSkill=skill.replaceAll('&','&').replaceAll('<','<').replaceAll('>','>'); const wrap='\\n\\n'+escapedSkill+(process.env.BODY==='closed early'?'':'')+(process.env.BODY==='inside resources'?body:'')+'\\n\\n\\n';\n const payload=wrap+(process.env.BODY==='closed wrapper'?'':'')+((process.env.BODY.startsWith('complete')||process.env.BODY==='closed wrapper'||process.env.BODY==='closed early')?'\\n'+body+'\\n\\n':'')+''+(process.env.BODY==='outside wrapper'?body:'');\n const messages=[{role:'tool',content:payload},...(process.env.BODY==='split messages'?[{role:'tool',content:body}]:[])];\n const second=await globalThis.fakeRequest({body:{messages},tools,requestIndex:2});\n await writeFile(process.env.STATE+'/observed.json',JSON.stringify({first,second,payload}));throw Error('intentional stop after empty-skill request');\n },async close(){}};}\n".replace( + "REPLACE_CONTRACTS", + JSON.stringify( + new URL("../../../dist/contracts/toolContracts.js", import.meta.url) + .href, + ), ); - await writeFile( - join(state, "hook.mjs"), - "import{registerHooks}from'node:module';import{pathToFileURL}from'node:url';registerHooks({resolve(specifier,context,next){if(context.parentURL?.endsWith('/scripts/verify-deepseek-client.mjs')){let f;if(specifier==='./lib/verify-commands.mjs')f='commands.mjs';if(specifier==='./verify/clients/model-fixture.mjs')f='fixture.mjs';if(f)return{url:pathToFileURL(process.env.STATE+'/'+f).href,shortCircuit:true};}return next(specifier,context);}});\n", - ); - const script = fileURLToPath( - new URL("../../../scripts/verify-deepseek-client.mjs", import.meta.url), - ); - const temporary = - body === "complete escaped" ? join(state, "dir &") : state; - if (temporary !== state) await mkdir(temporary); - const result = spawnSync( - process.execPath, - ["--import", join(state, "hook.mjs"), script], - { - encoding: "utf8", - timeout: 10_000, - env: { - ...process.env, - STATE: state, - BODY: body, - TMPDIR: temporary, - TMP: temporary, - TEMP: temporary, - }, + await writeFile(join(state, "commands.mjs"), commands); + await writeFile( + join(state, "fixture.mjs"), + "export async function createOpenAiModelFixture({onRequest}){globalThis.fakeRequest=onRequest;return {baseUrl:'http://127.0.0.1:1',async close(){}};}\n", + ); + await writeFile( + join(state, "hook.mjs"), + "import{registerHooks}from'node:module';import{pathToFileURL}from'node:url';registerHooks({resolve(specifier,context,next){if(context.parentURL?.endsWith('/scripts/verify-deepseek-client.mjs')){let f;if(specifier==='./lib/verify-commands.mjs')f='commands.mjs';if(specifier==='./verify/clients/model-fixture.mjs')f='fixture.mjs';if(f)return{url:pathToFileURL(process.env.STATE+'/'+f).href,shortCircuit:true};}return next(specifier,context);}});\n", + ); + const script = fileURLToPath( + new URL("../../../scripts/verify-deepseek-client.mjs", import.meta.url), + ); + const temporary = + body === "complete escaped" ? join(state, "dir &") : state; + if (temporary !== state) await mkdir(temporary); + const result = spawnSync( + process.execPath, + ["--import", join(state, "hook.mjs"), script], + { + encoding: "utf8", + timeout: 10_000, + env: { + ...process.env, + STATE: state, + BODY: body, + TMPDIR: temporary, + TMP: temporary, + TEMP: temporary, }, + }, + ); + expect(result.status).not.toBe(0); + const observed = await readFile(join(state, "observed.json"), "utf8").then( + JSON.parse, + () => null, + ); + expect(observed !== null).toBe(allowed); + if (allowed) + expect(observed.second.tool_calls[0].function.name).toBe( + "mcp__rea__analyze_javascript_application", ); - expect(result.status).not.toBe(0); - const observed = await readFile( - join(state, "observed.json"), - "utf8", - ).then(JSON.parse, () => null); - expect(observed !== null).toBe(allowed); - if (allowed) - expect(observed.second.tool_calls[0].function.name).toBe( - "mcp__rea__analyze_javascript_application", - ); - expect(result.stderr).toContain( - allowed - ? "intentional stop after empty-skill request" - : "Harness must load the installed skill before analysis", - ); - } finally { - await rm(state, { recursive: true, force: true }); - } + expect(result.stderr).toContain( + allowed + ? "intentional stop after empty-skill request" + : "Harness must load the installed skill before analysis", + ); }, ); diff --git a/tests/boundary/process/geminiVerifierSkillBody.test.ts b/tests/boundary/process/geminiVerifierSkillBody.test.ts index 422b3b3ec8..c1d1e3ab9c 100644 --- a/tests/boundary/process/geminiVerifierSkillBody.test.ts +++ b/tests/boundary/process/geminiVerifierSkillBody.test.ts @@ -4,6 +4,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { expect, it } from "vitest"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; it.each([ ["empty", false], @@ -17,61 +18,57 @@ it.each([ ] as const)( "requires actual Gemini skill body in %s response", async (body, allowed) => { - const state = await mkdtemp(join(tmpdir(), "rea-gemini-skill-")); - try { - const commands = - "import {mkdir,readFile,writeFile} from 'node:fs/promises';import {join} from 'node:path';import {TOOL_CONTRACTS} from REPLACE_CONTRACTS;\nlet setup=0;export async function createVerifierCommands(){return {async exec(command,args,options={}){\nif(command==='git')return {stdout:'',stderr:''};if(args.includes('--version'))return {stdout:'fake',stderr:''};\nconst profile=options.env.GEMINI_CLI_HOME;const config=join(profile,'.gemini','settings.json');const skill=join(profile,'.agents','skills','reverse-engineer-anything');\nif(args.includes('setup')&&args.includes('--dry-run'))return {stdout:JSON.stringify({status:'planned',plannedActions:[{target:config},{target:join(skill,'SKILL.md')}]}),stderr:''};\nif(args.includes('setup')){if(setup++===0){await writeFile(config+'.rea.backup',await readFile(config));await mkdir(skill,{recursive:true});await writeFile(join(skill,'SKILL.md'),('---\\nname: reverse-engineer-anything\\n---\\n'+(process.env.BODY==='complete tags'?'literal and \\n':'')+'Essential \"instructions\"\\nKeep a \\\\ literal\\n').replaceAll('\\n',process.env.BODY==='complete CRLF'?'\\r\\n':'\\n'));return {stdout:JSON.stringify({status:'ready'}),stderr:''};}return {stdout:JSON.stringify({appliedActions:[]}),stderr:''};}\nif(args.includes('doctor'))return {stdout:JSON.stringify({healthy:true}),stderr:''};\nconst tools=[...TOOL_CONTRACTS.map(t=>({name:'rea__'+t.name,parametersJsonSchema:{type:'object'}})),{name:'activate_skill'}];\nconst first=await globalThis.fakeRequest({body:{contents:[]},tools});\nconst body=((process.env.BODY==='complete tags'?'literal and \\n':'')+'Essential \"instructions\"\\nKeep a \\\\ literal').replaceAll('\\n',process.env.BODY==='complete CRLF'?'\\r\\n':'\\n');\nconst valid=(process.env.BODY.startsWith('complete')||process.env.BODY==='closed early');\nconst instructions=valid?' \\n '+body+'\\n \\n\\n':'';\nconst output='\\n'+instructions+' \\n '+skill+(process.env.BODY==='closed early'?'':'')+(process.env.BODY==='inside resources'?body:'')+'\\n \\n'+(process.env.BODY==='outside wrapper'?body:'');\nconst responses=[{functionResponse:{name:'activate_skill',response:{output}}},...(process.env.BODY==='split messages'?[{functionResponse:{name:'activate_skill',response:{output:body}}}]:[])];\nconst second=await globalThis.fakeRequest({body:{contents:[{parts:responses}]},tools});const payload=output;\nawait writeFile(process.env.STATE+'/observed.json',JSON.stringify({first,second,payload}));throw Error('intentional stop before client mutation');},async close(){}};}\n".replace( - "REPLACE_CONTRACTS", - JSON.stringify( - new URL("../../../dist/contracts/toolContracts.js", import.meta.url) - .href, - ), - ); - await writeFile(join(state, "commands.mjs"), commands); - await writeFile( - join(state, "fixture.mjs"), - "export async function createGeminiModelFixture({onRequest}){globalThis.fakeRequest=onRequest;return {baseUrl:'http://127.0.0.1:1',async close(){}};}\n", + const state = await createTestTempDirectory("rea-gemini-skill-"); + const commands = + "import {mkdir,readFile,writeFile} from 'node:fs/promises';import {join} from 'node:path';import {TOOL_CONTRACTS} from REPLACE_CONTRACTS;\nlet setup=0;export async function createVerifierCommands(){return {async exec(command,args,options={}){\nif(command==='git')return {stdout:'',stderr:''};if(args.includes('--version'))return {stdout:'fake',stderr:''};\nconst profile=options.env.GEMINI_CLI_HOME;const config=join(profile,'.gemini','settings.json');const skill=join(profile,'.agents','skills','reverse-engineer-anything');\nif(args.includes('setup')&&args.includes('--dry-run'))return {stdout:JSON.stringify({status:'planned',plannedActions:[{target:config},{target:join(skill,'SKILL.md')}]}),stderr:''};\nif(args.includes('setup')){if(setup++===0){await writeFile(config+'.rea.backup',await readFile(config));await mkdir(skill,{recursive:true});await writeFile(join(skill,'SKILL.md'),('---\\nname: reverse-engineer-anything\\n---\\n'+(process.env.BODY==='complete tags'?'literal and \\n':'')+'Essential \"instructions\"\\nKeep a \\\\ literal\\n').replaceAll('\\n',process.env.BODY==='complete CRLF'?'\\r\\n':'\\n'));return {stdout:JSON.stringify({status:'ready'}),stderr:''};}return {stdout:JSON.stringify({appliedActions:[]}),stderr:''};}\nif(args.includes('doctor'))return {stdout:JSON.stringify({healthy:true}),stderr:''};\nconst tools=[...TOOL_CONTRACTS.map(t=>({name:'rea__'+t.name,parametersJsonSchema:{type:'object'}})),{name:'activate_skill'}];\nconst first=await globalThis.fakeRequest({body:{contents:[]},tools});\nconst body=((process.env.BODY==='complete tags'?'literal and \\n':'')+'Essential \"instructions\"\\nKeep a \\\\ literal').replaceAll('\\n',process.env.BODY==='complete CRLF'?'\\r\\n':'\\n');\nconst valid=(process.env.BODY.startsWith('complete')||process.env.BODY==='closed early');\nconst instructions=valid?' \\n '+body+'\\n \\n\\n':'';\nconst output='\\n'+instructions+' \\n '+skill+(process.env.BODY==='closed early'?'':'')+(process.env.BODY==='inside resources'?body:'')+'\\n \\n'+(process.env.BODY==='outside wrapper'?body:'');\nconst responses=[{functionResponse:{name:'activate_skill',response:{output}}},...(process.env.BODY==='split messages'?[{functionResponse:{name:'activate_skill',response:{output:body}}}]:[])];\nconst second=await globalThis.fakeRequest({body:{contents:[{parts:responses}]},tools});const payload=output;\nawait writeFile(process.env.STATE+'/observed.json',JSON.stringify({first,second,payload}));throw Error('intentional stop before client mutation');},async close(){}};}\n".replace( + "REPLACE_CONTRACTS", + JSON.stringify( + new URL("../../../dist/contracts/toolContracts.js", import.meta.url) + .href, + ), ); - await writeFile( - join(state, "hook.mjs"), - "import{registerHooks}from'node:module';import{pathToFileURL}from'node:url';registerHooks({resolve(specifier,context,next){if(context.parentURL?.endsWith('/scripts/verify-gemini-client.mjs')){let f;if(specifier==='./lib/verify-commands.mjs')f='commands.mjs';if(specifier==='./verify/clients/gemini-model-fixture.mjs')f='fixture.mjs';if(f)return{url:pathToFileURL(process.env.STATE+'/'+f).href,shortCircuit:true};}return next(specifier,context);}});\n", - ); - const script = fileURLToPath( - new URL("../../../scripts/verify-gemini-client.mjs", import.meta.url), - ); - const result = spawnSync( - process.execPath, - ["--import", join(state, "hook.mjs"), script], - { - encoding: "utf8", - timeout: 10_000, - env: { - ...process.env, - STATE: state, - BODY: body, - TMPDIR: state, - TMP: state, - TEMP: state, - }, + await writeFile(join(state, "commands.mjs"), commands); + await writeFile( + join(state, "fixture.mjs"), + "export async function createGeminiModelFixture({onRequest}){globalThis.fakeRequest=onRequest;return {baseUrl:'http://127.0.0.1:1',async close(){}};}\n", + ); + await writeFile( + join(state, "hook.mjs"), + "import{registerHooks}from'node:module';import{pathToFileURL}from'node:url';registerHooks({resolve(specifier,context,next){if(context.parentURL?.endsWith('/scripts/verify-gemini-client.mjs')){let f;if(specifier==='./lib/verify-commands.mjs')f='commands.mjs';if(specifier==='./verify/clients/gemini-model-fixture.mjs')f='fixture.mjs';if(f)return{url:pathToFileURL(process.env.STATE+'/'+f).href,shortCircuit:true};}return next(specifier,context);}});\n", + ); + const script = fileURLToPath( + new URL("../../../scripts/verify-gemini-client.mjs", import.meta.url), + ); + const result = spawnSync( + process.execPath, + ["--import", join(state, "hook.mjs"), script], + { + encoding: "utf8", + timeout: 10_000, + env: { + ...process.env, + STATE: state, + BODY: body, + TMPDIR: state, + TMP: state, + TEMP: state, }, + }, + ); + expect(result.status).not.toBe(0); + const observed = await readFile(join(state, "observed.json"), "utf8").then( + JSON.parse, + () => null, + ); + expect(observed !== null).toBe(allowed); + if (allowed) + expect(observed.second[0].functionCall.name).toBe( + "rea__analyze_javascript_application", ); - expect(result.status).not.toBe(0); - const observed = await readFile( - join(state, "observed.json"), - "utf8", - ).then(JSON.parse, () => null); - expect(observed !== null).toBe(allowed); - if (allowed) - expect(observed.second[0].functionCall.name).toBe( - "rea__analyze_javascript_application", - ); - expect(result.stderr).toContain( - allowed - ? "intentional stop before client mutation" - : "Gemini must load the installed personal REA skill", - ); - } finally { - await rm(state, { recursive: true, force: true }); - } + expect(result.stderr).toContain( + allowed + ? "intentional stop before client mutation" + : "Gemini must load the installed personal REA skill", + ); }, ); diff --git a/tests/boundary/process/il2cppVerifierRetention.test.ts b/tests/boundary/process/il2cppVerifierRetention.test.ts index 260c31a2b9..141135f73a 100644 --- a/tests/boundary/process/il2cppVerifierRetention.test.ts +++ b/tests/boundary/process/il2cppVerifierRetention.test.ts @@ -5,6 +5,7 @@ import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { promisify } from "node:util"; import { expect, it } from "vitest"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const execute = promisify(execFile); @@ -20,15 +21,14 @@ it.each([ ] as const)( "requires overflow Evidence before reanalysis (%s, retained=%s)", async (collection, retained) => { - const directory = await mkdtemp( - join(tmpdir(), "rea-il2cpp-retention-oracle-"), + const directory = await createTestTempDirectory( + "rea-il2cpp-retention-oracle-", ); - try { - // This recording qualifies the verifier's ordering oracle, not metadata - // decoding or transport conformance. A later analysis repairs retention. - await writeFile( - join(directory, "recording.mjs"), - ` + // This recording qualifies the verifier's ordering oracle, not metadata + // decoding or transport conformance. A later analysis repairs retention. + await writeFile( + join(directory, "recording.mjs"), + ` import {promisify} from 'node:util'; const path=process.env.INPUT; const identity={sha256:'a'.repeat(64),bytes:1}; @@ -56,61 +56,58 @@ export const requireMcpOutput=response=>{if(response.isError)throw Error('Unexpe export const requireMcpToolError=response=>response.error; export const hashVerifierFile=async()=>identity; `, - ); - await writeFile( - join(directory, "hook.mjs"), - ` + ); + await writeFile( + join(directory, "hook.mjs"), + ` import {registerHooks} from 'node:module';import {pathToFileURL} from 'node:url'; registerHooks({resolve(specifier,context,next){ if(context.parentURL?.endsWith('/scripts/verify-il2cpp-metadata.mjs')&&['node:child_process','@modelcontextprotocol/client','@modelcontextprotocol/client/stdio','../dist/contracts/toolContracts.js','./lib/mcp-verifier-results.mjs','./lib/verifier-file-resources.mjs'].includes(specifier))return {shortCircuit:true,url:pathToFileURL(process.env.RECORDING).href}; return next(specifier,context); }}); `, - ); - const result = execute( - process.execPath, - [ - "--import", - join(directory, "hook.mjs"), - fileURLToPath( - new URL( - "../../../scripts/verify-il2cpp-metadata.mjs", - import.meta.url, - ), + ); + const result = execute( + process.execPath, + [ + "--import", + join(directory, "hook.mjs"), + fileURLToPath( + new URL( + "../../../scripts/verify-il2cpp-metadata.mjs", + import.meta.url, ), - "--metadata", - join(directory, "metadata.dat"), - ], - { - timeout: 10_000, - env: { - ...process.env, - RECORDING: join(directory, "recording.mjs"), - INPUT: join(directory, "metadata.dat"), - RETAINED: String(retained), - COLLECTION: collection, - }, + ), + "--metadata", + join(directory, "metadata.dat"), + ], + { + timeout: 10_000, + env: { + ...process.env, + RECORDING: join(directory, "recording.mjs"), + INPUT: join(directory, "metadata.dat"), + RETAINED: String(retained), + COLLECTION: collection, }, + }, + ); + if (retained) { + const { stdout } = await result; + expect(stdout).toContain( + "All asserted IL2CPP metadata CLI/MCP checks passed", ); - if (retained) { - const { stdout } = await result; - expect(stdout).toContain( - "All asserted IL2CPP metadata CLI/MCP checks passed", - ); - expect( - stdout.includes( - "empty queryable tables provide no nonempty row-content witness", - ), - ).toBe(collection === "empty"); - } else { - await expect(result).rejects.toMatchObject({ - stderr: expect.stringContaining( - "Failed delivery Evidence was not retained before reanalysis", - ), - }); - } - } finally { - await rm(directory, { recursive: true, force: true }); + expect( + stdout.includes( + "empty queryable tables provide no nonempty row-content witness", + ), + ).toBe(collection === "empty"); + } else { + await expect(result).rejects.toMatchObject({ + stderr: expect.stringContaining( + "Failed delivery Evidence was not retained before reanalysis", + ), + }); } }, ); diff --git a/tests/boundary/process/nativeUiVerifierActions.test.ts b/tests/boundary/process/nativeUiVerifierActions.test.ts index f75b6cac3f..a7dd2b4688 100644 --- a/tests/boundary/process/nativeUiVerifierActions.test.ts +++ b/tests/boundary/process/nativeUiVerifierActions.test.ts @@ -5,19 +5,19 @@ import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { promisify } from "node:util"; import { expect, it } from "vitest"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const execute = promisify(execFile); it.each(["complete", "drop CLI", "drop MCP"])( "requires each native UI adapter's own action (%s)", async (mode) => { - const directory = await mkdtemp(join(tmpdir(), "rea-ui-action-oracle-")); - try { - // Authored AX recordings qualify adapter action assertions, not macOS - // permissions, native capture, Swift compilation or process ownership. - await writeFile( - join(directory, "recording.mjs"), - ` + const directory = await createTestTempDirectory("rea-ui-action-oracle-"); + // Authored AX recordings qualify adapter action assertions, not macOS + // permissions, native capture, Swift compilation or process ownership. + await writeFile( + join(directory, "recording.mjs"), + ` import {EventEmitter} from 'node:events';import {writeFile} from 'node:fs/promises';import {promisify} from 'node:util'; let count=0; const nodes=()=>[{role:'AXButton',path:[0],title:count===0?'Increment REA fixture':'REA fixture incremented '+count}]; @@ -37,53 +37,50 @@ export const observeNativeUi=async(target,operation)=>{ export const withArtifactMcp=async(_path,run)=>run({}); export const artifactMcpResult=async(_client,name)=>{if(name==='observe_native_ui')return {initial:{nodes:nodes()}};if(process.env.MODE!=='drop MCP')count++;return captured();}; `, - ); - await writeFile( - join(directory, "hook.mjs"), - ` + ); + await writeFile( + join(directory, "hook.mjs"), + ` import {registerHooks} from 'node:module';import {pathToFileURL} from 'node:url'; const darwinHost='data:text/javascript,'+encodeURIComponent('export const HOST_PLATFORM=Object.freeze({platform:"darwin",architecture:'+JSON.stringify(process.arch)+'});');process.kill=()=>{const open=globalThis.reaOpenChild;if(open?.exitCode===null){open.exitCode=0;queueMicrotask(()=>open.emit('exit',0,null));}return true;}; registerHooks({resolve(specifier,context,next){if(context.parentURL?.endsWith('/scripts/verify-native-ui.mjs')&&specifier==='../dist/platform/HostPlatform.js')return {shortCircuit:true,url:darwinHost};if(context.parentURL?.endsWith('/scripts/verify-native-ui.mjs')&&['node:child_process','../dist/application/BinaryTargetResolver.js','../dist/native/NativeUiObservation.js','./lib/artifact-e2e.mjs','../dist/process/ProcessOwnershipObservation.js'].includes(specifier))return {shortCircuit:true,url:pathToFileURL(process.env.RECORDING).href};return next(specifier,context);}}); `, - ); - const result = execute( - process.execPath, - [ - "--import", - join(directory, "hook.mjs"), - fileURLToPath( - new URL("../../../scripts/verify-native-ui.mjs", import.meta.url), - ), - ], - { - timeout: 10_000, - env: { - ...process.env, - TMPDIR: directory, - TMP: directory, - TEMP: directory, - RECORDING: join(directory, "recording.mjs"), - MODE: mode, - }, + ); + const result = execute( + process.execPath, + [ + "--import", + join(directory, "hook.mjs"), + fileURLToPath( + new URL("../../../scripts/verify-native-ui.mjs", import.meta.url), + ), + ], + { + timeout: 10_000, + env: { + ...process.env, + TMPDIR: directory, + TMP: directory, + TEMP: directory, + RECORDING: join(directory, "recording.mjs"), + MODE: mode, }, - ); - if (mode === "complete") { - expect(JSON.parse((await result).stdout)).toMatchObject({ - ok: true, - positive_e2e: true, - scenario: "selected-AX-button-action-and-CLI-MCP-parity-observed", - }); - } else { - await expect(result).rejects.toMatchObject({ - stderr: expect.stringContaining( - mode === "drop CLI" - ? "Native UI CLI did not preserve the selected scenario" - : "MCP did not observe the selected fixture's AX action", - ), - }); - } - } finally { - await rm(directory, { recursive: true, force: true }); + }, + ); + if (mode === "complete") { + expect(JSON.parse((await result).stdout)).toMatchObject({ + ok: true, + positive_e2e: true, + scenario: "selected-AX-button-action-and-CLI-MCP-parity-observed", + }); + } else { + await expect(result).rejects.toMatchObject({ + stderr: expect.stringContaining( + mode === "drop CLI" + ? "Native UI CLI did not preserve the selected scenario" + : "MCP did not observe the selected fixture's AX action", + ), + }); } }, ); diff --git a/tests/boundary/process/processCaptureNormalization.test.ts b/tests/boundary/process/processCaptureNormalization.test.ts index d21a463150..cb6966d988 100644 --- a/tests/boundary/process/processCaptureNormalization.test.ts +++ b/tests/boundary/process/processCaptureNormalization.test.ts @@ -13,7 +13,10 @@ import { import { parseProcessCapture } from "../../../src/domain/process/processCaptureParsing.js"; import { compareProcessCaptures } from "../../../src/domain/process/processComparison.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { itWithCaptureCapability } from "./processCaptureCapability.js"; +import { + CAPTURE_SKIP_REASON, + itWithCaptureCapability, +} from "./processCaptureCapability.js"; const processFixture = fileURLToPath( new URL("../../fixtures/processIdentity.mjs", import.meta.url), @@ -25,8 +28,8 @@ const identitySchema = z.object({ session_id: z.number().int().positive(), }); -const itWithLinuxCaptureCapability = - process.platform === "linux" ? itWithCaptureCapability : it.skip; +const linuxCaptureSkip = + process.platform !== "linux" || CAPTURE_SKIP_REASON !== false; itWithCaptureCapability( "captures a POSIX PATH executable with a literal backslash in its basename", @@ -180,8 +183,10 @@ itWithCaptureCapability.each([ 20_000, ); -itWithLinuxCaptureCapability.each([false, true])( - "honors pids=%s in owned Linux capture samples and committed Evidence", +it.skipIf(linuxCaptureSkip).each([false, true])( + linuxCaptureSkip && CAPTURE_SKIP_REASON !== false + ? `honors pids=%s in owned Linux capture samples and committed Evidence — ${CAPTURE_SKIP_REASON}` + : "honors pids=%s in owned Linux capture samples and committed Evidence", async (pids) => { const root = await createTestTempDirectory("rea-process-identifiers-"); const identityPath = join(root, "identity.json"); diff --git a/tests/boundary/process/processCleanupRetryOwnership.test.ts b/tests/boundary/process/processCleanupRetryOwnership.test.ts index af10da763d..38f1e318d6 100644 --- a/tests/boundary/process/processCleanupRetryOwnership.test.ts +++ b/tests/boundary/process/processCleanupRetryOwnership.test.ts @@ -13,6 +13,7 @@ import { type ProcessCaptureCleanupHost, type ProcessCaptureResourceCleanupOptions, } from "../../../src/process/capture/ProcessCaptureLifecycle.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const cleanupOptions = ( temporaryRoot: string, @@ -34,7 +35,7 @@ const cleanupOptions = ( }; const createRoot = (): Promise => - mkdtemp(join(tmpdir(), "rea-process-cleanup-retry-")); + createTestTempDirectory("rea-process-cleanup-retry-"); const expectCaptureError = (operation: () => unknown): ProcessCaptureError => { try { @@ -101,7 +102,6 @@ it("resolves the origin and finalization copy after the exact root retry", async } finally { denyRemoval = false; await scope.close(); - await rm(root, { recursive: true, force: true }); } }); @@ -183,9 +183,6 @@ it("keeps mixed root errors and retry aggregates bound to only unresolved roots" } finally { denied.clear(); await scope.close(); - await Promise.all( - roots.map((root) => rm(root, { recursive: true, force: true })), - ); } }); @@ -215,7 +212,6 @@ it("joins an already-started release when shutdown begins", async () => { resourceRelease.release(); await Promise.allSettled([releasing, ...(closing ? [closing] : [])]); await scope.close(); - await rm(root, { recursive: true, force: true }); } await releasing; await closing; @@ -256,7 +252,6 @@ it("binds a historical failed report after its owner already retried", async () } finally { denyRemoval = false; await scope.close(); - await rm(root, { recursive: true, force: true }); } }); @@ -308,7 +303,6 @@ it("blocks scope admission on owner-bound retry failure and resumes after releas } finally { denyRemoval = false; await scope.close(); - await rm(root, { recursive: true, force: true }); } }); @@ -339,6 +333,5 @@ it("keeps historical report edits separate from cleanup authority", async () => } finally { denied = false; await scope.close(); - await rm(root, { recursive: true, force: true }); } }); diff --git a/tests/boundary/process/processSnapshotHandleOwnership.test.ts b/tests/boundary/process/processSnapshotHandleOwnership.test.ts index 51904e5b8e..67f01c3005 100644 --- a/tests/boundary/process/processSnapshotHandleOwnership.test.ts +++ b/tests/boundary/process/processSnapshotHandleOwnership.test.ts @@ -114,11 +114,10 @@ import { } from "../../../src/process/capture/ProcessCaptureLifecycle.js"; import { ProcessCaptureError } from "../../../src/process/capture/ProcessCaptureError.js"; import { classifyFilesystemEffects } from "../../../src/process/capture/ProcessFilesystemEffects.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -const roots: string[] = []; const trackedRoot = async (): Promise => { - const root = await mkdtemp(join(tmpdir(), "rea-snapshot-handle-owner-")); - roots.push(root); + const root = await createTestTempDirectory("rea-snapshot-handle-owner-"); return root; }; @@ -210,9 +209,6 @@ afterEach(async () => { } }); } - await Promise.all( - roots.splice(0).map((root) => rm(root, { recursive: true, force: true })), - ); }); it("retains a completed file hash and its exact descriptor owner when close fails", async () => { diff --git a/tests/boundary/process/qwenVerifierSkillBody.test.ts b/tests/boundary/process/qwenVerifierSkillBody.test.ts index f6ef708561..79d2b40e97 100644 --- a/tests/boundary/process/qwenVerifierSkillBody.test.ts +++ b/tests/boundary/process/qwenVerifierSkillBody.test.ts @@ -4,6 +4,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { expect, it } from "vitest"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; it.each([ ["no skill tool", false], @@ -18,63 +19,59 @@ it.each([ ["output terminal failure", false], ["output success", true], ] as const)("binds Qwen skill evidence for %s", async (mode, loaded) => { - const state = await mkdtemp(join(tmpdir(), "rea-qwen-skill-")); - try { - await writeFile( - join(state, "commands.mjs"), - "import {mkdir,readFile,writeFile} from 'node:fs/promises';import {join} from 'node:path';\nexport async function createVerifierCommands(){return {async exec(command,args,options={}){\n if(args.includes('--version'))return {stdout:'fake',stderr:''};\n const profile=options.env.QWEN_HOME;const config=join(profile,'settings.json');\n if(args.includes('setup')&&args.includes('--dry-run'))return {stdout:JSON.stringify({plannedActions:[{target:config}]}),stderr:''};\n if(args.includes('setup')){\n const original=await readFile(config,'utf8');await writeFile(config+'.rea.backup',original);\n const value=JSON.parse(original);value.mcpServers.rea={command:'/fake/rea',args:['mcp']};await writeFile(config,JSON.stringify(value));\n const skill=join(options.env.HOME,'.agents','skills','reverse-engineer-anything');await mkdir(skill,{recursive:true});\n const source='---\\nname: reverse-engineer-anything\\n---\\nQwen \"authored\" body\\nKeep a \\\\ literal\\n';\n await writeFile(join(skill,'SKILL.md'),process.env.MODE==='CRLF'?source.replaceAll('\\n','\\r\\n'):source);\n return {stdout:'{}',stderr:''};\n }\n if(args.includes('doctor'))return {stdout:'{}',stderr:''};\n if(process.env.MODE.startsWith('output ')){await globalThis.fakeRequest({body:{messages:[]},tools:[],requestIndex:1});const {TOOL_CONTRACTS}=await import(process.env.CONTRACTS);const events=[{type:'system',subtype:'init',tools:TOOL_CONTRACTS.map(c=>'mcp__rea__'+c.name)}];const success={type:'result',subtype:'success',is_error:false,result:process.env.MODE==='output missing marker'?'': 'REA_CLIENT_COMPATIBILITY_OK'};if(process.env.MODE!=='output missing result')events.push(success);if(process.env.MODE==='output terminal failure')events.push({type:'result',subtype:'error_during_execution',is_error:true});return {stdout:events.map(e=>JSON.stringify(e)).join('\\n')+'\\n',stderr:''};}\n const skill=join(options.env.HOME,'.agents','skills','reverse-engineer-anything');\n const content='Base directory for this skill: '+skill+'\\nImportant: ALWAYS resolve absolute paths from this base directory when working with skills.\\n\\nQwen \"authored\" body\\nKeep a \\\\ literal\\n';\n const tools=[...(process.env.MODE==='no skill tool'?[]:[{name:'skill'}]),{name:'mcp__rea__analyze_javascript_application'}];\n const first=await globalThis.fakeRequest({body:{messages:[]},tools,requestIndex:1});const callId=first.tool_calls[0].id;\n let messages=(process.env.MODE==='user mention'||process.env.MODE==='no skill tool')?[{role:'user',content:'Unrelated '+skill}]:[{role:'tool',tool_call_id:process.env.MODE==='wrong id'?'other':callId,content:process.env.MODE==='missing body'?'Base directory for this skill: '+skill:content}];\n if(process.env.MODE==='split')messages=[{role:'tool',tool_call_id:callId,content:'Base directory for this skill: '+skill},{role:'tool',tool_call_id:'other',content}];\n const second=await globalThis.fakeRequest({body:{messages},tools,requestIndex:2});await writeFile(process.env.STATE+'/observed.json',JSON.stringify({first,second,messages}));throw Error('intentional stop before client output');\n},async close(){}};}\n", - ); - await writeFile( - join(state, "fixture.mjs"), - "export async function createOpenAiModelFixture({onRequest}){globalThis.fakeRequest=onRequest;return {baseUrl:'http://127.0.0.1:1',async close(){}};}\n", - ); - await writeFile( + const state = await createTestTempDirectory("rea-qwen-skill-"); + await writeFile( + join(state, "commands.mjs"), + "import {mkdir,readFile,writeFile} from 'node:fs/promises';import {join} from 'node:path';\nexport async function createVerifierCommands(){return {async exec(command,args,options={}){\n if(args.includes('--version'))return {stdout:'fake',stderr:''};\n const profile=options.env.QWEN_HOME;const config=join(profile,'settings.json');\n if(args.includes('setup')&&args.includes('--dry-run'))return {stdout:JSON.stringify({plannedActions:[{target:config}]}),stderr:''};\n if(args.includes('setup')){\n const original=await readFile(config,'utf8');await writeFile(config+'.rea.backup',original);\n const value=JSON.parse(original);value.mcpServers.rea={command:'/fake/rea',args:['mcp']};await writeFile(config,JSON.stringify(value));\n const skill=join(options.env.HOME,'.agents','skills','reverse-engineer-anything');await mkdir(skill,{recursive:true});\n const source='---\\nname: reverse-engineer-anything\\n---\\nQwen \"authored\" body\\nKeep a \\\\ literal\\n';\n await writeFile(join(skill,'SKILL.md'),process.env.MODE==='CRLF'?source.replaceAll('\\n','\\r\\n'):source);\n return {stdout:'{}',stderr:''};\n }\n if(args.includes('doctor'))return {stdout:'{}',stderr:''};\n if(process.env.MODE.startsWith('output ')){await globalThis.fakeRequest({body:{messages:[]},tools:[],requestIndex:1});const {TOOL_CONTRACTS}=await import(process.env.CONTRACTS);const events=[{type:'system',subtype:'init',tools:TOOL_CONTRACTS.map(c=>'mcp__rea__'+c.name)}];const success={type:'result',subtype:'success',is_error:false,result:process.env.MODE==='output missing marker'?'': 'REA_CLIENT_COMPATIBILITY_OK'};if(process.env.MODE!=='output missing result')events.push(success);if(process.env.MODE==='output terminal failure')events.push({type:'result',subtype:'error_during_execution',is_error:true});return {stdout:events.map(e=>JSON.stringify(e)).join('\\n')+'\\n',stderr:''};}\n const skill=join(options.env.HOME,'.agents','skills','reverse-engineer-anything');\n const content='Base directory for this skill: '+skill+'\\nImportant: ALWAYS resolve absolute paths from this base directory when working with skills.\\n\\nQwen \"authored\" body\\nKeep a \\\\ literal\\n';\n const tools=[...(process.env.MODE==='no skill tool'?[]:[{name:'skill'}]),{name:'mcp__rea__analyze_javascript_application'}];\n const first=await globalThis.fakeRequest({body:{messages:[]},tools,requestIndex:1});const callId=first.tool_calls[0].id;\n let messages=(process.env.MODE==='user mention'||process.env.MODE==='no skill tool')?[{role:'user',content:'Unrelated '+skill}]:[{role:'tool',tool_call_id:process.env.MODE==='wrong id'?'other':callId,content:process.env.MODE==='missing body'?'Base directory for this skill: '+skill:content}];\n if(process.env.MODE==='split')messages=[{role:'tool',tool_call_id:callId,content:'Base directory for this skill: '+skill},{role:'tool',tool_call_id:'other',content}];\n const second=await globalThis.fakeRequest({body:{messages},tools,requestIndex:2});await writeFile(process.env.STATE+'/observed.json',JSON.stringify({first,second,messages}));throw Error('intentional stop before client output');\n},async close(){}};}\n", + ); + await writeFile( + join(state, "fixture.mjs"), + "export async function createOpenAiModelFixture({onRequest}){globalThis.fakeRequest=onRequest;return {baseUrl:'http://127.0.0.1:1',async close(){}};}\n", + ); + await writeFile( + join(state, "hook.mjs"), + "import {registerHooks}from'node:module';import{pathToFileURL}from'node:url';registerHooks({resolve(specifier,context,next){if(context.parentURL?.endsWith('/scripts/verify-qwen-client.mjs')){let f;if(specifier==='./lib/verify-commands.mjs')f='commands.mjs';if(specifier==='./verify/clients/model-fixture.mjs')f='fixture.mjs';if(f)return{url:pathToFileURL(process.env.STATE+'/'+f).href,shortCircuit:true};}return next(specifier,context);}});\n", + ); + const script = fileURLToPath( + new URL("../../../scripts/verify-qwen-client.mjs", import.meta.url), + ); + const result = spawnSync( + process.execPath, + [ + "--import", join(state, "hook.mjs"), - "import {registerHooks}from'node:module';import{pathToFileURL}from'node:url';registerHooks({resolve(specifier,context,next){if(context.parentURL?.endsWith('/scripts/verify-qwen-client.mjs')){let f;if(specifier==='./lib/verify-commands.mjs')f='commands.mjs';if(specifier==='./verify/clients/model-fixture.mjs')f='fixture.mjs';if(f)return{url:pathToFileURL(process.env.STATE+'/'+f).href,shortCircuit:true};}return next(specifier,context);}});\n", - ); - const script = fileURLToPath( - new URL("../../../scripts/verify-qwen-client.mjs", import.meta.url), - ); - const result = spawnSync( - process.execPath, - [ - "--import", - join(state, "hook.mjs"), - script, - ...(mode.startsWith("output ") ? ["chat"] : []), - ], - { - encoding: "utf8", - timeout: 10_000, - env: { - ...process.env, - STATE: state, - MODE: mode, - CONTRACTS: new URL( - "../../../dist/contracts/toolContracts.js", - import.meta.url, - ).href, - TMPDIR: state, - TMP: state, - TEMP: state, - }, + script, + ...(mode.startsWith("output ") ? ["chat"] : []), + ], + { + encoding: "utf8", + timeout: 10_000, + env: { + ...process.env, + STATE: state, + MODE: mode, + CONTRACTS: new URL( + "../../../dist/contracts/toolContracts.js", + import.meta.url, + ).href, + TMPDIR: state, + TMP: state, + TEMP: state, }, - ); - if (mode.startsWith("output ")) { - // Authored stream result shape from Qwen's BaseJsonOutputAdapter at - // QwenLM/qwen-code c238beb56fa3c65a0f62ad7a3b2cd14db978b998. - expect(result.status, result.stderr).toBe(loaded ? 0 : 1); - const receipt = JSON.parse(result.stdout.trim().split("\n")[0] ?? ""); - expect(receipt.finalSeen).toBe(loaded); - expect(receipt.passed).toBe(loaded); - return; - } - expect(result.status).not.toBe(0); - expect(result.stderr).toContain("intentional stop before client output"); + }, + ); + if (mode.startsWith("output ")) { + // Authored stream result shape from Qwen's BaseJsonOutputAdapter at + // QwenLM/qwen-code c238beb56fa3c65a0f62ad7a3b2cd14db978b998. + expect(result.status, result.stderr).toBe(loaded ? 0 : 1); const receipt = JSON.parse(result.stdout.trim().split("\n")[0] ?? ""); - expect(receipt.selectedSkillLoaded).toBe(loaded); - expect(receipt.passed).toBe(false); - } finally { - await rm(state, { recursive: true, force: true }); + expect(receipt.finalSeen).toBe(loaded); + expect(receipt.passed).toBe(loaded); + return; } + expect(result.status).not.toBe(0); + expect(result.stderr).toContain("intentional stop before client output"); + const receipt = JSON.parse(result.stdout.trim().split("\n")[0] ?? ""); + expect(receipt.selectedSkillLoaded).toBe(loaded); + expect(receipt.passed).toBe(false); }); diff --git a/tests/boundary/process/selectedExecutableCleanup.test.ts b/tests/boundary/process/selectedExecutableCleanup.test.ts index ec4f253a6b..c867269632 100644 --- a/tests/boundary/process/selectedExecutableCleanup.test.ts +++ b/tests/boundary/process/selectedExecutableCleanup.test.ts @@ -54,11 +54,10 @@ import { observeSelectedExecutable, } from "../../../src/process/capture/ProcessCaptureLifecycle.js"; import { ProcessCaptureReadFailure } from "../../../src/process/capture/ProcessCaptureRead.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -const roots: string[] = []; const createExecutable = async (content: string): Promise => { - const root = await mkdtemp(join(tmpdir(), "rea-selected-executable-owner-")); - roots.push(root); + const root = await createTestTempDirectory("rea-selected-executable-owner-"); const path = join(root, "selected-fixture"); await writeFile(path, content); return path; @@ -97,9 +96,6 @@ afterEach(async () => { injected.abortOnClose = undefined; injected.opened = []; injected.closed = []; - await Promise.all( - roots.splice(0).map((root) => rm(root, { recursive: true, force: true })), - ); vi.restoreAllMocks(); }); diff --git a/tests/composition/reverseEngineering.test.ts b/tests/composition/reverseEngineering.test.ts index 9fc8434b3d..562787e1b8 100644 --- a/tests/composition/reverseEngineering.test.ts +++ b/tests/composition/reverseEngineering.test.ts @@ -23,12 +23,13 @@ import { projectAnalysisError } from "../../src/domain/analysisErrorProjection.j import { AnalysisCancelledError } from "../../src/domain/analysisErrorCore.js"; import { rizinCommandLimitations } from "../../src/rizin/RizinCommand.js"; import { objdumpCommandLimitations } from "../../src/objdump/ObjdumpCommand.js"; +import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; it.each(["objdump", "rizin"] as const)( "recovers a bounded %s capture after the caller raises output admission", async (provider) => { - const directory = await mkdtemp( - join(tmpdir(), "rea-reverse-output-admission-"), + const directory = await createTestTempDirectory( + "rea-reverse-output-admission-", ); const path = join(directory, "input.bin"); await writeFile(path, "selected artifact"); @@ -99,14 +100,13 @@ it.each(["objdump", "rizin"] as const)( }); } finally { await service.close(); - await rm(directory, { recursive: true, force: true }); } }, ); describe("reverse engineering command services", () => { it("uses compatible local DWARF link flags and disables debuginfod through the environment", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-reverse-tools-")); + const directory = await createTestTempDirectory("rea-reverse-tools-"); const path = join(directory, "fixture.bin"); await writeFile(path, Buffer.from([0x7f, 0x45, 0x4c, 0x46])); const calls: { @@ -129,38 +129,34 @@ describe("reverse engineering command services", () => { }, }); - try { - const result = await service.inspectWithObjdump({ - path, - operation: "dwarf", - follow_debug_links: true, - }); - if (!result.ok) throw result.error; - expect(result.ok).toBe(true); - if (!result.ok) return; - expect(calls).toEqual([ - { - command: resolve("/tools/objdump"), - args: ["-W", "-WK", path], - env: expect.objectContaining({ DEBUGINFOD_URLS: "" }), - }, - ]); - expect(result.value.raw_result).toMatchObject({ - stdout: "DWARF output\n", - stderr: "diagnostic\n", - exit_code: 0, - output_truncated: false, - }); - expect(result.value.provider.id).toBe("objdump.command"); - expect(result.value.subject?.local_path).toBe(path); - } finally { - await rm(directory, { recursive: true, force: true }); - } + const result = await service.inspectWithObjdump({ + path, + operation: "dwarf", + follow_debug_links: true, + }); + if (!result.ok) throw result.error; + expect(result.ok).toBe(true); + if (!result.ok) return; + expect(calls).toEqual([ + { + command: resolve("/tools/objdump"), + args: ["-W", "-WK", path], + env: expect.objectContaining({ DEBUGINFOD_URLS: "" }), + }, + ]); + expect(result.value.raw_result).toMatchObject({ + stdout: "DWARF output\n", + stderr: "diagnostic\n", + exit_code: 0, + output_truncated: false, + }); + expect(result.value.provider.id).toBe("objdump.command"); + expect(result.value.subject?.local_path).toBe(path); }); it("rejects non-regular artifacts without launching the provider", async () => { - const directory = await mkdtemp( - join(tmpdir(), "rea-reverse-special-file-"), + const directory = await createTestTempDirectory( + "rea-reverse-special-file-", ); let invoked = false; const service = new ReverseEngineeringService({ @@ -170,31 +166,27 @@ describe("reverse engineering command services", () => { }, }); - try { - const result = await service.inspectWithObjdump({ - path: directory, - operation: "file_headers", - }); - expect(result).toMatchObject({ - ok: false, - error: { - _tag: "AnalysisInputError", - operation: "inspect_with_objdump", - cause: { - code: "EINVAL", - message: "Artifact digest requires a regular file", - }, + const result = await service.inspectWithObjdump({ + path: directory, + operation: "file_headers", + }); + expect(result).toMatchObject({ + ok: false, + error: { + _tag: "AnalysisInputError", + operation: "inspect_with_objdump", + cause: { + code: "EINVAL", + message: "Artifact digest requires a regular file", }, - }); - expect(invoked).toBe(false); - } finally { - await rm(directory, { recursive: true, force: true }); - } + }, + }); + expect(invoked).toBe(false); }); it("classifies captured spawn failures instead of reporting provider evidence", async () => { - const directory = await mkdtemp( - join(tmpdir(), "rea-objdump-spawn-failure-"), + const directory = await createTestTempDirectory( + "rea-objdump-spawn-failure-", ); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); @@ -204,24 +196,20 @@ describe("reverse engineering command services", () => { }, }); - try { - const result = await service.inspectWithObjdump({ - path, - operation: "file_headers", - }); - expect(result.ok).toBe(false); - if (!result.ok) { - expect(result.error._tag).toBe("AnalysisCapabilityUnavailableError"); - expect(result.error.message).toContain("ENOENT"); - expect(result.error.message).not.toContain("stdout"); - } - } finally { - await rm(directory, { recursive: true, force: true }); + const result = await service.inspectWithObjdump({ + path, + operation: "file_headers", + }); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error._tag).toBe("AnalysisCapabilityUnavailableError"); + expect(result.error.message).toContain("ENOENT"); + expect(result.error.message).not.toContain("stdout"); } }); it("marks max-buffer termination as unknown completion while retaining output", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-objdump-max-buffer-")); + const directory = await createTestTempDirectory("rea-objdump-max-buffer-"); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); const service = new ReverseEngineeringService({ @@ -237,25 +225,21 @@ describe("reverse engineering command services", () => { }, }); - try { - const result = await service.executeRizinCommand({ - path, - command: "wx 00 @ 0", + const result = await service.executeRizinCommand({ + path, + command: "wx 00 @ 0", + }); + expect(result.ok).toBe(true); + if (result.ok) { + expect(result.value.raw_result).toMatchObject({ + stdout: "captured partial output", + exit_code: null, + output_truncated: true, + completion_status: "unknown", }); - expect(result.ok).toBe(true); - if (result.ok) { - expect(result.value.raw_result).toMatchObject({ - stdout: "captured partial output", - exit_code: null, - output_truncated: true, - completion_status: "unknown", - }); - expect(result.value.limitations).toContainEqual( - expect.stringContaining("max_output_bytes=16777216"), - ); - } - } finally { - await rm(directory, { recursive: true, force: true }); + expect(result.value.limitations).toContainEqual( + expect.stringContaining("max_output_bytes=16777216"), + ); } }); }); @@ -279,7 +263,7 @@ it.each([ ] as const)( "scopes the debug-link limitation to %s when following links is %s", async (operation, followDebugLinks, symbol, flags, warns) => { - const directory = await mkdtemp(join(tmpdir(), "rea-objdump-limitations-")); + const directory = await createTestTempDirectory("rea-objdump-limitations-"); const path = join(directory, "fixture.bin"); await writeFile(path, "inert artifact"); const calls: (readonly string[])[] = []; @@ -321,13 +305,12 @@ it.each([ ).toBe(warns); } finally { await service.close(); - await rm(directory, { recursive: true, force: true }); } }, ); it("uses one selected environment snapshot for provider selection and execution", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-provider-environment-")); + const directory = await createTestTempDirectory("rea-provider-environment-"); const path = join(directory, "fixture.bin"); await writeFile(path, Buffer.from([0x7f, 0x45, 0x4c, 0x46])); const selectedEnvironment = { @@ -345,29 +328,25 @@ it("uses one selected environment snapshot for provider selection and execution" selectedEnvironment.REA_RIZIN_COMMAND = "/changed/rizin"; selectedEnvironment.SELECTED_VALUE = "changed after construction"; - try { - const result = await service.executeRizinCommand({ - path, - command: "iI", - }); - expect(result.ok).toBe(true); - expect(calls).toEqual([ - { - command: resolve("/selected/rizin"), - env: { - REA_RIZIN_COMMAND: "/selected/rizin", - PATH: "/selected/bin", - SELECTED_VALUE: "captured", - }, + const result = await service.executeRizinCommand({ + path, + command: "iI", + }); + expect(result.ok).toBe(true); + expect(calls).toEqual([ + { + command: resolve("/selected/rizin"), + env: { + REA_RIZIN_COMMAND: "/selected/rizin", + PATH: "/selected/bin", + SELECTED_VALUE: "captured", }, - ]); - } finally { - await rm(directory, { recursive: true, force: true }); - } + }, + ]); }); it("marks signal-terminated provider commands as unknown completion", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-provider-signal-exit-")); + const directory = await createTestTempDirectory("rea-provider-signal-exit-"); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); let invocation = 0; @@ -388,55 +367,51 @@ it("marks signal-terminated provider commands as unknown completion", async () = }, }); - try { - const signaled = await service.executeRizinCommand({ - path, - command: "wx 00 @ 0", + const signaled = await service.executeRizinCommand({ + path, + command: "wx 00 @ 0", + }); + expect(signaled.ok).toBe(true); + if (signaled.ok) { + expect(signaled.value.raw_result).toMatchObject({ + stdout: "partial output", + exit_code: null, + signal: "SIGKILL", + completion_status: "unknown", }); - expect(signaled.ok).toBe(true); - if (signaled.ok) { - expect(signaled.value.raw_result).toMatchObject({ - stdout: "partial output", - exit_code: null, - signal: "SIGKILL", - completion_status: "unknown", - }); - expect(signaled.value.limitations.join(" ")).toContain("signal"); - } - const ordinaryExit = await service.executeRizinCommand({ - path, - command: "iI", + expect(signaled.value.limitations.join(" ")).toContain("signal"); + } + const ordinaryExit = await service.executeRizinCommand({ + path, + command: "iI", + }); + expect(ordinaryExit.ok).toBe(true); + if (ordinaryExit.ok) { + expect(ordinaryExit.value.raw_result).toMatchObject({ + stdout: "ordinary partial output", + exit_code: 2, + signal: null, + completion_status: "complete", }); - expect(ordinaryExit.ok).toBe(true); - if (ordinaryExit.ok) { - expect(ordinaryExit.value.raw_result).toMatchObject({ - stdout: "ordinary partial output", - exit_code: 2, - signal: null, - completion_status: "complete", - }); - expect(ordinaryExit.value.limitations.join(" ")).not.toContain( - "terminated by signal", - ); - expect(ordinaryExit.value.limitations).toContainEqual( - expect.stringMatching( - /files.*network.*outside the selected artifact.*not bound to Evidence/iu, - ), - ); - expect(ordinaryExit.value.limitations).toEqual( - expect.arrayContaining([ - ...rizinCommandLimitations({ path, command: "iI" }), - ]), - ); - } - } finally { - await rm(directory, { recursive: true, force: true }); + expect(ordinaryExit.value.limitations.join(" ")).not.toContain( + "terminated by signal", + ); + expect(ordinaryExit.value.limitations).toContainEqual( + expect.stringMatching( + /files.*network.*outside the selected artifact.*not bound to Evidence/iu, + ), + ); + expect(ordinaryExit.value.limitations).toEqual( + expect.arrayContaining([ + ...rizinCommandLimitations({ path, command: "iI" }), + ]), + ); } }); describe("Rizin command service", () => { it("defaults Rizin to plain presentation and preserves caller-selected colour on nonzero exits", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-rizin-")); + const directory = await createTestTempDirectory("rea-rizin-"); const path = join(directory, "fixture with spaces.bin"); const callerPath = relative(process.cwd(), path); await writeFile(path, "fixture"); @@ -453,63 +428,59 @@ describe("Rizin command service", () => { }, }); - try { - const command = "e scr.color=1;pd 1"; - const result = await service.executeRizinCommand({ - path: callerPath, - command, - }); - expect(result.ok).toBe(true); - if (!result.ok) return; - const lookupPath = result.value.subject?.local_path; - expect(isAbsolute(lookupPath ?? "")).toBe(true); - expect(fileIdentity(result.value.subject).sha256).toBe( - createHash("sha256").update("fixture").digest("hex"), - ); - expect(calls).toEqual([ - { - command: resolve("/tools/rizin"), - args: ["-N", "-q", "-e", "scr.color=0", "-c", command, lookupPath], - }, - ]); - expect(result.value.raw_result).toMatchObject({ - stdout: "\u001b[31mpartial data\u001b[0m\n", - stderr: "command error", - exit_code: 2, - completion_status: "complete", - }); - expect(result.value.limitations.join(" ")).not.toContain( - "terminated by signal", - ); - expect(result.value.normalized_result).toMatchObject({ - stdout: "\u001b[31mpartial data\u001b[0m\n", - }); - expect(result.value.limitations).toContainEqual( - expect.stringMatching( - /files.*network.*outside the selected artifact.*not bound to Evidence/iu, - ), - ); - expect(result.value.limitations).toEqual( - expect.arrayContaining([ - ...rizinCommandLimitations({ path: callerPath, command }), - ]), - ); - expect(result.value.parameters).toMatchObject({ - path: callerPath, - command, - provider_executable: resolve("/tools/rizin"), - }); - expect(result.value.parameters.command_arguments).toEqual(calls[0]?.args); - expect(result.value.provider.id).toBe("rizin"); - } finally { - await rm(directory, { recursive: true, force: true }); - } + const command = "e scr.color=1;pd 1"; + const result = await service.executeRizinCommand({ + path: callerPath, + command, + }); + expect(result.ok).toBe(true); + if (!result.ok) return; + const lookupPath = result.value.subject?.local_path; + expect(isAbsolute(lookupPath ?? "")).toBe(true); + expect(fileIdentity(result.value.subject).sha256).toBe( + createHash("sha256").update("fixture").digest("hex"), + ); + expect(calls).toEqual([ + { + command: resolve("/tools/rizin"), + args: ["-N", "-q", "-e", "scr.color=0", "-c", command, lookupPath], + }, + ]); + expect(result.value.raw_result).toMatchObject({ + stdout: "\u001b[31mpartial data\u001b[0m\n", + stderr: "command error", + exit_code: 2, + completion_status: "complete", + }); + expect(result.value.limitations.join(" ")).not.toContain( + "terminated by signal", + ); + expect(result.value.normalized_result).toMatchObject({ + stdout: "\u001b[31mpartial data\u001b[0m\n", + }); + expect(result.value.limitations).toContainEqual( + expect.stringMatching( + /files.*network.*outside the selected artifact.*not bound to Evidence/iu, + ), + ); + expect(result.value.limitations).toEqual( + expect.arrayContaining([ + ...rizinCommandLimitations({ path: callerPath, command }), + ]), + ); + expect(result.value.parameters).toMatchObject({ + path: callerPath, + command, + provider_executable: resolve("/tools/rizin"), + }); + expect(result.value.parameters.command_arguments).toEqual(calls[0]?.args); + expect(result.value.provider.id).toBe("rizin"); }); it.each(["iI\nq", "wx\u0000 00"])( "rejects malformed Rizin command %j before launching the provider", async (command) => { - const directory = await mkdtemp(join(tmpdir(), "rea-rizin-input-")); + const directory = await createTestTempDirectory("rea-rizin-input-"); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); let invoked = false; @@ -520,27 +491,23 @@ describe("Rizin command service", () => { }, }); - try { - const result = await service.executeRizinCommand({ - path, - command, - }); - expect(result).toMatchObject({ - ok: false, - error: { - _tag: "AnalysisInputError", - operation: "execute_rizin_command", - }, - }); - expect(invoked).toBe(false); - } finally { - await rm(directory, { recursive: true, force: true }); - } + const result = await service.executeRizinCommand({ + path, + command, + }); + expect(result).toMatchObject({ + ok: false, + error: { + _tag: "AnalysisInputError", + operation: "execute_rizin_command", + }, + }); + expect(invoked).toBe(false); }, ); it("preserves pre and post artifact digests when an unrestricted Rizin command changes the file", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-rizin-mutation-")); + const directory = await createTestTempDirectory("rea-rizin-mutation-"); const path = join(directory, "fixture.bin"); await writeFile(path, "before"); const service = new ReverseEngineeringService({ @@ -550,35 +517,29 @@ describe("Rizin command service", () => { }, }); - try { - const result = await service.executeRizinCommand({ - path, - command: "wx 00 @ 0", - }); - expect(result.ok).toBe(true); - if (!result.ok) return; - expect(fileIdentity(result.value.subject).sha256).toBe( - createHash("sha256").update("before").digest("hex"), - ); - expect(result.value.raw_result).toMatchObject({ - artifact_sha256_after: createHash("sha256") - .update("after") - .digest("hex"), - artifact_changed: true, - }); - expect(result.value.limitations).toContainEqual( - expect.stringMatching( - /may have changed the artifact.*pre-run and post-run digests/iu, - ), - ); - } finally { - await rm(directory, { recursive: true, force: true }); - } + const result = await service.executeRizinCommand({ + path, + command: "wx 00 @ 0", + }); + expect(result.ok).toBe(true); + if (!result.ok) return; + expect(fileIdentity(result.value.subject).sha256).toBe( + createHash("sha256").update("before").digest("hex"), + ); + expect(result.value.raw_result).toMatchObject({ + artifact_sha256_after: createHash("sha256").update("after").digest("hex"), + artifact_changed: true, + }); + expect(result.value.limitations).toContainEqual( + expect.stringMatching( + /may have changed the artifact.*pre-run and post-run digests/iu, + ), + ); }); }); it("passes decoder and map inputs to Rizin before the path and records them", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-rizin-load-")); + const directory = await createTestTempDirectory("rea-rizin-load-"); const path = join(directory, "flash.bin"); await writeFile(path, Buffer.from([0x70, 0x47])); const calls: (readonly string[])[] = []; @@ -598,72 +559,66 @@ it("passes decoder and map inputs to Rizin before the path and records them", as base_address: "0x08000000", }; - try { - const result = await service.executeRizinCommand(input); - if (!result.ok) throw result.error; - const argv = [ - "-N", - "-q", - "-e", - "scr.color=0", - "-a", - "arm", - "-b", - "16", - "-e", - "asm.cpu=cortexm", - "-n", - "-m", - "0x08000000", - "-c", - "pi 1", + const result = await service.executeRizinCommand(input); + if (!result.ok) throw result.error; + const argv = [ + "-N", + "-q", + "-e", + "scr.color=0", + "-a", + "arm", + "-b", + "16", + "-e", + "asm.cpu=cortexm", + "-n", + "-m", + "0x08000000", + "-c", + "pi 1", + path, + ]; + // The command is the last launch; a provider probe may precede it. + expect(calls.at(-1)).toEqual(argv); + expect(result.value.parameters).toMatchObject({ + ...input, + command_arguments: argv, + }); + expect(result.value.limitations).toEqual( + expect.arrayContaining([...rizinCommandLimitations(input)]), + ); + // Command validity stays unverified independently of decoder/map inputs. + expect(rizinCommandLimitations(input)).toHaveLength(4); + expect( + rizinCommandLimitations({ path, command: "pi 1", asm_bits: 16 }), + ).toHaveLength(3); + expect( + rizinCommandLimitations({ path, command: "pi 1", base_address: "0x0" }), + ).toHaveLength(3); + expect(rizinCommandLimitations({ path, command: "pi 1" })).toHaveLength(2); + expect( + objdumpCommandLimitations({ operation: "architectures" }), + ).toHaveLength(1); + expect(objdumpCommandLimitations({ path, operation: "symbols" })).toEqual([]); + expect( + objdumpCommandLimitations({ path, - ]; - // The command is the last launch; a provider probe may precede it. - expect(calls.at(-1)).toEqual(argv); - expect(result.value.parameters).toMatchObject({ - ...input, - command_arguments: argv, - }); - expect(result.value.limitations).toEqual( - expect.arrayContaining([...rizinCommandLimitations(input)]), - ); - // Command validity stays unverified independently of decoder/map inputs. - expect(rizinCommandLimitations(input)).toHaveLength(4); - expect( - rizinCommandLimitations({ path, command: "pi 1", asm_bits: 16 }), - ).toHaveLength(3); - expect( - rizinCommandLimitations({ path, command: "pi 1", base_address: "0x0" }), - ).toHaveLength(3); - expect(rizinCommandLimitations({ path, command: "pi 1" })).toHaveLength(2); - expect( - objdumpCommandLimitations({ operation: "architectures" }), - ).toHaveLength(1); - expect(objdumpCommandLimitations({ path, operation: "symbols" })).toEqual( - [], - ); - expect( - objdumpCommandLimitations({ - path, - operation: "disassemble", - symbol: "main", - }), - ).toHaveLength(1); - expect( - objdumpCommandLimitations({ - path, - operation: "dwarf", - follow_debug_links: true, - }), - ).toHaveLength(1); - } finally { - await rm(directory, { recursive: true, force: true }); - } + operation: "disassemble", + symbol: "main", + }), + ).toHaveLength(1); + expect( + objdumpCommandLimitations({ + path, + operation: "dwarf", + follow_debug_links: true, + }), + ).toHaveLength(1); }); it("checks only the spelling and range of decoder and map inputs", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-rizin-load-input-")); + const directory = await createTestTempDirectory("rea-rizin-load-input-"); const path = join(directory, "image.bin"); await writeFile(path, "image"); const calls: (readonly string[])[] = []; @@ -674,78 +629,74 @@ it("checks only the spelling and range of decoder and map inputs", async () => { }, }); - try { - // Rizin's own names: digits first, dotted plugins, '+', '-' and case. - for (const [selection, flags] of [ - [{ asm_arch: "6502", asm_bits: 8 }, ["-a", "6502", "-b", "8"]], - [{ asm_arch: "x86.nasm", asm_bits: 64 }, ["-a", "x86.nasm", "-b", "64"]], - [{ asm_cpu: "c55x+" }, ["-e", "asm.cpu=c55x+"]], - [{ asm_cpu: "c166-generic" }, ["-e", "asm.cpu=c166-generic"]], - [{ asm_cpu: "ATxmega128a4u" }, ["-e", "asm.cpu=ATxmega128a4u"]], - [{ asm_bits: 1 }, ["-b", "1"]], - [{ base_address: "0x0" }, ["-n", "-m", "0x0"]], - [ - { base_address: "0xFFFFFFFFFFFFFFFF" }, - ["-n", "-m", "0xFFFFFFFFFFFFFFFF"], - ], - ] as const) { - const result = await service.executeRizinCommand({ - path, - command: "e asm.arch", - ...selection, - }); - if (!result.ok) throw result.error; - expect(result.value.parameters.command_arguments).toEqual([ - "-N", - "-q", - "-e", - "scr.color=0", - ...flags, - "-c", - "e asm.arch", - path, - ]); - } - - calls.length = 0; - for (const [field, value] of [ - ["asm_arch", ""], - ["asm_arch", "arm;q"], - ["asm_arch", "arm x"], - ["asm_arch", "-q"], - ["asm_cpu", "?"], - ["asm_cpu", "cortex=m"], - ["asm_bits", 0], - ["asm_bits", 65], - ["asm_bits", 16.5], - ["asm_bits", "16"], - ["base_address", "0x"], - ["base_address", "08000000"], - ["base_address", "0X08000000"], - ["base_address", "0x10000000000000000"], - ] as const) { - const result = await service.executeRizinCommand({ - path, - command: "e asm.arch", - [field]: value, - }); - if (result.ok) throw new Error(`${field}=${String(value)} was accepted`); - expect(projectAnalysisError(result.error)).toMatchObject({ - code: "invalid_request", - details: { - operation: "execute_rizin_command", - issues: [expect.objectContaining({ path: [field] })], - }, - }); - } - expect(calls).toEqual([]); - } finally { - await rm(directory, { recursive: true, force: true }); + // Rizin's own names: digits first, dotted plugins, '+', '-' and case. + for (const [selection, flags] of [ + [{ asm_arch: "6502", asm_bits: 8 }, ["-a", "6502", "-b", "8"]], + [{ asm_arch: "x86.nasm", asm_bits: 64 }, ["-a", "x86.nasm", "-b", "64"]], + [{ asm_cpu: "c55x+" }, ["-e", "asm.cpu=c55x+"]], + [{ asm_cpu: "c166-generic" }, ["-e", "asm.cpu=c166-generic"]], + [{ asm_cpu: "ATxmega128a4u" }, ["-e", "asm.cpu=ATxmega128a4u"]], + [{ asm_bits: 1 }, ["-b", "1"]], + [{ base_address: "0x0" }, ["-n", "-m", "0x0"]], + [ + { base_address: "0xFFFFFFFFFFFFFFFF" }, + ["-n", "-m", "0xFFFFFFFFFFFFFFFF"], + ], + ] as const) { + const result = await service.executeRizinCommand({ + path, + command: "e asm.arch", + ...selection, + }); + if (!result.ok) throw result.error; + expect(result.value.parameters.command_arguments).toEqual([ + "-N", + "-q", + "-e", + "scr.color=0", + ...flags, + "-c", + "e asm.arch", + path, + ]); } + + calls.length = 0; + for (const [field, value] of [ + ["asm_arch", ""], + ["asm_arch", "arm;q"], + ["asm_arch", "arm x"], + ["asm_arch", "-q"], + ["asm_cpu", "?"], + ["asm_cpu", "cortex=m"], + ["asm_bits", 0], + ["asm_bits", 65], + ["asm_bits", 16.5], + ["asm_bits", "16"], + ["base_address", "0x"], + ["base_address", "08000000"], + ["base_address", "0X08000000"], + ["base_address", "0x10000000000000000"], + ] as const) { + const result = await service.executeRizinCommand({ + path, + command: "e asm.arch", + [field]: value, + }); + if (result.ok) throw new Error(`${field}=${String(value)} was accepted`); + expect(projectAnalysisError(result.error)).toMatchObject({ + code: "invalid_request", + details: { + operation: "execute_rizin_command", + issues: [expect.objectContaining({ path: [field] })], + }, + }); + } + expect(calls).toEqual([]); }); it("retains partial Evidence when a provider command times out", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-rizin-timeout-partial-")); + const directory = await createTestTempDirectory("rea-rizin-timeout-partial-"); const path = join(directory, "fixture.bin"); await writeFile(path, "before"); const service = new ReverseEngineeringService({ @@ -760,40 +711,36 @@ it("retains partial Evidence when a provider command times out", async () => { }, }); - try { - const result = await service.executeRizinCommand({ - path, - command: "wx 00 @ 0", + const result = await service.executeRizinCommand({ + path, + command: "wx 00 @ 0", + }); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error._tag).toBe("AnalysisTimeoutError"); + expect(JSON.stringify(result.error.partialObservation)).not.toContain( + "command_timeout_ms", + ); + expect(result.error.partialObservation).toMatchObject({ + operation: "execute_rizin_command", + raw_result: { + stdout: "captured before timeout", + stderr: "still running", + completion_status: "unknown", + signal: "SIGTERM", + artifact_sha256_after: expect.any(String), + }, }); - expect(result.ok).toBe(false); - if (!result.ok) { - expect(result.error._tag).toBe("AnalysisTimeoutError"); - expect(JSON.stringify(result.error.partialObservation)).not.toContain( - "command_timeout_ms", - ); - expect(result.error.partialObservation).toMatchObject({ - operation: "execute_rizin_command", - raw_result: { - stdout: "captured before timeout", - stderr: "still running", - completion_status: "unknown", - signal: "SIGTERM", - artifact_sha256_after: expect.any(String), - }, - }); - expect(JSON.stringify(projectAnalysisError(result.error))).toContain( - "captured before timeout", - ); - const projected = projectAnalysisError(result.error); - expect(projected.message).toContain("command_timeout_ms"); - // The deadline names the provider whose command it bounded. - expect(projected.message).toContain("rizin command"); - expect(projected.message).not.toContain("objdump"); - expect(projected.message).toContain("120000 ms"); - expect(projected.remediation.action).toContain("120000"); - } - } finally { - await rm(directory, { recursive: true, force: true }); + expect(JSON.stringify(projectAnalysisError(result.error))).toContain( + "captured before timeout", + ); + const projected = projectAnalysisError(result.error); + expect(projected.message).toContain("command_timeout_ms"); + // The deadline names the provider whose command it bounded. + expect(projected.message).toContain("rizin command"); + expect(projected.message).not.toContain("objdump"); + expect(projected.message).toContain("120000 ms"); + expect(projected.remediation.action).toContain("120000"); } }); @@ -895,7 +842,7 @@ it("retains an object reason when cancellation interrupts an active digest strea }); it("forwards selected and omitted deadlines through the production command runner", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-owned-timeout-")); + const directory = await createTestTempDirectory("rea-owned-timeout-"); const path = join(directory, "fixture.bin"); await writeFile(path, "before"); const timeouts: (number | null)[] = []; @@ -905,27 +852,23 @@ it("forwards selected and omitted deadlines through the production command runne return providerSnapshot(); }, }); - try { - const selected = await service.executeRizinCommand({ - path, - command: "i", - command_timeout_ms: 5_000, - }); - expect(selected.ok).toBe(true); - const omitted = await service.executeRizinCommand({ - path, - command: "i", - }); - expect(omitted.ok).toBe(true); - expect(timeouts).toEqual([5_000, 120_000]); - } finally { - await rm(directory, { recursive: true, force: true }); - } + const selected = await service.executeRizinCommand({ + path, + command: "i", + command_timeout_ms: 5_000, + }); + expect(selected.ok).toBe(true); + const omitted = await service.executeRizinCommand({ + path, + command: "i", + }); + expect(omitted.ok).toBe(true); + expect(timeouts).toEqual([5_000, 120_000]); }); it("applies a caller-selected one-shot deadline on the next call", async () => { - const directory = await mkdtemp( - join(tmpdir(), "rea-rizin-timeout-selected-"), + const directory = await createTestTempDirectory( + "rea-rizin-timeout-selected-", ); const path = join(directory, "fixture.bin"); await writeFile(path, "before"); @@ -940,38 +883,34 @@ it("applies a caller-selected one-shot deadline on the next call", async () => { }, }); - try { - const selected = await service.executeRizinCommand({ + const selected = await service.executeRizinCommand({ + path, + command: "i", + command_timeout_ms: 5_000, + }); + expect(selected.ok).toBe(false); + if (!selected.ok) { + expect(selected.error).toMatchObject({ timeoutMs: 5_000 }); + expect(projectAnalysisError(selected.error).message).toContain("5000 ms"); + expect(selected.error.partialObservation).toMatchObject({ + parameters: { command_timeout_ms: 5_000 }, + }); + } + expect(timeouts).toEqual([5_000]); + for (const command_timeout_ms of [0, 2_147_483_648]) { + const rejected = await service.executeRizinCommand({ path, command: "i", - command_timeout_ms: 5_000, + command_timeout_ms, }); - expect(selected.ok).toBe(false); - if (!selected.ok) { - expect(selected.error).toMatchObject({ timeoutMs: 5_000 }); - expect(projectAnalysisError(selected.error).message).toContain("5000 ms"); - expect(selected.error.partialObservation).toMatchObject({ - parameters: { command_timeout_ms: 5_000 }, - }); - } - expect(timeouts).toEqual([5_000]); - for (const command_timeout_ms of [0, 2_147_483_648]) { - const rejected = await service.executeRizinCommand({ - path, - command: "i", - command_timeout_ms, - }); - expect(rejected.ok).toBe(false); - if (!rejected.ok) expect(rejected.error._tag).toBe("AnalysisInputError"); - } - } finally { - await rm(directory, { recursive: true, force: true }); + expect(rejected.ok).toBe(false); + if (!rejected.ok) expect(rejected.error._tag).toBe("AnalysisInputError"); } }); it("preserves raw byte counts when cancellation rejects invalid UTF-8 output", async () => { - const directory = await mkdtemp( - join(tmpdir(), "rea-rizin-abort-invalid-utf8-"), + const directory = await createTestTempDirectory( + "rea-rizin-abort-invalid-utf8-", ); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); @@ -990,28 +929,24 @@ it("preserves raw byte counts when cancellation rejects invalid UTF-8 output", a }, }); - try { - const result = await service.executeRizinCommand( - { path, command: "iI" }, - { signal: controller.signal }, - ); - expect(result.ok).toBe(false); - if (!result.ok) { - expect(result.error._tag).toBe("AnalysisCancelledError"); - expect(result.error.capturedOutput).toMatchObject({ - stdout: "�", - stderr: "€", - stdout_bytes: 1, - stderr_bytes: 3, - }); - } - } finally { - await rm(directory, { recursive: true, force: true }); + const result = await service.executeRizinCommand( + { path, command: "iI" }, + { signal: controller.signal }, + ); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error._tag).toBe("AnalysisCancelledError"); + expect(result.error.capturedOutput).toMatchObject({ + stdout: "�", + stderr: "€", + stdout_bytes: 1, + stderr_bytes: 3, + }); } }); it("preserves provider facts when cancellation interrupts the post-run digest", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-rizin-digest-cancel-")); + const directory = await createTestTempDirectory("rea-rizin-digest-cancel-"); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); const controller = new AbortController(); @@ -1035,41 +970,37 @@ it("preserves provider facts when cancellation interrupts the post-run digest", }, }); - try { - const result = await service.executeRizinCommand( - { path, command: "iI" }, - { signal: controller.signal }, - ); - expect(result.ok).toBe(false); - if (!result.ok) { - expect(result.error._tag).toBe("AnalysisCancelledError"); - expect(result.error.capturedOutput).toMatchObject({ + const result = await service.executeRizinCommand( + { path, command: "iI" }, + { signal: controller.signal }, + ); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error._tag).toBe("AnalysisCancelledError"); + expect(result.error.capturedOutput).toMatchObject({ + stdout: "�", + stderr: "�", + stdout_bytes: 1, + stderr_bytes: 1, + exit_code: 0, + signal: null, + }); + expect(result.error.partialObservation).toMatchObject({ + raw_result: { stdout: "�", - stderr: "�", - stdout_bytes: 1, - stderr_bytes: 1, - exit_code: 0, - signal: null, - }); - expect(result.error.partialObservation).toMatchObject({ - raw_result: { - stdout: "�", - exit_code: null, - completion_status: "unknown", - artifact_sha256_after: null, - }, - limitations: expect.arrayContaining([ - expect.stringContaining("cancellation interrupted the digest"), - ]), - }); - } - } finally { - await rm(directory, { recursive: true, force: true }); + exit_code: null, + completion_status: "unknown", + artifact_sha256_after: null, + }, + limitations: expect.arrayContaining([ + expect.stringContaining("cancellation interrupted the digest"), + ]), + }); } }); it("marks signal-terminated provider output incomplete", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-rizin-signal-")); + const directory = await createTestTempDirectory("rea-rizin-signal-"); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); const service = new ReverseEngineeringService({ @@ -1081,16 +1012,12 @@ it("marks signal-terminated provider output incomplete", async () => { }, }); - try { - const result = await service.executeRizinCommand({ path, command: "iI" }); - expect(result.ok).toBe(true); - if (result.ok) - expect(result.value.raw_result).toMatchObject({ - signal: "SIGSEGV", - exit_code: null, - completion_status: "unknown", - }); - } finally { - await rm(directory, { recursive: true, force: true }); - } + const result = await service.executeRizinCommand({ path, command: "iI" }); + expect(result.ok).toBe(true); + if (result.ok) + expect(result.value.raw_result).toMatchObject({ + signal: "SIGSEGV", + exit_code: null, + completion_status: "unknown", + }); }); diff --git a/tests/composition/reverseEngineeringAdmission.test.ts b/tests/composition/reverseEngineeringAdmission.test.ts index 117794617c..27ce63dce4 100644 --- a/tests/composition/reverseEngineeringAdmission.test.ts +++ b/tests/composition/reverseEngineeringAdmission.test.ts @@ -10,6 +10,7 @@ import { ownedFailure, ownedOutput, } from "../support/reverseEngineeringCompositionFixtures.js"; +import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; it.each([ { budget: undefined, capacity: 8 }, @@ -17,7 +18,7 @@ it.each([ ])( "bounds provider concurrency and aggregate capture admission at budget $budget", async ({ budget, capacity }) => { - const directory = await mkdtemp(join(tmpdir(), "rea-reverse-capacity-")); + const directory = await createTestTempDirectory("rea-reverse-capacity-"); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); const input = { @@ -102,13 +103,12 @@ it.each([ } finally { release(); await service.close(); - await rm(directory, { recursive: true, force: true }); } }, ); it("preserves the exact caller reason when objdump admission is already aborted", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-reverse-preabort-")); + const directory = await createTestTempDirectory("rea-reverse-preabort-"); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); const reason = Object.freeze({ requested: "stop" }); @@ -133,12 +133,11 @@ it("preserves the exact caller reason when objdump admission is already aborted" expect(runs).toBe(0); } finally { await service.close(); - await rm(directory, { recursive: true, force: true }); } }); it("preserves the caller reason when cancellation arrives after provider output", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-reverse-postabort-")); + const directory = await createTestTempDirectory("rea-reverse-postabort-"); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); const reason = Object.freeze({ requested: "stop-after-output" }); @@ -163,12 +162,11 @@ it("preserves the caller reason when cancellation arrives after provider output" }); } finally { await service.close(); - await rm(directory, { recursive: true, force: true }); } }); it("keeps a physical digest denial primary when the caller aborts during that failure", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-reverse-digest-deny-")); + const directory = await createTestTempDirectory("rea-reverse-digest-deny-"); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); const reason = Object.freeze({ requested: "stop-during-denial" }); @@ -201,12 +199,11 @@ it("keeps a physical digest denial primary when the caller aborts during that fa expect(runs).toBe(0); } finally { await service.close(); - await rm(directory, { recursive: true, force: true }); } }); it("keeps an unrelated digest ABORT_ERR as an adapter failure", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-reverse-digest-abort-")); + const directory = await createTestTempDirectory("rea-reverse-digest-abort-"); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); const denied = Object.assign(new Error("digest aborted elsewhere"), { @@ -230,12 +227,11 @@ it("keeps an unrelated digest ABORT_ERR as an adapter failure", async () => { expect(result.error.cause).toBe(denied); } finally { await service.close(); - await rm(directory, { recursive: true, force: true }); } }); it("does not report a command ABORT_ERR without the caller's abort as cancellation", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-reverse-command-abort-")); + const directory = await createTestTempDirectory("rea-reverse-command-abort-"); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); const failure = Object.assign(new Error("aborted by another signal"), { @@ -256,7 +252,6 @@ it("does not report a command ABORT_ERR without the caller's abort as cancellati expect(result.error._tag).not.toBe("AnalysisCancelledError"); } finally { await service.close(); - await rm(directory, { recursive: true, force: true }); } }); @@ -289,8 +284,8 @@ it.each([ ] as const)( "classifies a $code command failure with captured output $captured while the caller is aborted", async ({ captured, code, tag }) => { - const directory = await mkdtemp( - join(tmpdir(), "rea-reverse-command-abort-"), + const directory = await createTestTempDirectory( + "rea-reverse-command-abort-", ); const path = join(directory, "fixture.bin"); await writeFile(path, "fixture"); @@ -345,7 +340,6 @@ it.each([ } } finally { await service.close(); - await rm(directory, { recursive: true, force: true }); } }, ); diff --git a/tests/composition/reverseEngineeringCancellation.test.ts b/tests/composition/reverseEngineeringCancellation.test.ts index 14116315b4..a691b04b29 100644 --- a/tests/composition/reverseEngineeringCancellation.test.ts +++ b/tests/composition/reverseEngineeringCancellation.test.ts @@ -5,6 +5,7 @@ import { join } from "node:path"; import { expect, it } from "vitest"; import { TestReverseEngineeringService as ReverseEngineeringService } from "../support/reverseEngineeringService.js"; +import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; type PromiseOutcome = | { readonly status: "fulfilled"; readonly value: Value } @@ -159,44 +160,39 @@ const cleanupCancellationScenario = async (options: { readonly pending: Promise | undefined; readonly service: ReverseEngineeringService; readonly processIds: CancellationProcessIds | undefined; - readonly directory: string; }): Promise => { options.controller.abort(); - try { - if (options.pending !== undefined) { - try { - if (options.processIds !== undefined) - await settleCancelledProvider(options.pending); - else await settleWithin(options.pending, 10_000); - } catch { - // Main assertions report operation failures; continue cleanup. - } + if (options.pending !== undefined) { + try { + if (options.processIds !== undefined) + await settleCancelledProvider(options.pending); + else await settleWithin(options.pending, 10_000); + } catch { + // Main assertions report operation failures; continue cleanup. } - let cleanup = await settleWithin(options.service.close(), 7_000); - if (cleanup.status === "timeout" && options.processIds !== undefined) { - await killCancellationProcesses(options.processIds); - cleanup = await settleWithin(options.service.close(), 3_000); + } + let cleanup = await settleWithin(options.service.close(), 7_000); + if (cleanup.status === "timeout" && options.processIds !== undefined) { + await killCancellationProcesses(options.processIds); + cleanup = await settleWithin(options.service.close(), 3_000); + } + if (cleanup.status === "timeout") + throw new Error("Service cleanup did not settle within its deadline."); + if (cleanup.status === "rejected") throw cleanup.reason; + if (options.processIds !== undefined) { + for (const pid of [ + options.processIds.providerPid, + options.processIds.descendantPid, + ]) { + if (processIsRunning(pid)) await killProcessAndWait(pid, 2_000); } - if (cleanup.status === "timeout") - throw new Error("Service cleanup did not settle within its deadline."); - if (cleanup.status === "rejected") throw cleanup.reason; - if (options.processIds !== undefined) { - for (const pid of [ - options.processIds.providerPid, - options.processIds.descendantPid, - ]) { - if (processIsRunning(pid)) await killProcessAndWait(pid, 2_000); - } - } - } finally { - await rm(options.directory, { recursive: true, force: true }); } }; it.skipIf(process.platform === "win32")( "waits for a cancelled Rizin process to exit before returning", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-rizin-cancel-settle-")); + const directory = await createTestTempDirectory("rea-rizin-cancel-settle-"); const path = join(directory, "fixture.bin"); const providerPidMarker = join(directory, "provider-pid"); const descendantStartedMarker = join(directory, "descendant-started"); @@ -261,7 +257,6 @@ setInterval(() => {}, 1000); pending, service, processIds, - directory, }); } }, diff --git a/tests/conformance/package/conformanceFixtureVerifier.test.ts b/tests/conformance/package/conformanceFixtureVerifier.test.ts index 3c23580196..65a6c5cb59 100644 --- a/tests/conformance/package/conformanceFixtureVerifier.test.ts +++ b/tests/conformance/package/conformanceFixtureVerifier.test.ts @@ -19,6 +19,7 @@ import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js import { runningTestDeadline } from "../../support/runningTestDeadline.js"; import { waitFor } from "../../support/testWait.js"; import { removeTestWorkspace } from "../../support/workspace/workspaceFixture.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const digest = (bytes: string | Buffer) => createHash("sha256").update(bytes).digest("hex"); @@ -27,13 +28,10 @@ const verifier = fileURLToPath( ); const ownedFixture = async (prefix: string) => { + const root = await createTestTempDirectory(prefix); const commands = await createVerifierCommands(); - let root: string | undefined; - onTestFinished(async () => { - await commands.close(); - if (root !== undefined) await removeTestWorkspace(root); - }); - root = await realpath(await mkdtemp(join(await realpath(tmpdir()), prefix))); + // Registered after the directory helper so stack order closes commands first. + onTestFinished(() => commands.close()); return { commands, root }; }; diff --git a/tests/conformance/providers/ghidra/checkpointRefusal.test.ts b/tests/conformance/providers/ghidra/checkpointRefusal.test.ts index 05efc6abf2..7c0792aed1 100644 --- a/tests/conformance/providers/ghidra/checkpointRefusal.test.ts +++ b/tests/conformance/providers/ghidra/checkpointRefusal.test.ts @@ -3,10 +3,10 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { expect, it, onTestFinished } from "vitest"; import { assertCheckpointRefusalPreservesManifest } from "../../../../scripts/lib/ghidra-checkpoint-refusal.mjs"; +import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js"; const checkpointFile = async (bytes: Uint8Array) => { - const root = await mkdtemp(join(tmpdir(), "rea-checkpoint-refusal-")); - onTestFinished(() => rm(root, { recursive: true, force: true })); + const root = await createTestTempDirectory("rea-checkpoint-refusal-"); const path = join(root, "current.json"); await writeFile(path, bytes); return path; diff --git a/tests/evaluation/analysisEfficiencyComparison.test.ts b/tests/evaluation/analysisEfficiencyComparison.test.ts index 75e00e61b8..665e5b1368 100644 --- a/tests/evaluation/analysisEfficiencyComparison.test.ts +++ b/tests/evaluation/analysisEfficiencyComparison.test.ts @@ -6,6 +6,7 @@ import { join } from "node:path"; import { test } from "vitest"; import { z } from "zod"; import { fileURLToPath } from "node:url"; +import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; type Delivery = "complete" | "summary"; type Observation = { @@ -90,45 +91,41 @@ const fixture = (delivery: Delivery, artifact: string): Fixture => ({ ], }, }); -function compare(left: Fixture, right: Fixture) { - const root = mkdtempSync(join(tmpdir(), "rea-compare-test-")); - try { - const directories = [join(root, "left"), join(root, "right")] as const; - for (const [index, value] of [left, right].entries()) { - const directory = directories[index]; - assert.ok(directory); - mkdirSync(directory); - for (const name of ["summary", "answer"] as const) - writeFileSync( - join(directory, `${name}.json`), - JSON.stringify(value[name]), - ); - } - const result = spawnSync( - process.execPath, - [ - fileURLToPath( - new URL( - "../../tests/support/analysis-efficiency/compare.mjs", - import.meta.url, - ), - ), - "--left", - directories[0], - "--right", - directories[1], - ], - { encoding: "utf8" }, - ); - assert.ifError(result.error); - assert.equal(result.stderr, ""); - return { - status: result.status, - report: reportSchema.parse(JSON.parse(result.stdout)), - }; - } finally { - rmSync(root, { recursive: true, force: true }); +async function compare(left: Fixture, right: Fixture) { + const root = await createTestTempDirectory("rea-compare-test-"); + const directories = [join(root, "left"), join(root, "right")] as const; + for (const [index, value] of [left, right].entries()) { + const directory = directories[index]; + assert.ok(directory); + mkdirSync(directory); + for (const name of ["summary", "answer"] as const) + writeFileSync( + join(directory, `${name}.json`), + JSON.stringify(value[name]), + ); } + const result = spawnSync( + process.execPath, + [ + fileURLToPath( + new URL( + "../../tests/support/analysis-efficiency/compare.mjs", + import.meta.url, + ), + ), + "--left", + directories[0], + "--right", + directories[1], + ], + { encoding: "utf8" }, + ); + assert.ifError(result.error); + assert.equal(result.stderr, ""); + return { + status: result.status, + report: reportSchema.parse(JSON.parse(result.stdout)), + }; } for (const artifact of [ @@ -136,7 +133,7 @@ for (const artifact of [ "wireshark-dns.cap", "wireshark-dhcp.pcap", ]) { - test(`${artifact}: compare requested facts across packet plans and preserve observations`, () => { + test(`${artifact}: compare requested facts across packet plans and preserve observations`, async () => { const left = fixture("complete", artifact); left.summary.packet_selection = "baseline"; left.answer.facts = { @@ -149,7 +146,7 @@ for (const artifact of [ right.answer.delivery_observations[0]!.evidence_id = "ev_focused"; right.answer.delivery_observations[0]!.coverage = "up-to-input-packet-limit"; - const equivalent = compare(left, right); + const equivalent = await compare(left, right); assert.equal(equivalent.status, 0); assert.equal(equivalent.report.facts_match, true); assert.deepEqual( @@ -157,15 +154,15 @@ for (const artifact of [ [left.answer.delivery_observations, right.answer.delivery_observations], ); right.answer.facts!.matching_packets = 1338; - assert.equal(compare(left, right).report.facts_match, false); + assert.equal((await compare(left, right)).report.facts_match, false); right.answer.facts!.matching_packets = 1337; right.summary.packet_selection = "baseline"; - assert.equal(compare(left, right).report.facts_match, false); + assert.equal((await compare(left, right)).report.facts_match, false); }); } for (const artifact of ["sigrok-uart-max3232e.sr", "sigrok-i2c-nunchuk.sr"]) { - test(`${artifact}: compare decoded facts across signal plans and preserve display scope`, () => { + test(`${artifact}: compare decoded facts across signal plans and preserve display scope`, async () => { const left = fixture("complete", artifact); left.summary.signal_selection = "baseline"; left.answer.facts = { kind: "signal", bytes: [72, 101] }; @@ -173,7 +170,7 @@ for (const artifact of ["sigrok-uart-max3232e.sr", "sigrok-i2c-nunchuk.sr"]) { right.summary.signal_selection = "focused"; right.answer.delivery_observations[0]!.coverage = "selected annotation lines"; - const equivalent = compare(left, right); + const equivalent = await compare(left, right); assert.equal(equivalent.status, 0); assert.equal(equivalent.report.facts_match, true); assert.deepEqual( @@ -181,10 +178,10 @@ for (const artifact of ["sigrok-uart-max3232e.sr", "sigrok-i2c-nunchuk.sr"]) { [left.answer.delivery_observations, right.answer.delivery_observations], ); right.answer.facts!.bytes![0] = 73; - assert.equal(compare(left, right).report.facts_match, false); + assert.equal((await compare(left, right)).report.facts_match, false); right.answer.facts!.bytes![0] = 72; right.summary.signal_selection = "baseline"; - assert.equal(compare(left, right).report.facts_match, false); + assert.equal((await compare(left, right)).report.facts_match, false); }); } @@ -192,7 +189,7 @@ for (const artifact of [ "wireshark-http.cap", "wireshark-http_redirects.pcapng", ]) { - test(`${artifact}: repeated modes ignore only receipt IDs`, () => { + test(`${artifact}: repeated modes ignore only receipt IDs`, async () => { for (const delivery of ["complete", "summary"] as const) { const left = fixture(delivery, artifact); const right = structuredClone(left); @@ -201,7 +198,7 @@ for (const artifact of [ if (typeof value.parent_evidence_id === "string") value.parent_evidence_id += "_second"; } - const { status, report } = compare(left, right); + const { status, report } = await compare(left, right); assert.equal(status, 0); assert.equal(report.facts_match, true); assert.deepEqual( @@ -211,7 +208,7 @@ for (const artifact of [ } }); - test(`${artifact}: repeated modes reject stable observation changes`, () => { + test(`${artifact}: repeated modes reject stable observation changes`, async () => { for (const delivery of ["complete", "summary"] as const) { const left = fixture(delivery, artifact); const mutations: ((values: Observation[]) => void)[] = [ @@ -239,7 +236,7 @@ for (const artifact of [ for (const mutate of mutations) { const right = structuredClone(left); mutate(right.answer.delivery_observations); - const { status, report } = compare(left, right); + const { status, report } = await compare(left, right); assert.equal(status, 1); assert.equal(report.settings_match, true); assert.equal(report.facts_match, false); @@ -248,10 +245,10 @@ for (const artifact of [ } }); - test(`${artifact}: different modes preserve their different receipt scopes`, () => { + test(`${artifact}: different modes preserve their different receipt scopes`, async () => { const left = fixture("complete", artifact); const right = fixture("summary", artifact); - const { status, report } = compare(left, right); + const { status, report } = await compare(left, right); assert.equal(status, 0); assert.equal(report.facts_match, true); assert.deepEqual( @@ -259,6 +256,6 @@ for (const artifact of [ [left.answer.delivery_observations, right.answer.delivery_observations], ); right.answer.capture.coverage = "partial"; - assert.equal(compare(left, right).report.facts_match, false); + assert.equal((await compare(left, right)).report.facts_match, false); }); } diff --git a/tests/process-global/processObservationCommandFinalization.test.ts b/tests/process-global/processObservationCommandFinalization.test.ts index 3b2def8cbe..21fa95348a 100644 --- a/tests/process-global/processObservationCommandFinalization.test.ts +++ b/tests/process-global/processObservationCommandFinalization.test.ts @@ -9,6 +9,7 @@ import { createDarwinProcessRunTokenReader } from "../../src/process/DarwinProce import { createSystemProcessOwnershipHost } from "../../src/process/ProcessOwnershipObservation.js"; import { startProcessSampler } from "../../src/process/capture/ProcessSampling.js"; import { ProcessOwnershipInspectionCancelled } from "../../src/process/ProcessInspectionCancellation.js"; +import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; const waitForFile = async (path: string, timeoutMs = 4_000): Promise => { const deadline = Date.now() + timeoutMs; @@ -81,25 +82,21 @@ const createDelayedCommand = async (directory: string) => { const expectOwnedCommandToFinishOnAbort = async ( run: (signal: AbortSignal, directory: string) => Promise, ): Promise => { - const directory = await mkdtemp(join(tmpdir(), "rea-process-command-")); + const directory = await createTestTempDirectory("rea-process-command-"); const command = await createDelayedCommand(directory); const controller = new AbortController(); const reason = new Error("cancel owned process command"); let pending: Promise | undefined; onTestFinished(async () => { - try { - controller.abort(reason); - await pending?.catch(() => {}); - if ( - await readFile(command.readyPath).then( - () => true, - () => false, - ) + controller.abort(reason); + await pending?.catch(() => {}); + if ( + await readFile(command.readyPath).then( + () => true, + () => false, ) - await waitForFile(command.donePath); - } finally { - await rm(directory, { recursive: true, force: true }); - } + ) + await waitForFile(command.donePath); }); pending = run(controller.signal, directory); @@ -155,7 +152,7 @@ const processEntry = { it.skipIf(process.platform === "win32")( "waits for the owned ps command to exit when sampling stops", async () => { - const directory = await mkdtemp(join(tmpdir(), "rea-process-sampler-")); + const directory = await createTestTempDirectory("rea-process-sampler-"); const readyPath = join(directory, "ready"); const donePath = join(directory, "done"); const psPath = join(directory, "ps"); @@ -175,7 +172,6 @@ it.skipIf(process.platform === "win32")( await waitForFile(donePath); } finally { restoreProcessGlobals(); - await rm(directory, { recursive: true, force: true }); } }); @@ -229,27 +225,23 @@ it const expectDarwinReaderCommandToFinishOnAbort = async ( operation: "read" | "identities", ): Promise => { - const directory = await mkdtemp(join(tmpdir(), `rea-darwin-${operation}-`)); + const directory = await createTestTempDirectory(`rea-darwin-${operation}-`); const command = await createDelayedCommand(directory); const reader = await createFakeDarwinReader(directory, command); const controller = new AbortController(); const reason = new Error(`cancel ${operation}`); let pending: Promise | undefined; onTestFinished(async () => { - try { - controller.abort(reason); - await pending?.catch(() => {}); - if ( - await readFile(command.readyPath).then( - () => true, - () => false, - ) + controller.abort(reason); + await pending?.catch(() => {}); + if ( + await readFile(command.readyPath).then( + () => true, + () => false, ) - await waitForFile(command.donePath); - await reader.close(); - } finally { - await rm(directory, { recursive: true, force: true }); - } + ) + await waitForFile(command.donePath); + await reader.close(); }); pending =