gabriel / muse public
test_bundle_reachable_from.py python
108 lines 3.6 KB
Raw
sha256:1900655993c83c4107067375548a7be823e471d2515830842f1a12cba4bd3cdf fix: unified object store migration — idempotent writes, JS… Sonnet 4.6 minor ⚠ breaking 29 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.ids import hash_commit, hash_snapshot
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 = hash_snapshot(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 = hash_commit(
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 commit_id=cid,
56 branch="main",
57 snapshot_id=snap_id,
58 message=message,
59 committed_at=ts,
60 parent_commit_id=parent_id,
61 )
62 write_commit(root, rec)
63 return rec
64
65
66 # ---------------------------------------------------------------------------
67 # B1 Structural
68 # ---------------------------------------------------------------------------
69
70 def test_b1_reachable_from_uses_ancestor_ids() -> None:
71 """_reachable_from must not contain an inline deque BFS."""
72 from muse.cli.commands import bundle as bundle_mod
73
74 src = inspect.getsource(bundle_mod._reachable_from) # type: ignore[attr-defined]
75
76 assert "ancestor_ids" in src, (
77 "_reachable_from must delegate to ancestor_ids. "
78 "Replace the inline deque BFS."
79 )
80 assert "deque" not in src, (
81 "_reachable_from still uses an inline deque. Replace with ancestor_ids."
82 )
83
84
85 # ---------------------------------------------------------------------------
86 # B2 Behavioural
87 # ---------------------------------------------------------------------------
88
89 def test_b2_reachable_from_returns_all_reachable(
90 tmp_path: pathlib.Path,
91 monkeypatch: pytest.MonkeyPatch,
92 ) -> None:
93 """_reachable_from returns every commit ID reachable from the tips.
94
95 Chain: C1 → C2 → C3. Starting from C3, all three must be returned.
96 """
97 from muse.cli.commands.bundle import _reachable_from # type: ignore[attr-defined]
98
99 root = _repo(tmp_path, monkeypatch)
100 c1 = _make_commit(root, message="c1")
101 c2 = _make_commit(root, c1.commit_id, message="c2")
102 c3 = _make_commit(root, c2.commit_id, message="c3")
103
104 result = _reachable_from(root, [c3.commit_id])
105
106 assert c1.commit_id in result
107 assert c2.commit_id in result
108 assert c3.commit_id in result
File History 1 commit
sha256:1900655993c83c4107067375548a7be823e471d2515830842f1a12cba4bd3cdf fix: unified object store migration — idempotent writes, JS… Sonnet 4.6 minor 29 days ago