Files
reverse-engineering-anything/tests/boundary/cli/cliOwnedResources.test.ts
SandivandVincent Koc f5055cd2ed fix(process): report denied inspection as typed cleanup failures (#9261)
* fix(process): report denied inspection as typed cleanup failures

Preserve the native ps diagnostic through owned-worker cleanup and finalize one-shot JavaScript analysis before Evidence delivery. Retain mandatory inspection and existing cleanup authority.

Preserve the inspector diagnostic when the initial process-table read succeeds but pre-signal revalidation fails, and remove the unreachable typed-error branch so retained artifact retry failures project through the existing cleanup contract.

* fix(resources): preserve independent close failure ownership

* fix(cli): retain cleanup recovery and nested close evidence

* refactor(providers): classify rejected closes at the shared boundary

* test(cli): preserve bundle metadata and physical inspection failures

* fix(cli): preserve the JavaScript failure envelope after finalization

* test(cli): validate the JavaScript error envelope at its boundary

* fix(providers): preserve unnamed close failure resources

* fix(cli): retain invocation owners through outer cleanup

* test(cli): distinguish reported cleanup failures from uncaught errors

* fix(cli): compose each runtime’s declared cleanup owners

* test(cli): preserve the original cleanup rejection

* test(cli): allow cleanup retry recovery after a reported failure

* fix(cli): report final cleanup after returned results

* fix(cli): preserve launcher executable mode

* test(cli): retain invocation cleanup in file sessions

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-11-30 19:38:45 +08:00

107 lines
3.5 KiB
TypeScript

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