test(ci): avoid global module mock leaks

This commit is contained in:
YeonGyu-Kim
2026-05-15 17:54:59 +09:00
parent 8dcbccf063
commit b3b2da89c9
3 changed files with 39 additions and 70 deletions
@@ -1,30 +1,20 @@
import { afterAll, beforeEach, describe, expect, it, mock } from "bun:test"
import { describe, expect, it, mock, spyOn } from "bun:test"
import type { ClaudeCodeMcpServer } from "../claude-code-mcp-loader/types"
import type { OAuthTokenData } from "../mcp-oauth/storage"
import type { SkillMcpClientInfo, SkillMcpServerContext } from "./types"
import { unsafeTestValue } from "../../../test-support/unsafe-test-value"
import { SkillMcpManager } from "./manager"
import type { McpClient, SkillMcpClientInfo, SkillMcpServerContext } from "./types"
const mockGetOrCreateClient = mock(async () => {
throw new Error("not used")
})
type ManagerWithPrivateRetry = {
getOrCreateClientWithRetry: (info: SkillMcpClientInfo, config: ClaudeCodeMcpServer) => Promise<McpClient>
}
const mockGetOrCreateClientWithRetryImpl = mock(async () => ({
callTool: mock(async () => ({ content: [{ type: "text", text: "unused" }] })),
close: mock(async () => {}),
}))
type ManagerModule = typeof import("./manager")
async function importFreshManagerModule(): Promise<ManagerModule> {
mock.module("./connection", () => ({
getOrCreateClient: mockGetOrCreateClient,
getOrCreateClientWithRetryImpl: mockGetOrCreateClientWithRetryImpl,
}))
mock.module("../mcp-oauth/provider", () => ({
McpOAuthProvider: class MockMcpOAuthProvider {},
}))
return await import(new URL(`./manager.ts?oauth-retry-test=${Date.now()}-${Math.random()}`, import.meta.url).href)
function stubClientRetry(manager: SkillMcpManager, callTool: McpClient["callTool"]): void {
const client = unsafeTestValue<McpClient>({
callTool,
close: mock(async () => {}),
})
spyOn(unsafeTestValue<ManagerWithPrivateRetry>(manager), "getOrCreateClientWithRetry").mockResolvedValue(client)
}
function createInfo(): SkillMcpClientInfo {
@@ -46,19 +36,9 @@ function createContext(): SkillMcpServerContext {
}
}
afterAll(() => {
mock.restore()
})
describe("SkillMcpManager post-request OAuth retry", () => {
beforeEach(() => {
mockGetOrCreateClient.mockClear()
mockGetOrCreateClientWithRetryImpl.mockClear()
})
it("retries the operation after a 401 refresh succeeds", async () => {
// given
const { SkillMcpManager } = await importFreshManagerModule()
const refresh = mock(async () => ({ accessToken: "refreshed-token" } satisfies OAuthTokenData))
const manager = new SkillMcpManager({
createOAuthProvider: () => ({
@@ -74,7 +54,7 @@ describe("SkillMcpManager post-request OAuth retry", () => {
return { content: [{ type: "text", text: "success" }] }
})
mockGetOrCreateClientWithRetryImpl.mockResolvedValue({ callTool, close: mock(async () => {}) })
stubClientRetry(manager, callTool)
// when
const result = await manager.callTool(createInfo(), createContext(), "test-tool", {})
@@ -87,7 +67,6 @@ describe("SkillMcpManager post-request OAuth retry", () => {
it("retries the operation after a 403 refresh succeeds without step-up scope", async () => {
// given
const { SkillMcpManager } = await importFreshManagerModule()
const refresh = mock(async () => ({ accessToken: "refreshed-token" } satisfies OAuthTokenData))
const manager = new SkillMcpManager({
createOAuthProvider: () => ({
@@ -103,7 +82,7 @@ describe("SkillMcpManager post-request OAuth retry", () => {
return { content: [{ type: "text", text: "success" }] }
})
mockGetOrCreateClientWithRetryImpl.mockResolvedValue({ callTool, close: mock(async () => {}) })
stubClientRetry(manager, callTool)
// when
const result = await manager.callTool(createInfo(), createContext(), "test-tool", {})
@@ -116,7 +95,6 @@ describe("SkillMcpManager post-request OAuth retry", () => {
it("propagates the auth error without retry when refresh fails", async () => {
// given
const { SkillMcpManager } = await importFreshManagerModule()
const refresh = mock(async () => {
throw new Error("refresh failed")
})
@@ -130,7 +108,7 @@ describe("SkillMcpManager post-request OAuth retry", () => {
const callTool = mock(async () => {
throw new Error("401 Unauthorized")
})
mockGetOrCreateClientWithRetryImpl.mockResolvedValue({ callTool, close: mock(async () => {}) })
stubClientRetry(manager, callTool)
// when / then
await expect(manager.callTool(createInfo(), createContext(), "test-tool", {})).rejects.toThrow("401 Unauthorized")
@@ -140,7 +118,6 @@ describe("SkillMcpManager post-request OAuth retry", () => {
it("only attempts one refresh when the retried operation returns 401 again", async () => {
// given
const { SkillMcpManager } = await importFreshManagerModule()
const refresh = mock(async () => ({ accessToken: "refreshed-token" } satisfies OAuthTokenData))
const manager = new SkillMcpManager({
createOAuthProvider: () => ({
@@ -152,7 +129,7 @@ describe("SkillMcpManager post-request OAuth retry", () => {
const callTool = mock(async () => {
throw new Error("401 Unauthorized")
})
mockGetOrCreateClientWithRetryImpl.mockResolvedValue({ callTool, close: mock(async () => {}) })
stubClientRetry(manager, callTool)
// when / then
await expect(manager.callTool(createInfo(), createContext(), "test-tool", {})).rejects.toThrow("401 Unauthorized")
@@ -6,10 +6,6 @@ import type { OAuthProviderFactory, OAuthProviderLike } from "./types"
type OAuthHandlerModule = typeof import("./oauth-handler")
async function importFreshOAuthHandlerModule(): Promise<OAuthHandlerModule> {
mock.module("../mcp-oauth/provider", () => ({
McpOAuthProvider: class MockMcpOAuthProvider {},
}))
return await import(new URL(`./oauth-handler.ts?oauth-handler-test=${Date.now()}-${Math.random()}`, import.meta.url).href)
}