Skip to content

Commit 2b6cba4

Browse files
Fix flaky OAuth callback test (#634)
* Add missing properties to OAuth test mocks Tests timed out because mocks lacked url property and establishConnection method, causing real HTTP requests. * Extract helper for OAuth test mock creation
1 parent 6ddabb7 commit 2b6cba4

File tree

1 file changed

+45
-42
lines changed

1 file changed

+45
-42
lines changed

packages/agents/src/tests/worker.ts

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,20 @@ export class TestOAuthAgent extends Agent<Env> {
199199
this.mcp.configureOAuthCallback(config);
200200
}
201201

202-
async setupMockMcpConnection(
202+
private createMockMcpConnection(
203203
serverId: string,
204-
_serverName: string,
205-
_serverUrl: string,
206-
callbackUrl: string
207-
): Promise<void> {
208-
// Register the callback URL in memory (simulates non-hibernated state)
209-
this.mcp.registerCallbackUrl(`${callbackUrl}/${serverId}`);
210-
211-
// Create a mock connection object in mcpConnections to fully simulate non-hibernated state
212-
// This prevents _handlePotentialOAuthCallback from trying to restore the connection
213-
this.mcp.mcpConnections[serverId] = {
214-
connectionState: "ready",
204+
serverUrl: string,
205+
connectionState: "ready" | "authenticating" | "connecting" = "ready"
206+
): MCPClientConnection {
207+
return {
208+
url: new URL(serverUrl),
209+
connectionState,
215210
tools: [],
216211
resources: [],
217212
prompts: [],
213+
resourceTemplates: [],
214+
serverCapabilities: undefined,
215+
lastConnectedTransport: undefined,
218216
options: {
219217
transport: {
220218
authProvider: {
@@ -224,49 +222,54 @@ export class TestOAuthAgent extends Agent<Env> {
224222
}
225223
},
226224
completeAuthorization: async (_code: string) => {
227-
// Mock successful authorization
225+
this.mcp.mcpConnections[serverId].connectionState = "ready";
226+
},
227+
establishConnection: async () => {
228228
this.mcp.mcpConnections[serverId].connectionState = "ready";
229229
}
230230
} as unknown as MCPClientConnection;
231231
}
232232

233+
async setupMockMcpConnection(
234+
serverId: string,
235+
_serverName: string,
236+
serverUrl: string,
237+
callbackUrl: string
238+
): Promise<void> {
239+
this.mcp.registerCallbackUrl(`${callbackUrl}/${serverId}`);
240+
this.mcp.mcpConnections[serverId] = this.createMockMcpConnection(
241+
serverId,
242+
serverUrl,
243+
"ready"
244+
);
245+
}
246+
233247
async setupMockOAuthState(
234248
serverId: string,
235249
_code: string,
236250
_state: string,
237251
options?: { createConnection?: boolean }
238252
): Promise<void> {
239-
// Set up connection in authenticating state so OAuth callback can be processed
240-
241-
// If requested, pre-create a connection in authenticating state
242-
// This is needed for non-hibernation tests where the connection already exists
243253
if (options?.createConnection) {
244-
this.mcp.mcpConnections[serverId] = {
245-
connectionState: "authenticating",
246-
tools: [],
247-
resources: [],
248-
prompts: [],
249-
options: {
250-
transport: {
251-
authProvider: {
252-
clientId: "test-client-id",
253-
authUrl: "http://example.com/oauth/authorize"
254-
}
255-
}
256-
},
257-
completeAuthorization: async (_code: string) => {
258-
// Mock successful authorization
259-
this.mcp.mcpConnections[serverId].connectionState = "ready";
260-
}
261-
} as unknown as MCPClientConnection;
254+
const server = this.getMcpServerFromDb(serverId);
255+
if (!server) {
256+
throw new Error(
257+
`Test error: Server ${serverId} not found in DB. Set up DB record before calling setupMockOAuthState.`
258+
);
259+
}
260+
261+
this.mcp.mcpConnections[serverId] = this.createMockMcpConnection(
262+
serverId,
263+
server.server_url,
264+
"authenticating"
265+
);
262266
} else if (this.mcp.mcpConnections[serverId]) {
263-
// Set existing connection state to "authenticating" and mock completeAuthorization
264-
// so the callback can be processed
265-
this.mcp.mcpConnections[serverId].connectionState = "authenticating";
266-
this.mcp.mcpConnections[serverId].completeAuthorization = async (
267-
_code: string
268-
) => {
269-
// Mock successful authorization
267+
const conn = this.mcp.mcpConnections[serverId];
268+
conn.connectionState = "authenticating";
269+
conn.completeAuthorization = async (_code: string) => {
270+
this.mcp.mcpConnections[serverId].connectionState = "ready";
271+
};
272+
conn.establishConnection = async () => {
270273
this.mcp.mcpConnections[serverId].connectionState = "ready";
271274
};
272275
}

0 commit comments

Comments
 (0)