test_bundle_reachable_from.py
python
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d
feat(pack): delta-encode snapshots in MPackBundle wire format
Sonnet 4.6
minor
⚠ breaking
121 days ago
| 1 | """TDD — bundle._reachable_from must use ancestor_ids, not inline deque BFS. |
| 2 | |
| 3 | B1 Structural — _reachable_from contains no inline deque/seen BFS |
| 4 | B2 Behavioural — returns all commit IDs reachable from tip_ids |
| 5 | """ |
| 6 | from __future__ import annotations |
| 7 | |
| 8 | import datetime |
| 9 | import inspect |
| 10 | import json |
| 11 | import pathlib |
| 12 | |
| 13 | import pytest |
| 14 | |
| 15 | from muse._version import __version__ |
| 16 | from muse.core.object_store import write_object |
| 17 | from muse.core.snapshot import compute_commit_id, compute_snapshot_id |
| 18 | from muse.core.store import CommitRecord, SnapshotRecord, write_commit, write_snapshot |
| 19 | from muse.core.types import blob_id |
| 20 | from muse.core.paths import muse_dir |
| 21 | |
| 22 | |
| 23 | def _repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path: |
| 24 | dot_muse = muse_dir(tmp_path) |
| 25 | for d in ("commits", "snapshots", "objects", "refs/heads", "remotes"): |
| 26 | (dot_muse / d).mkdir(parents=True, exist_ok=True) |
| 27 | (dot_muse / "HEAD").write_text("ref: refs/heads/main\n") |
| 28 | (dot_muse / "repo.json").write_text( |
| 29 | json.dumps({"repo_id": "test-repo", "schema_version": __version__, "domain": "code"}) |
| 30 | ) |
| 31 | monkeypatch.setenv("MUSE_REPO_ROOT", str(tmp_path)) |
| 32 | monkeypatch.chdir(tmp_path) |
| 33 | return tmp_path |
| 34 | |
| 35 | |
| 36 | def _make_commit( |
| 37 | root: pathlib.Path, |
| 38 | parent_id: str | None = None, |
| 39 | *, |
| 40 | message: str = "test", |
| 41 | ) -> CommitRecord: |
| 42 | oid = blob_id(b"data-" + message.encode()) |
| 43 | write_object(root, oid, b"data-" + message.encode()) |
| 44 | manifest = {"f.py": oid} |
| 45 | snap_id = compute_snapshot_id(manifest) |
| 46 | write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest)) |
| 47 | ts = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) |
| 48 | cid = compute_commit_id( |
| 49 | parent_ids=[parent_id] if parent_id else [], |
| 50 | snapshot_id=snap_id, |
| 51 | message=message, |
| 52 | committed_at_iso=ts.isoformat(), |
| 53 | ) |
| 54 | rec = CommitRecord( |
| 55 | repo_id="test-repo", |
| 56 | commit_id=cid, |
| 57 | branch="main", |
| 58 | snapshot_id=snap_id, |
| 59 | message=message, |
| 60 | committed_at=ts, |
| 61 | parent_commit_id=parent_id, |
| 62 | ) |
| 63 | write_commit(root, rec) |
| 64 | return rec |
| 65 | |
| 66 | |
| 67 | # --------------------------------------------------------------------------- |
| 68 | # B1 Structural |
| 69 | # --------------------------------------------------------------------------- |
| 70 | |
| 71 | def test_b1_reachable_from_uses_ancestor_ids() -> None: |
| 72 | """_reachable_from must not contain an inline deque BFS.""" |
| 73 | from muse.cli.commands import bundle as bundle_mod |
| 74 | |
| 75 | src = inspect.getsource(bundle_mod._reachable_from) # type: ignore[attr-defined] |
| 76 | |
| 77 | assert "ancestor_ids" in src, ( |
| 78 | "_reachable_from must delegate to ancestor_ids. " |
| 79 | "Replace the inline deque BFS." |
| 80 | ) |
| 81 | assert "deque" not in src, ( |
| 82 | "_reachable_from still uses an inline deque. Replace with ancestor_ids." |
| 83 | ) |
| 84 | |
| 85 | |
| 86 | # --------------------------------------------------------------------------- |
| 87 | # B2 Behavioural |
| 88 | # --------------------------------------------------------------------------- |
| 89 | |
| 90 | def test_b2_reachable_from_returns_all_reachable( |
| 91 | tmp_path: pathlib.Path, |
| 92 | monkeypatch: pytest.MonkeyPatch, |
| 93 | ) -> None: |
| 94 | """_reachable_from returns every commit ID reachable from the tips. |
| 95 | |
| 96 | Chain: C1 → C2 → C3. Starting from C3, all three must be returned. |
| 97 | """ |
| 98 | from muse.cli.commands.bundle import _reachable_from # type: ignore[attr-defined] |
| 99 | |
| 100 | root = _repo(tmp_path, monkeypatch) |
| 101 | c1 = _make_commit(root, message="c1") |
| 102 | c2 = _make_commit(root, c1.commit_id, message="c2") |
| 103 | c3 = _make_commit(root, c2.commit_id, message="c3") |
| 104 | |
| 105 | result = _reachable_from(root, [c3.commit_id]) |
| 106 | |
| 107 | assert c1.commit_id in result |
| 108 | assert c2.commit_id in result |
| 109 | assert c3.commit_id in result |
File History
1 commit
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d
feat(pack): delta-encode snapshots in MPackBundle wire format
Sonnet 4.6
minor
⚠
121 days ago