"""Supercharge tests for ``muse code deps`` — agent-usability gaps. The existing TestDeps suite in test_code_commands.py covers correctness, JSON schemas, all flags (--reverse, --count, --filter, --depth, --transitive), file mode and symbol mode, and security (path traversal, empty file rel). This file targets only the gaps those tests leave open: Coverage matrix --------------- - --json / -j: -j alias works identically to --json (all 6 JSON paths) - exit_code: JSON output includes exit_code = 0 on success (all 6 paths) - duration_ms: JSON output includes non-negative float duration_ms (all 6 paths) - TypedDicts: _DepsFileJson and _DepsSymbolJson carry 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 Six JSON paths exercised ------------------------ 1. File mode, forward: --json billing.py → {path, imports, ...} 2. File mode, reverse: --reverse --json billing.py → {path, imported_by, ...} 3. Symbol mode, forward depth=1 --json billing.py::func → {address, depth, calls, ...} 4. Symbol mode, forward multi: --transitive --json → {address, by_depth, ...} 5. Symbol mode, reverse depth=1 --reverse --json symbol → {address, called_by, ...} 6. Symbol mode, reverse multi: --reverse --transitive → {address, by_depth, ...} """ from __future__ import annotations from collections.abc import Mapping import argparse import json import pathlib import textwrap import pytest from tests.cli_test_helper import CliRunner, InvokeResult runner = CliRunner() _SYMBOL = "billing.py::process_order" # --------------------------------------------------------------------------- # 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 import graph and call graph # --------------------------------------------------------------------------- @pytest.fixture() def deps_repo( tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch ) -> pathlib.Path: """Repo with an import graph and a call graph for deps analysis. Layout:: models.py — Invoice class with compute_total method utils.py — validate() function billing.py — imports models + utils; process_order calls both api.py — imports billing; handle_request calls process_order Import graph: billing.py → models, utils api.py → billing Call graph (process_order): process_order → validate, compute_total handle_request → process_order """ monkeypatch.chdir(tmp_path) r = _run(tmp_path, "init", "--domain", "code") assert r.exit_code == 0, r.output (tmp_path / "models.py").write_text(textwrap.dedent("""\ class Invoice: def compute_total(self, items): return sum(items) """)) (tmp_path / "utils.py").write_text(textwrap.dedent("""\ def validate(amount): return amount > 0 """)) (tmp_path / "billing.py").write_text(textwrap.dedent("""\ from models import Invoice from utils import validate def process_order(items): if not validate(sum(items)): raise ValueError("invalid amount") inv = Invoice() return inv.compute_total(items) """)) (tmp_path / "api.py").write_text(textwrap.dedent("""\ from billing import process_order def handle_request(items): return process_order(items) """)) r = _run(tmp_path, "code", "add", ".") assert r.exit_code == 0, r.output r = _run(tmp_path, "commit", "-m", "initial") assert r.exit_code == 0, r.output return tmp_path # --------------------------------------------------------------------------- # TestJsonAlias — -j works identically to --json (all major paths) # --------------------------------------------------------------------------- class TestJsonAlias: """-j shorthand must behave identically to --json on every JSON path.""" def test_j_alias_exits_zero_file_forward(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "-j", "billing.py") assert r.exit_code == 0, r.output def test_j_alias_valid_json_file_forward(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "-j", "billing.py") json.loads(r.output) # must not raise def test_j_alias_has_imports_key(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "-j", "billing.py") assert "imports" in json.loads(r.output) def test_j_alias_exits_zero_file_reverse(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "-j", "--reverse", "billing.py") assert r.exit_code == 0, r.output def test_j_alias_has_imported_by_key(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "-j", "--reverse", "billing.py") assert "imported_by" in json.loads(r.output) def test_j_alias_exits_zero_symbol_forward(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "-j", _SYMBOL) assert r.exit_code == 0, r.output def test_j_alias_has_calls_key(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "-j", _SYMBOL) assert "calls" in json.loads(r.output) def test_j_alias_exits_zero_symbol_transitive(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "-j", "--transitive", _SYMBOL) assert r.exit_code == 0, r.output def test_j_alias_has_by_depth_key(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "-j", "--transitive", _SYMBOL) assert "by_depth" in json.loads(r.output) def test_j_alias_same_top_level_keys_file_forward( self, deps_repo: pathlib.Path ) -> None: r1 = _run(deps_repo, "code", "deps", "--json", "billing.py") r2 = _run(deps_repo, "code", "deps", "-j", "billing.py") 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_imports_list_file_forward( self, deps_repo: pathlib.Path ) -> None: r1 = _run(deps_repo, "code", "deps", "--json", "billing.py") r2 = _run(deps_repo, "code", "deps", "-j", "billing.py") assert json.loads(r1.output)["imports"] == json.loads(r2.output)["imports"] # --------------------------------------------------------------------------- # TestDurationMs — all 6 JSON paths must include duration_ms # --------------------------------------------------------------------------- class TestDurationMs: """Every JSON output path must include a non-negative float duration_ms.""" def test_duration_ms_file_forward(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", "billing.py") data = json.loads(r.output) assert "duration_ms" in data assert isinstance(data["duration_ms"], float) assert data["duration_ms"] >= 0 def test_duration_ms_file_reverse(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", "--reverse", "billing.py") data = json.loads(r.output) assert "duration_ms" in data assert isinstance(data["duration_ms"], float) assert data["duration_ms"] >= 0 def test_duration_ms_symbol_forward_depth1(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", _SYMBOL) data = json.loads(r.output) assert "duration_ms" in data assert isinstance(data["duration_ms"], float) assert data["duration_ms"] >= 0 def test_duration_ms_symbol_forward_transitive( self, deps_repo: pathlib.Path ) -> None: r = _run(deps_repo, "code", "deps", "--json", "--transitive", _SYMBOL) data = json.loads(r.output) assert "duration_ms" in data assert isinstance(data["duration_ms"], float) assert data["duration_ms"] >= 0 def test_duration_ms_symbol_reverse_depth1(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", "--reverse", _SYMBOL) data = json.loads(r.output) assert "duration_ms" in data assert isinstance(data["duration_ms"], float) assert data["duration_ms"] >= 0 def test_duration_ms_symbol_reverse_transitive( self, deps_repo: pathlib.Path ) -> None: r = _run(deps_repo, "code", "deps", "--json", "--reverse", "--transitive", _SYMBOL) data = json.loads(r.output) assert "duration_ms" in data assert isinstance(data["duration_ms"], float) assert data["duration_ms"] >= 0 def test_j_alias_duration_ms_present_file_forward( self, deps_repo: pathlib.Path ) -> None: r = _run(deps_repo, "code", "deps", "-j", "billing.py") assert "duration_ms" in json.loads(r.output) def test_j_alias_duration_ms_present_symbol( self, deps_repo: pathlib.Path ) -> None: r = _run(deps_repo, "code", "deps", "-j", _SYMBOL) assert "duration_ms" in json.loads(r.output) # --------------------------------------------------------------------------- # TestExitCode — all 6 JSON paths must include exit_code = 0 # --------------------------------------------------------------------------- class TestExitCode: """JSON exit_code must be 0 on success across all 6 JSON output paths.""" def test_exit_code_file_forward(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", "billing.py") assert r.exit_code == 0 data = json.loads(r.output) assert "exit_code" in data assert data["exit_code"] == 0 def test_exit_code_file_reverse(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", "--reverse", "billing.py") assert r.exit_code == 0 data = json.loads(r.output) assert "exit_code" in data assert data["exit_code"] == 0 def test_exit_code_symbol_forward_depth1(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", _SYMBOL) assert r.exit_code == 0 data = json.loads(r.output) assert "exit_code" in data assert data["exit_code"] == 0 def test_exit_code_symbol_forward_transitive( self, deps_repo: pathlib.Path ) -> None: r = _run(deps_repo, "code", "deps", "--json", "--transitive", _SYMBOL) assert r.exit_code == 0 data = json.loads(r.output) assert "exit_code" in data assert data["exit_code"] == 0 def test_exit_code_symbol_reverse_depth1(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", "--reverse", _SYMBOL) assert r.exit_code == 0 data = json.loads(r.output) assert "exit_code" in data assert data["exit_code"] == 0 def test_exit_code_symbol_reverse_transitive( self, deps_repo: pathlib.Path ) -> None: r = _run(deps_repo, "code", "deps", "--json", "--reverse", "--transitive", _SYMBOL) assert r.exit_code == 0 data = json.loads(r.output) assert "exit_code" in data assert data["exit_code"] == 0 def test_exit_code_is_int_file_forward(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", "billing.py") assert isinstance(json.loads(r.output)["exit_code"], int) def test_exit_code_mirrors_process_exit(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", "billing.py") assert json.loads(r.output)["exit_code"] == r.exit_code def test_j_alias_exit_code_present_file(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "-j", "billing.py") assert "exit_code" in json.loads(r.output) def test_j_alias_exit_code_present_symbol(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "-j", _SYMBOL) assert "exit_code" in json.loads(r.output) # --------------------------------------------------------------------------- # TestTypedDicts — _DepsFileJson and _DepsSymbolJson carry new fields # --------------------------------------------------------------------------- class TestTypedDicts: """_DepsFileJson and _DepsSymbolJson must carry exit_code and duration_ms.""" def test_deps_file_json_typeddict_exists(self) -> None: from muse.cli.commands.deps import _DepsFileJson # noqa: F401 def test_deps_symbol_json_typeddict_exists(self) -> None: from muse.cli.commands.deps import _DepsSymbolJson # noqa: F401 def test_file_json_has_exit_code_annotation(self) -> None: from muse.cli.commands.deps import _DepsFileJson assert "exit_code" in _DepsFileJson.__annotations__ def test_file_json_has_duration_ms_annotation(self) -> None: from muse.cli.commands.deps import _DepsFileJson assert "duration_ms" in _DepsFileJson.__annotations__ def test_symbol_json_has_exit_code_annotation(self) -> None: from muse.cli.commands.deps import _DepsSymbolJson assert "exit_code" in _DepsSymbolJson.__annotations__ def test_symbol_json_has_duration_ms_annotation(self) -> None: from muse.cli.commands.deps import _DepsSymbolJson assert "duration_ms" in _DepsSymbolJson.__annotations__ def test_file_json_retains_path_annotation(self) -> None: from muse.cli.commands.deps import _DepsFileJson assert "path" in _DepsFileJson.__annotations__ def test_symbol_json_retains_address_annotation(self) -> None: from muse.cli.commands.deps import _DepsSymbolJson assert "address" in _DepsSymbolJson.__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_file_forward(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", "billing.py") assert "\x1b" not in r.output def test_json_output_no_ansi_file_reverse(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", "--reverse", "billing.py") assert "\x1b" not in r.output def test_json_output_no_ansi_symbol_forward(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", _SYMBOL) 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_file_forward( self, deps_repo: pathlib.Path ) -> None: r = _run(deps_repo, "code", "deps", "--json", "billing.py") assert json.loads(r.output)["duration_ms"] < 2000 def test_json_duration_under_2000ms_symbol(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", _SYMBOL) assert json.loads(r.output)["duration_ms"] < 2000 def test_duration_ms_is_float_not_int(self, deps_repo: pathlib.Path) -> None: r = _run(deps_repo, "code", "deps", "--json", "billing.py") assert isinstance(json.loads(r.output)["duration_ms"], float) # --------------------------------------------------------------------------- # TestRegisterFlags — --json / -j normalized at argparse level # --------------------------------------------------------------------------- class TestRegisterFlags: """register() must expose --json with -j shorthand and dest=json_out.""" def _make_parser(self) -> argparse.ArgumentParser: import argparse as ap from muse.cli.commands.deps import register root = ap.ArgumentParser() subs = root.add_subparsers() register(subs) return root def test_json_out_default_false(self) -> None: p = self._make_parser() ns = p.parse_args(["deps", "billing.py"]) assert ns.json_out is False def test_json_out_true_with_json_flag(self) -> None: p = self._make_parser() ns = p.parse_args(["deps", "billing.py", "--json"]) assert ns.json_out is True def test_json_out_true_with_j_flag(self) -> None: p = self._make_parser() ns = p.parse_args(["deps", "billing.py", "-j"]) assert ns.json_out is True