gabriel / muse public
test_query_history_supercharge.py python
694 lines 29.2 KB
Raw
sha256:81ae324db5ad375fbfe4834c6fcb378312cafad3cc92dec5d3e5c427306621a2 fix: remove commit_exists filter from have anchors — server… Sonnet 4.6 patch 20 days ago
1 """TDD supercharge tests for ``muse code query-history``.
2
3 Gaps being closed
4 -----------------
5 - ``-j`` alias for ``--json``
6 - ``exit_code`` and ``duration_ms`` in all three JSON envelopes
7 - ``truncated`` in introduced-only and removed-only JSON
8 - ``_QueryHistoryJson``, ``_IntroducedJson``, ``_RemovedJson`` TypedDicts
9 - Sanitize predicate values in human-readable output
10 - Unit tests for ``_SymbolHistory``, ``_sort_key_fn``, ``_collect_addresses``,
11 ``_RemovedSymbol``, ``_IntroducedSymbol``
12 - ``--sort last`` coverage
13 - ``stable=True`` correctness
14 - ``register()`` and ``run()`` docstring completeness
15
16 Test classes
17 ------------
18 TestJsonAlias -j alias works identically to --json
19 TestDefaultModeJson exit_code, duration_ms, schema in default mode
20 TestIntroducedJson exit_code, duration_ms, truncated in introduced-only
21 TestRemovedJson exit_code, duration_ms, truncated in removed-only
22 TestTypedDicts _QueryHistoryJson, _IntroducedJson, _RemovedJson
23 TestUnitSymbolHistory _SymbolHistory methods
24 TestUnitSortKeyFn _sort_key_fn behaviour
25 TestUnitDiffSymbols _RemovedSymbol, _IntroducedSymbol to_dict
26 TestCLIFilters --sort last, stable flag, --min-changes+--changed-only
27 TestCLISecurity null bytes / ANSI in predicates
28 TestDocstrings run(), register() doc completeness
29 """
30
31 from __future__ import annotations
32 from collections.abc import Mapping
33
34 import json
35 import pathlib
36 import textwrap
37 import typing
38
39 import pytest
40
41 from tests.cli_test_helper import CliRunner
42
43 cli = None
44 runner = CliRunner()
45
46
47 # ---------------------------------------------------------------------------
48 # Helpers
49 # ---------------------------------------------------------------------------
50
51
52 def _run(root: pathlib.Path, *args: str) -> "InvokeResult":
53 return runner.invoke(cli, list(args), env={"MUSE_REPO_ROOT": str(root)})
54
55
56 def _commit(root: pathlib.Path, msg: str = "commit") -> None:
57 r = _run(root, "code", "add", ".")
58 assert r.exit_code == 0, r.output
59 r2 = _run(root, "commit", "-m", msg)
60 assert r2.exit_code == 0, r2.output
61
62
63 # ---------------------------------------------------------------------------
64 # Fixture — repo with two commits so history is meaningful
65 # ---------------------------------------------------------------------------
66
67
68 @pytest.fixture
69 def repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path:
70 """Code-domain repo with two commits containing different function versions."""
71 monkeypatch.chdir(tmp_path)
72 monkeypatch.setenv("MUSE_REPO_ROOT", str(tmp_path))
73 r = _run(tmp_path, "init", "--domain", "code")
74 assert r.exit_code == 0, r.output
75
76 (tmp_path / "billing.py").write_text(textwrap.dedent("""\
77 def compute_total(items: list[int]) -> int:
78 return sum(items)
79
80 def validate_amount(amount: float) -> bool:
81 return amount > 0
82 """))
83 _commit(tmp_path, "initial billing")
84
85 # Second commit: change compute_total, add new function, keep validate_amount
86 (tmp_path / "billing.py").write_text(textwrap.dedent("""\
87 def compute_total(items: list[int]) -> int:
88 return sum(items) * 2 # changed
89
90 def validate_amount(amount: float) -> bool:
91 return amount > 0
92
93 def format_total(total: int) -> str:
94 return f"${total}"
95 """))
96 _commit(tmp_path, "update billing")
97 return tmp_path
98
99
100 # ---------------------------------------------------------------------------
101 # 1. -j alias
102 # ---------------------------------------------------------------------------
103
104
105 class TestJsonAlias:
106 def test_j_alias_exits_zero(self, repo: pathlib.Path) -> None:
107 r = _run(repo, "code", "query-history", "kind=function", "-j")
108 assert r.exit_code == 0, r.output
109
110 def test_j_alias_emits_valid_json(self, repo: pathlib.Path) -> None:
111 r = _run(repo, "code", "query-history", "kind=function", "-j")
112 assert r.exit_code == 0, r.output
113 data = json.loads(r.output.strip())
114 assert isinstance(data, dict)
115
116 def test_j_alias_has_results(self, repo: pathlib.Path) -> None:
117 r = _run(repo, "code", "query-history", "kind=function", "-j")
118 data = json.loads(r.output)
119 assert "results" in data
120
121 def test_j_alias_same_keys_as_json_flag(self, repo: pathlib.Path) -> None:
122 r1 = _run(repo, "code", "query-history", "kind=function", "--json")
123 r2 = _run(repo, "code", "query-history", "kind=function", "-j")
124 d1 = json.loads(r1.output)
125 d2 = json.loads(r2.output)
126 d1.pop("duration_ms", None)
127 d2.pop("duration_ms", None)
128 assert set(d1.keys()) == set(d2.keys())
129
130 def test_j_alias_result_count_matches(self, repo: pathlib.Path) -> None:
131 r1 = _run(repo, "code", "query-history", "kind=function", "--json")
132 r2 = _run(repo, "code", "query-history", "kind=function", "-j")
133 assert len(json.loads(r1.output)["results"]) == len(json.loads(r2.output)["results"])
134
135 def test_j_alias_introduced_mode(self, repo: pathlib.Path) -> None:
136 r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "-j")
137 assert r.exit_code == 0, r.output
138 data = json.loads(r.output)
139 assert data["mode"] == "introduced-only"
140
141 def test_j_alias_removed_mode(self, repo: pathlib.Path) -> None:
142 r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "-j")
143 assert r.exit_code == 0, r.output
144 data = json.loads(r.output)
145 assert data["mode"] == "removed-only"
146
147
148 # ---------------------------------------------------------------------------
149 # 2. Default-mode JSON schema: exit_code + duration_ms
150 # ---------------------------------------------------------------------------
151
152
153 class TestDefaultModeJson:
154 def test_has_exit_code(self, repo: pathlib.Path) -> None:
155 r = _run(repo, "code", "query-history", "kind=function", "--json")
156 data = json.loads(r.output)
157 assert "exit_code" in data
158
159 def test_exit_code_is_zero(self, repo: pathlib.Path) -> None:
160 r = _run(repo, "code", "query-history", "kind=function", "--json")
161 data = json.loads(r.output)
162 assert data["exit_code"] == 0
163
164 def test_has_duration_ms(self, repo: pathlib.Path) -> None:
165 r = _run(repo, "code", "query-history", "kind=function", "--json")
166 data = json.loads(r.output)
167 assert "duration_ms" in data
168
169 def test_duration_ms_positive(self, repo: pathlib.Path) -> None:
170 r = _run(repo, "code", "query-history", "kind=function", "--json")
171 data = json.loads(r.output)
172 assert isinstance(data["duration_ms"], float)
173 assert data["duration_ms"] > 0
174
175 def test_has_schema_version(self, repo: pathlib.Path) -> None:
176 r = _run(repo, "code", "query-history", "kind=function", "--json")
177 data = json.loads(r.output)
178 assert "schema" in data
179
180 def test_has_commits_scanned(self, repo: pathlib.Path) -> None:
181 r = _run(repo, "code", "query-history", "kind=function", "--json")
182 data = json.loads(r.output)
183 assert "commits_scanned" in data
184 assert isinstance(data["commits_scanned"], int)
185
186 def test_has_truncated(self, repo: pathlib.Path) -> None:
187 r = _run(repo, "code", "query-history", "kind=function", "--json")
188 data = json.loads(r.output)
189 assert "truncated" in data
190
191 def test_result_record_schema(self, repo: pathlib.Path) -> None:
192 r = _run(repo, "code", "query-history", "kind=function", "--json")
193 data = json.loads(r.output)
194 assert data["results"]
195 rec = data["results"][0]
196 required = {
197 "address", "kind", "language", "commit_count", "change_count",
198 "first_commit_id", "first_committed_at",
199 "last_commit_id", "last_committed_at", "stable",
200 }
201 assert required <= set(rec.keys())
202
203 def test_no_match_exit_code_zero(self, repo: pathlib.Path) -> None:
204 r = _run(repo, "code", "query-history", "name=zzz_nonexistent", "--json")
205 assert r.exit_code == 0
206 data = json.loads(r.output)
207 assert data["exit_code"] == 0
208 assert data["results"] == []
209
210 def test_no_match_has_duration_ms(self, repo: pathlib.Path) -> None:
211 r = _run(repo, "code", "query-history", "name=zzz_nonexistent", "--json")
212 data = json.loads(r.output)
213 assert "duration_ms" in data
214
215
216 # ---------------------------------------------------------------------------
217 # 3. introduced-only JSON: exit_code + duration_ms + truncated
218 # ---------------------------------------------------------------------------
219
220
221 class TestIntroducedJson:
222 def test_has_exit_code(self, repo: pathlib.Path) -> None:
223 r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json")
224 data = json.loads(r.output)
225 assert "exit_code" in data
226
227 def test_exit_code_is_zero(self, repo: pathlib.Path) -> None:
228 r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json")
229 data = json.loads(r.output)
230 assert data["exit_code"] == 0
231
232 def test_has_duration_ms(self, repo: pathlib.Path) -> None:
233 r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json")
234 data = json.loads(r.output)
235 assert "duration_ms" in data
236
237 def test_duration_ms_positive(self, repo: pathlib.Path) -> None:
238 r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json")
239 data = json.loads(r.output)
240 assert isinstance(data["duration_ms"], float)
241 assert data["duration_ms"] > 0
242
243 def test_has_truncated(self, repo: pathlib.Path) -> None:
244 r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json")
245 data = json.loads(r.output)
246 assert "truncated" in data
247
248 def test_truncated_false_without_limit(self, repo: pathlib.Path) -> None:
249 r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json")
250 data = json.loads(r.output)
251 assert data["truncated"] is False
252
253 def test_truncated_true_when_limited(self, repo: pathlib.Path) -> None:
254 # format_total was introduced in commit 2; limit=0 still gets it
255 # Check we have at least 1 introduced symbol first
256 r_all = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json")
257 total = len(json.loads(r_all.output)["results"])
258 if total <= 1:
259 pytest.skip("need >1 introduced symbols to test truncation")
260 r = _run(repo, "code", "query-history", "kind=function",
261 "--introduced-only", "--json", "--limit", "1")
262 data = json.loads(r.output)
263 assert data["truncated"] is True
264
265 def test_finds_format_total(self, repo: pathlib.Path) -> None:
266 r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json")
267 data = json.loads(r.output)
268 addrs = [res["address"] for res in data["results"]]
269 # format_total was added in the second commit — it's net-new
270 assert any("format_total" in a for a in addrs)
271
272
273 # ---------------------------------------------------------------------------
274 # 4. removed-only JSON: exit_code + duration_ms + truncated
275 # ---------------------------------------------------------------------------
276
277
278 class TestRemovedJson:
279 def test_has_exit_code(self, repo: pathlib.Path) -> None:
280 r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "--json")
281 data = json.loads(r.output)
282 assert "exit_code" in data
283
284 def test_exit_code_is_zero(self, repo: pathlib.Path) -> None:
285 r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "--json")
286 data = json.loads(r.output)
287 assert data["exit_code"] == 0
288
289 def test_has_duration_ms(self, repo: pathlib.Path) -> None:
290 r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "--json")
291 data = json.loads(r.output)
292 assert "duration_ms" in data
293
294 def test_duration_ms_positive(self, repo: pathlib.Path) -> None:
295 r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "--json")
296 data = json.loads(r.output)
297 assert isinstance(data["duration_ms"], float)
298 assert data["duration_ms"] > 0
299
300 def test_has_truncated(self, repo: pathlib.Path) -> None:
301 r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "--json")
302 data = json.loads(r.output)
303 assert "truncated" in data
304
305 def test_truncated_false_without_limit(self, repo: pathlib.Path) -> None:
306 r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "--json")
307 data = json.loads(r.output)
308 assert data["truncated"] is False
309
310
311 # ---------------------------------------------------------------------------
312 # 5. TypedDicts
313 # ---------------------------------------------------------------------------
314
315
316 class TestTypedDicts:
317 def test_query_history_json_importable(self) -> None:
318 from muse.cli.commands.query_history import _QueryHistoryJson
319 assert _QueryHistoryJson is not None
320
321 def test_query_history_json_has_exit_code(self) -> None:
322 from muse.cli.commands.query_history import _QueryHistoryJson
323 hints = typing.get_type_hints(_QueryHistoryJson)
324 assert "exit_code" in hints
325
326 def test_query_history_json_has_duration_ms(self) -> None:
327 from muse.cli.commands.query_history import _QueryHistoryJson
328 hints = typing.get_type_hints(_QueryHistoryJson)
329 assert "duration_ms" in hints
330
331 def test_query_history_json_has_schema_version(self) -> None:
332 from muse.cli.commands.query_history import _QueryHistoryJson
333 hints = typing.get_type_hints(_QueryHistoryJson)
334 assert "schema" in hints
335
336 def test_query_history_json_has_truncated(self) -> None:
337 from muse.cli.commands.query_history import _QueryHistoryJson
338 hints = typing.get_type_hints(_QueryHistoryJson)
339 assert "truncated" in hints
340
341 def test_query_history_json_has_results(self) -> None:
342 from muse.cli.commands.query_history import _QueryHistoryJson
343 hints = typing.get_type_hints(_QueryHistoryJson)
344 assert "results" in hints
345
346 def test_introduced_json_importable(self) -> None:
347 from muse.cli.commands.query_history import _IntroducedJson
348 assert _IntroducedJson is not None
349
350 def test_introduced_json_has_exit_code(self) -> None:
351 from muse.cli.commands.query_history import _IntroducedJson
352 hints = typing.get_type_hints(_IntroducedJson)
353 assert "exit_code" in hints
354
355 def test_introduced_json_has_truncated(self) -> None:
356 from muse.cli.commands.query_history import _IntroducedJson
357 hints = typing.get_type_hints(_IntroducedJson)
358 assert "truncated" in hints
359
360 def test_removed_json_importable(self) -> None:
361 from muse.cli.commands.query_history import _RemovedJson
362 assert _RemovedJson is not None
363
364 def test_removed_json_has_exit_code(self) -> None:
365 from muse.cli.commands.query_history import _RemovedJson
366 hints = typing.get_type_hints(_RemovedJson)
367 assert "exit_code" in hints
368
369 def test_removed_json_has_truncated(self) -> None:
370 from muse.cli.commands.query_history import _RemovedJson
371 hints = typing.get_type_hints(_RemovedJson)
372 assert "truncated" in hints
373
374
375 # ---------------------------------------------------------------------------
376 # 6. Unit — _SymbolHistory
377 # ---------------------------------------------------------------------------
378
379
380 class TestUnitSymbolHistory:
381 def test_initial_state(self) -> None:
382 from muse.cli.commands.query_history import _SymbolHistory
383 h = _SymbolHistory("billing.py::foo", "function", "Python")
384 assert h.commit_count == 0
385 assert h.change_count == 0
386 assert h.address == "billing.py::foo"
387
388 def test_record_increments_commit_count(self) -> None:
389 from muse.cli.commands.query_history import _SymbolHistory
390 h = _SymbolHistory("billing.py::foo", "function", "Python")
391 h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1")
392 assert h.commit_count == 1
393
394 def test_record_tracks_first_and_last(self) -> None:
395 from muse.cli.commands.query_history import _SymbolHistory
396 h = _SymbolHistory("billing.py::foo", "function", "Python")
397 h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1")
398 h.record("sha256:bbb", "2026-02-01T00:00:00+00:00", "cid2")
399 assert h.first_commit_id == "sha256:aaa"
400 assert h.last_commit_id == "sha256:bbb"
401
402 def test_change_count_same_body(self) -> None:
403 from muse.cli.commands.query_history import _SymbolHistory
404 h = _SymbolHistory("billing.py::foo", "function", "Python")
405 h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1")
406 h.record("sha256:bbb", "2026-02-01T00:00:00+00:00", "cid1") # same cid
407 assert h.change_count == 1
408 assert h.commit_count == 2
409
410 def test_change_count_different_bodies(self) -> None:
411 from muse.cli.commands.query_history import _SymbolHistory
412 h = _SymbolHistory("billing.py::foo", "function", "Python")
413 h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1")
414 h.record("sha256:bbb", "2026-02-01T00:00:00+00:00", "cid2")
415 assert h.change_count == 2
416
417 def test_stable_true_when_one_version(self) -> None:
418 from muse.cli.commands.query_history import _SymbolHistory
419 h = _SymbolHistory("billing.py::foo", "function", "Python")
420 h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1")
421 d = h.to_dict()
422 assert d["stable"] is True
423
424 def test_stable_false_when_multiple_versions(self) -> None:
425 from muse.cli.commands.query_history import _SymbolHistory
426 h = _SymbolHistory("billing.py::foo", "function", "Python")
427 h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1")
428 h.record("sha256:bbb", "2026-02-01T00:00:00+00:00", "cid2")
429 d = h.to_dict()
430 assert d["stable"] is False
431
432 def test_to_dict_schema(self) -> None:
433 from muse.cli.commands.query_history import _SymbolHistory
434 h = _SymbolHistory("billing.py::foo", "function", "Python")
435 h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1")
436 d = h.to_dict()
437 required = {
438 "address", "kind", "language", "commit_count", "change_count",
439 "first_commit_id", "first_committed_at",
440 "last_commit_id", "last_committed_at", "stable",
441 }
442 assert required <= set(d.keys())
443
444 def test_first_committed_at_truncated_to_date(self) -> None:
445 from muse.cli.commands.query_history import _SymbolHistory
446 h = _SymbolHistory("billing.py::foo", "function", "Python")
447 h.record("sha256:aaa", "2026-04-18T12:34:56+00:00", "cid1")
448 d = h.to_dict()
449 assert d["first_committed_at"] == "2026-04-18"
450
451
452 # ---------------------------------------------------------------------------
453 # 7. Unit — _sort_key_fn
454 # ---------------------------------------------------------------------------
455
456
457 class TestUnitSortKeyFn:
458 def _make_history(self, address: str, commits: int, changes: int,
459 first: str, last: str) -> "_SymbolHistory":
460 from muse.cli.commands.query_history import _SymbolHistory
461 h = _SymbolHistory(address, "function", "Python")
462 # Simulate commit_count and change_count without real records
463 h.commit_count = commits
464 for i in range(changes):
465 h.content_ids.add(f"cid{i}")
466 h.first_committed_at = first
467 h.last_committed_at = last
468 h.first_commit_id = "sha256:aaa"
469 h.last_commit_id = "sha256:bbb"
470 return h
471
472 def test_sort_by_address(self) -> None:
473 from muse.cli.commands.query_history import _sort_key_fn
474 fn = _sort_key_fn("address")
475 h1 = self._make_history("z.py::foo", 1, 1, "2026-01-01", "2026-01-01")
476 h2 = self._make_history("a.py::bar", 1, 1, "2026-01-01", "2026-01-01")
477 assert fn(h2) < fn(h1) # type: ignore[arg-type]
478
479 def test_sort_by_commits_descending(self) -> None:
480 from muse.cli.commands.query_history import _sort_key_fn
481 fn = _sort_key_fn("commits")
482 h1 = self._make_history("a.py::foo", 10, 1, "2026-01-01", "2026-01-01")
483 h2 = self._make_history("b.py::bar", 1, 1, "2026-01-01", "2026-01-01")
484 assert fn(h1) < fn(h2) # more commits sorts first # type: ignore[arg-type]
485
486 def test_sort_by_changes_descending(self) -> None:
487 from muse.cli.commands.query_history import _sort_key_fn
488 fn = _sort_key_fn("changes")
489 h1 = self._make_history("a.py::foo", 1, 5, "2026-01-01", "2026-01-01")
490 h2 = self._make_history("b.py::bar", 1, 1, "2026-01-01", "2026-01-01")
491 assert fn(h1) < fn(h2) # more changes sorts first # type: ignore[arg-type]
492
493 def test_sort_by_first(self) -> None:
494 from muse.cli.commands.query_history import _sort_key_fn
495 fn = _sort_key_fn("first")
496 h1 = self._make_history("a.py::foo", 1, 1, "2026-01-01", "2026-06-01")
497 h2 = self._make_history("b.py::bar", 1, 1, "2026-03-01", "2026-06-01")
498 assert fn(h1) < fn(h2) # earlier first_committed_at sorts first # type: ignore[arg-type]
499
500 def test_sort_by_last(self) -> None:
501 from muse.cli.commands.query_history import _sort_key_fn
502 fn = _sort_key_fn("last")
503 h1 = self._make_history("a.py::foo", 1, 1, "2026-01-01", "2026-01-01")
504 h2 = self._make_history("b.py::bar", 1, 1, "2026-01-01", "2026-06-01")
505 assert fn(h1) < fn(h2) # earlier last_committed_at sorts first # type: ignore[arg-type]
506
507 def test_unknown_sort_falls_back_to_address(self) -> None:
508 from muse.cli.commands.query_history import _sort_key_fn
509 fn = _sort_key_fn("zzz_unknown")
510 h = self._make_history("billing.py::foo", 1, 1, "2026-01-01", "2026-01-01")
511 assert fn(h) == ("billing.py::foo",) # type: ignore[arg-type]
512
513
514 # ---------------------------------------------------------------------------
515 # 8. Unit — _RemovedSymbol / _IntroducedSymbol
516 # ---------------------------------------------------------------------------
517
518
519 class TestUnitDiffSymbols:
520 def _make_rec(self) -> Mapping[str, object]:
521 return {
522 "kind": "function",
523 "name": "foo",
524 "qualified_name": "foo",
525 "lineno": 1,
526 "end_lineno": 3,
527 "content_id": "sha256:abc",
528 "body_hash": "sha256:abc",
529 "signature_id": "sha256:def",
530 }
531
532 def test_removed_to_dict_schema(self) -> None:
533 from muse.cli.commands.query_history import _RemovedSymbol
534 sym = _RemovedSymbol("billing.py::foo", self._make_rec(), "Python") # type: ignore[arg-type]
535 d = sym.to_dict()
536 assert d["address"] == "billing.py::foo"
537 assert d["status"] == "removed"
538 assert d["kind"] == "function"
539 assert d["language"] == "Python"
540
541 def test_introduced_to_dict_schema(self) -> None:
542 from muse.cli.commands.query_history import _IntroducedSymbol
543 sym = _IntroducedSymbol("billing.py::bar", self._make_rec(), "Python") # type: ignore[arg-type]
544 d = sym.to_dict()
545 assert d["address"] == "billing.py::bar"
546 assert d["status"] == "introduced"
547 assert d["kind"] == "function"
548 assert d["language"] == "Python"
549
550
551 # ---------------------------------------------------------------------------
552 # 9. CLI filters — stable, --sort last, interaction tests
553 # ---------------------------------------------------------------------------
554
555
556 class TestCLIFilters:
557 def test_validate_amount_is_stable(self, repo: pathlib.Path) -> None:
558 # validate_amount never changed — should be stable=True
559 r = _run(repo, "code", "query-history", "name=validate_amount", "--json")
560 data = json.loads(r.output)
561 assert data["results"]
562 rec = data["results"][0]
563 assert rec["stable"] is True
564 assert rec["change_count"] == 1
565
566 def test_compute_total_is_not_stable(self, repo: pathlib.Path) -> None:
567 # compute_total changed between commits — stable=False
568 r = _run(repo, "code", "query-history", "name=compute_total", "--json")
569 data = json.loads(r.output)
570 assert data["results"]
571 rec = data["results"][0]
572 assert rec["stable"] is False
573 assert rec["change_count"] >= 2
574
575 def test_sort_last(self, repo: pathlib.Path) -> None:
576 r = _run(repo, "code", "query-history", "kind=function", "--sort", "last", "--json")
577 assert r.exit_code == 0, r.output
578 data = json.loads(r.output)
579 dates = [rec["last_committed_at"] for rec in data["results"]]
580 assert dates == sorted(dates)
581
582 def test_changed_only_plus_min_changes_interaction(self, repo: pathlib.Path) -> None:
583 # --changed-only (>1) with --min-changes 2 should be consistent
584 r = _run(repo, "code", "query-history", "kind=function",
585 "--changed-only", "--min-changes", "2", "--json")
586 assert r.exit_code == 0, r.output
587 data = json.loads(r.output)
588 for rec in data["results"]:
589 assert rec["change_count"] >= 2
590
591 def test_introduced_finds_format_total(self, repo: pathlib.Path) -> None:
592 r = _run(repo, "code", "query-history", "name=format_total",
593 "--introduced-only", "--json")
594 data = json.loads(r.output)
595 assert any("format_total" in res["address"] for res in data["results"])
596
597 def test_removed_empty_when_nothing_removed(self, repo: pathlib.Path) -> None:
598 r = _run(repo, "code", "query-history", "kind=function",
599 "--removed-only", "--json")
600 data = json.loads(r.output)
601 # validate_amount and compute_total both present in both commits — no removals
602 assert data["exit_code"] == 0
603 removed_addrs = [res["address"] for res in data["results"]]
604 assert not any("validate_amount" in a for a in removed_addrs)
605
606 def test_limit_applied_to_introduced(self, repo: pathlib.Path) -> None:
607 r = _run(repo, "code", "query-history", "kind=function",
608 "--introduced-only", "--json", "--limit", "1")
609 data = json.loads(r.output)
610 assert len(data["results"]) <= 1
611
612
613 # ---------------------------------------------------------------------------
614 # 10. Security
615 # ---------------------------------------------------------------------------
616
617
618 class TestCLISecurity:
619 def test_null_byte_in_predicate_not_in_output(self, repo: pathlib.Path) -> None:
620 r = _run(repo, "code", "query-history", "name=\x00malicious")
621 assert "\x00" not in r.output
622
623 def test_ansi_not_in_json_output(self, repo: pathlib.Path) -> None:
624 r = _run(repo, "code", "query-history", "kind=function", "--json")
625 assert "\x1b" not in r.output
626
627 def test_ansi_not_in_introduced_json(self, repo: pathlib.Path) -> None:
628 r = _run(repo, "code", "query-history", "kind=function",
629 "--introduced-only", "--json")
630 assert "\x1b" not in r.output
631
632
633 # ---------------------------------------------------------------------------
634 # 11. Docstrings
635 # ---------------------------------------------------------------------------
636
637
638 class TestDocstrings:
639 def test_run_docstring_exists(self) -> None:
640 from muse.cli.commands.query_history import run
641 assert run.__doc__ is not None
642 assert len(run.__doc__) > 50
643
644 def test_run_docstring_mentions_json(self) -> None:
645 from muse.cli.commands.query_history import run
646 assert "json" in (run.__doc__ or "").lower()
647
648
649
650 def test_register_docstring_exists(self) -> None:
651 from muse.cli.commands.query_history import register
652 assert register.__doc__ is not None
653 assert len(register.__doc__) > 50
654
655 def test_symbol_history_docstring_exists(self) -> None:
656 from muse.cli.commands.query_history import _SymbolHistory
657 assert _SymbolHistory.__doc__ is not None
658
659 def test_sort_key_fn_docstring_exists(self) -> None:
660 from muse.cli.commands.query_history import _sort_key_fn
661 assert _sort_key_fn.__doc__ is not None
662
663
664 class TestRegisterFlags:
665 def test_json_short_flag(self) -> None:
666 import argparse
667 from muse.cli.commands.query_history import register
668 p = argparse.ArgumentParser()
669 subs = p.add_subparsers()
670 register(subs)
671 args = p.parse_args(["query-history", "kind=function", "-j"])
672 assert args.json_out is True
673
674 def test_json_long_flag(self) -> None:
675 import argparse
676 from muse.cli.commands.query_history import register
677 p = argparse.ArgumentParser()
678 subs = p.add_subparsers()
679 register(subs)
680 args = p.parse_args(["query-history", "kind=function", "--json"])
681 assert args.json_out is True
682
683 def test_default_no_json(self) -> None:
684 import argparse
685 from muse.cli.commands.query_history import register
686 p = argparse.ArgumentParser()
687 subs = p.add_subparsers()
688 register(subs)
689 # Command-specific required args may differ; just check dest exists when possible
690 try:
691 args = p.parse_args(["query-history"])
692 assert args.json_out is False
693 except SystemExit:
694 pass # required positional args missing — flag default still correct
File History 4 commits
sha256:81ae324db5ad375fbfe4834c6fcb378312cafad3cc92dec5d3e5c427306621a2 fix: remove commit_exists filter from have anchors — server… Sonnet 4.6 patch 20 days ago
sha256:36c3cb3e76619d4c30a6d9bf81b5ec4ff148e30dcfed913e3114ca7b43b81c7e fix: rename objects→blobs in push client and all stale test… Sonnet 4.6 patch 22 days ago
sha256:c06a9b9b9fee26c68ea725b44d54b2c0a171301ce9de746d5b656617b4463a9a fix: repair four test failures from post-migration audit Sonnet 4.6 patch 28 days ago
sha256:1900655993c83c4107067375548a7be823e471d2515830842f1a12cba4bd3cdf fix: unified object store migration — idempotent writes, JS… Sonnet 4.6 minor 28 days ago