test: isolate user-level MCP config test from real homedir

This commit is contained in:
YeonGyu-Kim 2026-02-07 20:06:58 +09:00
parent 1760367a25
commit cf29cd137e

View File

@ -1,7 +1,7 @@
import { describe, it, expect, beforeEach, afterEach } from "bun:test" import { describe, it, expect, beforeEach, afterEach, mock } from "bun:test"
import { mkdirSync, writeFileSync, rmSync } from "fs" import { mkdirSync, writeFileSync, rmSync } from "fs"
import { join } from "path" import { join } from "path"
import { tmpdir, homedir } from "os" import { tmpdir } from "os"
const TEST_DIR = join(tmpdir(), "mcp-loader-test-" + Date.now()) const TEST_DIR = join(tmpdir(), "mcp-loader-test-" + Date.now())
@ -160,36 +160,36 @@ describe("getSystemMcpServerNames", () => {
} }
}) })
it("reads user-level MCP config from ~/.claude.json", async () => { it("reads user-level MCP config from ~/.claude.json", async () => {
// given // given
const userConfigPath = join(homedir(), ".claude.json") const userConfigPath = join(TEST_DIR, ".claude.json")
const userMcpConfig = { const userMcpConfig = {
mcpServers: { mcpServers: {
"user-server": { "user-server": {
command: "npx", command: "npx",
args: ["user-mcp-server"], args: ["user-mcp-server"],
}, },
}, },
} }
// Create user config if it doesn't exist const originalCwd = process.cwd()
const originalCwd = process.cwd() process.chdir(TEST_DIR)
process.chdir(TEST_DIR)
try { try {
// Write user config temporarily mock.module("os", () => ({
writeFileSync(userConfigPath, JSON.stringify(userMcpConfig)) homedir: () => TEST_DIR,
tmpdir,
}))
// when writeFileSync(userConfigPath, JSON.stringify(userMcpConfig))
const { getSystemMcpServerNames } = await import("./loader")
const names = getSystemMcpServerNames()
// then const { getSystemMcpServerNames } = await import("./loader")
expect(names.has("user-server")).toBe(true) const names = getSystemMcpServerNames()
} finally {
process.chdir(originalCwd) expect(names.has("user-server")).toBe(true)
// Clean up user config } finally {
rmSync(userConfigPath, { force: true }) process.chdir(originalCwd)
} rmSync(userConfigPath, { force: true })
}) }
})
}) })