test_midi_plugin.py
python
| 1 | """Tests for muse.plugins.midi.plugin — the MuseDomainPlugin reference implementation.""" |
| 2 | |
| 3 | import pathlib |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from muse.domain import DriftReport, MergeResult, MuseDomainPlugin, SnapshotManifest |
| 8 | from muse.plugins.midi.plugin import MidiPlugin, content_hash, plugin |
| 9 | |
| 10 | |
| 11 | def _snap(files: dict[str, str]) -> SnapshotManifest: |
| 12 | return SnapshotManifest(files=files, domain="midi") |
| 13 | |
| 14 | |
| 15 | class TestProtocolConformance: |
| 16 | def test_plugin_satisfies_protocol(self) -> None: |
| 17 | assert isinstance(plugin, MuseDomainPlugin) |
| 18 | |
| 19 | def test_module_singleton_is_music_plugin(self) -> None: |
| 20 | assert isinstance(plugin, MidiPlugin) |
| 21 | |
| 22 | |
| 23 | class TestSnapshot: |
| 24 | def test_from_dict_passthrough(self) -> None: |
| 25 | snap = SnapshotManifest(files={"a.mid": "h1"}, domain="midi") |
| 26 | assert plugin.snapshot(snap) is snap |
| 27 | |
| 28 | def test_from_workdir(self, tmp_path: pathlib.Path) -> None: |
| 29 | workdir = tmp_path |
| 30 | (workdir / "beat.mid").write_bytes(b"drums") |
| 31 | snap = plugin.snapshot(workdir) |
| 32 | assert "files" in snap |
| 33 | assert "beat.mid" in snap["files"] |
| 34 | assert snap["domain"] == "midi" |
| 35 | |
| 36 | def test_empty_workdir(self, tmp_path: pathlib.Path) -> None: |
| 37 | workdir = tmp_path |
| 38 | snap = plugin.snapshot(workdir) |
| 39 | assert snap["files"] == {} |
| 40 | |
| 41 | |
| 42 | class TestDiff: |
| 43 | """diff() returns a StructuredDelta with typed ops — not the old DeltaManifest.""" |
| 44 | |
| 45 | def test_no_change_empty_ops(self) -> None: |
| 46 | snap = _snap({"a.mid": "h1"}) |
| 47 | delta = plugin.diff(snap, snap) |
| 48 | assert delta["ops"] == [] |
| 49 | |
| 50 | def test_no_change_summary(self) -> None: |
| 51 | snap = _snap({"a.mid": "h1"}) |
| 52 | delta = plugin.diff(snap, snap) |
| 53 | assert delta["summary"] == "no changes" |
| 54 | |
| 55 | def test_added_file_produces_insert_op(self) -> None: |
| 56 | base = _snap({}) |
| 57 | target = _snap({"new.mid": "h1"}) |
| 58 | delta = plugin.diff(base, target) |
| 59 | insert_ops = [op for op in delta["ops"] if op["op"] == "insert"] |
| 60 | assert any(op["address"] == "new.mid" for op in insert_ops) |
| 61 | |
| 62 | def test_removed_file_produces_delete_op(self) -> None: |
| 63 | base = _snap({"old.mid": "h1"}) |
| 64 | target = _snap({}) |
| 65 | delta = plugin.diff(base, target) |
| 66 | delete_ops = [op for op in delta["ops"] if op["op"] == "delete"] |
| 67 | assert any(op["address"] == "old.mid" for op in delete_ops) |
| 68 | |
| 69 | def test_modified_file_produces_replace_or_patch_op(self) -> None: |
| 70 | base = _snap({"f.mid": "old"}) |
| 71 | target = _snap({"f.mid": "new"}) |
| 72 | delta = plugin.diff(base, target) |
| 73 | changed_ops = [op for op in delta["ops"] if op["op"] in ("replace", "patch")] |
| 74 | assert any(op["address"] == "f.mid" for op in changed_ops) |
| 75 | |
| 76 | def test_domain_is_music(self) -> None: |
| 77 | snap = _snap({"a.mid": "h"}) |
| 78 | delta = plugin.diff(snap, snap) |
| 79 | assert delta["domain"] == "midi" |
| 80 | |
| 81 | |
| 82 | class TestMerge: |
| 83 | def test_clean_merge(self) -> None: |
| 84 | base = _snap({"a.mid": "h0", "b.mid": "h0"}) |
| 85 | left = _snap({"a.mid": "h_left", "b.mid": "h0"}) |
| 86 | right = _snap({"a.mid": "h0", "b.mid": "h_right"}) |
| 87 | result = plugin.merge(base, left, right) |
| 88 | assert isinstance(result, MergeResult) |
| 89 | assert result.is_clean |
| 90 | assert result.merged["files"]["a.mid"] == "h_left" |
| 91 | assert result.merged["files"]["b.mid"] == "h_right" |
| 92 | |
| 93 | def test_conflict_detected(self) -> None: |
| 94 | base = _snap({"a.mid": "h0"}) |
| 95 | left = _snap({"a.mid": "h_left"}) |
| 96 | right = _snap({"a.mid": "h_right"}) |
| 97 | result = plugin.merge(base, left, right) |
| 98 | assert not result.is_clean |
| 99 | assert result.conflicts == ["a.mid"] |
| 100 | |
| 101 | def test_both_delete_same_file(self) -> None: |
| 102 | base = _snap({"a.mid": "h0", "b.mid": "h0"}) |
| 103 | left = _snap({"b.mid": "h0"}) |
| 104 | right = _snap({"b.mid": "h0"}) |
| 105 | result = plugin.merge(base, left, right) |
| 106 | assert result.is_clean |
| 107 | assert "a.mid" not in result.merged["files"] |
| 108 | |
| 109 | |
| 110 | class TestDrift: |
| 111 | def test_no_drift(self) -> None: |
| 112 | snap = _snap({"a.mid": "h1"}) |
| 113 | report = plugin.drift(snap, snap) |
| 114 | assert isinstance(report, DriftReport) |
| 115 | assert not report.has_drift |
| 116 | |
| 117 | def test_has_drift_on_addition(self) -> None: |
| 118 | committed = _snap({"a.mid": "h1"}) |
| 119 | live = _snap({"a.mid": "h1", "b.mid": "h2"}) |
| 120 | report = plugin.drift(committed, live) |
| 121 | assert report.has_drift |
| 122 | assert "added" in report.summary |
| 123 | |
| 124 | def test_drift_delta_is_structured(self) -> None: |
| 125 | snap = _snap({"a.mid": "h1"}) |
| 126 | report = plugin.drift(snap, snap) |
| 127 | assert "ops" in report.delta |
| 128 | assert "summary" in report.delta |
| 129 | assert "domain" in report.delta |
| 130 | |
| 131 | |
| 132 | class TestContentHash: |
| 133 | def test_deterministic(self) -> None: |
| 134 | snap = SnapshotManifest(files={"a.mid": "h1"}, domain="midi") |
| 135 | assert content_hash(snap) == content_hash(snap) |
| 136 | |
| 137 | def test_different_content_different_hash(self) -> None: |
| 138 | a = SnapshotManifest(files={"a.mid": "h1"}, domain="midi") |
| 139 | b = SnapshotManifest(files={"a.mid": "h2"}, domain="midi") |
| 140 | assert content_hash(a) != content_hash(b) |