|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +from google.genai import types |
| 5 | + |
| 6 | +from fishjam.integrations.gemini import GeminiIntegration |
| 7 | +from fishjam.version import get_version |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def version(): |
| 12 | + return get_version() |
| 13 | + |
| 14 | + |
| 15 | +@patch("google.genai.Client") |
| 16 | +def test_create_client_passes_all_args(mock_client_cls: MagicMock, version: str): |
| 17 | + dummy_credentials = MagicMock() |
| 18 | + dummy_debug_config = MagicMock() |
| 19 | + |
| 20 | + GeminiIntegration.create_client( |
| 21 | + vertexai=True, |
| 22 | + api_key="test-key", |
| 23 | + credentials=dummy_credentials, |
| 24 | + project="my-project", |
| 25 | + location="us-central1", |
| 26 | + debug_config=dummy_debug_config, |
| 27 | + ) |
| 28 | + |
| 29 | + mock_client_cls.assert_called_once() |
| 30 | + |
| 31 | + kwargs = mock_client_cls.call_args.kwargs |
| 32 | + |
| 33 | + assert kwargs["vertexai"] is True |
| 34 | + assert kwargs["api_key"] == "test-key" |
| 35 | + assert kwargs["credentials"] is dummy_credentials |
| 36 | + assert kwargs["project"] == "my-project" |
| 37 | + assert kwargs["location"] == "us-central1" |
| 38 | + assert kwargs["debug_config"] is dummy_debug_config |
| 39 | + |
| 40 | + assert kwargs["http_options"] == { |
| 41 | + "headers": {"x-goog-api-client": f"fishjam-python-server-sdk/{version}"} |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | +@patch("google.genai.Client") |
| 46 | +def test_create_client_with_dict_options_no_headers( |
| 47 | + mock_client_cls: MagicMock, version: str |
| 48 | +): |
| 49 | + GeminiIntegration.create_client(http_options={"timeout": 30}) |
| 50 | + |
| 51 | + mock_client_cls.assert_called_once() |
| 52 | + |
| 53 | + assert mock_client_cls.call_args.kwargs["http_options"] == { |
| 54 | + "timeout": 30, |
| 55 | + "headers": {"x-goog-api-client": f"fishjam-python-server-sdk/{version}"}, |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +@patch("google.genai.Client") |
| 60 | +def test_create_client_with_dict_options_existing_headers( |
| 61 | + mock_client_cls: MagicMock, version: str |
| 62 | +): |
| 63 | + GeminiIntegration.create_client( |
| 64 | + http_options={ |
| 65 | + "headers": { |
| 66 | + "existing-header": "value", |
| 67 | + "x-goog-api-client": "other", |
| 68 | + } |
| 69 | + } |
| 70 | + ) |
| 71 | + |
| 72 | + mock_client_cls.assert_called_once() |
| 73 | + |
| 74 | + assert mock_client_cls.call_args.kwargs["http_options"] == { |
| 75 | + "headers": { |
| 76 | + "existing-header": "value", |
| 77 | + "x-goog-api-client": f"fishjam-python-server-sdk/{version}", |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | +@patch("google.genai.Client") |
| 83 | +def test_create_client_with_object_options(mock_client_cls: MagicMock, version: str): |
| 84 | + http_options = types.HttpOptions() |
| 85 | + |
| 86 | + GeminiIntegration.create_client(http_options=http_options) |
| 87 | + |
| 88 | + mock_client_cls.assert_called_once() |
| 89 | + |
| 90 | + # Verify the object passed has the correct headers set |
| 91 | + actual_options = mock_client_cls.call_args.kwargs["http_options"] |
| 92 | + assert actual_options.headers == { |
| 93 | + "x-goog-api-client": f"fishjam-python-server-sdk/{version}" |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | +@patch("google.genai.Client") |
| 98 | +def test_create_client_with_object_options_existing_headers( |
| 99 | + mock_client_cls: MagicMock, version: str |
| 100 | +): |
| 101 | + http_options = types.HttpOptions( |
| 102 | + headers={ |
| 103 | + "user-header": "123", |
| 104 | + "x-goog-api-client": "other", |
| 105 | + } |
| 106 | + ) |
| 107 | + |
| 108 | + GeminiIntegration.create_client(http_options=http_options) |
| 109 | + |
| 110 | + mock_client_cls.assert_called_once() |
| 111 | + |
| 112 | + actual_options = mock_client_cls.call_args.kwargs["http_options"] |
| 113 | + assert actual_options.headers == { |
| 114 | + "user-header": "123", |
| 115 | + "x-goog-api-client": f"fishjam-python-server-sdk/{version}", |
| 116 | + } |
0 commit comments