"""TDD tests for the snapshot entries refactor. Defines the new contract: - Snapshot file trees are stored as normalized rows in ``musehub_snapshot_entries`` (snapshot_id, path, object_id, size_bytes) rather than as a JSON blob in ``musehub_snapshots.manifest``. - A utility ``get_snapshot_manifest`` reconstructs {path: object_id} from those rows for callers that need the dict form. - A utility ``upsert_snapshot_entries`` is the canonical write path, used by wire_push, ingest_push, and merge_proposaloposal. - Re-pushing a snapshot whose row already exists STILL writes / updates its entries — the old "skip if exists" guard is the root cause of the bug being fixed here. All tests are RED until the implementation is in place. """ from __future__ import annotations import secrets from muse.core.types import blob_id from datetime import datetime, timezone import pytest from sqlalchemy import func, select from sqlalchemy.ext.asyncio import AsyncSession from musehub.core.genesis import compute_branch_id, compute_identity_id, compute_repo_id from musehub.db import musehub_models as db from musehub.models.musehub import CommitInput, SnapshotInput import msgpack from muse.core.mpack import MuseWireFrameWriter from musehub.models.wire import ( WireBundle, WireCommit, WireSnapshot, SFRAME_HEADER, SFRAME_OBJECT, SFRAME_COMMIT_PACK, SFRAME_END, ) from musehub.services.musehub_sync import ingest_push from musehub.services.musehub_wire import wire_push_stream from musehub.types.json_types import JSONObject, JSONValue, StrDict # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- MANIFEST_A = { "README.md": blob_id(b"obj-readme-001"), "musehub/main.py": blob_id(b"obj-main-001"), "musehub/db/models.py": blob_id(b"obj-models-001"), } MANIFEST_B = { "README.md": blob_id(b"obj-readme-002"), # changed "musehub/main.py": blob_id(b"obj-main-001"), # unchanged "musehub/db/models.py": blob_id(b"obj-models-002"), # changed "musehub/new_file.py": blob_id(b"obj-new-001"), # added } # Wire-compatible manifest — sha256: IDs required by wire_push_stream validation. MANIFEST_WIRE = { "README.md": "sha256:b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5", "musehub/main.py": "sha256:0a60476c9b54c039576ddeb49f91a1251adc5840e3a9eb57fcf23e085c6a40e9", "musehub/db/models.py": "sha256:1996895592963cc9535d725374cb5605d2e6dfce8d3351b8e8f14f18df368b92", } def _uid() -> str: return blob_id(secrets.token_bytes(16)) _OWNER_ID = compute_identity_id(b"testuser") _FIXED_TS = "2026-01-01T00:00:00+00:00" def _make_repo_id(slug: str, owner_id: str = _OWNER_ID) -> str: return compute_repo_id(owner_id, slug, "code", _FIXED_TS) def _repo(slug: str, owner_user_id: str = _OWNER_ID) -> db.MusehubRepo: repo_id = _make_repo_id(slug, owner_user_id) return db.MusehubRepo( repo_id=repo_id, name=slug, owner="testuser", slug=slug, visibility="private", owner_user_id=owner_user_id, created_at=datetime.now(timezone.utc), updated_at=datetime.now(timezone.utc), ) def _commit_input( commit_id: str, snapshot_id: str, parent_ids: list[str] | None = None, ) -> CommitInput: return CommitInput( commit_id=commit_id, branch="dev", parent_ids=parent_ids or [], message="test commit", author="tester", timestamp="2026-01-01T00:00:00Z", snapshot_id=snapshot_id, ) def _snap_input( snapshot_id: str, manifest: StrDict, ) -> SnapshotInput: return SnapshotInput(snapshot_id=snapshot_id, manifest=manifest) def _wire_bundle( commit_id: str, snapshot_id: str, manifest: StrDict, parent_id: str | None = None, ) -> WireBundle: return WireBundle( commits=[ WireCommit( commit_id=commit_id, branch="dev", parent_commit_id=parent_id or None, message="test commit", author="tester", timestamp="2026-01-01T00:00:00Z", snapshot_id=snapshot_id, ) ], snapshots=[ WireSnapshot( snapshot_id=snapshot_id, manifest=manifest, created_at="2026-01-01T00:00:00Z", ) ], objects=[], ) async def _count_entries( session: AsyncSession, snapshot_id: str ) -> int: result = await session.execute( select(func.count()).select_from(db.MusehubSnapshotEntry).where( db.MusehubSnapshotEntry.snapshot_id == snapshot_id ) ) return result.scalar_one() # --------------------------------------------------------------------------- # 1. Model existence # --------------------------------------------------------------------------- def test_snapshot_entry_model_exists() -> None: """MusehubSnapshotEntry ORM model must exist on the db module.""" assert hasattr(db, "MusehubSnapshotEntry"), ( "db.MusehubSnapshotEntry not found — add the ORM model and migration" ) def test_snapshot_entry_has_required_columns() -> None: """MusehubSnapshotEntry must have snapshot_id, path, object_id, size_bytes.""" entry = db.MusehubSnapshotEntry cols = {c.key for c in entry.__table__.columns} assert "snapshot_id" in cols assert "path" in cols assert "object_id" in cols assert "size_bytes" in cols, "size_bytes is the muse augmentation over git trees" def test_snapshot_manifest_column_removed() -> None: """MusehubSnapshot must NOT have a manifest column — it moved to entries.""" snap_cols = {c.key for c in db.MusehubSnapshot.__table__.columns} assert "manifest" not in snap_cols, ( "manifest JSON blob must be removed from musehub_snapshots — " "file trees now live in musehub_snapshot_entries" ) # --------------------------------------------------------------------------- # 2. Utility functions # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_get_snapshot_manifest_returns_empty_for_unknown( db_session: AsyncSession, ) -> None: """get_snapshot_manifest returns {} when snapshot_id has no entries.""" from musehub.services.musehub_snapshot import get_snapshot_manifest result = await get_snapshot_manifest(db_session, "nonexistent-snap") assert result == {} @pytest.mark.asyncio async def test_get_snapshot_manifest_reconstructs_from_entries( db_session: AsyncSession, ) -> None: """get_snapshot_manifest returns the correct {path: object_id} dict.""" from musehub.services.musehub_snapshot import get_snapshot_manifest, upsert_snapshot_entries snap_id = "snap-util-001" slug = "repo-util-001" repo_id = _make_repo_id(slug) db_session.add(_repo(slug)) await db_session.flush() await upsert_snapshot_entries(db_session, repo_id, snap_id, MANIFEST_A) await db_session.flush() result = await get_snapshot_manifest(db_session, snap_id) assert result == MANIFEST_A @pytest.mark.asyncio async def test_upsert_snapshot_entries_is_idempotent( db_session: AsyncSession, ) -> None: """Calling upsert_snapshot_entries twice with the same data is safe.""" from musehub.services.musehub_snapshot import upsert_snapshot_entries snap_id = "snap-idempotent-001" slug = "repo-idempotent-001" repo_id = _make_repo_id(slug) db_session.add(_repo(slug)) await db_session.flush() await upsert_snapshot_entries(db_session, repo_id, snap_id, MANIFEST_A) await db_session.flush() await upsert_snapshot_entries(db_session, repo_id, snap_id, MANIFEST_A) await db_session.flush() from musehub.services.musehub_snapshot import get_snapshot_manifest result = await get_snapshot_manifest(db_session, snap_id) assert result == MANIFEST_A @pytest.mark.asyncio async def test_upsert_snapshot_entries_backfills_null_blob( db_session: AsyncSession, ) -> None: """upsert_snapshot_entries creates a new snapshot with the correct manifest_blob. Snapshots are content-addressed: the same snapshot_id always has the same manifest. Calling upsert creates the row and the manifest is readable immediately after. """ from musehub.services.musehub_snapshot import get_snapshot_manifest, upsert_snapshot_entries snap_id = "snap-backfill-001" slug = "repo-backfill-001" repo_id = _make_repo_id(slug) db_session.add(_repo(slug)) await db_session.flush() await upsert_snapshot_entries(db_session, repo_id, snap_id, MANIFEST_A) await db_session.flush() result = await get_snapshot_manifest(db_session, snap_id) assert result == MANIFEST_A # --------------------------------------------------------------------------- # 3. ingest_push write path (REST API / MCP path) # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_ingest_push_stores_snapshot_entries( db_session: AsyncSession, ) -> None: """ingest_push must write entries to musehub_snapshot_entries.""" slug = "repo-ip-entries-001" repo_id = _make_repo_id(slug) snap_id = "snap-ip-001" commit_id = _uid() db_session.add(_repo(slug)) await db_session.flush() await ingest_push( db_session, repo_id=repo_id, branch="dev", head_commit_id=commit_id, commits=[_commit_input(commit_id, snap_id)], snapshots=[_snap_input(snap_id, MANIFEST_A)], objects=[], force=True, author="tester", ) from musehub.services.musehub_snapshot import get_snapshot_manifest manifest = await get_snapshot_manifest(db_session, snap_id) assert manifest == MANIFEST_A @pytest.mark.asyncio async def test_ingest_push_entries_idempotent_on_repush( db_session: AsyncSession, ) -> None: """Re-pushing the same snapshot via ingest_push does not duplicate entries.""" slug = "repo-ip-idem-001" repo_id = _make_repo_id(slug) snap_id = "snap-ip-idem-001" commit_id = _uid() db_session.add(_repo(slug)) await db_session.flush() for _ in range(3): await ingest_push( db_session, repo_id=repo_id, branch="dev", head_commit_id=commit_id, commits=[_commit_input(commit_id, snap_id)], snapshots=[_snap_input(snap_id, MANIFEST_A)], objects=[], force=True, author="tester", ) from musehub.services.musehub_snapshot import get_snapshot_manifest result = await get_snapshot_manifest(db_session, snap_id) assert result == MANIFEST_A # --------------------------------------------------------------------------- # 4. wire_push write path (muse CLI push path) # --------------------------------------------------------------------------- async def _run_wire_push_stream( db_session: AsyncSession, repo_id: str, user_id: str, commit_id: str, snap_id: str, manifest: StrDict, *, force: bool = False, ) -> JSONObject: """Drive wire_push_stream with a commit+snapshot pack (no objects). Pre-seeds MusehubObject + MusehubObjectRef rows for all manifest values so wire_push_stream's object-existence validation passes. """ from unittest.mock import patch # Pre-seed objects referenced by the manifest so the server's object-existence # check does not reject the push. for path, oid in manifest.items(): existing = await db_session.get(db.MusehubObject, oid) if existing is None: db_session.add(db.MusehubObject( object_id=oid, path=path, size_bytes=1, disk_path="", )) ref_result = await db_session.execute( __import__("sqlalchemy", fromlist=["select"]).select(db.MusehubObjectRef).where( db.MusehubObjectRef.repo_id == repo_id, db.MusehubObjectRef.object_id == oid, ) ) if ref_result.scalar_one_or_none() is None: db_session.add(db.MusehubObjectRef(repo_id=repo_id, object_id=oid)) await db_session.flush() fw = MuseWireFrameWriter() def _wrap(ft: str, data: JSONValue) -> bytes: return fw.wrap(frame_type=ft, payload=msgpack.packb(data, use_bin_type=True)) body = ( _wrap(SFRAME_HEADER, { "t": SFRAME_HEADER, "branch": "dev", "force": force, "have": [], "head": commit_id, "n_objects": 0, "n_commits": 1, }) + _wrap(SFRAME_COMMIT_PACK, { "t": SFRAME_COMMIT_PACK, "commits": [{ "commit_id": commit_id, "branch": "dev", "snapshot_id": snap_id, "message": "test commit", "author": "tester", "committed_at": "2026-01-01T00:00:00Z", }], "snapshots": [{ "snapshot_id": snap_id, "manifest": manifest, "created_at": "2026-01-01T00:00:00Z", }], }) + _wrap(SFRAME_END, {"t": SFRAME_END, "n_objects": 0, "n_commits": 1}) ) async def body_iter() -> None: yield body # Pre-seeded objects exist in DB but not in storage — mock the backend so # the ghost-object guard treats them as present in storage. seeded_oids = set(manifest.values()) from unittest.mock import AsyncMock, MagicMock mock_backend = MagicMock() mock_backend.exists = AsyncMock(side_effect=lambda oid, **_: oid in seeded_oids) mock_backend.put = AsyncMock(return_value="mock://object") mock_backend.presign_batch = AsyncMock(return_value={}) frames: list[dict] = [] with patch("musehub.services.musehub_wire.settings") as mock_settings, \ patch("musehub.services.musehub_wire.get_backend", return_value=mock_backend): mock_settings.per_repo_quota_bytes = 100 * 1024 * 1024 mock_settings.require_signed_commits = False mock_settings.trusted_agent_ids = [] async for chunk in wire_push_stream(db_session, repo_id, body_iter(), pusher_id=user_id): unpacker = msgpack.Unpacker(raw=False) unpacker.feed(chunk) frames.extend(list(unpacker)) return frames[-1] if frames else {} @pytest.mark.asyncio async def test_wire_push_stores_snapshot_entries( db_session: AsyncSession, ) -> None: """wire_push_stream must write entries to musehub_snapshot_entries.""" slug = "repo-wire-001" repo_id = _make_repo_id(slug) user_id = "testuser" snap_id = _uid() commit_id = _uid() db_session.add(_repo(slug)) await db_session.flush() result = await _run_wire_push_stream(db_session, repo_id, user_id, commit_id, snap_id, MANIFEST_WIRE) assert result.get("ok") is True, f"wire_push_stream failed: {result}" from musehub.services.musehub_snapshot import get_snapshot_manifest manifest = await get_snapshot_manifest(db_session, snap_id) assert manifest == MANIFEST_WIRE @pytest.mark.asyncio async def test_wire_push_entries_idempotent_on_repush( db_session: AsyncSession, ) -> None: """Re-pushing the same snapshot via wire_push_stream does not duplicate entries.""" slug = "repo-wire-idem-001" repo_id = _make_repo_id(slug) user_id = "testuser" snap_id = _uid() commit_id = _uid() db_session.add(_repo(slug)) await db_session.flush() await _run_wire_push_stream(db_session, repo_id, user_id, commit_id, snap_id, MANIFEST_WIRE, force=True) result = await _run_wire_push_stream(db_session, repo_id, user_id, commit_id, snap_id, MANIFEST_WIRE, force=True) assert result.get("ok") is True, f"second push failed: {result}" from musehub.services.musehub_snapshot import get_snapshot_manifest manifest = await get_snapshot_manifest(db_session, snap_id) assert manifest == MANIFEST_WIRE @pytest.mark.asyncio async def test_wire_push_repairs_stale_snapshot( db_session: AsyncSession, ) -> None: """ Regression test: wire_push_stream correctly stores the manifest for a new snapshot. Verifies that pushing a snapshot_id that does not yet exist in the DB correctly stores the manifest so subsequent pulls can reconstruct the file tree. """ slug = "repo-wire-stale-001" repo_id = _make_repo_id(slug) user_id = "testuser" snap_id = _uid() commit_id = _uid() db_session.add(_repo(slug)) await db_session.flush() result = await _run_wire_push_stream(db_session, repo_id, user_id, commit_id, snap_id, MANIFEST_WIRE, force=True) assert result.get("ok") is True, f"wire_push_stream failed: {result}" from musehub.services.musehub_snapshot import get_snapshot_manifest manifest = await get_snapshot_manifest(db_session, snap_id) assert manifest == MANIFEST_WIRE, ( "manifest was not stored — wire_push_stream must write the manifest on push" ) # --------------------------------------------------------------------------- # 5. Read path — repository service functions # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_get_file_at_ref_resolves_via_entries( db_session: AsyncSession, ) -> None: """get_file_at_ref must resolve object_id from snapshot entries, not manifest blob.""" from musehub.services.musehub_repository import get_file_at_ref from musehub.services.musehub_snapshot import upsert_snapshot_entries slug = "repo-gar-001" repo_id = _make_repo_id(slug) snap_id = "snap-gar-001" commit_id = _uid() db_session.add(_repo(slug)) await db_session.flush() await upsert_snapshot_entries(db_session, repo_id, snap_id, MANIFEST_A) await db_session.flush() db_session.add(db.MusehubCommit( commit_id=commit_id, repo_id=repo_id, branch="dev", parent_ids=[], message="m", author="a", timestamp=datetime.now(timezone.utc), snapshot_id=snap_id, )) db_session.add(db.MusehubBranch( branch_id=compute_branch_id(repo_id, "dev"), repo_id=repo_id, name="dev", head_commit_id=commit_id, )) await db_session.flush() result = await get_file_at_ref(db_session, repo_id, "dev", "README.md") assert result is not None assert result["object_id"] == MANIFEST_A["README.md"] assert result["path"] == "README.md" @pytest.mark.asyncio async def test_get_snapshot_diff_via_entries( db_session: AsyncSession, ) -> None: """get_snapshot_diff must diff two snapshots using entries, not manifest blobs.""" from musehub.services.musehub_repository import get_snapshot_diff from musehub.services.musehub_snapshot import upsert_snapshot_entries slug = "repo-diff-001" repo_id = _make_repo_id(slug) snap_a = "snap-diff-a" snap_b = "snap-diff-b" db_session.add(_repo(slug)) await db_session.flush() await upsert_snapshot_entries(db_session, repo_id, snap_a, MANIFEST_A) await upsert_snapshot_entries(db_session, repo_id, snap_b, MANIFEST_B) await db_session.flush() diff = await get_snapshot_diff(db_session, repo_id, snap_b, snap_a) # musehub/new_file.py is in B but not A → added assert "musehub/new_file.py" in diff["added"] # README.md and models.py changed object_id → modified assert "README.md" in diff["modified"] assert "musehub/db/models.py" in diff["modified"] # main.py is unchanged assert "musehub/main.py" not in diff["modified"] assert "musehub/main.py" not in diff["added"] assert "musehub/main.py" not in diff["removed"] @pytest.mark.asyncio async def test_get_file_last_commits_via_entries( db_session: AsyncSession, ) -> None: """get_file_last_commits must walk entries, not manifest blobs.""" from musehub.services.musehub_repository import get_file_last_commits from musehub.services.musehub_snapshot import upsert_snapshot_entries slug = "repo-flc-001" repo_id = _make_repo_id(slug) snap_1 = "snap-flc-001" snap_2 = "snap-flc-002" commit_1 = _uid() commit_2 = _uid() db_session.add(_repo(slug)) await db_session.flush() await upsert_snapshot_entries(db_session, repo_id, snap_1, MANIFEST_A) await upsert_snapshot_entries(db_session, repo_id, snap_2, MANIFEST_B) await db_session.flush() # commit_1 (older): only MANIFEST_A db_session.add(db.MusehubCommit( commit_id=commit_1, repo_id=repo_id, branch="dev", parent_ids=[], message="initial", author="a", timestamp=datetime(2026, 1, 1, tzinfo=timezone.utc), snapshot_id=snap_1, )) # commit_2 (newer): MANIFEST_B — README.md changed db_session.add(db.MusehubCommit( commit_id=commit_2, repo_id=repo_id, branch="dev", parent_ids=[commit_1], message="update readme", author="a", timestamp=datetime(2026, 1, 2, tzinfo=timezone.utc), snapshot_id=snap_2, )) await db_session.flush() result = await get_file_last_commits(db_session, repo_id, ["README.md", "musehub/main.py"]) # README.md changed in commit_2 — should be attributed there assert "README.md" in result assert result["README.md"]["sha"] == commit_2 # main.py was unchanged in commit_2 — attributed to commit_1 assert "musehub/main.py" in result assert result["musehub/main.py"]["sha"] == commit_1 @pytest.mark.asyncio async def test_list_tree_resolves_via_entries( db_session: AsyncSession, ) -> None: """list_tree must build the directory listing from snapshot entries.""" from musehub.services.musehub_repository import list_tree from musehub.services.musehub_snapshot import upsert_snapshot_entries slug = "repo-lt-001" repo_id = _make_repo_id(slug) snap_id = "snap-lt-001" commit_id = _uid() db_session.add(_repo(slug)) await db_session.flush() await upsert_snapshot_entries(db_session, repo_id, snap_id, MANIFEST_A) await db_session.flush() db_session.add(db.MusehubCommit( commit_id=commit_id, repo_id=repo_id, branch="dev", parent_ids=[], message="m", author="a", timestamp=datetime.now(timezone.utc), snapshot_id=snap_id, )) db_session.add(db.MusehubBranch( branch_id=compute_branch_id(repo_id, "dev"), repo_id=repo_id, name="dev", head_commit_id=commit_id, )) await db_session.flush() tree = await list_tree(db_session, repo_id, "t", repo_id, ref="dev", dir_path="") names = {e.name for e in tree.entries} assert "README.md" in names assert "musehub" in names # directory inferred from paths # --------------------------------------------------------------------------- # 6. wire fetch — object dedup uses entries # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_wire_fetch_includes_objects_from_entries( db_session: AsyncSession, ) -> None: """ wire_fetch / _to_wire_snapshot must reconstruct manifest from entries so that object deduplication works correctly on pull. """ from musehub.services.musehub_wire import _to_wire_snapshot from musehub.services.musehub_snapshot import upsert_snapshot_entries slug = "repo-fetch-001" repo_id = _make_repo_id(slug) snap_id = _uid() db_session.add(_repo(slug)) await db_session.flush() await upsert_snapshot_entries(db_session, repo_id, snap_id, MANIFEST_A) await db_session.flush() snap_row = await db_session.get(db.MusehubSnapshot, snap_id) assert snap_row is not None wire_snap = await _to_wire_snapshot(db_session, snap_row) assert wire_snap.manifest == MANIFEST_A