"""Section 42 — Protocol Introspection: 7-layer test suite. Covers: - musehub/protocol/events.py::EVENT_REGISTRY - musehub/protocol/responses.py::compute_protocol_hash, ProtocolInfoResponse, build_protocol_info - musehub/api/routes/protocol.py::get_protocol_info, get_events_json, get_tools_json, get_schema_json """ from __future__ import annotations import json import time import pytest from muse.core.types import content_hash from musehub.mcp.tools.musehub import MUSEHUB_TOOLS, MUSEHUB_TOOL_NAMES from musehub.protocol.events import EVENT_REGISTRY from musehub.protocol.responses import ( ProtocolInfoResponse, build_protocol_info, compute_protocol_hash, ) from musehub.protocol.version import MUSE_VERSION # ───────────────────────────────────────────────────────────────────────────── # LAYER 1 — UNIT # ───────────────────────────────────────────────────────────────────────────── class TestEventRegistryUnit: """Unit: EVENT_REGISTRY structure and completeness.""" def test_event_registry_is_frozenset(self) -> None: assert isinstance(EVENT_REGISTRY, frozenset) def test_event_registry_non_empty(self) -> None: assert len(EVENT_REGISTRY) > 0 def test_event_registry_contains_core_events(self) -> None: for event in ( "commit_pushed", "proposal_opened", "proposal_merged", "proposal_closed", "issue_opened", "issue_closed", "branch_created", "branch_deleted", ): assert event in EVENT_REGISTRY, f"{event!r} missing from EVENT_REGISTRY" def test_event_registry_all_strings(self) -> None: assert all(isinstance(e, str) for e in EVENT_REGISTRY) def test_event_registry_no_empty_strings(self) -> None: assert all(e.strip() for e in EVENT_REGISTRY) def test_event_registry_all_snake_case(self) -> None: for event in EVENT_REGISTRY: assert event == event.lower(), f"{event!r} is not lowercase" assert " " not in event, f"{event!r} contains spaces" def test_event_registry_immutable(self) -> None: """EVENT_REGISTRY is a frozenset — it has no add() method.""" assert not hasattr(EVENT_REGISTRY, "add"), ( "EVENT_REGISTRY must be a frozenset — it must not have an add() method" ) assert isinstance(EVENT_REGISTRY, frozenset), ( "EVENT_REGISTRY must be a frozenset to ensure immutability" ) class TestComputeProtocolHashUnit: """Unit: compute_protocol_hash determinism and correctness.""" def test_returns_canonical_id_string(self) -> None: h = compute_protocol_hash({"key": "value"}) assert isinstance(h, str) assert h.startswith("sha256:") assert len(h) == 71 # sha256:<64-hex> hex_part = h[7:] assert all(c in "0123456789abcdef" for c in hex_part) def test_same_input_same_hash(self) -> None: data = {"events": ["a", "b"], "tools": [{"name": "t1"}]} assert compute_protocol_hash(data) == compute_protocol_hash(data) def test_key_order_does_not_affect_hash(self) -> None: a = {"b": 2, "a": 1} b = {"a": 1, "b": 2} assert compute_protocol_hash(a) == compute_protocol_hash(b) def test_different_data_different_hash(self) -> None: assert compute_protocol_hash({"x": 1}) != compute_protocol_hash({"x": 2}) def test_hash_is_sha256_of_canonical_json(self) -> None: data = {"events": ["push"], "tools": []} assert compute_protocol_hash(data) == content_hash(data) def test_list_input_hashes_correctly(self) -> None: h = compute_protocol_hash(["a", "b", "c"]) assert isinstance(h, str) and len(h) == 71 # sha256:<64-hex> def test_empty_dict_hashes_deterministically(self) -> None: assert compute_protocol_hash({}) == compute_protocol_hash({}) def test_nested_structure_hashes_consistently(self) -> None: data = {"outer": {"inner": [1, 2, 3]}} assert compute_protocol_hash(data) == compute_protocol_hash(data) class TestProtocolInfoResponseUnit: """Unit: ProtocolInfoResponse Pydantic model.""" def test_required_fields_present(self) -> None: r = ProtocolInfoResponse( version="1.2.3", protocol_hash="a" * 64, event_count=12, tool_count=40, ) assert r.version == "1.2.3" assert r.protocol_hash == "a" * 64 assert r.event_count == 12 assert r.tool_count == 40 def test_serialises_to_dict(self) -> None: r = ProtocolInfoResponse( version="0.1.0", protocol_hash="b" * 64, event_count=5, tool_count=10, ) d = r.model_dump() assert set(d.keys()) == {"version", "protocol_hash", "event_count", "tool_count"} def test_build_protocol_info_uses_muse_version(self) -> None: info = build_protocol_info(event_count=5, tool_count=10, schema={"x": 1}) assert info.version == MUSE_VERSION def test_build_protocol_info_hash_from_schema(self) -> None: schema = {"events": ["a"], "tools": []} info = build_protocol_info(event_count=1, tool_count=0, schema=schema) assert info.protocol_hash == compute_protocol_hash(schema) def test_build_protocol_info_counts(self) -> None: info = build_protocol_info(event_count=12, tool_count=40, schema={}) assert info.event_count == 12 assert info.tool_count == 40 # ───────────────────────────────────────────────────────────────────────────── # LAYER 2 — INTEGRATION # ───────────────────────────────────────────────────────────────────────────── class TestProtocolIntegration: """Integration: protocol module wires together correctly.""" def test_musehub_tools_non_empty(self) -> None: assert len(MUSEHUB_TOOLS) > 0 def test_musehub_tool_names_set_matches_tools_list(self) -> None: names_from_list = {t["name"] for t in MUSEHUB_TOOLS} assert names_from_list == MUSEHUB_TOOL_NAMES def test_build_protocol_info_event_count_matches_registry(self) -> None: from musehub.api.routes.protocol import _build_schema schema = _build_schema() info = build_protocol_info( event_count=len(EVENT_REGISTRY), tool_count=len(MUSEHUB_TOOLS), schema=schema, ) assert info.event_count == len(EVENT_REGISTRY) def test_build_protocol_info_tool_count_matches_catalogue(self) -> None: from musehub.api.routes.protocol import _build_schema schema = _build_schema() info = build_protocol_info( event_count=len(EVENT_REGISTRY), tool_count=len(MUSEHUB_TOOLS), schema=schema, ) assert info.tool_count == len(MUSEHUB_TOOLS) def test_schema_events_match_registry(self) -> None: from musehub.api.routes.protocol import _build_schema schema = _build_schema() assert set(schema["events"]) == set(EVENT_REGISTRY) def test_schema_events_are_sorted(self) -> None: from musehub.api.routes.protocol import _build_schema schema = _build_schema() assert schema["events"] == sorted(EVENT_REGISTRY) def test_schema_tools_have_name_and_description(self) -> None: from musehub.api.routes.protocol import _build_schema schema = _build_schema() for tool in schema["tools"]: assert "name" in tool assert "description" in tool # ───────────────────────────────────────────────────────────────────────────── # LAYER 3 — E2E # ───────────────────────────────────────────────────────────────────────────── class TestProtocolE2E: """E2E: /protocol endpoints via async test client.""" async def test_get_protocol_returns_200(self, client: AsyncClient) -> None: r = await client.get("/protocol") assert r.status_code == 200 async def test_get_protocol_json_shape(self, client: AsyncClient) -> None: r = await client.get("/protocol") data = r.json() assert "version" in data assert "protocol_hash" in data assert "event_count" in data assert "tool_count" in data async def test_get_protocol_version_matches_muse_version(self, client: AsyncClient) -> None: r = await client.get("/protocol") assert r.json()["version"] == MUSE_VERSION async def test_get_protocol_hash_is_canonical_id(self, client: AsyncClient) -> None: r = await client.get("/protocol") h = r.json()["protocol_hash"] assert h.startswith("sha256:") assert len(h) == 71 async def test_get_protocol_event_count_matches_registry(self, client: AsyncClient) -> None: r = await client.get("/protocol") assert r.json()["event_count"] == len(EVENT_REGISTRY) async def test_get_protocol_tool_count_matches_catalogue(self, client: AsyncClient) -> None: r = await client.get("/protocol") assert r.json()["tool_count"] == len(MUSEHUB_TOOLS) async def test_get_events_json_returns_200(self, client: AsyncClient) -> None: r = await client.get("/protocol/events.json") assert r.status_code == 200 async def test_get_events_json_contains_events_key(self, client: AsyncClient) -> None: r = await client.get("/protocol/events.json") assert "events" in r.json() async def test_get_events_json_matches_registry(self, client: AsyncClient) -> None: r = await client.get("/protocol/events.json") assert set(r.json()["events"]) == set(EVENT_REGISTRY) async def test_get_events_json_sorted(self, client: AsyncClient) -> None: r = await client.get("/protocol/events.json") events = r.json()["events"] assert events == sorted(events) async def test_get_tools_json_returns_200(self, client: AsyncClient) -> None: r = await client.get("/protocol/tools.json") assert r.status_code == 200 async def test_get_tools_json_contains_tools_key(self, client: AsyncClient) -> None: r = await client.get("/protocol/tools.json") assert "tools" in r.json() async def test_get_tools_json_count_matches_catalogue(self, client: AsyncClient) -> None: r = await client.get("/protocol/tools.json") assert len(r.json()["tools"]) == len(MUSEHUB_TOOLS) async def test_get_tools_json_each_has_name_and_description(self, client: AsyncClient) -> None: r = await client.get("/protocol/tools.json") for tool in r.json()["tools"]: assert "name" in tool assert "description" in tool async def test_get_schema_json_returns_200(self, client: AsyncClient) -> None: r = await client.get("/protocol/schema.json") assert r.status_code == 200 async def test_get_schema_json_shape(self, client: AsyncClient) -> None: r = await client.get("/protocol/schema.json") data = r.json() assert "schema" in data assert "hash" in data async def test_get_schema_json_hash_matches_schema(self, client: AsyncClient) -> None: r = await client.get("/protocol/schema.json") data = r.json() expected_hash = compute_protocol_hash(data["schema"]) assert data["hash"] == expected_hash async def test_protocol_endpoints_no_auth_required(self, client: AsyncClient) -> None: """All /protocol endpoints must be publicly accessible without auth headers.""" for path in ("/protocol", "/protocol/events.json", "/protocol/tools.json", "/protocol/schema.json"): r = await client.get(path) assert r.status_code == 200, f"{path} returned {r.status_code}" # ───────────────────────────────────────────────────────────────────────────── # LAYER 4 — STRESS # ───────────────────────────────────────────────────────────────────────────── class TestProtocolStress: """Stress: repeated calls and bulk hash computation.""" def test_compute_protocol_hash_1000_times_stable(self) -> None: data = {"events": sorted(EVENT_REGISTRY), "tools": [t["name"] for t in MUSEHUB_TOOLS]} first = compute_protocol_hash(data) for _ in range(1000): assert compute_protocol_hash(data) == first def test_compute_protocol_hash_10000_small_dicts(self) -> None: for i in range(10_000): h = compute_protocol_hash({"i": i}) assert len(h) == 71 async def test_get_protocol_50_sequential_requests(self, client: AsyncClient) -> None: hashes = [] for _ in range(50): r = await client.get("/protocol") assert r.status_code == 200 hashes.append(r.json()["protocol_hash"]) assert len(set(hashes)) == 1, "protocol_hash changed between requests" async def test_get_events_json_50_sequential_stable(self, client: AsyncClient) -> None: first = None for _ in range(50): r = await client.get("/protocol/events.json") data = r.json()["events"] if first is None: first = data assert data == first # ───────────────────────────────────────────────────────────────────────────── # LAYER 5 — DATA INTEGRITY # ───────────────────────────────────────────────────────────────────────────── class TestProtocolDataIntegrity: """Data Integrity: schema stability and cross-endpoint consistency.""" def test_hash_stable_across_module_reloads(self) -> None: """Same schema data always hashes identically regardless of import order.""" from musehub.protocol.responses import compute_protocol_hash as h1 from musehub.protocol import responses as mod h2 = mod.compute_protocol_hash data = {"stable": True, "events": ["a", "b"]} assert h1(data) == h2(data) async def test_protocol_hash_equals_schema_hash(self, client: AsyncClient) -> None: """hash in GET /protocol must equal hash in GET /protocol/schema.json.""" info_r = await client.get("/protocol") schema_r = await client.get("/protocol/schema.json") assert info_r.json()["protocol_hash"] == schema_r.json()["hash"] async def test_events_json_matches_schema_events(self, client: AsyncClient) -> None: events_r = await client.get("/protocol/events.json") schema_r = await client.get("/protocol/schema.json") assert set(events_r.json()["events"]) == set(schema_r.json()["schema"]["events"]) async def test_tools_json_matches_schema_tools(self, client: AsyncClient) -> None: tools_r = await client.get("/protocol/tools.json") schema_r = await client.get("/protocol/schema.json") tool_names_from_tools = {t["name"] for t in tools_r.json()["tools"]} tool_names_from_schema = {t["name"] for t in schema_r.json()["schema"]["tools"]} assert tool_names_from_tools == tool_names_from_schema async def test_event_count_matches_events_list_length(self, client: AsyncClient) -> None: info_r = await client.get("/protocol") events_r = await client.get("/protocol/events.json") assert info_r.json()["event_count"] == len(events_r.json()["events"]) async def test_tool_count_matches_tools_list_length(self, client: AsyncClient) -> None: info_r = await client.get("/protocol") tools_r = await client.get("/protocol/tools.json") assert info_r.json()["tool_count"] == len(tools_r.json()["tools"]) def test_no_duplicate_event_types(self) -> None: events = list(EVENT_REGISTRY) assert len(events) == len(set(events)) def test_no_duplicate_tool_names(self) -> None: names = [t["name"] for t in MUSEHUB_TOOLS] assert len(names) == len(set(names)), "Duplicate tool names in MUSEHUB_TOOLS" def test_schema_events_no_duplicates(self) -> None: from musehub.api.routes.protocol import _build_schema schema = _build_schema() assert len(schema["events"]) == len(set(schema["events"])) # ───────────────────────────────────────────────────────────────────────────── # LAYER 6 — SECURITY # ───────────────────────────────────────────────────────────────────────────── class TestProtocolSecurity: """Security: public endpoints, no sensitive data exposure, injection safety.""" async def test_protocol_info_no_auth_header_returns_200(self, client: AsyncClient) -> None: r = await client.get("/protocol") assert r.status_code == 200 async def test_events_json_no_auth_returns_200(self, client: AsyncClient) -> None: r = await client.get("/protocol/events.json") assert r.status_code == 200 async def test_tools_json_no_auth_returns_200(self, client: AsyncClient) -> None: r = await client.get("/protocol/tools.json") assert r.status_code == 200 async def test_schema_json_no_auth_returns_200(self, client: AsyncClient) -> None: r = await client.get("/protocol/schema.json") assert r.status_code == 200 async def test_protocol_no_stack_trace_in_response(self, client: AsyncClient) -> None: r = await client.get("/protocol") text = r.text assert "Traceback" not in text assert "File \"/" not in text async def test_tools_json_no_credential_fields(self, client: AsyncClient) -> None: """Tool definitions must not expose internal credentials or private keys.""" r = await client.get("/protocol/tools.json") text = r.text.lower() assert "password" not in text assert "private_key" not in text assert "-----begin" not in text # PEM block async def test_events_json_content_type_json(self, client: AsyncClient) -> None: r = await client.get("/protocol/events.json") ct = r.headers.get("content-type", "") assert "json" in ct async def test_schema_json_content_type_json(self, client: AsyncClient) -> None: r = await client.get("/protocol/schema.json") ct = r.headers.get("content-type", "") assert "json" in ct async def test_protocol_post_not_allowed(self, client: AsyncClient) -> None: r = await client.post("/protocol", json={}) assert r.status_code in (404, 405) def test_compute_hash_does_not_mutate_input(self) -> None: data = {"events": ["a"], "tools": [{"name": "x"}]} original = json.dumps(data) compute_protocol_hash(data) assert json.dumps(data) == original # ───────────────────────────────────────────────────────────────────────────── # LAYER 7 — PERFORMANCE # ───────────────────────────────────────────────────────────────────────────── class TestProtocolPerformance: """Performance: latency budgets for protocol endpoints and hash computation.""" def test_compute_protocol_hash_1k_under_100ms(self) -> None: from musehub.api.routes.protocol import _build_schema schema = _build_schema() t0 = time.perf_counter() for _ in range(1000): compute_protocol_hash(schema) elapsed = time.perf_counter() - t0 assert elapsed < 0.2, f"1K compute_protocol_hash took {elapsed:.3f}s" async def test_get_protocol_under_200ms(self, client: AsyncClient) -> None: t0 = time.perf_counter() r = await client.get("/protocol") elapsed = time.perf_counter() - t0 assert r.status_code == 200 assert elapsed < 0.2, f"GET /protocol took {elapsed:.3f}s" async def test_get_events_json_under_200ms(self, client: AsyncClient) -> None: t0 = time.perf_counter() r = await client.get("/protocol/events.json") elapsed = time.perf_counter() - t0 assert r.status_code == 200 assert elapsed < 0.2, f"GET /protocol/events.json took {elapsed:.3f}s" async def test_get_tools_json_under_300ms(self, client: AsyncClient) -> None: t0 = time.perf_counter() r = await client.get("/protocol/tools.json") elapsed = time.perf_counter() - t0 assert r.status_code == 200 assert elapsed < 0.3, f"GET /protocol/tools.json took {elapsed:.3f}s" async def test_get_schema_json_under_300ms(self, client: AsyncClient) -> None: t0 = time.perf_counter() r = await client.get("/protocol/schema.json") elapsed = time.perf_counter() - t0 assert r.status_code == 200 assert elapsed < 0.3, f"GET /protocol/schema.json took {elapsed:.3f}s"