async def test_cache_persistence_across_manager_instances(
      self, credentials_config, mock_tool_context
  ):
    """Test that cached credentials persist across different manager instances.

    This tests the key benefit of the tool context caching - that
    credentials can be shared between different instances of the
    credential manager, avoiding redundant OAuth flows.
    """
    # Create first manager instance and simulate OAuth completion
    manager1 = BigQueryCredentialsManager(credentials_config)

    # Mock OAuth response for first manager
    mock_auth_response = Mock()
    mock_auth_response.oauth2.access_token = "cached_access_token"
    mock_auth_response.oauth2.refresh_token = "cached_refresh_token"
    mock_tool_context.get_auth_response.return_value = mock_auth_response

    # Create the mock credentials instance that will be returned by the constructor
    mock_creds = Mock(spec=Credentials)
    # Make sure our mock JSON matches the structure that real Credentials objects produce
    mock_creds_json = (
        '{"token": "cached_access_token", "refresh_token":'
        ' "cached_refresh_token", "token_uri":'
        ' "https://oauth2.googleapis.com/token", "client_id": "test_client_id",'
        ' "client_secret": "test_client_secret", "scopes":'
        ' ["https://www.googleapis.com/auth/calendar"], "universe_domain":'
        ' "googleapis.com", "account": ""}'
    )
    mock_creds.to_json.return_value = mock_creds_json
    mock_creds.valid = True

    # Use the correct module path - without the 'src.' prefix
    with patch(
        "google.adk.tools.bigquery.bigquery_credentials.Credentials",
        return_value=mock_creds,
    ) as mock_credentials_class:
      # Complete OAuth flow with first manager
      result1 = await manager1.get_valid_credentials(mock_tool_context)

      # Verify credentials were cached in tool context
      assert BIGQUERY_TOKEN_CACHE_KEY in mock_tool_context.state
      cached_creds_json = mock_tool_context.state[BIGQUERY_TOKEN_CACHE_KEY]
      assert cached_creds_json == mock_creds_json

    # Create second manager instance (simulating new request/session)
    manager2 = BigQueryCredentialsManager(credentials_config)
    credentials_config.credentials = None

    # Reset auth response to None (no new OAuth flow available)
    mock_tool_context.get_auth_response.return_value = None

    # Mock the from_authorized_user_info method for the second manager
    with patch(
        "google.adk.tools.bigquery.bigquery_credentials.Credentials.from_authorized_user_info"
    ) as mock_from_json:
      mock_cached_creds = Mock(spec=Credentials)
      mock_cached_creds.valid = True
      mock_from_json.return_value = mock_cached_creds

      # Get credentials with second manager
      result2 = await manager2.get_valid_credentials(mock_tool_context)

      # Verify second manager retrieved cached credentials successfully
      assert result2 == mock_cached_creds
      assert manager2.credentials_config.credentials is None
      assert (
          cached_creds_json == mock_tool_context.state[BIGQUERY_TOKEN_CACHE_KEY]
      )
      # The from_authorized_user_info should be called with the complete JSON structure
      mock_from_json.assert_called_once()
      # Extract the actual argument that was passed to verify it's the right JSON structure
      actual_json_arg = mock_from_json.call_args[0][0]
      # We need to parse and compare the structure rather than exact string match
      # since the order of keys in JSON might differ
      import json

      expected_data = json.loads(mock_creds_json)
      actual_data = (
          actual_json_arg
          if isinstance(actual_json_arg, dict)
          else json.loads(actual_json_arg)
      )
      assert actual_data == expected_data
Test that cached credentials persist across different manager instances.

    This tests the key benefit of the tool context caching - that
    credentials can be shared between different instances of the
    credential manager, avoiding redundant OAuth flows.
    
tests/unittests/tools/bigquery/test_bigquery_credentials_manager.py