|
| 1 | +"""Markdown links reach the project graph without changing authored content.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | +from fastmcp import Client |
| 7 | + |
| 8 | +from basic_memory import db |
| 9 | +from basic_memory.repository.entity_repository import EntityRepository |
| 10 | +from basic_memory.repository.relation_repository import RelationRepository |
| 11 | +from basic_memory.services.bulk_link_resolver import BulkLinkResolver |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.asyncio |
| 15 | +async def test_markdown_paths_are_indexed_and_resolved_exactly( |
| 16 | + mcp_server, app, app_config, test_project, engine_factory |
| 17 | +): |
| 18 | + body = "See [target](../targets/Guide.md#details) and [web](https://example.com).\n" |
| 19 | + async with Client(mcp_server) as client: |
| 20 | + for title, directory, content in [ |
| 21 | + ("Guide", "targets", "# Guide\n\n## Details\nContent."), |
| 22 | + ("Source", "notes", body), |
| 23 | + ]: |
| 24 | + result = await client.call_tool( |
| 25 | + "write_note", |
| 26 | + { |
| 27 | + "title": title, |
| 28 | + "directory": directory, |
| 29 | + "content": content, |
| 30 | + "project": test_project.name, |
| 31 | + }, |
| 32 | + ) |
| 33 | + assert not result.is_error |
| 34 | + |
| 35 | + _, session_maker = engine_factory |
| 36 | + entities = EntityRepository(project_id=test_project.id) |
| 37 | + relations = RelationRepository(project_id=test_project.id) |
| 38 | + async with db.scoped_session(session_maker) as session: |
| 39 | + target = await entities.get_by_file_path(session, "targets/Guide.md") |
| 40 | + assert target is not None |
| 41 | + edges = await relations.find_by_type(session, "links_to") |
| 42 | + assert [edge.to_name for edge in edges] == ["/targets/Guide.md"] |
| 43 | + assert edges[0].to_id == target.id |
| 44 | + resolved = await BulkLinkResolver(entities, app_config).resolve_relation_targets( |
| 45 | + [edges[0].to_name, "/Guide", "/targets/guide.md"], session=session |
| 46 | + ) |
| 47 | + resolved_target = resolved[edges[0].to_name] |
| 48 | + assert resolved_target is not None |
| 49 | + assert resolved_target.id == target.id |
| 50 | + assert resolved["/Guide"] is None |
| 51 | + assert resolved["/targets/guide.md"] is None |
| 52 | + |
| 53 | + assert body.strip() in (Path(test_project.path) / "notes" / "Source.md").read_text() |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_markdown_self_link_is_resolved_when_written( |
| 58 | + mcp_server, app, test_project, engine_factory |
| 59 | +): |
| 60 | + async with Client(mcp_server) as client: |
| 61 | + await client.call_tool( |
| 62 | + "write_note", |
| 63 | + { |
| 64 | + "title": "Self", |
| 65 | + "directory": "notes", |
| 66 | + "content": "[self](Self.md)", |
| 67 | + "project": test_project.name, |
| 68 | + }, |
| 69 | + ) |
| 70 | + _, session_maker = engine_factory |
| 71 | + async with db.scoped_session(session_maker) as session: |
| 72 | + source = await EntityRepository(project_id=test_project.id).get_by_file_path( |
| 73 | + session, "notes/Self.md" |
| 74 | + ) |
| 75 | + assert source is not None |
| 76 | + edges = await RelationRepository(project_id=test_project.id).find_by_type( |
| 77 | + session, "links_to" |
| 78 | + ) |
| 79 | + assert len(edges) == 1 |
| 80 | + assert edges[0].to_name == "/notes/Self.md" |
| 81 | + assert edges[0].to_id == source.id |
0 commit comments