"""Comprehensive tests for muse code api-surface. Test layers ----------- Unit Pure functions: _is_public, _classify_change, _semver_impact, _stability_pct, _ApiEntry.to_dict. Zero I/O. Integration CLI invocations against a real tmp-path repo built from the shared fixtures. Covers: list mode, diff mode, --json schema, --count, --language, --file, --breaking, --commit. Edge-case Empty API surface, no commits, invalid ref, --breaking without --diff, private symbol filtering, mixed symbol kinds. Stress 50-file snapshot and 100-commit diff history exercise the shared SymbolCache path and confirm sub-second latency. """ from __future__ import annotations import json import pathlib import textwrap import time import pytest from tests.cli_test_helper import CliRunner from muse.cli.commands.api_surface import ( _ApiEntry, _BREAKING_CHANGES, _classify_change, _is_public, _semver_impact, _stability_pct, ) from muse.plugins.code.ast_parser import SymbolRecord, SymbolTree type _ChangedMap = dict[str, tuple[SymbolRecord, SymbolRecord, str]] cli = None # argparse migration — CliRunner ignores this arg runner = CliRunner() # --------------------------------------------------------------------------- # Shared fixtures # --------------------------------------------------------------------------- @pytest.fixture def repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path: """Fresh code-domain Muse repo.""" monkeypatch.chdir(tmp_path) monkeypatch.setenv("MUSE_REPO_ROOT", str(tmp_path)) r = runner.invoke(cli, ["init", "--domain", "code"]) assert r.exit_code == 0, r.output return tmp_path @pytest.fixture def code_repo(repo: pathlib.Path) -> pathlib.Path: """Two-commit repo: billing module created then extended.""" (repo / "billing.py").write_text(textwrap.dedent("""\ class Invoice: def compute_total(self, items): return sum(items) def _validate(self, items): return bool(items) def process_order(invoice, items): return invoice.compute_total(items) def _internal_helper(): pass """)) runner.invoke(cli, ["code", "add", "."]) r = runner.invoke(cli, ["commit", "-m", "Initial billing module"]) assert r.exit_code == 0, r.output (repo / "billing.py").write_text(textwrap.dedent("""\ class Invoice: def compute_total(self, items, currency="USD"): return sum(items) def apply_discount(self, total, pct): return total * (1 - pct) def _validate(self, items): return bool(items) def process_order(invoice, items): return invoice.compute_total(items) def calculate_tax(amount, rate): return amount * rate def _internal_helper(): pass """)) runner.invoke(cli, ["code", "add", "."]) r = runner.invoke(cli, ["commit", "-m", "Add discount + tax helpers"]) assert r.exit_code == 0, r.output return repo # --------------------------------------------------------------------------- # Unit: _is_public # --------------------------------------------------------------------------- class TestIsPublic: def test_public_function(self) -> None: assert _is_public("compute_total", "function") is True def test_public_class(self) -> None: assert _is_public("Invoice", "class") is True def test_public_method(self) -> None: assert _is_public("Invoice.compute_total", "method") is True def test_private_name_rejected(self) -> None: assert _is_public("_internal", "function") is False def test_dunder_rejected(self) -> None: assert _is_public("__init__", "method") is False def test_import_kind_rejected(self) -> None: assert _is_public("os", "import") is False def test_variable_kind_rejected(self) -> None: assert _is_public("MAX_RETRIES", "variable") is False def test_async_function_is_public(self) -> None: assert _is_public("fetch_data", "async_function") is True def test_async_method_is_public(self) -> None: assert _is_public("SomeClass.run", "async_method") is True def test_qualified_private_method_rejected(self) -> None: """Check that the bare name (after last dot) is used for _ prefix detection.""" assert _is_public("Invoice._validate", "method") is False # --------------------------------------------------------------------------- # Unit: _classify_change # --------------------------------------------------------------------------- def _make_rec( content_id: str = "abc", signature_id: str = "sig", body_hash: str = "body", ) -> SymbolRecord: return SymbolRecord( name="f", kind="function", qualified_name="f", content_id=content_id, signature_id=signature_id, body_hash=body_hash, metadata_id="", canonical_key="test.py#f#function#f#1", lineno=1, end_lineno=2, ) class TestClassifyChange: def test_unchanged(self) -> None: rec = _make_rec("same", "same", "same") assert _classify_change(rec, rec) == "unchanged" def test_impl_only(self) -> None: old = _make_rec("old", "sig", "old_body") new = _make_rec("new", "sig", "new_body") assert _classify_change(old, new) == "impl_only" def test_signature_change(self) -> None: old = _make_rec("old", "sig_a", "body_a") new = _make_rec("new", "sig_b", "body_a") assert _classify_change(old, new) == "signature_change" def test_signature_plus_impl(self) -> None: old = _make_rec("old", "sig_a", "body_a") new = _make_rec("new", "sig_b", "body_b") assert _classify_change(old, new) == "signature+impl" # --------------------------------------------------------------------------- # Unit: _semver_impact # --------------------------------------------------------------------------- _EMPTY_SR: SymbolTree = {} _EMPTY_CHANGED: _ChangedMap = {} def _make_changed(cls: str) -> _ChangedMap: return {"addr": (_make_rec(), _make_rec(), cls)} class TestSemverImpact: def test_no_changes_is_none(self) -> None: assert _semver_impact(_EMPTY_SR, _EMPTY_SR, _EMPTY_CHANGED) == "NONE" def test_removal_is_major(self) -> None: assert _semver_impact(_EMPTY_SR, {"addr": _make_rec()}, _EMPTY_CHANGED) == "MAJOR" def test_signature_change_is_major(self) -> None: assert _semver_impact(_EMPTY_SR, _EMPTY_SR, _make_changed("signature_change")) == "MAJOR" def test_signature_plus_impl_is_major(self) -> None: assert _semver_impact(_EMPTY_SR, _EMPTY_SR, _make_changed("signature+impl")) == "MAJOR" def test_addition_only_is_minor(self) -> None: assert _semver_impact({"addr": _make_rec()}, _EMPTY_SR, _EMPTY_CHANGED) == "MINOR" def test_impl_only_change_is_patch(self) -> None: assert _semver_impact(_EMPTY_SR, _EMPTY_SR, _make_changed("impl_only")) == "PATCH" def test_removal_beats_addition(self) -> None: """MAJOR wins even when symbols were also added.""" assert _semver_impact({"x": _make_rec()}, {"y": _make_rec()}, _EMPTY_CHANGED) == "MAJOR" # --------------------------------------------------------------------------- # Unit: _stability_pct # --------------------------------------------------------------------------- class TestStabilityPct: def test_empty_base_is_100(self) -> None: assert _stability_pct(0, _EMPTY_SR, _EMPTY_CHANGED) == 100 def test_nothing_changed(self) -> None: assert _stability_pct(10, _EMPTY_SR, _EMPTY_CHANGED) == 100 def test_half_removed(self) -> None: removed = {f"addr_{i}": _make_rec() for i in range(5)} assert _stability_pct(10, removed, _EMPTY_CHANGED) == 50 def test_all_removed(self) -> None: removed = {f"addr_{i}": _make_rec() for i in range(4)} assert _stability_pct(4, removed, _EMPTY_CHANGED) == 0 def test_changes_count_toward_instability(self) -> None: changed = _make_changed("impl_only") assert _stability_pct(10, _EMPTY_SR, changed) == 90 # --------------------------------------------------------------------------- # Unit: _ApiEntry.to_dict # --------------------------------------------------------------------------- class TestApiEntryToDict: def test_full_sha_not_truncated(self) -> None: full_cid = "a" * 64 rec = _make_rec(content_id=full_cid, signature_id="s" * 64, body_hash="b" * 64) entry = _ApiEntry("billing.py::f", rec, "Python") d = entry.to_dict() assert d["content_id"] == full_cid assert len(d["content_id"]) == 64 def test_required_keys_present(self) -> None: rec = _make_rec() entry = _ApiEntry("a.py::f", rec, "Python") d = entry.to_dict() for key in ("address", "kind", "name", "qualified_name", "language", "content_id", "signature_id", "body_hash"): assert key in d def test_breaking_changes_set(self) -> None: """Confirm _BREAKING_CHANGES covers the two breaking classifications.""" assert "signature_change" in _BREAKING_CHANGES assert "signature+impl" in _BREAKING_CHANGES assert "impl_only" not in _BREAKING_CHANGES # --------------------------------------------------------------------------- # Integration: list mode # --------------------------------------------------------------------------- class TestApiSurfaceListMode: def test_exits_zero(self, code_repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface"]) assert r.exit_code == 0, r.output def test_private_symbols_excluded(self, code_repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface"]) assert r.exit_code == 0 assert "_validate" not in r.output assert "_internal_helper" not in r.output def test_public_symbols_included(self, code_repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface"]) assert r.exit_code == 0 assert "process_order" in r.output or "Invoice" in r.output def test_json_schema(self, code_repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface", "--json"]) assert r.exit_code == 0 data = json.loads(r.output) assert "commit_id" in data assert "total" in data assert "results" in data assert isinstance(data["results"], list) def test_json_commit_id_is_full_sha(self, code_repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface", "--json"]) assert r.exit_code == 0 data = json.loads(r.output) assert data["commit_id"].startswith("sha256:"), "commit_id must have sha256: prefix" def test_json_entry_content_id_is_full_sha(self, code_repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface", "--json"]) assert r.exit_code == 0 data = json.loads(r.output) for sym in data["results"]: cid = sym["content_id"] assert cid.startswith("sha256:") and len(cid) == 71, ( f"content_id must be 'sha256:<64hex>' in {sym['address']}: {cid!r}" ) def test_count_only_is_integer(self, code_repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface", "--count"]) assert r.exit_code == 0 assert r.output.strip().isdigit() def test_language_filter_python(self, code_repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface", "--language", "Python"]) assert r.exit_code == 0 def test_file_filter_restricts_output(self, code_repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface", "--json", "--file", "billing.py"]) assert r.exit_code == 0 data = json.loads(r.output) for sym in data["results"]: assert "billing.py" in sym["address"] def test_file_filter_nonexistent_returns_empty(self, code_repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface", "--json", "--file", "nonexistent_xyz.py"]) assert r.exit_code == 0 data = json.loads(r.output) assert data["total"] == 0 def test_no_commits_handled(self, repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface"]) assert r.exit_code in (0, 1) def test_invalid_ref_rejected(self, code_repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface", "--commit", "deadbeef0000nonexistent"]) assert r.exit_code == 1 def test_requires_repo(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.chdir(tmp_path) r = runner.invoke(cli, ["code", "api-surface"]) assert r.exit_code != 0 # --------------------------------------------------------------------------- # Integration: diff mode # --------------------------------------------------------------------------- @pytest.fixture def diff_repo(repo: pathlib.Path) -> pathlib.Path: """Three-commit repo that produces a clear diff with all change types.""" # Commit 1: baseline API (repo / "billing.py").write_text(textwrap.dedent("""\ def compute_total(items): return sum(items) def apply_discount(total, pct): return total * (1 - pct) def _private_helper(): pass """)) runner.invoke(cli, ["code", "add", "."]) r = runner.invoke(cli, ["commit", "-m", "Baseline API"]) assert r.exit_code == 0, r.output # Commit 2: modified API # - compute_total: signature changed (new param) # - apply_discount: removed (breaking) # - calculate_tax: added (repo / "billing.py").write_text(textwrap.dedent("""\ def compute_total(items, currency="USD"): return sum(items) def calculate_tax(amount, rate): return amount * rate def _private_helper(): pass """)) runner.invoke(cli, ["code", "add", "."]) r = runner.invoke(cli, ["commit", "-m", "Overhaul billing API"]) assert r.exit_code == 0, r.output return repo class TestApiSurfaceDiffMode: def _commits(self, repo: pathlib.Path) -> list[str]: from muse.core.commits import get_all_commits return [ c.commit_id for c in sorted(get_all_commits(repo), key=lambda c: c.committed_at) ] def test_diff_exits_zero(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, ["code", "api-surface", "--diff", commits[0]]) assert r.exit_code == 0, r.output def test_diff_json_schema(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, ["code", "api-surface", "--diff", commits[0], "--json"]) assert r.exit_code == 0 data = json.loads(r.output) for key in ("commit_id", "base_commit_id", "semver_impact", "stability_pct", "breaking_count", "added", "removed", "changed"): assert key in data, f"Missing key: {key}" def test_diff_commit_ids_are_full_sha(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, ["code", "api-surface", "--diff", commits[0], "--json"]) assert r.exit_code == 0 data = json.loads(r.output) assert data["commit_id"].startswith("sha256:") assert data["base_commit_id"].startswith("sha256:") def test_diff_detects_added_symbols(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, ["code", "api-surface", "--diff", commits[0], "--json"]) assert r.exit_code == 0 data = json.loads(r.output) added_names = {s["name"] for s in data["added"]} assert "calculate_tax" in added_names def test_diff_detects_removed_symbols(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, ["code", "api-surface", "--diff", commits[0], "--json"]) assert r.exit_code == 0 data = json.loads(r.output) removed_names = {s["name"] for s in data["removed"]} assert "apply_discount" in removed_names def test_diff_semver_impact_is_major(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, ["code", "api-surface", "--diff", commits[0], "--json"]) assert r.exit_code == 0 data = json.loads(r.output) assert data["semver_impact"] == "MAJOR" def test_diff_breaking_count_nonzero(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, ["code", "api-surface", "--diff", commits[0], "--json"]) assert r.exit_code == 0 data = json.loads(r.output) assert data["breaking_count"] > 0 def test_diff_stability_pct_in_range(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, ["code", "api-surface", "--diff", commits[0], "--json"]) assert r.exit_code == 0 data = json.loads(r.output) assert 0 <= data["stability_pct"] <= 100 def test_diff_changed_entries_have_breaking_field(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, ["code", "api-surface", "--diff", commits[0], "--json"]) assert r.exit_code == 0 data = json.loads(r.output) for entry in data["changed"]: assert "breaking" in entry assert isinstance(entry["breaking"], bool) def test_diff_count_only(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, ["code", "api-surface", "--diff", commits[0], "--count"]) assert r.exit_code == 0 assert r.output.strip().isdigit() def test_diff_human_output_contains_sections(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, ["code", "api-surface", "--diff", commits[0]]) assert r.exit_code == 0 assert "Added" in r.output or "Removed" in r.output or "Changed" in r.output def test_diff_human_output_shows_semver_impact(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, ["code", "api-surface", "--diff", commits[0]]) assert r.exit_code == 0 assert "semver impact:" in r.output def test_diff_invalid_base_ref_rejected(self, diff_repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface", "--diff", "totally_nonexistent_ref"]) assert r.exit_code == 1 def test_breaking_flag_requires_diff(self, diff_repo: pathlib.Path) -> None: r = runner.invoke(cli, ["code", "api-surface", "--breaking"]) assert r.exit_code == 1 def test_breaking_flag_filters_to_breaking_only(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, [ "code", "api-surface", "--diff", commits[0], "--breaking", "--json", ]) # Exit non-zero because breaking changes exist data = json.loads(r.output) # Added symbols should be filtered out in breaking-only mode assert len(data["added"]) == 0 for entry in data["changed"]: assert entry["breaking"] is True def test_diff_file_filter(self, diff_repo: pathlib.Path) -> None: commits = self._commits(diff_repo) r = runner.invoke(cli, [ "code", "api-surface", "--diff", commits[0], "--file", "billing.py", "--json", ]) assert r.exit_code == 0 data = json.loads(r.output) assert data["file_filter"] == "billing.py" def test_no_changes_message(self, code_repo: pathlib.Path) -> None: """Diffing a commit against itself shows no changes.""" from muse.core.commits import get_all_commits commits_list = sorted(get_all_commits(code_repo), key=lambda c: c.committed_at) head_id = commits_list[-1].commit_id r = runner.invoke(cli, ["code", "api-surface", "--diff", head_id]) assert r.exit_code == 0 assert "No public API changes" in r.output # --------------------------------------------------------------------------- # Stress: performance with large snapshot and long history # --------------------------------------------------------------------------- class TestApiSurfaceStress: def test_large_snapshot_completes_quickly( self, repo: pathlib.Path, monkeypatch: pytest.MonkeyPatch ) -> None: """50 Python files × 10 public functions — list mode under 5s. Each file has unique content (module index embedded) so Muse's content-addressed storage does not deduplicate symbol trees. """ for i in range(50): lines = "\n".join( f"def public_fn_m{i}_f{j}(x, y):\n return x + y + {i * 100 + j}" for j in range(10) ) (repo / f"module_{i}.py").write_text(lines) runner.invoke(cli, ["code", "add", "."]) r = runner.invoke(cli, ["commit", "-m", "Large snapshot"]) assert r.exit_code == 0 t0 = time.monotonic() r = runner.invoke(cli, ["code", "api-surface", "--json"]) elapsed = time.monotonic() - t0 assert r.exit_code == 0 data = json.loads(r.output) assert data["total"] == 500, ( f"Expected 500 public symbols (50 files × 10 fns), got {data['total']}" ) assert elapsed < 5.0, f"api-surface took {elapsed:.1f}s — too slow" def test_diff_large_snapshot_completes_quickly( self, repo: pathlib.Path, monkeypatch: pytest.MonkeyPatch ) -> None: """Diff across two 50-file snapshots — shared cache, under 6s.""" for i in range(50): (repo / f"module_{i}.py").write_text( "\n".join(f"def fn_m{i}_f{j}(x): return x + {i * 100 + j}" for j in range(10)) ) runner.invoke(cli, ["code", "add", "."]) r = runner.invoke(cli, ["commit", "-m", "Base snapshot"]) assert r.exit_code == 0 from muse.core.commits import get_all_commits base_commits = sorted(get_all_commits(repo), key=lambda c: c.committed_at) base_id = base_commits[0].commit_id # Modify half the files (unique content per file, still) for i in range(25): (repo / f"module_{i}.py").write_text( "\n".join( f"def fn_m{i}_f{j}(x, y=0): return x + y + {i * 100 + j}" for j in range(10) ) ) runner.invoke(cli, ["code", "add", "."]) r = runner.invoke(cli, ["commit", "-m", "Modify 25 modules"]) assert r.exit_code == 0 t0 = time.monotonic() r = runner.invoke(cli, ["code", "api-surface", "--diff", base_id, "--json"]) elapsed = time.monotonic() - t0 assert r.exit_code == 0 data = json.loads(r.output) assert data["semver_impact"] in ("MAJOR", "MINOR", "PATCH", "NONE") assert elapsed < 6.0, f"api-surface --diff took {elapsed:.1f}s — too slow" def test_cache_reuse_second_call_faster( self, repo: pathlib.Path, monkeypatch: pytest.MonkeyPatch ) -> None: """Second invocation benefits from warm SymbolCache.""" for i in range(20): (repo / f"mod_{i}.py").write_text( "\n".join(f"def g_m{i}_f{j}(x): return x + {i * 50 + j}" for j in range(20)) ) runner.invoke(cli, ["code", "add", "."]) r = runner.invoke(cli, ["commit", "-m", "Warm cache test"]) assert r.exit_code == 0 # First call — cold cache t0 = time.monotonic() runner.invoke(cli, ["code", "api-surface", "--json"]) first = time.monotonic() - t0 # Second call — warm cache t0 = time.monotonic() runner.invoke(cli, ["code", "api-surface", "--json"]) second = time.monotonic() - t0 assert second < first * 0.8 or second < 0.5, ( f"Cache not helping: first={first:.2f}s second={second:.2f}s" ) import argparse as _argparse class TestRegisterFlags: """Argparse registration tests for ``muse api-surface``.""" def _parse(self, *args: str) -> _argparse.Namespace: from muse.cli.commands.api_surface import register p = _argparse.ArgumentParser() sub = p.add_subparsers() register(sub) return p.parse_args(["api-surface", *args]) def test_default_json_out_is_false(self) -> None: ns = self._parse() assert ns.json_out is False def test_json_flag_sets_json_out(self) -> None: ns = self._parse("--json") assert ns.json_out is True def test_j_shorthand_sets_json_out(self) -> None: ns = self._parse("-j") assert ns.json_out is True def test_commit_flag(self) -> None: ns = self._parse("--commit", "HEAD~1") assert ns.ref == "HEAD~1" def test_diff_default(self) -> None: ns = self._parse() assert ns.diff_ref is None def test_language_flag(self) -> None: ns = self._parse("--language", "Python") assert ns.language == "Python" def test_count_default(self) -> None: ns = self._parse() assert ns.count_only is False