gabriel / muse public
test_describe_walk.py python
119 lines 4.0 KB
Raw
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d feat(pack): delta-encode snapshots in MPackBundle wire format Sonnet 4.6 minor ⚠ breaking 121 days ago
1 """TDD — describe_commit BFS must use walk_dag, not inline deque.
2
3 DC1 Structural — describe_commit uses walk_dag; no inline deque BFS
4 DC2 Behavioural — finds nearest tag with correct distance
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 # DC1 Structural
69 # ---------------------------------------------------------------------------
70
71 def test_dc1_describe_commit_uses_walk_dag() -> None:
72 """describe_commit must not contain an inline deque BFS."""
73 from muse.core import describe as describe_mod
74
75 src = inspect.getsource(describe_mod.describe_commit)
76
77 assert "walk_dag" in src, (
78 "describe_commit must delegate its BFS to walk_dag. "
79 "Replace the inline deque queue."
80 )
81 assert "deque" not in src, (
82 "describe_commit still uses an inline deque. Replace with walk_dag."
83 )
84
85
86 # ---------------------------------------------------------------------------
87 # DC2 Behavioural — nearest tag with correct distance
88 # ---------------------------------------------------------------------------
89
90 def test_dc2_describe_commit_distance(
91 tmp_path: pathlib.Path,
92 monkeypatch: pytest.MonkeyPatch,
93 ) -> None:
94 """describe_commit returns distance=2 when HEAD is 2 hops after the tag.
95
96 Chain: C1(tag=v1.0) → C2 → C3(HEAD)
97 Expected: tag=v1.0, distance=2.
98 """
99 import muse.core.describe as describe_mod
100 from muse.core.describe import describe_commit
101 from muse.core.store import TagRecord
102
103 root = _repo(tmp_path, monkeypatch)
104 c1 = _make_commit(root, message="c1")
105 c2 = _make_commit(root, c1.commit_id, message="c2")
106 c3 = _make_commit(root, c2.commit_id, message="c3")
107
108 fake_tag = TagRecord(
109 tag_id="tag-v1",
110 repo_id="test-repo",
111 tag="v1.0",
112 commit_id=c1.commit_id,
113 )
114 monkeypatch.setattr(describe_mod, "get_all_tags", lambda root, repo_id: [fake_tag])
115
116 result = describe_commit(root, "test-repo", c3.commit_id)
117
118 assert result["tag"] == "v1.0", f"Expected tag=v1.0, got {result['tag']}"
119 assert result["distance"] == 2, f"Expected distance=2, got {result['distance']}"
File History 1 commit
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d feat(pack): delta-encode snapshots in MPackBundle wire format Sonnet 4.6 minor 121 days ago