"""Tests for the Mist domain plugin — Phase 1. Test tiers covered ------------------ Tier 1 — Shape / API surface MistPlugin satisfies MuseDomainPlugin; all 6 required methods present; schema() returns a well-formed DomainSchema. Tier 5 — Data integrity compute_mist_id: determinism, uniqueness, length, alphabet; detect_artifact_type: magic bytes, JSON key inspection, extension fallback; validate_mist_filename: accepts valid names, rejects all attack vectors; extract_mist_symbol_anchors: anchors for Python source, empty for binary. Tier 6 — Performance compute_mist_id on a 1 MiB blob completes in under 100 ms. Tier 8 — Docstring completeness All public symbols in plugin.py carry a docstring. """ from __future__ import annotations import inspect import pathlib import sys import time from muse.core.types import blob_id from muse.domain import SnapshotManifest from muse.plugins.mist.plugin import MistPlugin import pytest # --------------------------------------------------------------------------- # Fixtures # --------------------------------------------------------------------------- @pytest.fixture() def plugin() -> MistPlugin: return MistPlugin() @pytest.fixture() def empty_snap() -> SnapshotManifest: return SnapshotManifest(files={}, domain="mist", directories=[]) @pytest.fixture() def snap_with_one(tmp_path: pathlib.Path) -> SnapshotManifest: """A SnapshotManifest containing one file keyed by its SHA-256 hex digest.""" content = b"hello mist" digest = blob_id(content) return SnapshotManifest(files={"aB3xQ9fWmK2r.py": digest}, domain="mist", directories=[]) # --------------------------------------------------------------------------- # Tier 1 — Shape / API surface # --------------------------------------------------------------------------- class TestMistPluginShape: """Verify MistPlugin satisfies the MuseDomainPlugin protocol.""" REQUIRED_METHODS = ("snapshot", "diff", "merge", "drift", "apply", "schema") def test_all_required_methods_present(self, plugin: MistPlugin) -> None: for method in self.REQUIRED_METHODS: assert hasattr(plugin, method), f"MistPlugin missing method: {method}" assert callable(getattr(plugin, method)) def test_schema_returns_domain_schema(self, plugin: MistPlugin) -> None: schema = plugin.schema() # DomainSchema is a TypedDict (a dict subclass) — check required keys assert isinstance(schema, dict) assert "domain" in schema assert "description" in schema assert "top_level" in schema assert "dimensions" in schema assert "merge_mode" in schema def test_schema_domain_is_mist(self, plugin: MistPlugin) -> None: assert plugin.schema()["domain"] == "mist" def test_schema_top_level_is_set(self, plugin: MistPlugin) -> None: top = plugin.schema()["top_level"] # SetSchema is a TypedDict assert isinstance(top, dict) assert top["kind"] == "set" assert top["element_type"] == "artifact" assert top["identity"] == "by_content" def test_schema_has_two_dimensions(self, plugin: MistPlugin) -> None: dims = plugin.schema()["dimensions"] assert len(dims) == 2 names = {d["name"] for d in dims} assert names == {"artifacts", "metadata"} def test_schema_merge_mode_three_way(self, plugin: MistPlugin) -> None: assert plugin.schema()["merge_mode"] == "three_way" def test_schema_version_is_string(self, plugin: MistPlugin) -> None: assert isinstance(plugin.schema()["schema_version"], str) assert len(plugin.schema()["schema_version"]) > 0 def test_registered_in_registry(self) -> None: from muse.plugins.registry import _REGISTRY assert "mist" in _REGISTRY from muse.plugins.mist.plugin import MistPlugin assert isinstance(_REGISTRY["mist"], MistPlugin) def test_resolve_plugin_by_domain(self) -> None: from muse.plugins.registry import resolve_plugin_by_domain from muse.plugins.mist.plugin import MistPlugin plugin = resolve_plugin_by_domain("mist") assert isinstance(plugin, MistPlugin) def test_registered_domains_includes_mist(self) -> None: from muse.plugins.registry import registered_domains assert "mist" in registered_domains() # --------------------------------------------------------------------------- # Tier 5 — Data integrity: compute_mist_id # --------------------------------------------------------------------------- class TestComputeMistId: """Tests for the compute_mist_id pure function.""" def test_deterministic(self) -> None: from muse.plugins.mist.plugin import compute_mist_id content = b"repeatability is key" assert compute_mist_id(content) == compute_mist_id(content) def test_length_is_12(self) -> None: from muse.plugins.mist.plugin import compute_mist_id assert len(compute_mist_id(b"")) == 12 assert len(compute_mist_id(b"x" * 1_000_000)) == 12 def test_only_base58_alphabet(self) -> None: from muse.plugins.mist.plugin import _BASE58_ALPHABET, compute_mist_id for content in (b"", b"a", b"\x00" * 32, b"\xff" * 32): mist_id = compute_mist_id(content) for ch in mist_id: assert ch in _BASE58_ALPHABET, f"Unexpected char {ch!r} in mist_id {mist_id!r}" def test_no_ambiguous_chars(self) -> None: from muse.plugins.mist.plugin import compute_mist_id ambiguous = set("0OIl") for i in range(256): mist_id = compute_mist_id(bytes([i])) for ch in mist_id: assert ch not in ambiguous, ( f"Ambiguous char {ch!r} found in mist_id {mist_id!r} for byte {i}" ) def test_uniqueness_across_different_content(self) -> None: from muse.plugins.mist.plugin import compute_mist_id ids = {compute_mist_id(f"artifact_{i}".encode()) for i in range(200)} assert len(ids) == 200, "mist IDs collided for distinct content" def test_empty_bytes_stable_id(self) -> None: """Empty content always maps to the same ID (regression guard).""" from muse.plugins.mist.plugin import compute_mist_id id1 = compute_mist_id(b"") id2 = compute_mist_id(b"") assert id1 == id2 def test_single_bit_change_produces_different_id(self) -> None: from muse.plugins.mist.plugin import compute_mist_id base = b"hello" modified = b"hfllo" assert compute_mist_id(base) != compute_mist_id(modified) # --------------------------------------------------------------------------- # Tier 5 — Data integrity: detect_artifact_type # --------------------------------------------------------------------------- class TestDetectArtifactType: """Tests for the detect_artifact_type pure function.""" def test_midi_magic_bytes(self) -> None: from muse.plugins.mist.plugin import detect_artifact_type result = detect_artifact_type("track.mid", b"MThd\x00\x00\x00\x06\x00\x01") assert result == {"artifact_type": "midi", "language": "midi"} def test_midi_magic_bytes_wrong_extension(self) -> None: """Magic bytes take priority over extension.""" from muse.plugins.mist.plugin import detect_artifact_type result = detect_artifact_type("track.dat", b"MThd\x00\x00\x00\x06\x00\x01") assert result == {"artifact_type": "midi", "language": "midi"} def test_abi_json(self) -> None: import json from muse.plugins.mist.plugin import detect_artifact_type abi = json.dumps([{"type": "function", "name": "transfer", "inputs": []}]).encode() result = detect_artifact_type("contract.abi.json", abi) assert result == {"artifact_type": "abi", "language": "json"} def test_json_schema(self) -> None: import json from muse.plugins.mist.plugin import detect_artifact_type schema = json.dumps({"$schema": "http://json-schema.org/draft-07/schema#"}).encode() result = detect_artifact_type("schema.json", schema) assert result == {"artifact_type": "json_schema", "language": "json"} def test_python_extension(self) -> None: from muse.plugins.mist.plugin import detect_artifact_type result = detect_artifact_type("utils.py", b"def add(a, b): return a + b") assert result == {"artifact_type": "code", "language": "python"} def test_typescript_extension(self) -> None: from muse.plugins.mist.plugin import detect_artifact_type result = detect_artifact_type("app.ts", b"export function hello(): void {}") assert result == {"artifact_type": "code", "language": "typescript"} def test_markdown_extension(self) -> None: from muse.plugins.mist.plugin import detect_artifact_type result = detect_artifact_type("README.md", b"# Hello\n\nWorld") assert result == {"artifact_type": "code", "language": "markdown"} def test_solidity_extension(self) -> None: from muse.plugins.mist.plugin import detect_artifact_type result = detect_artifact_type("Token.sol", b"// SPDX-License-Identifier: MIT") assert result == {"artifact_type": "code", "language": "solidity"} def test_unknown_extension_fallback(self) -> None: from muse.plugins.mist.plugin import detect_artifact_type result = detect_artifact_type("blob.xyzzy", b"\xde\xad\xbe\xef") assert result == {"artifact_type": "unknown", "language": "binary"} def test_returns_dict_with_required_keys(self) -> None: from muse.plugins.mist.plugin import detect_artifact_type for fname in ("a.py", "b.mid", "c.json", "d.unknown"): result = detect_artifact_type(fname, b"content") assert "artifact_type" in result assert "language" in result # --------------------------------------------------------------------------- # Tier 5 — Data integrity: validate_mist_filename # --------------------------------------------------------------------------- class TestValidateMistFilename: """Tests for the validate_mist_filename security gate.""" def test_valid_simple_name(self) -> None: from muse.plugins.mist.plugin import validate_mist_filename validate_mist_filename("aB3xQ9fWmK2r.py") # must not raise def test_valid_name_with_dots_and_dashes(self) -> None: from muse.plugins.mist.plugin import validate_mist_filename validate_mist_filename("my-artifact.abi.json") # must not raise def test_rejects_null_byte(self) -> None: from muse.plugins.mist.plugin import validate_mist_filename with pytest.raises(ValueError, match="null byte"): validate_mist_filename("malicious\x00.py") def test_rejects_forward_slash(self) -> None: from muse.plugins.mist.plugin import validate_mist_filename with pytest.raises(ValueError, match="path separator"): validate_mist_filename("path/traversal.py") def test_rejects_backslash(self) -> None: from muse.plugins.mist.plugin import validate_mist_filename with pytest.raises(ValueError, match="path separator"): validate_mist_filename("win\\traversal.py") def test_rejects_dotdot(self) -> None: from muse.plugins.mist.plugin import validate_mist_filename with pytest.raises(ValueError, match="path traversal"): validate_mist_filename("../traversal") def test_rejects_control_characters(self) -> None: from muse.plugins.mist.plugin import validate_mist_filename for cp in range(0x01, 0x20): with pytest.raises(ValueError, match="control char"): validate_mist_filename(f"malicious{chr(cp)}.py") def test_rejects_del_character(self) -> None: from muse.plugins.mist.plugin import validate_mist_filename with pytest.raises(ValueError, match="control char"): validate_mist_filename("malicious\x7f.py") def test_rejects_ansi_escape(self) -> None: from muse.plugins.mist.plugin import validate_mist_filename with pytest.raises(ValueError, match="ANSI escape"): validate_mist_filename("\x1b[31mmalicious\x1b[0m.py") def test_rejects_name_exceeding_255_chars(self) -> None: from muse.plugins.mist.plugin import validate_mist_filename with pytest.raises(ValueError, match="255"): validate_mist_filename("a" * 256) def test_accepts_name_of_exactly_255_chars(self) -> None: from muse.plugins.mist.plugin import validate_mist_filename validate_mist_filename("a" * 255) # must not raise # --------------------------------------------------------------------------- # Tier 5 — Data integrity: extract_mist_symbol_anchors # --------------------------------------------------------------------------- class TestExtractMistSymbolAnchors: """Tests for extract_mist_symbol_anchors.""" def test_python_function_anchor(self) -> None: from muse.plugins.mist.plugin import extract_mist_symbol_anchors source = b"def add(a, b):\n return a + b\n" anchors = extract_mist_symbol_anchors("add.py", source) assert any("add" in a for a in anchors), f"Expected 'add' in {anchors}" def test_python_class_anchor(self) -> None: from muse.plugins.mist.plugin import extract_mist_symbol_anchors source = b"class Foo:\n pass\n" anchors = extract_mist_symbol_anchors("foo.py", source) assert any("Foo" in a for a in anchors), f"Expected 'Foo' in {anchors}" def test_binary_returns_empty(self) -> None: from muse.plugins.mist.plugin import extract_mist_symbol_anchors binary = bytes(range(256)) anchors = extract_mist_symbol_anchors("blob.bin", binary) assert isinstance(anchors, list) # Binary may have anchors or not; it must not raise # (FallbackAdapter may return empty or line-based symbols) def test_returns_list_always(self) -> None: from muse.plugins.mist.plugin import extract_mist_symbol_anchors for fname, content in [ ("a.py", b"x = 1"), ("b.mid", b"MThd\x00\x00"), ("c.unknown", b"\xff\xfe"), ]: result = extract_mist_symbol_anchors(fname, content) assert isinstance(result, list) def test_no_import_pseudo_symbols(self) -> None: from muse.plugins.mist.plugin import extract_mist_symbol_anchors source = b"import os\nimport sys\ndef f(): pass\n" anchors = extract_mist_symbol_anchors("f.py", source) for anchor in anchors: assert "::import::" not in anchor, f"Import symbol leaked: {anchor}" # --------------------------------------------------------------------------- # Tier 5 — Data integrity: snapshot / diff / merge / drift via in-memory paths # --------------------------------------------------------------------------- class TestMistPluginInMemory: """Validate plugin behaviour using SnapshotManifest dicts (no filesystem).""" def test_snapshot_passes_through_manifest(self, plugin: MistPlugin, empty_snap: SnapshotManifest) -> None: result = plugin.snapshot(empty_snap) assert result["domain"] == "mist" assert result["files"] == {} def test_diff_empty_to_empty_has_no_ops(self, plugin: MistPlugin, empty_snap: SnapshotManifest) -> None: delta = plugin.diff(empty_snap, empty_snap) assert delta["ops"] == [] def test_diff_add_file(self, plugin: MistPlugin, empty_snap: SnapshotManifest, snap_with_one: SnapshotManifest) -> None: delta = plugin.diff(empty_snap, snap_with_one) assert len(delta["ops"]) == 1 assert delta["ops"][0]["op"] == "insert" def test_diff_remove_file(self, plugin: MistPlugin, empty_snap: SnapshotManifest, snap_with_one: SnapshotManifest) -> None: delta = plugin.diff(snap_with_one, empty_snap) assert len(delta["ops"]) == 1 assert delta["ops"][0]["op"] == "delete" def test_merge_no_conflict_both_sides_add_different(self, plugin: MistPlugin, empty_snap: SnapshotManifest) -> None: from muse.domain import SnapshotManifest left = SnapshotManifest(files={"a.py": "hash_a"}, domain="mist", directories=[]) right = SnapshotManifest(files={"b.py": "hash_b"}, domain="mist", directories=[]) result = plugin.merge(empty_snap, left, right) assert result.conflicts == [] assert "a.py" in result.merged["files"] assert "b.py" in result.merged["files"] def test_merge_conflict_both_sides_change_same_path(self, plugin: MistPlugin) -> None: from muse.domain import SnapshotManifest base = SnapshotManifest(files={"x.py": "hash_base"}, domain="mist", directories=[]) left = SnapshotManifest(files={"x.py": "hash_left"}, domain="mist", directories=[]) right = SnapshotManifest(files={"x.py": "hash_right"}, domain="mist", directories=[]) result = plugin.merge(base, left, right) assert "x.py" in result.conflicts def test_merge_no_conflict_same_add_both_sides(self, plugin: MistPlugin, empty_snap: SnapshotManifest) -> None: """Both sides adding the same mist (same content) is not a conflict.""" from muse.domain import SnapshotManifest left = SnapshotManifest(files={"z.py": "hash_z"}, domain="mist", directories=[]) right = SnapshotManifest(files={"z.py": "hash_z"}, domain="mist", directories=[]) result = plugin.merge(empty_snap, left, right) assert result.conflicts == [] assert result.merged["files"]["z.py"] == "hash_z" def test_drift_no_drift_when_identical(self, plugin: MistPlugin, snap_with_one: SnapshotManifest) -> None: report = plugin.drift(snap_with_one, snap_with_one) assert not report.has_drift def test_drift_detects_change(self, plugin: MistPlugin, empty_snap: SnapshotManifest, snap_with_one: SnapshotManifest) -> None: report = plugin.drift(empty_snap, snap_with_one) assert report.has_drift def test_apply_returns_live_state_unchanged(self, plugin: MistPlugin, empty_snap: SnapshotManifest) -> None: delta = plugin.diff(empty_snap, empty_snap) result = plugin.apply(delta, empty_snap) assert result is empty_snap # --------------------------------------------------------------------------- # Tier 5 — Data integrity: filesystem snapshot # --------------------------------------------------------------------------- class TestMistPluginFilesystemSnapshot: """Snapshot of a real directory on disk.""" def test_snapshot_empty_directory(self, plugin: MistPlugin, tmp_path: pathlib.Path) -> None: from muse.domain import SnapshotManifest snap = plugin.snapshot(tmp_path) assert isinstance(snap, dict) assert snap["domain"] == "mist" assert snap["files"] == {} def test_snapshot_single_file(self, plugin: MistPlugin, tmp_path: pathlib.Path) -> None: f = tmp_path / "hello.py" f.write_bytes(b"print('hello')") snap = plugin.snapshot(tmp_path) assert "hello.py" in snap["files"] assert isinstance(snap["files"]["hello.py"], str) def test_snapshot_hidden_files_excluded(self, plugin: MistPlugin, tmp_path: pathlib.Path) -> None: hidden = tmp_path / ".hidden" hidden.write_bytes(b"secret") visible = tmp_path / "visible.txt" visible.write_bytes(b"public") snap = plugin.snapshot(tmp_path) assert ".hidden" not in snap["files"] assert "visible.txt" in snap["files"] def test_snapshot_nested_files_included(self, plugin: MistPlugin, tmp_path: pathlib.Path) -> None: subdir = tmp_path / "subdir" subdir.mkdir() (subdir / "nested.py").write_bytes(b"x = 1") snap = plugin.snapshot(tmp_path) assert "subdir/nested.py" in snap["files"] def test_drift_detects_new_file_on_disk(self, plugin: MistPlugin, tmp_path: pathlib.Path) -> None: from muse.domain import SnapshotManifest committed = SnapshotManifest(files={}, domain="mist", directories=[]) (tmp_path / "new.py").write_bytes(b"def new(): pass") report = plugin.drift(committed, tmp_path) assert report.has_drift # --------------------------------------------------------------------------- # Tier 6 — Performance # --------------------------------------------------------------------------- class TestMistPluginPerformance: """Ensure compute_mist_id is fast enough for large artifacts.""" def test_compute_mist_id_1mb_under_100ms(self) -> None: from muse.plugins.mist.plugin import compute_mist_id blob = b"x" * (1024 * 1024) # 1 MiB start = time.perf_counter() mist_id = compute_mist_id(blob) elapsed = time.perf_counter() - start assert len(mist_id) == 12 assert elapsed < 0.100, f"compute_mist_id took {elapsed:.3f}s on 1 MiB blob" def test_snapshot_1000_files_under_5s(self, plugin: MistPlugin, tmp_path: pathlib.Path) -> None: for i in range(1000): (tmp_path / f"mist_{i:04d}.py").write_bytes(f"x = {i}".encode()) start = time.perf_counter() snap = plugin.snapshot(tmp_path) elapsed = time.perf_counter() - start assert len(snap["files"]) == 1000 assert elapsed < 5.0, f"snapshot of 1000 files took {elapsed:.3f}s" # --------------------------------------------------------------------------- # Tier 8 — Docstring completeness # --------------------------------------------------------------------------- class TestMistPluginDocstrings: """Every public symbol in plugin.py must carry a non-empty docstring.""" PUBLIC_FUNCTIONS = ( "compute_mist_id", "detect_artifact_type", "validate_mist_filename", "extract_mist_symbol_anchors", ) PLUGIN_METHODS = ( "snapshot", "diff", "merge", "drift", "apply", "schema", ) def test_module_docstring(self) -> None: import muse.plugins.mist.plugin as mod assert mod.__doc__ and len(mod.__doc__.strip()) > 0 def test_mist_plugin_class_docstring(self) -> None: from muse.plugins.mist.plugin import MistPlugin assert MistPlugin.__doc__ and len(MistPlugin.__doc__.strip()) > 0 @pytest.mark.parametrize("func_name", PUBLIC_FUNCTIONS) def test_function_has_docstring(self, func_name: str) -> None: import muse.plugins.mist.plugin as mod fn = getattr(mod, func_name) assert fn.__doc__ and len(fn.__doc__.strip()) > 0, ( f"{func_name} is missing a docstring" ) @pytest.mark.parametrize("method_name", PLUGIN_METHODS) def test_plugin_method_has_docstring(self, method_name: str) -> None: from muse.plugins.mist.plugin import MistPlugin method = getattr(MistPlugin, method_name) assert method.__doc__ and len(method.__doc__.strip()) > 0, ( f"MistPlugin.{method_name} is missing a docstring" )