import { test, expect, describe, beforeEach, afterEach } from "bun:test"; import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; /** * Capability probing decides what the input box promises users, so the rule it * has to get right is the conservative one: a server DECLARED in .mcp.json but * not approved for that project does not load, and must not be advertised. * * The module reads AI_WORKSPACE once per call through a 60s cache, so each case * points it at a fresh temp workspace and re-imports with a cache-busting query. */ let workspace = ""; let home = ""; /** * Every host-derived input the probe consults, saved once so each case can * neutralise them and afterEach can put them back. * * $HOME was always here: the probe reads ~/.claude.json for user-scope servers, * so a developer running one of these MCP servers would otherwise fail the * suite. The eddy capabilities arrived later and read the SAME kind of host * fact through different doors — `eddy-safety-v1` from `Bun.which` (i.e. $PATH), * `eddy-runtime` and `eddy-devicesim-v1` from two env vars — and nothing * neutralised those, so all ten cases went red on any machine that happens to * have the eddy agent binary installed. Same bug as the original $HOME one, * one layer out. * * Note this asserts nothing about whether those capabilities SHOULD be * advertised unconditionally — that is their feature's call. It only restores * the property this file is built on: what comes back depends on the config * under test, not on who is running it. */ const HOST_ENV = ["HOME", "CLAUDE_CONFIG_DIR", "EDDY_HOME", "EDDY_DEVICESIM_REPO", "EDDY_REVIEW_AGENT_BIN"] as const; const realEnv = Object.fromEntries(HOST_ENV.map((k) => [k, process.env[k]])); async function probe(claudeConfigDir?: string): Promise { process.env.AI_WORKSPACE = workspace; process.env.HOME = home; if (claudeConfigDir) process.env.CLAUDE_CONFIG_DIR = claudeConfigDir; else delete process.env.CLAUDE_CONFIG_DIR; delete process.env.EDDY_HOME; delete process.env.EDDY_DEVICESIM_REPO; // `eddy-safety-v1` is probed with `Bun.which(eddyAgent("review").binary)`, and // that binary defaults to plain "claude" — so it is advertised on every machine // with Claude Code installed, which is every machine that runs this suite. // // It has to be neutralised through eddyAgent's own override rather than by // emptying $PATH: `Bun.which` reads the PATH the PROCESS started with and does // not see a runtime `process.env.PATH = ""` (verified — it still resolved // ~/.local/bin/claude). Pointing the override at a name nothing can resolve is // the supported door, and it states the case plainly: this node has no eddy // review agent. process.env.EDDY_REVIEW_AGENT_BIN = "caps-test-no-such-binary"; const mod = await import(`../src/capabilities?${Math.random()}`); return mod.nodeCapabilities(); } function write(rel: string, body: unknown): void { const file = path.join(workspace, rel); mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, JSON.stringify(body)); } beforeEach(() => { workspace = mkdtempSync(path.join(tmpdir(), "caps-")); home = mkdtempSync(path.join(tmpdir(), "caps-home-")); }); afterEach(() => { rmSync(workspace, { recursive: true, force: true }); rmSync(home, { recursive: true, force: true }); for (const k of HOST_ENV) { if (realEnv[k] === undefined) delete process.env[k]; else process.env[k] = realEnv[k]; } }); describe("nodeCapabilities", () => { test("no config at all → advertises nothing, and does not throw", async () => { expect(await probe()).toEqual([]); }); test("declared AND approved → advertised", async () => { write(".mcp.json", { mcpServers: { "core-ai": {}, figma: {}, actl: {} } }); write(".claude/settings.local.json", { enabledMcpjsonServers: ["core-ai", "figma", "actl"] }); expect(await probe()).toEqual(["figma", "lark-doc"]); }); test("declared but never approved → NOT advertised (it would not load)", async () => { write(".mcp.json", { mcpServers: { "core-ai": {}, figma: {} } }); write(".claude/settings.local.json", { enabledMcpjsonServers: [] }); expect(await probe()).toEqual([]); }); test("enableAllProjectMcpServers approves everything declared", async () => { write(".mcp.json", { mcpServers: { figma: {} } }); write(".claude/settings.json", { enableAllProjectMcpServers: true }); expect(await probe()).toEqual(["figma"]); }); test("an explicit disable beats an enable", async () => { write(".mcp.json", { mcpServers: { "core-ai": {}, figma: {} } }); write(".claude/settings.local.json", { enabledMcpjsonServers: ["core-ai", "figma"], disabledMcpjsonServers: ["figma"], }); expect(await probe()).toEqual(["lark-doc"]); }); test("malformed json is treated as no information, not as a crash", async () => { mkdirSync(workspace, { recursive: true }); writeFileSync(path.join(workspace, ".mcp.json"), "{ not json"); expect(await probe()).toEqual([]); }); test("unknown servers map to no capability", async () => { write(".mcp.json", { mcpServers: { actl: {}, "some-other": {} } }); write(".claude/settings.local.json", { enabledMcpjsonServers: ["actl", "some-other"] }); expect(await probe()).toEqual([]); }); /* * Reading a Lark doc has several blessed paths in this org, and a probe that * knew only about the project's own .mcp.json would report "cannot read Lark * docs" on a machine that reads them all day — the same false-absence the pool * aggregation had to be fixed for. */ test("an ENABLED plugin's MCP server counts, with no project approval needed", () => { // plugin dirs live under $HOME, which probe() redirects const dir = path.join(home, ".claude/plugins/marketplaces/mp/plugins/im-tools"); mkdirSync(dir, { recursive: true }); writeFileSync(path.join(dir, ".mcp.json"), JSON.stringify({ mcpServers: { "lark-docs": {} } })); mkdirSync(path.join(home, ".claude"), { recursive: true }); writeFileSync( path.join(home, ".claude/settings.json"), JSON.stringify({ enabledPlugins: { "im-tools@mp": true } }), ); return probe().then((caps) => expect(caps).toEqual(["lark-doc"])); }); test("a plugin that is NOT enabled contributes nothing", async () => { const dir = path.join(home, ".claude/plugins/marketplaces/mp/plugins/im-tools"); mkdirSync(dir, { recursive: true }); writeFileSync(path.join(dir, ".mcp.json"), JSON.stringify({ mcpServers: { "lark-docs": {} } })); expect(await probe()).toEqual([]); }); test("the lark SKILL needs its plugin enabled AND its credentials present", async () => { const dir = path.join(home, ".claude/plugins/marketplaces/mp/plugins/im-tools"); mkdirSync(path.join(dir, "skills/lark"), { recursive: true }); mkdirSync(path.join(home, ".claude"), { recursive: true }); writeFileSync( path.join(home, ".claude/settings.json"), JSON.stringify({ enabledPlugins: { "im-tools@mp": true } }), ); // Enabled, but the skill's own prerequisite file is missing → not advertised. expect(await probe()).toEqual([]); mkdirSync(path.join(home, ".config"), { recursive: true }); writeFileSync(path.join(home, ".config/lark_auth_config.json"), "{}"); expect(await probe()).toEqual(["lark-doc"]); }); /* * A node that sets CLAUDE_CONFIG_DIR runs the agent on a config apart from the * operator's ~/.claude. The probe has to describe the agent's config: what sits * in $HOME is what the agent will NOT load. */ test("CLAUDE_CONFIG_DIR moves user-scope servers and plugins away from $HOME", async () => { const cfg = path.join(home, "agent-claude"); const writeAt = (file: string, body: unknown) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, JSON.stringify(body)); }; // The operator's own config: must be ignored. writeAt(path.join(home, ".claude.json"), { mcpServers: { "core-ai": {} } }); // The agent's config: .claude.json lives INSIDE the config dir. writeAt(path.join(cfg, ".claude.json"), { mcpServers: { figma: {} } }); writeAt(path.join(cfg, "settings.json"), { enabledPlugins: { "im-tools@mp": true } }); writeAt(path.join(cfg, "plugins/marketplaces/mp/plugins/im-tools/.mcp.json"), { mcpServers: { "lark-docs": {} } }); expect(await probe(cfg)).toEqual(["figma", "lark-doc"]); expect(await probe()).toEqual(["lark-doc"]); // without it, $HOME's core-ai is what loads }); });