|
14 | 14 |
|
15 | 15 |
|
16 | 16 | @pytest_asyncio.fixture |
17 | | -async def context_service(search_repository, entity_repository, observation_repository): |
| 17 | +async def context_service( |
| 18 | + search_repository, entity_repository, observation_repository, link_resolver |
| 19 | +): |
18 | 20 | """Create context service for testing.""" |
19 | | - return ContextService(search_repository, entity_repository, observation_repository) |
| 21 | + return ContextService( |
| 22 | + search_repository, entity_repository, observation_repository, link_resolver=link_resolver |
| 23 | + ) |
20 | 24 |
|
21 | 25 |
|
22 | 26 | @pytest.mark.asyncio |
@@ -333,3 +337,49 @@ async def test_project_isolation_in_find_related(session_maker, app_config): |
333 | 337 | assert entity1_p1.project_id == project1.id |
334 | 338 | assert entity2_p1.project_id == project1.id |
335 | 339 | assert entity1_p2.project_id == project2.id |
| 340 | + |
| 341 | + |
| 342 | +@pytest.mark.asyncio |
| 343 | +async def test_build_context_fallback_via_link_resolver(context_service, test_graph): |
| 344 | + """Test that build_context falls back to LinkResolver when exact permalink fails. |
| 345 | +
|
| 346 | + The test_graph creates entities with permalinks like 'test-project/test/root'. |
| 347 | + Looking up by title ('Root') won't match the exact permalink, but LinkResolver |
| 348 | + can resolve it via title matching. |
| 349 | + """ |
| 350 | + # This identifier is the entity title, not a permalink — exact lookup will fail |
| 351 | + url = memory_url.validate_strings("memory://Root") |
| 352 | + context_result = await context_service.build_context(url) |
| 353 | + |
| 354 | + # LinkResolver should resolve 'Root' → entity with permalink 'test-project/test/root' |
| 355 | + assert context_result.metadata.primary_count == 1 |
| 356 | + assert len(context_result.results) == 1 |
| 357 | + assert context_result.results[0].primary_result.id == test_graph["root"].id |
| 358 | + |
| 359 | + |
| 360 | +@pytest.mark.asyncio |
| 361 | +async def test_build_context_fallback_not_found(context_service): |
| 362 | + """Test that build_context returns empty when both exact lookup and fallback fail.""" |
| 363 | + url = memory_url.validate_strings("memory://completely-nonexistent-note-xyz") |
| 364 | + context_result = await context_service.build_context(url) |
| 365 | + |
| 366 | + assert context_result.metadata.primary_count == 0 |
| 367 | + assert len(context_result.results) == 0 |
| 368 | + |
| 369 | + |
| 370 | +@pytest.mark.asyncio |
| 371 | +async def test_build_context_without_link_resolver( |
| 372 | + search_repository, entity_repository, observation_repository, test_graph |
| 373 | +): |
| 374 | + """Test that build_context still works without a link_resolver (no fallback).""" |
| 375 | + service = ContextService(search_repository, entity_repository, observation_repository) |
| 376 | + |
| 377 | + # Exact permalink lookup should still work |
| 378 | + url = memory_url.validate_strings("memory://test-project/test/root") |
| 379 | + context_result = await service.build_context(url) |
| 380 | + assert context_result.metadata.primary_count == 1 |
| 381 | + |
| 382 | + # Title-based lookup should return empty (no fallback available) |
| 383 | + url = memory_url.validate_strings("memory://Root") |
| 384 | + context_result = await service.build_context(url) |
| 385 | + assert context_result.metadata.primary_count == 0 |
0 commit comments