"""Supercharge tests for ``muse code contract`` — agent-usability gaps. The existing TestContract suite in test_code_commands.py covers correctness, JSON schema, all field keys, parameter schema, history schema, stability values, arg observations, and error paths. 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 = 0 on success - duration_ms: JSON output includes non-negative float duration_ms - TypedDicts: _ContractJson 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 """ from __future__ import annotations from collections.abc import Mapping import json import os import pathlib import textwrap import pytest from tests.cli_test_helper import CliRunner, InvokeResult runner = CliRunner() _ADDR = "billing.py::compute_total" # --------------------------------------------------------------------------- # 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 — multi-commit repo with a real call graph + test assertions # --------------------------------------------------------------------------- @pytest.fixture() def contract_repo( tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch ) -> pathlib.Path: """Repo exercising every dimension of ``muse code contract``. Layout:: billing.py — compute_total(items, currency="USD") → float services.py — place_order() calls compute_total → stored audit.py — run_audit() calls compute_total → discarded tests/test_billing.py — test functions with assertions Commit history:: 1. readme.txt seed commit 2. billing.py added — compute_total created 3. callers + tests added 4. billing.py body rewrite (PATCH) 5. billing.py currency param added (MINOR) """ monkeypatch.chdir(tmp_path) r = _run(tmp_path, "init", "--domain", "code") assert r.exit_code == 0, r.output # commit 1 — seed (tmp_path / "readme.txt").write_text("# contract test repo\n") r = _run(tmp_path, "code", "add", ".") assert r.exit_code == 0, r.output r = _run(tmp_path, "commit", "-m", "seed: initial readme") assert r.exit_code == 0, r.output # commit 2 — add compute_total (tmp_path / "billing.py").write_text(textwrap.dedent("""\ def compute_total(items): return sum(i["price"] for i in items) """)) r = _run(tmp_path, "code", "add", ".") assert r.exit_code == 0, r.output r = _run(tmp_path, "commit", "-m", "feat: add compute_total") assert r.exit_code == 0, r.output # commit 3 — callers + tests os.makedirs(tmp_path / "tests", exist_ok=True) (tmp_path / "services.py").write_text(textwrap.dedent("""\ from billing import compute_total def place_order(items): total = compute_total(items, currency="EUR") return total """)) (tmp_path / "audit.py").write_text(textwrap.dedent("""\ from billing import compute_total def run_audit(items): compute_total(items) """)) (tmp_path / "tests" / "test_billing.py").write_text(textwrap.dedent("""\ from billing import compute_total def test_compute_total_basic(): result = compute_total([{"price": 10}, {"price": 5}]) assert result == 15 assert result > 0 assert isinstance(result, (int, float)) def test_compute_total_empty(): result = compute_total([]) assert result == 0 """)) r = _run(tmp_path, "code", "add", ".") assert r.exit_code == 0, r.output r = _run(tmp_path, "commit", "-m", "feat: add callers and tests") assert r.exit_code == 0, r.output # commit 4 — body rewrite (PATCH) (tmp_path / "billing.py").write_text(textwrap.dedent("""\ def compute_total(items): total = 0.0 for item in items: total += float(item["price"]) return total """)) r = _run(tmp_path, "code", "add", ".") assert r.exit_code == 0, r.output r = _run(tmp_path, "commit", "-m", "perf: vectorise compute_total") assert r.exit_code == 0, r.output # commit 5 — add currency param (MINOR) (tmp_path / "billing.py").write_text(textwrap.dedent("""\ def compute_total(items, currency="USD"): total = 0.0 for item in items: total += float(item["price"]) return total """)) r = _run(tmp_path, "code", "add", ".") assert r.exit_code == 0, r.output r = _run(tmp_path, "commit", "-m", "feat: add optional currency param") assert r.exit_code == 0, r.output return tmp_path # --------------------------------------------------------------------------- # TestJsonAlias — -j works identically to --json # --------------------------------------------------------------------------- class TestJsonAlias: """-j shorthand must behave identically to --json.""" def test_j_alias_exits_zero(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "-j") assert r.exit_code == 0, r.output def test_j_alias_valid_json(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "-j") json.loads(r.output) # must not raise def test_j_alias_has_address_key(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "-j") assert "address" in json.loads(r.output) def test_j_alias_has_ops_key(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "-j") data = json.loads(r.output) assert "stability" in data def test_j_alias_same_top_level_keys_as_json_flag( self, contract_repo: pathlib.Path ) -> None: r1 = _run(contract_repo, "code", "contract", _ADDR, "--json") r2 = _run(contract_repo, "code", "contract", _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, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "-j") assert json.loads(r.output)["address"] == _ADDR def test_j_alias_has_history_key(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "-j") assert "history" in json.loads(r.output) # --------------------------------------------------------------------------- # 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, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "--json") assert "duration_ms" in json.loads(r.output) def test_json_duration_ms_nonnegative(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "--json") assert json.loads(r.output)["duration_ms"] >= 0 def test_json_duration_ms_is_float(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "--json") assert isinstance(json.loads(r.output)["duration_ms"], float) def test_j_alias_duration_ms_present(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "-j") assert "duration_ms" in json.loads(r.output) def test_duration_ms_with_max_commits_1(self, contract_repo: pathlib.Path) -> None: """duration_ms present even with --max-commits 1.""" r = _run(contract_repo, "code", "contract", _ADDR, "--json", "--max-commits", "1") data = json.loads(r.output) assert "duration_ms" in data assert data["duration_ms"] >= 0 def test_duration_ms_not_zero_for_real_work(self, contract_repo: pathlib.Path) -> None: """Non-trivial analysis should take measurable time.""" r = _run(contract_repo, "code", "contract", _ADDR, "--json") # Allow 0.0 only in very fast CI — just confirm type and sign assert isinstance(json.loads(r.output)["duration_ms"], float) # --------------------------------------------------------------------------- # TestExitCode — JSON includes exit_code = 0 on success # --------------------------------------------------------------------------- class TestExitCode: """JSON exit_code must be 0 on success.""" def test_json_has_exit_code(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "--json") assert "exit_code" in json.loads(r.output) def test_json_exit_code_zero(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "--json") assert r.exit_code == 0 assert json.loads(r.output)["exit_code"] == 0 def test_json_exit_code_is_int(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "--json") assert isinstance(json.loads(r.output)["exit_code"], int) def test_j_alias_exit_code_present(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "-j") assert "exit_code" in json.loads(r.output) def test_exit_code_mirrors_process_exit(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "--json") assert json.loads(r.output)["exit_code"] == r.exit_code def test_exit_code_zero_with_max_commits(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "--json", "--max-commits", "3") assert r.exit_code == 0 assert json.loads(r.output)["exit_code"] == 0 def test_exit_code_not_present_in_error_path( self, contract_repo: pathlib.Path ) -> None: """Error paths raise SystemExit before JSON emits — no JSON to check.""" r = _run(contract_repo, "code", "contract", "billing.py::nonexistent", "--json") assert r.exit_code != 0 # --------------------------------------------------------------------------- # TestTypedDicts — _ContractJson carries the new fields # --------------------------------------------------------------------------- class TestTypedDicts: """_ContractJson must carry exit_code and duration_ms annotations.""" def test_contract_json_typeddict_exists(self) -> None: from muse.cli.commands.contract import _ContractJson # noqa: F401 def test_has_exit_code_annotation(self) -> None: from muse.cli.commands.contract import _ContractJson assert "exit_code" in _ContractJson.__annotations__ def test_has_duration_ms_annotation(self) -> None: from muse.cli.commands.contract import _ContractJson assert "duration_ms" in _ContractJson.__annotations__ def test_retains_address_annotation(self) -> None: from muse.cli.commands.contract import _ContractJson assert "address" in _ContractJson.__annotations__ def test_retains_history_annotation(self) -> None: from muse.cli.commands.contract import _ContractJson assert "history" in _ContractJson.__annotations__ def test_retains_stability_annotation(self) -> None: from muse.cli.commands.contract import _ContractJson assert "stability" in _ContractJson.__annotations__ def test_retains_warnings_annotation(self) -> None: from muse.cli.commands.contract import _ContractJson assert "warnings" in _ContractJson.__annotations__ # --------------------------------------------------------------------------- # 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, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "--json") assert "\x1b" not in r.output def test_j_alias_output_no_ansi(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "-j") assert "\x1b" not in r.output def test_json_output_no_ansi_with_max_commits( self, contract_repo: pathlib.Path ) -> None: r = _run( contract_repo, "code", "contract", _ADDR, "--json", "--max-commits", "2", ) assert "\x1b" not in r.output # --------------------------------------------------------------------------- # TestPerformance — duration_ms under 2000 ms for a small repo # --------------------------------------------------------------------------- class TestPerformance: """duration_ms must stay under 2000 ms for small repos.""" def test_json_duration_under_2000ms(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "--json") assert json.loads(r.output)["duration_ms"] < 2000 def test_j_alias_duration_under_2000ms(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "-j") assert json.loads(r.output)["duration_ms"] < 2000 def test_duration_ms_is_float_not_int(self, contract_repo: pathlib.Path) -> None: r = _run(contract_repo, "code", "contract", _ADDR, "--json") assert isinstance(json.loads(r.output)["duration_ms"], float) # --------------------------------------------------------------------------- # TestRegisterFlags # --------------------------------------------------------------------------- import argparse as _argparse class TestRegisterFlags: """register() wires --json / -j correctly.""" def _parse(self, *args: str) -> _argparse.Namespace: from muse.cli.commands.contract import register p = _argparse.ArgumentParser() sub = p.add_subparsers() register(sub) return p.parse_args(["contract", *args]) def test_default_json_out_is_false(self) -> None: ns = self._parse("src/billing.py::compute_total") assert ns.json_out is False def test_json_flag_sets_json_out(self) -> None: ns = self._parse("--json", "src/billing.py::compute_total") assert ns.json_out is True def test_j_shorthand_sets_json_out(self) -> None: ns = self._parse("-j", "src/billing.py::compute_total") assert ns.json_out is True