"""TDD — musehub#209: docs/reference/mcp.md tool/resource counts were stale. Guards against re-drift: ``docs/reference/mcp.md``'s generated regions (tool/resource catalogues + their counts) must always match what ``scripts/gen_mcp_reference.py`` would produce from the live registries. If this test fails, someone added/removed a tool or resource and forgot to run ``python scripts/gen_mcp_reference.py``. """ from __future__ import annotations import sys from pathlib import Path import pytest sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) import gen_mcp_reference as gen # noqa: E402 @pytest.fixture() def committed_doc() -> str: return gen._DOC_PATH.read_text(encoding="utf-8") class TestDocStaysInSync: def test_committed_doc_matches_generated_output(self, committed_doc: str) -> None: """The real regression guard: committed mcp.md == freshly generated.""" assert gen.generate(committed_doc) == committed_doc def test_tool_counts_match_live_registry(self, committed_doc: str) -> None: from musehub.mcp.tools import MCP_TOOLS assert f"Tools — {len(MCP_TOOLS)} total" in committed_doc def test_resource_counts_match_live_registry(self, committed_doc: str) -> None: from musehub.mcp.resources import STATIC_RESOURCES, RESOURCE_TEMPLATES total = len(STATIC_RESOURCES) + len(RESOURCE_TEMPLATES) assert f"Resources — {total} total" in committed_doc def test_every_live_tool_is_documented(self, committed_doc: str) -> None: from musehub.mcp.tools import MCP_TOOLS missing = [t["name"] for t in MCP_TOOLS if f"#### `{t['name']}`" not in committed_doc] assert not missing, f"{len(missing)} live tools missing from mcp.md: {missing[:10]}" def test_every_live_static_resource_is_documented(self, committed_doc: str) -> None: from musehub.mcp.resources import STATIC_RESOURCES missing = [r["uri"] for r in STATIC_RESOURCES if f"#### `{r['uri']}`" not in committed_doc] assert not missing, f"missing static resources: {missing}" def test_every_live_resource_template_is_documented(self, committed_doc: str) -> None: from musehub.mcp.resources import RESOURCE_TEMPLATES missing = [ r["uriTemplate"] for r in RESOURCE_TEMPLATES if f"#### `{r['uriTemplate']}`" not in committed_doc ] assert not missing, f"missing resource templates: {missing}" class TestGeneratorUnit: def test_param_table_escapes_literal_pipes(self) -> None: schema = { "properties": {"x": {"type": "string", "description": "a | b"}}, "required": ["x"], } rendered = gen._param_rows(schema) assert "a \\| b" in rendered # Every data row must have exactly 5 unescaped pipe characters # (leading + 4 separators + trailing) or it breaks table rendering. for line in rendered.splitlines(): if line.startswith("| `"): unescaped = line.replace("\\|", "") assert unescaped.count("|") == 5, line def test_param_table_empty_schema_has_no_parameters_note(self) -> None: assert gen._param_rows({"properties": {}}) == "_No parameters._" def test_tool_classification_is_a_partition(self) -> None: from musehub.mcp.tools import MCP_TOOLS read, write, elicit = gen._classify_tools() assert len(read) + len(write) + len(elicit) == len(MCP_TOOLS) names = [t["name"] for t in (*read, *write, *elicit)] assert len(names) == len(set(names)) # no tool double-counted def test_check_passes_against_committed_doc(self, capsys: pytest.CaptureFixture[str]) -> None: """Exercises the --check code path end-to-end via main(), matching how the script is actually invoked (python scripts/gen_mcp_reference.py --check).""" import sys as _sys old_argv = _sys.argv try: _sys.argv = ["gen_mcp_reference.py", "--check"] gen.main() finally: _sys.argv = old_argv assert "up to date" in capsys.readouterr().out