Files
reverse-engineering-anything/tests/boundary/cli/commandEvidenceDelivery.test.ts
T
Ysopwnableandgensokyo-dev 62014b566d refactor(cli)!: dispatch commands through REA-owned execution
Run ordinary CLI commands through REA's registry, argv parser and result owner.
Remove Incur, the second admission parse, SDK error middleware, output capture
and redundant command traversal. Preserve explicit redaction, publication
receipts, incremental output, terminal notice eligibility and drain ownership.
Migrate fixtures to real registrations and retain the derived-source notices.

Validation includes unchanged public parity goldens, public and lifecycle
regressions, repaired fixture consumers, real TTY base comparisons, exports
beyond the engine string limit, four schema catalogs and final static/docs
checks. Native provider and optional validator coverage limits are in the PR.

Fixes #11654. Fixes #10583. Refs #11682, #11476 and #10511.

BREAKING CHANGE: Markdown argument diagnostics use REA's literal escaping,
table widths and token counts. Flag-shaped unknown MCP subcommands honor the
selected format. Completion --schema/--version routes use the canonical
REA-owned hook rather than the retired SDK generator. Refresh affected literal
snapshots and regenerate completion hooks with rea completions <shell>.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-11-16 13:56:34 +00:00

287 lines
10 KiB
TypeScript

