gabriel / muse public
test_cli_show.py python
237 lines 8.6 KB
Raw
1 """Tests for muse read — inspect a commit's metadata, diff, and files."""
2
3 import json
4 import pathlib
5
6 import pytest
7 from tests.cli_test_helper import CliRunner
8 from muse.core.paths import config_toml_path
9
10 cli = None # argparse migration — CliRunner ignores this arg
11
12 runner = CliRunner()
13
14
15 @pytest.fixture
16 def repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path:
17 monkeypatch.chdir(tmp_path)
18 monkeypatch.setenv("MUSE_REPO_ROOT", str(tmp_path))
19 result = runner.invoke(cli, ["init"])
20 assert result.exit_code == 0, result.output
21 return tmp_path
22
23
24 def _write(repo: pathlib.Path, filename: str, content: str = "data") -> None:
25 (repo / filename).write_text(content)
26
27
28 def _commit(msg: str = "initial", **flags: str) -> str:
29 runner.invoke(cli, ["code", "add", "."])
30 args = ["commit", "-m", msg]
31 for k, v in flags.items():
32 args += [f"--{k}", v]
33 result = runner.invoke(cli, args)
34 assert result.exit_code == 0, result.output
35 # output: "[main abcd1234] msg" → strip trailing ] from token
36 return result.output.split()[1].rstrip("]")
37
38
39 class TestShowHead:
40 def test_shows_commit_id(self, repo: pathlib.Path) -> None:
41 _write(repo, "beat.py")
42 _commit("initial commit")
43 result = runner.invoke(cli, ["read"])
44 assert result.exit_code == 0, result.output
45 assert "commit" in result.output
46
47 def test_shows_message(self, repo: pathlib.Path) -> None:
48 _write(repo, "beat.py")
49 _commit("my special message")
50 result = runner.invoke(cli, ["read"])
51 assert result.exit_code == 0
52 assert "my special message" in result.output
53
54 def test_shows_date(self, repo: pathlib.Path) -> None:
55 _write(repo, "beat.py")
56 _commit("dated commit")
57 result = runner.invoke(cli, ["read"])
58 assert result.exit_code == 0
59 assert "Date:" in result.output
60
61 def test_shows_author(self, repo: pathlib.Path) -> None:
62 _write(repo, "beat.py")
63 runner.invoke(cli, ["commit", "-m", "authored", "--author", "Gabriel"])
64 result = runner.invoke(cli, ["read"])
65 assert result.exit_code == 0
66 assert "Gabriel" in result.output
67
68 def test_author_from_identity_when_not_explicit(self, repo: pathlib.Path) -> None:
69 import muse.core.identity as _id_mod
70
71 # Wire a hub URL so get_config_value("user.handle") can resolve.
72 (config_toml_path(repo)).write_text('[hub]\nurl = "https://localhost:1337"\n')
73 # Write a minimal identity entry for that hub (patched path via conftest).
74 _id_mod._IDENTITY_FILE.parent.mkdir(parents=True, exist_ok=True)
75 _id_mod._IDENTITY_FILE.write_text(
76 '["localhost:1337"]\nhandle = "gabriel"\ntype = "human"\nalgorithm = "ed25519"\n'
77 )
78
79 _write(repo, "beat.py")
80 _commit("implicit author")
81 result = runner.invoke(cli, ["read"])
82 assert result.exit_code == 0
83 assert "Author:" in result.output
84
85
86 class TestShowStat:
87 def test_shows_added_file_by_default(self, repo: pathlib.Path) -> None:
88 _write(repo, "beat.py")
89 _commit("add beat")
90 result = runner.invoke(cli, ["read"])
91 assert result.exit_code == 0
92 assert "beat.py" in result.output
93 assert "+" in result.output
94
95 def test_no_stat_flag_hides_files(self, repo: pathlib.Path) -> None:
96 _write(repo, "beat.py")
97 _commit("add beat")
98 result = runner.invoke(cli, ["read", "--no-stat"])
99 assert result.exit_code == 0
100 assert "beat.py" not in result.output
101
102 def test_shows_modified_file(self, repo: pathlib.Path) -> None:
103 _write(repo, "beat.py", "v1")
104 _commit("v1")
105 _write(repo, "beat.py", "v2")
106 _commit("v2")
107 result = runner.invoke(cli, ["read"])
108 assert result.exit_code == 0
109 assert "beat.py" in result.output
110
111 def test_file_change_count(self, repo: pathlib.Path) -> None:
112 _write(repo, "a.py")
113 _write(repo, "b.py")
114 _commit("two files")
115 result = runner.invoke(cli, ["read"])
116 assert result.exit_code == 0
117 assert "added" in result.output or "file(s) changed" in result.output
118
119 def test_no_files_changed_no_count_line(self, repo: pathlib.Path) -> None:
120 _write(repo, "beat.py", "v1")
121 _commit("v1")
122 _write(repo, "beat.py", "v1")
123 result = runner.invoke(cli, ["commit", "--allow-empty"])
124 # empty commit — stat block should show no files changed
125 result2 = runner.invoke(cli, ["read"])
126 assert result2.exit_code == 0
127
128
129 class TestShowMetadata:
130 def test_shows_section_metadata(self, repo: pathlib.Path) -> None:
131 _write(repo, "beat.py")
132 runner.invoke(cli, ["commit", "-m", "verse", "--section", "verse"])
133 result = runner.invoke(cli, ["read"])
134 assert result.exit_code == 0
135 assert "section" in result.output
136 assert "verse" in result.output
137
138 def test_shows_track_and_emotion(self, repo: pathlib.Path) -> None:
139 _write(repo, "beat.py")
140 runner.invoke(cli, ["commit", "-m", "drums", "--track", "drums", "--emotion", "joyful"])
141 result = runner.invoke(cli, ["read"])
142 assert result.exit_code == 0
143 assert "track" in result.output
144 assert "emotion" in result.output
145
146
147 class TestShowRef:
148 def test_show_specific_commit(self, repo: pathlib.Path) -> None:
149 _write(repo, "beat.py")
150 short = _commit("first")
151 _write(repo, "lead.py")
152 _commit("second")
153 # show the first commit by prefix
154 result = runner.invoke(cli, ["read", short])
155 assert result.exit_code == 0
156 assert "first" in result.output
157
158 def test_show_unknown_ref_errors(self, repo: pathlib.Path) -> None:
159 _write(repo, "beat.py")
160 _commit("only")
161 result = runner.invoke(cli, ["read", "deadbeef"])
162 assert result.exit_code != 0
163 assert "not found" in result.stderr.lower() or "deadbeef" in result.stderr
164
165 def test_show_no_commits_errors(self, repo: pathlib.Path) -> None:
166 result = runner.invoke(cli, ["read"])
167 assert result.exit_code != 0
168
169
170 class TestShowParent:
171 def test_shows_parent_after_second_commit(self, repo: pathlib.Path) -> None:
172 _write(repo, "beat.py")
173 _commit("first")
174 _write(repo, "lead.py")
175 _commit("second")
176 result = runner.invoke(cli, ["read"])
177 assert result.exit_code == 0
178 assert "Parent:" in result.output
179
180 def test_root_commit_has_no_parent_line(self, repo: pathlib.Path) -> None:
181 _write(repo, "beat.py")
182 short = _commit("root commit")
183 result = runner.invoke(cli, ["read", short])
184 assert result.exit_code == 0
185 assert "Parent:" not in result.output
186
187
188 class TestShowJson:
189 def test_json_output_is_valid(self, repo: pathlib.Path) -> None:
190 _write(repo, "beat.py")
191 _commit("json test")
192 result = runner.invoke(cli, ["read", "--json"])
193 assert result.exit_code == 0
194 data = json.loads(result.output)
195 assert "commit_id" in data
196 assert "message" in data
197
198 def test_json_contains_message(self, repo: pathlib.Path) -> None:
199 _write(repo, "beat.py")
200 _commit("the message")
201 result = runner.invoke(cli, ["read", "--json"])
202 data = json.loads(result.output)
203 assert data["message"] == "the message"
204
205 def test_json_with_stat_includes_file_lists(self, repo: pathlib.Path) -> None:
206 _write(repo, "beat.py")
207 _commit("add beat")
208 result = runner.invoke(cli, ["read", "--json"])
209 data = json.loads(result.output)
210 assert "files_added" in data
211 assert "beat.py" in data["files_added"]
212
213 def test_json_no_stat_excludes_file_lists(self, repo: pathlib.Path) -> None:
214 _write(repo, "beat.py")
215 _commit("add beat")
216 result = runner.invoke(cli, ["read", "--json", "--no-stat"])
217 data = json.loads(result.output)
218 assert "files_added" not in data
219
220 def test_json_stat_shows_removed_file(self, repo: pathlib.Path) -> None:
221 _write(repo, "beat.py", "v1")
222 _commit("add")
223 (repo / "beat.py").unlink()
224 _write(repo, "lead.py", "new")
225 _commit("swap")
226 result = runner.invoke(cli, ["read", "--json"])
227 data = json.loads(result.output)
228 assert "beat.py" in data["files_removed"]
229
230 def test_json_stat_shows_modified_file(self, repo: pathlib.Path) -> None:
231 _write(repo, "beat.py", "v1")
232 _commit("v1")
233 _write(repo, "beat.py", "v2")
234 _commit("v2")
235 result = runner.invoke(cli, ["read", "--json"])
236 data = json.loads(result.output)
237 assert "beat.py" in data["files_modified"]
File History 1 commit