test_cmd_symlog_show.py
file-level
1
files
1
commits
0
hotspots
0
🧊 dead
0
💥 blast risk
| 1 | """muse#53 item 9 -- `muse symlog show "file.py::Symbol@{N}"`. |
| 2 | |
| 3 | Convenience alias for `muse code cat "addr@{N}"` with a human-focused |
| 4 | header (operation, author, timestamp) printed before the full symbol |
| 5 | body. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import json |
| 11 | import os |
| 12 | import pathlib |
| 13 | import textwrap |
| 14 | |
| 15 | from tests.cli_test_helper import CliRunner |
| 16 | |
| 17 | runner = CliRunner() |
| 18 | |
| 19 | |
| 20 | def _invoke(repo: pathlib.Path, args: list[str]): |
| 21 | saved = os.getcwd() |
| 22 | try: |
| 23 | os.chdir(repo) |
| 24 | return runner.invoke(None, args) |
| 25 | finally: |
| 26 | os.chdir(saved) |
| 27 | |
| 28 | |
| 29 | def _init_repo(repo: pathlib.Path) -> None: |
| 30 | repo.mkdir(parents=True, exist_ok=True) |
| 31 | _invoke(repo, ["init"]) |
| 32 | |
| 33 | |
| 34 | def _write_file(repo: pathlib.Path, rel: str, content: str) -> None: |
| 35 | p = repo / rel |
| 36 | p.parent.mkdir(parents=True, exist_ok=True) |
| 37 | p.write_text(textwrap.dedent(content)) |
| 38 | |
| 39 | |
| 40 | def _commit(repo: pathlib.Path, msg: str, author: str = "claude-code") -> str: |
| 41 | _invoke(repo, ["code", "add", "."]) |
| 42 | result = _invoke(repo, [ |
| 43 | "commit", "-m", msg, |
| 44 | "--agent-id", "claude-code", |
| 45 | "--model-id", "claude-sonnet-4-6", |
| 46 | "--author", author, |
| 47 | "--json", |
| 48 | ]) |
| 49 | data = json.loads(result.stdout) |
| 50 | return data["commit_id"] |
| 51 | |
| 52 | |
| 53 | def _make_repo(tmp_path: pathlib.Path) -> pathlib.Path: |
| 54 | _init_repo(tmp_path) |
| 55 | return tmp_path |
| 56 | |
| 57 | |
| 58 | def _symlog(repo: pathlib.Path, *args: str): |
| 59 | return _invoke(repo, ["symlog", *args]) |
| 60 | |
| 61 | |
| 62 | class TestSymlogShow: |
| 63 | def test_show_returns_body_and_metadata(self, tmp_path: pathlib.Path) -> None: |
| 64 | repo = _make_repo(tmp_path) |
| 65 | _write_file(repo, "src/billing.py", "def compute_total(items):\n return sum(items)\n") |
| 66 | _commit(repo, "add compute_total") |
| 67 | |
| 68 | result = _symlog(repo, "show", "src/billing.py::compute_total@{0}", "--json") |
| 69 | assert result.exit_code == 0, result.stdout |
| 70 | data = json.loads(result.stdout) |
| 71 | assert data["symbol"] == "src/billing.py::compute_total" |
| 72 | assert data["index"] == 0 |
| 73 | assert data["author"] == "claude-code" |
| 74 | assert data["operation"].startswith("symbol-created") |
| 75 | assert data["source"] is not None |
| 76 | assert "def compute_total" in data["source"] |
| 77 | |
| 78 | def test_show_at_older_index_returns_older_body(self, tmp_path: pathlib.Path) -> None: |
| 79 | repo = _make_repo(tmp_path) |
| 80 | _write_file(repo, "src/billing.py", "def compute_total(items):\n return sum(items)\n") |
| 81 | _commit(repo, "add compute_total") |
| 82 | _write_file(repo, "src/billing.py", "def compute_total(items):\n return sum(items) + 1\n") |
| 83 | _commit(repo, "fix off-by-one") |
| 84 | |
| 85 | newest = json.loads(_symlog(repo, "show", "src/billing.py::compute_total@{0}", "--json").stdout) |
| 86 | oldest = json.loads(_symlog(repo, "show", "src/billing.py::compute_total@{1}", "--json").stdout) |
| 87 | assert "+ 1" in newest["source"] |
| 88 | assert "+ 1" not in oldest["source"] |
| 89 | |
| 90 | def test_show_missing_symbol_errors(self, tmp_path: pathlib.Path) -> None: |
| 91 | repo = _make_repo(tmp_path) |
| 92 | result = _symlog(repo, "show", "src/billing.py::nonexistent@{0}", "--json") |
| 93 | assert result.exit_code == 1 |
| 94 | |
| 95 | def test_show_out_of_range_index_errors(self, tmp_path: pathlib.Path) -> None: |
| 96 | repo = _make_repo(tmp_path) |
| 97 | _write_file(repo, "src/billing.py", "def compute_total(items):\n return sum(items)\n") |
| 98 | _commit(repo, "add") |
| 99 | result = _symlog(repo, "show", "src/billing.py::compute_total@{5}", "--json") |
| 100 | assert result.exit_code == 1 |
| 101 | |
| 102 | def test_show_invalid_ref_form_errors(self, tmp_path: pathlib.Path) -> None: |
| 103 | repo = _make_repo(tmp_path) |
| 104 | result = _symlog(repo, "show", "src/billing.py::compute_total", "--json") |
| 105 | assert result.exit_code == 1 |
| 106 | |
| 107 | def test_show_text_mode_prints_header_and_body(self, tmp_path: pathlib.Path) -> None: |
| 108 | repo = _make_repo(tmp_path) |
| 109 | _write_file(repo, "src/billing.py", "def compute_total(items):\n return sum(items)\n") |
| 110 | _commit(repo, "add compute_total") |
| 111 | |
| 112 | result = _symlog(repo, "show", "src/billing.py::compute_total@{0}") |
| 113 | assert result.exit_code == 0, result.stdout |
| 114 | assert "author:" in result.stdout |
| 115 | assert "def compute_total" in result.stdout |