Files
reverse-engineering-anything/tests/boundary/cli/projectUninstall.test.ts
T

588 lines
21 KiB
TypeScript

import { mkdir, readFile, symlink, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { expect } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
import { registrationEnvironment } from "../../support/cli/registrationEnvironment.js";
const put = async (
path: string,
document: Record<string, unknown> | string,
) => {
await mkdir(dirname(path), { recursive: true });
const text =
typeof document === "string" ? document : JSON.stringify(document);
await writeFile(path, text);
return text;
};
const piRegistration = {
command: "npx",
args: ["-y", "rea-agents", "mcp"],
enabled: false,
};
const openCodeRegistration = {
type: "local",
command: ["npx", "-y", "rea-agents", "mcp"],
};
const openCodePeer = { type: "local", command: ["unrelated", "mcp"] };
const openCodeRemote = { type: "remote", url: "https://example.invalid/mcp" };
for (const distinctAlias of [false, true]) {
cliTest.skipIf(distinctAlias && process.platform === "win32")(
distinctAlias
? "project uninstall still refuses distinct paths to the same configuration file"
: "project uninstall edits a configuration in a selected .opencode directory once",
async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-project-path-occurrences-",
);
const project = join(root, ".opencode");
const path = join(project, "opencode.json");
const alias = join(project, "opencode.jsonc");
const original = await put(path, {
mcp: { servers: { rea: openCodeRegistration, peer: openCodePeer } },
});
if (distinctAlias) await symlink(path, alias);
const result = await cli.run({
arguments: ["uninstall", "--project", project, "--json"],
environment: await registrationEnvironment(join(root, "home")),
cwd: root,
});
if (distinctAlias) {
expect(result.exitCode, result.stdout).toBe(1);
expect(result.json).toMatchObject({
status: "failed",
items: expect.arrayContaining([
expect.objectContaining({
name: "opencode",
detail: expect.stringContaining(
"Multiple selected configuration paths alias",
),
}),
]),
});
expect(await readFile(path, "utf8")).toBe(original);
await expect(readFile(`${path}.rea.backup`)).rejects.toMatchObject({
code: "ENOENT",
});
return;
}
expect(result.exitCode, result.stdout).toBe(0);
expect(result.json).toMatchObject({
status: "complete",
items: expect.arrayContaining([
expect.objectContaining({
name: "opencode",
status: "removed",
configuration_path: path,
}),
]),
});
expect(JSON.parse(await readFile(path, "utf8"))).toEqual({
mcp: { servers: { peer: openCodePeer } },
});
expect(await readFile(`${path}.rea.backup`, "utf8")).toBe(original);
},
);
}
cliTest(
"uninstall selects an explicit project, including disabled owned rows, while leaving ancestor and user files intact",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-project-uninstall-");
const home = join(root, "home");
const parent = join(root, "workspace");
const project = join(parent, "selected");
const piPath = join(project, ".pi", "mcp.json");
const openCodePath = join(project, "opencode.jsonc");
const environment = await registrationEnvironment(home);
const piUser = join(home, ".pi", "agent", "mcp.json");
const userOriginal = await put(piUser, {
mcpServers: { peer: { command: "unrelated" } },
});
await put(join(home, ".pi", "agent", "trust.json"), { [project]: true });
const ancestor = join(parent, "opencode.json");
const ancestorOriginal = await put(ancestor, {
mcp: { servers: { ancestor: openCodePeer } },
});
const piOriginal = await put(piPath, {
mcpServers: { rea: piRegistration, peer: { command: "unrelated" } },
});
const openCodeOriginal = await put(openCodePath, {
mcp: { servers: { rea: openCodeRegistration, peer: openCodePeer } },
});
const run = (cwd: string, ...arguments_: string[]) =>
cli.run({
arguments: ["uninstall", ...arguments_, "--json"],
environment,
cwd,
});
const ordinary = await run(project);
expect(ordinary.exitCode, ordinary.stdout).toBe(0);
expect(await readFile(piPath, "utf8")).toBe(piOriginal);
expect(await readFile(openCodePath, "utf8")).toBe(openCodeOriginal);
const incompatible = await run(root, "--project", project, "--purge-data");
expect(incompatible.exitCode, incompatible.stdout).toBe(1);
expect(await readFile(piPath, "utf8")).toBe(piOriginal);
const selected = await run(root, "--project", project);
expect(selected.exitCode, selected.stdout).toBe(0);
expect(selected.json).toMatchObject({
status: "complete",
items: expect.arrayContaining([
expect.objectContaining({
name: "pi",
status: "removed",
configuration_path: piPath,
}),
expect.objectContaining({
name: "opencode",
status: "removed",
configuration_path: openCodePath,
}),
]),
});
expect(JSON.parse(await readFile(piPath, "utf8"))).toEqual({
mcpServers: { peer: { command: "unrelated" } },
});
expect(JSON.parse(await readFile(openCodePath, "utf8"))).toEqual({
mcp: { servers: { peer: openCodePeer } },
});
expect(await readFile(`${piPath}.rea.backup`, "utf8")).toBe(piOriginal);
expect(await readFile(`${openCodePath}.rea.backup`, "utf8")).toBe(
openCodeOriginal,
);
expect(await readFile(ancestor, "utf8")).toBe(ancestorOriginal);
expect(await readFile(piUser, "utf8")).toBe(userOriginal);
},
);
for (const { kind, retained } of [
{
kind: "foreign local",
retained: { type: "local", command: ["npx", "-y", "other-package", "mcp"] },
},
{ kind: "remote", retained: openCodeRemote },
]) {
cliTest(
`uninstall removes shadowed OpenCode and Pi registrations while retaining the effective ${kind} row`,
async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-project-shadowed-uninstall-",
);
const home = join(root, "home");
const project = join(root, "selected");
const lower = join(project, "opencode.json");
const higher = join(project, "opencode.jsonc");
const pi = join(project, ".pi", "mcp.json");
const environment = await registrationEnvironment(home);
await put(join(home, ".pi", "agent", "trust.json"), { [project]: true });
const lowerOriginal = await put(lower, {
mcp: { servers: { rea: openCodeRegistration, peer: openCodePeer } },
});
const higherOriginal = await put(higher, {
mcp: { servers: { rea: retained } },
});
const piOriginal = await put(pi, {
mcpServers: { rea: piRegistration, peer: { command: "unrelated" } },
});
const result = await cli.run({
arguments: ["uninstall", "--project", project, "--json"],
environment,
cwd: root,
});
expect(result.exitCode, result.stdout).toBe(0);
expect(result.json).toMatchObject({ status: "complete" });
expect(JSON.parse(await readFile(lower, "utf8"))).toEqual({
mcp: { servers: { peer: openCodePeer } },
});
expect(JSON.parse(await readFile(pi, "utf8"))).toEqual({
mcpServers: { peer: { command: "unrelated" } },
});
expect(await readFile(`${lower}.rea.backup`, "utf8")).toBe(lowerOriginal);
expect(await readFile(`${pi}.rea.backup`, "utf8")).toBe(piOriginal);
expect(await readFile(higher, "utf8")).toBe(higherOriginal);
await expect(readFile(`${higher}.rea.backup`)).rejects.toMatchObject({
code: "ENOENT",
});
},
);
}
cliTest(
"uninstall skips a project candidate beneath a regular file before reading unrelated user data",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-project-absent-uninstall-");
const home = join(root, "home");
const project = join(root, "selected");
const piUser = join(home, ".pi", "agent", "mcp.json");
const blocker = join(project, ".pi");
const openCode = join(project, "opencode.json");
const environment = await registrationEnvironment(home);
const userOriginal = await put(piUser, "{ malformed");
const blockerOriginal = await put(blocker, "ordinary file");
const original = await put(openCode, {
mcp: { servers: { rea: openCodeRegistration, peer: openCodePeer } },
});
const result = await cli.run({
arguments: ["uninstall", "--project", project, "--json"],
environment,
cwd: root,
});
expect(result.exitCode, result.stdout).toBe(0);
expect(result.json).toMatchObject({
status: "complete",
items: expect.arrayContaining([
expect.objectContaining({ name: "opencode", status: "removed" }),
]),
});
expect(result.json).not.toMatchObject({
items: expect.arrayContaining([expect.objectContaining({ name: "pi" })]),
});
expect(JSON.parse(await readFile(openCode, "utf8"))).toEqual({
mcp: { servers: { peer: openCodePeer } },
});
expect(await readFile(`${openCode}.rea.backup`, "utf8")).toBe(original);
expect(await readFile(piUser, "utf8")).toBe(userOriginal);
expect(await readFile(blocker, "utf8")).toBe(blockerOriginal);
},
);
for (const [scenario, trusted, fallback, status = "failed"] of [
["Pi trust is denied", false, undefined],
["a user fallback would become active", true, { command: "unrelated" }],
[
"the user fallback carries only a foreign disabled flag",
true,
{ command: "unrelated", disabled: true },
],
["a user fallback is malformed", true, "{ bad"],
[
"the user fallback stays disabled under Pi's enabled flag",
true,
{ command: "unrelated", enabled: false },
"removed",
],
] as const) {
cliTest(
`project uninstall reports ${status} when ${scenario}`,
async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-project-uninstall-fallback-",
);
const home = join(root, "home");
const project = join(root, "selected");
const environment = await registrationEnvironment(home);
await put(join(home, ".pi", "agent", "trust.json"), {
[project]: trusted,
});
const piUser = join(home, ".pi", "agent", "mcp.json");
const userOriginal =
fallback === undefined
? undefined
: await put(
piUser,
typeof fallback === "string"
? fallback
: { mcpServers: { rea: fallback } },
);
const piPath = join(project, ".pi", "mcp.json");
const original = await put(piPath, {
mcpServers: { rea: piRegistration },
});
const result = await cli.run({
arguments: ["uninstall", "--project", project, "--json"],
environment,
cwd: root,
});
expect(result.exitCode, result.stdout).toBe(status === "removed" ? 0 : 1);
expect(result.json).toMatchObject({
status: status === "removed" ? "complete" : "failed",
items: expect.arrayContaining([
expect.objectContaining({ name: "pi", status }),
]),
});
const updated = await readFile(piPath, "utf8");
if (status === "removed") {
expect(JSON.parse(updated)).toEqual({ mcpServers: {} });
expect(await readFile(`${piPath}.rea.backup`, "utf8")).toBe(original);
} else {
expect(updated).toBe(original);
await expect(readFile(`${piPath}.rea.backup`)).rejects.toMatchObject({
code: "ENOENT",
});
}
if (userOriginal !== undefined)
expect(await readFile(piUser, "utf8")).toBe(userOriginal);
},
);
}
for (const scope of ["outside", "user"] as const) {
cliTest.skipIf(process.platform === "win32")(
`project uninstall refuses a registration symlink to the ${scope} configuration`,
async ({ cli }) => {
const root = await createTestTempDirectory("rea-project-uninstall-link-");
const home = join(root, "home");
const project = join(root, "selected");
const external =
scope === "outside"
? join(root, "elsewhere", "opencode.json")
: join(home, ".config", "opencode", "opencode.json");
const environment = await registrationEnvironment(home);
const original = await put(external, {
mcp: { servers: { rea: openCodeRegistration } },
});
await mkdir(project);
const link = join(project, "opencode.json");
await symlink(external, link);
const result = await cli.run({
arguments: ["uninstall", "--project", project, "--json"],
environment,
cwd: root,
});
expect(result.exitCode, result.stdout).toBe(1);
expect(result.json).toMatchObject({
status: "failed",
items: expect.arrayContaining([
expect.objectContaining({
name: "opencode",
status: "failed",
detail: expect.stringContaining(
scope === "outside"
? "outside the project directory"
: "aliases user configuration",
),
}),
]),
});
expect(await readFile(external, "utf8")).toBe(original);
await expect(readFile(`${link}.rea.backup`)).rejects.toMatchObject({
code: "ENOENT",
});
},
);
}
for (const aliasedClient of ["opencode", "pi"] as const) {
cliTest(
`project uninstall ignores an unrelated ${aliasedClient} user alias`,
async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-project-uninstall-unrelated-alias-",
);
const home = join(root, "home");
const project = join(root, "selected");
const piDirectory = join(project, ".pi");
const piPath = join(piDirectory, "mcp.json");
const openCodePath = join(project, "opencode.json");
const alias = aliasedClient === "pi" ? piPath : openCodePath;
const selected = aliasedClient === "pi" ? openCodePath : piPath;
const environment = await registrationEnvironment(
home,
aliasedClient === "pi"
? { PI_CODING_AGENT_DIR: piDirectory }
: { OPENCODE_CONFIG: openCodePath },
);
const original = await put(
alias,
aliasedClient === "pi"
? { mcpServers: { rea: { command: "unrelated" } } }
: { mcp: { servers: { peer: openCodePeer } } },
);
const run = () =>
cli.run({
arguments: ["uninstall", "--project", project, "--json"],
environment,
cwd: root,
});
const noOp = await run();
expect(noOp.exitCode, noOp.stdout).toBe(0);
if (aliasedClient === "opencode")
await put(join(home, ".pi", "agent", "trust.json"), {
[project]: true,
});
const selectedOriginal = await put(
selected,
aliasedClient === "pi"
? { mcp: { servers: { rea: openCodeRegistration } } }
: { mcpServers: { rea: piRegistration } },
);
const removed = await run();
expect(removed.exitCode, removed.stdout).toBe(0);
expect(removed.json).toMatchObject({
status: "complete",
items: expect.arrayContaining([
expect.objectContaining({
configuration_path: selected,
status: "removed",
}),
]),
});
expect(await readFile(`${selected}.rea.backup`, "utf8")).toBe(
selectedOriginal,
);
expect(await readFile(alias, "utf8")).toBe(original);
await expect(readFile(`${alias}.rea.backup`)).rejects.toMatchObject({
code: "ENOENT",
});
},
);
}
cliTest(
"project uninstall removes an owned launcher with invalid REA environment settings",
async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-project-uninstall-invalid-owned-",
);
const environment = await registrationEnvironment(join(root, "home"));
const selected = join(root, "selected");
const configuration = join(selected, "opencode.json");
const original = await put(configuration, {
mcp: {
servers: {
rea: {
...openCodeRegistration,
environment: { REA_GHIDRA_STARTUP_TIMEOUT_MS: "bad" },
},
peer: openCodePeer,
},
},
});
const result = await cli.run({
arguments: ["uninstall", "--project", selected, "--json"],
environment,
cwd: root,
});
expect(result.exitCode, result.stdout).toBe(0);
expect(result.json).toMatchObject({
status: "complete",
items: expect.arrayContaining([
expect.objectContaining({ name: "opencode", status: "removed" }),
]),
});
expect(JSON.parse(await readFile(configuration, "utf8"))).toEqual({
mcp: { servers: { peer: openCodePeer } },
});
expect(await readFile(`${configuration}.rea.backup`, "utf8")).toBe(
original,
);
},
);
for (const { label, entry, permitsRemoval } of [
{ label: "no fallback", entry: undefined, permitsRemoval: true },
{
label: "a foreign local fallback",
entry: openCodePeer,
permitsRemoval: false,
},
{
label: "an enabled remote fallback",
entry: openCodeRemote,
permitsRemoval: false,
},
{
label: "a disabled remote fallback",
entry: { ...openCodeRemote, disabled: true },
permitsRemoval: true,
},
{
label: "a remote fallback with malformed activation",
entry: { ...openCodeRemote, disabled: "true" },
permitsRemoval: false,
},
]) {
cliTest(
permitsRemoval
? `project uninstall removes all owned files with ${label}`
: `project uninstall preserves the whole selected set when removal would expose ${label}`,
async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-project-uninstall-multi-file-",
);
const environment = await registrationEnvironment(join(root, "home"));
const selected = join(root, "selected");
const firstPath = join(selected, "opencode.json");
const secondPath = join(selected, "opencode.jsonc");
const first = await put(firstPath, {
mcp: { servers: { rea: openCodeRegistration, first: openCodePeer } },
});
const second = await put(secondPath, {
mcp: { servers: { rea: openCodeRegistration, second: openCodePeer } },
});
const lowerPath = join(root, "opencode.json");
const lower =
entry === undefined
? undefined
: await put(lowerPath, { mcp: { servers: { rea: entry } } });
const run = () =>
cli.run({
arguments: ["uninstall", "--project", selected, "--json"],
environment,
cwd: root,
});
const result = await run();
if (!permitsRemoval) {
expect(result.exitCode, result.stdout).toBe(1);
expect(result.json).toMatchObject({ status: "failed" });
expect(await readFile(firstPath, "utf8")).toBe(first);
expect(await readFile(secondPath, "utf8")).toBe(second);
expect(await readFile(lowerPath, "utf8")).toBe(lower);
await expect(readFile(`${firstPath}.rea.backup`)).rejects.toMatchObject(
{
code: "ENOENT",
},
);
await expect(
readFile(`${secondPath}.rea.backup`),
).rejects.toMatchObject({
code: "ENOENT",
});
return;
}
expect(result.exitCode, result.stdout).toBe(0);
expect(result.json).toMatchObject({
status: "complete",
items: expect.arrayContaining([
expect.objectContaining({
name: "opencode",
status: "removed",
actions: expect.arrayContaining([
expect.objectContaining({
configuration_path: firstPath,
status: "removed",
}),
expect.objectContaining({
configuration_path: secondPath,
status: "removed",
}),
]),
}),
]),
});
if (lower !== undefined)
expect(await readFile(lowerPath, "utf8")).toBe(lower);
expect(JSON.parse(await readFile(firstPath, "utf8"))).toEqual({
mcp: { servers: { first: openCodePeer } },
});
expect(JSON.parse(await readFile(secondPath, "utf8"))).toEqual({
mcp: { servers: { second: openCodePeer } },
});
expect(await readFile(`${firstPath}.rea.backup`, "utf8")).toBe(first);
expect(await readFile(`${secondPath}.rea.backup`, "utf8")).toBe(second);
expect((await run()).exitCode).toBe(0);
},
);
}