Files
reverse-engineering-anything/tests/boundary/cli/dispatcher.test.ts
T
Kaoru0822andClaude Opus 5.5 0781716788 fix(cli): accept --flag=value for global output flags (#3811)
* fix(cli): accept --flag=value for global output flags

`rea <command> --format=json` failed with "Unknown flag: --format".
`--filter-output=...`, `--token-limit=...` and `--token-offset=...`
failed the same way. Incur reads these global flags only in spaced form,
so the equals form fell through to the command parser. Command options
already accept both forms, and `rea mcp doctor` parses `--format=` itself.

A valued output flag with no value (`--format` last, or `--format=`)
also reported the flag as unknown with code UNKNOWN.

normalizeCliOutputArguments, in the module that owns output-argument
interpretation, now splits the equals forms of the four valued output
flags. The entry point passes the normalized argv to rea's output
validation and to Incur, so both read the same effective values. A
missing value now fails with MISSING_OUTPUT_VALUE and names the flag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): detect missing output values while parsing output flags

The missing-value check scanned argv again after parsing, so it also
checked tokens the parser had already consumed as values. For example,
`--filter-output --format` selects the field `--format`, yet the second
scan reported `--format` as missing a value. parseCliOutputArguments
now records the first valued flag it skips for lack of a value, in the
same pass that consumes values.

Cover normalizeCliOutputArguments directly: values containing `=`, empty
values, and command options that only share a prefix. Export it in the
dispatcher test's runtime stub as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 20:22:02 -07:00

105 lines
3.9 KiB
TypeScript

import { copyFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect } from "vitest";
import { workspaceCliTest } from "../../support/cli/workspaceCliFixture.js";
describe("executable dispatcher", () => {
workspaceCliTest(
"reserves production MCP routing for bare mode arguments",
async ({ processes, workspace }) => {
const scripts = await workspace.mkdir("scripts");
await workspace.mkdir("dist");
await workspace.mkdir("dist/cli-runtime");
await workspace.mkdir("dist/mcp");
await Promise.all([
copyFile("scripts/rea.mjs", join(scripts, "rea.mjs")),
workspace.write(
"package.json",
JSON.stringify({ type: "module", version: "1.0.0" }),
),
workspace.write(
"dist/mcp/main.js",
'export const runEntrypoint = () => process.stdout.write(JSON.stringify({ adapter: "mcp" }));\n',
),
workspace.write(
"dist/cli-runtime/cli.js",
'export const cliConfigurationError = () => undefined; export const createCli = () => ({ serve: (args) => process.stdout.write(JSON.stringify({ adapter: "cli", args })) });\n',
),
workspace.write(
"dist/cli-runtime/cliOutput.js",
"export const normalizeCliOutputArguments = (args) => args; export const validateCliOutputArguments = () => ({ ok: true }); export const renderCliOutputArgumentError = () => ''; export const renderEmptyFilteredCliOutput = () => undefined;\n",
),
workspace.write(
"dist/cli-runtime/cliResultOutput.js",
"export const createCliResultOutput = () => undefined;\n",
),
workspace.write(
"dist/cli-runtime/mcpDoctor.js",
'export const runProductionMcpDoctorCli = (args) => Promise.resolve({ output: JSON.stringify({ adapter: "mcp-doctor", args }), exitCode: 0 });\n',
),
]);
const invoke = async (args: readonly string[]): Promise<unknown> => {
const { exitCode, stdout } = await processes.run(
process.execPath,
[join(scripts, "rea.mjs"), ...args],
{ cwd: workspace.root },
);
expect(exitCode).toBe(0);
return JSON.parse(stdout) as unknown;
};
await expect(invoke(["mcp"])).resolves.toEqual({ adapter: "mcp" });
await expect(invoke(["--mcp"])).resolves.toEqual({ adapter: "mcp" });
await expect(invoke(["mcp", "doctor", "--json"])).resolves.toEqual({
adapter: "mcp-doctor",
args: ["--json"],
});
await expect(invoke(["mcp", "add"])).resolves.toEqual({
adapter: "cli",
args: ["mcp", "add"],
});
await expect(invoke(["--mcp", "extra"])).resolves.toEqual({
adapter: "cli",
args: ["--mcp", "extra"],
});
},
);
workspaceCliTest(
"explains how to restore a missing compiled runtime",
async ({ processes, workspace }) => {
const scripts = await workspace.mkdir("scripts");
await Promise.all([
copyFile("scripts/rea.mjs", join(scripts, "rea.mjs")),
workspace.write(
"package.json",
JSON.stringify({ type: "module", version: "1.0.0" }),
),
]);
for (const args of [["mcp"], ["mcp", "doctor"], ["--help"]]) {
const result = await processes.run(
process.execPath,
[join(scripts, "rea.mjs"), ...args],
{ cwd: workspace.root },
);
expect(result).toMatchObject({
exitCode: 1,
stderr: expect.stringContaining(
"REA's compiled runtime is missing. Run `npm ci && npm run build:cached` in",
),
});
expect(result.stderr).toContain(
"If this is an installed package, reinstall rea-agents.",
);
expect(result).toMatchObject({
stderr: expect.not.stringContaining("ERR_MODULE_NOT_FOUND"),
});
}
},
);
});