@@ -320,12 +320,42 @@ suite("Roo Code MCP OAuth", function () {
320320 console . log ( "[TEST] MCP OAuth flow completed successfully. Endpoints hit:" , [ ...endpointsHit ] )
321321 } )
322322
323+ // Ensure a valid token is stored in SecretStorage. Uses timeout: 45 (not the
324+ // McpHub default of 60) so this write always constitutes a config change that
325+ // triggers a reconnect, regardless of what previous tests may have set.
326+ // Waits for mcp-authed without requiring mcp-401: if no token is cached the
327+ // full OAuth flow runs; if a token is already stored it is reused directly.
328+ async function ensureOAuthTokenCached ( ) {
329+ const workspaceDir = vscode . workspace . workspaceFolders ?. [ 0 ] ?. uri . fsPath || tempDir
330+ const mcpConfigPath = path . join ( workspaceDir , ".roo" , "mcp.json" )
331+
332+ await fs . writeFile (
333+ mcpConfigPath ,
334+ JSON . stringify (
335+ {
336+ mcpServers : {
337+ "test-oauth-server" : {
338+ type : "streamable-http" ,
339+ url : `http://localhost:${ mockServerPort } /mcp` ,
340+ timeout : 45 ,
341+ } ,
342+ } ,
343+ } ,
344+ null ,
345+ 2 ,
346+ ) ,
347+ )
348+
349+ await waitFor ( ( ) => endpointsHit . has ( "mcp-authed" ) , { timeout : 45_000 } )
350+ }
351+
323352 test ( "Should reuse stored token on reconnect without re-running the full OAuth flow" , async function ( ) {
324- // Test 1 stored a valid token in SecretStorage. The per-test setup already
325- // cleared endpointsHit, so any hits here are from this reconnect only.
326353 const workspaceDir = vscode . workspace . workspaceFolders ?. [ 0 ] ?. uri . fsPath || tempDir
327354 const mcpConfigPath = path . join ( workspaceDir , ".roo" , "mcp.json" )
328355
356+ await ensureOAuthTokenCached ( )
357+ endpointsHit . clear ( )
358+
329359 // Slightly modify the config to force a reconnect
330360 await fs . writeFile (
331361 mcpConfigPath ,
@@ -350,9 +380,9 @@ suite("Roo Code MCP OAuth", function () {
350380 console . log ( "[TEST] Token reuse: MCP server got authenticated request" )
351381
352382 // The full OAuth flow should NOT have re-run (token was cached in SecretStorage)
353- assert . ok ( endpointsHit . has ( "mcp-authed" ) , "Reconnect should use cached token" )
354383 assert . ok ( ! endpointsHit . has ( "mcp-401" ) , "Should not get 401 when token is cached" )
355384 assert . ok ( ! endpointsHit . has ( "register" ) , "Should not re-register client when token is cached" )
385+ assert . ok ( ! endpointsHit . has ( "token" ) , "Should not re-exchange token when token is cached" )
356386
357387 console . log ( "[TEST] Token reuse test passed. Endpoints hit:" , [ ...endpointsHit ] )
358388 } )
0 commit comments