import { cliCommand } from "../../../src/cli/commandRegistry.js";
import { createCommandCli } from "../../support/cli/commandCli.js";
import { unknownTarget } from "../../../src/domain/targetIdentity.js";
import { closeVerifierResources } from "../../../scripts/lib/verifier-cleanup.mjs";
import { z } from "zod";
import pino from "pino";
import { readFile, readdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, onTestFinished, vi } from "vitest";
import { registerReverseEngineeringCommands } from "../../../src/cli/reverseEngineeringCommands.js";
import type { CliInstance } from "../../../src/cli/types.js";
import {
CLI_COMMANDS,
cliCommandFor,
} from "../../../src/contracts/cliCommandNames.js";
import {
createEvidence,
parseEvidence,
evidenceSchema,
} from "../../../src/domain/evidence.js";
import { parseEvidenceBundle } from "../../../src/domain/evidenceBundle.js";
import { ok } from "../../../src/domain/result.js";
import { OwnedStagedOutput } from "../../../src/filesystem/OwnedStagedOutput.js";
import { GdbSessionManager } from "../../../src/gdb/GdbSessionManager.js";
import { TestReverseEngineeringService } from "../../support/reverseEngineeringService.js";
import { ownedOutput } from "../../support/reverseEngineeringCompositionFixtures.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const kinds = ["objdump", "architectures", "gdb"] as const;
const fixture = async (kind: (typeof kinds)[number]) => {
const directory = await createTestTempDirectory("rea-command-delivery-");
const path = join(directory, "input.bin");
await writeFile(path, "local CLI publication fixture");
const runProvider = vi.fn(async () =>
ownedOutput("opaque provider output\n", "provider diagnostic\n"),
);
const service = new TestReverseEngineeringService({
environment: {},
runOwned: runProvider,
});
const closeService = vi.spyOn(service, "close");
const manager = new GdbSessionManager({ platform: "linux", environment: {} });
const sessionId = "00000000-0000-4000-8000-000000000001";
const report = {
command: "show architecture",
mi: "5^done",
records: ['~"architecture\\n"', "unknown record", "5^done"],
console: '~"architecture\\n"',
target: "",
log: "",
completion_status: "complete" as const,
output_truncated: false,
};
const parent = createEvidence(
unknownTarget("not-observed"),
{ id: "gdb", name: "GDB", version: null },
{
operation: "gdb_console",
parameters: { session_id: sessionId, command: report.command },
result: report,
rawResult: report,
},
);
const start = vi
.spyOn(manager, "start")
.mockResolvedValue(ok({ session_id: sessionId, mi_version: "mi3" }));
vi.spyOn(manager, "execute").mockResolvedValue(
ok({ result: report, evidence: parent }),
);
const close = vi
.spyOn(manager, "close")
.mockResolvedValue(ok({ target_state: "none_observed" }));
const closeAll = vi.spyOn(manager, "closeAll").mockResolvedValue(undefined);
const createManager = vi.fn(() => manager);
const cli = createCommandCli();
registerReverseEngineeringCommands(
cli as unknown as CliInstance,
pino({ enabled: false }),
{},
createManager,
() => service,
);
onTestFinished(() =>
closeVerifierResources([() => service.close(), () => manager.closeAll()]),
);
const commandName =
kind === "gdb"
? CLI_COMMANDS.debugWithGdb
: kind === "objdump"
? cliCommandFor("inspect_with_objdump")
: CLI_COMMANDS.listObjdumpArchitectures;
const command = cliCommand(cli, commandName)?.definition;
if (command === undefined) throw new Error("CLI command was not registered");
// A command without positionals registers no argument schema.
const argsSchema: unknown = Reflect.get(command, "args") ?? z.object({});
const optionsSchema = Reflect.get(command, "options");
const execute = Reflect.get(command, "run");
if (
!(argsSchema instanceof z.ZodObject) ||
!(optionsSchema instanceof z.ZodObject) ||
typeof execute !== "function"
)
throw new Error("Missing CLI command schemas or callback");
const args =
kind === "gdb"
? { command: report.command }
: kind === "objdump"
? { path, operation: "file_headers" }
: {};
const invoke = async (options: Record<string, unknown>): Promise<unknown> =>
execute({
args: argsSchema.parse(args),
formatExplicit: false,
options: optionsSchema.parse({
...(kind === "gdb" ? { inferiorTty: "/dev/pts/caller-fixture" } : {}),
...options,
}),
});
return {
directory,
invoke,
runProvider,
createManager,
start,
close,
closeAll,
closeService,
parent,
report,
};
};
it.each(kinds)(
"publishes %s summaries with exact complete parents",
async (kind) => {
const f = await fixture(kind);
const destination = join(f.directory, "complete.json");
const output = z
.object({
summary: evidenceSchema,
evidence_bundle: z.object({ path: z.string(), bytes: z.number() }),
})
.parse(
await f.invoke({ detail: "summary", evidence_output: destination }),
);
const selected = parseEvidence(output.summary);
const serialized = await readFile(destination, "utf8");
const bundle = JSON.parse(serialized);
expect(output.evidence_bundle.bytes).toBe(Buffer.byteLength(serialized));
expect(bundle.records).toHaveLength(1);
const parent = parseEvidence(bundle.records[0]);
expect(parent.raw_result).toEqual(parent.normalized_result);
expect(selected.normalized_result).toMatchObject({
parent_evidence_id: parent.evidence_id,
source_parameters: parent.parameters,
source_provider: parent.provider,
});
expect(selected.raw_result).toBeNull();
expect(output.evidence_bundle.path).toBe(destination);
if (kind === "gdb") {
expect(parent).toEqual(f.parent);
expect(selected.normalized_result).toMatchObject({
artifact: null,
summary: { records: f.report.records },
});
expect(f.close).toHaveBeenCalledOnce();
expect(f.closeAll).toHaveBeenCalledOnce();
} else {
expect(selected.normalized_result).toMatchObject({
summary: parent.normalized_result,
});
expect(f.runProvider).toHaveBeenCalledOnce();
expect(f.closeService).toHaveBeenCalledOnce();
}
if (kind !== "objdump") {
expect(parent.subject.identity.kind).toBe("unknown");
expect(selected.subject.identity.kind).toBe("unknown");
expect(selected.normalized_result).toMatchObject({ artifact: null });
}
},
);
it.each(kinds)(
"rejects %s summary without a destination before provider work",
async (kind) => {
const f = await fixture(kind);
const output = await f.invoke({ detail: "summary" });
expect(output).toMatchObject({ code: "invalid_request" });
expect(JSON.stringify(output)).toContain("evidence_output");
expect(f.createManager).not.toHaveBeenCalled();
expect(f.start).not.toHaveBeenCalled();
expect(f.runProvider).not.toHaveBeenCalled();
},
);
it.each(kinds)(
"preserves an existing %s destination and reports publication failure with source Evidence",
async (kind) => {
const f = await fixture(kind);
const destination = join(f.directory, "existing.json");
const original = Buffer.from("caller-selected content\n");
await writeFile(destination, original);
const output = await f.invoke({
detail: "summary",
evidence_output: destination,
});
expect(Buffer.compare(await readFile(destination), original)).toBe(0);
expect(output).toHaveProperty("code");
expect(output).not.toHaveProperty("summary");
expect(output).toHaveProperty("details.partial_observation.evidence_id");
if (kind === "gdb") {
expect(f.close).toHaveBeenCalledOnce();
expect(f.closeAll).toHaveBeenCalledOnce();
} else expect(f.closeService).toHaveBeenCalledOnce();
},
);
const interruptAt = (method: "assertReadyForPublication" | "close"): void => {
const original = OwnedStagedOutput.prototype[method];
const seam = vi
.spyOn(OwnedStagedOutput.prototype, method)
.mockImplementationOnce(async function (
this: OwnedStagedOutput,
...arguments_: Parameters<OwnedStagedOutput[typeof method]>
) {
await Reflect.apply(original, this, arguments_);
process.emit("SIGINT");
});
onTestFinished(() => {
seam.mockRestore();
process.exitCode = undefined;
});
};
it.each(kinds)(
"keeps the committed %s receipt when SIGINT arrives during final staging cleanup",
async (kind) => {
const f = await fixture(kind);
const destination = join(f.directory, "complete.json");
interruptAt("close");
const output = await f.invoke({
detail: "summary",
evidence_output: destination,
});
expect(process.exitCode).toBe(130);
const serialized = await readFile(destination, "utf8");
const parent = parseEvidenceBundle(JSON.parse(serialized)).records[0]!;
expect(output).toMatchObject({
code: "cancelled",
details: {
diagnostics: {
evidence_bundle: {
path: destination,
bytes: Buffer.byteLength(serialized),
},
},
partial_observation: {
evidence_links: [parent.evidence_id],
normalized_result: { parent_evidence_id: parent.evidence_id },
},
},
});
expect(await readdir(f.directory)).not.toContainEqual(
expect.stringMatching(/^\.rea-write-/u),
);
},
);
it.each(kinds)(
"publishes no %s bundle or receipt when SIGINT arrives before commit",
async (kind) => {
const f = await fixture(kind);
const destination = join(f.directory, "complete.json");
interruptAt("assertReadyForPublication");
const output = await f.invoke({
detail: "summary",
evidence_output: destination,
});
expect(process.exitCode).toBe(130);
expect(output).toMatchObject({ code: "cancelled" });
expect(output).not.toHaveProperty("details.diagnostics");
expect(JSON.stringify(output)).not.toContain(destination);
expect(output).toHaveProperty("details.partial_observation.raw_result");
expect(await readdir(f.directory)).toEqual(["input.bin"]);
},
);