fix(mcp): Fix discover_tools test mock and credential auto-unselect

- Add missing `refresh_if_needed` mock to test_discover_tools_auto_uses_stored_credential
  so it returns the stored credential instead of a MagicMock
- Fix credential auto-unselect clearing MCP credentials on initial render:
  skip the "unselect if not available" check when the saved credentials
  list is empty (empty list means not loaded yet, not invalid)
This commit is contained in:
Zamil Majdy
2026-02-10 12:55:35 +04:00
parent 5e2ae3cec5
commit 8a2f98b23c
2 changed files with 5 additions and 1 deletions

View File

@@ -108,6 +108,7 @@ class TestDiscoverTools:
patch("backend.api.features.mcp.routes.creds_manager") as mock_cm,
):
mock_cm.store.get_creds_by_provider = AsyncMock(return_value=[stored_cred])
mock_cm.refresh_if_needed = AsyncMock(return_value=stored_cred)
instance = MockClient.return_value
instance.initialize = AsyncMock(
return_value={"serverInfo": {}, "protocolVersion": "2025-03-26"}

View File

@@ -88,11 +88,14 @@ export function useCredentialsInput({
}
}, [credentials, onLoaded]);
// Unselect credential if not available
// Unselect credential if not available in the loaded credential list.
// Skip when no credentials have been loaded yet (empty list could mean
// the provider data hasn't finished loading, not that the credential is invalid).
useEffect(() => {
if (readOnly) return;
if (!credentials || !("savedCredentials" in credentials)) return;
const availableCreds = credentials.savedCredentials;
if (availableCreds.length === 0) return;
if (
selectedCredential &&
!availableCreds.some((c) => c.id === selectedCredential.id)