Project: pygitweb

47a5dca from __future__ import annotations
import json
ece926d from collections.abc import Callable
47a5dca from pathlib import Path
8614508 from unittest.mock import MagicMock, patch
import pytest
ece926d from pygit2 import Commit, Oid, Repository, Signature
8614508 from pyratatui import Rect
47a5dca
ec18fab from pygittools.tasks import BOARD_REF_PREFIX, EMPTY_TREE_OID_HEX, TASK_REF_PREFIX, Board, Task
ece926d from pygittools.tui.branches import current_branch_name
8614508 from pygittools.tui.keys import key
053f925 from pygittools.tui.pages.actions import ActionsPage
47a5dca from pygittools.tui.pages.branches import BranchPickerPage, branch_picker_page
from pygittools.tui.pages.help import HelpPage, help_page
ece926d from pygittools.tui.pages.home import HomePage, home_page
47a5dca from pygittools.tui.pages.no_repo import NoRepoPage
from pygittools.tui.pages.projects import ProjectsPage, projects_page
ec18fab from pygittools.tui.pages.task_editor import TaskEditorPage
90ea48d from pygittools.tui.types import PageAction, PageContext, PageResult
47a5dca
def test_home_page_on_enter_and_quit(page_ctx: PageContext) -> None:
73c9c30 page = home_page()
page.on_enter(page_ctx)
assert page.status_text()
result = page.handle_key(key("q"))
assert result.action == PageAction.QUIT
47a5dca
8614508 def test_home_page_draw_skips_refresh(page_ctx: PageContext) -> None:
73c9c30 page = HomePage()
page.on_enter(page_ctx)
with (
patch.object(page.changes, "refresh") as changes_refresh,
patch.object(page.log, "refresh") as log_refresh,
patch.object(page.tasks, "refresh") as tasks_refresh,
patch.object(page.worktrees, "refresh") as worktrees_refresh,
):
page.draw(MagicMock(), Rect(0, 0, 80, 20))
changes_refresh.assert_not_called()
log_refresh.assert_not_called()
tasks_refresh.assert_not_called()
worktrees_refresh.assert_not_called()
8614508
def test_home_page_idle_refresh_after_interval(page_ctx: PageContext) -> None:
73c9c30 page = HomePage()
with patch("pygittools.tui.pages.home.time.monotonic", return_value=100.0):
page.on_enter(page_ctx)
with (
patch.object(page.changes, "refresh") as changes_refresh,
patch.object(page.log, "refresh") as log_refresh,
patch.object(page.tasks, "refresh") as tasks_refresh,
patch.object(page.worktrees, "refresh") as worktrees_refresh,
patch("pygittools.tui.pages.home.time.monotonic", side_effect=[109.0, 110.0, 110.0]),
):
page.on_idle()
changes_refresh.assert_not_called()
page.on_idle()
changes_refresh.assert_called_once_with(page_ctx.repo)
log_refresh.assert_called_once_with(page_ctx.repo)
tasks_refresh.assert_called_once_with(page_ctx.repo)
worktrees_refresh.assert_called_once_with(page_ctx.repo)
8614508
965624d def test_home_page_handle_key_skips_refresh(page_ctx: PageContext) -> None:
73c9c30 page = HomePage()
page.on_enter(page_ctx)
with (
patch.object(page.changes, "refresh") as changes_refresh,
patch.object(page.log, "refresh") as log_refresh,
patch.object(page.tasks, "refresh") as tasks_refresh,
patch.object(page.worktrees, "refresh") as worktrees_refresh,
):
page.handle_key(key("Down"))
changes_refresh.assert_not_called()
log_refresh.assert_not_called()
tasks_refresh.assert_not_called()
worktrees_refresh.assert_not_called()
965624d
def test_home_page_commit_typing_skips_refresh(page_ctx: PageContext) -> None:
73c9c30 page = HomePage()
page.on_enter(page_ctx)
_slash(page, "commit")
page.handle_key(key("a"))
with (
patch.object(page.changes, "refresh") as changes_refresh,
patch.object(page.log, "refresh") as log_refresh,
patch.object(page.tasks, "refresh") as tasks_refresh,
patch.object(page.worktrees, "refresh") as worktrees_refresh,
):
page.handle_key(key("b"))
changes_refresh.assert_not_called()
log_refresh.assert_not_called()
tasks_refresh.assert_not_called()
worktrees_refresh.assert_not_called()
assert page.changes.commit_message == "ab"
965624d
def test_home_page_commit_refreshes_log(committed_repo: Repository, monkeypatch: pytest.MonkeyPatch) -> None:
73c9c30 monkeypatch.setattr(
"pygittools.tui.changes_list.commit_staged",
lambda _repo, _msg, *, amend=False: (True, ""),
)
ctx = PageContext(repo=committed_repo, repo_path=Path(committed_repo.path))
page = HomePage()
page.on_enter(ctx)
page.changes.set_commit_message("amend note")
page.handle_key(key("Enter"))
page.handle_key(key("Tab"))
with (
patch.object(page.log, "refresh") as log_refresh,
patch.object(page.tasks, "refresh") as tasks_refresh,
patch.object(page.worktrees, "refresh") as worktrees_refresh,
):
page.handle_key(key("Enter"))
log_refresh.assert_called_once_with(committed_repo)
tasks_refresh.assert_not_called()
worktrees_refresh.assert_not_called()
965624d
def test_home_page_input_commit_refreshes_log(committed_repo: Repository, monkeypatch: pytest.MonkeyPatch) -> None:
73c9c30 monkeypatch.setattr(
"pygittools.tui.changes_list.commit_staged",
lambda _repo, _msg, *, amend=False: (True, ""),
)
ctx = PageContext(repo=committed_repo, repo_path=Path(committed_repo.path))
page = HomePage()
page.on_enter(ctx)
_slash(page, "commit")
page.handle_key(key("m"))
page.handle_key(key("Tab"))
with (
patch.object(page.log, "refresh") as log_refresh,
patch.object(page.tasks, "refresh") as tasks_refresh,
patch.object(page.worktrees, "refresh") as worktrees_refresh,
):
page.handle_key(key("Enter"))
log_refresh.assert_called_once_with(committed_repo)
tasks_refresh.assert_not_called()
worktrees_refresh.assert_not_called()
8614508
47a5dca def test_home_page_draw_and_tabs(committed_repo: Repository) -> None:
73c9c30 ctx = PageContext(repo=committed_repo, repo_path=Path(committed_repo.path))
page = home_page()
page.on_enter(ctx)
page.draw(MagicMock(), Rect(0, 0, 80, 20))
page.set_active_tab("log")
page.draw(MagicMock(), Rect(0, 0, 80, 20))
page.set_active_tab("tasks")
page.draw(MagicMock(), Rect(0, 0, 80, 20))
page.set_active_tab("worktrees")
page.draw(MagicMock(), Rect(0, 0, 80, 20))
page.set_header_focus("repo")
page.draw(MagicMock(), Rect(0, 0, 80, 20))
page.handle_key(key("Null"))
47a5dca
def test_home_page_switch_to_input(page_ctx: PageContext) -> None:
73c9c30 page = home_page()
page.on_enter(page_ctx)
_slash(page, "commit")
assert "Esc" in page.status_text()
47a5dca
8614508 def test_home_page_types_q_in_commit_input(page_ctx: PageContext) -> None:
73c9c30 page = HomePage()
page.on_enter(page_ctx)
_slash(page, "commit")
result = page.handle_key(key("q"))
assert result.action == PageAction.NONE
assert "q" in page.changes.commit_message
result = page.handle_key(key("Q"))
assert result.action == PageAction.NONE
assert "qQ" in page.changes.commit_message
8614508
def test_branch_filter_types_q(committed_repo: Repository) -> None:
73c9c30 ctx = PageContext(repo=committed_repo, repo_path=Path(committed_repo.path))
page = BranchPickerPage()
page.on_enter(ctx)
result = page.handle_key(key("q"))
assert result.action == PageAction.NONE
assert page._filter_state.value() == "q"
result = page.handle_key(key("Q"))
assert result.action == PageAction.NONE
assert page._filter_state.value() == "qQ"
8614508
def test_task_editor_types_q_in_input(page_ctx: PageContext) -> None:
73c9c30 page = TaskEditorPage(board_name="Tasks", task_ref=None, default_status="todo")
page.on_enter(page_ctx)
page.handle_key(key("Enter"))
result = page.handle_key(key("q"))
assert result.action == PageAction.NONE
assert page._text_state.value() == "q"
result = page.handle_key(key("Q"))
assert result.action == PageAction.NONE
assert page._text_state.value() == "qQ"
8614508
47a5dca def test_home_page_push_help(page_ctx: PageContext) -> None:
73c9c30 page = home_page()
page.on_enter(page_ctx)
result = _slash(page, "help")
assert result.action == PageAction.PUSH
assert isinstance(result.next_page, HelpPage)
47a5dca
966394d def test_home_page_actions_row(repo: Repository) -> None:
73c9c30 workdir = Path(repo.workdir or repo.path)
(workdir / "pyproject.toml").write_text(
'[tool.pgt]\nactions = [{ name = "lint", script = "lint.py" }]\n\n'
'[tool.pgt.sets.release]\nactions = [{ name = "build", script = "build.py" }]\n',
encoding="utf-8",
)
ctx = PageContext(repo=repo, repo_path=Path(repo.path))
page = home_page()
assert isinstance(page, HomePage)
page.on_enter(ctx)
assert page.has_actions
page.handle_key(key("Up"))
assert page.header_focus == "actions"
assert "open actions" in page.status_text()
page.draw(MagicMock(), Rect(0, 0, 80, 20))
opened = page.handle_key(key("Enter"))
assert opened.action == PageAction.PUSH
assert isinstance(opened.next_page, ActionsPage)
page.set_active_tab("log")
assert page.header_focus == "changes"
966394d
053f925 def test_home_page_push_actions(page_ctx: PageContext) -> None:
73c9c30 page = home_page()
page.on_enter(page_ctx)
result = _slash(page, "actions")
assert result.action == PageAction.PUSH
assert isinstance(result.next_page, ActionsPage)
053f925
ece926d def test_home_page_draws_detached_head(committed_repo: Repository) -> None:
73c9c30 commit = committed_repo.get(committed_repo.head.target)
committed_repo.set_head(commit.id)
ctx = PageContext(repo=committed_repo, repo_path=Path(committed_repo.path))
page = home_page()
page.on_enter(ctx)
page.draw(MagicMock(), Rect(0, 0, 80, 20))
assert current_branch_name(committed_repo) == "(detached)"
assert "arrows move" in page.status_text()
page.set_active_tab("log")
page.draw(MagicMock(), Rect(0, 0, 80, 20))
assert "No commits" not in page.status_text()
ece926d
def test_empty_repo_can_create_branch_and_commit(repo: Repository) -> None:
73c9c30 assert repo.head_is_unborn
assert list(repo.branches.local) == []
ctx = PageContext(repo=repo, repo_path=Path(repo.path))
page = home_page()
assert isinstance(page, HomePage)
page.on_enter(ctx)
page.draw(MagicMock(), Rect(0, 0, 80, 20))
page.set_active_tab("log")
page.draw(MagicMock(), Rect(0, 0, 80, 20))
assert page.status_text() == "No commits — arrows move"
page.set_active_tab("status")
page.draw(MagicMock(), Rect(0, 0, 80, 20))
page.handle_key(key("Up"))
page.handle_key(key("Up"))
opened = page.handle_key(key("Enter"))
assert opened.action == PageAction.PUSH
assert isinstance(opened.next_page, BranchPickerPage)
picker = opened.next_page
picker.on_enter(ctx)
picker.draw(MagicMock(), Rect(0, 0, 80, 20))
for char in "feature":
picker.handle_key(key(char))
created = picker.handle_key(key("Enter"))
assert created.action == PageAction.POP
assert current_branch_name(repo) == "feature"
workdir = repo.workdir
assert workdir is not None
(Path(workdir) / "hello.txt").write_text("hello\n", encoding="utf-8")
page.refresh()
page.draw(MagicMock(), Rect(0, 0, 80, 20))
page.handle_key(key("Down"))
_press_until(page, key("Down"), lambda hint: hint.startswith("Enter stage —"))
page.handle_key(key("Enter"))
_press_until(page, key("Up"), lambda hint: "edit" in hint.casefold())
page.handle_key(key("Enter"))
for char in "init":
page.handle_key(key(char))
page.handle_key(key("Tab"))
page.handle_key(key("Enter"))
assert not repo.head_is_unborn, page.changes.status_hint()
assert current_branch_name(repo) == "feature"
assert "feature" in repo.branches.local
assert repo.head.peel(Commit).message.splitlines()[0] == "init"
page.set_active_tab("log")
page.draw(MagicMock(), Rect(0, 0, 80, 20))
assert "No commits" not in page.status_text()
ece926d
8614508 def _press_until(page: HomePage, press: object, ready: Callable[[str], bool]) -> None:
73c9c30 for _ in range(12):
hint = page.status_text()
if ready(hint):
return
page.handle_key(press) # type: ignore[arg-type]
raise AssertionError(page.status_text())
ece926d
90ea48d def _slash(page: HomePage | NoRepoPage, command: str) -> PageResult:
73c9c30 page.handle_key(key("/"))
for char in command:
page.handle_key(key(char))
return page.handle_key(key("Enter"))
90ea48d
47a5dca def test_home_page_draw_without_context() -> None:
73c9c30 page = home_page()
page.draw(MagicMock(), Rect(0, 0, 80, 10))
47a5dca
def test_no_repo_page(tmp_path: Path) -> None:
73c9c30 page = NoRepoPage(tmp_path)
page.on_enter(PageContext(repo=MagicMock(), repo_path=tmp_path)) # type: ignore[arg-type]
page.draw(MagicMock(), Rect(0, 0, 80, 10))
assert page.handle_key(key("q")).action == PageAction.QUIT
assert _slash(page, "help").action == PageAction.PUSH
page.handle_key(key("Null"))
47a5dca
def test_help_page_scroll_and_pop() -> None:
73c9c30 page = help_page()
page.on_enter(PageContext(repo=MagicMock(), repo_path=Path("."))) # type: ignore[arg-type]
page.draw(MagicMock(), Rect(0, 0, 80, 5))
page.handle_key(key("Down"))
page.handle_key(key("Up"))
assert page.handle_key(key("q")).action == PageAction.POP
page.handle_key(key("Null"))
47a5dca
def test_branch_picker_lists_and_filters(committed_repo: Repository) -> None:
73c9c30 ctx = PageContext(repo=committed_repo, repo_path=Path(committed_repo.path))
page = branch_picker_page()
page.on_enter(ctx)
page.draw(MagicMock(), Rect(0, 0, 80, 10))
page.handle_key(key("f"))
page.handle_key(key("Backspace"))
page.handle_key(key("Down"))
page.handle_key(key("Up"))
page.handle_key(key("Enter"))
assert page.handle_key(key("q")).action == PageAction.POP
47a5dca
def test_branch_picker_checkout_branch(committed_repo: Repository) -> None:
73c9c30 commit = committed_repo.get(committed_repo.head.target)
committed_repo.branches.create("feature", commit)
ctx = PageContext(repo=committed_repo, repo_path=Path(committed_repo.path))
page = branch_picker_page()
page.on_enter(ctx)
page.handle_key(key("Down"))
page.handle_key(key("Enter"))
assert page.handle_key(key("Enter")).action == PageAction.POP
47a5dca
def test_branch_picker_create_branch(committed_repo: Repository) -> None:
73c9c30 ctx = PageContext(repo=committed_repo, repo_path=Path(committed_repo.path))
page = branch_picker_page()
page.on_enter(ctx)
page.handle_key(key("n"))
page.handle_key(key("e"))
page.handle_key(key("w"))
page.handle_key(key("Enter"))
assert "new" in committed_repo.branches.local
47a5dca
def test_branch_picker_create_invalid_shows_error(committed_repo: Repository) -> None:
73c9c30 ctx = PageContext(repo=committed_repo, repo_path=Path(committed_repo.path))
page = branch_picker_page()
page.on_enter(ctx)
page.handle_key(key(" "))
page.handle_key(key("Enter"))
assert page.status_text()
47a5dca
def test_branch_picker_draw_zero_height() -> None:
73c9c30 page = BranchPickerPage()
page.draw(MagicMock(), Rect(0, 0, 80, 0))
47a5dca
def test_projects_page_lists_repos(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
73c9c30 root = tmp_path / "root"
project = root / "demo"
project.mkdir(parents=True)
from pygit2 import init_repository
repo = init_repository(str(project), bare=False)
workdir = Path(repo.workdir)
(workdir / "README").write_text("x\n", encoding="utf-8")
repo.index.add("README")
repo.index.write()
tree = repo.index.write_tree()
sig = Signature("t", "t@example.com")
repo.create_commit("HEAD", sig, sig, "init", tree, [])
settings = tmp_path / "settings.json"
settings.write_text(json.dumps({"PROJECTROOT": str(root)}), encoding="utf-8")
monkeypatch.setenv("PYGITWEB_SETTINGS_CONFIG", str(settings))
ctx = PageContext(repo=Repository(str(project / ".git")), repo_path=project / ".git")
page = projects_page()
page.on_enter(ctx)
page.draw(MagicMock(), Rect(0, 0, 80, 10))
page.handle_key(key("Down"))
result = page.handle_key(key("Enter"))
assert result.switch_repo == project.resolve()
47a5dca
def test_projects_page_empty(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
73c9c30 settings = tmp_path / "settings.json"
settings.write_text(json.dumps({"PROJECTROOT": str(tmp_path / "empty")}), encoding="utf-8")
monkeypatch.setenv("PYGITWEB_SETTINGS_CONFIG", str(settings))
page = projects_page()
page.on_enter(PageContext(repo=MagicMock(), repo_path=tmp_path)) # type: ignore[arg-type]
assert "No projects" in page.status_text()
page.handle_key(key("Enter"))
47a5dca
def test_projects_page_draw_zero_height() -> None:
73c9c30 ProjectsPage().draw(MagicMock(), Rect(0, 0, 80, 0))
ec18fab
def _empty_tree() -> Oid:
73c9c30 return Oid(hex=EMPTY_TREE_OID_HEX)
ec18fab
def test_home_page_tasks_tab_creates_and_edits_tasks(committed_repo: Repository) -> None:
73c9c30 board_ref = f"{BOARD_REF_PREFIX}Tasks"
board = Board(_empty_tree(), board_ref, tagger="", description="Main")
task = Task(
_empty_tree(),
f"{TASK_REF_PREFIX}alpha",
"alice <alice@example.com>",
title="Alpha",
description="Do alpha",
status=Task.Status.TODO,
)
task_oid = task.write(committed_repo)
board.tasks = [str(task_oid)]
board.update_message()
board.write(committed_repo)
ctx = PageContext(repo=committed_repo, repo_path=Path(committed_repo.path))
page = home_page()
page.on_enter(ctx)
page.set_active_tab("tasks")
page.draw(MagicMock(), Rect(0, 0, 80, 20))
# Move to first status header and then to (+ New Task)
page.handle_key(key("Down"))
page.handle_key(key("Down"))
result_new = page.handle_key(key("Enter"))
assert result_new.action == PageAction.PUSH
assert isinstance(result_new.next_page, TaskEditorPage)
# Move to existing task row and open editor
page = home_page()
page.on_enter(ctx)
page.set_active_tab("tasks")
page.handle_key(key("Down"))
page.handle_key(key("Down"))
page.handle_key(key("Down"))
result_existing = page.handle_key(key("Enter"))
assert result_existing.action == PageAction.PUSH
assert isinstance(result_existing.next_page, TaskEditorPage)