cgcardona / muse public
test_annotate_command.py python
111 lines 4.2 KB
766ee24d feat: code domain leverages core invariants, query engine, manifests, p… Gabriel Cardona <gabriel@tellurstori.com> 1d ago
1 """Tests for muse annotate — CRDT-backed commit annotations."""
2 from __future__ import annotations
3
4 import datetime
5 import pathlib
6
7 import pytest
8 from typer.testing import CliRunner
9
10 from muse.cli.app import cli
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("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, commit_id: str = "abc12345") -> CommitRecord:
36 record = CommitRecord(
37 commit_id=commit_id,
38 repo_id="test-repo",
39 branch="main",
40 snapshot_id="snap1",
41 message="test commit",
42 committed_at=datetime.datetime.now(datetime.timezone.utc),
43 author="test-author",
44 )
45 write_commit(root, record)
46 (root / ".muse" / "refs" / "heads" / "main").write_text(commit_id)
47 return record
48
49
50 # ---------------------------------------------------------------------------
51 # Tests
52 # ---------------------------------------------------------------------------
53
54
55 class TestAnnotateCommand:
56 def test_show_annotations_when_no_flags(self, repo: pathlib.Path) -> None:
57 _write_commit(repo, "abc12345")
58 result = runner.invoke(cli, ["annotate", "abc12345"], catch_exceptions=False)
59 assert result.exit_code == 0
60 assert "reviewed-by" in result.output
61
62 def test_add_reviewer(self, repo: pathlib.Path) -> None:
63 _write_commit(repo, "abc12345")
64 result = runner.invoke(cli, ["annotate", "--reviewed-by", "agent-x", "abc12345"], catch_exceptions=False)
65 assert result.exit_code == 0, result.output
66 assert "agent-x" in result.output
67
68 # Verify it's persisted.
69 from muse.core.store import read_commit
70 record = read_commit(repo, "abc12345")
71 assert record is not None
72 assert "agent-x" in record.reviewed_by
73
74 def test_add_multiple_reviewers(self, repo: pathlib.Path) -> None:
75 _write_commit(repo, "abc12345")
76 # Pass comma-separated reviewers in one call.
77 r = runner.invoke(cli, ["annotate", "--reviewed-by", "alice,bob", "abc12345"], catch_exceptions=False)
78 assert r.exit_code == 0, r.output
79
80 from muse.core.store import read_commit
81 record = read_commit(repo, "abc12345")
82 assert record is not None
83 # ORSet semantics: both reviewers should be present.
84 assert "alice" in record.reviewed_by
85 assert "bob" in record.reviewed_by
86
87 def test_add_reviewer_idempotent(self, repo: pathlib.Path) -> None:
88 _write_commit(repo, "abc12345")
89 runner.invoke(cli, ["annotate", "--reviewed-by", "alice", "abc12345"], catch_exceptions=False)
90 runner.invoke(cli, ["annotate", "--reviewed-by", "alice", "abc12345"], catch_exceptions=False)
91
92 from muse.core.store import read_commit
93 record = read_commit(repo, "abc12345")
94 assert record is not None
95 # ORSet: adding the same element twice should appear once.
96 assert record.reviewed_by.count("alice") == 1
97
98 def test_increment_test_run_counter(self, repo: pathlib.Path) -> None:
99 _write_commit(repo, "abc12345")
100 runner.invoke(cli, ["annotate", "--test-run", "abc12345"], catch_exceptions=False)
101 runner.invoke(cli, ["annotate", "--test-run", "abc12345"], catch_exceptions=False)
102
103 from muse.core.store import read_commit
104 record = read_commit(repo, "abc12345")
105 assert record is not None
106 assert record.test_runs == 2 # GCounter: monotone increment
107
108 def test_unknown_commit_exits_error(self, repo: pathlib.Path) -> None:
109 (repo / ".muse" / "refs" / "heads" / "main").write_text("nosuchcommit")
110 result = runner.invoke(cli, ["annotate", "nosuchcommit"])
111 assert result.exit_code != 0