Files
reverse-engineering-anything/tests/boundary/filesystem/clientRegistrationOwnership.test.ts
T
7915d15938 fix(setup): own bare rea only when PATH resolves to this package (#15046)
Resolve bare REA launchers using the registration's effective PATH and
verify the resulting installation before setup or uninstall changes an
entry. Share the parsed environment through registration workflows and
keep unproven launchers unknown. Cover npm shim lookup on Windows and
use deterministic ownership fixtures for layered removal.

Fixes #15026.

Co-authored-by: N0zoM1z0 <161784452+N0zoM1z0@users.noreply.github.com>
Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-12-01 00:53:48 +08:00

173 lines
5.4 KiB
TypeScript

import { HOST_PLATFORM } from "../../../src/platform/HostPlatform.js";
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { parse as parseToml } from "smol-toml";
import { expect, it } from "vitest";
import { readClientRegistrationStatuses } from "../../../src/application/ClientRegistrationStatus.js";
import { systemUninstallHost } from "../../../src/application/Uninstall.js";
import { PRODUCT_IDENTITY } from "../../../src/identity.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const ownedLauncher = [
"npx",
"-y",
`${PRODUCT_IDENTITY.packageName}@6.6.0`,
"mcp",
] as const;
const codexSource = (invalid: string): string =>
[
'caller_root = "keep"',
"[mcp_servers.rea]",
`command = "${ownedLauncher[0]}"`,
`args = ${JSON.stringify(ownedLauncher.slice(1))}`,
invalid,
"[mcp_servers.peer]",
'command = "peer"',
"args = []",
"",
].join("\n");
it.each([
{
client: "codex",
path: ".codex/config.toml",
invalid: "invalid tool policy",
source: codexSource('enabled_tools = "not-an-array"'),
parse: (text: string): unknown => parseToml(text),
remaining: {
caller_root: "keep",
mcp_servers: { peer: { command: "peer", args: [] } },
},
},
{
client: "codex",
path: ".codex/config.toml",
invalid: "a nonpositive startup timeout",
source: codexSource("startup_timeout_sec = 0"),
parse: (text: string): unknown => parseToml(text),
remaining: {
caller_root: "keep",
mcp_servers: { peer: { command: "peer", args: [] } },
},
},
{
client: "opencode",
path: ".config/opencode/opencode.json",
invalid: "a non-string environment value",
source: JSON.stringify({
caller_root: "keep",
mcp: {
servers: {
rea: {
type: "local",
command: [...ownedLauncher],
environment: { CALLER_SETTING: 42 },
},
peer: { type: "local", command: ["peer"] },
},
},
}),
parse: (text: string): unknown => JSON.parse(text),
remaining: {
caller_root: "keep",
mcp: { servers: { peer: { type: "local", command: ["peer"] } } },
},
},
])(
"removes the owned $client registration despite $invalid",
async (sample) => {
const home = await createTestTempDirectory("rea-registration-ownership-");
const configPath = join(home, sample.path);
const environment = {
CODEX_HOME: join(home, ".codex"),
XDG_CONFIG_HOME: join(home, ".config"),
};
await mkdir(dirname(configPath), { recursive: true });
await writeFile(configPath, sample.source);
const statuses = await readClientRegistrationStatuses(home, undefined, {
platform: HOST_PLATFORM.platform,
environment,
});
expect(
statuses.find(({ client }) => client === sample.client),
).toMatchObject({
state: "invalid",
});
const host = systemUninstallHost(home, undefined, environment);
try {
const client = (await host.clients()).find(
(client) =>
client.name === sample.client && client.configPath === configPath,
);
if (client === undefined)
throw new Error("Expected the configured client");
expect(await host.removeClient(client)).toMatchObject({
name: sample.client,
status: "removed",
});
expect(sample.parse(await readFile(configPath, "utf8"))).toEqual(
sample.remaining,
);
expect(await readFile(`${configPath}.rea.backup`, "utf8")).toBe(
sample.source,
);
} finally {
expect(await host.close?.()).toBeUndefined();
}
},
);
it("removes the owned OpenCode launcher despite invalid transport", async () => {
const home = await createTestTempDirectory("rea-registration-remote-owner-");
const configPath = join(home, ".config/opencode/opencode.json");
const environment = { XDG_CONFIG_HOME: join(home, ".config") };
const source = JSON.stringify({
caller_root: "keep",
mcp: {
servers: {
rea: {
type: "remote",
url: "https://caller.example/mcp",
command: [...ownedLauncher],
},
peer: { type: "local", command: ["peer"] },
},
},
});
await mkdir(dirname(configPath), { recursive: true });
await writeFile(configPath, source);
const statuses = await readClientRegistrationStatuses(home, undefined, {
platform: HOST_PLATFORM.platform,
environment,
});
expect(statuses.find(({ client }) => client === "opencode")).toMatchObject({
state: "invalid",
});
const host = systemUninstallHost(home, undefined, environment);
try {
const client = (await host.clients()).find(
(client) =>
client.name === "opencode" && client.configPath === configPath,
);
if (client === undefined) throw new Error("Expected the configured client");
expect(await host.removeClient(client)).toMatchObject({
name: "opencode",
status: "removed",
});
expect(JSON.parse(await readFile(configPath, "utf8"))).toEqual({
caller_root: "keep",
mcp: { servers: { peer: { type: "local", command: ["peer"] } } },
});
expect(await readFile(`${configPath}.rea.backup`, "utf8")).toBe(source);
expect(await readdir(dirname(configPath))).toEqual([
"opencode.json",
"opencode.json.rea.backup",
]);
} finally {
expect(await host.close?.()).toBeUndefined();
}
});