1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from __future__ import annotations
import json
from collections.abc import Callable
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from pygit2 import Commit, Oid, Repository, Signature
from pygittools.tasks import BOARD_REF_PREFIX, EMPTY_TREE_OID_HEX, TASK_REF_PREFIX, Board, Task
from pygittools.tui.branches import current_branch_name
from pygittools.tui.pages.branches import BranchPickerPage, branch_picker_page
from pygittools.tui.pages.help import HelpPage, help_page
from pygittools.tui.pages.home import HomePage, home_page
from pygittools.tui.pages.no_repo import NoRepoPage
from pygittools.tui.pages.projects import ProjectsPage, projects_page
from pygittools.tui.pages.task_editor import TaskEditorPage
from pygittools.tui.types import PageAction, PageContext
from pyratatui import Rect
from pygittools.tui.keys import key
def test_home_page_on_enter_and_quit(page_ctx: PageContext) -> None:
page = home_page()
page.on_enter(page_ctx)
assert page.status_text()
result = page.handle_key(key("q"))
assert result.action == PageAction.QUIT
def test_home_page_draw_and_tabs(committed_repo: Repository) -> None:
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_header_focus("repo")
page.draw(MagicMock(), Rect(0, 0, 80, 20))
page.handle_key(key("Null"))
def test_home_page_switch_to_input(page_ctx: PageContext) -> None:
page = home_page()
page.on_enter(page_ctx)
page.handle_key(key("a"))
assert "Esc" in page.status_text()
def test_home_page_push_help(page_ctx: PageContext) -> None:
page = home_page()
page.on_enter(page_ctx)
result = page.handle_key(key("?"))
assert result.action == PageAction.PUSH
assert isinstance(result.next_page, HelpPage)
def test_home_page_draws_detached_head(committed_repo: Repository) -> None:
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 "j/k 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()
def test_empty_repo_can_create_branch_and_commit(repo: Repository) -> None:
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 — j/k move"
page.set_active_tab("status")
page.draw(MagicMock(), Rect(0, 0, 80, 20))
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.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: "type message" in hint.casefold())
for char in "init":
page.handle_key(key(char))
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()
def _press_until(page: HomePage, press: object, ready: Callable[[str], bool]) -> None:
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())
def test_home_page_draw_without_context() -> None:
page = home_page()
page.draw(MagicMock(), Rect(0, 0, 80, 10))
def test_no_repo_page(tmp_path: Path) -> None:
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 page.handle_key(key("h")).action == PageAction.PUSH
page.handle_key(key("Null"))
def test_help_page_scroll_and_pop() -> None:
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"))
def test_branch_picker_lists_and_filters(committed_repo: Repository) -> None:
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
def test_branch_picker_checkout_branch(committed_repo: Repository) -> None:
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
def test_branch_picker_create_branch(committed_repo: Repository) -> None:
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
def test_branch_picker_create_invalid_shows_error(committed_repo: Repository) -> None:
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()
def test_branch_picker_draw_zero_height() -> None:
page = BranchPickerPage()
page.draw(MagicMock(), Rect(0, 0, 80, 0))
def test_projects_page_lists_repos(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
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()
def test_projects_page_empty(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
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"))
def test_projects_page_draw_zero_height() -> None:
ProjectsPage().draw(MagicMock(), Rect(0, 0, 80, 0))
def _empty_tree() -> Oid:
return Oid(hex=EMPTY_TREE_OID_HEX)
def test_home_page_tasks_tab_creates_and_edits_tasks(committed_repo: Repository) -> None:
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)