Files
reverse-engineering-anything/tests/boundary/cli/mcpAddClientPolicy.test.ts
T
Shubham Bhamareandmorluto 43f7680c1f fix(setup): support Antigravity workspace .agents/mcp_config.json (#15125)
* fix(setup): support Antigravity workspace .agents/mcp_config.json

* fix(setup): verify Antigravity project writes and repository lookup

* fix(setup): confine project registration to the selected workspace

* fix(mcp): enforce write scope for every project registration

* test(setup): use existing project root in Kilo layer conflict test

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-29 20:39:47 +00:00

355 lines
11 KiB
TypeScript

import {
access,
mkdir,
readFile,
readlink,
symlink,
unlink,
writeFile,
} from "node:fs/promises";
import { dirname, join } from "node:path";
import { expect } from "vitest";
import { mcpRegistrationReportSchema } from "../../../src/contracts/mcpRegistration.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest, type TestCli } from "../../support/cli/cliFixture.js";
import { registrationEnvironment } from "../../support/cli/registrationEnvironment.js";
cliTest(
"mcp add reports each selected client's own refusal without writing settings or backups",
async ({ cli }) => {
const home = await createTestTempDirectory("rea-mcp-client-policy-");
const system = join(home, "system", "settings.json");
const clients = [
{
client: "gemini-cli",
path: join(home, ".gemini", "settings.json"),
original: '{"theme":"dark"}\n',
reason: "Gemini mcp.excluded blocks rea",
},
{
client: "opencode",
path: join(home, ".config", "opencode", "opencode.json"),
original:
'{"mcp":{"rea":{"type":"local","command":["rea","mcp"],"environment":{"EXAMPLE_SETTING":17}}}}\n',
reason: "environment.EXAMPLE_SETTING",
},
];
await mkdir(dirname(system), { recursive: true });
await writeFile(system, '{"mcp":{"excluded":["rea"]}}\n');
for (const { path, original } of clients) {
await mkdir(dirname(path), { recursive: true });
await writeFile(path, original);
}
const environment = await registrationEnvironment(home, {
GEMINI_CLI_SYSTEM_SETTINGS_PATH: system,
GEMINI_CLI_SYSTEM_DEFAULTS_PATH: join(home, "system", "defaults.json"),
GEMINI_CLI_TRUSTED_FOLDERS_PATH: join(home, "trusted.json"),
});
const result = await cli.run({
arguments: [
"mcp",
"add",
"--agent",
"gemini",
"--agent",
"opencode",
"--command",
"/replacement mcp",
"--json",
],
cwd: home,
environment,
});
expect(result.exitCode, result.stderr).toBe(1);
expect(mcpRegistrationReportSchema.parse(result.json)).toMatchObject({
ok: false,
clients: clients.map(({ client, path, reason }) => ({
client,
config_path: path,
status: "failed",
error: { reason: expect.stringContaining(reason) },
})),
});
for (const { path, original } of clients) {
expect(await readFile(path, "utf8"), path).toBe(original);
await expect(access(`${path}.rea.backup`)).rejects.toMatchObject({
code: "ENOENT",
});
}
},
);
cliTest(
"project OpenCode registration verifies the launcher in native load order before publishing",
async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-mcp-opencode-project-policy-",
);
const project = join(root, "project");
const home = join(root, "home");
await mkdir(join(project, ".git"), { recursive: true });
await mkdir(join(project, ".opencode"));
const environment = await registrationEnvironment(home, {});
const selected = join(project, "opencode.jsonc");
const higher = join(project, ".opencode", "opencode.json");
const local = (command: string) =>
JSON.stringify({
mcp: { servers: { rea: { type: "local", command: [command, "mcp"] } } },
});
const original = local("previous-direct-launcher");
const overriding = local("nested-launcher");
await writeFile(selected, original);
await writeFile(higher, overriding);
const blocked = await addOpenCodeProject(cli, project, environment);
expect(blocked.exitCode).toBe(1);
expect(mcpRegistrationReportSchema.parse(blocked.json)).toMatchObject({
ok: false,
clients: [
{
config_path: selected,
scope: "project",
status: "failed",
error: { reason: expect.stringContaining(higher) },
},
],
});
expect(await readFile(selected, "utf8")).toBe(original);
expect(await readFile(higher, "utf8")).toBe(overriding);
for (const path of [selected, higher])
await expect(access(`${path}.rea.backup`)).rejects.toMatchObject({
code: "ENOENT",
});
await unlink(higher);
expect((await addOpenCodeProject(cli, project, environment)).exitCode).toBe(
0,
);
expect(JSON.parse(await readFile(selected, "utf8"))).toMatchObject({
mcp: { servers: { rea: { command: ["rea", "mcp"] } } },
});
expect(await readFile(`${selected}.rea.backup`, "utf8")).toBe(original);
const fresh = join(root, "new-project");
await mkdir(fresh);
expect((await addOpenCodeProject(cli, fresh, environment)).exitCode).toBe(
0,
);
expect(
JSON.parse(await readFile(join(fresh, "opencode.jsonc"), "utf8")),
).toMatchObject({
mcp: { rea: { command: ["rea", "mcp"] } },
});
},
);
cliTest.skipIf(process.platform === "win32")(
"project OpenCode registration cannot create a user settings file through an alias",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-mcp-opencode-user-alias-");
const project = join(root, "project");
const parentAlias = join(root, "project-alias");
const fileAlias = join(root, "user-override.jsonc");
await mkdir(project);
const selected = join(project, "opencode.jsonc");
await symlink(project, parentAlias);
await symlink(selected, fileAlias);
for (const userPath of [join(parentAlias, "opencode.jsonc"), fileAlias]) {
const result = await addOpenCodeProject(
cli,
project,
await registrationEnvironment(join(root, "home"), {
OPENCODE_CONFIG: userPath,
}),
);
expect(result.exitCode, userPath).toBe(1);
for (const path of [selected, userPath, `${selected}.rea.backup`])
await expect(access(path), path).rejects.toMatchObject({
code: "ENOENT",
});
expect(mcpRegistrationReportSchema.parse(result.json)).toMatchObject({
ok: false,
clients: [
{
config_path: selected,
scope: "project",
status: "failed",
error: { reason: expect.stringContaining(userPath) },
},
],
});
}
expect(await readlink(fileAlias)).toBe(selected);
},
);
cliTest.skipIf(process.platform === "win32")(
"project OpenCode registration replays a proposed edit through a higher alias of the same file",
async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-mcp-opencode-project-alias-",
);
const project = join(root, "project");
const selected = join(project, "opencode.jsonc");
const higher = join(project, ".opencode", "opencode.json");
const original =
'{"mcp":{"servers":{"rea":{"type":"local","command":["previous-direct-launcher","mcp"]}}}}';
await mkdir(join(project, ".git"), { recursive: true });
await mkdir(dirname(higher));
await writeFile(selected, original);
await symlink(selected, higher);
const result = await addOpenCodeProject(
cli,
project,
await registrationEnvironment(join(root, "home"), {}),
);
expect(result.exitCode, result.stderr).toBe(0);
expect(mcpRegistrationReportSchema.parse(result.json)).toMatchObject({
ok: true,
clients: [
{ config_path: selected, scope: "project", status: "configured" },
],
});
expect(await readlink(higher)).toBe(selected);
expect(JSON.parse(await readFile(higher, "utf8"))).toMatchObject({
mcp: { servers: { rea: { command: ["rea", "mcp"] } } },
});
expect(await readFile(`${selected}.rea.backup`, "utf8")).toBe(original);
},
);
cliTest(
"Pi project registration keeps the selected user profile read-only",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-pi-project-policy-");
const home = join(root, "home");
const project = join(root, "project");
const profile = join(project, ".pi");
const config = join(profile, "mcp.json");
const original = '{"mcpServers":{"peer":{"command":"peer"}}}';
await mkdir(profile, { recursive: true });
await writeFile(config, original);
const register = (environment: NodeJS.ProcessEnv) =>
cli.run({
arguments: [
"mcp",
"add",
"--no-global",
"--agent",
"pi",
"--command",
"rea mcp",
"--json",
],
cwd: project,
environment,
});
const blocked = await register(
await registrationEnvironment(home, { PI_CODING_AGENT_DIR: profile }),
);
expect(blocked.exitCode, blocked.stderr).toBe(1);
expect(mcpRegistrationReportSchema.parse(blocked.json)).toMatchObject({
ok: false,
clients: [
{
config_path: config,
scope: "project",
status: "failed",
error: {
reason: expect.stringContaining("aliases the user configuration"),
},
},
],
});
expect(await readFile(config, "utf8")).toBe(original);
await expect(access(`${config}.rea.backup`)).rejects.toMatchObject({
code: "ENOENT",
});
const admitted = await register(await registrationEnvironment(home));
expect(admitted.exitCode, admitted.stderr).toBe(0);
expect(JSON.parse(await readFile(config, "utf8"))).toMatchObject({
mcpServers: {
peer: { command: "peer" },
rea: { command: "rea", args: ["mcp"] },
},
});
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original);
},
);
const addOpenCodeProject = (
cli: TestCli,
cwd: string,
environment: NodeJS.ProcessEnv,
) =>
cli.run({
arguments: [
"mcp",
"add",
"--no-global",
"--agent",
"opencode",
"--command",
"rea mcp",
"--json",
],
cwd,
environment,
});
cliTest.for(["global", "project"] as const)(
"mcp add respects the Mastra disable flag for the %s registration",
async (scope, { cli }) => {
const home = await createTestTempDirectory("rea-mastra-mcp-flag-");
const project = join(home, "project");
await mkdir(project);
const path = join(
scope === "global" ? home : project,
".mastracode",
"mcp.json",
);
const command = [
"mcp",
"add",
"--agent",
"mastra",
"--command",
"rea mcp",
"--json",
...(scope === "global" ? [] : ["--no-global"]),
];
const blocked = await cli.run({
arguments: command,
cwd: project,
environment: await registrationEnvironment(home, {
MASTRACODE_DISABLE_MCP: " YES ",
}),
});
expect(blocked.exitCode, blocked.stdout).toBe(1);
expect(mcpRegistrationReportSchema.parse(blocked.json)).toMatchObject({
clients: [
{
config_path: path,
status: "failed",
error: { reason: expect.stringContaining("MASTRACODE_DISABLE_MCP") },
},
],
});
await expect(access(path)).rejects.toMatchObject({ code: "ENOENT" });
const enabled = await cli.run({
arguments: command,
cwd: project,
environment: await registrationEnvironment(home, {
MASTRACODE_DISABLE_MCP: "false",
}),
});
expect(enabled.exitCode, enabled.stdout).toBe(0);
expect(JSON.parse(await readFile(path, "utf8"))).toMatchObject({
mcpServers: { rea: { command: "rea", args: ["mcp"] } },
});
},
);