Files
reverse-engineering-anything/tests/boundary/cli/cliBuiltinArguments.test.ts
T
morluto 5257a3d912 refactor(cli): read global arguments once with one REA owner (#11687)
parseCliGlobalArguments replaces normalizeCliArguments, splitIncurBuiltinArguments and splitCliTimingArguments: it normalizes spaced output forms, routes the root --update alias, splits builtin switches and output values, and reads the timing global in one pass. The output-argument reader, the launcher's doctor path, lazy registration, the Amp scope guard, completions, mcp add, strict positionals and sensitive-value redaction read that record instead of scanning argv again. Only the incur hand-off adapter remains until dispatch moves to REA. Output is unchanged; the CLI parity goldens pass as recorded.
2026-11-16 12:23:51 +08:00

219 lines
6.1 KiB
TypeScript

import { spawnSync } from "node:child_process";
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
import { join, resolve } from "node:path";
import { describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import {
classifyMcpAddDiscovery,
isProjectScopedAmpRegistration,
parseCliGlobalArguments,
} from "../../../src/cliBuiltinArguments.js";
/** Whether these caller arguments ask for a project-scoped Amp registration. */
const projectScoped = (arguments_: readonly string[]): boolean =>
isProjectScopedAmpRegistration(parseCliGlobalArguments(arguments_));
describe("mcp add discovery classification", () => {
it("bypasses help and listing flags", () => {
for (const flag of [
"--help",
"-h",
"--llms",
"--llms-full",
"--mcp",
"--update",
"--incur-update-check",
]) {
expect(classifyMcpAddDiscovery(new Set([flag]))).toBe("bypass");
}
});
it("answers version and schema inside the mcp add special-casing", () => {
expect(classifyMcpAddDiscovery(new Set(["--version"]))).toBe("version");
expect(classifyMcpAddDiscovery(new Set(["--schema"]))).toBe("schema");
});
it("keeps routing precedence when several discovery flags combine", () => {
expect(classifyMcpAddDiscovery(new Set(["--help", "--version"]))).toBe(
"bypass",
);
expect(classifyMcpAddDiscovery(new Set(["--version", "--schema"]))).toBe(
"version",
);
});
it("returns undefined for real registrations", () => {
expect(classifyMcpAddDiscovery(new Set(["--json"]))).toBeUndefined();
expect(classifyMcpAddDiscovery(new Set())).toBeUndefined();
});
});
describe("project-scoped Amp registration guard", () => {
it("flags explicit Amp targets under --no-global", () => {
expect(
projectScoped([
"mcp",
"add",
"--no-global",
"--agent",
"amp",
"--command",
"node project-rea.mjs",
]),
).toBe(true);
});
it("flags the default Amp registration when no agent is given", () => {
expect(projectScoped(["mcp", "add", "--no-global"])).toBe(true);
});
it("ignores output flags Incur strips before dispatch", () => {
expect(
projectScoped([
"mcp",
"add",
"--no-global",
"--agent",
"amp",
"--command",
"node project-rea.mjs",
"--json",
]),
).toBe(true);
});
it("passes global Amp registration through", () => {
expect(
projectScoped([
"mcp",
"add",
"--agent",
"amp",
"--command",
"node project-rea.mjs",
]),
).toBe(false);
});
it("passes project-scoped non-Amp agents through", () => {
expect(
projectScoped(["mcp", "add", "--no-global", "--agent", "claude-code"]),
).toBe(false);
});
it("passes discovery requests through, matching mcp add discovery", () => {
for (const discovery of [
"--help",
"-h",
"--llms",
"--llms-full",
"--version",
"--schema",
]) {
expect(
projectScoped([
"mcp",
"add",
"--no-global",
"--agent",
"amp",
discovery,
]),
).toBe(false);
}
expect(projectScoped(["mcp", "add", "--no-global", "--help"])).toBe(false);
});
it("reads equals-form agents the same way dispatch does", () => {
// --agent=claude-code is a non-Amp registration, not the default Amp.
expect(
projectScoped(["mcp", "add", "--no-global", "--agent=claude-code"]),
).toBe(false);
expect(projectScoped(["mcp", "add", "--no-global", "--agent=amp"])).toBe(
true,
);
// A later equals-form Amp must not slip past an earlier spaced agent.
expect(
projectScoped([
"mcp",
"add",
"--no-global",
"--agent",
"claude-code",
"--agent=amp",
]),
).toBe(true);
// A --command value is never read as an agent, even in equals form.
expect(
projectScoped(["mcp", "add", "--no-global", "--command=--agent", "amp"]),
).toBe(true);
});
it("passes other commands through", () => {
expect(projectScoped(["mcp", "doctor"])).toBe(false);
expect(projectScoped(["doctor"])).toBe(false);
expect(projectScoped(["mcp", "add", "--no-global", "--agent", "Amp"])).toBe(
false,
);
});
});
describe("project-scoped Amp registration launcher", () => {
it("refuses explicit Amp and default-agent requests without writing settings", async () => {
const home = await createTestTempDirectory("rea-amp-launcher-home-");
const workingDirectory = await createTestTempDirectory(
"rea-amp-launcher-cwd-",
);
const ampDirectory = join(home, ".config", "amp");
await mkdir(ampDirectory, { recursive: true });
const settingsPath = join(ampDirectory, "settings.json");
const sentinel = JSON.stringify({ preexisting: true });
await writeFile(settingsPath, sentinel);
for (const args of [
["mcp", "add", "--no-global", "--agent", "amp", "--json"],
["mcp", "add", "--no-global", "--json"],
["mcp", "add", "--no-global", "--agent=amp", "--json"],
[
"mcp",
"add",
"--no-global",
"--agent",
"claude-code",
"--agent=amp",
"--json",
],
]) {
const result = spawnSync(
process.execPath,
[resolve("scripts/rea.mjs"), ...args],
{
cwd: workingDirectory,
env: {
PATH: process.env.PATH ?? "",
HOME: home,
REA_LOG_LEVEL: "silent",
},
encoding: "utf8",
timeout: 60_000,
},
);
expect(result.error).toBeUndefined();
expect(result.signal).toBeNull();
expect(result.status).toBe(1);
expect(JSON.parse(result.stdout)).toEqual({
ok: false,
error: {
code: "AMP_PROJECT_SCOPE_UNSUPPORTED",
message: expect.any(String),
},
});
expect(result.stderr).toBe("");
expect(await readFile(settingsPath, "utf8")).toBe(sentinel);
expect(await readdir(workingDirectory)).toEqual([]);
}
});
});