"""Envelope tests for ``muse code age --json``.""" from __future__ import annotations import argparse import json import pytest from tests.cli_test_helper import CliRunner, InvokeResult runner = CliRunner() def _age_json(*extra: str) -> InvokeResult: return runner.invoke(None, ["code", "age", "--json", *extra]) class TestAgeEnvelope: def test_has_muse_version(self) -> None: r = _age_json() assert r.exit_code == 0, r.output assert "muse_version" in json.loads(r.output) def test_has_schema(self) -> None: r = _age_json() assert "schema" in json.loads(r.output) def test_has_timestamp(self) -> None: r = _age_json() assert "timestamp" in json.loads(r.output) def test_has_warnings(self) -> None: r = _age_json() assert "warnings" in json.loads(r.output) def test_no_schema_version(self) -> None: r = _age_json() assert "schema_version" not in json.loads(r.output) def test_j_alias_works(self) -> None: r = runner.invoke(None, ["code", "age", "-j"]) assert r.exit_code == 0 d = json.loads(r.output) assert "muse_version" in d class TestRegisterFlags: def _parse(self, *args: str) -> argparse.Namespace: from muse.cli.commands.age import register p = argparse.ArgumentParser() sub = p.add_subparsers() register(sub) return p.parse_args(["age", *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_top_default(self) -> None: ns = self._parse() assert ns.top == 20 def test_top_flag(self) -> None: ns = self._parse("--top", "50") assert ns.top == 50 def test_top_has_no_n_shorthand(self) -> None: import sys p = argparse.ArgumentParser() sub = p.add_subparsers() from muse.cli.commands.age import register register(sub) with pytest.raises(SystemExit): p.parse_args(["age", "-n", "10"]) def test_sort_default(self) -> None: ns = self._parse() assert ns.sort == "rewrites" def test_sort_flag(self) -> None: ns = self._parse("--sort", "calendar") assert ns.sort == "calendar" def test_kind_filter_default(self) -> None: ns = self._parse() assert ns.kind_filter is None def test_kind_flag(self) -> None: ns = self._parse("--kind", "function") assert ns.kind_filter == "function" def test_file_filter_default(self) -> None: ns = self._parse() assert ns.file_filter is None def test_file_flag(self) -> None: ns = self._parse("--file", "billing.py") assert ns.file_filter == "billing.py" def test_file_has_no_f_shorthand(self) -> None: p = argparse.ArgumentParser() sub = p.add_subparsers() from muse.cli.commands.age import register register(sub) with pytest.raises(SystemExit): p.parse_args(["age", "-f", "billing.py"]) def test_since_default(self) -> None: ns = self._parse() assert ns.since is None def test_since_flag(self) -> None: ns = self._parse("--since", "v1.0") assert ns.since == "v1.0" def test_max_commits_default(self) -> None: ns = self._parse() assert ns.max_commits == 10_000 def test_explain_default(self) -> None: ns = self._parse() assert ns.explain is None