Files
cd-browser/tests/test_ui.py
Saky 65508d263f feat: implement history mode and AI development logging
- add dedicated history mode UI (toggle with `h`)
- allow navigation inside history with arrows
- allow selecting history entries with Enter
- add tests for history mode behavior
- introduce AI development logs (`ai-worklog.md`, `ai-prompts.md`)
- add AI worklog policy to `instructions-agent.md`
2026-03-15 17:46:11 +01:00

150 lines
4.1 KiB
Python

import curses
from pathlib import Path
import pytest
from app.navigator import Navigator, VisibleEntry
from app.ui import (
HistoryLine,
RenderLine,
TerminalUI,
build_history_lines,
build_render_lines,
format_entry,
move_selection,
open_terminal_streams,
)
def test_move_selection_stays_within_bounds() -> None:
assert move_selection(current_index=0, step=-1, entry_count=3) == 0
assert move_selection(current_index=1, step=1, entry_count=3) == 2
assert move_selection(current_index=2, step=1, entry_count=3) == 2
def test_format_entry_supports_parent_and_tree_indicators(tmp_path: Path) -> None:
parent_entry = VisibleEntry(
name="..",
path=tmp_path,
depth=0,
has_children=True,
is_expanded=False,
is_parent=True,
)
collapsed_entry = VisibleEntry(
name="scripts",
path=tmp_path / "scripts",
depth=0,
has_children=True,
is_expanded=False,
)
expanded_child = VisibleEntry(
name="build",
path=tmp_path / "scripts" / "build",
depth=1,
has_children=False,
is_expanded=False,
)
assert format_entry(parent_entry) == ".."
assert format_entry(collapsed_entry) == "▶ scripts"
assert format_entry(expanded_child) == " build"
def test_build_render_lines_marks_selected_entry(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
alpha = workspace / "alpha"
beta = workspace / "beta"
alpha.mkdir()
beta.mkdir()
navigator = Navigator(workspace)
navigator.set_selected_index(2)
assert build_render_lines(navigator) == [
RenderLine(text="..", is_selected=False),
RenderLine(text=" alpha", is_selected=False),
RenderLine(text=" beta", is_selected=True),
]
def test_build_history_lines_marks_selected_history_entry(tmp_path: Path) -> None:
first = tmp_path / "first"
second = tmp_path / "second"
assert build_history_lines((first, second), selected_index=1) == [
HistoryLine(text=str(first), is_selected=False),
HistoryLine(text=str(second), is_selected=True),
]
def test_toggle_history_mode_selects_current_history_entry(tmp_path: Path) -> None:
root = tmp_path / "root"
child = root / "child"
root.mkdir()
child.mkdir()
navigator = Navigator(root)
navigator.set_selected_index(1)
navigator.enter_selected_directory()
ui = TerminalUI()
ui._toggle_history_mode(navigator)
assert ui._history_mode is True
assert ui._history_selected_index == 1
def test_history_mode_enter_selects_entry_and_exits(tmp_path: Path) -> None:
root = tmp_path / "root"
first = root / "first"
second = root / "second"
root.mkdir()
first.mkdir()
second.mkdir()
navigator = Navigator(root)
navigator.set_selected_index(1)
navigator.enter_selected_directory()
navigator.go_to_parent_directory()
navigator.set_selected_index(2)
navigator.enter_selected_directory()
ui = TerminalUI()
ui._toggle_history_mode(navigator)
ui._handle_history_key(curses.KEY_UP, navigator)
ui._handle_history_key(10, navigator)
assert ui._history_mode is False
assert navigator.current_path == root.resolve()
assert navigator.selected_index == 0
def test_history_mode_toggle_off_keeps_current_directory(tmp_path: Path) -> None:
root = tmp_path / "root"
root.mkdir()
navigator = Navigator(root)
ui = TerminalUI()
ui._toggle_history_mode(navigator)
ui._toggle_history_mode(navigator)
assert ui._history_mode is False
assert navigator.current_path == root.resolve()
def test_open_terminal_streams_raises_without_terminal(
monkeypatch: pytest.MonkeyPatch,
) -> None:
def raise_os_error(*args: object, **kwargs: object) -> object:
raise OSError
monkeypatch.setattr("builtins.open", raise_os_error)
monkeypatch.setattr("sys.stdin.isatty", lambda: False)
monkeypatch.setattr("sys.stderr.isatty", lambda: False)
with pytest.raises(RuntimeError, match="Interactive terminal is required"):
with open_terminal_streams():
pass