gabriel / muse public
test_annotate_command.py python
123 lines 4.6 KB
Raw
sha256:660fcac1df3ab28f61862e961890bd2ca8b754fa0242079d93ca1e25037ec8a6 chore(tests): add docstring to tests/__init__.py so rc14 tr… Human 26 days ago
1 """Tests for muse annotate — CRDT-backed commit annotations."""
2
3 import datetime
4 import pathlib
5
6 import pytest
7 from tests.cli_test_helper import CliRunner
8
9 cli = None # argparse migration — CliRunner ignores this arg
10 from muse.core.snapshot import compute_commit_id, compute_snapshot_id
11 from muse.core.store import CommitRecord, write_commit
12
13 runner = CliRunner()
14
15
16 # ---------------------------------------------------------------------------
17 # Fixtures
18 # ---------------------------------------------------------------------------
19
20
21 @pytest.fixture
22 def repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path:
23 """Set up a minimal Muse repo and chdir into it."""
24 monkeypatch.chdir(tmp_path)
25 muse = tmp_path / ".muse"
26 muse.mkdir()
27 (muse / "repo.json").write_text('{"repo_id":"test-repo"}')
28 (muse / "HEAD").write_text("ref: refs/heads/main")
29 (muse / "commits").mkdir()
30 (muse / "snapshots").mkdir()
31 (muse / "refs" / "heads").mkdir(parents=True)
32 return tmp_path
33
34
35 def _write_commit(root: pathlib.Path, message: str = "test commit") -> CommitRecord:
36 """Write a CommitRecord with a content-addressed commit_id.
37
38 Uses ``compute_commit_id`` so every record passes ``_verify_commit_id``
39 on read-back. The branch ref is updated to point at the new commit.
40 """
41 committed_at = datetime.datetime(2026, 3, 1, tzinfo=datetime.timezone.utc)
42 snap_id = compute_snapshot_id({})
43 cid = compute_commit_id([], snap_id, message, committed_at.isoformat())
44 record = CommitRecord(
45 commit_id=cid,
46 repo_id="test-repo",
47 branch="main",
48 snapshot_id=snap_id,
49 message=message,
50 committed_at=committed_at,
51 author="test-author",
52 )
53 write_commit(root, record)
54 (root / ".muse" / "refs" / "heads" / "main").write_text(cid)
55 return record
56
57
58 # ---------------------------------------------------------------------------
59 # Tests
60 # ---------------------------------------------------------------------------
61
62
63 class TestAnnotateCommand:
64 def test_show_annotations_when_no_flags(self, repo: pathlib.Path) -> None:
65 c = _write_commit(repo)
66 result = runner.invoke(cli, ["annotate", c.commit_id], catch_exceptions=False)
67 assert result.exit_code == 0
68 assert "reviewed-by" in result.output
69
70 def test_add_reviewer(self, repo: pathlib.Path) -> None:
71 c = _write_commit(repo)
72 result = runner.invoke(
73 cli, ["annotate", "--reviewed-by", "agent-x", c.commit_id],
74 catch_exceptions=False,
75 )
76 assert result.exit_code == 0, result.output
77 assert "agent-x" in result.output
78
79 from muse.core.store import read_commit
80 record = read_commit(repo, c.commit_id)
81 assert record is not None
82 assert "agent-x" in record.reviewed_by
83
84 def test_add_multiple_reviewers(self, repo: pathlib.Path) -> None:
85 c = _write_commit(repo)
86 r = runner.invoke(
87 cli, ["annotate", "--reviewed-by", "alice,bob", c.commit_id],
88 catch_exceptions=False,
89 )
90 assert r.exit_code == 0, r.output
91
92 from muse.core.store import read_commit
93 record = read_commit(repo, c.commit_id)
94 assert record is not None
95 # ORSet semantics: both reviewers should be present.
96 assert "alice" in record.reviewed_by
97 assert "bob" in record.reviewed_by
98
99 def test_add_reviewer_idempotent(self, repo: pathlib.Path) -> None:
100 c = _write_commit(repo)
101 runner.invoke(cli, ["annotate", "--reviewed-by", "alice", c.commit_id], catch_exceptions=False)
102 runner.invoke(cli, ["annotate", "--reviewed-by", "alice", c.commit_id], catch_exceptions=False)
103
104 from muse.core.store import read_commit
105 record = read_commit(repo, c.commit_id)
106 assert record is not None
107 # ORSet: adding the same element twice should appear once.
108 assert record.reviewed_by.count("alice") == 1
109
110 def test_increment_test_run_counter(self, repo: pathlib.Path) -> None:
111 c = _write_commit(repo)
112 runner.invoke(cli, ["annotate", "--test-run", c.commit_id], catch_exceptions=False)
113 runner.invoke(cli, ["annotate", "--test-run", c.commit_id], catch_exceptions=False)
114
115 from muse.core.store import read_commit
116 record = read_commit(repo, c.commit_id)
117 assert record is not None
118 assert record.test_runs == 2 # GCounter: monotone increment
119
120 def test_unknown_commit_exits_error(self, repo: pathlib.Path) -> None:
121 (repo / ".muse" / "refs" / "heads" / "main").write_text("nosuchcommit")
122 result = runner.invoke(cli, ["annotate", "nosuchcommit"])
123 assert result.exit_code != 0
File History 5 commits
sha256:660fcac1df3ab28f61862e961890bd2ca8b754fa0242079d93ca1e25037ec8a6 chore(tests): add docstring to tests/__init__.py so rc14 tr… Human 26 days ago
sha256:d8316ffae901be06347e16ab55be11868eb519dd16ade3e8aa16a99e662f7e62 baseline: rc14 re-baseline after rc3 store corruption recovery Human patch 26 days ago
sha256:50d413a635f57761c3fce1f70251b957b6423821525bf330747ef4007339222e Merge branch 'dev' into main Human 29 days ago
sha256:fb67fed5a4d3e40de84bdd163de94ef1386570bef1dd1a020a732c8a038962ce Merge branch 'dev' into main Human 48 days ago
sha256:1c4b3e3a9a1f300774c3ee662b572a698d5fd405bf765a71e6011a2e9c3eaaaa feat: Muse — version control for the agent era Human 100 days ago