"""Supercharge tests for ``muse code blame`` — agent-usability gaps. The three existing test files (test_cmd_blame.py, test_cmd_blame_hardening.py, test_core_blame.py) already cover correctness, security, rename tracking, kind/ author filters, E2E, and stress. This file targets only the gaps those files leave open: Coverage matrix --------------- - --json / -j: -j alias works identically to --json - exit_code: JSON output includes exit_code = 0 on success - duration_ms: JSON output includes non-negative float duration_ms - TypedDicts: _BlameResultJson gains exit_code/duration_ms annotations - Docstrings: run() docstring mentions exit_code and duration_ms - ANSI: JSON output never contains terminal escape sequences - Performance: duration_ms stays under 1000 ms for a small repo """ from __future__ import annotations from collections.abc import Mapping import json import pathlib import textwrap import pytest from tests.cli_test_helper import CliRunner runner = CliRunner() # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def _env(root: pathlib.Path) -> Mapping[str, str]: return {"MUSE_REPO_ROOT": str(root)} def _run(root: pathlib.Path, *args: str) -> "InvokeResult": return runner.invoke(None, list(args), env=_env(root)) # --------------------------------------------------------------------------- # Fixture — repo with a committed symbol so blame returns real events # --------------------------------------------------------------------------- @pytest.fixture() def blame_repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path: """Code-domain repo with two commits containing structured deltas. Commit 1: create billing.py with Invoice.compute_total + process_order Commit 2: modify compute_total body → blame sees a 'modified' event """ monkeypatch.chdir(tmp_path) r = _run(tmp_path, "init", "--domain", "code") assert r.exit_code == 0, r.output (tmp_path / "billing.py").write_text(textwrap.dedent("""\ class Invoice: def compute_total(self, items): return sum(items) def process_order(invoice, items): return invoice.compute_total(items) """)) r1 = _run(tmp_path, "code", "add", "billing.py") assert r1.exit_code == 0, r1.output r2 = _run(tmp_path, "commit", "-m", "initial billing") assert r2.exit_code == 0, r2.output (tmp_path / "billing.py").write_text(textwrap.dedent("""\ class Invoice: def compute_total(self, items): return round(sum(items), 2) def process_order(invoice, items): return invoice.compute_total(items) """)) r3 = _run(tmp_path, "code", "add", "billing.py") assert r3.exit_code == 0, r3.output r4 = _run(tmp_path, "commit", "-m", "round result") assert r4.exit_code == 0, r4.output return tmp_path def _blame_address(root: pathlib.Path) -> str: """Return a symbol address that blame can find in blame_repo.""" return "billing.py::Invoice.compute_total" # --------------------------------------------------------------------------- # TestJsonAlias — -j works identically to --json # --------------------------------------------------------------------------- class TestJsonAlias: """The -j shorthand must behave identically to --json.""" def test_j_alias_exits_zero(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "-j") assert r.exit_code == 0, r.output def test_j_alias_valid_json(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "-j") json.loads(r.output) # must not raise def test_j_alias_has_events_key(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "-j") data = json.loads(r.output) assert "events" in data def test_j_alias_has_address_key(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "-j") data = json.loads(r.output) assert data["address"] == addr def test_j_alias_same_top_level_keys_as_json_flag(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r1 = _run(blame_repo, "code", "blame", addr, "--json") r2 = _run(blame_repo, "code", "blame", addr, "-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_address_matches(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r1 = _run(blame_repo, "code", "blame", addr, "--json") r2 = _run(blame_repo, "code", "blame", addr, "-j") assert json.loads(r1.output)["address"] == json.loads(r2.output)["address"] # --------------------------------------------------------------------------- # TestDurationMs — JSON output must include duration_ms # --------------------------------------------------------------------------- class TestDurationMs: """JSON output must include a non-negative float duration_ms.""" def test_json_has_duration_ms(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "--json") data = json.loads(r.output) assert "duration_ms" in data def test_json_duration_ms_nonnegative(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "--json") data = json.loads(r.output) assert data["duration_ms"] >= 0 def test_json_duration_ms_is_float(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "--json") data = json.loads(r.output) assert isinstance(data["duration_ms"], float) def test_j_alias_duration_ms_present(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "-j") data = json.loads(r.output) assert "duration_ms" in data def test_duration_ms_with_kind_filter(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "--json", "--kind", "created") data = json.loads(r.output) assert "duration_ms" in data assert data["duration_ms"] >= 0 def test_duration_ms_with_all_flag(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "--json", "--all") data = json.loads(r.output) assert "duration_ms" in data assert data["duration_ms"] >= 0 # --------------------------------------------------------------------------- # TestExitCode — JSON output must include exit_code # --------------------------------------------------------------------------- class TestExitCode: """JSON output must include exit_code = 0 on success.""" def test_json_has_exit_code(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "--json") data = json.loads(r.output) assert "exit_code" in data def test_json_exit_code_zero_on_success(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "--json") assert r.exit_code == 0 data = json.loads(r.output) assert data["exit_code"] == 0 def test_json_exit_code_is_int(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "--json") data = json.loads(r.output) assert isinstance(data["exit_code"], int) def test_j_alias_exit_code_present(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "-j") data = json.loads(r.output) assert "exit_code" in data def test_exit_code_mirrors_process_exit(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "--json") data = json.loads(r.output) assert data["exit_code"] == r.exit_code def test_exit_code_zero_with_no_events_found(self, blame_repo: pathlib.Path) -> None: """Blame on a symbol with no recorded history exits 0 with empty events.""" r = _run(blame_repo, "code", "blame", "billing.py::nonexistent_fn", "--json") assert r.exit_code == 0 data = json.loads(r.output) assert data["exit_code"] == 0 assert data["events"] == [] # --------------------------------------------------------------------------- # TestTypedDicts — _BlameResultJson carries the new fields # --------------------------------------------------------------------------- class TestTypedDicts: """_BlameResultJson must gain exit_code/duration_ms annotations.""" def test_blame_result_json_exists(self) -> None: from muse.cli.commands.blame import _BlameResultJson # noqa: F401 def test_blame_result_json_has_exit_code_annotation(self) -> None: from muse.cli.commands.blame import _BlameResultJson assert "exit_code" in _BlameResultJson.__annotations__ def test_blame_result_json_has_duration_ms_annotation(self) -> None: from muse.cli.commands.blame import _BlameResultJson assert "duration_ms" in _BlameResultJson.__annotations__ def test_blame_result_json_retains_events_annotation(self) -> None: from muse.cli.commands.blame import _BlameResultJson assert "events" in _BlameResultJson.__annotations__ def test_blame_result_json_retains_address_annotation(self) -> None: from muse.cli.commands.blame import _BlameResultJson assert "address" in _BlameResultJson.__annotations__ def test_blame_event_json_exists(self) -> None: from muse.cli.commands.blame import _BlameEventJson # noqa: F401 def test_blame_event_json_has_commit_id(self) -> None: from muse.cli.commands.blame import _BlameEventJson assert "commit_id" in _BlameEventJson.__annotations__ # --------------------------------------------------------------------------- # TestDocstrings — run() docstring documents new fields # --------------------------------------------------------------------------- class TestDocstrings: """run() must document exit_code.""" def test_run_docstring_documents_fields(self) -> None: from muse.cli.commands.blame import run assert "Exit codes" in run.__doc__ # --------------------------------------------------------------------------- # TestAnsiSanitization — no escape codes in JSON output # --------------------------------------------------------------------------- class TestAnsiSanitization: """No ANSI escape sequences anywhere in the JSON output.""" def test_json_output_no_ansi(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "--json") assert "\x1b" not in r.output def test_j_alias_output_no_ansi(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "-j") assert "\x1b" not in r.output def test_json_address_field_no_ansi(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "--json") data = json.loads(r.output) assert "\x1b" not in data["address"] # --------------------------------------------------------------------------- # TestPerformance — duration_ms under 1000 ms for a small repo # --------------------------------------------------------------------------- class TestPerformance: """duration_ms must be non-negative and under 1000 ms for small repos.""" def test_json_duration_under_1000ms(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "--json") data = json.loads(r.output) assert data["duration_ms"] < 1000 def test_j_alias_duration_under_1000ms(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "-j") data = json.loads(r.output) assert data["duration_ms"] < 1000 def test_duration_ms_is_float_not_int(self, blame_repo: pathlib.Path) -> None: addr = _blame_address(blame_repo) r = _run(blame_repo, "code", "blame", addr, "--json") data = json.loads(r.output) assert isinstance(data["duration_ms"], float)