diff --git a/scripts/sync_from_registry.py b/scripts/sync_from_registry.py index a904ab2..6f99f45 100644 --- a/scripts/sync_from_registry.py +++ b/scripts/sync_from_registry.py @@ -429,8 +429,13 @@ def load_existing_search_index( return {} try: data = json.loads(path.read_text(encoding="utf-8")) - except (ValueError, OSError): - return {} + if not isinstance(data, list): + raise ValueError("expected a JSON array") + except (ValueError, OSError) as exc: + raise SystemExit( + f"Cannot parse existing search index {path}: {exc}. " + "Refusing to overwrite it." + ) from exc return {e["slug"]: e for e in data if isinstance(e, dict) and e.get("slug")} diff --git a/tests/test_sync_from_registry.py b/tests/test_sync_from_registry.py index e7187fe..d1386dd 100644 --- a/tests/test_sync_from_registry.py +++ b/tests/test_sync_from_registry.py @@ -4,6 +4,7 @@ from __future__ import annotations import json +import sys from pathlib import Path import pytest @@ -63,6 +64,17 @@ def _entry(**overrides) -> dict: """ +INDEX_WITH_SEARCH = """ + + + + +""" + @pytest.fixture def catalog(tmp_path: Path): @@ -84,6 +96,86 @@ def _embedded(root: Path) -> list[dict]: return json.loads(html[start:end]) +def test_corrupt_search_index_fails_loudly(catalog, monkeypatch): + root = catalog([_entry()]) + (root / "docs" / "index.html").write_text(INDEX_WITH_SEARCH, encoding="utf-8") + search_path = root / "docs" / "search-index.json" + corrupt = "[{\"slug\": \"sample-tools\",\n" + search_path.write_text(corrupt, encoding="utf-8") + monkeypatch.setattr(sync, "REPO_ROOT", root) + monkeypatch.setattr(sync, "REGISTRY_PATH", root / "registry.json") + monkeypatch.setattr(sys, "argv", ["sync_from_registry.py"]) + + with pytest.raises(SystemExit) as excinfo: + sync.main() + + assert excinfo.value.code != 0 + error = str(excinfo.value) + assert "Cannot parse existing search index" in error + assert "docs/search-index.json" in error + assert search_path.read_text(encoding="utf-8") == corrupt + + +def test_search_arrays_survive_sync(catalog): + root = catalog([_entry(description="Updated description")]) + (root / "docs" / "index.html").write_text(INDEX_WITH_SEARCH, encoding="utf-8") + existing = [{ + "name": "Sample Tools", + "slug": "sample-tools", + "skills": ["Preserved Skill"], + "rules": ["Preserved Rule"], + "mcpTools": ["preserved_tool"], + }] + (root / "docs" / "search-index.json").write_text( + json.dumps(existing), encoding="utf-8" + ) + + sync.sync_all(root=root) + + result = json.loads((root / "docs" / "search-index.json").read_text()) + assert result[0]["description"] == "Updated description" + assert result[0]["skills"] == ["Preserved Skill"] + assert result[0]["rules"] == ["Preserved Rule"] + assert result[0]["mcpTools"] == ["preserved_tool"] + + +def test_new_search_entry_gets_empty_arrays(catalog): + root = catalog([ + _entry(), + _entry(name="Other", repo="TMHSDigital/Other", slug="other"), + ]) + (root / "docs" / "index.html").write_text(INDEX_WITH_SEARCH, encoding="utf-8") + (root / "docs" / "search-index.json").write_text(json.dumps([{ + "name": "Sample Tools", + "slug": "sample-tools", + "skills": ["Existing Skill"], + "rules": [], + "mcpTools": [], + }]), encoding="utf-8") + + sync.sync_all(root=root) + + result = json.loads((root / "docs" / "search-index.json").read_text()) + other = next(entry for entry in result if entry["slug"] == "other") + assert other["skills"] == [] + assert other["rules"] == [] + assert other["mcpTools"] == [] + + +def test_removed_search_entry_is_not_kept(catalog): + root = catalog([_entry()]) + (root / "docs" / "index.html").write_text(INDEX_WITH_SEARCH, encoding="utf-8") + (root / "docs" / "search-index.json").write_text(json.dumps([ + {"name": "Sample Tools", "slug": "sample-tools", "skills": [], "rules": [], "mcpTools": []}, + {"name": "Removed", "slug": "removed", "skills": ["Stale"], "rules": [], "mcpTools": []}, + ]), encoding="utf-8") + + sync.sync_all(root=root) + + result = json.loads((root / "docs" / "search-index.json").read_text()) + assert [entry["slug"] for entry in result] == ["sample-tools"] + + def test_write_then_check_is_clean(catalog): root = catalog([_entry()]) assert sync.sync_all(root=root, check=False) is True