|
| 1 | +"""A write stores the domain its scope check resolved (GHSA-7848-qh48-mx2w). |
| 2 | +
|
| 3 | +Review 1 of PR #969, B1: `ingest_prd` and `ingest_document` checked the resolved |
| 4 | +domain and stored the raw argument. `_group_repos` can register a directory name as |
| 5 | +an alias of a project whose canonical differs while that same name is another |
| 6 | +family's canonical: rows stored under the raw name reach that family's hot memories. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from mcp_server.handlers import _write_scope |
| 14 | +from mcp_server.shared import domain_mapping |
| 15 | +from tests_py.handlers.test_ghsa_7848_cross_project_write import ( |
| 16 | + projects as _projects, |
| 17 | +) |
| 18 | + |
| 19 | +projects = _projects # the fixture, registered in this module under its name |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def aliased(projects, monkeypatch): |
| 24 | + """`family` is a name that resolves to the session's project `beta`.""" |
| 25 | + real = domain_mapping.resolve_explicit_domain |
| 26 | + alias = lambda name: "beta" if name.strip().lower() == "family" else real(name) # noqa: E731 |
| 27 | + monkeypatch.setattr(_write_scope, "resolve_explicit_domain", alias) |
| 28 | + return projects |
| 29 | + |
| 30 | + |
| 31 | +def test_the_check_returns_the_domain_it_resolved(aliased): |
| 32 | + assert _write_scope.require_writable_domain("family", "t") == "beta" |
| 33 | + assert _write_scope.require_writable_domain(" Beta ", "t") == "beta" |
| 34 | + # A shared domain names no project: it is stored as given. |
| 35 | + assert _write_scope.require_writable_domain("Team-Notes", "t") == "Team-Notes" |
| 36 | + assert _write_scope.require_writable_domain("", "t") == "" |
| 37 | + assert _write_scope.require_writable_domain(None, "t") == "" |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.asyncio |
| 41 | +async def test_ingest_prd_stores_the_resolved_domain_never_the_alias( |
| 42 | + aliased, monkeypatch |
| 43 | +): |
| 44 | + from mcp_server.handlers import ingest_prd |
| 45 | + |
| 46 | + rows = [] |
| 47 | + |
| 48 | + class Store: |
| 49 | + def insert_memory(self, record): |
| 50 | + rows.append(record) |
| 51 | + return len(rows) |
| 52 | + |
| 53 | + monkeypatch.setattr(ingest_prd, "write_page", lambda *a, **k: None) |
| 54 | + monkeypatch.setattr(ingest_prd, "_get_store", lambda: Store()) |
| 55 | + text = "# Plan\n\n## Decisions\n\n- keep it\n\n## Requirements\n\n- ship it\n" |
| 56 | + await ingest_prd.handler({"content": text, "domain": "family"}) |
| 57 | + assert rows and {row["domain"] for row in rows} == {"beta"} |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.asyncio |
| 61 | +async def test_ingest_document_stores_the_resolved_domain_never_the_alias( |
| 62 | + aliased, tmp_path, monkeypatch |
| 63 | +): |
| 64 | + from mcp_server.handlers import ingest_document |
| 65 | + |
| 66 | + seen = [] |
| 67 | + |
| 68 | + async def persist(path, parsed, prov, domain): |
| 69 | + seen.append(domain) |
| 70 | + return {"ingested": True} |
| 71 | + |
| 72 | + monkeypatch.setattr(ingest_document, "_persist", persist) |
| 73 | + page = tmp_path / "export.html" |
| 74 | + page.write_text("<h1>Onboarding</h1><p>welcome</p>", encoding="utf-8") |
| 75 | + await ingest_document.handler({"path": str(page), "domain": "family"}) |
| 76 | + assert seen == ["beta"] |
0 commit comments