gabriel / muse public

test_describe_walk.py file-level

at sha256:c · View file ↗ · Intel ↗

History
1 files
1 commits
0 hotspots
0 🧊 dead
0 💥 blast risk
sha256:6 chore(timeline): remove unused RationalRate import in entity.py · · Jul 10, 2026
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 version 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.ids import hash_commit as compute_commit_id, hash_snapshot as compute_snapshot_id
18 from muse.core.commits import (
19 CommitRecord,
20 write_commit,
21 )
22 from muse.core.snapshots import (
23 SnapshotRecord,
24 write_snapshot,
25 )
26 from muse.core.version_tags import VersionTagRecord, write_version_tag
27 from muse.core.semver import parse_semver
28 from muse.core.types import blob_id, content_hash
29 from muse.core.paths import muse_dir
30
31 _REPO_ID = content_hash({"name": "walk-test"})
32
33
34 def _repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path:
35 dot_muse = muse_dir(tmp_path)
36 for d in ("commits", "snapshots", "objects", "refs/heads", "remotes", "version-tags"):
37 (dot_muse / d).mkdir(parents=True, exist_ok=True)
38 (dot_muse / "HEAD").write_text("ref: refs/heads/main\n")
39 (dot_muse / "repo.json").write_text(
40 json.dumps({"repo_id": _REPO_ID, "schema_version": __version__, "domain": "code"})
41 )
42 monkeypatch.setenv("MUSE_REPO_ROOT", str(tmp_path))
43 monkeypatch.chdir(tmp_path)
44 return tmp_path
45
46
47 def _make_commit(
48 root: pathlib.Path,
49 parent_id: str | None = None,
50 *,
51 message: str = "test",
52 ) -> CommitRecord:
53 oid = blob_id(b"data-" + message.encode())
54 write_object(root, oid, b"data-" + message.encode())
55 manifest = {"f.py": oid}
56 snap_id = compute_snapshot_id(manifest)
57 write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest))
58 ts = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc)
59 cid = compute_commit_id(
60 parent_ids=[parent_id] if parent_id else [],
61 snapshot_id=snap_id,
62 message=message,
63 committed_at_iso=ts.isoformat(),
64 )
65 rec = CommitRecord(
66 commit_id=cid,
67 branch="main",
68 snapshot_id=snap_id,
69 message=message,
70 committed_at=ts,
71 parent_commit_id=parent_id,
72 )
73 write_commit(root, rec)
74 return rec
75
76
77 def _make_vtag(root: pathlib.Path, tag: str, commit_id: str) -> None:
78 write_version_tag(root, VersionTagRecord(
79 tag_id=content_hash({"tag": tag, "commit_id": commit_id}),
80 repo_id=_REPO_ID,
81 tag=tag,
82 semver=parse_semver(tag),
83 commit_id=commit_id,
84 created_at=datetime.datetime.now(datetime.timezone.utc),
85 author="",
86 message="",
87 ))
88
89
90 # ---------------------------------------------------------------------------
91 # DC1 Structural
92 # ---------------------------------------------------------------------------
93
94 def test_dc1_describe_commit_uses_walk_dag() -> None:
95 """describe_commit must not contain an inline deque BFS."""
96 from muse.core import describe as describe_mod
97
98 src = inspect.getsource(describe_mod.describe_commit)
99
100 assert "walk_dag" in src, (
101 "describe_commit must delegate its BFS to walk_dag. "
102 "Replace the inline deque queue."
103 )
104 assert "deque" not in src, (
105 "describe_commit still uses an inline deque. Replace with walk_dag."
106 )
107
108
109 # ---------------------------------------------------------------------------
110 # DC2 Behavioural — nearest version tag with correct distance
111 # ---------------------------------------------------------------------------
112
113 def test_dc2_describe_commit_distance(
114 tmp_path: pathlib.Path,
115 monkeypatch: pytest.MonkeyPatch,
116 ) -> None:
117 """describe_commit returns distance=2 when HEAD is 2 hops after the tag.
118
119 Chain: C1(tag=v1.0.0) → C2 → C3(HEAD)
120 Expected: tag=v1.0.0, distance=2.
121 """
122 from muse.core.describe import describe_commit
123
124 root = _repo(tmp_path, monkeypatch)
125 c1 = _make_commit(root, message="c1")
126 c2 = _make_commit(root, c1.commit_id, message="c2")
127 c3 = _make_commit(root, c2.commit_id, message="c3")
128
129 _make_vtag(root, "v1.0.0", c1.commit_id)
130
131 result = describe_commit(root, _REPO_ID, c3.commit_id)
132
133 assert result["tag"] == "v1.0.0", f"Expected tag=v1.0.0, got {result['tag']}"
134 assert result["distance"] == 2, f"Expected distance=2, got {result['distance']}"