"""TDD — snapshot delta reconstruction on the server side. Tests that wire_push_stream correctly: A. Accepts C frames with snapshot_deltas and reconstructs full snapshots B. Rejects a delta whose base_id is not in the stream cache C. Delta + full snapshot in same C frame both stored correctly D. Chain of deltas reconstructs all snapshots correctly E. WireSnapshotDelta model rejects invalid snapshot_id / base_id F. reconstruct_snapshot_delta returns correct WireSnapshot G. reconstruct_snapshot_delta raises WireFrameError for missing base """ from __future__ import annotations import hashlib import msgpack import pytest # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def _sha256_manifest(manifest: dict) -> str: raw = msgpack.packb(sorted(manifest.items()), use_bin_type=True) return "sha256:" + hashlib.sha256(raw).hexdigest() def _make_manifest(n: int) -> dict[str, str]: return {f"src/file_{i}.py": "sha256:" + "a" * 63 + str(i % 10) for i in range(n)} def _snap_id(manifest: dict) -> str: return _sha256_manifest(manifest) # --------------------------------------------------------------------------- # E. WireSnapshotDelta model validation # --------------------------------------------------------------------------- def test_wire_snapshot_delta_valid(): from musehub.models.wire import WireSnapshotDelta delta = WireSnapshotDelta( snapshot_id="sha256:" + "a" * 64, base_id="sha256:" + "b" * 64, added={"src/foo.py": "sha256:" + "c" * 64}, removed=["src/old.py"], directories=[], created_at="2026-04-23T00:00:00+00:00", ) assert delta.snapshot_id == "sha256:" + "a" * 64 assert delta.base_id == "sha256:" + "b" * 64 def test_wire_snapshot_delta_invalid_snapshot_id(): from musehub.models.wire import WireSnapshotDelta import pydantic with pytest.raises(pydantic.ValidationError): WireSnapshotDelta( snapshot_id="not-a-valid-id", base_id="sha256:" + "b" * 64, ) def test_wire_snapshot_delta_invalid_base_id(): from musehub.models.wire import WireSnapshotDelta import pydantic with pytest.raises(pydantic.ValidationError): WireSnapshotDelta( snapshot_id="sha256:" + "a" * 64, base_id="bare-hex-no-prefix", ) # --------------------------------------------------------------------------- # F. reconstruct_snapshot_delta returns correct WireSnapshot # --------------------------------------------------------------------------- def test_reconstruct_snapshot_delta_basic(): from musehub.services.musehub_wire import reconstruct_snapshot_delta from musehub.models.wire import WireSnapshotDelta base_manifest = _make_manifest(50) base_id = "sha256:" + "b" * 64 new_manifest = dict(base_manifest) new_manifest["src/file_5.py"] = "sha256:" + "f" * 64 del new_manifest["src/file_10.py"] snap_id = "sha256:" + "a" * 64 delta = WireSnapshotDelta( snapshot_id=snap_id, base_id=base_id, added={"src/file_5.py": "sha256:" + "f" * 64}, removed=["src/file_10.py"], directories=[], created_at="2026-04-23T00:00:00+00:00", ) cache = {base_id: base_manifest} result = reconstruct_snapshot_delta(delta, cache) assert result.snapshot_id == snap_id assert result.manifest["src/file_5.py"] == "sha256:" + "f" * 64 assert "src/file_10.py" not in result.manifest assert len(result.manifest) == len(base_manifest) - 1 # file_10 removed, file_5 modified (same count) def test_reconstruct_snapshot_delta_add_only(): from musehub.services.musehub_wire import reconstruct_snapshot_delta from musehub.models.wire import WireSnapshotDelta base_manifest = _make_manifest(10) base_id = "sha256:" + "b" * 64 snap_id = "sha256:" + "a" * 64 new_file = "src/brand_new.py" new_hash = "sha256:" + "9" * 64 delta = WireSnapshotDelta( snapshot_id=snap_id, base_id=base_id, added={new_file: new_hash}, removed=[], directories=[], created_at="", ) cache = {base_id: base_manifest} result = reconstruct_snapshot_delta(delta, cache) assert result.manifest[new_file] == new_hash assert len(result.manifest) == len(base_manifest) + 1 # --------------------------------------------------------------------------- # G. reconstruct_snapshot_delta raises WireFrameError for missing base # --------------------------------------------------------------------------- def test_reconstruct_snapshot_delta_missing_base_raises(): from musehub.services.musehub_wire import reconstruct_snapshot_delta from musehub.models.wire import WireSnapshotDelta from muse.core.mpack import WireFrameError delta = WireSnapshotDelta( snapshot_id="sha256:" + "a" * 64, base_id="sha256:" + "b" * 64, added={}, removed=[], directories=[], created_at="", ) with pytest.raises(WireFrameError, match="base snapshot"): reconstruct_snapshot_delta(delta, manifest_cache={}) # --------------------------------------------------------------------------- # A. Chain reconstruction — multiple deltas in sequence # --------------------------------------------------------------------------- def test_delta_chain_reconstruction(): """Build a 5-snapshot delta chain manually and verify each reconstructs.""" from musehub.services.musehub_wire import reconstruct_snapshot_delta from musehub.models.wire import WireSnapshotDelta base_manifest = _make_manifest(100) base_id = "sha256:" + "0" * 64 cache: dict[str, dict] = {base_id: base_manifest} expected_manifests: dict[str, dict] = {} prev_id = base_id prev_manifest = base_manifest for i in range(5): new_manifest = dict(prev_manifest) changed_path = f"src/file_{i * 10}.py" new_hash = f"sha256:{'c' * 63}{i}" new_manifest[changed_path] = new_hash snap_id = f"sha256:{'d' * 63}{i}" delta = WireSnapshotDelta( snapshot_id=snap_id, base_id=prev_id, added={changed_path: new_hash}, removed=[], directories=[], created_at="", ) result = reconstruct_snapshot_delta(delta, cache) cache[snap_id] = dict(result.manifest) expected_manifests[snap_id] = new_manifest prev_id = snap_id prev_manifest = new_manifest for snap_id, expected in expected_manifests.items(): assert cache[snap_id] == expected, f"Mismatch at {snap_id[:16]}" # --------------------------------------------------------------------------- # B. Empty delta (no-op commit) reconstructs to identical manifest # --------------------------------------------------------------------------- def test_empty_delta_is_identity(): from musehub.services.musehub_wire import reconstruct_snapshot_delta from musehub.models.wire import WireSnapshotDelta base_manifest = _make_manifest(20) base_id = "sha256:" + "b" * 64 snap_id = "sha256:" + "a" * 64 delta = WireSnapshotDelta( snapshot_id=snap_id, base_id=base_id, added={}, removed=[], directories=[], created_at="", ) cache = {base_id: base_manifest} result = reconstruct_snapshot_delta(delta, cache) assert result.manifest == base_manifest