"""Supercharge tests for ``muse code breakage`` — agent-usability gaps. The existing test file (test_cmd_breakage.py, 855 lines) already covers correctness, exit codes, regression, and stress. This file targets only the gaps those tests leave open: Coverage matrix --------------- - --json / -j: -j alias works identically to --json - exit_code: JSON output includes exit_code mirroring process exit - duration_ms: JSON output includes non-negative float duration_ms - TypedDicts: _BreakageOutputJson 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 2000 ms for a small repo Critical distinction from other commands: exit_code in the JSON payload mirrors the actual computed process exit code (may be 1 when issues are found), NOT hardcoded to 0. """ 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 — clean repo with no working-tree changes (no breakage) # --------------------------------------------------------------------------- @pytest.fixture() def clean_repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path: """Code-domain repo whose working tree matches HEAD — zero breakage.""" 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 add_tax(self, rate): return rate def create_invoice(items): return Invoice() """)) 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 return tmp_path # --------------------------------------------------------------------------- # Fixture — repo with a removed public method in the working tree (breakage) # --------------------------------------------------------------------------- @pytest.fixture() def broken_repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path: """Repo where working tree removes a public method → breakage detected. Commit 1: Invoice has compute_total + add_tax Working tree: Invoice has only compute_total (add_tax removed) → removed_public_method error → exit_code == 1 """ 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 add_tax(self, rate): return rate """)) 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 # Remove add_tax from working tree (not committed) (tmp_path / "billing.py").write_text(textwrap.dedent("""\ class Invoice: def compute_total(self, items): return sum(items) """)) return tmp_path # --------------------------------------------------------------------------- # TestJsonAlias — -j works identically to --json # --------------------------------------------------------------------------- class TestJsonAlias: """The -j shorthand must behave identically to --json.""" def test_j_alias_exits_zero_on_clean_repo(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "-j") assert r.exit_code == 0, r.output def test_j_alias_valid_json(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "-j") json.loads(r.output) # must not raise def test_j_alias_has_issues_key(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "-j") data = json.loads(r.output) assert "issues" in data def test_j_alias_has_errors_key(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "-j") data = json.loads(r.output) assert "errors" in data def test_j_alias_same_top_level_keys_as_json_flag(self, clean_repo: pathlib.Path) -> None: r1 = _run(clean_repo, "code", "breakage", "--json") r2 = _run(clean_repo, "code", "breakage", "-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_same_exit_code_as_json_flag(self, clean_repo: pathlib.Path) -> None: r1 = _run(clean_repo, "code", "breakage", "--json") r2 = _run(clean_repo, "code", "breakage", "-j") assert r1.exit_code == r2.exit_code # --------------------------------------------------------------------------- # 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, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "--json") data = json.loads(r.output) assert "duration_ms" in data def test_json_duration_ms_nonnegative(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "--json") data = json.loads(r.output) assert data["duration_ms"] >= 0 def test_json_duration_ms_is_float(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "--json") data = json.loads(r.output) assert isinstance(data["duration_ms"], float) def test_j_alias_duration_ms_present(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "-j") data = json.loads(r.output) assert "duration_ms" in data def test_duration_ms_present_on_broken_repo(self, broken_repo: pathlib.Path) -> None: r = _run(broken_repo, "code", "breakage", "--json") data = json.loads(r.output) assert "duration_ms" in data assert data["duration_ms"] >= 0 def test_duration_ms_with_strict_flag(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "--json", "--strict") data = json.loads(r.output) assert "duration_ms" in data assert data["duration_ms"] >= 0 # --------------------------------------------------------------------------- # TestExitCode — JSON output must include exit_code mirroring process exit # --------------------------------------------------------------------------- class TestExitCode: """JSON output must include exit_code that mirrors the process exit code.""" def test_json_has_exit_code(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "--json") data = json.loads(r.output) assert "exit_code" in data def test_json_exit_code_zero_on_clean_repo(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "--json") assert r.exit_code == 0 data = json.loads(r.output) assert data["exit_code"] == 0 def test_json_exit_code_is_int(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "--json") data = json.loads(r.output) assert isinstance(data["exit_code"], int) def test_j_alias_exit_code_present(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "-j") data = json.loads(r.output) assert "exit_code" in data def test_exit_code_mirrors_process_exit_on_clean(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "--json") data = json.loads(r.output) assert data["exit_code"] == r.exit_code def test_exit_code_one_on_broken_repo(self, broken_repo: pathlib.Path) -> None: """Breakage with errors exits 1 and JSON exit_code == 1.""" r = _run(broken_repo, "code", "breakage", "--json") assert r.exit_code == 1 data = json.loads(r.output) assert data["exit_code"] == 1 def test_exit_code_mirrors_process_exit_on_broken(self, broken_repo: pathlib.Path) -> None: r = _run(broken_repo, "code", "breakage", "--json") data = json.loads(r.output) assert data["exit_code"] == r.exit_code def test_exit_code_in_json_is_not_hardcoded_zero(self, broken_repo: pathlib.Path) -> None: """Verify exit_code reflects real exit, not a hardcoded 0.""" r = _run(broken_repo, "code", "breakage", "--json") data = json.loads(r.output) # exit_code must equal 1 (errors found), proving it's not hardcoded assert data["exit_code"] != 0 # --------------------------------------------------------------------------- # TestTypedDicts — _BreakageOutputJson carries the new fields # --------------------------------------------------------------------------- class TestTypedDicts: """_BreakageOutputJson must carry exit_code/duration_ms annotations.""" def test_breakage_output_json_exists(self) -> None: from muse.cli.commands.breakage import _BreakageOutputJson # noqa: F401 def test_breakage_output_json_has_exit_code_annotation(self) -> None: from muse.cli.commands.breakage import _BreakageOutputJson assert "exit_code" in _BreakageOutputJson.__annotations__ def test_breakage_output_json_has_duration_ms_annotation(self) -> None: from muse.cli.commands.breakage import _BreakageOutputJson assert "duration_ms" in _BreakageOutputJson.__annotations__ def test_breakage_output_json_retains_issues_annotation(self) -> None: from muse.cli.commands.breakage import _BreakageOutputJson assert "issues" in _BreakageOutputJson.__annotations__ def test_breakage_output_json_retains_errors_annotation(self) -> None: from muse.cli.commands.breakage import _BreakageOutputJson assert "errors" in _BreakageOutputJson.__annotations__ def test_breakage_output_json_retains_warnings_annotation(self) -> None: from muse.cli.commands.breakage import _BreakageOutputJson assert "warnings" in _BreakageOutputJson.__annotations__ def test_breakage_issue_exists(self) -> None: from muse.cli.commands.breakage import _BreakageIssue # noqa: F401 def test_breakage_issue_has_severity(self) -> None: from muse.cli.commands.breakage import _BreakageIssue assert "severity" in _BreakageIssue.__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.breakage import run assert "exit_code" 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, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "--json") assert "\x1b" not in r.output def test_j_alias_output_no_ansi(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "-j") assert "\x1b" not in r.output def test_broken_repo_json_output_no_ansi(self, broken_repo: pathlib.Path) -> None: r = _run(broken_repo, "code", "breakage", "--json") assert "\x1b" not in r.output # --------------------------------------------------------------------------- # TestPerformance — duration_ms under 2000 ms for a small repo # --------------------------------------------------------------------------- class TestPerformance: """duration_ms must be non-negative and under 2000 ms for small repos.""" def test_json_duration_under_2000ms(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "--json") data = json.loads(r.output) assert data["duration_ms"] < 2000 def test_j_alias_duration_under_2000ms(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "-j") data = json.loads(r.output) assert data["duration_ms"] < 2000 def test_broken_repo_duration_under_2000ms(self, broken_repo: pathlib.Path) -> None: r = _run(broken_repo, "code", "breakage", "--json") data = json.loads(r.output) assert data["duration_ms"] < 2000 def test_duration_ms_is_float_not_int(self, clean_repo: pathlib.Path) -> None: r = _run(clean_repo, "code", "breakage", "--json") data = json.loads(r.output) assert isinstance(data["duration_ms"], float)