Non-invasive MCP accelerator plugin for Google Antigravity (CLI/Desktop/IDE): cuts LLM turns (-34%), token quota (-31%), and upload traffic (-40%) without affecting native UI or diffs.
TypeScript ai-agentai-codingantigravityantigravity-cliantigravity-ideantigravity-pluginscoding-agentcontext-optimizationdeveloper-productivitydeveloper-toolsgeminigoogle-antigravitylatency-optimizationmcpmcp-pluginmcp-servermodel-context-protocolperformanceplugintoken-optimization
import { describe, it, before, after } from "node:test";
import assert from "node:assert/strict";
import { spawn } from "node:child_process";
import { promises as fs } from "node:fs";
import os from "node:os";
import path from "node:path";
import { PassThrough } from "node:stream";
import { runAgyFastCli, shouldCliExitNonZero } from "../src/cli-runner.js";
const MCP_SERVER_SCRIPT = path.resolve(process.cwd(), "dist", "mcp-server.mjs");
function runCliSubprocess(
args: string[],
options: { cwd: string; stdin?: string; closeStdin?: boolean },
): Promise<{ code: number | null; stdout: string; stderr: string }> {
return new Promise((resolve, reject) => {
const child = spawn(process.execPath, [MCP_SERVER_SCRIPT, "--cli", ...args], {
cwd: options.cwd,
stdio: ["pipe", "pipe", "pipe"],
windowsHide: true,
});
let stdout = "";
let stderr = "";
child.stdout.on("data", (d) => (stdout += d.toString("utf8")));
child.stderr.on("data", (d) => (stderr += d.toString("utf8")));
child.on("error", reject);
child.on("close", (code) => resolve({ code, stdout, stderr }));
if (options.stdin !== undefined) {
child.stdin.write(options.stdin);
}
if (options.closeStdin !== false) {
child.stdin.end();
}
});
}
describe("agy-fast --cli direct runner", () => {
let tmpDir: string;
before(async () => {
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "agy-fast-cli-test-"));
await fs.mkdir(path.join(tmpDir, "src"), { recursive: true });
await fs.writeFile(
path.join(tmpDir, "src", "math.ts"),
[
"export function add(a: number, b: number): number {",
" return a + b;",
"}",
"",
"export function multiply(a: number, b: number): number {",
" return a * b;",
"}",
].join("\n") + "\n",
"utf8",
);
await fs.writeFile(
path.join(tmpDir, "src", "greet.ts"),
[
"export function greet(name: string): string {",
" return `hello ${name}`;",
"}",
].join("\n") + "\n",
"utf8",
);
});
after(async () => {
await fs.rm(tmpDir, { recursive: true, force: true });
});
it("supports outline, slice, search, check, and edit via built dist/mcp-server.mjs --cli", async () => {
// 1. Help
const helpRes = await runCliSubprocess(["--help"], { cwd: tmpDir });
assert.equal(helpRes.code, 0);
assert.match(helpRes.stdout, /agy-fast direct CLI mode/);
// 2. Positional outline (even when stdin is left open, must NOT hang!)
const outlineRes = await runCliSubprocess(["outline", "src/math.ts", "src/greet.ts"], {
cwd: tmpDir,
closeStdin: false,
});
assert.equal(outlineRes.code, 0, outlineRes.stderr);
assert.match(outlineRes.stdout, /\[function\] add/);
assert.match(outlineRes.stdout, /\[function\] multiply/);
assert.match(outlineRes.stdout, /\[function\] greet/);
assert.ok(Buffer.byteLength(outlineRes.stdout, "utf8") <= 8800);
// 3. Slice with positional lineRanges + --outline
const sliceRes = await runCliSubprocess(
["slice", "src/math.ts:1-3", "--outline", "src/greet.ts"],
{ cwd: tmpDir },
);
assert.equal(sliceRes.code, 0, sliceRes.stderr);
assert.match(sliceRes.stdout, /return a \+ b/);
assert.match(sliceRes.stdout, /\[function\] greet/);
// 4. Search via shorthand flags (multiple queries in one call)
const searchFlagsRes = await runCliSubprocess(
["search", "--text", "multiply", "--glob", "src/*.ts"],
{ cwd: tmpDir },
);
assert.equal(searchFlagsRes.code, 0, searchFlagsRes.stderr);
assert.match(searchFlagsRes.stdout, /Query 1 \[text\] multiply/);
assert.match(searchFlagsRes.stdout, /Query 2 \[glob\] src\/\*\.ts/);
assert.match(searchFlagsRes.stdout, /src\/greet\.ts/);
// 5. Search via piped JSON shorthand array
const searchPipeRes = await runCliSubprocess(["search"], {
cwd: tmpDir,
stdin: JSON.stringify([{ type: "text", pattern: "hello", path: "src" }]),
});
assert.equal(searchPipeRes.code, 0, searchPipeRes.stderr);
assert.match(searchPipeRes.stdout, /src\/greet\.ts/);
// 6. Check concurrent commands in compact & full modes
const checkRes = await runCliSubprocess(
["check", "--full", "node -e \"console.log('ALPHA_OK')\"", "node -e \"console.log('BETA_OK')\""],
{ cwd: tmpDir },
);
assert.equal(checkRes.code, 0, checkRes.stderr);
assert.match(checkRes.stdout, /Checks: 2\/2 passed/);
assert.match(checkRes.stdout, /ALPHA_OK/);
assert.match(checkRes.stdout, /BETA_OK/);
// 7. Edit via piped stdin JSON + --verify override
const editPayload = JSON.stringify({
edits: [
{
path: "src/greet.ts",
hunks: [{ target: "hello ${name}", replacement: "hi ${name}" }],
},
],
});
const editRes = await runCliSubprocess(
["edit", "--verify", "node -e \"console.log('VERIFIED')\""],
{ cwd: tmpDir, stdin: editPayload },
);
assert.equal(editRes.code, 0, editRes.stderr);
assert.match(editRes.stdout, /Applied edits to 1 file/);
assert.match(editRes.stdout, /Verify \[PASS/);
const updated = await fs.readFile(path.join(tmpDir, "src", "greet.ts"), "utf8");
assert.match(updated, /hi \$\{name\}/);
});
it("handles errors cleanly and never hangs on open empty stdin", async () => {
const unknownRes = await runCliSubprocess(["bogus"], { cwd: tmpDir });
assert.equal(unknownRes.code, 1);
assert.match(unknownRes.stderr, /unknown --cli command/);
const badJsonRes = await runCliSubprocess(["edit", "--json", "{bad"], { cwd: tmpDir });
assert.equal(badJsonRes.code, 1);
assert.match(badJsonRes.stderr, /Invalid JSON input/);
// Simulate non-TTY open pipe with no bytes sent and no positional args
const openStdin = new PassThrough() as PassThrough & { isTTY?: boolean };
openStdin.isTTY = false;
const stdoutBuf: string[] = [];
const stderrBuf: string[] = [];
const out = new PassThrough();
const err = new PassThrough();
out.on("data", (d) => stdoutBuf.push(d.toString("utf8")));
err.on("data", (d) => stderrBuf.push(d.toString("utf8")));
const startedAt = Date.now();
const code = await runAgyFastCli(["search"], {
stdin: openStdin,
stdout: out,
stderr: err,
cwd: tmpDir,
});
const elapsed = Date.now() - startedAt;
assert.equal(code, 1);
assert.match(stderrBuf.join(""), /requires at least one query/);
assert.ok(elapsed < 2000, `Expected non-hanging open stdin guard, took ${elapsed}ms`);
// Failed edit returns exit code 1 with diagnostic text on stdout
const failEdit = await runCliSubprocess(
[
"edit",
"--json",
JSON.stringify({
edits: [{ path: "src/math.ts", hunks: [{ target: "DOES_NOT_EXIST", replacement: "x" }] }],
}),
],
{ cwd: tmpDir },
);
assert.equal(failEdit.code, 1);
assert.match(failEdit.stdout, /target text not found/);
});
it("exits non-zero on failed checks or verify, scopes per-query --path, and searches literal --help/-h", async () => {
const subA = path.join(tmpDir, "pkg-a");
const subB = path.join(tmpDir, "pkg-b");
await fs.mkdir(subA, { recursive: true });
await fs.mkdir(subB, { recursive: true });
await fs.writeFile(path.join(subA, "cli.ts"), "export const flag = '--help';\nexport const tokenA = 'ONLY_IN_A';\n", "utf8");
await fs.writeFile(path.join(subB, "cli.ts"), "export const tokenB = 'ONLY_IN_B';\n", "utf8");
// 1. Literal --help / -h after --text or -- must search, NOT print CLI usage
const literalHelp = await runCliSubprocess(
["search", "--path=pkg-a", "--text", "--help"],
{ cwd: tmpDir },
);
assert.equal(literalHelp.code, 0, literalHelp.stderr);
assert.doesNotMatch(literalHelp.stdout, /agy-fast direct CLI mode/);
assert.match(literalHelp.stdout, /flag = '--help'/);
const dashDashHelp = await runCliSubprocess(
["search", "--path", "pkg-a", "--", "--help"],
{ cwd: tmpDir },
);
assert.equal(dashDashHelp.code, 0, dashDashHelp.stderr);
assert.match(dashDashHelp.stdout, /flag = '--help'/);
// Subcommand --help (not consumed by a value flag) still shows usage and exits 0
const subHelp = await runCliSubprocess(["search", "--help"], { cwd: tmpDir });
assert.equal(subHelp.code, 0);
assert.match(subHelp.stdout, /agy-fast direct CLI mode/);
// 2. Per-query --path scoping must not overwrite earlier queries
const multiScoped = await runCliSubprocess(
["search", "--path", "pkg-a", "--text", "ONLY_IN_A", "--path", "pkg-b", "--text", "ONLY_IN_B"],
{ cwd: tmpDir },
);
assert.equal(multiScoped.code, 0, multiScoped.stderr);
assert.match(multiScoped.stdout, /Query 1 \[text\] ONLY_IN_A \(in pkg-a\)/);
assert.match(multiScoped.stdout, /pkg-a\/cli\.ts/);
assert.match(multiScoped.stdout, /Query 2 \[text\] ONLY_IN_B \(in pkg-b\)/);
assert.match(multiScoped.stdout, /pkg-b\/cli\.ts/);
// 3. --cli check MUST return exit code 1 when any command fails
const failedCheck = await runCliSubprocess(
["check", 'node -e "process.exit(0)"', 'node -e "console.error(\'BOOM\'); process.exit(2)"'],
{ cwd: tmpDir },
);
assert.equal(failedCheck.code, 1);
assert.match(failedCheck.stdout, /Checks: 1\/2 passed, 1 failed/);
assert.match(failedCheck.stdout, /BOOM/);
// 4. --cli edit --verify MUST return exit code 1 when edits succeed but verify fails
const failedVerifyEdit = await runCliSubprocess(
["edit", "--verify=node -e \"console.error('TYPE_ERR'); process.exit(1)\""],
{
cwd: tmpDir,
stdin: JSON.stringify({
edits: [{ path: "pkg-b/cli.ts", hunks: [{ target: "ONLY_IN_B", replacement: "ONLY_IN_B_V2" }] }],
}),
},
);
assert.equal(failedVerifyEdit.code, 1);
assert.match(failedVerifyEdit.stdout, /Verify \[FAIL/);
assert.match(failedVerifyEdit.stdout, /TYPE_ERR/);
// 5. shouldCliExitNonZero must flag `not run` even when failedCount === 0 (no "0 failed," segment)
assert.equal(
shouldCliExitNonZero("run_checks", {
text: "Checks: 1/2 passed, 1 not run (call time budget of 170s used up) (wall: 170000ms)",
isError: false,
}),
true,
);
assert.equal(
shouldCliExitNonZero("run_checks", {
text: "Checks: 1/2 passed, 1 failed (wall: 50ms)",
isError: false,
}),
true,
);
assert.equal(
shouldCliExitNonZero("run_checks", {
text: "Checks: 2/2 passed (wall: 50ms)",
isError: false,
}),
false,
);
assert.equal(
shouldCliExitNonZero("apply_edits", {
text: "Applied edits to 1 file(s):\n- a.ts\n\nVerify [PASS in 10ms] `npm test`:\nOK",
isError: false,
}),
false,
);
});
it("preserves option values matching --flag=val or '--' and scopes modifiers for positional search queries", async () => {
const scopeA = path.join(tmpDir, "scope-a");
const scopeB = path.join(tmpDir, "scope-b");
await fs.mkdir(scopeA, { recursive: true });
await fs.mkdir(scopeB, { recursive: true });
await fs.writeFile(
path.join(scopeA, "flags.ts"),
"export const cliSample = '--max-chars=100';\nexport const sep = '--';\nexport const alpha = 'SCOPE_A_ONLY';\n",
"utf8",
);
await fs.writeFile(
path.join(scopeB, "flags.ts"),
"export const beta = 'SCOPE_B_ONLY';\n",
"utf8",
);
// 1. Value matching --<known-flag>=val after --text must NOT be split into extra queries
const flagValRes = await runCliSubprocess(
["search", "--path=scope-a", "--text", "--max-chars=100"],
{ cwd: tmpDir },
);
assert.equal(flagValRes.code, 0, flagValRes.stderr);
assert.match(flagValRes.stdout, /Query 1 \[text\] --max-chars=100 \(in scope-a\)/);
assert.doesNotMatch(flagValRes.stdout, /Query 2/);
assert.match(flagValRes.stdout, /cliSample = '--max-chars=100'/);
// 2. Value '--' after --text must NOT disable --flag=val parsing for subsequent options
const dashValRes = await runCliSubprocess(
["search", "--text", "--", "--path=scope-a"],
{ cwd: tmpDir },
);
assert.equal(dashValRes.code, 0, dashValRes.stderr);
assert.match(dashValRes.stdout, /Query 1 \[text\] -- \(in scope-a\)/);
assert.match(dashValRes.stdout, /sep = '--'/);
// 3. Positional search queries consume preceding modifiers and inherit trailing modifiers
const posScopedRes = await runCliSubprocess(
["search", "--path", "scope-a", "scope_a_only", "--path", "scope-b", "scope_b_only", "--ignore-case"],
{ cwd: tmpDir },
);
assert.equal(posScopedRes.code, 0, posScopedRes.stderr);
assert.match(posScopedRes.stdout, /Query 1 \[text\] scope_a_only \(in scope-a\)/);
assert.match(posScopedRes.stdout, /SCOPE_A_ONLY/);
assert.match(posScopedRes.stdout, /Query 2 \[text\] scope_b_only \(in scope-b\)/);
assert.match(posScopedRes.stdout, /SCOPE_B_ONLY/);
// 4. Unknown long flag with '=val' for another tool reports the full unknown option cleanly
const badFlagRes = await runCliSubprocess(["outline", "--text=foo"], { cwd: tmpDir });
assert.equal(badFlagRes.code, 1);
assert.match(badFlagRes.stderr, /Unknown option '--text=foo' for 'quick_outline'/);
});
it("supports --pattern and --type (preceding, trailing, and inline) for --cli search", async () => {
// 1. --pattern defaults to text search and inherits trailing modifiers
const defaultPat = await runCliSubprocess(
["search", "--pattern", "multiply", "--path", "src"],
{ cwd: tmpDir },
);
assert.equal(defaultPat.code, 0, defaultPat.stderr);
assert.match(defaultPat.stdout, /Query 1 \[text\] multiply \(in src\)/);
assert.match(defaultPat.stdout, /src\/math\.ts/);
// 2. Preceding --type scopes only to the next --pattern; subsequent --pattern defaults to text
const preTyped = await runCliSubprocess(
["search", "--type", "regex", "--pattern", "add|multiply", "--pattern=greet", "--path=src"],
{ cwd: tmpDir },
);
assert.equal(preTyped.code, 0, preTyped.stderr);
assert.match(preTyped.stdout, /Query 1 \[regex\] add\|multiply \(in src\)/);
assert.match(preTyped.stdout, /Query 2 \[text\] greet \(in src\)/);
assert.match(preTyped.stdout, /src\/greet\.ts/);
// 3. Trailing --type applies to implicit --pattern queries without overwriting explicit --glob
const trailTyped = await runCliSubprocess(
["search", "--pattern=add|multiply", "--glob", "src/*.ts", "--type=regex"],
{ cwd: tmpDir },
);
assert.equal(trailTyped.code, 0, trailTyped.stderr);
assert.match(trailTyped.stdout, /Query 1 \[regex\] add\|multiply/);
assert.match(trailTyped.stdout, /Query 2 \[glob\] src\/\*\.ts/);
// 4. Literal --help value after --pattern must search, NOT print usage
const litHelpPat = await runCliSubprocess(
["search", "--pattern", "--help", "--path", "pkg-a"],
{ cwd: tmpDir },
);
assert.equal(litHelpPat.code, 0, litHelpPat.stderr);
assert.doesNotMatch(litHelpPat.stdout, /agy-fast direct CLI mode/);
assert.match(litHelpPat.stdout, /flag = '--help'/);
// 5. '--type' before '--' scopes only to the post-'--' query without retroactively retyping earlier '--pattern' queries
const dashDashTyped = await runCliSubprocess(
["search", "--path=src", "--pattern", "add", "--path=pkg-a", "--type", "regex", "--", "--help|ONLY_IN_A"],
{ cwd: tmpDir },
);
assert.equal(dashDashTyped.code, 0, dashDashTyped.stderr);
assert.match(dashDashTyped.stdout, /Query 1 \[text\] add \(in src\)/);
assert.match(dashDashTyped.stdout, /Query 2 \[regex\] --help\|ONLY_IN_A \(in pkg-a\)/);
assert.match(dashDashTyped.stdout, /flag = '--help'/);
// 6. Invalid --type and --pattern on non-search subcommand fail cleanly
const badType = await runCliSubprocess(
["search", "--type", "bogus", "--pattern", "add"],
{ cwd: tmpDir },
);
assert.equal(badType.code, 1);
assert.match(badType.stderr, /Invalid --type: bogus/);
const badOutlineFlag = await runCliSubprocess(["outline", "--pattern=foo"], { cwd: tmpDir });
assert.equal(badOutlineFlag.code, 1);
assert.match(badOutlineFlag.stderr, /Unknown option '--pattern=foo' for 'quick_outline'/);
assert.match(badOutlineFlag.stderr, /Valid flags:/);
});
it("supports --query/-q and --paths aliases on --cli search, --file JSON input, and actionable Valid flags hints", async () => {
// 1. --query, -q, and --paths work cleanly on search
const aliasRes = await runCliSubprocess(
["search", "--paths=pkg-a", "--query=ONLY_IN_A", "--paths", "src", "-q", "multiply"],
{ cwd: tmpDir },
);
assert.equal(aliasRes.code, 0, aliasRes.stderr);
assert.match(aliasRes.stdout, /Query 1 \[text\] ONLY_IN_A \(in pkg-a\)/);
assert.match(aliasRes.stdout, /Query 2 \[text\] multiply \(in src\)/);
// 2. --file / --from-file reads JSON payload (including UTF-8 BOM + relative --cwd) and rejects directories / conflicting input flags
const editPayloadPath = path.join(tmpDir, "edit-payload.json");
const targetFile = path.join(tmpDir, "cli-file-edit.ts");
await fs.writeFile(targetFile, "export const v = 1;\n", "utf8");
await fs.writeFile(
editPayloadPath,
`\uFEFF${JSON.stringify({
edits: [{ path: "cli-file-edit.ts", hunks: [{ target: "v = 1", replacement: "v = 2" }] }],
})}`,
"utf8",
);
const fileEditRes = await runCliSubprocess(
["edit", "--best-effort", "--file", editPayloadPath],
{ cwd: tmpDir },
);
assert.equal(fileEditRes.code, 0, fileEditRes.stderr);
assert.equal(await fs.readFile(targetFile, "utf8"), "export const v = 2;\n");
const fromFileSub = path.join(tmpDir, "from-file-sub");
await fs.mkdir(fromFileSub, { recursive: true });
await fs.writeFile(path.join(fromFileSub, "sub.ts"), "export const s = 1;\n", "utf8");
await fs.writeFile(
path.join(fromFileSub, "sub-edit.json"),
JSON.stringify({
edits: [{ path: "sub.ts", hunks: [{ target: "s = 1", replacement: "s = 2" }] }],
}),
"utf8",
);
const fromFileRes = await runCliSubprocess(
["edit", "--cwd", "from-file-sub", "--from-file=sub-edit.json"],
{ cwd: tmpDir },
);
assert.equal(fromFileRes.code, 0, fromFileRes.stderr);
assert.equal(await fs.readFile(path.join(fromFileSub, "sub.ts"), "utf8"), "export const s = 2;\n");
const dirFileRes = await runCliSubprocess(["edit", "--file", fromFileSub], { cwd: tmpDir });
assert.equal(dirFileRes.code, 1);
assert.match(dirFileRes.stderr, /not a regular file/);
if (process.platform !== "win32") {
const devNullRes = await runCliSubprocess(["edit", "--file", "/dev/null"], { cwd: tmpDir });
assert.equal(devNullRes.code, 1);
assert.match(devNullRes.stderr, /not a regular file/);
}
const conflictRes = await runCliSubprocess(
["edit", "--json", '{"edits":[]}', "--file", editPayloadPath],
{ cwd: tmpDir },
);
assert.equal(conflictRes.code, 1);
assert.match(conflictRes.stderr, /Cannot combine '--json', '--file'\/'--from-file', and '--stdin'/);
const conflictStdinRes = await runCliSubprocess(
["edit", "--stdin", "--from-file", editPayloadPath],
{ cwd: tmpDir, stdin: '{"edits":[]}' },
);
assert.equal(conflictStdinRes.code, 1);
assert.match(conflictStdinRes.stderr, /Cannot combine '--json', '--file'\/'--from-file', and '--stdin'/);
// 3. Unknown search flag surfaces valid search flags in 1-line hint
const badSearchFlag = await runCliSubprocess(
["search", "--symbol", "add"],
{ cwd: tmpDir },
);
assert.equal(badSearchFlag.code, 1);
assert.match(badSearchFlag.stderr, /Unknown option '--symbol' for 'batch_search'/);
assert.match(badSearchFlag.stderr, /Valid flags:.*--text.*--query.*--path.*--paths.*--ignore-case/);
// 4. Duplicate --file / --from-file and JSON + shorthand/positional mixing fail fast
const dupFileRes = await runCliSubprocess(
["edit", "--file", editPayloadPath, "--from-file", editPayloadPath],
{ cwd: tmpDir },
);
assert.equal(dupFileRes.code, 1);
assert.match(dupFileRes.stderr, /Cannot pass '--file'\/'--from-file' more than once/);
const mixedShorthandRes = await runCliSubprocess(
["search", "--file", editPayloadPath, "--text", "extra"],
{ cwd: tmpDir },
);
assert.equal(mixedShorthandRes.code, 1);
assert.match(mixedShorthandRes.stderr, /Cannot combine JSON input/);
// 5. UTF-16 LE BOM file returns actionable UTF-8 guidance
const utf16Path = path.join(tmpDir, "utf16-payload.json");
await fs.writeFile(
utf16Path,
Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from('{"edits":[]}', "utf16le")]),
);
const utf16Res = await runCliSubprocess(["edit", "--file", utf16Path], { cwd: tmpDir });
assert.equal(utf16Res.code, 1);
assert.match(utf16Res.stderr, /encoded as UTF-16 LE/);
// 6. In-process relative --cwd resolves both --from-file (including fallback to defaultCwd) and effectiveCwd consistently, and empty jsonInput.cwd does not override defaultCwd
const inProcSub = path.join(tmpDir, "inproc-sub");
await fs.mkdir(inProcSub, { recursive: true });
const inProcTarget = path.join(inProcSub, "inproc.ts");
await fs.writeFile(inProcTarget, "export const ip = 1;\n", "utf8");
const rootRelPayload = path.join(tmpDir, "root-rel-edit.json");
await fs.writeFile(
rootRelPayload,
JSON.stringify({
cwd: "",
edits: [{ path: "inproc.ts", hunks: [{ target: "ip = 1", replacement: "ip = 2" }] }],
}),
"utf8",
);
const outStream = new PassThrough();
const errStream = new PassThrough();
const stdinStream = Object.assign(new PassThrough(), { isTTY: true });
let errText = "";
errStream.on("data", (d) => (errText += d.toString("utf8")));
const code = await runAgyFastCli(
["edit", "--cwd", "inproc-sub", "--from-file", "root-rel-edit.json"],
{ cwd: tmpDir, stdin: stdinStream, stdout: outStream, stderr: errStream },
);
assert.equal(code, 0, errText);
assert.equal(await fs.readFile(inProcTarget, "utf8"), "export const ip = 2;\n");
});
it("reads --from-file paths with spaces and non-ASCII characters and preserves multiline edit text", async () => {
const project = path.join(tmpDir, "工作 目录");
const inputDir = path.join(project, "输入 文件");
const target = path.join(project, "编辑 目标.txt");
const payload = path.join(inputDir, "批量 编辑.json");
const replacement = '中文第一行\n"quoted" C:\\目录';
await fs.mkdir(inputDir, { recursive: true });
await fs.writeFile(target, "原文\n", "utf8");
await fs.writeFile(payload, JSON.stringify({
edits: [{ path: "编辑 目标.txt", hunks: [{ target: "原文", replacement }] }],
}), "utf8");
const result = await runCliSubprocess(
["edit", "--cwd", "工作 目录", "--from-file", path.relative(project, payload)],
{ cwd: tmpDir },
);
assert.equal(result.code, 0, result.stderr);
assert.equal(await fs.readFile(target, "utf8"), replacement + "\n");
});
it("accepts a 2 MiB UTF-8 payload and rejects an extra byte before editing", async () => {
const limit = 2 * 1024 * 1024;
const target = path.join(tmpDir, "file-limit.txt");
const payload = path.join(tmpDir, "file-limit.json");
const original = "原文\n";
const body = Buffer.from(JSON.stringify({
edits: [{ path: "file-limit.txt", hunks: [{ target: "原文", replacement: "已编辑" }] }],
}), "utf8");
await fs.writeFile(target, original, "utf8");
await fs.writeFile(payload, Buffer.concat([body, Buffer.alloc(limit - body.length, 0x20)]));
const exact = await runCliSubprocess(["edit", `--from-file=${payload}`], { cwd: tmpDir });
assert.equal(exact.code, 0, exact.stderr);
assert.equal(await fs.readFile(target, "utf8"), "已编辑\n");
await fs.writeFile(target, original, "utf8");
await fs.appendFile(payload, " ");
const above = await runCliSubprocess(["edit", "--from-file", payload], { cwd: tmpDir });
assert.equal(above.code, 1);
assert.match(above.stderr, /exceeds 2097152 bytes/);
assert.equal(await fs.readFile(target, "utf8"), original);
});
it("emits actionable misuse hints for MCP-vs-CLI flag mix-ups (including --flag=value)", async () => {
const spaceCases: Array<[string[], RegExp]> = [
[["slice", "--lineRanges", "src/math.ts:1-2"], /Pass ranges as positional args instead/],
[["slice", "--outlines", "src/math.ts"], /Use --outline <file> \(repeatable\) instead of --outlines/],
[["outline", "--paths", "src/math.ts"], /Pass file paths as positional args instead/],
[["edit", "--edits", "[]"], /Pass edits via UTF-8 JSON using --from-file/],
[["check", "--commands", "echo ok"], /Pass commands as positional args instead/],
[["search", "--queries", "[]"], /Pass queries via --text\/--regex\/--glob\/--fuzzy\/--git flags/],
];
for (const [args, pattern] of spaceCases) {
const res = await runCliSubprocess(args, { cwd: tmpDir });
assert.equal(res.code, 1, `expected exit 1 for ${args.join(" ")}`);
assert.match(res.stderr, pattern);
}
const equalsCases: Array<[string[], RegExp]> = [
[["slice", "--lineRanges=src/math.ts:1-2"], /Pass ranges as positional args instead/],
[["slice", "--outlines=src/math.ts"], /Use --outline <file> \(repeatable\) instead of --outlines/],
[["outline", "--files=src/math.ts"], /Pass file paths as positional args instead/],
[["outline", "--PATH=src/math.ts"], /Pass file paths as positional args instead/],
[["edit", "--edits=[]"], /Pass edits via UTF-8 JSON using --from-file/],
[["check", "--commands=echo ok"], /Pass commands as positional args instead/],
[["search", "--queries=[]"], /Pass queries via --text\/--regex\/--glob\/--fuzzy\/--git flags/],
];
for (const [args, pattern] of equalsCases) {
const res = await runCliSubprocess(args, { cwd: tmpDir });
assert.equal(res.code, 1, `expected exit 1 for ${args.join(" ")}`);
assert.match(res.stderr, pattern);
}
const unknownSlice = await runCliSubprocess(["slice", "--definitely-unknown"], { cwd: tmpDir });
assert.equal(unknownSlice.code, 1);
assert.match(unknownSlice.stderr, /Unknown option '--definitely-unknown' for 'read_slices'\. Valid flags:/);
assert.doesNotMatch(unknownSlice.stderr, /Pass .* instead|Use --outline/);
});
});