mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 05:07:49 +00:00
332 lines
11 KiB
TypeScript
332 lines
11 KiB
TypeScript
import {
|
|
access,
|
|
chmod,
|
|
mkdir,
|
|
readFile,
|
|
symlink,
|
|
unlink,
|
|
} from "node:fs/promises";
|
|
import { dirname, join } from "node:path";
|
|
import { expect, it, vi } from "vitest";
|
|
|
|
import { readClientRegistrationStatuses } from "../../../src/application/ClientRegistrationStatus.js";
|
|
import { resolveMcpRegistrationClients } from "../../../src/application/McpRegistrationCatalog.js";
|
|
import { supportedClients } from "../../../src/application/SupportedClients.js";
|
|
import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceScope.js";
|
|
import { registerMcpClientOnHost } from "../../fixtures/mcpRegistration.js";
|
|
import {
|
|
readProjectRegistration,
|
|
projectServer as readReaServer,
|
|
writeProjectConfiguration as writeConfiguration,
|
|
} from "../../fixtures/projectRegistration.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
|
|
const server = (command: string) => ({
|
|
command,
|
|
args: ["mcp"],
|
|
});
|
|
|
|
const fixture = async (git = true) => {
|
|
const root = await createTestTempDirectory("rea-antigravity-project-layout-");
|
|
const home = join(root, "home");
|
|
const repository = join(root, "repository");
|
|
const projectDirectory = join(repository, "packages", "child");
|
|
if (git) await mkdir(join(repository, ".git"), { recursive: true });
|
|
await mkdir(projectDirectory, { recursive: true });
|
|
const client = supportedClients(home, process.platform, {}).find(
|
|
({ name }) => name === "antigravity",
|
|
);
|
|
if (client === undefined)
|
|
throw new Error("Antigravity is missing from the client table");
|
|
return {
|
|
client,
|
|
home,
|
|
root,
|
|
repository,
|
|
projectDirectory,
|
|
read: () => readProjectRegistration(client, projectDirectory),
|
|
};
|
|
};
|
|
|
|
it("treats workspace .agents/mcp_config.json rea over the global file without leaking global env", async () => {
|
|
const { client, projectDirectory, read } = await fixture();
|
|
const project = join(projectDirectory, ".agents", "mcp_config.json");
|
|
await writeConfiguration(client.configPath, {
|
|
mcpServers: {
|
|
rea: { ...server("/user/node"), env: { CALLER_SETTING: "user" } },
|
|
peer: server("/user/peer"),
|
|
},
|
|
});
|
|
await writeConfiguration(project, {
|
|
mcpServers: { rea: server("/project/node") },
|
|
});
|
|
|
|
const result = await read();
|
|
if (!result.ok) throw Error(result.error.detail);
|
|
expect(result.value).toMatchObject({
|
|
scope: "project",
|
|
configPath: project,
|
|
launcherScope: "project",
|
|
launcherConfigPath: project,
|
|
projectInspection: "evaluated",
|
|
});
|
|
// A workspace server replaces the global entry whole rather than inheriting
|
|
// global env fields, while unrelated global servers remain visible.
|
|
expect(readReaServer(result.value.document)).toEqual(server("/project/node"));
|
|
expect(readReaServer(result.value.document, "peer")).toEqual(
|
|
server("/user/peer"),
|
|
);
|
|
});
|
|
|
|
it("loads .agents/mcp_config.json from the working directory up to the repository root", async () => {
|
|
const { root, repository, projectDirectory, read } = await fixture();
|
|
const aboveRoot = join(root, ".agents", "mcp_config.json");
|
|
const rootConfig = join(repository, ".agents", "mcp_config.json");
|
|
const childConfig = join(projectDirectory, ".agents", "mcp_config.json");
|
|
await writeConfiguration(aboveRoot, {
|
|
mcpServers: { rea: server("above-root") },
|
|
});
|
|
await writeConfiguration(rootConfig, {
|
|
mcpServers: { rea: server("root") },
|
|
});
|
|
await writeConfiguration(childConfig, {
|
|
mcpServers: { rea: server("child") },
|
|
});
|
|
|
|
const closest = await read();
|
|
if (!closest.ok) throw Error(closest.error.detail);
|
|
expect(closest.value.configPath).toBe(childConfig);
|
|
expect(readReaServer(closest.value.document)).toEqual(server("child"));
|
|
|
|
await unlink(childConfig);
|
|
const rootOnly = await read();
|
|
if (!rootOnly.ok) throw Error(rootOnly.error.detail);
|
|
expect(rootOnly.value.configPath).toBe(rootConfig);
|
|
expect(readReaServer(rootOnly.value.document)).toEqual(server("root"));
|
|
|
|
// The walk stops at the repository root: configs above it do not load.
|
|
await unlink(rootConfig);
|
|
const stopped = await read();
|
|
if (!stopped.ok) throw Error(stopped.error.detail);
|
|
expect(stopped.value.scope).toBeUndefined();
|
|
expect(readReaServer(stopped.value.document)).toBeUndefined();
|
|
});
|
|
|
|
it("reads only the working directory outside a Git repository", async () => {
|
|
const { repository, projectDirectory, read } = await fixture(false);
|
|
await writeConfiguration(join(repository, ".agents", "mcp_config.json"), {
|
|
mcpServers: { rea: server("ancestor") },
|
|
});
|
|
|
|
const stopped = await read();
|
|
if (!stopped.ok) throw Error(stopped.error.detail);
|
|
expect(stopped.value.scope).toBeUndefined();
|
|
expect(readReaServer(stopped.value.document)).toBeUndefined();
|
|
|
|
const local = join(projectDirectory, ".agents", "mcp_config.json");
|
|
await writeConfiguration(local, { mcpServers: { rea: server("local") } });
|
|
const loaded = await read();
|
|
if (!loaded.ok) throw Error(loaded.error.detail);
|
|
expect(loaded.value.configPath).toBe(local);
|
|
expect(readReaServer(loaded.value.document)).toEqual(server("local"));
|
|
});
|
|
|
|
it("falls back to global config when workspace .agents/mcp_config.json defines only other servers", async () => {
|
|
const { client, projectDirectory, read } = await fixture();
|
|
await writeConfiguration(client.configPath, {
|
|
mcpServers: { rea: server("/user/node") },
|
|
});
|
|
await writeConfiguration(
|
|
join(projectDirectory, ".agents", "mcp_config.json"),
|
|
{
|
|
mcpServers: { peer: server("/project/peer") },
|
|
},
|
|
);
|
|
|
|
const result = await read();
|
|
if (!result.ok) throw Error(result.error.detail);
|
|
expect(result.value).toMatchObject({
|
|
scope: "user",
|
|
configPath: client.configPath,
|
|
launcherScope: "user",
|
|
launcherConfigPath: client.configPath,
|
|
});
|
|
expect(readReaServer(result.value.document)).toEqual(server("/user/node"));
|
|
});
|
|
|
|
it("reports workspace .agents/mcp_config.json in client status even without global markers", async () => {
|
|
const { home, projectDirectory } = await fixture();
|
|
const project = join(projectDirectory, ".agents", "mcp_config.json");
|
|
await writeConfiguration(project, {
|
|
mcpServers: {
|
|
rea: {
|
|
command: "npx",
|
|
args: ["-y", "rea-agents@1.0.0", "mcp"],
|
|
},
|
|
},
|
|
});
|
|
|
|
const cwd = vi.spyOn(process, "cwd").mockReturnValue(projectDirectory);
|
|
try {
|
|
const statuses = await readClientRegistrationStatuses(home, undefined, {
|
|
platform: process.platform,
|
|
environment: {},
|
|
});
|
|
const antigravity = statuses.find(
|
|
({ client: name }) => name === "antigravity",
|
|
);
|
|
expect(antigravity).toMatchObject({
|
|
state: "stale",
|
|
scope: "project",
|
|
config_path: project,
|
|
remediation: expect.stringContaining(
|
|
`This project-scoped registration in ${project}`,
|
|
),
|
|
});
|
|
} finally {
|
|
cwd.mockRestore();
|
|
}
|
|
});
|
|
|
|
it("resolves Antigravity project registration target to .agents/mcp_config.json", async () => {
|
|
const { home, projectDirectory } = await fixture();
|
|
const resources = new ArtifactResourceScope();
|
|
try {
|
|
const result = await resolveMcpRegistrationClients(
|
|
{
|
|
agents: ["antigravity"],
|
|
global: false,
|
|
cwd: projectDirectory,
|
|
home,
|
|
platform: process.platform,
|
|
environment: {},
|
|
},
|
|
resources,
|
|
);
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value).toEqual([
|
|
expect.objectContaining({
|
|
id: "antigravity",
|
|
scope: "project",
|
|
format: "json",
|
|
configPath: join(projectDirectory, ".agents", "mcp_config.json"),
|
|
}),
|
|
]);
|
|
} finally {
|
|
await resources.close();
|
|
}
|
|
});
|
|
|
|
it("keeps workspace registration writes inside the selected directory", async () => {
|
|
const { client, home, projectDirectory, root } = await fixture();
|
|
const project = join(projectDirectory, ".agents", "mcp_config.json");
|
|
await writeConfiguration(client.configPath, {
|
|
mcpServers: { other: server("keep-global") },
|
|
});
|
|
const original = await readFile(client.configPath, "utf8");
|
|
await symlink(dirname(client.configPath), dirname(project), "junction");
|
|
const resources = new ArtifactResourceScope();
|
|
try {
|
|
const targets = await resolveMcpRegistrationClients(
|
|
{
|
|
agents: ["antigravity"],
|
|
global: false,
|
|
cwd: projectDirectory,
|
|
home,
|
|
platform: process.platform,
|
|
environment: {},
|
|
},
|
|
resources,
|
|
);
|
|
if (!targets.ok) throw targets.error;
|
|
const target = targets.value[0];
|
|
if (target === undefined) throw new Error("missing Antigravity target");
|
|
const register = () =>
|
|
registerMcpClientOnHost(
|
|
target,
|
|
server(process.execPath),
|
|
undefined,
|
|
resources,
|
|
);
|
|
expect(await register()).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
message: expect.stringContaining(
|
|
"outside the selected project directory",
|
|
),
|
|
},
|
|
});
|
|
expect(await readFile(client.configPath, "utf8")).toBe(original);
|
|
await expect(
|
|
access(`${client.configPath}.rea.backup`),
|
|
).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
await unlink(dirname(project));
|
|
const outside = join(root, "separate", "mcp_config.json");
|
|
await writeConfiguration(outside, {
|
|
mcpServers: { other: server("keep") },
|
|
});
|
|
const outsideOriginal = await readFile(outside, "utf8");
|
|
await symlink(dirname(outside), dirname(project), "junction");
|
|
expect(await register()).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
message: expect.stringContaining(
|
|
"outside the selected project directory",
|
|
),
|
|
},
|
|
});
|
|
expect(await readFile(outside, "utf8")).toBe(outsideOriginal);
|
|
await expect(access(`${outside}.rea.backup`)).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
await unlink(dirname(project));
|
|
expect(await register()).toMatchObject({
|
|
ok: true,
|
|
value: { status: "configured" },
|
|
});
|
|
expect(await readFile(client.configPath, "utf8")).toBe(original);
|
|
} finally {
|
|
await resources.close();
|
|
}
|
|
});
|
|
|
|
it.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
|
|
"reports an unreadable repository marker rather than declaring the global registration effective",
|
|
async () => {
|
|
const { client, home, projectDirectory } = await fixture();
|
|
await writeConfiguration(client.configPath, {
|
|
mcpServers: { rea: server(process.execPath) },
|
|
});
|
|
const getStatus = async () =>
|
|
(
|
|
await readClientRegistrationStatuses(home, undefined, {
|
|
platform: process.platform,
|
|
environment: {},
|
|
selectedClientIds: ["antigravity"],
|
|
})
|
|
).find(({ client: name }) => name === "antigravity");
|
|
const workingDirectory = vi
|
|
.spyOn(process, "cwd")
|
|
.mockReturnValue(projectDirectory);
|
|
try {
|
|
expect(await getStatus()).toMatchObject({
|
|
config_path: client.configPath,
|
|
});
|
|
await chmod(projectDirectory, 0o600);
|
|
expect(await getStatus()).toMatchObject({
|
|
config_path: join(projectDirectory, ".git"),
|
|
scope: "project",
|
|
state: "unknown",
|
|
remediation: expect.stringContaining(
|
|
"repository root could not be established",
|
|
),
|
|
});
|
|
} finally {
|
|
workingDirectory.mockRestore();
|
|
await chmod(projectDirectory, 0o700);
|
|
}
|
|
},
|
|
);
|