"""Supercharge tests for ``muse code codemap`` — agent-usability gaps. The existing test_cmd_codemap.py covers import graph correctness, cycle detection, all flags (--top, --min-importers, --language, --commit), JSON schema, stress/performance, and agent-safe zones. 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: _CodemapOutputJson in production code gains exit_code/duration_ms - 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 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)) def _stage_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 # --------------------------------------------------------------------------- @pytest.fixture() def codemap_repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path: """Code-domain repo with a small import graph committed. alpha.py is the hub — beta.py and gamma.py both import it. delta.py is isolated (agent-safe zone). """ monkeypatch.chdir(tmp_path) r = _run(tmp_path, "init", "--domain", "code") assert r.exit_code == 0, r.output (tmp_path / "alpha.py").write_text(textwrap.dedent("""\ def alpha_fn(): return 1 """)) (tmp_path / "beta.py").write_text(textwrap.dedent("""\ import alpha def beta_fn(): return alpha.alpha_fn() + 1 """)) (tmp_path / "gamma.py").write_text(textwrap.dedent("""\ import alpha def gamma_fn(): return alpha.alpha_fn() + 2 """)) (tmp_path / "delta.py").write_text(textwrap.dedent("""\ def delta_fn(): return 42 """)) _stage_commit(tmp_path, "add modules") 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, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "-j") assert r.exit_code == 0, r.output def test_j_alias_valid_json(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "-j") json.loads(r.output) # must not raise def test_j_alias_has_modules_key(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "-j") data = json.loads(r.output) assert "modules" in data def test_j_alias_has_commit_key(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "-j") data = json.loads(r.output) assert "commit" in data def test_j_alias_same_top_level_keys_as_json_flag(self, codemap_repo: pathlib.Path) -> None: r1 = _run(codemap_repo, "code", "codemap", "--json") r2 = _run(codemap_repo, "code", "codemap", "-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_module_count_matches_json_flag(self, codemap_repo: pathlib.Path) -> None: r1 = _run(codemap_repo, "code", "codemap", "--json") r2 = _run(codemap_repo, "code", "codemap", "-j") d1 = json.loads(r1.output) d2 = json.loads(r2.output) assert len(d1["modules"]) == len(d2["modules"]) def test_j_alias_with_language_flag(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "-j", "--language", "Python") assert r.exit_code == 0, r.output data = json.loads(r.output) assert data["language_filter"] == "Python" def test_j_alias_with_top_flag(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "-j", "--top", "2") assert r.exit_code == 0, r.output data = json.loads(r.output) assert len(data["modules"]) <= 2 # --------------------------------------------------------------------------- # 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, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json") data = json.loads(r.output) assert "duration_ms" in data def test_json_duration_ms_nonnegative(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json") data = json.loads(r.output) assert data["duration_ms"] >= 0 def test_json_duration_ms_is_float(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json") data = json.loads(r.output) assert isinstance(data["duration_ms"], float) def test_j_alias_duration_ms_present(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "-j") data = json.loads(r.output) assert "duration_ms" in data def test_duration_ms_with_language_filter(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json", "--language", "Python") data = json.loads(r.output) assert "duration_ms" in data assert data["duration_ms"] >= 0 def test_duration_ms_with_top_filter(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json", "--top", "5") data = json.loads(r.output) assert "duration_ms" in data assert data["duration_ms"] >= 0 # --------------------------------------------------------------------------- # 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, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json") data = json.loads(r.output) assert "exit_code" in data def test_json_exit_code_zero_on_success(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json") assert r.exit_code == 0 data = json.loads(r.output) assert data["exit_code"] == 0 def test_json_exit_code_is_int(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json") data = json.loads(r.output) assert isinstance(data["exit_code"], int) def test_j_alias_exit_code_present(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "-j") data = json.loads(r.output) assert "exit_code" in data def test_exit_code_mirrors_process_exit(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json") data = json.loads(r.output) assert data["exit_code"] == r.exit_code def test_exit_code_zero_with_min_importers(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json", "--min-importers", "1") assert r.exit_code == 0 data = json.loads(r.output) assert data["exit_code"] == 0 def test_exit_code_zero_with_language_filter(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json", "--language", "Python") assert r.exit_code == 0 data = json.loads(r.output) assert data["exit_code"] == 0 # --------------------------------------------------------------------------- # TestTypedDicts — _CodemapOutputJson in production code carries new fields # --------------------------------------------------------------------------- class TestTypedDicts: """_CodemapOutputJson must carry exit_code and duration_ms annotations.""" def test_codemap_output_json_exists(self) -> None: from muse.cli.commands.codemap import _CodemapOutputJson # noqa: F401 def test_has_exit_code_annotation(self) -> None: from muse.cli.commands.codemap import _CodemapOutputJson assert "exit_code" in _CodemapOutputJson.__annotations__ def test_has_duration_ms_annotation(self) -> None: from muse.cli.commands.codemap import _CodemapOutputJson assert "duration_ms" in _CodemapOutputJson.__annotations__ def test_retains_modules_annotation(self) -> None: from muse.cli.commands.codemap import _CodemapOutputJson assert "modules" in _CodemapOutputJson.__annotations__ def test_retains_commit_annotation(self) -> None: from muse.cli.commands.codemap import _CodemapOutputJson assert "commit" in _CodemapOutputJson.__annotations__ def test_retains_import_cycles_annotation(self) -> None: from muse.cli.commands.codemap import _CodemapOutputJson assert "import_cycles" in _CodemapOutputJson.__annotations__ def test_retains_high_centrality_annotation(self) -> None: from muse.cli.commands.codemap import _CodemapOutputJson assert "high_centrality" in _CodemapOutputJson.__annotations__ def test_retains_boundary_files_annotation(self) -> None: from muse.cli.commands.codemap import _CodemapOutputJson assert "boundary_files" in _CodemapOutputJson.__annotations__ def test_retains_agent_safe_zones_annotation(self) -> None: from muse.cli.commands.codemap import _CodemapOutputJson assert "agent_safe_zones" in _CodemapOutputJson.__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.codemap 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, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json") assert "\x1b" not in r.output def test_j_alias_output_no_ansi(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "-j") assert "\x1b" not in r.output def test_json_no_ansi_with_language_filter(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json", "--language", "Python") 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, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json") data = json.loads(r.output) assert data["duration_ms"] < 2000 def test_j_alias_duration_under_2000ms(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "-j") data = json.loads(r.output) assert data["duration_ms"] < 2000 def test_duration_ms_is_float_not_int(self, codemap_repo: pathlib.Path) -> None: r = _run(codemap_repo, "code", "codemap", "--json") data = json.loads(r.output) assert isinstance(data["duration_ms"], float)