|
| 1 | +"""Tests for LiteLLMEmbeddingProvider. |
| 2 | +
|
| 3 | +Uses AST parsing and direct SDK mocking to avoid importing the full |
| 4 | +basic_memory dependency chain (logfire, alembic, etc.). |
| 5 | +""" |
| 6 | + |
| 7 | +import ast |
| 8 | +import sys |
| 9 | +import types |
| 10 | +from pathlib import Path |
| 11 | +from unittest.mock import AsyncMock, MagicMock |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +PROVIDER_PATH = ( |
| 16 | + Path(__file__).resolve().parents[2] |
| 17 | + / "src" |
| 18 | + / "basic_memory" |
| 19 | + / "repository" |
| 20 | + / "litellm_provider.py" |
| 21 | +) |
| 22 | +FACTORY_PATH = ( |
| 23 | + Path(__file__).resolve().parents[2] |
| 24 | + / "src" |
| 25 | + / "basic_memory" |
| 26 | + / "repository" |
| 27 | + / "embedding_provider_factory.py" |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +class TestLiteLLMProviderStructure: |
| 32 | + """Verify the provider file has the correct structure.""" |
| 33 | + |
| 34 | + def _parse(self): |
| 35 | + return ast.parse(PROVIDER_PATH.read_text()) |
| 36 | + |
| 37 | + def test_file_exists(self): |
| 38 | + assert PROVIDER_PATH.exists() |
| 39 | + |
| 40 | + def test_has_litellm_embedding_provider_class(self): |
| 41 | + tree = self._parse() |
| 42 | + classes = [n.name for n in ast.walk(tree) if isinstance(n, ast.ClassDef)] |
| 43 | + assert "LiteLLMEmbeddingProvider" in classes |
| 44 | + |
| 45 | + def test_has_embed_documents_method(self): |
| 46 | + tree = self._parse() |
| 47 | + for node in ast.walk(tree): |
| 48 | + if isinstance(node, ast.ClassDef) and node.name == "LiteLLMEmbeddingProvider": |
| 49 | + methods = [ |
| 50 | + n.name |
| 51 | + for n in node.body |
| 52 | + if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef)) |
| 53 | + ] |
| 54 | + assert "embed_documents" in methods |
| 55 | + assert "embed_query" in methods |
| 56 | + return |
| 57 | + pytest.fail("LiteLLMEmbeddingProvider class not found") |
| 58 | + |
| 59 | + def test_embed_documents_is_async(self): |
| 60 | + tree = self._parse() |
| 61 | + for node in ast.walk(tree): |
| 62 | + if isinstance(node, ast.ClassDef) and node.name == "LiteLLMEmbeddingProvider": |
| 63 | + for item in node.body: |
| 64 | + if isinstance(item, ast.AsyncFunctionDef) and item.name == "embed_documents": |
| 65 | + return |
| 66 | + pytest.fail("embed_documents is not async") |
| 67 | + |
| 68 | + def test_uses_drop_params_true(self): |
| 69 | + src = PROVIDER_PATH.read_text() |
| 70 | + assert "drop_params" in src |
| 71 | + |
| 72 | + def test_uses_litellm_aembedding(self): |
| 73 | + src = PROVIDER_PATH.read_text() |
| 74 | + assert "aembedding" in src |
| 75 | + |
| 76 | + def test_has_runtime_log_attrs(self): |
| 77 | + tree = self._parse() |
| 78 | + for node in ast.walk(tree): |
| 79 | + if isinstance(node, ast.ClassDef) and node.name == "LiteLLMEmbeddingProvider": |
| 80 | + methods = [ |
| 81 | + n.name |
| 82 | + for n in node.body |
| 83 | + if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef)) |
| 84 | + ] |
| 85 | + assert "runtime_log_attrs" in methods |
| 86 | + return |
| 87 | + |
| 88 | + def test_default_model_in_source(self): |
| 89 | + src = PROVIDER_PATH.read_text() |
| 90 | + assert "openai/text-embedding-3-small" in src |
| 91 | + |
| 92 | + |
| 93 | +class TestFactoryRegistration: |
| 94 | + """Verify the factory recognizes litellm as a provider.""" |
| 95 | + |
| 96 | + def test_litellm_branch_in_factory(self): |
| 97 | + src = FACTORY_PATH.read_text() |
| 98 | + assert 'provider_name == "litellm"' in src |
| 99 | + |
| 100 | + def test_imports_litellm_provider(self): |
| 101 | + src = FACTORY_PATH.read_text() |
| 102 | + assert "LiteLLMEmbeddingProvider" in src |
| 103 | + |
| 104 | + |
| 105 | +class TestLiteLLMSDKInteraction: |
| 106 | + """Test litellm SDK calls directly (no basic_memory deps needed).""" |
| 107 | + |
| 108 | + def test_aembedding_called_with_drop_params(self): |
| 109 | + fake = types.ModuleType("litellm") |
| 110 | + response = MagicMock() |
| 111 | + response.data = [{"index": 0, "embedding": [0.1, 0.2]}] |
| 112 | + fake.aembedding = AsyncMock(return_value=response) |
| 113 | + sys.modules["litellm"] = fake |
| 114 | + |
| 115 | + try: |
| 116 | + import asyncio |
| 117 | + |
| 118 | + async def run(): |
| 119 | + await fake.aembedding( |
| 120 | + model="openai/text-embedding-3-small", |
| 121 | + input=["hello"], |
| 122 | + drop_params=True, |
| 123 | + ) |
| 124 | + |
| 125 | + asyncio.run(run()) |
| 126 | + kwargs = fake.aembedding.call_args.kwargs |
| 127 | + assert kwargs["drop_params"] is True |
| 128 | + assert kwargs["model"] == "openai/text-embedding-3-small" |
| 129 | + finally: |
| 130 | + del sys.modules["litellm"] |
| 131 | + |
| 132 | + def test_aembedding_forwards_api_key(self): |
| 133 | + fake = types.ModuleType("litellm") |
| 134 | + response = MagicMock() |
| 135 | + response.data = [{"index": 0, "embedding": [0.1]}] |
| 136 | + fake.aembedding = AsyncMock(return_value=response) |
| 137 | + sys.modules["litellm"] = fake |
| 138 | + |
| 139 | + try: |
| 140 | + import asyncio |
| 141 | + |
| 142 | + async def run(): |
| 143 | + await fake.aembedding( |
| 144 | + model="openai/text-embedding-3-small", |
| 145 | + input=["hello"], |
| 146 | + api_key="sk-test", |
| 147 | + drop_params=True, |
| 148 | + ) |
| 149 | + |
| 150 | + asyncio.run(run()) |
| 151 | + assert fake.aembedding.call_args.kwargs["api_key"] == "sk-test" |
| 152 | + finally: |
| 153 | + del sys.modules["litellm"] |
| 154 | + |
| 155 | + def test_aembedding_response_has_vectors(self): |
| 156 | + fake = types.ModuleType("litellm") |
| 157 | + response = MagicMock() |
| 158 | + response.data = [ |
| 159 | + {"index": 0, "embedding": [0.1, 0.2, 0.3]}, |
| 160 | + {"index": 1, "embedding": [0.4, 0.5, 0.6]}, |
| 161 | + ] |
| 162 | + fake.aembedding = AsyncMock(return_value=response) |
| 163 | + sys.modules["litellm"] = fake |
| 164 | + |
| 165 | + try: |
| 166 | + import asyncio |
| 167 | + |
| 168 | + async def run(): |
| 169 | + resp = await fake.aembedding( |
| 170 | + model="openai/text-embedding-3-small", |
| 171 | + input=["hello", "world"], |
| 172 | + drop_params=True, |
| 173 | + ) |
| 174 | + return resp |
| 175 | + |
| 176 | + resp = asyncio.run(run()) |
| 177 | + assert len(resp.data) == 2 |
| 178 | + assert resp.data[0]["embedding"] == [0.1, 0.2, 0.3] |
| 179 | + assert resp.data[1]["embedding"] == [0.4, 0.5, 0.6] |
| 180 | + finally: |
| 181 | + del sys.modules["litellm"] |
0 commit comments