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