mirror of
https://github.com/morluto/rea.git
synced 2026-12-01 11:01:11 +00:00
Discover the OpenCode system and managed layers and honor their effective MCP policy when planning, publishing, diagnosing, or removing registrations. Preserve the writable layer's dialect and verify effective registration and policy after publication. Fixes #15036. Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing) Co-authored-by: N0zoM1z0 <161784452+N0zoM1z0@users.noreply.github.com> Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
812 lines
26 KiB
TypeScript
812 lines
26 KiB
TypeScript
import {
|
|
mkdir,
|
|
readdir,
|
|
readFile,
|
|
rm,
|
|
symlink,
|
|
writeFile,
|
|
} from "node:fs/promises";
|
|
import { userInfo } from "node:os";
|
|
import { dirname, join } from "node:path";
|
|
import { build, buildBinary } from "plist";
|
|
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
|
|
|
import { readEffectiveClientRegistrationConfiguration } from "../../../src/application/ClientConfigurationFiles.js";
|
|
import { currentClientRegistrationCommand } from "../../../src/application/ClientRegistrationIdentity.js";
|
|
import { readClientRegistrationStatuses } from "../../../src/application/ClientRegistrationStatus.js";
|
|
import {
|
|
clientRefreshRefusal,
|
|
configureClientConfiguration,
|
|
inspectClientConfiguration,
|
|
} from "../../../src/application/SetupClientConfiguration.js";
|
|
import {
|
|
runUninstall,
|
|
systemUninstallHost,
|
|
} from "../../../src/application/Uninstall.js";
|
|
import { resolveMcpRegistrationClients } from "../../../src/application/McpRegistrationCatalog.js";
|
|
import {
|
|
prepareRegistration,
|
|
verifyEffectiveRegistration,
|
|
} from "../../../src/application/McpRegistrationConfiguration.js";
|
|
import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceScope.js";
|
|
import { registerMcpClientsOnHost } from "../../fixtures/mcpRegistration.js";
|
|
import {
|
|
resolveSupportedClients,
|
|
type SetupClient,
|
|
} from "../../../src/application/SupportedClients.js";
|
|
import { clearClientLocationEnvironment } from "../../fixtures/clientEnvironment.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
|
|
const platform = process.platform;
|
|
const command = currentClientRegistrationCommand({ platform });
|
|
const registration = (launcher: readonly string[]) =>
|
|
JSON.stringify({
|
|
mcp: { rea: { type: "local", command: launcher, enabled: true } },
|
|
});
|
|
|
|
beforeEach(clearClientLocationEnvironment);
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
/** A current user registration beneath a system directory with its own files. */
|
|
const layers = async (system: Record<string, string>) => {
|
|
const home = await createTestTempDirectory("rea-opencode-system-");
|
|
const user = join(home, ".config", "opencode", "opencode.json");
|
|
await mkdir(join(home, ".config", "opencode"), { recursive: true });
|
|
await writeFile(user, registration(command));
|
|
const directory = join(home, "system");
|
|
await mkdir(directory);
|
|
for (const [name, content] of Object.entries(system))
|
|
await writeFile(join(directory, name), content);
|
|
const systemPaths = { openCodeSystemDirectory: directory };
|
|
const row = (
|
|
await resolveSupportedClients(home, platform, {}, systemPaths)
|
|
).find(({ name }) => name === "opencode");
|
|
if (row === undefined) throw new Error("OpenCode row is missing");
|
|
const status = async () =>
|
|
(
|
|
await readClientRegistrationStatuses(home, command[1], {
|
|
platform,
|
|
environment: {},
|
|
systemPaths,
|
|
scope: "user",
|
|
})
|
|
).find(({ client }) => client === "opencode");
|
|
return { home, user, directory, row, status, systemPaths };
|
|
};
|
|
|
|
it.each(["opencode.json", "opencode.jsonc"])(
|
|
"reports the system %s entry OpenCode runs instead of the user row",
|
|
async (name) => {
|
|
const { directory, status } = await layers({
|
|
[name]: registration(["/opt/other/node", "server.js"]),
|
|
});
|
|
const reported = await status();
|
|
expect(reported?.config_path).toBe(join(directory, name));
|
|
expect(reported?.state).not.toBe("aligned");
|
|
},
|
|
);
|
|
|
|
it("keeps reporting the user row when the system files omit REA", async () => {
|
|
const { user, status } = await layers({
|
|
"opencode.json": JSON.stringify({ mcp: {} }),
|
|
});
|
|
expect(await status()).toMatchObject({ config_path: user, state: "aligned" });
|
|
});
|
|
|
|
it("refuses a setup write the system entry would override, editing neither", async () => {
|
|
const content = registration(["/opt/other/node", "server.js"]);
|
|
const { user, directory, row } = await layers({ "opencode.json": content });
|
|
const before = await readFile(user, "utf8");
|
|
expect(
|
|
await clientRefreshRefusal(row, {}, command, {
|
|
platform,
|
|
currentCommandPath: command[1] ?? "",
|
|
}),
|
|
).toMatchObject({
|
|
alignment: "refused",
|
|
remediation: expect.stringContaining(join(directory, "opencode.json")),
|
|
});
|
|
await configureClientConfiguration(row, {}, command).catch(() => undefined);
|
|
expect(await readFile(user, "utf8")).toBe(before);
|
|
expect(await readFile(join(directory, "opencode.json"), "utf8")).toBe(
|
|
content,
|
|
);
|
|
});
|
|
|
|
it("refuses a setup write that an owned but different system entry overrides", async () => {
|
|
const { directory, row } = await layers({
|
|
"opencode.json": JSON.stringify({
|
|
mcp: { rea: { type: "local", command, enabled: false } },
|
|
}),
|
|
});
|
|
expect(
|
|
await clientRefreshRefusal(row, {}, command, {
|
|
platform,
|
|
currentCommandPath: command[1] ?? "",
|
|
}),
|
|
).toMatchObject({
|
|
alignment: "refused",
|
|
remediation: expect.stringContaining(
|
|
`${join(directory, "opencode.json")} overrides REA's rea registration`,
|
|
),
|
|
});
|
|
});
|
|
|
|
it("accepts the same enabled legacy launcher from the user and system layers", async () => {
|
|
const { row } = await layers({ "opencode.json": registration(command) });
|
|
expect(await inspectClientConfiguration(row, {}, command)).toMatchObject({
|
|
status: "already_current",
|
|
});
|
|
});
|
|
|
|
it("checks the writable OpenCode dialect when the upper row replaces its environment", async () => {
|
|
const { row, user, directory } = await layers({
|
|
"opencode.json": registration(command),
|
|
});
|
|
await writeFile(
|
|
user,
|
|
JSON.stringify({ mcp: { servers: { rea: { type: "local", command } } } }),
|
|
);
|
|
|
|
expect(
|
|
await inspectClientConfiguration(
|
|
row,
|
|
{ REA_MCP_TOOL_FAMILIES: "electron" },
|
|
command,
|
|
),
|
|
).toMatchObject({
|
|
status: "invalid",
|
|
remediation: expect.stringContaining(
|
|
`${join(directory, "opencode.json")} overrides REA's rea registration`,
|
|
),
|
|
});
|
|
});
|
|
|
|
it("reports the system entry uninstall cannot remove, leaving that file", async () => {
|
|
const content = registration(command);
|
|
const { home, directory, systemPaths } = await layers({
|
|
"opencode.json": content,
|
|
});
|
|
const result = await runUninstall(
|
|
false,
|
|
systemUninstallHost(home, undefined, {}, platform, systemPaths),
|
|
);
|
|
const opencode = result.items.find(({ name }) => name === "opencode");
|
|
expect(opencode?.status).toBe("failed");
|
|
expect(JSON.stringify(opencode)).toContain(join(directory, "opencode.json"));
|
|
expect(await readFile(join(directory, "opencode.json"), "utf8")).toBe(
|
|
content,
|
|
);
|
|
});
|
|
|
|
it("applies the system entry above a project entry", async () => {
|
|
const { home, directory, row } = await layers({
|
|
"opencode.json": registration(["/system/node", "server.js"]),
|
|
});
|
|
const project = join(home, "project");
|
|
await mkdir(join(project, ".git"), { recursive: true });
|
|
await writeFile(
|
|
join(project, "opencode.json"),
|
|
registration(["/project/node", "server.js"]),
|
|
);
|
|
const effective = await readEffectiveClientRegistrationConfiguration(
|
|
row,
|
|
undefined,
|
|
platform,
|
|
{ projectDirectory: project },
|
|
);
|
|
expect(effective).toMatchObject({
|
|
ok: true,
|
|
value: {
|
|
overridingConfigPath: join(directory, "opencode.json"),
|
|
document: {
|
|
servers: { rea: { command: ["/system/node", "server.js"] } },
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("leaves OpenCode unresolved when a system file cannot be parsed", async () => {
|
|
const { directory, row } = await layers({ "opencode.json": "{" });
|
|
expect(row.configPathError).toContain(join(directory, "opencode.json"));
|
|
});
|
|
|
|
it("keeps the system files as separate layers under native replacement", async () => {
|
|
const { home, user, row } = await layers({
|
|
"opencode.json": JSON.stringify({
|
|
mcp: { rea: { type: "local", command, environment: { OLD: "1" } } },
|
|
}),
|
|
"opencode.jsonc": registration(command),
|
|
});
|
|
await writeFile(user, JSON.stringify({ mcp: { servers: {} } }));
|
|
const effective = await readEffectiveClientRegistrationConfiguration(
|
|
row,
|
|
undefined,
|
|
platform,
|
|
);
|
|
if (!effective.ok) throw new Error(effective.error.detail);
|
|
expect(effective.value.document?.servers.rea).not.toHaveProperty(
|
|
"environment",
|
|
);
|
|
expect(home).toBeTruthy();
|
|
});
|
|
|
|
it.each(["OPENCODE_CONFIG", "a user symlink"])(
|
|
"refuses a write target that is a system file through %s",
|
|
async (route) => {
|
|
const { home, user, directory, systemPaths } = await layers({
|
|
"opencode.json": "{}",
|
|
});
|
|
const system = join(directory, "opencode.json");
|
|
if (route === "a user symlink") {
|
|
await rm(user);
|
|
await symlink(system, user);
|
|
}
|
|
const row = (
|
|
await resolveSupportedClients(
|
|
home,
|
|
platform,
|
|
route === "OPENCODE_CONFIG" ? { OPENCODE_CONFIG: system } : {},
|
|
systemPaths,
|
|
)
|
|
).find(({ name }) => name === "opencode");
|
|
expect(row?.configPathError).toContain("system configuration");
|
|
},
|
|
);
|
|
|
|
it("refuses a project registration the system entry would override", async () => {
|
|
const { home, systemPaths } = await layers({
|
|
"opencode.json": registration(["/system/node", "server.js"]),
|
|
});
|
|
const project = join(home, "project");
|
|
await mkdir(join(project, ".git"), { recursive: true });
|
|
const resources = new ArtifactResourceScope();
|
|
const selected = await resolveMcpRegistrationClients(
|
|
{
|
|
agents: ["opencode"],
|
|
global: false,
|
|
cwd: project,
|
|
home,
|
|
platform,
|
|
environment: {},
|
|
systemPaths,
|
|
},
|
|
resources,
|
|
);
|
|
if (!selected.ok) throw selected.error;
|
|
const [outcome] = await registerMcpClientsOnHost(
|
|
selected.value,
|
|
{ command: command[0] ?? "", args: command.slice(1) },
|
|
undefined,
|
|
resources,
|
|
);
|
|
if (outcome?.status !== "completed") throw new Error("OpenCode did not run");
|
|
if (outcome.result.ok) throw new Error("The system launcher was ignored");
|
|
expect(outcome.result.error.message).toContain(
|
|
"higher-priority configuration",
|
|
);
|
|
});
|
|
|
|
const managedFile = "ai.opencode.managed.plist";
|
|
const preferences = (entry: readonly string[]) => ({
|
|
mcp: { rea: { type: "local", command: [...entry], enabled: true } },
|
|
});
|
|
|
|
/** OpenCode on macOS with the given managed preference files. */
|
|
const managed = async (files: {
|
|
readonly machine?: Buffer | string;
|
|
readonly user?: Buffer | string;
|
|
}) => {
|
|
const { home, directory } = await layers({
|
|
"opencode.json": registration(["/system/node", "server.js"]),
|
|
});
|
|
const root = join(home, "managed");
|
|
const perUser = join(root, userInfo().username);
|
|
await mkdir(perUser, { recursive: true });
|
|
if (files.machine !== undefined)
|
|
await writeFile(join(root, managedFile), files.machine);
|
|
if (files.user !== undefined)
|
|
await writeFile(join(perUser, managedFile), files.user);
|
|
const row = (
|
|
await resolveSupportedClients(
|
|
home,
|
|
"darwin",
|
|
{},
|
|
{
|
|
openCodeSystemDirectory: directory,
|
|
openCodeManagedPreferencesDirectory: root,
|
|
},
|
|
)
|
|
).find(({ name }) => name === "opencode");
|
|
if (row === undefined) throw new Error("OpenCode row is missing");
|
|
return { row, root, perUser };
|
|
};
|
|
|
|
const effectiveCommand = async (row: SetupClient) => {
|
|
const effective = await readEffectiveClientRegistrationConfiguration(
|
|
row,
|
|
undefined,
|
|
"darwin",
|
|
);
|
|
if (!effective.ok) throw new Error(effective.error.detail);
|
|
return {
|
|
command: effective.value.document?.servers.rea,
|
|
file: effective.value.overridingConfigPath,
|
|
};
|
|
};
|
|
|
|
it.each([
|
|
["XML", (value: ReturnType<typeof preferences>) => build(value)],
|
|
["binary", (value: ReturnType<typeof preferences>) => buildBinary(value)],
|
|
])(
|
|
"applies %s managed preferences above the system file",
|
|
async (_, encode) => {
|
|
const { row, root } = await managed({
|
|
machine: Buffer.from(encode(preferences(["/managed/node", "s.js"]))),
|
|
});
|
|
expect(await effectiveCommand(row)).toMatchObject({
|
|
command: { command: ["/managed/node", "s.js"] },
|
|
file: join(root, managedFile),
|
|
});
|
|
},
|
|
);
|
|
|
|
it("lets the user's managed profile replace the machine-wide one", async () => {
|
|
const { row, perUser } = await managed({
|
|
machine: build(preferences(["/machine/node", "s.js"])),
|
|
user: build(preferences(["/user/node", "s.js"])),
|
|
});
|
|
expect(await effectiveCommand(row)).toMatchObject({
|
|
command: { command: ["/user/node", "s.js"] },
|
|
file: join(perUser, managedFile),
|
|
});
|
|
});
|
|
|
|
it("leaves OpenCode unresolved when managed preferences cannot be parsed", async () => {
|
|
const { row, root } = await managed({ machine: "bplist00 broken" });
|
|
expect(row.configPathError).toContain(join(root, managedFile));
|
|
});
|
|
|
|
const deny = (resource: string, effect = "deny") => ({
|
|
action: "mcp.use",
|
|
resource,
|
|
effect,
|
|
});
|
|
|
|
it.each([
|
|
["a policy", { policies: [deny("rea")] }],
|
|
["a wildcard", { policies: [deny("r?a*")] }],
|
|
["an experimental policy", { experimental: { policies: [deny("*")] } }],
|
|
[
|
|
"a prefixed integration policy",
|
|
{
|
|
policies: [
|
|
{ action: "integration.use", resource: "mcp:rea", effect: "deny" },
|
|
],
|
|
},
|
|
],
|
|
])("blocks REA under a system file with %s", async (_, document) => {
|
|
const { directory, row, status } = await layers({
|
|
"opencode.json": JSON.stringify(document),
|
|
});
|
|
const file = join(directory, "opencode.json");
|
|
expect(await status()).toMatchObject({
|
|
state: "stale",
|
|
remediation: expect.stringContaining(file),
|
|
});
|
|
expect(
|
|
await clientRefreshRefusal(row, {}, command, {
|
|
platform,
|
|
currentCommandPath: command[1] ?? "",
|
|
}),
|
|
).toMatchObject({
|
|
alignment: "refused",
|
|
remediation: expect.stringContaining("denies"),
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
["another server", [deny("github")]],
|
|
["a later allow", [deny("*"), deny("rea", "allow")]],
|
|
["an invalid statement", [{ action: "mcp.use", effect: "deny" }]],
|
|
])("keeps REA aligned when the policy names %s", async (_, policies) => {
|
|
const { status } = await layers({
|
|
"opencode.json": JSON.stringify({ policies }),
|
|
});
|
|
expect(await status()).toMatchObject({ state: "aligned" });
|
|
});
|
|
|
|
it.each(["re*", "r?a"])(
|
|
"applies a matching OpenCode policy for %s",
|
|
async (pattern) => {
|
|
const { status } = await layers({
|
|
"opencode.json": JSON.stringify({ policies: [deny(pattern)] }),
|
|
});
|
|
expect(await status()).toMatchObject({
|
|
state: "stale",
|
|
remediation: expect.stringContaining("denies mcp.use"),
|
|
});
|
|
},
|
|
);
|
|
|
|
it("rechecks OpenCode's global policy during registration verification", async () => {
|
|
const { directory, home, systemPaths } = await layers({});
|
|
const resources = new ArtifactResourceScope();
|
|
const selected = await resolveMcpRegistrationClients(
|
|
{
|
|
agents: ["opencode"],
|
|
global: true,
|
|
cwd: home,
|
|
home,
|
|
platform,
|
|
environment: {},
|
|
systemPaths,
|
|
},
|
|
resources,
|
|
);
|
|
if (!selected.ok) throw selected.error;
|
|
const [client] = selected.value;
|
|
if (client === undefined) throw new Error("OpenCode registration is missing");
|
|
const prepared = await prepareRegistration(
|
|
client.configPath,
|
|
client,
|
|
{ command: command[0] ?? "", args: command.slice(1) },
|
|
platform,
|
|
);
|
|
if (!prepared.ok) throw prepared.error;
|
|
expect(
|
|
await verifyEffectiveRegistration(prepared.value, prepared.value.contents),
|
|
).toMatchObject({ ok: true });
|
|
|
|
await writeFile(
|
|
join(directory, "opencode.json"),
|
|
JSON.stringify({ policies: [deny("rea")] }),
|
|
);
|
|
expect(
|
|
await verifyEffectiveRegistration(prepared.value, prepared.value.contents),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: { message: expect.stringContaining("denies mcp.use") },
|
|
});
|
|
});
|
|
|
|
it("lets a system deny outrank a user allow", async () => {
|
|
const { user, status } = await layers({
|
|
"opencode.json": JSON.stringify({ policies: [deny("rea")] }),
|
|
});
|
|
await writeFile(
|
|
user,
|
|
JSON.stringify({
|
|
...(JSON.parse(registration(command)) as object),
|
|
policies: [deny("rea", "allow")],
|
|
}),
|
|
);
|
|
expect(await status()).toMatchObject({ state: "stale" });
|
|
});
|
|
|
|
it("refuses a project registration a system policy blocks", async () => {
|
|
const { home, systemPaths } = await layers({
|
|
"opencode.json": JSON.stringify({ policies: [deny("rea")] }),
|
|
});
|
|
const project = join(home, "project");
|
|
await mkdir(join(project, ".git"), { recursive: true });
|
|
const resources = new ArtifactResourceScope();
|
|
const selected = await resolveMcpRegistrationClients(
|
|
{
|
|
agents: ["opencode"],
|
|
global: false,
|
|
cwd: project,
|
|
home,
|
|
platform,
|
|
environment: {},
|
|
systemPaths,
|
|
},
|
|
resources,
|
|
);
|
|
if (!selected.ok) throw selected.error;
|
|
const [outcome] = await registerMcpClientsOnHost(
|
|
selected.value,
|
|
{ command: command[0] ?? "", args: command.slice(1) },
|
|
undefined,
|
|
resources,
|
|
);
|
|
if (outcome?.status !== "completed") throw new Error("OpenCode did not run");
|
|
if (outcome.result.ok) throw new Error("The policy was ignored");
|
|
expect(outcome.result.error.message).toContain("denies");
|
|
});
|
|
|
|
it("keeps the project launcher when the system entry sets only environment", async () => {
|
|
const { home, directory, status, systemPaths } = await layers({
|
|
"opencode.json": JSON.stringify({
|
|
mcp: { rea: { environment: { NOTE: "system" } } },
|
|
}),
|
|
});
|
|
const project = join(home, "project");
|
|
await mkdir(join(project, ".git"), { recursive: true });
|
|
const projectFile = join(project, "opencode.json");
|
|
await writeFile(
|
|
projectFile,
|
|
registration(["npx", "-y", "rea-agents@0.0.1", "mcp"]),
|
|
);
|
|
vi.spyOn(process, "cwd").mockReturnValue(project);
|
|
const reported = (
|
|
await readClientRegistrationStatuses(home, command[1], {
|
|
platform,
|
|
environment: {},
|
|
systemPaths,
|
|
})
|
|
).find(({ client }) => client === "opencode");
|
|
expect(reported?.config_path).toBe(join(directory, "opencode.json"));
|
|
expect(reported?.remediation).toContain(projectFile);
|
|
expect(await status()).toBeDefined();
|
|
});
|
|
|
|
it.each([
|
|
["global", true, registration(["/system/node", "server.js"])],
|
|
["project", false, registration(["/system/node", "server.js"])],
|
|
["global", true, JSON.stringify({ policies: [deny("rea")] })],
|
|
["project", false, JSON.stringify({ policies: [deny("rea")] })],
|
|
])(
|
|
"rereads a system file created after %s selection (global %s): %s",
|
|
async (_, global, late) => {
|
|
const { home, directory, systemPaths } = await layers({});
|
|
const project = join(home, "project");
|
|
await mkdir(join(project, ".git"), { recursive: true });
|
|
const resources = new ArtifactResourceScope();
|
|
const selected = await resolveMcpRegistrationClients(
|
|
{
|
|
agents: ["opencode"],
|
|
global,
|
|
cwd: project,
|
|
home,
|
|
platform,
|
|
environment: {},
|
|
systemPaths,
|
|
},
|
|
resources,
|
|
);
|
|
if (!selected.ok) throw selected.error;
|
|
await writeFile(join(directory, "opencode.json"), late);
|
|
const [outcome] = await registerMcpClientsOnHost(
|
|
selected.value,
|
|
{ command: command[0] ?? "", args: command.slice(1) },
|
|
undefined,
|
|
resources,
|
|
);
|
|
if (outcome?.status !== "completed")
|
|
throw new Error("OpenCode did not run");
|
|
expect(outcome.result.ok).toBe(false);
|
|
},
|
|
);
|
|
|
|
it.each(["{}", undefined])(
|
|
"refuses a project target that links to the system folder (system file %s)",
|
|
async (content) => {
|
|
const { home, directory, systemPaths } = await layers(
|
|
content === undefined ? {} : { "opencode.json": content },
|
|
);
|
|
const project = join(home, "project");
|
|
await mkdir(join(project, ".git"), { recursive: true });
|
|
await symlink(directory, join(project, ".opencode"));
|
|
const resources = new ArtifactResourceScope();
|
|
const selected = await resolveMcpRegistrationClients(
|
|
{
|
|
agents: ["opencode"],
|
|
global: false,
|
|
cwd: project,
|
|
home,
|
|
platform,
|
|
environment: {},
|
|
systemPaths,
|
|
},
|
|
resources,
|
|
);
|
|
if (selected.ok)
|
|
await registerMcpClientsOnHost(
|
|
selected.value,
|
|
{ command: command[0] ?? "", args: command.slice(1) },
|
|
undefined,
|
|
resources,
|
|
);
|
|
expect(await readdir(directory)).toEqual(
|
|
content === undefined ? [] : ["opencode.json"],
|
|
);
|
|
if (content !== undefined)
|
|
expect(await readFile(join(directory, "opencode.json"), "utf8")).toBe(
|
|
content,
|
|
);
|
|
},
|
|
);
|
|
|
|
it("keeps a project uninstall from editing a linked system file", async () => {
|
|
const content = registration(command);
|
|
const { home, directory, systemPaths } = await layers({
|
|
"opencode.json": content,
|
|
});
|
|
const project = join(home, "project");
|
|
await mkdir(join(project, ".git"), { recursive: true });
|
|
await symlink(directory, join(project, ".opencode"));
|
|
await runUninstall(
|
|
false,
|
|
systemUninstallHost(home, undefined, {}, platform, systemPaths, project),
|
|
);
|
|
expect(await readdir(directory)).toEqual(["opencode.json"]);
|
|
expect(await readFile(join(directory, "opencode.json"), "utf8")).toBe(
|
|
content,
|
|
);
|
|
});
|
|
|
|
it("refuses a missing user target inside a linked system folder", async () => {
|
|
const { home, directory, systemPaths } = await layers({});
|
|
const link = join(home, "linked");
|
|
await symlink(directory, link);
|
|
const row = (
|
|
await resolveSupportedClients(
|
|
home,
|
|
platform,
|
|
{ OPENCODE_CONFIG: join(link, "opencode.json") },
|
|
systemPaths,
|
|
)
|
|
).find(({ name }) => name === "opencode");
|
|
expect(row?.configPathError).toContain("system configuration");
|
|
});
|
|
|
|
const windowsWarning = "any local account can create";
|
|
|
|
it.each([
|
|
["win32", true],
|
|
["linux", false],
|
|
] as const)("warns about a system file's owner on %s", async (host, warns) => {
|
|
const { directory, systemPaths, home } = await layers({
|
|
"opencode.json": registration(["/opt/other/node", "server.js"]),
|
|
});
|
|
const row = (await resolveSupportedClients(home, host, {}, systemPaths)).find(
|
|
({ name }) => name === "opencode",
|
|
);
|
|
expect(row?.inlineConfiguration?.origin).toBe(
|
|
join(directory, "opencode.json"),
|
|
);
|
|
expect(row?.inlineConfiguration?.caution !== undefined).toBe(warns);
|
|
});
|
|
|
|
it.each([
|
|
["an overriding entry", registration(["/opt/other/node", "server.js"])],
|
|
[
|
|
"a policy deny",
|
|
JSON.stringify({
|
|
policies: [{ action: "mcp.use", resource: "rea", effect: "deny" }],
|
|
}),
|
|
],
|
|
])(
|
|
"tells a Windows user to check the owner behind %s",
|
|
async (_case, content) => {
|
|
const { home, systemPaths } = await layers({ "opencode.json": content });
|
|
const status = (
|
|
await readClientRegistrationStatuses(home, command[1], {
|
|
platform: "win32",
|
|
environment: {},
|
|
systemPaths,
|
|
scope: "user",
|
|
})
|
|
).find(({ client }) => client === "opencode");
|
|
expect(status?.remediation).toContain(windowsWarning);
|
|
const row = (
|
|
await resolveSupportedClients(home, "win32", {}, systemPaths)
|
|
).find(({ name }) => name === "opencode");
|
|
if (row === undefined) throw new Error("OpenCode row is missing");
|
|
expect(
|
|
await clientRefreshRefusal(row, {}, command, {
|
|
platform: "win32",
|
|
currentCommandPath: command[1] ?? "",
|
|
}),
|
|
).toMatchObject({
|
|
remediation: expect.stringContaining(windowsWarning),
|
|
});
|
|
},
|
|
);
|
|
|
|
it("asks a Windows user to check every system file that applies", async () => {
|
|
const { home, directory, systemPaths } = await layers({
|
|
"opencode.json": JSON.stringify({
|
|
policies: [{ action: "mcp.use", resource: "rea", effect: "deny" }],
|
|
}),
|
|
"opencode.jsonc": "{}",
|
|
});
|
|
const row = (
|
|
await resolveSupportedClients(home, "win32", {}, systemPaths)
|
|
).find(({ name }) => name === "opencode");
|
|
expect(row?.inlineConfiguration?.caution).toContain(
|
|
`${join(directory, "opencode.json")} and ${join(directory, "opencode.jsonc")}`,
|
|
);
|
|
});
|
|
|
|
it.each(["deny", "allow"] as const)(
|
|
"uses the earlier ordinary file for an OpenCode %s policy",
|
|
async (effect) => {
|
|
const { user, status } = await layers({});
|
|
const later = effect === "deny" ? "allow" : "deny";
|
|
await writeFile(
|
|
user,
|
|
JSON.stringify({
|
|
...(JSON.parse(registration(command)) as object),
|
|
policies: [deny("rea", effect)],
|
|
}),
|
|
);
|
|
await writeFile(
|
|
join(dirname(user), "opencode.jsonc"),
|
|
JSON.stringify({
|
|
policies: [deny("rea", later)],
|
|
}),
|
|
);
|
|
expect(await status()).toMatchObject({
|
|
state: effect === "deny" ? "stale" : "aligned",
|
|
});
|
|
},
|
|
);
|
|
|
|
it("reports ambiguity when OpenCode V1 and V2 select different legacy environments", async () => {
|
|
const { user, directory, status } = await layers({
|
|
"opencode.json": registration(command),
|
|
});
|
|
await writeFile(
|
|
user,
|
|
JSON.stringify({
|
|
mcp: {
|
|
rea: {
|
|
type: "local",
|
|
command,
|
|
environment: { REA_MCP_TOOL_FAMILIES: "electron" },
|
|
},
|
|
},
|
|
}),
|
|
);
|
|
expect(await status()).toMatchObject({
|
|
config_path: join(directory, "opencode.json"),
|
|
remediation: expect.stringContaining(
|
|
"V1 and V2 select different MCP settings",
|
|
),
|
|
});
|
|
});
|
|
|
|
it("attaches the Windows ownership caution to an aligned system registration", async () => {
|
|
const { home, directory, systemPaths } = await layers({
|
|
"opencode.json": registration(command),
|
|
});
|
|
const reported = (
|
|
await readClientRegistrationStatuses(home, command[1], {
|
|
platform: "win32",
|
|
environment: {},
|
|
systemPaths,
|
|
scope: "user",
|
|
})
|
|
).find(({ client }) => client === "opencode");
|
|
expect(reported).toMatchObject({
|
|
config_path: join(directory, "opencode.json"),
|
|
state: "aligned",
|
|
advisory: expect.stringContaining("confirm an administrator owns"),
|
|
});
|
|
});
|
|
|
|
it.each(["win32", "linux"] as const)(
|
|
"matches OpenCode server policy case on %s",
|
|
async (targetPlatform) => {
|
|
const { home, systemPaths } = await layers({
|
|
"opencode.json": JSON.stringify({ policies: [deny("REA")] }),
|
|
});
|
|
const reported = (
|
|
await readClientRegistrationStatuses(home, command[1], {
|
|
platform: targetPlatform,
|
|
environment: {},
|
|
systemPaths,
|
|
scope: "user",
|
|
})
|
|
).find(({ client }) => client === "opencode");
|
|
expect(reported?.remediation?.includes("denies mcp.use") ?? false).toBe(
|
|
targetPlatform === "win32",
|
|
);
|
|
},
|
|
);
|