|
1 | | -from unittest.mock import MagicMock, patch |
| 1 | +from unittest.mock import AsyncMock, MagicMock, patch |
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
@@ -65,3 +65,121 @@ def test_sync_and_async_clients_merge_posthog_defaults_without_mutation(client_c |
65 | 65 | {"organization": "default-org"}, |
66 | 66 | ) |
67 | 67 | assert default_properties == {"shared": "default", "default-only": True} |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture |
| 71 | +def provider_client_class(): |
| 72 | + with patch.object(google_genai, "Client") as patched: |
| 73 | + patched.return_value = MagicMock() |
| 74 | + yield patched |
| 75 | + |
| 76 | + |
| 77 | +@pytest.fixture |
| 78 | +def provider_client(provider_client_class): |
| 79 | + """A stand-in for the underlying google-genai client, with both surfaces.""" |
| 80 | + return provider_client_class.return_value |
| 81 | + |
| 82 | + |
| 83 | +@pytest.fixture |
| 84 | +def gemini_response(): |
| 85 | + response = MagicMock() |
| 86 | + response.text = "Test response from Gemini" |
| 87 | + |
| 88 | + usage = MagicMock() |
| 89 | + usage.prompt_token_count = 20 |
| 90 | + usage.candidates_token_count = 10 |
| 91 | + usage.cached_content_token_count = 0 |
| 92 | + usage.thoughts_token_count = 0 |
| 93 | + response.usage_metadata = usage |
| 94 | + |
| 95 | + part = MagicMock() |
| 96 | + part.text = "Test response from Gemini" |
| 97 | + content = MagicMock() |
| 98 | + content.parts = [part] |
| 99 | + candidate = MagicMock() |
| 100 | + candidate.content = content |
| 101 | + response.candidates = [candidate] |
| 102 | + |
| 103 | + return response |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.parametrize("client_class", [Client, AsyncClient]) |
| 107 | +def test_every_surface_shares_one_provider_client( |
| 108 | + client_class, provider_client_class, provider_client |
| 109 | +): |
| 110 | + """`models`, `aio.models` and `files` must not open separate connections.""" |
| 111 | + client = client_class(api_key="test-key", posthog_client=MagicMock()) |
| 112 | + |
| 113 | + provider_client_class.assert_called_once() |
| 114 | + assert client.models._client is provider_client |
| 115 | + assert client.aio.models._client is provider_client |
| 116 | + |
| 117 | + |
| 118 | +def test_sync_client_exposes_the_provider_files_api(provider_client): |
| 119 | + client = Client(api_key="test-key", posthog_client=MagicMock()) |
| 120 | + |
| 121 | + assert client.files is provider_client.files |
| 122 | + assert client.aio.files is provider_client.aio.files |
| 123 | + |
| 124 | + uploaded = client.files.upload(file="notes.pdf") |
| 125 | + |
| 126 | + provider_client.files.upload.assert_called_once_with(file="notes.pdf") |
| 127 | + assert uploaded is provider_client.files.upload.return_value |
| 128 | + |
| 129 | + |
| 130 | +def test_async_client_exposes_the_async_files_api(provider_client): |
| 131 | + """AsyncClient is async end to end, so its files API is the aio one.""" |
| 132 | + client = AsyncClient(api_key="test-key", posthog_client=MagicMock()) |
| 133 | + |
| 134 | + assert client.files is provider_client.aio.files |
| 135 | + assert client.aio.files is provider_client.aio.files |
| 136 | + # `models` is already async; `aio.models` is an alias so SDK-shaped code works. |
| 137 | + assert client.aio.models is client.models |
| 138 | + |
| 139 | + |
| 140 | +def test_sync_client_aio_models_inherits_posthog_defaults(provider_client): |
| 141 | + client = Client( |
| 142 | + api_key="test-key", |
| 143 | + posthog_client=MagicMock(), |
| 144 | + posthog_distinct_id="default-id", |
| 145 | + posthog_properties={"team": "ai"}, |
| 146 | + posthog_privacy_mode=True, |
| 147 | + posthog_groups={"organization": "default-org"}, |
| 148 | + ) |
| 149 | + |
| 150 | + assert client.aio.models._merge_posthog_params(None, "trace", None, None, None) == ( |
| 151 | + "default-id", |
| 152 | + "trace", |
| 153 | + {"team": "ai"}, |
| 154 | + True, |
| 155 | + {"organization": "default-org"}, |
| 156 | + ) |
| 157 | + |
| 158 | + |
| 159 | +@pytest.mark.asyncio |
| 160 | +async def test_sync_client_aio_models_tracks_generations( |
| 161 | + provider_client, gemini_response |
| 162 | +): |
| 163 | + """`client.aio.models.generate_content` is the drop-in async entry point.""" |
| 164 | + provider_client.aio.models.generate_content = AsyncMock( |
| 165 | + return_value=gemini_response |
| 166 | + ) |
| 167 | + posthog_client = MagicMock() |
| 168 | + posthog_client.privacy_mode = False |
| 169 | + |
| 170 | + client = Client(api_key="test-key", posthog_client=posthog_client) |
| 171 | + |
| 172 | + response = await client.aio.models.generate_content( |
| 173 | + model="gemini-2.0-flash", |
| 174 | + contents=["Tell me a fun fact about hedgehogs"], |
| 175 | + posthog_distinct_id="test-id", |
| 176 | + ) |
| 177 | + |
| 178 | + assert response is gemini_response |
| 179 | + provider_client.aio.models.generate_content.assert_awaited_once() |
| 180 | + |
| 181 | + assert posthog_client.capture.call_count == 1 |
| 182 | + call_args = posthog_client.capture.call_args[1] |
| 183 | + assert call_args["distinct_id"] == "test-id" |
| 184 | + assert call_args["event"] == "$ai_generation" |
| 185 | + assert call_args["properties"]["$ai_model"] == "gemini-2.0-flash" |
0 commit comments