Files
reverse-engineering-anything/src/application/binary/LazyAnalysisProvider.test.ts
T
morluto 15dc40e2bf test(lifecycle): let phase gates release with a value or a rejection (#11367)
* test(lifecycle): let phase gates release with a value or a rejection

phaseGate<T, R> now hands R back from hold, and reject(error) makes hold
throw, so gates around a late module load or a descriptor read that
completes or fails after cancellation fit the shared harness. Migrates the
src/application gates (AnalysisProviderRegistry x5, LazyAnalysisProvider,
BinarySessionLiveness, CapabilityScope, McpClientRegistration) and
fileHandleChunks, which exercises reject.

* test(lifecycle): cover an early phase-gate rejection and first-decision release

* test(lifecycle): move src/artifacts and src/process phase gates onto the harness (#11371)

Bun, Deno, FIT, NTFS and Mach-O slice reader gates, and the artifact
resources, owned worker, owned lease and launcher grace gates now use
phaseGate. The lipo capture and launcher grace gates release with their
command result and exit verdict. Arrival-only acknowledgements (ESP and HEX
decoder writes, process cleanup start) and the abort-aware process listing
fake stay as they are.
2026-11-16 03:05:55 +08:00

131 lines
3.6 KiB
TypeScript

import { expect, it } from "vitest";
import { withVerifierCleanup } from "../../../scripts/lib/verifier-cleanup.mjs";
import { phaseGate } from "../../../tests/support/lifecycleHarness.js";
import { parseBinaryTarget } from "../BinaryTargetResolver.js";
import type {
AnalysisProvider,
ProviderIdentity,
} from "../AnalysisProvider.js";
import { createAnalysisExecution } from "../AnalysisProvider.js";
import { LazyAnalysisProvider } from "./LazyAnalysisProvider.js";
import { ok as resultOk } from "../../domain/result.js";
const identity: ProviderIdentity = {
id: "test-provider",
name: "Test provider",
version: "1",
};
const providerWithClient = (
onCreate: () => void = () => undefined,
): AnalysisProvider => ({
identity: () => identity,
capabilities: () => [],
createClient: () => {
onCreate();
return {
execute: async () => resultOk(createAnalysisExecution(null, identity)),
close: async () => resultOk(null),
};
},
});
const target = async () => {
const parsed = await parseBinaryTarget(process.execPath);
if (!parsed.ok) throw parsed.error;
return parsed.value;
};
it("returns a tagged provider error when lazy loading fails", async () => {
const provider = new LazyAnalysisProvider({
identity,
capabilities: [],
load: async () => {
throw new Error("provider module unavailable");
},
});
const result = await provider
.createClient(await target())
.execute("health", {});
expect(result).toMatchObject({
ok: false,
error: {
_tag: "ProviderAdapterError",
providerId: "test-provider",
operation: "health",
},
});
});
it("joins a cancelled unabortable module load and refuses late client acquisition", async () => {
const loading = phaseGate<void, AnalysisProvider>();
let creates = 0;
const provider = new LazyAnalysisProvider({
identity,
capabilities: [],
load: () => loading.hold(),
});
const controller = new AbortController();
const reason = { request: "cancel during import" };
const client = provider.createClient(await target());
let settled = false;
const pending = client
.execute("health", {}, { signal: controller.signal })
.then((value) => {
settled = true;
return value;
});
await withVerifierCleanup(async () => {
await loading.reached(pending, "provider module load");
controller.abort(reason);
await new Promise<void>((resolve) => setImmediate(resolve));
expect(settled).toBe(false);
loading.release(
providerWithClient(() => {
creates++;
}),
);
const result = await pending;
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisCancelledError", operation: "health" },
});
if (result.ok) throw new Error("Cancellation unexpectedly succeeded");
expect(result.error.cause).toBe(reason);
expect(creates).toBe(0);
expect(await client.close()).toEqual(resultOk(null));
}, [
() => controller.abort(),
() => loading.release(providerWithClient()),
async () => {
await pending;
},
async () => {
const closed = await client.close();
if (!closed.ok) throw closed.error;
},
]);
});
it("reports that a lazy client was closed before its first operation", async () => {
const provider = new LazyAnalysisProvider({
identity,
capabilities: [],
load: async () => providerWithClient(),
});
const client = provider.createClient(await target());
await client.close();
expect(await client.execute("health", {})).toMatchObject({
ok: false,
error: {
_tag: "ProviderAdapterError",
operation: "health",
diagnostics: { reason: "client_closed" },
},
});
});