test_mist_plugin.py
python
sha256:f1f585ee9ca4e1ada936668c1b14f42f961a1fa78a2c033b643595f9c1bf9ac7
fixes for proposal flow
Human
patch
1 day ago
| 1 | """Tests for the Mist domain plugin — Phase 1. |
| 2 | |
| 3 | Test tiers covered |
| 4 | ------------------ |
| 5 | Tier 1 — Shape / API surface |
| 6 | MistPlugin satisfies MuseDomainPlugin; all 6 required methods present; |
| 7 | schema() returns a well-formed DomainSchema. |
| 8 | |
| 9 | Tier 5 — Data integrity |
| 10 | compute_mist_id: determinism, uniqueness, length, alphabet; |
| 11 | detect_artifact_type: magic bytes, JSON key inspection, extension fallback; |
| 12 | validate_mist_filename: accepts valid names, rejects all attack vectors; |
| 13 | extract_mist_symbol_anchors: anchors for Python source, empty for binary. |
| 14 | |
| 15 | Tier 6 — Performance |
| 16 | compute_mist_id on a 1 MiB blob completes in under 100 ms. |
| 17 | |
| 18 | Tier 8 — Docstring completeness |
| 19 | All public symbols in plugin.py carry a docstring. |
| 20 | """ |
| 21 | |
| 22 | from __future__ import annotations |
| 23 | |
| 24 | import inspect |
| 25 | import pathlib |
| 26 | import sys |
| 27 | import time |
| 28 | |
| 29 | from muse.core.types import blob_id |
| 30 | from muse.domain import SnapshotManifest |
| 31 | from muse.plugins.mist.plugin import MistPlugin |
| 32 | |
| 33 | import pytest |
| 34 | |
| 35 | # --------------------------------------------------------------------------- |
| 36 | # Fixtures |
| 37 | # --------------------------------------------------------------------------- |
| 38 | |
| 39 | |
| 40 | @pytest.fixture() |
| 41 | def plugin() -> MistPlugin: |
| 42 | return MistPlugin() |
| 43 | |
| 44 | |
| 45 | @pytest.fixture() |
| 46 | def empty_snap() -> SnapshotManifest: |
| 47 | return SnapshotManifest(files={}, domain="mist", directories=[]) |
| 48 | |
| 49 | |
| 50 | @pytest.fixture() |
| 51 | def snap_with_one(tmp_path: pathlib.Path) -> SnapshotManifest: |
| 52 | """A SnapshotManifest containing one file keyed by its SHA-256 hex digest.""" |
| 53 | content = b"hello mist" |
| 54 | digest = blob_id(content) |
| 55 | return SnapshotManifest(files={"aB3xQ9fWmK2r.py": digest}, domain="mist", directories=[]) |
| 56 | |
| 57 | |
| 58 | # --------------------------------------------------------------------------- |
| 59 | # Tier 1 — Shape / API surface |
| 60 | # --------------------------------------------------------------------------- |
| 61 | |
| 62 | |
| 63 | class TestMistPluginShape: |
| 64 | """Verify MistPlugin satisfies the MuseDomainPlugin protocol.""" |
| 65 | |
| 66 | REQUIRED_METHODS = ("snapshot", "diff", "merge", "drift", "apply", "schema") |
| 67 | |
| 68 | def test_all_required_methods_present(self, plugin: MistPlugin) -> None: |
| 69 | for method in self.REQUIRED_METHODS: |
| 70 | assert hasattr(plugin, method), f"MistPlugin missing method: {method}" |
| 71 | assert callable(getattr(plugin, method)) |
| 72 | |
| 73 | def test_schema_returns_domain_schema(self, plugin: MistPlugin) -> None: |
| 74 | schema = plugin.schema() |
| 75 | # DomainSchema is a TypedDict (a dict subclass) — check required keys |
| 76 | assert isinstance(schema, dict) |
| 77 | assert "domain" in schema |
| 78 | assert "description" in schema |
| 79 | assert "top_level" in schema |
| 80 | assert "dimensions" in schema |
| 81 | assert "merge_mode" in schema |
| 82 | |
| 83 | def test_schema_domain_is_mist(self, plugin: MistPlugin) -> None: |
| 84 | assert plugin.schema()["domain"] == "mist" |
| 85 | |
| 86 | def test_schema_top_level_is_set(self, plugin: MistPlugin) -> None: |
| 87 | top = plugin.schema()["top_level"] |
| 88 | # SetSchema is a TypedDict |
| 89 | assert isinstance(top, dict) |
| 90 | assert top["kind"] == "set" |
| 91 | assert top["element_type"] == "artifact" |
| 92 | assert top["identity"] == "by_content" |
| 93 | |
| 94 | def test_schema_has_two_dimensions(self, plugin: MistPlugin) -> None: |
| 95 | dims = plugin.schema()["dimensions"] |
| 96 | assert len(dims) == 2 |
| 97 | names = {d["name"] for d in dims} |
| 98 | assert names == {"artifacts", "metadata"} |
| 99 | |
| 100 | def test_schema_merge_mode_three_way(self, plugin: MistPlugin) -> None: |
| 101 | assert plugin.schema()["merge_mode"] == "three_way" |
| 102 | |
| 103 | def test_schema_version_is_string(self, plugin: MistPlugin) -> None: |
| 104 | assert isinstance(plugin.schema()["schema_version"], str) |
| 105 | assert len(plugin.schema()["schema_version"]) > 0 |
| 106 | |
| 107 | def test_registered_in_registry(self) -> None: |
| 108 | from muse.plugins.registry import _REGISTRY |
| 109 | |
| 110 | assert "mist" in _REGISTRY |
| 111 | from muse.plugins.mist.plugin import MistPlugin |
| 112 | |
| 113 | assert isinstance(_REGISTRY["mist"], MistPlugin) |
| 114 | |
| 115 | def test_resolve_plugin_by_domain(self) -> None: |
| 116 | from muse.plugins.registry import resolve_plugin_by_domain |
| 117 | from muse.plugins.mist.plugin import MistPlugin |
| 118 | |
| 119 | plugin = resolve_plugin_by_domain("mist") |
| 120 | assert isinstance(plugin, MistPlugin) |
| 121 | |
| 122 | def test_registered_domains_includes_mist(self) -> None: |
| 123 | from muse.plugins.registry import registered_domains |
| 124 | |
| 125 | assert "mist" in registered_domains() |
| 126 | |
| 127 | |
| 128 | # --------------------------------------------------------------------------- |
| 129 | # Tier 5 — Data integrity: compute_mist_id |
| 130 | # --------------------------------------------------------------------------- |
| 131 | |
| 132 | |
| 133 | class TestComputeMistId: |
| 134 | """Tests for the compute_mist_id pure function.""" |
| 135 | |
| 136 | def test_deterministic(self) -> None: |
| 137 | from muse.plugins.mist.plugin import compute_mist_id |
| 138 | |
| 139 | content = b"repeatability is key" |
| 140 | assert compute_mist_id(content) == compute_mist_id(content) |
| 141 | |
| 142 | def test_length_is_12(self) -> None: |
| 143 | from muse.plugins.mist.plugin import compute_mist_id |
| 144 | |
| 145 | assert len(compute_mist_id(b"")) == 12 |
| 146 | assert len(compute_mist_id(b"x" * 1_000_000)) == 12 |
| 147 | |
| 148 | def test_only_base58_alphabet(self) -> None: |
| 149 | from muse.plugins.mist.plugin import _BASE58_ALPHABET, compute_mist_id |
| 150 | |
| 151 | for content in (b"", b"a", b"\x00" * 32, b"\xff" * 32): |
| 152 | mist_id = compute_mist_id(content) |
| 153 | for ch in mist_id: |
| 154 | assert ch in _BASE58_ALPHABET, f"Unexpected char {ch!r} in mist_id {mist_id!r}" |
| 155 | |
| 156 | def test_no_ambiguous_chars(self) -> None: |
| 157 | from muse.plugins.mist.plugin import compute_mist_id |
| 158 | |
| 159 | ambiguous = set("0OIl") |
| 160 | for i in range(256): |
| 161 | mist_id = compute_mist_id(bytes([i])) |
| 162 | for ch in mist_id: |
| 163 | assert ch not in ambiguous, ( |
| 164 | f"Ambiguous char {ch!r} found in mist_id {mist_id!r} for byte {i}" |
| 165 | ) |
| 166 | |
| 167 | def test_uniqueness_across_different_content(self) -> None: |
| 168 | from muse.plugins.mist.plugin import compute_mist_id |
| 169 | |
| 170 | ids = {compute_mist_id(f"artifact_{i}".encode()) for i in range(200)} |
| 171 | assert len(ids) == 200, "mist IDs collided for distinct content" |
| 172 | |
| 173 | def test_empty_bytes_stable_id(self) -> None: |
| 174 | """Empty content always maps to the same ID (regression guard).""" |
| 175 | from muse.plugins.mist.plugin import compute_mist_id |
| 176 | |
| 177 | id1 = compute_mist_id(b"") |
| 178 | id2 = compute_mist_id(b"") |
| 179 | assert id1 == id2 |
| 180 | |
| 181 | def test_single_bit_change_produces_different_id(self) -> None: |
| 182 | from muse.plugins.mist.plugin import compute_mist_id |
| 183 | |
| 184 | base = b"hello" |
| 185 | modified = b"hfllo" |
| 186 | assert compute_mist_id(base) != compute_mist_id(modified) |
| 187 | |
| 188 | |
| 189 | # --------------------------------------------------------------------------- |
| 190 | # Tier 5 — Data integrity: detect_artifact_type |
| 191 | # --------------------------------------------------------------------------- |
| 192 | |
| 193 | |
| 194 | class TestDetectArtifactType: |
| 195 | """Tests for the detect_artifact_type pure function.""" |
| 196 | |
| 197 | def test_midi_magic_bytes(self) -> None: |
| 198 | from muse.plugins.mist.plugin import detect_artifact_type |
| 199 | |
| 200 | result = detect_artifact_type("track.mid", b"MThd\x00\x00\x00\x06\x00\x01") |
| 201 | assert result == {"artifact_type": "midi", "language": "midi"} |
| 202 | |
| 203 | def test_midi_magic_bytes_wrong_extension(self) -> None: |
| 204 | """Magic bytes take priority over extension.""" |
| 205 | from muse.plugins.mist.plugin import detect_artifact_type |
| 206 | |
| 207 | result = detect_artifact_type("track.dat", b"MThd\x00\x00\x00\x06\x00\x01") |
| 208 | assert result == {"artifact_type": "midi", "language": "midi"} |
| 209 | |
| 210 | def test_abi_json(self) -> None: |
| 211 | import json |
| 212 | from muse.plugins.mist.plugin import detect_artifact_type |
| 213 | |
| 214 | abi = json.dumps([{"type": "function", "name": "transfer", "inputs": []}]).encode() |
| 215 | result = detect_artifact_type("contract.abi.json", abi) |
| 216 | assert result == {"artifact_type": "abi", "language": "json"} |
| 217 | |
| 218 | def test_json_schema(self) -> None: |
| 219 | import json |
| 220 | from muse.plugins.mist.plugin import detect_artifact_type |
| 221 | |
| 222 | schema = json.dumps({"$schema": "http://json-schema.org/draft-07/schema#"}).encode() |
| 223 | result = detect_artifact_type("schema.json", schema) |
| 224 | assert result == {"artifact_type": "json_schema", "language": "json"} |
| 225 | |
| 226 | def test_python_extension(self) -> None: |
| 227 | from muse.plugins.mist.plugin import detect_artifact_type |
| 228 | |
| 229 | result = detect_artifact_type("utils.py", b"def add(a, b): return a + b") |
| 230 | assert result == {"artifact_type": "code", "language": "python"} |
| 231 | |
| 232 | def test_typescript_extension(self) -> None: |
| 233 | from muse.plugins.mist.plugin import detect_artifact_type |
| 234 | |
| 235 | result = detect_artifact_type("app.ts", b"export function hello(): void {}") |
| 236 | assert result == {"artifact_type": "code", "language": "typescript"} |
| 237 | |
| 238 | def test_markdown_extension(self) -> None: |
| 239 | from muse.plugins.mist.plugin import detect_artifact_type |
| 240 | |
| 241 | result = detect_artifact_type("README.md", b"# Hello\n\nWorld") |
| 242 | assert result == {"artifact_type": "code", "language": "markdown"} |
| 243 | |
| 244 | def test_solidity_extension(self) -> None: |
| 245 | from muse.plugins.mist.plugin import detect_artifact_type |
| 246 | |
| 247 | result = detect_artifact_type("Token.sol", b"// SPDX-License-Identifier: MIT") |
| 248 | assert result == {"artifact_type": "code", "language": "solidity"} |
| 249 | |
| 250 | def test_unknown_extension_fallback(self) -> None: |
| 251 | from muse.plugins.mist.plugin import detect_artifact_type |
| 252 | |
| 253 | result = detect_artifact_type("blob.xyzzy", b"\xde\xad\xbe\xef") |
| 254 | assert result == {"artifact_type": "unknown", "language": "binary"} |
| 255 | |
| 256 | def test_returns_dict_with_required_keys(self) -> None: |
| 257 | from muse.plugins.mist.plugin import detect_artifact_type |
| 258 | |
| 259 | for fname in ("a.py", "b.mid", "c.json", "d.unknown"): |
| 260 | result = detect_artifact_type(fname, b"content") |
| 261 | assert "artifact_type" in result |
| 262 | assert "language" in result |
| 263 | |
| 264 | |
| 265 | # --------------------------------------------------------------------------- |
| 266 | # Tier 5 — Data integrity: validate_mist_filename |
| 267 | # --------------------------------------------------------------------------- |
| 268 | |
| 269 | |
| 270 | class TestValidateMistFilename: |
| 271 | """Tests for the validate_mist_filename security gate.""" |
| 272 | |
| 273 | def test_valid_simple_name(self) -> None: |
| 274 | from muse.plugins.mist.plugin import validate_mist_filename |
| 275 | |
| 276 | validate_mist_filename("aB3xQ9fWmK2r.py") # must not raise |
| 277 | |
| 278 | def test_valid_name_with_dots_and_dashes(self) -> None: |
| 279 | from muse.plugins.mist.plugin import validate_mist_filename |
| 280 | |
| 281 | validate_mist_filename("my-artifact.abi.json") # must not raise |
| 282 | |
| 283 | def test_rejects_null_byte(self) -> None: |
| 284 | from muse.plugins.mist.plugin import validate_mist_filename |
| 285 | |
| 286 | with pytest.raises(ValueError, match="null byte"): |
| 287 | validate_mist_filename("malicious\x00.py") |
| 288 | |
| 289 | def test_rejects_forward_slash(self) -> None: |
| 290 | from muse.plugins.mist.plugin import validate_mist_filename |
| 291 | |
| 292 | with pytest.raises(ValueError, match="path separator"): |
| 293 | validate_mist_filename("path/traversal.py") |
| 294 | |
| 295 | def test_rejects_backslash(self) -> None: |
| 296 | from muse.plugins.mist.plugin import validate_mist_filename |
| 297 | |
| 298 | with pytest.raises(ValueError, match="path separator"): |
| 299 | validate_mist_filename("win\\traversal.py") |
| 300 | |
| 301 | def test_rejects_dotdot(self) -> None: |
| 302 | from muse.plugins.mist.plugin import validate_mist_filename |
| 303 | |
| 304 | with pytest.raises(ValueError, match="path traversal"): |
| 305 | validate_mist_filename("../traversal") |
| 306 | |
| 307 | def test_rejects_control_characters(self) -> None: |
| 308 | from muse.plugins.mist.plugin import validate_mist_filename |
| 309 | |
| 310 | for cp in range(0x01, 0x20): |
| 311 | with pytest.raises(ValueError, match="control char"): |
| 312 | validate_mist_filename(f"malicious{chr(cp)}.py") |
| 313 | |
| 314 | def test_rejects_del_character(self) -> None: |
| 315 | from muse.plugins.mist.plugin import validate_mist_filename |
| 316 | |
| 317 | with pytest.raises(ValueError, match="control char"): |
| 318 | validate_mist_filename("malicious\x7f.py") |
| 319 | |
| 320 | def test_rejects_ansi_escape(self) -> None: |
| 321 | from muse.plugins.mist.plugin import validate_mist_filename |
| 322 | |
| 323 | with pytest.raises(ValueError, match="ANSI escape"): |
| 324 | validate_mist_filename("\x1b[31mmalicious\x1b[0m.py") |
| 325 | |
| 326 | def test_rejects_name_exceeding_255_chars(self) -> None: |
| 327 | from muse.plugins.mist.plugin import validate_mist_filename |
| 328 | |
| 329 | with pytest.raises(ValueError, match="255"): |
| 330 | validate_mist_filename("a" * 256) |
| 331 | |
| 332 | def test_accepts_name_of_exactly_255_chars(self) -> None: |
| 333 | from muse.plugins.mist.plugin import validate_mist_filename |
| 334 | |
| 335 | validate_mist_filename("a" * 255) # must not raise |
| 336 | |
| 337 | |
| 338 | # --------------------------------------------------------------------------- |
| 339 | # Tier 5 — Data integrity: extract_mist_symbol_anchors |
| 340 | # --------------------------------------------------------------------------- |
| 341 | |
| 342 | |
| 343 | class TestExtractMistSymbolAnchors: |
| 344 | """Tests for extract_mist_symbol_anchors.""" |
| 345 | |
| 346 | def test_python_function_anchor(self) -> None: |
| 347 | from muse.plugins.mist.plugin import extract_mist_symbol_anchors |
| 348 | |
| 349 | source = b"def add(a, b):\n return a + b\n" |
| 350 | anchors = extract_mist_symbol_anchors("add.py", source) |
| 351 | assert any("add" in a for a in anchors), f"Expected 'add' in {anchors}" |
| 352 | |
| 353 | def test_python_class_anchor(self) -> None: |
| 354 | from muse.plugins.mist.plugin import extract_mist_symbol_anchors |
| 355 | |
| 356 | source = b"class Foo:\n pass\n" |
| 357 | anchors = extract_mist_symbol_anchors("foo.py", source) |
| 358 | assert any("Foo" in a for a in anchors), f"Expected 'Foo' in {anchors}" |
| 359 | |
| 360 | def test_binary_returns_empty(self) -> None: |
| 361 | from muse.plugins.mist.plugin import extract_mist_symbol_anchors |
| 362 | |
| 363 | binary = bytes(range(256)) |
| 364 | anchors = extract_mist_symbol_anchors("blob.bin", binary) |
| 365 | assert isinstance(anchors, list) |
| 366 | # Binary may have anchors or not; it must not raise |
| 367 | # (FallbackAdapter may return empty or line-based symbols) |
| 368 | |
| 369 | def test_returns_list_always(self) -> None: |
| 370 | from muse.plugins.mist.plugin import extract_mist_symbol_anchors |
| 371 | |
| 372 | for fname, content in [ |
| 373 | ("a.py", b"x = 1"), |
| 374 | ("b.mid", b"MThd\x00\x00"), |
| 375 | ("c.unknown", b"\xff\xfe"), |
| 376 | ]: |
| 377 | result = extract_mist_symbol_anchors(fname, content) |
| 378 | assert isinstance(result, list) |
| 379 | |
| 380 | def test_no_import_pseudo_symbols(self) -> None: |
| 381 | from muse.plugins.mist.plugin import extract_mist_symbol_anchors |
| 382 | |
| 383 | source = b"import os\nimport sys\ndef f(): pass\n" |
| 384 | anchors = extract_mist_symbol_anchors("f.py", source) |
| 385 | for anchor in anchors: |
| 386 | assert "::import::" not in anchor, f"Import symbol leaked: {anchor}" |
| 387 | |
| 388 | |
| 389 | # --------------------------------------------------------------------------- |
| 390 | # Tier 5 — Data integrity: snapshot / diff / merge / drift via in-memory paths |
| 391 | # --------------------------------------------------------------------------- |
| 392 | |
| 393 | |
| 394 | class TestMistPluginInMemory: |
| 395 | """Validate plugin behaviour using SnapshotManifest dicts (no filesystem).""" |
| 396 | |
| 397 | def test_snapshot_passes_through_manifest(self, plugin: MistPlugin, empty_snap: SnapshotManifest) -> None: |
| 398 | result = plugin.snapshot(empty_snap) |
| 399 | assert result["domain"] == "mist" |
| 400 | assert result["files"] == {} |
| 401 | |
| 402 | def test_diff_empty_to_empty_has_no_ops(self, plugin: MistPlugin, empty_snap: SnapshotManifest) -> None: |
| 403 | delta = plugin.diff(empty_snap, empty_snap) |
| 404 | assert delta["ops"] == [] |
| 405 | |
| 406 | def test_diff_add_file(self, plugin: MistPlugin, empty_snap: SnapshotManifest, snap_with_one: SnapshotManifest) -> None: |
| 407 | delta = plugin.diff(empty_snap, snap_with_one) |
| 408 | assert len(delta["ops"]) == 1 |
| 409 | assert delta["ops"][0]["op"] == "insert" |
| 410 | |
| 411 | def test_diff_remove_file(self, plugin: MistPlugin, empty_snap: SnapshotManifest, snap_with_one: SnapshotManifest) -> None: |
| 412 | delta = plugin.diff(snap_with_one, empty_snap) |
| 413 | assert len(delta["ops"]) == 1 |
| 414 | assert delta["ops"][0]["op"] == "delete" |
| 415 | |
| 416 | def test_merge_no_conflict_both_sides_add_different(self, plugin: MistPlugin, empty_snap: SnapshotManifest) -> None: |
| 417 | from muse.domain import SnapshotManifest |
| 418 | |
| 419 | left = SnapshotManifest(files={"a.py": "hash_a"}, domain="mist", directories=[]) |
| 420 | right = SnapshotManifest(files={"b.py": "hash_b"}, domain="mist", directories=[]) |
| 421 | result = plugin.merge(empty_snap, left, right) |
| 422 | assert result.conflicts == [] |
| 423 | assert "a.py" in result.merged["files"] |
| 424 | assert "b.py" in result.merged["files"] |
| 425 | |
| 426 | def test_merge_conflict_both_sides_change_same_path(self, plugin: MistPlugin) -> None: |
| 427 | from muse.domain import SnapshotManifest |
| 428 | |
| 429 | base = SnapshotManifest(files={"x.py": "hash_base"}, domain="mist", directories=[]) |
| 430 | left = SnapshotManifest(files={"x.py": "hash_left"}, domain="mist", directories=[]) |
| 431 | right = SnapshotManifest(files={"x.py": "hash_right"}, domain="mist", directories=[]) |
| 432 | result = plugin.merge(base, left, right) |
| 433 | assert "x.py" in result.conflicts |
| 434 | |
| 435 | def test_merge_no_conflict_same_add_both_sides(self, plugin: MistPlugin, empty_snap: SnapshotManifest) -> None: |
| 436 | """Both sides adding the same mist (same content) is not a conflict.""" |
| 437 | from muse.domain import SnapshotManifest |
| 438 | |
| 439 | left = SnapshotManifest(files={"z.py": "hash_z"}, domain="mist", directories=[]) |
| 440 | right = SnapshotManifest(files={"z.py": "hash_z"}, domain="mist", directories=[]) |
| 441 | result = plugin.merge(empty_snap, left, right) |
| 442 | assert result.conflicts == [] |
| 443 | assert result.merged["files"]["z.py"] == "hash_z" |
| 444 | |
| 445 | def test_drift_no_drift_when_identical(self, plugin: MistPlugin, snap_with_one: SnapshotManifest) -> None: |
| 446 | report = plugin.drift(snap_with_one, snap_with_one) |
| 447 | assert not report.has_drift |
| 448 | |
| 449 | def test_drift_detects_change(self, plugin: MistPlugin, empty_snap: SnapshotManifest, snap_with_one: SnapshotManifest) -> None: |
| 450 | report = plugin.drift(empty_snap, snap_with_one) |
| 451 | assert report.has_drift |
| 452 | |
| 453 | def test_apply_returns_live_state_unchanged(self, plugin: MistPlugin, empty_snap: SnapshotManifest) -> None: |
| 454 | delta = plugin.diff(empty_snap, empty_snap) |
| 455 | result = plugin.apply(delta, empty_snap) |
| 456 | assert result is empty_snap |
| 457 | |
| 458 | |
| 459 | # --------------------------------------------------------------------------- |
| 460 | # Tier 5 — Data integrity: filesystem snapshot |
| 461 | # --------------------------------------------------------------------------- |
| 462 | |
| 463 | |
| 464 | class TestMistPluginFilesystemSnapshot: |
| 465 | """Snapshot of a real directory on disk.""" |
| 466 | |
| 467 | def test_snapshot_empty_directory(self, plugin: MistPlugin, tmp_path: pathlib.Path) -> None: |
| 468 | from muse.domain import SnapshotManifest |
| 469 | |
| 470 | snap = plugin.snapshot(tmp_path) |
| 471 | assert isinstance(snap, dict) |
| 472 | assert snap["domain"] == "mist" |
| 473 | assert snap["files"] == {} |
| 474 | |
| 475 | def test_snapshot_single_file(self, plugin: MistPlugin, tmp_path: pathlib.Path) -> None: |
| 476 | f = tmp_path / "hello.py" |
| 477 | f.write_bytes(b"print('hello')") |
| 478 | snap = plugin.snapshot(tmp_path) |
| 479 | assert "hello.py" in snap["files"] |
| 480 | assert isinstance(snap["files"]["hello.py"], str) |
| 481 | |
| 482 | def test_snapshot_hidden_files_excluded(self, plugin: MistPlugin, tmp_path: pathlib.Path) -> None: |
| 483 | hidden = tmp_path / ".hidden" |
| 484 | hidden.write_bytes(b"secret") |
| 485 | visible = tmp_path / "visible.txt" |
| 486 | visible.write_bytes(b"public") |
| 487 | snap = plugin.snapshot(tmp_path) |
| 488 | assert ".hidden" not in snap["files"] |
| 489 | assert "visible.txt" in snap["files"] |
| 490 | |
| 491 | def test_snapshot_nested_files_included(self, plugin: MistPlugin, tmp_path: pathlib.Path) -> None: |
| 492 | subdir = tmp_path / "subdir" |
| 493 | subdir.mkdir() |
| 494 | (subdir / "nested.py").write_bytes(b"x = 1") |
| 495 | snap = plugin.snapshot(tmp_path) |
| 496 | assert "subdir/nested.py" in snap["files"] |
| 497 | |
| 498 | def test_drift_detects_new_file_on_disk(self, plugin: MistPlugin, tmp_path: pathlib.Path) -> None: |
| 499 | from muse.domain import SnapshotManifest |
| 500 | |
| 501 | committed = SnapshotManifest(files={}, domain="mist", directories=[]) |
| 502 | (tmp_path / "new.py").write_bytes(b"def new(): pass") |
| 503 | report = plugin.drift(committed, tmp_path) |
| 504 | assert report.has_drift |
| 505 | |
| 506 | |
| 507 | # --------------------------------------------------------------------------- |
| 508 | # Tier 6 — Performance |
| 509 | # --------------------------------------------------------------------------- |
| 510 | |
| 511 | |
| 512 | class TestMistPluginPerformance: |
| 513 | """Ensure compute_mist_id is fast enough for large artifacts.""" |
| 514 | |
| 515 | def test_compute_mist_id_1mb_under_100ms(self) -> None: |
| 516 | from muse.plugins.mist.plugin import compute_mist_id |
| 517 | |
| 518 | blob = b"x" * (1024 * 1024) # 1 MiB |
| 519 | start = time.perf_counter() |
| 520 | mist_id = compute_mist_id(blob) |
| 521 | elapsed = time.perf_counter() - start |
| 522 | assert len(mist_id) == 12 |
| 523 | assert elapsed < 0.100, f"compute_mist_id took {elapsed:.3f}s on 1 MiB blob" |
| 524 | |
| 525 | def test_snapshot_1000_files_under_5s(self, plugin: MistPlugin, tmp_path: pathlib.Path) -> None: |
| 526 | for i in range(1000): |
| 527 | (tmp_path / f"mist_{i:04d}.py").write_bytes(f"x = {i}".encode()) |
| 528 | start = time.perf_counter() |
| 529 | snap = plugin.snapshot(tmp_path) |
| 530 | elapsed = time.perf_counter() - start |
| 531 | assert len(snap["files"]) == 1000 |
| 532 | assert elapsed < 5.0, f"snapshot of 1000 files took {elapsed:.3f}s" |
| 533 | |
| 534 | |
| 535 | # --------------------------------------------------------------------------- |
| 536 | # Tier 8 — Docstring completeness |
| 537 | # --------------------------------------------------------------------------- |
| 538 | |
| 539 | |
| 540 | class TestMistPluginDocstrings: |
| 541 | """Every public symbol in plugin.py must carry a non-empty docstring.""" |
| 542 | |
| 543 | PUBLIC_FUNCTIONS = ( |
| 544 | "compute_mist_id", |
| 545 | "detect_artifact_type", |
| 546 | "validate_mist_filename", |
| 547 | "extract_mist_symbol_anchors", |
| 548 | ) |
| 549 | |
| 550 | PLUGIN_METHODS = ( |
| 551 | "snapshot", |
| 552 | "diff", |
| 553 | "merge", |
| 554 | "drift", |
| 555 | "apply", |
| 556 | "schema", |
| 557 | ) |
| 558 | |
| 559 | def test_module_docstring(self) -> None: |
| 560 | import muse.plugins.mist.plugin as mod |
| 561 | |
| 562 | assert mod.__doc__ and len(mod.__doc__.strip()) > 0 |
| 563 | |
| 564 | def test_mist_plugin_class_docstring(self) -> None: |
| 565 | from muse.plugins.mist.plugin import MistPlugin |
| 566 | |
| 567 | assert MistPlugin.__doc__ and len(MistPlugin.__doc__.strip()) > 0 |
| 568 | |
| 569 | @pytest.mark.parametrize("func_name", PUBLIC_FUNCTIONS) |
| 570 | def test_function_has_docstring(self, func_name: str) -> None: |
| 571 | import muse.plugins.mist.plugin as mod |
| 572 | |
| 573 | fn = getattr(mod, func_name) |
| 574 | assert fn.__doc__ and len(fn.__doc__.strip()) > 0, ( |
| 575 | f"{func_name} is missing a docstring" |
| 576 | ) |
| 577 | |
| 578 | @pytest.mark.parametrize("method_name", PLUGIN_METHODS) |
| 579 | def test_plugin_method_has_docstring(self, method_name: str) -> None: |
| 580 | from muse.plugins.mist.plugin import MistPlugin |
| 581 | |
| 582 | method = getattr(MistPlugin, method_name) |
| 583 | assert method.__doc__ and len(method.__doc__.strip()) > 0, ( |
| 584 | f"MistPlugin.{method_name} is missing a docstring" |
| 585 | ) |
File History
1 commit
sha256:f1f585ee9ca4e1ada936668c1b14f42f961a1fa78a2c033b643595f9c1bf9ac7
fixes for proposal flow
Human
patch
1 day ago