"""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 msgpack from muse.core.types import blob_id, fake_id, long_id from musehub.types.json_types import StrDict import pytest _SNAP_A = fake_id("snap-a") _SNAP_B = fake_id("snap-b") _SNAP_C = fake_id("snap-c") _SNAP_0 = fake_id("snap-0") _OBJ_F = fake_id("obj-f") _OBJ_9 = fake_id("obj-9") # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def _sha256_manifest(manifest: StrDict) -> str: raw = msgpack.packb(sorted(manifest.items()), use_bin_type=True) return blob_id(raw) def _make_manifest(n: int) -> StrDict: return {f"src/file_{i}.py": fake_id(f"file-{i}") for i in range(n)} def _snap_id(manifest: StrDict) -> str: return _sha256_manifest(manifest) # --------------------------------------------------------------------------- # E. WireSnapshotDelta model validation # --------------------------------------------------------------------------- def test_wire_snapshot_delta_valid() -> None: from musehub.models.wire import WireSnapshotDelta delta = WireSnapshotDelta( snapshot_id=_SNAP_A, base_id=_SNAP_B, added={"src/foo.py": long_id("c" * 64)}, removed=["src/old.py"], directories=[], created_at="2026-04-23T00:00:00+00:00", ) assert delta.snapshot_id == _SNAP_A assert delta.base_id == _SNAP_B def test_wire_snapshot_delta_invalid_snapshot_id() -> None: from musehub.models.wire import WireSnapshotDelta import pydantic with pytest.raises(pydantic.ValidationError): WireSnapshotDelta( snapshot_id="not-a-valid-id", base_id=_SNAP_B, ) def test_wire_snapshot_delta_invalid_base_id() -> None: from musehub.models.wire import WireSnapshotDelta import pydantic with pytest.raises(pydantic.ValidationError): WireSnapshotDelta( snapshot_id=_SNAP_A, base_id="bare-hex-no-prefix", ) # --------------------------------------------------------------------------- # F. reconstruct_snapshot_delta returns correct WireSnapshot # --------------------------------------------------------------------------- def test_reconstruct_snapshot_delta_basic() -> None: from musehub.services.musehub_wire import reconstruct_snapshot_delta from musehub.models.wire import WireSnapshotDelta base_manifest = _make_manifest(50) base_id = _SNAP_B new_manifest = dict(base_manifest) new_manifest["src/file_5.py"] = _OBJ_F del new_manifest["src/file_10.py"] snap_id = _SNAP_A delta = WireSnapshotDelta( snapshot_id=snap_id, base_id=base_id, added={"src/file_5.py": _OBJ_F}, 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"] == _OBJ_F 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() -> None: from musehub.services.musehub_wire import reconstruct_snapshot_delta from musehub.models.wire import WireSnapshotDelta base_manifest = _make_manifest(10) base_id = _SNAP_B snap_id = _SNAP_A new_file = "src/brand_new.py" new_hash = _OBJ_9 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() -> None: 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=_SNAP_A, base_id=_SNAP_B, 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() -> None: """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 = _SNAP_0 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 = long_id("c" * 63 + str(i)) new_manifest[changed_path] = new_hash snap_id = long_id("d" * 63 + str(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() -> None: from musehub.services.musehub_wire import reconstruct_snapshot_delta from musehub.models.wire import WireSnapshotDelta base_manifest = _make_manifest(20) base_id = _SNAP_B snap_id = _SNAP_A 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