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()
process.chdir(TEST_DIR)
try {
// Write user config temporarily
writeFileSync(userConfigPath, JSON.stringify(userMcpConfig))
// when const originalCwd = process.cwd()
const { getSystemMcpServerNames } = await import("./loader") process.chdir(TEST_DIR)
const names = getSystemMcpServerNames()
// then try {
expect(names.has("user-server")).toBe(true) mock.module("os", () => ({
} finally { homedir: () => TEST_DIR,
process.chdir(originalCwd) tmpdir,
// Clean up user config }))
rmSync(userConfigPath, { force: true })
} writeFileSync(userConfigPath, JSON.stringify(userMcpConfig))
})
const { getSystemMcpServerNames } = await import("./loader")
const names = getSystemMcpServerNames()
expect(names.has("user-server")).toBe(true)
} finally {
process.chdir(originalCwd)
rmSync(userConfigPath, { force: true })
}
})
}) })