import os
import sys
from unittest.mock import MagicMock, patch

# Ensure project root is in path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))

# Import the necessary functions from ai_client, including the reset helper
from src.ai_client import get_gemini_cache_stats, reset_session

def test_get_gemini_cache_stats_with_mock_client() -> None:
 """
 
  
      Test that get_gemini_cache_stats correctly processes cache lists
      from a mocked client instance.
 """
 # Ensure a clean state before the test by resetting the session
 reset_session()
 # 1. Create a mock for the cache object that the client will return
 mock_cache = MagicMock()
 mock_cache.name = "cachedContents/test-cache"
 mock_cache.display_name = "Test Cache"
 mock_cache.model = "models/gemini-1.5-pro-001"
 mock_cache.size_bytes = 1024
 # 2. Create a mock for the client instance
 mock_client_instance = MagicMock()
 # Configure its `caches.list` method to return our mock cache
 mock_client_instance.caches.list.return_value = [mock_cache]
 # 3. Patch the Client constructor to return our mock instance
 #    This intercepts the `_ensure_gemini_client` call inside the function
 with patch('google.genai.Client', return_value=mock_client_instance) as mock_client_constructor:
 # 4. Call the function under test
  stats = get_gemini_cache_stats()
  # 5. Assert that the function behaved as expected
  # It should have constructed the client
  mock_client_constructor.assert_called_once()
  # It should have called the `list` method on the `caches` attribute
  mock_client_instance.caches.list.assert_called_once()
  # The returned stats dictionary should be correct
  assert "cache_count" in stats
  assert "total_size_bytes" in stats
  assert stats["cache_count"] == 1
  assert stats["total_size_bytes"] == 1024