Files
reverse-engineering-anything/tests/acceptance/elfNoteCoreProfile.test.ts
T
morluto b3657c557f perf(tests): run plain CLI commands on the file's shared process (#10412)
* refactor(cli): run one-shot commands through one shared owner

scripts/rea.mjs and the test CLI session worker each implemented the
one-shot command sequence: argument normalization, output admission,
configuration errors, version documents, the Amp scope refusal, and
result delivery. The worker copy had drifted: it never rendered
formatted version reports or the Amp refusal.

Move the sequence into src/cliOneShot.ts. Both callers supply a host
for their streams, exit and resource ownership. The launcher keeps
closed-pipe status, worker shutdown and process exit; the session
worker captures output per command.

The cliOutput bundle entry is superseded and removed. The dispatcher
test now replaces only the command runtime, so output admission runs
production code. A new parity test compares the file session with a
fresh process.

* test(cli): bundle the one-shot entry in the runtime bundle fixture

* perf(tests): run plain CLI commands on the file's shared process

`cli.run` in `cliTest` files started a fresh compiled CLI for every
command, about 0.45s and 260 MB each before any work. The file-scoped
session already existed, but callers had to opt in through
`runFileCli` or `sharedCliRun`, so 1,300 call sites still spawned.

Make the fixture choose: `cli.run` uses the shared process when it
reports the command exactly as a fresh process would, and starts one
otherwise (stdin, explicit deadlines, MCP and MCP doctor routing,
NODE_OPTIONS, or enabled logging). `freshProcess: true` keeps
explicit process-level checks. The session worker now presents the
launcher's entrypoint, so registration identity matches a fresh
process.

`runFileCli` and `sharedCliRun` are superseded and removed; their
call sites use `cli.run`, and paired fresh/shared checks use
`freshProcess`.

* style(tests): format the managed member summary call site

* test(cli): give each shared-session command an empty stdin

The shared CLI process reads session frames from its own stdin. A
command that reads stdin without supplied input, such as
`compare-wasm-interfaces - --json`, read that pipe and hung, where a
fresh process receives EOF. The worker now presents an ended, empty
stdin to each command and restores its own afterwards.

The parity test compared the routed path with itself after cli.run
began choosing the session. It now compares a fresh process with the
session directly, including the `mcp add` refusal and a command
reading absent stdin.

* test(cli): keep default command logging and launcher checks on fresh processes

environment-serve and environment-build log at info when REA_LOG_LEVEL is
unset, through a logger that writes to stderr directly. The shared CLI
process captures only process.stderr writes, so routed runs lost those
lines. Declare the default-logging commands beside the command names,
have infoLogger read that set, and start a fresh process for them.

The log-level precedence checks compared the shared process with itself
after routing; their fresh side now passes freshProcess: true.
2026-11-14 14:28:08 +08:00

80 lines
3.0 KiB
TypeScript

import { createHash } from "node:crypto";
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "vitest";
import { toolContract } from "../../src/contracts/toolContracts.js";
import {
corePrpsinfoNote,
elfNoteFixture,
gnuBuildIdNote,
} from "../../src/native/elf/ElfNotes.fixture.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { cliTest } from "../support/cli/cliFixture.js";
cliTest.for([
{ name: "System V control", osabi: 0, version: 0, linux: true },
{ name: "Linux control", osabi: 3, version: 0, linux: true },
{ name: "Solaris negative", osabi: 6, version: 0, linux: false },
{ name: "FreeBSD negative", osabi: 9, version: 0, linux: false },
{ name: "System V version negative", osabi: 0, version: 1, linux: false },
{ name: "Linux version negative", osabi: 3, version: 1, linux: false },
])(
"qualifies CORE fields through CLI and stdio MCP: $name",
async ({ osabi, version, linux }, { cli }) => {
const root = await createTestTempDirectory("rea-elf-core-profile-");
const path = join(root, "profile.core");
const bytes = elfNoteFixture({
type: 4,
ownership: "program",
notes: Buffer.concat([
corePrpsinfoNote({ pid: 1234, uid: 70000 }),
gnuBuildIdNote("0123456789abcdef"),
]),
});
bytes[7] = osabi;
bytes[8] = version;
await writeFile(path, bytes);
const hash = createHash("sha256").update(bytes).digest("hex");
const output = await cli.run({
arguments: ["inspect-elf-notes", path, "--json"],
});
expect(output.exitCode, output.stderr).toBe(0);
const call = await cli.sharedMcpCall!();
const response = await call("inspect_elf_notes", { path });
expect(response.isError, JSON.stringify(response.content)).not.toBe(true);
const schema = toolContract("inspect_elf_notes").outputSchema;
for (const raw of [output.json, response.structuredContent]) {
const evidence = schema.parse(raw);
expect(evidence.subject?.digest.sha256).toBe(hash);
const report = evidence.normalized_result;
expect(report.coverage).toMatchObject({
status: "complete",
examined_tables: 1,
examined_entries: 2,
});
expect(report.entries[0]?.descriptor).toMatchObject({
bytes: 136,
sha256: createHash("sha256")
.update(bytes.subarray(140, 276))
.digest("hex"),
});
if (linux) {
expect(report.entries[0]?.type_name).toBe("core_prpsinfo");
expect(report.entries[0]?.core?.prpsinfo).toMatchObject({
pid: 1234,
uid: 70000,
});
} else {
expect(report.entries[0]?.type_name).toBe("unknown");
expect(report.entries[0]?.core).toBeNull();
expect(report.limitations.join(" ")).toContain(
`OSABI ${String(osabi)}`,
);
}
expect(report.entries[1]?.gnu?.build_id_hex).toBe("0123456789abcdef");
}
expect((await readFile(path)).equals(bytes)).toBe(true);
},
);