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