"""TDD supercharge tests for ``muse code query-history``. Gaps being closed ----------------- - ``-j`` alias for ``--json`` - ``exit_code`` and ``duration_ms`` in all three JSON envelopes - ``truncated`` in introduced-only and removed-only JSON - ``_QueryHistoryJson``, ``_IntroducedJson``, ``_RemovedJson`` TypedDicts - Sanitize predicate values in human-readable output - Unit tests for ``_SymbolHistory``, ``_sort_key_fn``, ``_collect_addresses``, ``_RemovedSymbol``, ``_IntroducedSymbol`` - ``--sort last`` coverage - ``stable=True`` correctness - ``register()`` and ``run()`` docstring completeness Test classes ------------ TestJsonAlias -j alias works identically to --json TestDefaultModeJson exit_code, duration_ms, schema in default mode TestIntroducedJson exit_code, duration_ms, truncated in introduced-only TestRemovedJson exit_code, duration_ms, truncated in removed-only TestTypedDicts _QueryHistoryJson, _IntroducedJson, _RemovedJson TestUnitSymbolHistory _SymbolHistory methods TestUnitSortKeyFn _sort_key_fn behaviour TestUnitDiffSymbols _RemovedSymbol, _IntroducedSymbol to_dict TestCLIFilters --sort last, stable flag, --min-changes+--changed-only TestCLISecurity null bytes / ANSI in predicates TestDocstrings run(), register() doc completeness """ from __future__ import annotations from collections.abc import Mapping import json import pathlib import textwrap import typing import pytest from tests.cli_test_helper import CliRunner cli = None runner = CliRunner() # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def _run(root: pathlib.Path, *args: str) -> "InvokeResult": return runner.invoke(cli, list(args), env={"MUSE_REPO_ROOT": str(root)}) def _commit(root: pathlib.Path, msg: str = "commit") -> None: r = _run(root, "code", "add", ".") assert r.exit_code == 0, r.output r2 = _run(root, "commit", "-m", msg) assert r2.exit_code == 0, r2.output # --------------------------------------------------------------------------- # Fixture — repo with two commits so history is meaningful # --------------------------------------------------------------------------- @pytest.fixture def repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path: """Code-domain repo with two commits containing different function versions.""" monkeypatch.chdir(tmp_path) monkeypatch.setenv("MUSE_REPO_ROOT", str(tmp_path)) r = _run(tmp_path, "init", "--domain", "code") assert r.exit_code == 0, r.output (tmp_path / "billing.py").write_text(textwrap.dedent("""\ def compute_total(items: list[int]) -> int: return sum(items) def validate_amount(amount: float) -> bool: return amount > 0 """)) _commit(tmp_path, "initial billing") # Second commit: change compute_total, add new function, keep validate_amount (tmp_path / "billing.py").write_text(textwrap.dedent("""\ def compute_total(items: list[int]) -> int: return sum(items) * 2 # changed def validate_amount(amount: float) -> bool: return amount > 0 def format_total(total: int) -> str: return f"${total}" """)) _commit(tmp_path, "update billing") return tmp_path # --------------------------------------------------------------------------- # 1. -j alias # --------------------------------------------------------------------------- class TestJsonAlias: def test_j_alias_exits_zero(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "-j") assert r.exit_code == 0, r.output def test_j_alias_emits_valid_json(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "-j") assert r.exit_code == 0, r.output data = json.loads(r.output.strip()) assert isinstance(data, dict) def test_j_alias_has_results(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "-j") data = json.loads(r.output) assert "results" in data def test_j_alias_same_keys_as_json_flag(self, repo: pathlib.Path) -> None: r1 = _run(repo, "code", "query-history", "kind=function", "--json") r2 = _run(repo, "code", "query-history", "kind=function", "-j") d1 = json.loads(r1.output) d2 = json.loads(r2.output) d1.pop("duration_ms", None) d2.pop("duration_ms", None) assert set(d1.keys()) == set(d2.keys()) def test_j_alias_result_count_matches(self, repo: pathlib.Path) -> None: r1 = _run(repo, "code", "query-history", "kind=function", "--json") r2 = _run(repo, "code", "query-history", "kind=function", "-j") assert len(json.loads(r1.output)["results"]) == len(json.loads(r2.output)["results"]) def test_j_alias_introduced_mode(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "-j") assert r.exit_code == 0, r.output data = json.loads(r.output) assert data["mode"] == "introduced-only" def test_j_alias_removed_mode(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "-j") assert r.exit_code == 0, r.output data = json.loads(r.output) assert data["mode"] == "removed-only" # --------------------------------------------------------------------------- # 2. Default-mode JSON schema: exit_code + duration_ms # --------------------------------------------------------------------------- class TestDefaultModeJson: def test_has_exit_code(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--json") data = json.loads(r.output) assert "exit_code" in data def test_exit_code_is_zero(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--json") data = json.loads(r.output) assert data["exit_code"] == 0 def test_has_duration_ms(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--json") data = json.loads(r.output) assert "duration_ms" in data def test_duration_ms_positive(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--json") data = json.loads(r.output) assert isinstance(data["duration_ms"], float) assert data["duration_ms"] > 0 def test_has_schema_version(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--json") data = json.loads(r.output) assert "schema" in data def test_has_commits_scanned(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--json") data = json.loads(r.output) assert "commits_scanned" in data assert isinstance(data["commits_scanned"], int) def test_has_truncated(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--json") data = json.loads(r.output) assert "truncated" in data def test_result_record_schema(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--json") data = json.loads(r.output) assert data["results"] rec = data["results"][0] required = { "address", "kind", "language", "commit_count", "change_count", "first_commit_id", "first_committed_at", "last_commit_id", "last_committed_at", "stable", } assert required <= set(rec.keys()) def test_no_match_exit_code_zero(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "name=zzz_nonexistent", "--json") assert r.exit_code == 0 data = json.loads(r.output) assert data["exit_code"] == 0 assert data["results"] == [] def test_no_match_has_duration_ms(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "name=zzz_nonexistent", "--json") data = json.loads(r.output) assert "duration_ms" in data # --------------------------------------------------------------------------- # 3. introduced-only JSON: exit_code + duration_ms + truncated # --------------------------------------------------------------------------- class TestIntroducedJson: def test_has_exit_code(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json") data = json.loads(r.output) assert "exit_code" in data def test_exit_code_is_zero(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json") data = json.loads(r.output) assert data["exit_code"] == 0 def test_has_duration_ms(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json") data = json.loads(r.output) assert "duration_ms" in data def test_duration_ms_positive(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json") data = json.loads(r.output) assert isinstance(data["duration_ms"], float) assert data["duration_ms"] > 0 def test_has_truncated(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json") data = json.loads(r.output) assert "truncated" in data def test_truncated_false_without_limit(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json") data = json.loads(r.output) assert data["truncated"] is False def test_truncated_true_when_limited(self, repo: pathlib.Path) -> None: # format_total was introduced in commit 2; limit=0 still gets it # Check we have at least 1 introduced symbol first r_all = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json") total = len(json.loads(r_all.output)["results"]) if total <= 1: pytest.skip("need >1 introduced symbols to test truncation") r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json", "--limit", "1") data = json.loads(r.output) assert data["truncated"] is True def test_finds_format_total(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json") data = json.loads(r.output) addrs = [res["address"] for res in data["results"]] # format_total was added in the second commit — it's net-new assert any("format_total" in a for a in addrs) # --------------------------------------------------------------------------- # 4. removed-only JSON: exit_code + duration_ms + truncated # --------------------------------------------------------------------------- class TestRemovedJson: def test_has_exit_code(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "--json") data = json.loads(r.output) assert "exit_code" in data def test_exit_code_is_zero(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "--json") data = json.loads(r.output) assert data["exit_code"] == 0 def test_has_duration_ms(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "--json") data = json.loads(r.output) assert "duration_ms" in data def test_duration_ms_positive(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "--json") data = json.loads(r.output) assert isinstance(data["duration_ms"], float) assert data["duration_ms"] > 0 def test_has_truncated(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "--json") data = json.loads(r.output) assert "truncated" in data def test_truncated_false_without_limit(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "--json") data = json.loads(r.output) assert data["truncated"] is False # --------------------------------------------------------------------------- # 5. TypedDicts # --------------------------------------------------------------------------- class TestTypedDicts: def test_query_history_json_importable(self) -> None: from muse.cli.commands.query_history import _QueryHistoryJson assert _QueryHistoryJson is not None def test_query_history_json_has_exit_code(self) -> None: from muse.cli.commands.query_history import _QueryHistoryJson hints = typing.get_type_hints(_QueryHistoryJson) assert "exit_code" in hints def test_query_history_json_has_duration_ms(self) -> None: from muse.cli.commands.query_history import _QueryHistoryJson hints = typing.get_type_hints(_QueryHistoryJson) assert "duration_ms" in hints def test_query_history_json_has_schema_version(self) -> None: from muse.cli.commands.query_history import _QueryHistoryJson hints = typing.get_type_hints(_QueryHistoryJson) assert "schema" in hints def test_query_history_json_has_truncated(self) -> None: from muse.cli.commands.query_history import _QueryHistoryJson hints = typing.get_type_hints(_QueryHistoryJson) assert "truncated" in hints def test_query_history_json_has_results(self) -> None: from muse.cli.commands.query_history import _QueryHistoryJson hints = typing.get_type_hints(_QueryHistoryJson) assert "results" in hints def test_introduced_json_importable(self) -> None: from muse.cli.commands.query_history import _IntroducedJson assert _IntroducedJson is not None def test_introduced_json_has_exit_code(self) -> None: from muse.cli.commands.query_history import _IntroducedJson hints = typing.get_type_hints(_IntroducedJson) assert "exit_code" in hints def test_introduced_json_has_truncated(self) -> None: from muse.cli.commands.query_history import _IntroducedJson hints = typing.get_type_hints(_IntroducedJson) assert "truncated" in hints def test_removed_json_importable(self) -> None: from muse.cli.commands.query_history import _RemovedJson assert _RemovedJson is not None def test_removed_json_has_exit_code(self) -> None: from muse.cli.commands.query_history import _RemovedJson hints = typing.get_type_hints(_RemovedJson) assert "exit_code" in hints def test_removed_json_has_truncated(self) -> None: from muse.cli.commands.query_history import _RemovedJson hints = typing.get_type_hints(_RemovedJson) assert "truncated" in hints # --------------------------------------------------------------------------- # 6. Unit — _SymbolHistory # --------------------------------------------------------------------------- class TestUnitSymbolHistory: def test_initial_state(self) -> None: from muse.cli.commands.query_history import _SymbolHistory h = _SymbolHistory("billing.py::foo", "function", "Python") assert h.commit_count == 0 assert h.change_count == 0 assert h.address == "billing.py::foo" def test_record_increments_commit_count(self) -> None: from muse.cli.commands.query_history import _SymbolHistory h = _SymbolHistory("billing.py::foo", "function", "Python") h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1") assert h.commit_count == 1 def test_record_tracks_first_and_last(self) -> None: from muse.cli.commands.query_history import _SymbolHistory h = _SymbolHistory("billing.py::foo", "function", "Python") h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1") h.record("sha256:bbb", "2026-02-01T00:00:00+00:00", "cid2") assert h.first_commit_id == "sha256:aaa" assert h.last_commit_id == "sha256:bbb" def test_change_count_same_body(self) -> None: from muse.cli.commands.query_history import _SymbolHistory h = _SymbolHistory("billing.py::foo", "function", "Python") h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1") h.record("sha256:bbb", "2026-02-01T00:00:00+00:00", "cid1") # same cid assert h.change_count == 1 assert h.commit_count == 2 def test_change_count_different_bodies(self) -> None: from muse.cli.commands.query_history import _SymbolHistory h = _SymbolHistory("billing.py::foo", "function", "Python") h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1") h.record("sha256:bbb", "2026-02-01T00:00:00+00:00", "cid2") assert h.change_count == 2 def test_stable_true_when_one_version(self) -> None: from muse.cli.commands.query_history import _SymbolHistory h = _SymbolHistory("billing.py::foo", "function", "Python") h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1") d = h.to_dict() assert d["stable"] is True def test_stable_false_when_multiple_versions(self) -> None: from muse.cli.commands.query_history import _SymbolHistory h = _SymbolHistory("billing.py::foo", "function", "Python") h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1") h.record("sha256:bbb", "2026-02-01T00:00:00+00:00", "cid2") d = h.to_dict() assert d["stable"] is False def test_to_dict_schema(self) -> None: from muse.cli.commands.query_history import _SymbolHistory h = _SymbolHistory("billing.py::foo", "function", "Python") h.record("sha256:aaa", "2026-01-01T00:00:00+00:00", "cid1") d = h.to_dict() required = { "address", "kind", "language", "commit_count", "change_count", "first_commit_id", "first_committed_at", "last_commit_id", "last_committed_at", "stable", } assert required <= set(d.keys()) def test_first_committed_at_truncated_to_date(self) -> None: from muse.cli.commands.query_history import _SymbolHistory h = _SymbolHistory("billing.py::foo", "function", "Python") h.record("sha256:aaa", "2026-04-18T12:34:56+00:00", "cid1") d = h.to_dict() assert d["first_committed_at"] == "2026-04-18" # --------------------------------------------------------------------------- # 7. Unit — _sort_key_fn # --------------------------------------------------------------------------- class TestUnitSortKeyFn: def _make_history(self, address: str, commits: int, changes: int, first: str, last: str) -> "_SymbolHistory": from muse.cli.commands.query_history import _SymbolHistory h = _SymbolHistory(address, "function", "Python") # Simulate commit_count and change_count without real records h.commit_count = commits for i in range(changes): h.content_ids.add(f"cid{i}") h.first_committed_at = first h.last_committed_at = last h.first_commit_id = "sha256:aaa" h.last_commit_id = "sha256:bbb" return h def test_sort_by_address(self) -> None: from muse.cli.commands.query_history import _sort_key_fn fn = _sort_key_fn("address") h1 = self._make_history("z.py::foo", 1, 1, "2026-01-01", "2026-01-01") h2 = self._make_history("a.py::bar", 1, 1, "2026-01-01", "2026-01-01") assert fn(h2) < fn(h1) # type: ignore[arg-type] def test_sort_by_commits_descending(self) -> None: from muse.cli.commands.query_history import _sort_key_fn fn = _sort_key_fn("commits") h1 = self._make_history("a.py::foo", 10, 1, "2026-01-01", "2026-01-01") h2 = self._make_history("b.py::bar", 1, 1, "2026-01-01", "2026-01-01") assert fn(h1) < fn(h2) # more commits sorts first # type: ignore[arg-type] def test_sort_by_changes_descending(self) -> None: from muse.cli.commands.query_history import _sort_key_fn fn = _sort_key_fn("changes") h1 = self._make_history("a.py::foo", 1, 5, "2026-01-01", "2026-01-01") h2 = self._make_history("b.py::bar", 1, 1, "2026-01-01", "2026-01-01") assert fn(h1) < fn(h2) # more changes sorts first # type: ignore[arg-type] def test_sort_by_first(self) -> None: from muse.cli.commands.query_history import _sort_key_fn fn = _sort_key_fn("first") h1 = self._make_history("a.py::foo", 1, 1, "2026-01-01", "2026-06-01") h2 = self._make_history("b.py::bar", 1, 1, "2026-03-01", "2026-06-01") assert fn(h1) < fn(h2) # earlier first_committed_at sorts first # type: ignore[arg-type] def test_sort_by_last(self) -> None: from muse.cli.commands.query_history import _sort_key_fn fn = _sort_key_fn("last") h1 = self._make_history("a.py::foo", 1, 1, "2026-01-01", "2026-01-01") h2 = self._make_history("b.py::bar", 1, 1, "2026-01-01", "2026-06-01") assert fn(h1) < fn(h2) # earlier last_committed_at sorts first # type: ignore[arg-type] def test_unknown_sort_falls_back_to_address(self) -> None: from muse.cli.commands.query_history import _sort_key_fn fn = _sort_key_fn("zzz_unknown") h = self._make_history("billing.py::foo", 1, 1, "2026-01-01", "2026-01-01") assert fn(h) == ("billing.py::foo",) # type: ignore[arg-type] # --------------------------------------------------------------------------- # 8. Unit — _RemovedSymbol / _IntroducedSymbol # --------------------------------------------------------------------------- class TestUnitDiffSymbols: def _make_rec(self) -> Mapping[str, object]: return { "kind": "function", "name": "foo", "qualified_name": "foo", "lineno": 1, "end_lineno": 3, "content_id": "sha256:abc", "body_hash": "sha256:abc", "signature_id": "sha256:def", } def test_removed_to_dict_schema(self) -> None: from muse.cli.commands.query_history import _RemovedSymbol sym = _RemovedSymbol("billing.py::foo", self._make_rec(), "Python") # type: ignore[arg-type] d = sym.to_dict() assert d["address"] == "billing.py::foo" assert d["status"] == "removed" assert d["kind"] == "function" assert d["language"] == "Python" def test_introduced_to_dict_schema(self) -> None: from muse.cli.commands.query_history import _IntroducedSymbol sym = _IntroducedSymbol("billing.py::bar", self._make_rec(), "Python") # type: ignore[arg-type] d = sym.to_dict() assert d["address"] == "billing.py::bar" assert d["status"] == "introduced" assert d["kind"] == "function" assert d["language"] == "Python" # --------------------------------------------------------------------------- # 9. CLI filters — stable, --sort last, interaction tests # --------------------------------------------------------------------------- class TestCLIFilters: def test_validate_amount_is_stable(self, repo: pathlib.Path) -> None: # validate_amount never changed — should be stable=True r = _run(repo, "code", "query-history", "name=validate_amount", "--json") data = json.loads(r.output) assert data["results"] rec = data["results"][0] assert rec["stable"] is True assert rec["change_count"] == 1 def test_compute_total_is_not_stable(self, repo: pathlib.Path) -> None: # compute_total changed between commits — stable=False r = _run(repo, "code", "query-history", "name=compute_total", "--json") data = json.loads(r.output) assert data["results"] rec = data["results"][0] assert rec["stable"] is False assert rec["change_count"] >= 2 def test_sort_last(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--sort", "last", "--json") assert r.exit_code == 0, r.output data = json.loads(r.output) dates = [rec["last_committed_at"] for rec in data["results"]] assert dates == sorted(dates) def test_changed_only_plus_min_changes_interaction(self, repo: pathlib.Path) -> None: # --changed-only (>1) with --min-changes 2 should be consistent r = _run(repo, "code", "query-history", "kind=function", "--changed-only", "--min-changes", "2", "--json") assert r.exit_code == 0, r.output data = json.loads(r.output) for rec in data["results"]: assert rec["change_count"] >= 2 def test_introduced_finds_format_total(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "name=format_total", "--introduced-only", "--json") data = json.loads(r.output) assert any("format_total" in res["address"] for res in data["results"]) def test_removed_empty_when_nothing_removed(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--removed-only", "--json") data = json.loads(r.output) # validate_amount and compute_total both present in both commits — no removals assert data["exit_code"] == 0 removed_addrs = [res["address"] for res in data["results"]] assert not any("validate_amount" in a for a in removed_addrs) def test_limit_applied_to_introduced(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json", "--limit", "1") data = json.loads(r.output) assert len(data["results"]) <= 1 # --------------------------------------------------------------------------- # 10. Security # --------------------------------------------------------------------------- class TestCLISecurity: def test_null_byte_in_predicate_not_in_output(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "name=\x00malicious") assert "\x00" not in r.output def test_ansi_not_in_json_output(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--json") assert "\x1b" not in r.output def test_ansi_not_in_introduced_json(self, repo: pathlib.Path) -> None: r = _run(repo, "code", "query-history", "kind=function", "--introduced-only", "--json") assert "\x1b" not in r.output # --------------------------------------------------------------------------- # 11. Docstrings # --------------------------------------------------------------------------- class TestDocstrings: def test_run_docstring_exists(self) -> None: from muse.cli.commands.query_history import run assert run.__doc__ is not None assert len(run.__doc__) > 50 def test_run_docstring_mentions_json(self) -> None: from muse.cli.commands.query_history import run assert "json" in (run.__doc__ or "").lower() def test_register_docstring_exists(self) -> None: from muse.cli.commands.query_history import register assert register.__doc__ is not None assert len(register.__doc__) > 50 def test_symbol_history_docstring_exists(self) -> None: from muse.cli.commands.query_history import _SymbolHistory assert _SymbolHistory.__doc__ is not None def test_sort_key_fn_docstring_exists(self) -> None: from muse.cli.commands.query_history import _sort_key_fn assert _sort_key_fn.__doc__ is not None class TestRegisterFlags: def test_json_short_flag(self) -> None: import argparse from muse.cli.commands.query_history import register p = argparse.ArgumentParser() subs = p.add_subparsers() register(subs) args = p.parse_args(["query-history", "kind=function", "-j"]) assert args.json_out is True def test_json_long_flag(self) -> None: import argparse from muse.cli.commands.query_history import register p = argparse.ArgumentParser() subs = p.add_subparsers() register(subs) args = p.parse_args(["query-history", "kind=function", "--json"]) assert args.json_out is True def test_default_no_json(self) -> None: import argparse from muse.cli.commands.query_history import register p = argparse.ArgumentParser() subs = p.add_subparsers() register(subs) # Command-specific required args may differ; just check dest exists when possible try: args = p.parse_args(["query-history"]) assert args.json_out is False except SystemExit: pass # required positional args missing — flag default still correct