Skip to content

Commit 7312199

Browse files
committed
test(McpHub): addressing flakey promises
1 parent ff9070e commit 7312199

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,7 @@ describe("McpHub", () => {
24392439

24402440
mockSecretStorage = {
24412441
getOAuthData: vi.fn().mockResolvedValue(null),
2442-
onDidChange: vi.fn().mockReturnValue(vi.fn()),
2442+
onDidChange: vi.fn().mockImplementation(() => vi.fn()),
24432443
}
24442444
;(mcpHub as any).secretStorage = mockSecretStorage
24452445

@@ -2585,11 +2585,9 @@ describe("McpHub", () => {
25852585
mockConnection,
25862586
)
25872587

2588-
// _initiateOAuthFlow awaits getOAuthData() before calling withProgress.
2589-
// Two ticks: tick 1 resolves getOAuthData, tick 2 runs the continuation
2590-
// that calls withProgress, setting capturedCancellationToken.
2591-
await Promise.resolve()
2592-
await Promise.resolve()
2588+
// Wait for withProgress to be invoked: getOAuthData must resolve first,
2589+
// then the continuation calls withProgress synchronously, setting capturedCancellationToken.
2590+
await vi.waitFor(() => expect(capturedCancellationToken).toBeDefined())
25932591
capturedCancellationToken._fire()
25942592

25952593
await flowPromise
@@ -2917,9 +2915,9 @@ describe("McpHub", () => {
29172915
mockConnection,
29182916
)
29192917

2920-
// Two ticks: getOAuthData resolves, then withProgress registers the watcher
2921-
await Promise.resolve()
2922-
await Promise.resolve()
2918+
// Flush microtasks: getOAuthData resolves, then withProgress fires synchronously
2919+
// (registering the watcher) — no tick-counting needed.
2920+
await vi.advanceTimersByTimeAsync(0)
29232921
expect((mcpHub as any)._oauthWatchers.size).toBe(1)
29242922
const firstEntry = (mcpHub as any)._oauthWatchers.get(`${serverName}:${source}`)
29252923

@@ -2933,10 +2931,11 @@ describe("McpHub", () => {
29332931
mockConnection,
29342932
)
29352933

2936-
await Promise.resolve()
2937-
await Promise.resolve()
2934+
await vi.advanceTimersByTimeAsync(0)
29382935
// Still exactly one watcher for this server key
29392936
expect((mcpHub as any)._oauthWatchers.size).toBe(1)
2937+
// The first watcher's unsubscribe must have been called before replacement
2938+
expect(firstEntry.unsubscribe).toHaveBeenCalledOnce()
29402939
// Watcher entry was replaced (second flow's entry, not first)
29412940
const secondEntry = (mcpHub as any)._oauthWatchers.get(`${serverName}:${source}`)
29422941
expect(secondEntry).not.toBe(firstEntry)

0 commit comments

Comments
 (0)