import { chmod, mkdir, symlink, unlink } from "node:fs/promises"; import { join } from "node:path"; import { expect, it, vi } from "vitest"; import { effectiveClientServer } from "../../../src/application/ClientConfigurationDocument.js"; import { readClientRegistrationStatuses } from "../../../src/application/ClientRegistrationStatus.js"; import { clientProjectConfigurationPaths } from "../../../src/application/SupportedClientLocations.js"; import { supportedClients } from "../../../src/application/SupportedClients.js"; import { readProjectRegistration, writeProjectConfiguration as writeConfiguration, } from "../../fixtures/projectRegistration.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const server = (command: string, env?: Record) => ({ command, args: ["mcp"], ...(env === undefined ? {} : { env }), }); const fixture = async (marker: ".git" | ".jj" | "none" = ".git") => { const root = await createTestTempDirectory("rea-devin-project-"); const home = join(root, "home"); const repository = join(root, "repository"); const projectDirectory = join(repository, "child"); await mkdir(projectDirectory, { recursive: true }); if (marker !== "none") await mkdir(join(repository, marker), { recursive: true }); const client = supportedClients(home, process.platform, {}).find( ({ name }) => name === "devin", ); if (client === undefined) throw new Error("Devin is missing from the client table"); const devin = (directory: string, name: string) => join(directory, ".devin", name); return { client, home, repository, projectDirectory, shared: devin(projectDirectory, "mcp_config.json"), local: devin(projectDirectory, "mcp_config.local.json"), rootShared: devin(repository, "mcp_config.json"), rootLocal: devin(repository, "mcp_config.local.json"), read: () => readProjectRegistration(client, projectDirectory), }; }; it.each([".git", ".jj"] as const)( "orders Devin project files from the %s root, shared before local and ancestors before the working directory", async (marker) => { const { client, projectDirectory, shared, local, rootShared, rootLocal } = await fixture(marker); expect( clientProjectConfigurationPaths( client, projectDirectory, process.platform, ), ).toEqual({ ok: true, value: [rootShared, rootLocal, shared, local] }); }, ); it("reads only the working directory's Devin files outside a project", async () => { const { client, projectDirectory, shared, local } = await fixture("none"); expect( clientProjectConfigurationPaths(client, projectDirectory, process.platform), ).toEqual({ ok: true, value: [shared, local] }); }); it("selects the highest Devin file and replaces the whole server definition (#15041)", async () => { const { client, shared, local, rootLocal, read } = await fixture(); await writeConfiguration(client.configPath, { mcpServers: { rea: server("user", { FROM_USER: "1" }) }, }); const userOnly = await read(); if (!userOnly.ok) throw Error(userOnly.error.detail); expect(userOnly.value.configPath).toBe(client.configPath); await writeConfiguration(shared, { mcpServers: { rea: server("shared") } }); const sharedResult = await read(); if (!sharedResult.ok) throw Error(sharedResult.error.detail); expect(sharedResult.value).toMatchObject({ scope: "project", configPath: shared, launcherConfigPath: shared, }); expect(effectiveClientServer(sharedResult.value.document!, "rea")).toEqual( server("shared"), ); // The nested shared file outranks the repository root’s local file. await writeConfiguration(rootLocal, { mcpServers: { rea: server("root-local") }, }); const rootLocalResult = await read(); if (!rootLocalResult.ok) throw Error(rootLocalResult.error.detail); expect(rootLocalResult.value.configPath).toBe(shared); await unlink(shared); const rootOnly = await read(); if (!rootOnly.ok) throw Error(rootOnly.error.detail); expect(rootOnly.value.configPath).toBe(rootLocal); await writeConfiguration(local, { mcpServers: { rea: server("local") } }); const localResult = await read(); if (!localResult.ok) throw Error(localResult.error.detail); expect(localResult.value).toMatchObject({ scope: "project", configPath: local, }); expect(effectiveClientServer(localResult.value.document!, "rea")).toEqual( server("local"), ); await unlink(local); await unlink(rootLocal); const restored = await read(); if (!restored.ok) throw Error(restored.error.detail); expect(restored.value.configPath).toBe(client.configPath); }); it("keeps the user Devin registration when project files define only other servers", async () => { const { client, shared, read } = await fixture(); await writeConfiguration(client.configPath, { mcpServers: { rea: server("user") }, }); await writeConfiguration(shared, { mcpServers: { peer: server("peer") } }); const result = await read(); if (!result.ok) throw Error(result.error.detail); expect(result.value).toMatchObject({ scope: "user", configPath: client.configPath, }); expect(effectiveClientServer(result.value.document!, "rea")).toEqual( server("user"), ); }); it("reports Devin's project registration and Windsurf's shared user registration in doctor", async () => { const { client, home, projectDirectory, local } = await fixture(); await writeConfiguration(client.configPath, { mcpServers: { rea: server("user") }, }); await writeConfiguration(local, { mcpServers: { rea: server("unrelated") } }); const cwd = vi.spyOn(process, "cwd").mockReturnValue(projectDirectory); try { const statuses = await readClientRegistrationStatuses(home, undefined, { platform: process.platform, environment: {}, selectedClientIds: ["devin", "windsurf"], }); expect(statuses.find(({ client: name }) => name === "devin")).toMatchObject( { scope: "project", config_path: local }, ); expect( statuses.find(({ client: name }) => name === "windsurf"), ).toMatchObject({ config_path: client.configPath, command: ["user", "mcp"], }); } finally { cwd.mockRestore(); } }); it.skipIf(process.platform === "win32")( "replays a user file reached through a higher Devin project alias", async () => { const { client, shared, local, read } = await fixture(); await writeConfiguration(client.configPath, { mcpServers: { rea: server("user") }, }); await writeConfiguration(shared, { mcpServers: { rea: server("shared") } }); await symlink(client.configPath, local); const result = await read(); if (!result.ok) throw Error(result.error.detail); expect(result.value).toMatchObject({ scope: "project", launcherConfigPath: local, }); expect(effectiveClientServer(result.value.document!, "rea")).toEqual( server("user"), ); }, ); it.skipIf(process.platform === "win32" || process.getuid?.() === 0)( "preserves an unreadable Devin project boundary instead of treating it as absent", async () => { const { client, projectDirectory } = await fixture(); await chmod(projectDirectory, 0o000); try { expect( clientProjectConfigurationPaths( client, projectDirectory, process.platform, ), ).toMatchObject({ ok: false, error: { path: join(projectDirectory, ".git") }, }); } finally { await chmod(projectDirectory, 0o700); } }, );