Files

623 lines
22 KiB
TypeScript

import { mkdir, readFile, readdir, symlink, writeFile } from "node:fs/promises";
import { dirname, join, resolve } from "node:path";
import { expect } from "vitest";
import { ownedPackageCommand } from "../../fixtures/ownedPackageRegistration.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
import { setupCliInHome } from "./setupCliInHome.js";
const setup = ["setup", "--client", "claude_code", "--skill=false", "--json"];
cliTest(
"setup preserves a replacement foreign registration and its original backup",
async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-ownership-");
const config = join(home, ".claude.json");
const original = '{"mcpServers":{"peer":{"command":"peer"}}}\n';
await writeFile(config, original);
const run = setupCliInHome(cli, home, "claude");
expect((await run([...setup, "--yes"])).exitCode).toBe(0);
const replacement =
'{"mcpServers":{"rea":{"command":"peer-owned-server","args":["mcp"]},"peer":{"command":"peer-edited"}},"theme":"edited"}\n';
await writeFile(config, replacement);
for (const mode of ["--dry-run", "--yes"]) {
const result = await run([...setup, mode]);
expect(result.exitCode, result.stdout).toBe(1);
expect(result.json).toMatchObject({
status: "needs_human",
plannedActions: [],
appliedActions: [],
remediation: expect.stringContaining("foreign ownership"),
});
expect(await readFile(config, "utf8")).toBe(replacement);
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original);
expect(
(await readdir(home)).filter((name) => name.includes(".recovery")),
).toEqual([]);
}
expect((await run(["uninstall", "--json"])).exitCode).toBe(0);
expect(await readFile(config, "utf8")).toBe(replacement);
},
);
for (const [identity, entry] of [
["foreign", { command: "other-server", args: ["mcp"] }],
[
"unknown",
{
command: process.execPath,
args: ["/missing/old-rea/scripts/rea.mjs", "mcp"],
},
],
] as const)
cliTest(
`setup blocks ${identity} ownership before any backup or skill writes`,
async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-ownership-");
const config = join(home, ".claude.json");
const original = JSON.stringify({ mcpServers: { rea: entry } });
await writeFile(config, original);
const run = setupCliInHome(cli, home, "claude");
const result = await run([
"setup",
"--client",
"claude_code",
"--yes",
"--json",
]);
expect(result.exitCode, result.stdout).toBe(1);
expect(result.json).toMatchObject({
status: "needs_human",
plannedActions: [],
appliedActions: [],
remediation: expect.stringContaining(`${identity} ownership`),
});
expect(await readFile(config, "utf8")).toBe(original);
const doctor = await run([
"doctor",
"--client",
"claude_code",
"--skill=false",
"--json",
]);
expect(doctor.exitCode, doctor.stdout).toBe(1);
expect(doctor.json).toMatchObject({
identity: {
registrations: expect.arrayContaining([
expect.objectContaining({
client: "claude_code",
state: identity === "unknown" ? "unknown" : "stale",
remediation: expect.stringContaining("manually rename or remove"),
}),
]),
},
});
expect(await readdir(home)).not.toContain(".claude.json.rea.backup");
await expect(
readFile(
join(
home,
".claude",
"skills",
"reverse-engineer-anything",
"SKILL.md",
),
),
).rejects.toMatchObject({ code: "ENOENT" });
},
);
cliTest(
"setup refreshes a proven owned stale launcher and preserves peer settings",
async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-owned-refresh-");
const config = join(home, ".claude.json");
const original = JSON.stringify({
mcpServers: {
rea: {
command: "npx",
args: ["-y", "rea-agents@6.6.0", "mcp"],
env: { CUSTOM: "keep" },
},
peer: { command: "peer" },
},
});
await writeFile(config, original);
const run = setupCliInHome(cli, home, "claude");
const result = await run([...setup, "--yes"]);
expect(result.exitCode, result.stdout).toBe(0);
expect(JSON.parse(await readFile(config, "utf8"))).toMatchObject({
mcpServers: {
rea: { command: process.execPath, env: { CUSTOM: "keep" } },
peer: { command: "peer" },
},
});
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original);
},
);
/** Lay out a package with REA's CLI script shape and the given manifest. */
const installPackage = async (
root: string,
manifest: Readonly<Record<string, unknown>> | string,
): Promise<string> => {
const script = join(root, "scripts", "rea.mjs");
await mkdir(dirname(script), { recursive: true });
await writeFile(script, "");
await writeFile(
join(root, "package.json"),
typeof manifest === "string" ? manifest : JSON.stringify(manifest),
);
return script;
};
const reaManifest = { name: "rea-agents", bin: { rea: "scripts/rea.mjs" } };
cliTest(
"setup refreshes this installation's script under another runtime",
async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-other-runtime-");
const config = join(home, ".claude.json");
const runtime = join(home, "old-runtime", "node");
await mkdir(dirname(runtime), { recursive: true });
await writeFile(runtime, "");
// The CLI fixture runs this checkout's script, outside any node_modules.
const original = JSON.stringify({
mcpServers: {
rea: { command: runtime, args: [resolve("scripts/rea.mjs"), "mcp"] },
},
});
await writeFile(config, original);
const run = setupCliInHome(cli, home, "claude");
const result = await run([...setup, "--yes"]);
expect(result.exitCode, result.stdout).toBe(0);
expect(JSON.parse(await readFile(config, "utf8"))).toMatchObject({
mcpServers: { rea: { command: process.execPath } },
});
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original);
},
);
cliTest(
"setup refreshes a verified REA package script when its Node path is gone",
async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-missing-runtime-");
const config = join(home, ".claude.json");
const script = await installPackage(
join(home, "npm-cache", "_npx", "af752b90", "node_modules", "rea-agents"),
reaManifest,
);
const runtime = join(home, "old-runtime", "node");
const original = JSON.stringify({
mcpServers: {
rea: {
command: runtime,
args: [script, "mcp"],
env: { CUSTOM: "keep" },
},
peer: { command: "peer" },
},
});
await writeFile(config, original);
const run = setupCliInHome(cli, home, "claude");
const result = await run([...setup, "--yes"]);
expect(result.exitCode, result.stdout).toBe(0);
expect(JSON.parse(await readFile(config, "utf8"))).toMatchObject({
mcpServers: {
rea: { command: process.execPath, env: { CUSTOM: "keep" } },
peer: { command: "peer" },
},
});
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original);
},
);
for (const runtime of ["current", "other"] as const)
cliTest(
`setup refreshes a launcher of another installed REA package (${runtime} runtime)`,
async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-other-install-");
const config = join(home, ".claude.json");
const script = await installPackage(
join(
home,
"npm-cache",
"_npx",
"af752b90",
"node_modules",
"rea-agents",
),
reaManifest,
);
let command = process.execPath;
if (runtime === "other") {
command = join(home, "old-runtime", "node");
await mkdir(dirname(command), { recursive: true });
await writeFile(command, "");
}
const original = JSON.stringify({
mcpServers: {
rea: { command, args: [script, "mcp"], env: { CUSTOM: "keep" } },
peer: { command: "peer" },
},
});
await writeFile(config, original);
const run = setupCliInHome(cli, home, "claude");
const doctor = await run([
"doctor",
"--client",
"claude_code",
"--skill=false",
"--json",
]);
expect(doctor.json).toMatchObject({
identity: {
registrations: expect.arrayContaining([
expect.objectContaining({
client: "claude_code",
command_identity: "owned",
state: "stale",
remediation: expect.stringContaining(
"another installed rea-agents package or runtime",
),
}),
]),
},
});
const result = await run([...setup, "--yes"]);
expect(result.exitCode, result.stdout).toBe(0);
const refreshed = JSON.parse(await readFile(config, "utf8"));
expect(refreshed).toMatchObject({
mcpServers: {
rea: { command: process.execPath, env: { CUSTOM: "keep" } },
peer: { command: "peer" },
},
});
expect(refreshed.mcpServers.rea.args).not.toContain(script);
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original);
},
);
for (const [label, root, manifest, identity] of [
[
"another package name",
["node_modules", "rea-agents"],
{ ...reaManifest, name: "other-agents" },
"foreign",
],
["a source checkout", ["checkout", "rea-agents"], reaManifest, "foreign"],
[
"a different CLI script",
["node_modules", "rea-agents"],
{ ...reaManifest, bin: { rea: "scripts/other.mjs" } },
"foreign",
],
[
"an unparseable REA manifest",
["node_modules", "rea-agents"],
'{"name":"rea-agents",',
"unknown",
],
] as const)
cliTest(`setup keeps a launcher of ${label} ${identity}`, async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-unproven-install-");
const config = join(home, ".claude.json");
const script = await installPackage(join(home, ...root), manifest);
const original = JSON.stringify({
mcpServers: { rea: { command: process.execPath, args: [script, "mcp"] } },
});
await writeFile(config, original);
const run = setupCliInHome(cli, home, "claude");
const result = await run([...setup, "--yes"]);
expect(result.exitCode, result.stdout).toBe(1);
expect(result.json).toMatchObject({
status: "needs_human",
remediation: expect.stringContaining(`${identity} ownership`),
});
expect(await readFile(config, "utf8")).toBe(original);
});
for (const [runtime, launcher] of [
["removed", ["rea-agents", "scripts", "rea.mjs"]],
["current", ["rea-agents", "scripts", "rea.mjs"]],
["removed", [".bin", "rea"]],
] as const)
cliTest(
`setup refreshes a launcher from a removed npx cache entry (${runtime} runtime, ${launcher.join("/")})`,
async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-removed-npx-");
const config = join(home, ".claude.json");
// npm deletes the whole cache entry, and a version manager may remove the runtime.
const command =
runtime === "current"
? process.execPath
: join(home, "removed-runtime", "node");
const script = join(
home,
"npm-cache",
"_npx",
"af752b90495482d7",
"node_modules",
...launcher,
);
const original = JSON.stringify({
mcpServers: {
rea: { command, args: [script, "mcp"], env: { CUSTOM: "keep" } },
peer: { command: "peer" },
},
});
await writeFile(config, original);
const run = setupCliInHome(cli, home, "claude");
const doctor = await run([
"doctor",
"--client",
"claude_code",
"--skill=false",
"--json",
]);
expect(doctor.json).toMatchObject({
identity: {
registrations: expect.arrayContaining([
expect.objectContaining({
client: "claude_code",
command_identity: "owned",
state: "stale",
remediation: expect.stringContaining(
"npx cache entry that npm has removed. Run npx rea-agents@latest setup",
),
}),
]),
},
});
const result = await run([...setup, "--yes"]);
expect(result.exitCode, result.stdout).toBe(0);
const refreshed = JSON.parse(await readFile(config, "utf8"));
expect(refreshed).toMatchObject({
mcpServers: {
rea: { command: process.execPath, env: { CUSTOM: "keep" } },
peer: { command: "peer" },
},
});
expect(refreshed.mcpServers.rea.args).not.toContain(script);
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original);
},
);
// Only setup's own npx cache layout is accepted without a manifest.
for (const [label, segments] of [
[
"a removed global install",
["lib", "node_modules", "rea-agents", "scripts", "rea.mjs"],
],
["a non-npx cache key", ["_npx", "af752b90", "node_modules", ".bin", "rea"]],
[
"another package in npx",
[
"_npx",
"af752b90495482d7",
"node_modules",
"other-agents",
"scripts",
"rea.mjs",
],
],
[
"another bin in npx",
["_npx", "af752b90495482d7", "node_modules", ".bin", "other"],
],
] as const)
cliTest(
`setup leaves a removed launcher of ${label} unknown`,
async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-removed-install-");
const config = join(home, ".claude.json");
const runtime = join(home, "old-runtime", "node");
await mkdir(dirname(runtime), { recursive: true });
await writeFile(runtime, "");
const script = join(home, "npm-cache", ...segments);
const original = JSON.stringify({
mcpServers: { rea: { command: runtime, args: [script, "mcp"] } },
});
await writeFile(config, original);
const run = setupCliInHome(cli, home, "claude");
const result = await run([...setup, "--yes"]);
expect(result.exitCode, result.stdout).toBe(1);
expect(result.json).toMatchObject({
status: "needs_human",
remediation: expect.stringContaining("unknown ownership"),
});
expect(await readFile(config, "utf8")).toBe(original);
},
);
cliTest.skipIf(process.platform === "win32")(
"setup leaves a launcher unknown when Node and native traversal select different scripts",
async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-divergent-install-");
const config = join(home, ".claude.json");
const root = join(home, "pkg", "node_modules", "rea-agents");
await installPackage(root, reaManifest);
await mkdir(join(root, "lib"));
// Native traversal follows the link before `..` into the REA package;
// Node removes `..` first and runs the unrelated sibling script.
await installPackage(join(home, "foreign"), { name: "foreign" });
await symlink(join(root, "lib"), join(home, "foreign", "link"), "dir");
const runtime = join(home, "old-runtime", "node");
await mkdir(dirname(runtime), { recursive: true });
await writeFile(runtime, "");
const original = JSON.stringify({
mcpServers: {
rea: {
command: runtime,
args: [`${home}/foreign/link/../scripts/rea.mjs`, "mcp"],
},
},
});
await writeFile(config, original);
const run = setupCliInHome(cli, home, "claude");
const result = await run([...setup, "--yes"]);
expect(result.exitCode, result.stdout).toBe(1);
expect(result.json).toMatchObject({
status: "needs_human",
remediation: expect.stringContaining("unknown ownership"),
});
expect(await readFile(config, "utf8")).toBe(original);
},
);
for (const repeated of [false, true])
cliTest(
`OpenCode setup preserves foreign legacy fallback (shadowed=${repeated})`,
async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-legacy-ownership-");
const config = join(home, ".config", "opencode", "opencode.json");
await mkdir(dirname(config), { recursive: true });
const foreign = '"rea":{"type":"local","command":["other"]}';
const owned = `"rea":${JSON.stringify({ type: "local", command: ownedPackageCommand })}`;
const original = `{"mcp":{${foreign},${repeated ? `${owned},` : ""}"servers":{${owned}}}}`;
await writeFile(config, original);
const run = setupCliInHome(cli, home, "xdg");
const result = await run([
"setup",
"--client",
"opencode",
"--skill=false",
"--yes",
"--json",
]);
expect(result.exitCode, result.stdout).toBe(1);
expect(result.json).toMatchObject({
status: "needs_human",
remediation: expect.stringContaining("foreign ownership"),
});
expect(await readFile(config, "utf8")).toBe(original);
await expect(readFile(`${config}.rea.backup`)).rejects.toMatchObject({
code: "ENOENT",
});
},
);
cliTest(
"ZCode setup preserves an unrelated fallback registration before its first native entry",
async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-zcode-ownership-");
const fallback = join(home, ".agents", "mcp.json");
await mkdir(dirname(fallback), { recursive: true });
const original =
'{"mcpServers":{"rea":{"command":"other"},"peer":{"command":"peer"}}}';
await writeFile(fallback, original);
const run = setupCliInHome(cli, home);
const result = await run([
"setup",
"--client",
"zcode",
"--skill=false",
"--yes",
"--json",
]);
expect(result.exitCode, result.stdout).toBe(1);
expect(result.json).toMatchObject({
status: "needs_human",
plannedActions: [],
remediation: expect.stringContaining(fallback),
});
expect(await readFile(fallback, "utf8")).toBe(original);
await expect(
readFile(join(home, ".zcode", "cli", "config.json")),
).rejects.toMatchObject({ code: "ENOENT" });
await expect(readFile(`${fallback}.rea.backup`)).rejects.toMatchObject({
code: "ENOENT",
});
},
);
cliTest(
"OpenCode setup preserves an inherited foreign registration instead of shadowing it",
async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-layered-ownership-");
const directory = join(home, ".config", "opencode");
await mkdir(directory, { recursive: true });
const lower = join(directory, "config.json");
const upper = join(directory, "opencode.json");
const originalLower =
'{"mcp":{"rea":{"type":"local","command":["other"]}}}';
const originalUpper = "{}";
await writeFile(lower, originalLower);
await writeFile(upper, originalUpper);
const run = setupCliInHome(cli, home, "xdg");
const result = await run([
"setup",
"--client",
"opencode",
"--skill=false",
"--yes",
"--json",
]);
expect(result.exitCode, result.stdout).toBe(1);
expect(result.json).toMatchObject({
status: "needs_human",
plannedActions: [],
remediation: expect.stringContaining("foreign ownership"),
});
expect(await readFile(lower, "utf8")).toBe(originalLower);
expect(await readFile(upper, "utf8")).toBe(originalUpper);
await expect(readFile(`${upper}.rea.backup`)).rejects.toMatchObject({
code: "ENOENT",
});
},
);
cliTest(
"OpenCode setup refreshes an owned inherited launcher through a policy fragment",
async ({ cli }) => {
const home = await createTestTempDirectory("rea-setup-owned-layer-");
const directory = join(home, ".config", "opencode");
await mkdir(directory, { recursive: true });
const lower = join(directory, "config.json");
const upper = join(directory, "opencode.json");
const originalLower = JSON.stringify({
mcp: { rea: { type: "local", command: ownedPackageCommand } },
});
const originalUpper = JSON.stringify({
mcp: { rea: { environment: { CUSTOM: "keep" } } },
});
await writeFile(lower, originalLower);
await writeFile(upper, originalUpper);
const run = setupCliInHome(cli, home, "xdg");
const result = await run([
"setup",
"--client",
"opencode",
"--skill=false",
"--yes",
"--json",
]);
expect(result.exitCode, result.stdout).toBe(0);
expect(await readFile(lower, "utf8")).toBe(originalLower);
expect(await readFile(`${upper}.rea.backup`, "utf8")).toBe(originalUpper);
expect(JSON.parse(await readFile(upper, "utf8"))).toMatchObject({
mcp: { rea: { type: "local", environment: { CUSTOM: "keep" } } },
});
const foreignFragment = JSON.stringify({
mcp: { rea: { command: ["other", "mcp"] } },
});
await writeFile(upper, foreignFragment);
const blocked = await run([
"setup",
"--client",
"opencode",
"--skill=false",
"--yes",
"--json",
]);
expect(blocked.exitCode, blocked.stdout).toBe(1);
expect(blocked.json).toMatchObject({
status: "needs_human",
remediation: expect.stringContaining("foreign ownership"),
});
expect(await readFile(upper, "utf8")).toBe(foreignFragment);
expect(await readFile(`${upper}.rea.backup`, "utf8")).toBe(originalUpper);
},
);