Files
reverse-engineering-anything/tests/boundary/process/verifierArtifactScope.test.ts
T
morluto b4a2a79c55 test(lifecycle): move local gate copies, tests/process-global and tests/boundary phase gates onto the shared harness (#11412)
* test(lifecycle): replace eight local gate helper copies with the shared harness

* test(lifecycle): count an arrival announced in the turn its operation settles

* test(lifecycle): move tests/process-global phase gates onto the shared harness (#11414)

* test(lifecycle): move tests/process-global phase gates onto the shared harness

* test(lifecycle): move tests/boundary filesystem and process phase gates onto the shared harness (#11418)

* test(lifecycle): move tests/boundary provider phase gates onto the shared harness

* test(lifecycle): move tests/boundary/mcp phase gates onto the shared harness (#11433)

* test(lifecycle): give each phaseGate hold its own promise so unhandled held rejections stay visible

* test(lifecycle): move tests/boundary browser, pcap and CLI worker phase gates onto the shared harness (#11437)

* test(lifecycle): move tests/boundary browser, pcap and CLI worker phase gates onto the shared harness

* test(lifecycle): move fixture-owned and arrival-only provider gates onto the shared harness (#11441)

* test(lifecycle): move src/server and provider phase gates onto the shared harness (#11447)

* test(lifecycle): move the remaining src server, Kuna copy, Frida and Ghidra phase gates onto the shared harness (#11471)
2026-11-16 04:49:19 +08:00

99 lines
3.2 KiB
TypeScript

import { expect, it } from "vitest";
import {
VerifierArtifactCleanupFailure,
withVerifierArtifactScope,
} from "../../../scripts/lib/verifier-artifact-scope.mjs";
import type { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceScope.js";
import { phaseGate } from "../../support/lifecycleHarness.js";
it("retains the exact scope for retry and preserves the operation failure", async () => {
const primary = new Error("analysis failed");
const cleanup = new Error("worker close failed");
let attempts = 0;
let scope: ArtifactResourceScope | undefined;
let failure: unknown;
try {
await withVerifierArtifactScope(async (owner) => {
scope = owner;
owner.addCloseHook(async () => {
if (++attempts === 1) throw cleanup;
});
throw primary;
});
} catch (cause) {
failure = cause;
}
expect(failure).toBeInstanceOf(VerifierArtifactCleanupFailure);
const aggregate = failure as VerifierArtifactCleanupFailure;
expect(aggregate.errors).toEqual([primary, cleanup]);
expect(aggregate.cause).toBe(primary);
expect(aggregate.cleanupOwner).toBe(scope);
await aggregate.cleanupOwner.close();
expect(attempts).toBe(2);
});
it("retains cleanup authority when the operation succeeds", async () => {
let attempts = 0;
let failure: unknown;
try {
await withVerifierArtifactScope(async (scope) => {
scope.addCloseHook(async () => {
if (++attempts === 1) throw new Error("close failed");
});
return "result";
});
} catch (cause) {
failure = cause;
}
expect(failure).toBeInstanceOf(VerifierArtifactCleanupFailure);
const aggregate = failure as VerifierArtifactCleanupFailure;
expect(aggregate.errors).toHaveLength(1);
await aggregate.cleanupOwner.close();
expect(attempts).toBe(2);
});
it("finishes every close hook when cancellation arrives during final cleanup", async () => {
const controller = new AbortController();
const cleanup = new Error("first close failed");
const verifierArtifactCloseHook = phaseGate();
const calls: string[] = [];
let attempts = 0;
const operation = withVerifierArtifactScope(async (scope) => {
scope.addCloseHook(async () => {
calls.push("first");
if (++attempts === 1) {
await verifierArtifactCloseHook.hold();
if (controller.signal.aborted) throw cleanup;
}
});
scope.addCloseHook(async () => {
calls.push("second");
});
return "finished";
});
try {
await verifierArtifactCloseHook.reached(
operation,
"verifier artifact close hook",
);
controller.abort();
} finally {
// Release and join the parked cleanup even when the boundary is missed.
verifierArtifactCloseHook.release();
await operation.catch(() => undefined);
}
let failure: unknown;
try {
await operation;
} catch (cause) {
failure = cause;
}
expect(controller.signal.aborted).toBe(true);
expect(failure).toBeInstanceOf(VerifierArtifactCleanupFailure);
expect(calls).toEqual(["first", "second"]);
const aggregate = failure as VerifierArtifactCleanupFailure;
expect(aggregate.errors).toEqual([cleanup]);
await aggregate.cleanupOwner.close();
expect(calls).toEqual(["first", "second", "first", "second"]);
});