|
| 1 | +"""Tests for the /knowledge/orphans API endpoint.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from httpx import AsyncClient |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.asyncio |
| 8 | +async def test_get_orphan_entities_empty_project(client: AsyncClient, v2_project_url): |
| 9 | + """An empty project returns an empty orphans list.""" |
| 10 | + response = await client.get(f"{v2_project_url}/knowledge/orphans") |
| 11 | + |
| 12 | + assert response.status_code == 200 |
| 13 | + assert response.json() == {"entities": [], "total": 0} |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.asyncio |
| 17 | +async def test_get_orphan_entities_returns_unlinked_entities(client: AsyncClient, v2_project_url): |
| 18 | + """Entities with no relations appear in the orphans endpoint.""" |
| 19 | + first = await client.post( |
| 20 | + f"{v2_project_url}/knowledge/entities", |
| 21 | + json={"title": "Orphan One", "directory": "orphan", "content": "No links here"}, |
| 22 | + ) |
| 23 | + second = await client.post( |
| 24 | + f"{v2_project_url}/knowledge/entities", |
| 25 | + json={"title": "Orphan Two", "directory": "orphan", "content": "Also no links"}, |
| 26 | + ) |
| 27 | + assert first.status_code == 200 |
| 28 | + assert second.status_code == 200 |
| 29 | + |
| 30 | + response = await client.get(f"{v2_project_url}/knowledge/orphans") |
| 31 | + |
| 32 | + assert response.status_code == 200 |
| 33 | + data = response.json() |
| 34 | + titles = {entity["title"] for entity in data["entities"]} |
| 35 | + assert titles == {"Orphan One", "Orphan Two"} |
| 36 | + assert data["total"] == 2 |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.asyncio |
| 40 | +async def test_get_orphan_entities_excludes_incoming_and_outgoing_relation_nodes( |
| 41 | + client: AsyncClient, v2_project_url |
| 42 | +): |
| 43 | + """Entities with either side of a resolved relation are excluded from orphans.""" |
| 44 | + target = await client.post( |
| 45 | + f"{v2_project_url}/knowledge/entities", |
| 46 | + json={ |
| 47 | + "title": "Target Note", |
| 48 | + "directory": "linked", |
| 49 | + "content": "Referenced entity", |
| 50 | + }, |
| 51 | + ) |
| 52 | + source = await client.post( |
| 53 | + f"{v2_project_url}/knowledge/entities", |
| 54 | + json={ |
| 55 | + "title": "Source Note", |
| 56 | + "directory": "linked", |
| 57 | + "content": "- links_to [[Target Note]]", |
| 58 | + }, |
| 59 | + ) |
| 60 | + standalone = await client.post( |
| 61 | + f"{v2_project_url}/knowledge/entities", |
| 62 | + json={"title": "Standalone Note", "directory": "linked", "content": "No links"}, |
| 63 | + ) |
| 64 | + assert source.status_code == 200 |
| 65 | + assert target.status_code == 200 |
| 66 | + assert standalone.status_code == 200 |
| 67 | + |
| 68 | + response = await client.get(f"{v2_project_url}/knowledge/orphans") |
| 69 | + |
| 70 | + assert response.status_code == 200 |
| 71 | + titles = {entity["title"] for entity in response.json()["entities"]} |
| 72 | + assert "Source Note" not in titles |
| 73 | + assert "Target Note" not in titles |
| 74 | + assert "Standalone Note" in titles |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.asyncio |
| 78 | +async def test_get_orphan_entities_response_shape(client: AsyncClient, v2_project_url): |
| 79 | + """Each orphan entity in the response has the expected graph-node fields.""" |
| 80 | + created = await client.post( |
| 81 | + f"{v2_project_url}/knowledge/entities", |
| 82 | + json={"title": "Shape Test", "directory": "shape", "content": "Testing shape"}, |
| 83 | + ) |
| 84 | + assert created.status_code == 200 |
| 85 | + |
| 86 | + response = await client.get(f"{v2_project_url}/knowledge/orphans") |
| 87 | + |
| 88 | + assert response.status_code == 200 |
| 89 | + data = response.json() |
| 90 | + entity = next(entity for entity in data["entities"] if entity["title"] == "Shape Test") |
| 91 | + assert set(entity) == {"external_id", "title", "note_type", "file_path"} |
| 92 | + assert entity["file_path"].endswith(".md") |
0 commit comments