|
| 1 | +from types import SimpleNamespace |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from models.processors import TaskProcessor |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.asyncio |
| 9 | +async def test_standard_processor_uses_admin_client_for_embedding_mapping( |
| 10 | + tmp_path, |
| 11 | + monkeypatch, |
| 12 | +): |
| 13 | + user_client = SimpleNamespace( |
| 14 | + exists_calls=[], |
| 15 | + index_calls=[], |
| 16 | + ) |
| 17 | + admin_client = object() |
| 18 | + mapping_clients = [] |
| 19 | + |
| 20 | + async def exists(*, index, id): |
| 21 | + user_client.exists_calls.append({"index": index, "id": id}) |
| 22 | + return False |
| 23 | + |
| 24 | + async def index(**kwargs): |
| 25 | + user_client.index_calls.append(kwargs) |
| 26 | + |
| 27 | + user_client.exists = exists |
| 28 | + user_client.index = index |
| 29 | + |
| 30 | + class SessionManager: |
| 31 | + def get_user_opensearch_client(self, user_id, jwt_token): |
| 32 | + assert user_id == "user-1" |
| 33 | + assert jwt_token == "Bearer user-token" |
| 34 | + return user_client |
| 35 | + |
| 36 | + class ModelsService: |
| 37 | + async def get_litellm_model_name(self, embedding_model): |
| 38 | + return embedding_model |
| 39 | + |
| 40 | + class EmbeddingClient: |
| 41 | + class Embeddings: |
| 42 | + async def create(self, model, input): |
| 43 | + return SimpleNamespace( |
| 44 | + data=[ |
| 45 | + SimpleNamespace(embedding=[0.1, 0.2, 0.3]) |
| 46 | + for _ in input |
| 47 | + ] |
| 48 | + ) |
| 49 | + |
| 50 | + embeddings = Embeddings() |
| 51 | + |
| 52 | + async def ensure_embedding_field_exists(client, model_name, index_name, dimensions): |
| 53 | + mapping_clients.append(client) |
| 54 | + assert model_name == "text-embedding-3-small" |
| 55 | + assert index_name == "documents" |
| 56 | + assert dimensions == 3 |
| 57 | + return "chunk_embedding_text_embedding_3_small" |
| 58 | + |
| 59 | + monkeypatch.setattr( |
| 60 | + "config.settings.clients", |
| 61 | + SimpleNamespace( |
| 62 | + opensearch=admin_client, |
| 63 | + patched_embedding_client=EmbeddingClient(), |
| 64 | + ), |
| 65 | + ) |
| 66 | + monkeypatch.setattr("config.settings.get_index_name", lambda: "documents") |
| 67 | + monkeypatch.setattr( |
| 68 | + "config.settings.get_openrag_config", |
| 69 | + lambda: SimpleNamespace(knowledge=SimpleNamespace(embedding_model="")), |
| 70 | + ) |
| 71 | + monkeypatch.setattr( |
| 72 | + "utils.embedding_fields.ensure_embedding_field_exists", |
| 73 | + ensure_embedding_field_exists, |
| 74 | + ) |
| 75 | + |
| 76 | + file_path = tmp_path / "doc.md" |
| 77 | + file_path.write_text("# Test\n\nhello world", encoding="utf-8") |
| 78 | + document_service = SimpleNamespace(session_manager=SessionManager()) |
| 79 | + processor = TaskProcessor( |
| 80 | + document_service=document_service, |
| 81 | + models_service=ModelsService(), |
| 82 | + docling_service=None, |
| 83 | + ) |
| 84 | + |
| 85 | + result = await processor.process_document_standard( |
| 86 | + file_path=str(file_path), |
| 87 | + file_hash="file-1", |
| 88 | + owner_user_id="user-1", |
| 89 | + original_filename="doc.md", |
| 90 | + jwt_token="Bearer user-token", |
| 91 | + embedding_model="text-embedding-3-small", |
| 92 | + ) |
| 93 | + |
| 94 | + assert result == {"status": "indexed", "id": "file-1"} |
| 95 | + assert mapping_clients == [admin_client] |
| 96 | + assert user_client.exists_calls == [{"index": "documents", "id": "file-1"}] |
| 97 | + assert user_client.index_calls |
0 commit comments