Something went wrong. Try again.
Integrated repository, worktree, git stacker, and project manager
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206"""Tests for `project_manager.tui`.
The picker's navigation/selection loop is exercised through `_pick_core`with a scripted `read_key` and a non-terminal `Console` — so these testsnever touch termios or real stdin and don't depend on running in a TTY.`pick`'s TTY guard is exercised directly (it raises before touchingtermios)."""
from __future__ import annotations
import iofrom collections.abc import Callablefrom dataclasses import dataclass
import pytestfrom rich.console import Console
from project_manager import tuifrom project_manager.render import Column, GroupColumn, Sectionfrom project_manager.tui import Key, TuiError
@dataclass(frozen=True)class _Row: name: str selectable: bool = True
_COLUMNS: list[Column[_Row]] = [Column("Name", "name")]
def _script(keys: list[Key]) -> Callable[[], Key]: """Return a `read_key` callable that yields `keys` in order.
After the last key the script raises — a well-behaved picker must have returned by then. Surfacing it as a loud test failure prevents silent infinite loops. """ it = iter(keys)
def read() -> Key: try: return next(it) except StopIteration as e: raise AssertionError("picker read past end of key script") from e
return read
def _sink_console() -> Console: # Non-terminal destination: rich's Live becomes a near-no-op (no cursor # hiding, no alt-screen), so tests don't need a real TTY. return Console(file=io.StringIO(), force_terminal=False, width=80)
def _call_core( sections: list[Section[_Row]], keys: list[Key], *, is_selectable: Callable[[_Row], bool] = lambda _: True,) -> _Row | None: materialized = [(s.title, list(s.rows)) for s in sections] flat_rows = [r for _, rs in materialized for r in rs] selectable_idxs = [i for i, r in enumerate(flat_rows) if is_selectable(r)] return tui._pick_core( materialized, flat_rows, selectable_idxs, _COLUMNS, group=GroupColumn("G"), header=None, read_key=_script(keys), console=_sink_console(), )
def test_enter_on_first_row_returns_first_row() -> None: secs = [Section(title="s", rows=[_Row("a"), _Row("b"), _Row("c")])] assert _call_core(secs, [Key.ENTER]) == _Row("a")
def test_down_down_enter_returns_third_row() -> None: secs = [Section(title="s", rows=[_Row("a"), _Row("b"), _Row("c")])] assert _call_core(secs, [Key.DOWN, Key.DOWN, Key.ENTER]) == _Row("c")
def test_down_up_enter_returns_first_row() -> None: secs = [Section(title="s", rows=[_Row("a"), _Row("b"), _Row("c")])] assert _call_core(secs, [Key.DOWN, Key.UP, Key.ENTER]) == _Row("a")
def test_up_at_top_stays_at_top() -> None: secs = [Section(title="s", rows=[_Row("a"), _Row("b")])] # Extra UPs at the top should have no effect; next ENTER returns row 0. assert _call_core(secs, [Key.UP, Key.UP, Key.UP, Key.ENTER]) == _Row("a")
def test_down_at_bottom_stays_at_bottom() -> None: secs = [Section(title="s", rows=[_Row("a"), _Row("b")])] assert _call_core( secs, [Key.DOWN, Key.DOWN, Key.DOWN, Key.ENTER], ) == _Row("b")
def test_cancel_returns_none() -> None: secs = [Section(title="s", rows=[_Row("a"), _Row("b")])] assert _call_core(secs, [Key.CANCEL]) is None
def test_other_keys_are_ignored() -> None: secs = [Section(title="s", rows=[_Row("a"), _Row("b")])] # OTHER should not move the cursor or select — only the final ENTER # resolves the pick, still on row 0. assert _call_core( secs, [Key.OTHER, Key.OTHER, Key.ENTER], ) == _Row("a")
def test_keyboard_interrupt_cancels() -> None: """Ctrl-C during cbreak raises KeyboardInterrupt in the caller's key reader; the picker treats it as cancel, not an unhandled crash."""
def read() -> Key: raise KeyboardInterrupt
materialized = [("s", [_Row("a")])] got = tui._pick_core( materialized, [_Row("a")], [0], _COLUMNS, group=GroupColumn("G"), header=None, read_key=read, console=_sink_console(), ) assert got is None
def test_is_selectable_skips_placeholder_rows() -> None: """Placeholder rows still render (visual continuity with `pm agent ls`) but navigation hops over them — DOWN from row 0 lands on row 2, skipping the middle placeholder.""" secs = [ Section( title="s", rows=[ _Row("a", selectable=True), _Row("placeholder", selectable=False), _Row("c", selectable=True), ], ), ] got = _call_core( secs, [Key.DOWN, Key.ENTER], is_selectable=lambda r: r.selectable, ) assert got == _Row("c")
def test_all_unselectable_returns_none_without_entering_loop() -> None: """No row is selectable → picker returns None immediately; the scripted reader is never consumed, which our `_script` would flag as an assertion error if the picker tried to read.""" secs = [ Section( title="s", rows=[ _Row("p1", selectable=False), _Row("p2", selectable=False), ], ), ] got = tui.pick( secs, _COLUMNS, group=GroupColumn("G"), is_selectable=lambda r: r.selectable, ) assert got is None
def test_empty_sections_returns_none() -> None: got = tui.pick([], _COLUMNS, group=GroupColumn("G")) assert got is None
def test_pick_raises_tui_error_on_non_tty_stdin( monkeypatch: pytest.MonkeyPatch,) -> None: """Library-level guard — call sites should normally pre-check, but `pick` still fails loudly rather than silently hanging on a non-TTY."""
class NonTtyStdin: def isatty(self) -> bool: return False
monkeypatch.setattr("sys.stdin", NonTtyStdin()) secs = [Section(title="s", rows=[_Row("a")])] with pytest.raises(TuiError, match="TTY"): tui.pick(secs, _COLUMNS, group=GroupColumn("G"))