Skip to content

Commit 332253a

Browse files
committed
test(mcp): cover getMcpSettingsFilePath fallback branches
Codecov reported 2 patch lines (1 missing, 1 partial) in the safeWriteJson merge callback. Add the three remaining fallback cases: absent file (merge sees null), existing content without an mcpServers object, and mcpServers present but not an object - all must write the default stub. All changed lines and branches of the merge callback are now covered.
1 parent e11ef14 commit 332253a

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

‎src/services/mcp/__tests__/McpHub.spec.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,58 @@ describe("McpHub", () => {
260260
expect(writtenPath).toBe(settingsPath)
261261
expect(JSON.parse(writtenData as string)).toEqual(concurrentConfig)
262262
})
263+
264+
it("writes the default stub when no settings file exists yet", async () => {
265+
const settingsPath = path.join("/mock/settings/path", "mcp_settings.json")
266+
267+
// Existence check and the locked read both see an absent file.
268+
vi.mocked(fs.access).mockRejectedValueOnce(
269+
Object.assign(new Error("ENOENT: no such file or directory"), { code: "ENOENT" }),
270+
)
271+
vi.mocked(fs.readFile).mockRejectedValueOnce(
272+
Object.assign(new Error("ENOENT: no such file or directory"), { code: "ENOENT" }),
273+
)
274+
275+
const returnedPath = await mcpHub.getMcpSettingsFilePath()
276+
277+
expect(returnedPath).toBe(settingsPath)
278+
expect(fs.writeFile).toHaveBeenCalledTimes(1)
279+
const [writtenPath, writtenData] = vi.mocked(fs.writeFile).mock.calls[0]
280+
expect(writtenPath).toBe(settingsPath)
281+
expect(JSON.parse(writtenData as string)).toEqual({ mcpServers: {} })
282+
})
283+
284+
it("writes the default stub when the existing content has no mcpServers object", async () => {
285+
const settingsPath = path.join("/mock/settings/path", "mcp_settings.json")
286+
287+
// Existence check sees an absent file, but the locked read finds content
288+
// that does not carry a mcpServers object (e.g. a torn or foreign write).
289+
vi.mocked(fs.access).mockRejectedValueOnce(
290+
Object.assign(new Error("ENOENT: no such file or directory"), { code: "ENOENT" }),
291+
)
292+
vi.mocked(fs.readFile).mockResolvedValueOnce(JSON.stringify({ someOtherKey: true }))
293+
294+
await mcpHub.getMcpSettingsFilePath()
295+
296+
expect(fs.writeFile).toHaveBeenCalledTimes(1)
297+
const [, writtenData] = vi.mocked(fs.writeFile).mock.calls[0]
298+
expect(JSON.parse(writtenData as string)).toEqual({ mcpServers: {} })
299+
})
300+
301+
it("writes the default stub when the existing mcpServers value is not an object", async () => {
302+
const settingsPath = path.join("/mock/settings/path", "mcp_settings.json")
303+
304+
vi.mocked(fs.access).mockRejectedValueOnce(
305+
Object.assign(new Error("ENOENT: no such file or directory"), { code: "ENOENT" }),
306+
)
307+
vi.mocked(fs.readFile).mockResolvedValueOnce(JSON.stringify({ mcpServers: "corrupted" }))
308+
309+
await mcpHub.getMcpSettingsFilePath()
310+
311+
expect(fs.writeFile).toHaveBeenCalledTimes(1)
312+
const [, writtenData] = vi.mocked(fs.writeFile).mock.calls[0]
313+
expect(JSON.parse(writtenData as string)).toEqual({ mcpServers: {} })
314+
})
263315
})
264316

265317
describe("Discriminated union type handling", () => {

0 commit comments

Comments
 (0)