Files
reverse-engineering-anything/tests/acceptance/cliFormattingCapacity.test.ts
T
morluto ec1cf3a9bd refactor(cli): render command help with REA's own help owner (#11683)
* refactor(cli): render command help with REA's own help owner

Add src/cli/help.ts and move the three incur Help.formatCommand callers
(rea mcp add, rea mcp doctor, rea mcp --http) onto it. Option spelling and
the unwrapped field type move to src/cli/optionFields.ts, which completion
now reads instead of its own copies. A counted incur-import guard records
the names still imported from incur so the list can only shrink.

Refs #11654

* refactor(cli): share option spelling across CLI owners and count every incur import

The argv parser, strict positionals, contract command projection and the
command registry now read the flag spelling, declared field type and count
marker from src/cli/optionFields.ts instead of their own copies. The
incur-import guard also counts namespace, default, star, side-effect and
dynamic imports, so `import * as Incur from "incur"` cannot bring a removed
owner back.

* refactor(cli): format and filter CLI output without incur (#11684)

* refactor(cli): format and filter CLI output without incur

formatCliOutput encodes TOON (@toon-format/toon), JSON, YAML and JSON Lines
itself instead of calling incur's Formatter; YAML still loads on first use.
src/cli/outputFilter.ts owns the --filter-output grammar and returns a
Result: an unclosed slice is a parse failure, so output validation reads
the parser instead of a second bracket scanner, and the parsed paths
travel with the output arguments to the result writer and JSON Lines.
@toon-format/toon moves from devDependencies to dependencies because REA
now imports it at runtime.

Refs #11654

* test(cli): pin output encodings and filter grammar with literal cases

Literal expected outputs for empty values, TOON scalars, objects and rows,
JSON-text re-indentation, nested BigInt values, JSON Lines rows and YAML,
and literal parsed paths and unclosed-slice refusals for --filter-output,
so a change to either owner fails without comparing against itself.
2026-11-16 12:01:04 +08:00

202 lines
7.2 KiB
TypeScript

import { writeFile } from "node:fs/promises";
import { resolve } from "node:path";
import { expect } from "vitest";
import { parse as parseYaml } from "yaml";
import { buildManagedPeFixture } from "../../src/dotnet/ManagedPe.fixture.js";
import { analysisErrorProjectionSchema } from "../../src/domain/analysisErrorProjectionSchema.js";
import { workspaceCliTest } from "../support/cli/workspaceCliFixture.js";
// Exercise the real dispatcher and the pinned TOON and YAML encoders with an
// authored capacity failure. No engine-sized document or native target
// execution is needed.
const encoderFailure = (value: string) => `
if (${value}?.operation === "inspect_managed_artifact" ||
${value}?.data?.operation === "inspect_managed_artifact" ||
${value}?.code === "target_unavailable" ||
${value}?.error?.code === "target_unavailable") {
process.stderr.write("REA_AUTHORED_FORMAT_CAPACITY\\n");
throw new RangeError("Invalid string length");
}
`;
const encoders = [
{
file: "/@toon-format/toon/dist/index.mjs",
signature: "function encode(input, options) {",
failure: encoderFailure("input"),
},
{
file: "/yaml/dist/public-api.js",
signature: "function stringify(value, replacer, options) {",
failure: encoderFailure("value"),
},
];
const encoderCapacityHook = `data:text/javascript,${encodeURIComponent(`
import { registerHooks } from "node:module";
const encoders = ${JSON.stringify(encoders)};
registerHooks({
load(url, context, next) {
const loaded = next(url, context);
const encoder = encoders.find(({ file }) => url.endsWith(file));
if (encoder === undefined) return loaded;
const source = String(loaded.source);
if (!source.includes(encoder.signature)) throw new Error("Pinned encoder boundary changed: " + encoder.file);
return { ...loaded, source: source.replace(encoder.signature, encoder.signature + encoder.failure) };
},
});
`)}`;
// REA owns Markdown rendering. Fail one scalar's string materialization in the
// CLI child, restoring String before the recovery document uses that formatter.
// This does not depend on generated bundle names or compiled function text.
const markdownCapacityHook = `data:text/javascript,${encodeURIComponent(`
globalThis.String = new Proxy(String, {
apply(target, receiver, args) {
if (["inspect_managed_artifact", "target_unavailable"].includes(args[0])) {
globalThis.String = target;
process.stderr.write("REA_AUTHORED_FORMAT_CAPACITY\\n");
throw new RangeError("Invalid string length");
}
return Reflect.apply(target, receiver, args);
},
});
`)}`;
type FormattingScenario = {
readonly name: string;
readonly arguments: readonly string[];
readonly full: boolean;
readonly format?: "yaml" | "md";
readonly human?: boolean;
};
const scenarios: readonly FormattingScenario[] = [
{ name: "default TOON", arguments: [], full: false },
{ name: "human TOON", arguments: [], full: false, human: true },
{ name: "full TOON", arguments: ["--full-output"], full: true },
{ name: "TOON token count", arguments: ["--token-count"], full: false },
{ name: "TOON token window", arguments: ["--token-limit", "3"], full: false },
{
name: "full TOON token window",
arguments: ["--full-output", "--token-limit", "3"],
full: true,
},
{ name: "YAML", format: "yaml", arguments: [], full: false },
{
name: "full YAML",
format: "yaml",
arguments: ["--full-output"],
full: true,
},
{ name: "Markdown", format: "md", arguments: [], full: false },
{
name: "full Markdown",
format: "md",
arguments: ["--full-output"],
full: true,
},
{
name: "Markdown token window",
format: "md",
arguments: ["--token-limit", "3"],
full: false,
},
];
for (const scenario of scenarios)
for (const outcome of ["success", "failure"] as const)
workspaceCliTest(
`${scenario.name} returns a typed formatting constraint after ${outcome}`,
async ({ processes, workspace }) => {
const target = resolve(workspace.root, "fixture.dll");
if (outcome === "success")
await writeFile(target, buildManagedPeFixture());
const result = await processes.run(
process.execPath,
[
"--import",
scenario.human
? `data:text/javascript,${encodeURIComponent('Object.defineProperty(process.stdout, "isTTY", { value: true });')}`
: "data:text/javascript,",
"--import",
scenario.format === "md"
? markdownCapacityHook
: encoderCapacityHook,
resolve("scripts/rea.mjs"),
"inspect-managed-artifact",
target,
...(scenario.format ? ["--format", scenario.format] : []),
...scenario.arguments,
],
{ cwd: workspace.root, env: { PATH: process.env.PATH ?? "" } },
);
expect(result.exitCode, result.stderr).toBe(1);
expect(result.stderr.trim()).toBe("REA_AUTHORED_FORMAT_CAPACITY");
expect(result.stdout).toContain("resource_constraint");
expect(result.stdout).toContain("--format json");
expect(result.stdout).toContain("formatted_characters");
expect(result.stdout).not.toContain("[truncated:");
expect(result.stdout).not.toContain("UNKNOWN");
if (outcome === "failure")
expect(result.stdout).toContain("target_unavailable");
if (scenario.format === "md" && scenario.full) {
expect(result.stdout).toMatch(/^## ok\n\nfalse$/m);
expect(result.stdout).toMatch(
/^## error\.code\n\nresource_constraint$/m,
);
expect(result.stdout).toMatch(
/^## meta\n\n(?:\|[^\n]*\|\n)*\| command +\| inspect-managed-artifact +\|$/m,
);
}
if (scenario.format === "yaml") {
const document = parseYaml(result.stdout);
const error = scenario.full ? document.error : document;
expect(analysisErrorProjectionSchema.parse(error)).toMatchObject({
code: "resource_constraint",
retryable: false,
details: {
resource: "memory",
reported_limits: { formatted_characters: null },
},
});
if (scenario.full)
expect(document).toMatchObject({
ok: false,
meta: { command: "inspect-managed-artifact" },
});
}
},
);
workspaceCliTest.skipIf(process.platform === "win32")(
"keeps the SIGPIPE outcome when the reader closes before formatting recovery",
async ({ processes, workspace }) => {
const child = processes.spawn(
process.execPath,
[
"--import",
encoderCapacityHook,
resolve("scripts/rea.mjs"),
"inspect-managed-artifact",
workspace.path("missing.dll"),
],
{ cwd: workspace.root, env: { PATH: process.env.PATH ?? "" } },
);
child.stdout.destroy();
child.stdin.end();
let stderr = "";
child.stderr.setEncoding("utf8");
child.stderr.on("data", (part: string) => {
stderr += part;
});
const code = await new Promise<number | null>((done) =>
child.once("close", done),
);
expect({ code, stderr }).toEqual({
code: 141,
stderr: "REA_AUTHORED_FORMAT_CAPACITY\n",
});
},
);