Files
reverse-engineering-anything/tests/boundary/filesystem/ampWorkspaceConfiguration.test.ts
993c693bc1 fix(setup): read Amp workspace MCP settings (#15102)
* fix(setup): read Amp workspace MCP settings

Amp reads the nearest .amp/settings.json or .amp/settings.jsonc from the
working directory up to the repository root (only the working directory
outside a Git repository), and a workspace amp.mcpServers entry takes
precedence over the user entry with the same name. REA only read
~/.config/amp/settings.json, so doctor and setup judged a registration
Amp does not run and project uninstall left the workspace row in place.

The effective registration reader now admits Amp's workspace layer and
selects one whole server definition per name. Both workspace files in
the same directory are refused, as for the user settings, because Amp
does not document which one wins. Project uninstall includes Amp.

Fixes #15037

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(setup): share Amp settings names and the existence check

Reuse one existence check for OpenCode's user path and Amp's workspace
walk, name the Amp settings files once, and name the formats whose
effective registration includes project layers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(setup): describe Amp workspace settings selection

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(setup): honor Amp workspace selection and approval scope

* fix(setup): retain Amp launcher repair alongside approval guidance

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-30 01:51:18 +00:00

152 lines
5.6 KiB
TypeScript

import { mkdir, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { expect, it, vi } from "vitest";
import {
effectiveClientServer,
type ClientConfigurationDocument,
} from "../../../src/application/ClientConfigurationDocument.js";
import { readEffectiveClientRegistrationConfiguration } from "../../../src/application/ClientConfigurationFiles.js";
import { readClientRegistrationStatuses } from "../../../src/application/ClientRegistrationStatus.js";
import { supportedClients } from "../../../src/application/SupportedClients.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const writeSettings = async (path: string, servers: unknown) => {
await mkdir(dirname(path), { recursive: true });
await writeFile(path, JSON.stringify({ "amp.mcpServers": servers }));
};
const server = (command: string) => ({ command, args: ["mcp"] });
const serverNamed = (
document: ClientConfigurationDocument | undefined,
name: string,
) =>
document === undefined ? undefined : effectiveClientServer(document, name);
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: () =>
readEffectiveClientRegistrationConfiguration(
client,
undefined,
process.platform,
{ 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();
}
});