"""Phase 6 — Non-wire msgpack cleanup tests (issue #63). Verifies: - _snap_row_to_wire no longer reads delta_blob (dead code removed) - _snap_row_to_wire fallback uses only manifest_blob - _snap_row_to_wire_s3 emits a warning when falling back to DB - _commit_to_wire_s3 emits a warning when falling back to DB - Dead imports removed from musehub_wire and musehub_wire_push """ from __future__ import annotations import datetime import logging import msgpack import pytest from muse.core.types import fake_id from musehub.db.musehub_repo_models import MusehubCommit, MusehubSnapshot def _snap( snapshot_id: str, manifest: dict[str, str] | None = None, delta_blob: bytes | None = None, storage_uri: str | None = None, ) -> MusehubSnapshot: blob = msgpack.packb(manifest, use_bin_type=True) if manifest is not None else None return MusehubSnapshot( snapshot_id=snapshot_id, manifest_blob=blob, delta_blob=delta_blob, entry_count=len(manifest) if manifest else 0, directories=[], storage_uri=storage_uri, ) def _commit(commit_id: str, storage_uri: str | None = None) -> MusehubCommit: return MusehubCommit( commit_id=commit_id, branch="main", parent_ids=[], message="test", author="gabriel", timestamp=datetime.datetime.now(tz=datetime.timezone.utc), snapshot_id=None, storage_uri=storage_uri, ) # --------------------------------------------------------------------------- # _snap_row_to_wire — delta_blob branch removed # --------------------------------------------------------------------------- class TestSnapRowToWireDeltaBlobRemoved: def test_delta_blob_not_used_when_manifest_blob_present(self) -> None: """_snap_row_to_wire must read from manifest_blob, never from delta_blob.""" from musehub.services.musehub_wire_shared import _snap_row_to_wire manifest = {"a.py": fake_id("obj-a"), "b.py": fake_id("obj-b")} # delta_blob has DIFFERENT content — if it were read, delta_upsert would differ delta_encoded = {"add": {"WRONG.py": fake_id("wrong")}} snap = _snap( snapshot_id=fake_id("snap1"), manifest=manifest, delta_blob=msgpack.packb(delta_encoded, use_bin_type=True), ) result = _snap_row_to_wire(snap) assert "WRONG.py" not in result["delta_upsert"], \ "_snap_row_to_wire must NOT read delta_blob — that branch was removed in Phase 6" assert "a.py" in result["delta_upsert"], \ "_snap_row_to_wire must read manifest_blob for delta_upsert" def test_delta_blob_only_returns_empty_delta_upsert(self) -> None: """Row with only delta_blob (no manifest_blob) returns empty delta_upsert. Previously _snap_row_to_wire would decode delta_blob and return its content. After Phase 6, the delta_blob branch is removed — the fallback is S3 via _snap_row_to_wire_s3, not _snap_row_to_wire. """ from musehub.services.musehub_wire_shared import _snap_row_to_wire delta_encoded = {"add": {"c.py": fake_id("obj-c")}} snap = _snap( snapshot_id=fake_id("snap2"), manifest=None, delta_blob=msgpack.packb(delta_encoded, use_bin_type=True), ) result = _snap_row_to_wire(snap) assert result["delta_upsert"] == {}, \ "After Phase 6, _snap_row_to_wire must return empty delta_upsert " \ "when manifest_blob is absent — S3 path handles the real data" assert result["delta_remove"] == [] def test_manifest_blob_present_returns_correct_manifest(self) -> None: """_snap_row_to_wire still works correctly for manifest_blob fallback.""" from musehub.services.musehub_wire_shared import _snap_row_to_wire manifest = {"src/main.py": fake_id("obj-main"), "README.md": fake_id("obj-readme")} snap = _snap(snapshot_id=fake_id("snap3"), manifest=manifest) result = _snap_row_to_wire(snap) assert result["delta_upsert"] == manifest assert result["delta_remove"] == [] assert result["snapshot_id"] == snap.snapshot_id # --------------------------------------------------------------------------- # _snap_row_to_wire_s3 — storage_uri=None is correct, no warning expected # --------------------------------------------------------------------------- class TestSnapRowToWireS3FallbackWarning: async def test_no_warning_when_storage_uri_is_none( self, caplog: pytest.LogCaptureFixture ) -> None: """_snap_row_to_wire_s3 must NOT warn when storage_uri=None — that is the correct state. Snapshots are canonical in the DB. storage_uri=None means the object is served from DB (the intended path), not a fallback condition. """ from musehub.services.musehub_wire_shared import _snap_row_to_wire_s3 from musehub.storage.backends import get_backend manifest = {"f.py": fake_id("obj-f")} snap = _snap(snapshot_id=fake_id("snap-warn"), manifest=manifest, storage_uri=None) with caplog.at_level(logging.WARNING, logger="musehub.services.musehub_wire_shared"): result = await _snap_row_to_wire_s3(snap, get_backend()) assert result["delta_upsert"] == manifest assert not any("fallback" in r.message.lower() or "storage_uri" in r.message.lower() for r in caplog.records), \ "storage_uri=None is expected — must not produce a spurious warning" # --------------------------------------------------------------------------- # _commit_to_wire_s3 — storage_uri=None is correct, no warning expected # --------------------------------------------------------------------------- class TestCommitToWireS3FallbackWarning: async def test_no_warning_when_storage_uri_is_none( self, caplog: pytest.LogCaptureFixture ) -> None: """_commit_to_wire_s3 must NOT warn when storage_uri=None — that is the correct state. Commits are canonical in the DB. storage_uri=None means the object is served from DB (the intended path), not a fallback condition. """ from musehub.services.musehub_wire_shared import _commit_to_wire_s3 from musehub.storage.backends import get_backend commit = _commit(commit_id=fake_id("commit-warn"), storage_uri=None) with caplog.at_level(logging.WARNING, logger="musehub.services.musehub_wire_shared"): result = await _commit_to_wire_s3(commit, get_backend()) assert result.commit_id == commit.commit_id assert not any("fallback" in r.message.lower() or "storage_uri" in r.message.lower() for r in caplog.records), \ "storage_uri=None is expected — must not produce a spurious warning" # --------------------------------------------------------------------------- # Dead imports removed # --------------------------------------------------------------------------- class TestDeadImportsRemoved: def test_snap_row_to_wire_not_in_wire_push_namespace(self) -> None: """_snap_row_to_wire must not be imported at module level in musehub_wire_push.""" import musehub.services.musehub_wire_push as push_mod assert not hasattr(push_mod, "_snap_row_to_wire"), \ "_snap_row_to_wire is unused in musehub_wire_push — import must be removed" def test_snap_row_to_wire_not_in_wire_namespace(self) -> None: """_snap_row_to_wire must not be imported at module level in musehub_wire.""" import musehub.services.musehub_wire as wire_mod assert not hasattr(wire_mod, "_snap_row_to_wire"), \ "_snap_row_to_wire is unused in musehub_wire — import must be removed"