|
1 | 1 | """Unit tests for the /rags REST API endpoints.""" |
2 | 2 |
|
| 3 | +from pathlib import Path |
| 4 | +from typing import Any |
| 5 | + |
3 | 6 | import pytest |
4 | 7 | from fastapi import HTTPException, Request, status |
5 | 8 | from llama_stack_client import APIConnectionError, BadRequestError |
6 | 9 | from pytest_mock import MockerFixture |
7 | 10 |
|
8 | 11 | from app.endpoints.rags import ( |
| 12 | + _resolve_rag_id_to_vector_db_id, |
9 | 13 | get_rag_endpoint_handler, |
10 | 14 | rags_endpoint_handler, |
11 | 15 | ) |
@@ -244,3 +248,146 @@ def __init__(self) -> None: |
244 | 248 | assert response.object == "faiss" |
245 | 249 | assert response.status == "completed" |
246 | 250 | assert response.usage_bytes == 100 |
| 251 | + |
| 252 | + |
| 253 | +def _make_byok_config(tmp_path: Any) -> AppConfig: |
| 254 | + """Create an AppConfig with BYOK RAG entries for testing.""" |
| 255 | + db_file = Path(tmp_path) / "test.db" |
| 256 | + db_file.touch() |
| 257 | + cfg = AppConfig() |
| 258 | + cfg.init_from_dict( |
| 259 | + { |
| 260 | + "name": "test", |
| 261 | + "service": {"host": "localhost", "port": 8080}, |
| 262 | + "llama_stack": { |
| 263 | + "api_key": "test-key", |
| 264 | + "url": "http://test.com:1234", |
| 265 | + "use_as_library_client": False, |
| 266 | + }, |
| 267 | + "user_data_collection": {}, |
| 268 | + "authentication": {"module": "noop"}, |
| 269 | + "authorization": {"access_rules": []}, |
| 270 | + "byok_rag": [ |
| 271 | + { |
| 272 | + "rag_id": "ocp-4.18-docs", |
| 273 | + "rag_type": "inline::faiss", |
| 274 | + "embedding_model": "all-MiniLM-L6-v2", |
| 275 | + "embedding_dimension": 384, |
| 276 | + "vector_db_id": "vs_abc123", |
| 277 | + "db_path": str(db_file), |
| 278 | + }, |
| 279 | + { |
| 280 | + "rag_id": "company-kb", |
| 281 | + "rag_type": "inline::faiss", |
| 282 | + "embedding_model": "all-MiniLM-L6-v2", |
| 283 | + "embedding_dimension": 384, |
| 284 | + "vector_db_id": "vs_def456", |
| 285 | + "db_path": str(db_file), |
| 286 | + }, |
| 287 | + ], |
| 288 | + } |
| 289 | + ) |
| 290 | + return cfg |
| 291 | + |
| 292 | + |
| 293 | +@pytest.mark.asyncio |
| 294 | +async def test_rags_endpoint_returns_rag_ids_from_config( |
| 295 | + mocker: MockerFixture, tmp_path: str |
| 296 | +) -> None: |
| 297 | + """Test that /rags endpoint maps llama-stack IDs to user-facing rag_ids.""" |
| 298 | + byok_config = _make_byok_config(str(tmp_path)) |
| 299 | + mocker.patch("app.endpoints.rags.configuration", byok_config) |
| 300 | + |
| 301 | + # pylint: disable=R0903 |
| 302 | + class RagInfo: |
| 303 | + """RagInfo mock.""" |
| 304 | + |
| 305 | + def __init__(self, rag_id: str) -> None: |
| 306 | + """Initialize with ID.""" |
| 307 | + self.id = rag_id |
| 308 | + |
| 309 | + # pylint: disable=R0903 |
| 310 | + class RagList: |
| 311 | + """List of RAGs mock.""" |
| 312 | + |
| 313 | + def __init__(self) -> None: |
| 314 | + """Initialize with mapped and unmapped entries.""" |
| 315 | + self.data = [ |
| 316 | + RagInfo("vs_abc123"), # mapped to ocp-4.18-docs |
| 317 | + RagInfo("vs_def456"), # mapped to company-kb |
| 318 | + RagInfo("vs_unmapped"), # not in config, passed through |
| 319 | + ] |
| 320 | + |
| 321 | + mock_client = mocker.AsyncMock() |
| 322 | + mock_client.vector_stores.list.return_value = RagList() |
| 323 | + mocker.patch( |
| 324 | + "app.endpoints.rags.AsyncLlamaStackClientHolder" |
| 325 | + ).return_value.get_client.return_value = mock_client |
| 326 | + |
| 327 | + request = Request(scope={"type": "http"}) |
| 328 | + auth: AuthTuple = ("test_user_id", "test_user", True, "test_token") |
| 329 | + |
| 330 | + response = await rags_endpoint_handler(request=request, auth=auth) |
| 331 | + assert response.rags == ["ocp-4.18-docs", "company-kb", "vs_unmapped"] |
| 332 | + |
| 333 | + |
| 334 | +@pytest.mark.asyncio |
| 335 | +async def test_rag_info_endpoint_accepts_rag_id_from_config( |
| 336 | + mocker: MockerFixture, tmp_path: str |
| 337 | +) -> None: |
| 338 | + """Test that /rags/{rag_id} accepts a user-facing rag_id and resolves it.""" |
| 339 | + byok_config = _make_byok_config(str(tmp_path)) |
| 340 | + mocker.patch("app.endpoints.rags.configuration", byok_config) |
| 341 | + |
| 342 | + # pylint: disable=R0902,R0903 |
| 343 | + class RagInfo: |
| 344 | + """RagInfo mock.""" |
| 345 | + |
| 346 | + def __init__(self) -> None: |
| 347 | + """Initialize with test data.""" |
| 348 | + self.id = "vs_abc123" |
| 349 | + self.name = "OCP 4.18 Docs" |
| 350 | + self.created_at = 100 |
| 351 | + self.last_active_at = 200 |
| 352 | + self.expires_at = 300 |
| 353 | + self.object = "vector_store" |
| 354 | + self.status = "completed" |
| 355 | + self.usage_bytes = 500 |
| 356 | + |
| 357 | + mock_client = mocker.AsyncMock() |
| 358 | + mock_client.vector_stores.retrieve.return_value = RagInfo() |
| 359 | + mocker.patch( |
| 360 | + "app.endpoints.rags.AsyncLlamaStackClientHolder" |
| 361 | + ).return_value.get_client.return_value = mock_client |
| 362 | + |
| 363 | + request = Request(scope={"type": "http"}) |
| 364 | + auth: AuthTuple = ("test_user_id", "test_user", True, "test_token") |
| 365 | + |
| 366 | + # Pass the user-facing rag_id, not the vector_store_id |
| 367 | + response = await get_rag_endpoint_handler( |
| 368 | + request=request, auth=auth, rag_id="ocp-4.18-docs" |
| 369 | + ) |
| 370 | + |
| 371 | + # The endpoint should resolve ocp-4.18-docs -> vs_abc123 for the lookup |
| 372 | + mock_client.vector_stores.retrieve.assert_called_once_with("vs_abc123") |
| 373 | + # The response should show the user-facing ID |
| 374 | + assert response.id == "ocp-4.18-docs" |
| 375 | + |
| 376 | + |
| 377 | +def test_resolve_rag_id_to_vector_db_id_with_mapping( |
| 378 | + mocker: MockerFixture, tmp_path: str |
| 379 | +) -> None: |
| 380 | + """Test that _resolve_rag_id_to_vector_db_id maps rag_id to vector_db_id.""" |
| 381 | + byok_config = _make_byok_config(str(tmp_path)) |
| 382 | + mocker.patch("app.endpoints.rags.configuration", byok_config) |
| 383 | + assert _resolve_rag_id_to_vector_db_id("ocp-4.18-docs") == "vs_abc123" |
| 384 | + assert _resolve_rag_id_to_vector_db_id("company-kb") == "vs_def456" |
| 385 | + |
| 386 | + |
| 387 | +def test_resolve_rag_id_to_vector_db_id_passthrough( |
| 388 | + mocker: MockerFixture, tmp_path: str |
| 389 | +) -> None: |
| 390 | + """Test that unmapped IDs are passed through unchanged.""" |
| 391 | + byok_config = _make_byok_config(str(tmp_path)) |
| 392 | + mocker.patch("app.endpoints.rags.configuration", byok_config) |
| 393 | + assert _resolve_rag_id_to_vector_db_id("vs_unknown") == "vs_unknown" |
0 commit comments