|
| 1 | +"""The narrative handler's contract: which memories it reads and what it returns. |
| 2 | +
|
| 3 | +The handler picks one read (a directory, else an explicit domain, else the hot |
| 4 | +memories), then returns either a brief summary with its memory count or the full |
| 5 | +narrative labelled with the directory or the domain. The core renderers are |
| 6 | +replaced by recorders, so each test reads exactly what the handler passed and |
| 7 | +returned. |
| 8 | +
|
| 9 | +source: mcp_server/handlers/narrative.py; issue #721""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import asyncio |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from mcp_server.handlers import narrative |
| 18 | +from mcp_server.shared import domain_mapping as dm |
| 19 | + |
| 20 | +_MEMORIES = [{"id": 1}, {"id": 2}, {"id": 3}] |
| 21 | + |
| 22 | + |
| 23 | +class _RecordingStore: |
| 24 | + """Records every read with its arguments and returns three memories.""" |
| 25 | + |
| 26 | + def __init__(self) -> None: |
| 27 | + self.calls: list[tuple] = [] |
| 28 | + |
| 29 | + def get_memories_for_directory(self, *args, **kwargs): |
| 30 | + self.calls.append(("directory", args, kwargs)) |
| 31 | + return list(_MEMORIES) |
| 32 | + |
| 33 | + def get_memories_for_domain(self, *args, **kwargs): |
| 34 | + self.calls.append(("domain", args, kwargs)) |
| 35 | + return list(_MEMORIES) |
| 36 | + |
| 37 | + def get_hot_memories(self, *args, **kwargs): |
| 38 | + self.calls.append(("hot", args, kwargs)) |
| 39 | + return list(_MEMORIES) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def run(monkeypatch, tmp_path): |
| 44 | + """Run the public handler; return (result, store reads, renderer calls).""" |
| 45 | + monkeypatch.setattr(dm, "_candidate_dev_roots", lambda: [tmp_path]) |
| 46 | + dm._build_registry.cache_clear() |
| 47 | + store = _RecordingStore() |
| 48 | + rendered: list[tuple] = [] |
| 49 | + |
| 50 | + def brief(memories): |
| 51 | + rendered.append(("brief", memories)) |
| 52 | + return "brief summary" |
| 53 | + |
| 54 | + def full(memories, **kwargs): |
| 55 | + rendered.append(("full", memories, kwargs)) |
| 56 | + return {"narrative": "story"} |
| 57 | + |
| 58 | + monkeypatch.setattr(narrative, "_get_store", lambda: store) |
| 59 | + monkeypatch.setattr(narrative, "generate_brief_summary", brief) |
| 60 | + monkeypatch.setattr(narrative, "generate_narrative", full) |
| 61 | + |
| 62 | + def call(args): |
| 63 | + result = asyncio.run(narrative.handler(args)) |
| 64 | + return result, store.calls, rendered |
| 65 | + |
| 66 | + yield call |
| 67 | + dm._build_registry.cache_clear() |
| 68 | + |
| 69 | + |
| 70 | +def test_no_arguments_reads_hot_memories(run): |
| 71 | + _, calls, _ = run(None) |
| 72 | + assert calls == [("hot", (), {"min_heat": 0.1, "limit": 200})] |
| 73 | + |
| 74 | + |
| 75 | +def test_empty_arguments_read_hot_memories(run): |
| 76 | + _, calls, _ = run({}) |
| 77 | + assert calls == [("hot", (), {"min_heat": 0.1, "limit": 200})] |
| 78 | + |
| 79 | + |
| 80 | +def test_directory_reads_every_memory_of_the_directory(run): |
| 81 | + _, calls, _ = run({"directory": "/work/repo"}) |
| 82 | + assert calls == [("directory", ("/work/repo",), {"min_heat": 0.0})] |
| 83 | + |
| 84 | + |
| 85 | +def test_directory_wins_over_domain(run): |
| 86 | + _, calls, _ = run({"directory": "/work/repo", "domain": "cortex"}) |
| 87 | + assert [c[0] for c in calls] == ["directory"] |
| 88 | + |
| 89 | + |
| 90 | +def test_domain_reads_up_to_200_memories_at_any_heat(run): |
| 91 | + _, calls, _ = run({"domain": "cortex"}) |
| 92 | + assert calls == [("domain", ("cortex",), {"min_heat": 0.0, "limit": 200})] |
| 93 | + |
| 94 | + |
| 95 | +def test_default_is_the_full_narrative_labelled_by_directory(run): |
| 96 | + result, _, rendered = run({"directory": "/work/repo"}) |
| 97 | + assert result == {"narrative": "story"} |
| 98 | + assert rendered == [("full", _MEMORIES, {"directory": "/work/repo"})] |
| 99 | + |
| 100 | + |
| 101 | +def test_full_narrative_is_labelled_by_the_resolved_domain(run): |
| 102 | + _, _, rendered = run({"domain": " Cortex"}) |
| 103 | + assert rendered == [("full", _MEMORIES, {"directory": "cortex"})] |
| 104 | + |
| 105 | + |
| 106 | +def test_full_narrative_without_scope_has_an_empty_label(run): |
| 107 | + _, _, rendered = run({"brief": False}) |
| 108 | + assert rendered == [("full", _MEMORIES, {"directory": ""})] |
| 109 | + |
| 110 | + |
| 111 | +def test_brief_returns_the_summary_and_the_memory_count(run): |
| 112 | + result, _, rendered = run({"domain": "cortex", "brief": True}) |
| 113 | + assert result == {"summary": "brief summary", "memory_count": 3} |
| 114 | + assert rendered == [("brief", _MEMORIES)] |
0 commit comments