Skip to content

Commit fec5f66

Browse files
committed
test(mcp): restore os.homedir spy after each test to prevent cross-file leak
The `beforeEach` in `test/mcp/lifecycle.test.ts` installs a `spyOn(os, "homedir")` that returns a fresh `mkdtempSync(... "mcp-lifecycle-home-...")` on every call. Without a matching restore, the spy persists past this file into subsequent files in the same bun worker process. Downstream tests that call `os.homedir()` then get a different random path on every invocation — most visibly the tilde and `$HOME` expansion assertions in `test/permission/next.test.ts`, which see two different `mkdtempSync` paths within the same `expect().toEqual()` call and fail 6 times. The failure was file-order dependent (passes in isolation, fails when the full suite runs in the wrong order), which manifested as a required `CI/TypeScript` check failing intermittently on PRs — including v0.9.3. Fix: add `afterEach(() => mock.restore())` in `lifecycle.test.ts` so the spy is torn down between tests and cannot leak past the file boundary. Blanket `mock.restore()` matches the pattern already used elsewhere in the suite and cleans up any other spies installed during a test. Closes #1042
1 parent a441d17 commit fec5f66

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

‎packages/opencode/test/mcp/lifecycle.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from "node:path"
22
import { mkdtempSync } from "node:fs"
33
import os, { tmpdir } from "node:os"
44
import { pathToFileURL } from "node:url"
5-
import { expect, mock, beforeEach, spyOn } from "bun:test"
5+
import { expect, mock, beforeEach, afterEach, spyOn } from "bun:test"
66
import { ListRootsRequestSchema, ToolListChangedNotificationSchema } from "@modelcontextprotocol/sdk/types.js"
77
import { Cause, Effect, Exit } from "effect"
88
import type { MCP as MCPNS } from "../../src/mcp/index"
@@ -257,6 +257,16 @@ beforeEach(() => {
257257
transportCloseCount = 0
258258
})
259259

260+
// Restore all spies (including the `os.homedir` spy installed above) after
261+
// every test. Without this the spy persists past this file and pollutes any
262+
// downstream test in the same worker that calls `os.homedir()` — most
263+
// visibly the tilde/`$HOME`-expansion assertions in
264+
// `test/permission/next.test.ts`, which then see two different random
265+
// `mkdtempSync` paths within the same expect(). See issue #1042.
266+
afterEach(() => {
267+
mock.restore()
268+
})
269+
260270
// Import after mocks
261271
const { MCP } = await import("../../src/mcp/index")
262272
const { McpOAuthCallback } = await import("../../src/mcp/oauth-callback")

0 commit comments

Comments
 (0)