Files
reverse-engineering-anything/tests/boundary/filesystem/ampWorkspaceConfiguration.test.ts
T

138 lines
5.2 KiB
TypeScript

import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, vi } from "vitest";
import { readClientRegistrationStatuses } from "../../../src/application/ClientRegistrationStatus.js";
import { supportedClients } from "../../../src/application/SupportedClients.js";
import {
readProjectRegistration,
projectServer as serverNamed,
writeProjectConfiguration,
} from "../../fixtures/projectRegistration.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const writeSettings = (path: string, servers: unknown) =>
writeProjectConfiguration(path, { "amp.mcpServers": servers });
const server = (command: string) => ({ command, args: ["mcp"] });
const fixture = async ({ repository = true } = {}) => {
const root = await createTestTempDirectory("rea-amp-workspace-");
const home = join(root, "home");
const workspace = join(root, "workspace");
const projectDirectory = join(workspace, "packages", "app");
if (repository) await mkdir(join(workspace, ".git"), { recursive: true });
await mkdir(projectDirectory, { recursive: true });
const client = supportedClients(home, process.platform, {}).find(
({ name }) => name === "amp",
);
if (client === undefined)
throw new Error("Amp is missing from the client table");
await writeSettings(client.configPath, {
rea: server("user"),
other: server("user-other"),
});
return {
root,
home,
userSettings: client.configPath,
workspace,
projectDirectory,
read: () => readProjectRegistration(client, projectDirectory),
};
};
it("selects a workspace amp.mcpServers.rea over the user entry and keeps other user servers", async () => {
const { workspace, read } = await fixture();
const workspaceSettings = join(workspace, ".amp", "settings.json");
await writeSettings(workspaceSettings, { rea: server("workspace") });
const result = await read();
if (!result.ok) throw Error(result.error.detail);
expect(result.value).toMatchObject({
scope: "project",
configPath: workspaceSettings,
launcherConfigPath: workspaceSettings,
});
expect(serverNamed(result.value.document, "rea")).toEqual(
server("workspace"),
);
expect(serverNamed(result.value.document, "other")).toEqual(
server("user-other"),
);
});
it("reads only the nearest workspace settings between the working directory and the repository root", async () => {
const { workspace, projectDirectory, userSettings, read } = await fixture();
await writeSettings(join(workspace, ".amp", "settings.json"), {
rea: server("repository-root"),
});
const nearest = join(projectDirectory, "..", ".amp", "settings.jsonc");
await writeSettings(nearest, { other: server("nearest-other") });
const result = await read();
if (!result.ok) throw Error(result.error.detail);
// The nearer file has no rea row, and Amp does not consult the farther one.
expect(result.value.configPath).toBe(userSettings);
expect(serverNamed(result.value.document, "rea")).toEqual(server("user"));
expect(serverNamed(result.value.document, "other")).toEqual(
server("nearest-other"),
);
});
it("does not walk past the working directory outside a Git repository", async () => {
const { workspace, userSettings, read } = await fixture({
repository: false,
});
await writeSettings(join(workspace, ".amp", "settings.json"), {
rea: server("ancestor"),
});
const result = await read();
if (!result.ok) throw Error(result.error.detail);
expect(result.value.configPath).toBe(userSettings);
expect(serverNamed(result.value.document, "rea")).toEqual(server("user"));
});
it("prefers settings.json when one workspace has both settings.json and settings.jsonc", async () => {
const { projectDirectory, read } = await fixture();
await writeSettings(join(projectDirectory, ".amp", "settings.json"), {
rea: server("json"),
});
await writeSettings(join(projectDirectory, ".amp", "settings.jsonc"), {
rea: server("jsonc"),
});
const result = await read();
if (!result.ok) throw new Error(result.error.detail);
expect(result.value.configPath).toBe(
join(projectDirectory, ".amp", "settings.json"),
);
});
it("reports an ancestor Amp workspace registration through a regular .amp file and retains approval and launcher diagnostics", async () => {
const { home, projectDirectory, workspace: repository } = await fixture();
await writeFile(join(projectDirectory, "..", ".amp"), "regular file");
const workspace = join(repository, ".amp", "settings.json");
await writeSettings(workspace, {
rea: { command: "npx", args: ["-y", "rea-agents@0.0.1", "mcp"] },
});
const cwd = vi.spyOn(process, "cwd").mockReturnValue(projectDirectory);
try {
const statuses = await readClientRegistrationStatuses(home, undefined, {
platform: process.platform,
environment: {},
});
const status = statuses.find(({ client }) => client === "amp");
expect(status).toMatchObject({
config_path: workspace,
scope: "project",
state: "unknown",
});
expect(status?.remediation).toContain("rea-agents@0.0.1");
expect(status?.remediation).toContain("amp mcp doctor rea");
} finally {
cwd.mockRestore();
}
});