mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 05:02:40 +00:00
106 lines
4.1 KiB
TypeScript
106 lines
4.1 KiB
TypeScript
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
import { dirname, join } from "node:path";
|
|
import { expect, it } from "vitest";
|
|
|
|
import { readConfigurationFile } from "../../../src/application/ClientConfigurationFiles.js";
|
|
import { readClientPolicyBlock } from "../../../src/application/GeminiClientSettings.js";
|
|
import type { SetupClient } from "../../../src/application/SupportedClients.js";
|
|
import { supportedClientRegistrationProfile } from "../../fixtures/clientRegistrationProfile.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
|
|
const fixture = async () => {
|
|
const home = await createTestTempDirectory("rea-gemini-policy-sources-");
|
|
const configPath = join(home, ".gemini", "settings.json");
|
|
await mkdir(dirname(configPath), { recursive: true });
|
|
const client: SetupClient = {
|
|
name: "gemini_cli",
|
|
registrationProfile: supportedClientRegistrationProfile("gemini_cli"),
|
|
configPath,
|
|
format: "json",
|
|
geminiSettings: {
|
|
defaultsPath: join(home, "defaults.json"),
|
|
systemPath: join(home, "system.json"),
|
|
workspaceDirectory: home,
|
|
trustedFoldersPath: join(home, "trust.json"),
|
|
platform: process.platform,
|
|
},
|
|
};
|
|
return { client, configPath, systemPath: join(home, "system.json") };
|
|
};
|
|
|
|
it("keeps observed user absence until the caller reads a new configuration", async () => {
|
|
const { client, configPath } = await fixture();
|
|
const absent = await readConfigurationFile(configPath, "json");
|
|
expect(absent).toEqual({ ok: true, value: undefined });
|
|
if (!absent.ok) throw absent.error;
|
|
|
|
await writeFile(configPath, JSON.stringify({ mcp: { excluded: ["rea"] } }));
|
|
expect(await readClientPolicyBlock(client, absent.value)).toEqual({
|
|
ok: true,
|
|
value: undefined,
|
|
});
|
|
|
|
const current = await readConfigurationFile(configPath, "json");
|
|
if (!current.ok) throw current.error;
|
|
expect(await readClientPolicyBlock(client, current.value)).toMatchObject({
|
|
ok: true,
|
|
value: expect.stringContaining("mcp.excluded"),
|
|
});
|
|
});
|
|
|
|
it("preserves the parsed user policy when its source disappears", async () => {
|
|
const { client, configPath } = await fixture();
|
|
await writeFile(configPath, JSON.stringify({ mcp: { excluded: ["rea"] } }));
|
|
const observed = await readConfigurationFile(configPath, "json");
|
|
if (!observed.ok) throw observed.error;
|
|
await rm(configPath);
|
|
|
|
expect(await readClientPolicyBlock(client, observed.value)).toMatchObject({
|
|
ok: true,
|
|
value: expect.stringContaining(configPath),
|
|
});
|
|
});
|
|
|
|
it("still reads independent system policy when the user file was absent", async () => {
|
|
const { client, configPath, systemPath } = await fixture();
|
|
const absent = await readConfigurationFile(configPath, "json");
|
|
if (!absent.ok) throw absent.error;
|
|
await writeFile(systemPath, JSON.stringify({ mcp: { excluded: ["rea"] } }));
|
|
|
|
expect(await readClientPolicyBlock(client, absent.value)).toMatchObject({
|
|
ok: true,
|
|
value: expect.stringContaining(systemPath),
|
|
});
|
|
await writeFile(systemPath, "{ invalid settings");
|
|
expect(await readClientPolicyBlock(client, absent.value)).toMatchObject({
|
|
ok: false,
|
|
error: { kind: "malformed", path: systemPath },
|
|
});
|
|
});
|
|
|
|
it("accepts a system registration whose Windows environment names differ only in case", async () => {
|
|
const { client, systemPath } = await fixture();
|
|
const settings = client.geminiSettings;
|
|
if (settings === undefined) throw new Error("Gemini settings are missing");
|
|
const windows: SetupClient = {
|
|
...client,
|
|
geminiSettings: { ...settings, platform: "win32" },
|
|
};
|
|
const entry = (env: Record<string, string>) => ({
|
|
command: "node",
|
|
args: ["cli.js", "mcp"],
|
|
env,
|
|
});
|
|
await writeFile(
|
|
systemPath,
|
|
JSON.stringify({ mcpServers: { rea: entry({ Path: "C:\\bin" }) } }),
|
|
);
|
|
expect(
|
|
await readClientPolicyBlock(windows, undefined, entry({ PATH: "C:\\bin" })),
|
|
).toEqual({ ok: true, value: undefined });
|
|
// POSIX names are case-sensitive, so the system entry still differs.
|
|
expect(
|
|
await readClientPolicyBlock(client, undefined, entry({ PATH: "C:\\bin" })),
|
|
).toMatchObject({ ok: true, value: expect.stringContaining("overrides") });
|
|
});
|