pi-odd-worktree 2.1.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/README.md +54 -319
  2. package/extensions/odd-worktree-tools.ts +4182 -2539
  3. package/lib/bash-activity.js +1166 -0
  4. package/lib/bash-design.js +684 -0
  5. package/lib/bash-destructive.js +559 -0
  6. package/lib/bash-frame.js +205 -0
  7. package/lib/card-content.js +23 -0
  8. package/lib/card-style.js +175 -0
  9. package/lib/checkout-facts.js +30 -70
  10. package/lib/codegraph-card-preview.js +628 -0
  11. package/lib/codegraph-card.js +515 -0
  12. package/lib/codegraph-compaction.js +76 -0
  13. package/lib/codegraph-daemon.js +246 -0
  14. package/lib/codegraph-edit-sync.js +182 -0
  15. package/lib/codegraph-guard-target.js +337 -0
  16. package/lib/codegraph-guard.js +211 -0
  17. package/lib/codegraph-index.js +1020 -0
  18. package/lib/codegraph-lock.js +607 -0
  19. package/lib/codegraph-mcp-config.js +134 -0
  20. package/lib/codegraph-mcp.js +123 -0
  21. package/lib/codegraph-paths.js +69 -0
  22. package/lib/codegraph-prompt/explore.md +11 -0
  23. package/lib/codegraph-prompt/fix.md +6 -0
  24. package/lib/codegraph-prompt/generic.md +8 -0
  25. package/lib/codegraph-prompt/judge.md +9 -0
  26. package/lib/codegraph-prompt/orchestrator.md +17 -0
  27. package/lib/codegraph-prompt/verify.md +10 -0
  28. package/lib/codegraph-prompt/worker.md +7 -0
  29. package/lib/codegraph-role.js +55 -0
  30. package/lib/codegraph-rules.js +146 -0
  31. package/lib/codegraph-session.js +303 -383
  32. package/lib/codegraph-warmup.js +139 -0
  33. package/lib/file-card-markdown.js +141 -0
  34. package/lib/file-card-resolver.js +245 -0
  35. package/lib/file-card.js +659 -0
  36. package/lib/files-index.js +387 -0
  37. package/lib/files-insert.js +81 -0
  38. package/lib/files-orders.js +326 -0
  39. package/lib/files-picker-view.js +1421 -0
  40. package/lib/files-preview.js +338 -0
  41. package/lib/files-scope-detail.js +171 -0
  42. package/lib/files-scope-view.js +507 -0
  43. package/lib/gh-issues.js +323 -15
  44. package/lib/issue-preview.js +26 -7
  45. package/lib/issues-bash-mutation.js +206 -0
  46. package/lib/issues-cache-store.js +261 -0
  47. package/lib/issues-section.js +710 -0
  48. package/lib/issues-view.js +649 -52
  49. package/lib/odd-prompt/odd-worktrees.md +21 -0
  50. package/lib/odd-worktrees-prompt.js +10 -28
  51. package/lib/related-issues-cache.js +210 -0
  52. package/lib/shell-card.js +825 -0
  53. package/lib/skill-odd-card.js +183 -0
  54. package/lib/tui-paint.js +4 -5
  55. package/lib/warp-create-panel.js +538 -250
  56. package/lib/warp-create-picker.js +56 -9
  57. package/lib/warp-delete-confirm.js +19 -28
  58. package/lib/warp-selector-view.js +160 -82
  59. package/lib/which-command.js +90 -0
  60. package/lib/worktree-card.js +267 -897
  61. package/lib/worktree-cards.js +383 -0
  62. package/lib/worktree-create.js +287 -0
  63. package/lib/worktree-git.js +3 -3
  64. package/lib/worktree-guard.js +548 -0
  65. package/lib/worktree-hooks.js +421 -712
  66. package/lib/worktree-list.js +120 -0
  67. package/lib/worktree-registry.js +162 -879
  68. package/lib/{worktree-close-herdr.js → worktree-remove.js} +9 -11
  69. package/lib/worktree-setup-runner.js +376 -0
  70. package/lib/worktree-setup.js +140 -0
  71. package/lib/worktree-warp-busy.js +462 -85
  72. package/lib/worktree-warp-sessions.js +1 -2
  73. package/package.json +2 -2
  74. package/tools/bash-activity.test.js +1420 -0
  75. package/tools/bash-design.test.js +1093 -0
  76. package/tools/bash-destructive.test.js +139 -0
  77. package/tools/card-content.test.js +44 -0
  78. package/tools/card-style.test.js +298 -0
  79. package/tools/codegraph-card-preview.test.js +254 -0
  80. package/tools/codegraph-card.test.js +1080 -0
  81. package/tools/codegraph-compaction.test.js +378 -0
  82. package/tools/codegraph-daemon.test.js +248 -0
  83. package/tools/codegraph-edit-sync.test.js +295 -0
  84. package/tools/codegraph-guard-target.test.js +174 -0
  85. package/tools/codegraph-guard.test.js +238 -0
  86. package/tools/codegraph-index.test.js +1948 -0
  87. package/tools/codegraph-lock.test.js +932 -0
  88. package/tools/codegraph-mcp-config.test.js +181 -0
  89. package/tools/codegraph-mcp.test.js +194 -0
  90. package/tools/codegraph-paths.test.js +44 -0
  91. package/tools/codegraph-role.test.js +30 -0
  92. package/tools/codegraph-rules.test.js +250 -0
  93. package/tools/codegraph-session.test.js +660 -391
  94. package/tools/codegraph-warmup.test.js +316 -0
  95. package/tools/commands.test.js +510 -31
  96. package/tools/file-card-markdown.test.js +188 -0
  97. package/tools/file-card.test.js +1289 -0
  98. package/tools/files-bridge.test.js +575 -0
  99. package/tools/files-command.test.js +613 -0
  100. package/tools/files-index.test.js +473 -0
  101. package/tools/files-insert.test.js +139 -0
  102. package/tools/files-orders.test.js +597 -0
  103. package/tools/files-picker-view.test.js +1889 -0
  104. package/tools/files-preview.test.js +452 -0
  105. package/tools/files-scope-detail.test.js +264 -0
  106. package/tools/files-scope-view.test.js +614 -0
  107. package/tools/fixtures/card-codegraph-outputs.js +110 -0
  108. package/tools/fixtures/codegraph-cli-stub.js +16 -0
  109. package/tools/fixtures/codegraph-init-process.js +28 -0
  110. package/tools/fixtures/codegraph-lock-worker.js +117 -0
  111. package/tools/fixtures/daemon-fake.js +98 -0
  112. package/tools/fixtures/daemon-mcp-server.js +39 -0
  113. package/tools/fixtures/daemon-warmup-worker.js +12 -0
  114. package/tools/fixtures/load-extension.js +136 -3
  115. package/tools/gh-issues-feedback.test.js +31 -7
  116. package/tools/gh-issues.test.js +417 -1
  117. package/tools/issue-preview.test.js +44 -7
  118. package/tools/issues-bash-mutation.test.js +101 -0
  119. package/tools/issues-cache-store.test.js +222 -0
  120. package/tools/issues-command.test.js +1517 -67
  121. package/tools/issues-open-guard.test.js +1096 -0
  122. package/tools/issues-section.test.js +4235 -0
  123. package/tools/issues-seed-query-regression.test.js +67 -0
  124. package/tools/issues-view.test.js +1489 -60
  125. package/tools/odd-worktrees-prompt.test.js +98 -65
  126. package/tools/package-surface.test.js +431 -36
  127. package/tools/related-issues-cache.test.js +324 -0
  128. package/tools/shell-card.test.js +834 -0
  129. package/tools/skill-odd-card.test.js +200 -0
  130. package/tools/t5-session-identity.test.js +44 -22
  131. package/tools/warp-busy-guard.test.js +2565 -309
  132. package/tools/warp-create-panel.test.js +499 -74
  133. package/tools/warp-create-picker.test.js +101 -5
  134. package/tools/warp-delete-confirm.test.js +49 -15
  135. package/tools/warp-empty-session.test.js +0 -1
  136. package/tools/warp-fallback-regression.test.js +533 -0
  137. package/tools/warp-selector-view.test.js +460 -196
  138. package/tools/which-command.test.js +126 -0
  139. package/tools/worktree-card.test.js +551 -0
  140. package/tools/worktree-cards.test.js +845 -0
  141. package/tools/worktree-changes.test.js +4 -5
  142. package/tools/worktree-create.test.js +489 -0
  143. package/tools/worktree-guard.test.js +823 -0
  144. package/tools/worktree-hooks.test.js +905 -0
  145. package/tools/worktree-list.test.js +170 -0
  146. package/tools/worktree-registry.test.js +89 -230
  147. package/tools/worktree-remove-async.test.js +318 -0
  148. package/tools/worktree-setup-runner.test.js +276 -0
  149. package/tools/worktree-setup.test.js +140 -0
  150. package/tools/worktree-tools.test.js +419 -199
  151. package/lib/codegraph-commands.js +0 -165
  152. package/lib/odd-prompt/odd-worktrees.with-codegraph.md +0 -19
  153. package/lib/odd-prompt/odd-worktrees.without-codegraph.md +0 -15
  154. package/lib/worktree-model-result.js +0 -291
  155. package/tools/codegraph-commands.test.js +0 -371
  156. package/tools/herdr-free-create.test.js +0 -161
  157. package/tools/worktree-close-herdr-async.test.js +0 -204
  158. package/tools/worktree-odd-migration.test.js +0 -224
  159. package/tools/worktree-registry-cache.test.js +0 -163
  160. package/tools/worktree-registry-odd.test.js +0 -231
@@ -0,0 +1,614 @@
1
+ 'use strict';
2
+ // T3 (files-picker S9) and T10 (S20): `lib/files-scope-view.js` — the worktree
3
+ // scope step.
4
+ //
5
+ // Contract under test, as observable output only: the card frame at the
6
+ // requested width, the `main`-first rows with each scope's `±n files` count at
7
+ // the right edge, the focused scope's detail line directly above the footer,
8
+ // the one-option-per-key legend painted with the key role, the arrow/enter/esc
9
+ // keys, the `a` toggle between the registered list and every worktree of the
10
+ // clone, the mouse hover/wheel/click routes, and every degradation (no scopes, a
11
+ // narrow or short terminal, a hostile theme, a long label).
12
+
13
+ const { test, describe } = require('node:test');
14
+ const assert = require('node:assert/strict');
15
+
16
+ const { createFilesScopeView } = require('../lib/files-scope-view.js');
17
+
18
+ const NOW = 1_700_000_000_000;
19
+ const WIDTH = 80;
20
+ const HEIGHT = 12;
21
+
22
+ const strip = (value) => String(value).replace(/\x1b\[[0-9;]*m/g, '');
23
+
24
+ // ANSI-aware stand-ins for the host TUI helpers, injected exactly as the view
25
+ // takes them: nothing here is imported statically.
26
+ const helpers = {
27
+ visibleWidth: (text) => [...strip(text)].length,
28
+ truncateToWidth: (text, width, ellipsis = '…') => {
29
+ const chars = [...String(text)];
30
+ return chars.length > width
31
+ ? `${chars.slice(0, Math.max(0, width - 1)).join('')}${ellipsis}`
32
+ : String(text);
33
+ },
34
+ matchesKey: (key, name) => key === name,
35
+ isKeyRelease: (key) => key === 'release',
36
+ };
37
+
38
+ const plainTheme = {
39
+ fg: (_role, text) => String(text),
40
+ bold: (text) => String(text),
41
+ bg: (_role, text) => String(text),
42
+ };
43
+
44
+ // A theme that records which role painted each string and paints every role with
45
+ // its own SGR code, so a rendered line can be read for the ROLE behind a token.
46
+ const ROLE_SGR = { accent: 96, customMessageLabel: 33, muted: 90, dim: 2, border: 34, text: 39, error: 91 };
47
+
48
+ function recordingTheme() {
49
+ const calls = [];
50
+ const code = (role) => ROLE_SGR[role] ?? 95;
51
+ return {
52
+ calls,
53
+ fg(role, text) {
54
+ calls.push({ role, text: String(text) });
55
+ return `\x1b[${code(role)}m${text}\x1b[0m`;
56
+ },
57
+ bold(text) {
58
+ calls.push({ role: 'bold', text: String(text) });
59
+ return `\x1b[1m${text}\x1b[22m`;
60
+ },
61
+ bg(role, text) {
62
+ calls.push({ role: `bg:${role}`, text: String(text) });
63
+ return `\x1b[48;5;${code(role)}m${text}\x1b[0m`;
64
+ },
65
+ };
66
+ }
67
+
68
+ function detail(overrides = {}) {
69
+ return {
70
+ changedFiles: 2,
71
+ lastModifiedMs: NOW - 5 * 60_000,
72
+ lastCommitSubject: 'main subject',
73
+ ahead: 1,
74
+ behind: 0,
75
+ comparison: 'origin/main',
76
+ ...overrides,
77
+ };
78
+ }
79
+
80
+ function scope(overrides = {}) {
81
+ return { root: '/repo', label: 'main', kind: 'main', lastModifiedMs: 100, detail: detail(), ...overrides };
82
+ }
83
+
84
+ const WORKTREE = scope({
85
+ root: '/w/feat-x',
86
+ label: 'feat/x · files-picker',
87
+ kind: 'worktree',
88
+ lastModifiedMs: 900,
89
+ detail: detail({
90
+ lastCommitSubject: 'add the scope step',
91
+ changedFiles: 3,
92
+ lastModifiedMs: NOW - 2 * 60_000,
93
+ ahead: 2,
94
+ behind: 1,
95
+ comparison: 'main',
96
+ }),
97
+ });
98
+
99
+ const SCOPES = [WORKTREE, scope()];
100
+
101
+ // S20: a checkout Git knows but this package did not create, so it is only on the
102
+ // `a` list.
103
+ const UNREGISTERED = scope({
104
+ root: '/w/loose',
105
+ label: 'loose · other',
106
+ kind: 'worktree',
107
+ lastModifiedMs: 500,
108
+ detail: detail({ lastCommitSubject: 'an unrelated checkout', changedFiles: 1, lastModifiedMs: NOW - 60 * 60_000, ahead: 0, behind: 0, comparison: 'main' }),
109
+ });
110
+
111
+ function mount(options = {}) {
112
+ const selected = [];
113
+ const closes = [];
114
+ let renders = 0;
115
+ const theme = options.theme === undefined ? plainTheme : options.theme;
116
+ const view = createFilesScopeView({
117
+ scopes: options.scopes === undefined ? SCOPES : options.scopes,
118
+ ...(options.allScopes === undefined ? {} : { allScopes: options.allScopes }),
119
+ theme,
120
+ rowsAvailable: options.rowsAvailable === undefined ? HEIGHT : options.rowsAvailable,
121
+ helpers: options.helpers || helpers,
122
+ now: options.now === undefined ? NOW : options.now,
123
+ onSelect: (chosen) => {
124
+ selected.push(chosen);
125
+ if (typeof options.onSelect === 'function') options.onSelect(chosen);
126
+ },
127
+ onClose: () => {
128
+ closes.push(true);
129
+ if (typeof options.onClose === 'function') options.onClose();
130
+ },
131
+ requestRender: () => {
132
+ renders += 1;
133
+ if (typeof options.requestRender === 'function') options.requestRender();
134
+ },
135
+ });
136
+ return { view, theme, selected, closes, renderCount: () => renders };
137
+ }
138
+
139
+ function rowsOf(view, width = WIDTH) {
140
+ return view.render(width).map(strip);
141
+ }
142
+
143
+ // A mouse event local to the last painted card.
144
+ function mouse(overrides = {}) {
145
+ return { type: 'move', button: 'none', x: 5, y: 3, width: WIDTH, height: HEIGHT, ...overrides };
146
+ }
147
+
148
+ describe('frame and geometry (S9)', () => {
149
+ test('draws the card frame, title and subtitle at the requested width', () => {
150
+ const { view } = mount();
151
+ const painted = view.render(WIDTH);
152
+ assert.equal(painted.length, HEIGHT);
153
+ for (const row of painted) assert.equal(helpers.visibleWidth(row), WIDTH);
154
+ const rows = painted.map(strip);
155
+ assert.ok(rows[0].startsWith('╭─ ✿ Files · Choose a scope'), rows[0]);
156
+ assert.ok(rows[0].endsWith('╮'));
157
+ assert.ok(rows[rows.length - 1].startsWith('╰'));
158
+ assert.ok(rows[rows.length - 1].endsWith('╯'));
159
+ });
160
+
161
+ test('a terminal too narrow or too short paints one fitted line', () => {
162
+ const { view } = mount({ rowsAvailable: 20 });
163
+ const narrow = view.render(10).map(strip);
164
+ assert.equal(narrow.length, 1);
165
+ assert.equal(helpers.visibleWidth(narrow[0]), 10);
166
+ assert.ok(narrow[0].startsWith('Files —'));
167
+
168
+ const short = view.render(WIDTH).map(strip);
169
+ assert.equal(mount({ rowsAvailable: 5 }).view.render(WIDTH).length, 1);
170
+ assert.equal(short.length, 20);
171
+ });
172
+
173
+ test('the height follows rowsAvailable, including its function form, capped at 24', () => {
174
+ assert.equal(mount({ rowsAvailable: 8 }).view.render(WIDTH).length, 8);
175
+ assert.equal(mount({ rowsAvailable: () => 9 }).view.render(WIDTH).length, 9);
176
+ assert.equal(mount({ rowsAvailable: 40 }).view.render(WIDTH).length, 24);
177
+ assert.deepEqual(mount({ rowsAvailable: () => { throw new Error('no rows'); } }).view.render(WIDTH), []);
178
+ });
179
+
180
+ test('an absent rows source paints nothing', () => {
181
+ const view = createFilesScopeView({ scopes: SCOPES, theme: plainTheme, helpers, now: NOW });
182
+ assert.deepEqual(view.render(WIDTH), []);
183
+ assert.deepEqual(view.render(0), []);
184
+ });
185
+ });
186
+
187
+ describe('scope rows (S9)', () => {
188
+ test('main is first and the worktrees follow by recency', () => {
189
+ const { view } = mount({
190
+ rowsAvailable: 14,
191
+ scopes: [
192
+ scope({ root: '/w/new', label: 'newer', kind: 'worktree', lastModifiedMs: 900 }),
193
+ scope({ root: '/w/old', label: 'older', kind: 'worktree', lastModifiedMs: 10 }),
194
+ scope(),
195
+ ],
196
+ });
197
+ const rows = rowsOf(view);
198
+ assert.ok(rows[2].includes('main'), rows[2]);
199
+ assert.ok(rows[3].includes('newer'), rows[3]);
200
+ assert.ok(rows[4].includes('older'), rows[4]);
201
+ });
202
+
203
+ test('each row paints its ±n files count at the right edge, singular at one', () => {
204
+ const { view } = mount();
205
+ const rows = rowsOf(view);
206
+ assert.ok(rows[2].endsWith('±2 files │'), rows[2]);
207
+ assert.ok(rows[3].endsWith('±3 files │'), rows[3]);
208
+
209
+ const one = mount({ scopes: [scope({ detail: detail({ changedFiles: 1 }) })] });
210
+ assert.ok(rowsOf(one.view)[2].endsWith('±1 file │'));
211
+ });
212
+
213
+ test('a scope with no uncommitted files paints no count', () => {
214
+ for (const detailValue of [{ changedFiles: 0 }, {}, undefined]) {
215
+ const { view } = mount({ scopes: [scope({ detail: detailValue })] });
216
+ const row = rowsOf(view)[2];
217
+ assert.ok(!row.includes('±'), row);
218
+ assert.ok(row.endsWith(' │'));
219
+ }
220
+ });
221
+
222
+ test('the focused row carries the band and a marker of constant width', () => {
223
+ const theme = recordingTheme();
224
+ const { view } = mount({ theme });
225
+ const rows = rowsOf(view);
226
+ assert.ok(rows[2].includes('▸ main'), rows[2]);
227
+ assert.ok(!rows[3].includes('▸'));
228
+ // Both rows start their label at the same column: the marker never shifts it.
229
+ assert.equal(rows[2].indexOf('main'), rows[3].indexOf('feat/x · files-picker'));
230
+ const band = theme.calls.find((call) => call.role === 'bg:selectedBg');
231
+ assert.ok(band && band.text.includes('main'), 'the focused row is painted as a band');
232
+ assert.ok(theme.calls.some((call) => call.role === 'text' && call.text === 'feat/x · files-picker'));
233
+ });
234
+
235
+ test('a long label is clipped with an ellipsis and the count keeps its right edge', () => {
236
+ const long = scope({
237
+ label: 'a-very-long-worktree-branch-name · an-equally-long-directory-name',
238
+ detail: detail({ changedFiles: 12 }),
239
+ });
240
+ const { view } = mount({ scopes: [long] });
241
+ const row = rowsOf(view)[2];
242
+ assert.ok(row.includes('…'), row);
243
+ assert.ok(row.endsWith('±12 files │'), row);
244
+ assert.equal(helpers.visibleWidth(row), WIDTH);
245
+ });
246
+
247
+ test('a terminal sequence and control characters in a label never reach the row', () => {
248
+ const { view } = mount({ scopes: [scope({ label: 'evil\x1b[31mred\x07 \x00name' })] });
249
+ const row = view.render(WIDTH)[2];
250
+ assert.ok(!row.includes('\x1b'), 'no escape sequence is painted');
251
+ assert.ok(!row.includes('\x07') && !row.includes('\x00'));
252
+ assert.ok(strip(row).includes('evilred'), strip(row));
253
+ assert.ok(strip(row).includes('name'), strip(row));
254
+ });
255
+ });
256
+
257
+ describe('the focused detail line (S9)', () => {
258
+ test('sits directly above the footer, subject first and then the data', () => {
259
+ const { view } = mount();
260
+ const rows = rowsOf(view);
261
+ const detailRow = rows[rows.length - 3];
262
+ const footer = rows[rows.length - 2];
263
+ assert.equal(detailRow, `│ ${'main subject · ±2 files · 5m ago · ↑1 origin/main'}${' '.repeat(WIDTH - 4 - 'main subject · ±2 files · 5m ago · ↑1 origin/main'.length)} │`);
264
+ assert.ok(footer.includes('↑/↓ move'), footer);
265
+ });
266
+
267
+ test('it follows the focused scope as the selection moves', () => {
268
+ const { view } = mount();
269
+ assert.ok(rowsOf(view)[HEIGHT - 3].includes('main subject'));
270
+ view.handleInput('down');
271
+ const moved = rowsOf(view);
272
+ assert.ok(moved[HEIGHT - 3].includes('add the scope step · ±3 files · 2m ago · ↑2 main · ↓1 main'), moved[HEIGHT - 3]);
273
+ });
274
+
275
+ test('an absent or empty detail keeps a blank row of the same geometry', () => {
276
+ const { view } = mount({ scopes: [scope({ detail: undefined })] });
277
+ const row = view.render(WIDTH)[HEIGHT - 3];
278
+ assert.equal(strip(row), `│${' '.repeat(WIDTH - 2)}│`);
279
+ });
280
+
281
+ test('zero or absent fields are not painted on it', () => {
282
+ const { view } = mount({
283
+ scopes: [scope({ detail: { changedFiles: 0, lastModifiedMs: 0, ahead: 0, behind: 0, comparison: 'main' } })],
284
+ });
285
+ const row = rowsOf(view)[HEIGHT - 3];
286
+ assert.ok(!row.includes('±0'));
287
+ assert.ok(!row.includes('↑0') && !row.includes('↓0'));
288
+ assert.ok(!row.includes('main'));
289
+ });
290
+ });
291
+
292
+ describe('the footer legend (S9)', () => {
293
+ test('names one option per key: move, open, close', () => {
294
+ const { view } = mount();
295
+ const footer = rowsOf(view)[HEIGHT - 2];
296
+ assert.ok(footer.includes('↑/↓ move · enter open · esc close'), footer);
297
+ assert.ok(!footer.includes('ctrl+k') && !footer.includes('ctrl+j'));
298
+ assert.ok(!footer.includes('ctrl+c'));
299
+ });
300
+
301
+ test('paints each key in the key role and its description muted', () => {
302
+ const theme = recordingTheme();
303
+ const { view } = mount({ theme });
304
+ view.render(WIDTH);
305
+ for (const [keyText, rest] of [['↑/↓', ' move'], ['enter', ' open'], ['esc', ' close']]) {
306
+ assert.ok(theme.calls.some((call) => call.role === 'accent' && call.text === keyText), keyText);
307
+ assert.ok(theme.calls.some((call) => call.role === 'muted' && call.text === rest), rest);
308
+ }
309
+ assert.ok(theme.calls.some((call) => call.role === 'bold' && strip(call.text) === '↑/↓'));
310
+ });
311
+ });
312
+
313
+ describe('the two lists and the `a` key (S20)', () => {
314
+ test('the step opens on the registered rows and the footer names the list `a` switches to', () => {
315
+ const { view } = mount({ scopes: [scope()], allScopes: [scope(), UNREGISTERED] });
316
+ const registered = rowsOf(view);
317
+ assert.ok(registered[2].includes('main'), registered[2]);
318
+ assert.ok(!registered.join('\n').includes('loose · other'), 'the default rows are the registered ones');
319
+ assert.ok(registered[HEIGHT - 2].includes('a all'), registered[HEIGHT - 2]);
320
+ assert.ok(!registered[HEIGHT - 2].includes('a registered'), 'and it names where the key goes, not where it is');
321
+
322
+ view.handleInput('a');
323
+ const all = rowsOf(view);
324
+ assert.ok(all.join('\n').includes('loose · other'), 'every worktree of the clone is offered');
325
+ assert.ok(all[2].includes('main'), 'the main checkout stays first');
326
+ assert.ok(all.findIndex((row) => row.includes('loose · other')) > all.findIndex((row) => row.includes('main')),
327
+ 'and every worktree follows it');
328
+ assert.ok(all[HEIGHT - 2].includes('a registered'), all[HEIGHT - 2]);
329
+ assert.ok(!all[HEIGHT - 2].includes('a all'), 'the footer names the way back');
330
+
331
+ view.handleInput('a');
332
+ assert.ok(!rowsOf(view).join('\n').includes('loose · other'), 'the key switches back');
333
+ });
334
+
335
+ test('a scope both lists carry keeps the cursor; one they do not drops it on the first row', () => {
336
+ const toggled = mount({ scopes: [scope(), WORKTREE], allScopes: [scope(), WORKTREE, UNREGISTERED] });
337
+ toggled.view.handleInput('down');
338
+ assert.ok(rowsOf(toggled.view)[3].includes('▸ feat/x · files-picker'), 'the registered worktree is focused');
339
+ toggled.view.handleInput('a');
340
+ assert.ok(rowsOf(toggled.view)[3].includes('▸ feat/x · files-picker'), 'the same root keeps the cursor on the wider list');
341
+ toggled.view.handleInput('a');
342
+ assert.ok(rowsOf(toggled.view)[3].includes('▸ feat/x · files-picker'), 'and on the way back');
343
+
344
+ const dropped = mount({ scopes: [scope(), WORKTREE], allScopes: [scope(), WORKTREE, UNREGISTERED] });
345
+ dropped.view.handleInput('a');
346
+ dropped.view.handleInput('down');
347
+ dropped.view.handleInput('down');
348
+ assert.ok(rowsOf(dropped.view)[4].includes('loose · other'), 'the unregistered checkout is focused');
349
+ dropped.view.handleInput('a');
350
+ assert.ok(rowsOf(dropped.view)[2].includes('▸ main'), 'a root the other list does not carry leaves the first row');
351
+ });
352
+
353
+ test('the scope answered is the original object of the list on screen', () => {
354
+ const { view, selected } = mount({ scopes: [scope()], allScopes: [scope(), UNREGISTERED] });
355
+ view.handleInput('a');
356
+ view.handleInput('down');
357
+ view.handleInput('enter');
358
+ assert.equal(selected.length, 1);
359
+ assert.equal(selected[0], UNREGISTERED, 'the `a` list answers its own scope object');
360
+ });
361
+
362
+ test('the focused scope’s detail line follows the list on screen', () => {
363
+ const { view } = mount({ scopes: [scope()], allScopes: [scope(), UNREGISTERED] });
364
+ view.handleInput('a');
365
+ view.handleInput('down');
366
+ assert.ok(rowsOf(view)[HEIGHT - 3].includes('an unrelated checkout'), rowsOf(view)[HEIGHT - 3]);
367
+ });
368
+
369
+ test('the toggle paints its key in the key role and its destination muted', () => {
370
+ const theme = recordingTheme();
371
+ const { view } = mount({ theme, scopes: [scope()], allScopes: [scope(), UNREGISTERED] });
372
+ view.render(WIDTH);
373
+ assert.ok(theme.calls.some((call) => call.role === 'accent' && call.text === 'a'));
374
+ assert.ok(theme.calls.some((call) => call.role === 'muted' && call.text === ' all'));
375
+ view.handleInput('a');
376
+ theme.calls.length = 0;
377
+ view.render(WIDTH);
378
+ assert.ok(theme.calls.some((call) => call.role === 'muted' && call.text === ' registered'));
379
+ });
380
+
381
+ test('with no second list the key is not offered and does nothing', () => {
382
+ const { view, selected, closes } = mount();
383
+ const footer = rowsOf(view)[HEIGHT - 2];
384
+ assert.ok(!footer.includes('a all') && !footer.includes('a registered'), footer);
385
+ view.handleInput('a');
386
+ assert.ok(rowsOf(view)[2].includes('▸ main'), 'the rows did not move');
387
+ assert.deepEqual(selected, []);
388
+ assert.deepEqual(closes, []);
389
+ });
390
+ });
391
+
392
+ describe('keys (S9)', () => {
393
+ test('down/ctrl+j and up/ctrl+k move the focus and stop at the ends', () => {
394
+ const { view, renderCount } = mount();
395
+ view.handleInput('down');
396
+ assert.ok(rowsOf(view)[HEIGHT - 3].includes('add the scope step'));
397
+ view.handleInput('down');
398
+ assert.ok(rowsOf(view)[HEIGHT - 3].includes('add the scope step'));
399
+ view.handleInput('ctrl+k');
400
+ assert.ok(rowsOf(view)[HEIGHT - 3].includes('main subject'));
401
+ view.handleInput('ctrl+k');
402
+ assert.ok(rowsOf(view)[HEIGHT - 3].includes('main subject'));
403
+ assert.ok(renderCount() >= 2, 'a move requests a repaint');
404
+ });
405
+
406
+ test('enter and return select the focused scope once', () => {
407
+ const { view, selected } = mount();
408
+ view.handleInput('down');
409
+ view.handleInput('enter');
410
+ assert.equal(selected.length, 1);
411
+ assert.equal(selected[0], WORKTREE);
412
+ view.handleInput('enter');
413
+ view.handleInput('down');
414
+ assert.equal(selected.length, 1, 'the step is used once');
415
+ });
416
+
417
+ test('escape and ctrl+c close without selecting', () => {
418
+ const escaped = mount();
419
+ escaped.view.handleInput('escape');
420
+ assert.deepEqual(escaped.selected, []);
421
+ assert.equal(escaped.closes.length, 1);
422
+
423
+ const ctrlC = mount();
424
+ ctrlC.view.handleInput('ctrl+c');
425
+ assert.equal(ctrlC.closes.length, 1);
426
+ assert.deepEqual(ctrlC.selected, []);
427
+ });
428
+
429
+ test('a key release is ignored', () => {
430
+ const { view, selected } = mount();
431
+ view.handleInput('release');
432
+ assert.deepEqual(selected, []);
433
+ view.handleInput('enter');
434
+ assert.equal(selected.length, 1);
435
+ });
436
+
437
+ test('with no scopes enter does nothing and escape still closes', () => {
438
+ const { view, selected, closes } = mount({ scopes: [] });
439
+ view.handleInput('enter');
440
+ assert.deepEqual(selected, []);
441
+ view.handleInput('escape');
442
+ assert.equal(closes.length, 1);
443
+ });
444
+
445
+ test('a throwing select hands the close back and rethrows', () => {
446
+ const { view, closes } = mount({
447
+ onSelect: () => { throw new Error('handoff failed'); },
448
+ });
449
+ assert.throws(() => view.handleInput('enter'), /handoff failed/);
450
+ assert.equal(closes.length, 1);
451
+ });
452
+ });
453
+
454
+ describe('mouse (S9)', () => {
455
+ test('hover repaints the hovered row in the key role and moving off clears it', () => {
456
+ const theme = recordingTheme();
457
+ const { view } = mount({ theme });
458
+ view.render(WIDTH);
459
+ const hovered = view.handleMouse(mouse({ y: 3 }));
460
+ assert.deepEqual(hovered, { handled: true, render: true });
461
+ view.render(WIDTH);
462
+ assert.ok(theme.calls.some((call) => call.role === 'accent' && call.text === 'feat/x · files-picker'));
463
+
464
+ const callsBefore = theme.calls.length;
465
+ const cleared = view.handleMouse(mouse({ y: 1 }));
466
+ // The row that was hovered has to be repainted plain again.
467
+ assert.deepEqual(cleared, { handled: true, render: true });
468
+ view.render(WIDTH);
469
+ assert.ok(!theme.calls.slice(callsBefore).some((call) => call.role === 'accent' && call.text === 'feat/x · files-picker'));
470
+ });
471
+
472
+ test('the wheel moves the focus and never selects', () => {
473
+ const { view, selected } = mount();
474
+ view.render(WIDTH);
475
+ assert.deepEqual(view.handleMouse(mouse({ y: 3, type: 'wheel', wheelDelta: 1 })), { handled: true, render: true });
476
+ assert.ok(rowsOf(view)[HEIGHT - 3].includes('add the scope step'));
477
+ view.render(WIDTH);
478
+ view.handleMouse(mouse({ y: 3, type: 'wheel', wheelDelta: -1 }));
479
+ assert.ok(rowsOf(view)[HEIGHT - 3].includes('main subject'));
480
+ assert.deepEqual(selected, []);
481
+ });
482
+
483
+ test('a click selects the row under it, even when another row had the focus', () => {
484
+ const { view, selected } = mount();
485
+ view.render(WIDTH);
486
+ const result = view.handleMouse(mouse({ x: 4, y: 3, type: 'click', button: 'left' }));
487
+ assert.deepEqual(result, { handled: true, render: true });
488
+ assert.equal(selected.length, 1);
489
+ assert.equal(selected[0], WORKTREE);
490
+ });
491
+
492
+ test('a press claims the gesture without selecting or repainting', () => {
493
+ const { view, selected } = mount();
494
+ view.render(WIDTH);
495
+ assert.deepEqual(view.handleMouse(mouse({ y: 3, type: 'press', button: 'left' })), { handled: true, render: false });
496
+ assert.deepEqual(selected, []);
497
+ });
498
+
499
+ test('events outside the body and events from another geometry are not handled', () => {
500
+ const { view } = mount();
501
+ view.render(WIDTH);
502
+ assert.equal(view.handleMouse(mouse({ x: 0, y: 3, type: 'click', button: 'left' })), undefined);
503
+ assert.equal(view.handleMouse(mouse({ y: 0, type: 'click', button: 'left' })), undefined);
504
+ assert.equal(view.handleMouse(mouse({ y: 40, type: 'click', button: 'left' })), undefined);
505
+ assert.equal(view.handleMouse(mouse({ width: 60, type: 'click', button: 'left' })), undefined);
506
+ assert.equal(view.handleMouse(null), undefined);
507
+ });
508
+
509
+ test('invalidate drops the hover and the hit geometry', () => {
510
+ const { view } = mount();
511
+ view.render(WIDTH);
512
+ view.handleMouse(mouse({ y: 3 }));
513
+ view.invalidate();
514
+ assert.equal(view.handleMouse(mouse({ y: 3, type: 'click', button: 'left' })), undefined);
515
+ });
516
+
517
+ test('a completed step ignores every later event', () => {
518
+ const { view, selected, closes } = mount();
519
+ view.render(WIDTH);
520
+ view.handleInput('escape');
521
+ assert.equal(view.handleMouse(mouse({ y: 3, type: 'click', button: 'left' })), undefined);
522
+ view.handleInput('enter');
523
+ assert.deepEqual(selected, []);
524
+ assert.equal(closes.length, 1);
525
+ // The step still paints a stable frame if the host asks once more; it only
526
+ // stops taking input.
527
+ assert.equal(view.render(WIDTH).length, HEIGHT);
528
+ });
529
+ });
530
+
531
+ describe('degradations (S9)', () => {
532
+ test('with no scopes the card says so explicitly', () => {
533
+ const theme = recordingTheme();
534
+ const { view } = mount({ scopes: [], theme });
535
+ const rows = rowsOf(view);
536
+ const empty = rows.find((row) => row.includes('No checkouts to choose from.'));
537
+ assert.ok(empty, 'the empty state is painted');
538
+ assert.ok(theme.calls.some((call) => call.role === 'dim' && call.text.includes('No checkouts to choose from.')));
539
+ });
540
+
541
+ test('a hostile theme still paints every row at the requested width', () => {
542
+ const hostile = {
543
+ fg() { throw new Error('no colour'); },
544
+ bold() { throw new Error('no bold'); },
545
+ bg() { throw new Error('no band'); },
546
+ };
547
+ const { view } = mount({ theme: hostile });
548
+ const rows = view.render(WIDTH);
549
+ assert.equal(rows.length, HEIGHT);
550
+ for (const row of rows) assert.equal(helpers.visibleWidth(row), WIDTH);
551
+ assert.ok(strip(rows[2]).includes('main'));
552
+ });
553
+
554
+ test('an absent theme leaves the text plain', () => {
555
+ const view = createFilesScopeView({ scopes: SCOPES, rowsAvailable: HEIGHT, helpers, now: NOW });
556
+ const rows = rowsOf(view);
557
+ assert.ok(rows[2].includes('main'));
558
+ assert.ok(rows[HEIGHT - 2].includes('esc close'));
559
+ });
560
+
561
+ test('a hostile key matcher falls through to the spelled key name', () => {
562
+ const { view, selected } = mount({
563
+ helpers: {
564
+ ...helpers,
565
+ matchesKey() { throw new Error('no matcher'); },
566
+ },
567
+ });
568
+ view.handleInput('down');
569
+ view.handleInput('enter');
570
+ assert.equal(selected.length, 1);
571
+ assert.equal(selected[0], WORKTREE);
572
+ });
573
+ });
574
+
575
+ describe('the list window (S9)', () => {
576
+ function manyScopes(count) {
577
+ const list = [scope()];
578
+ for (let index = 0; index < count; index += 1) {
579
+ list.push(scope({
580
+ root: `/w/${index}`,
581
+ label: `worktree-${index}`,
582
+ kind: 'worktree',
583
+ lastModifiedMs: 1000 - index,
584
+ }));
585
+ }
586
+ return list;
587
+ }
588
+
589
+ test('more scopes than rows stay one window with more-indicators', () => {
590
+ const { view } = mount({ scopes: manyScopes(8), rowsAvailable: 10 });
591
+ const rows = rowsOf(view);
592
+ assert.ok(rows.some((row) => row.includes('↓') && row.includes('more')), rows.join('\n'));
593
+ assert.ok(!rows.some((row) => row.includes('↑') && row.includes('more')));
594
+ assert.ok(rows[2].includes('▸ main'));
595
+ });
596
+
597
+ test('moving to the end flips the indicator without leaving the window', () => {
598
+ const { view } = mount({ scopes: manyScopes(8), rowsAvailable: 10 });
599
+ for (let step = 0; step < 8; step += 1) {
600
+ view.handleInput('down');
601
+ view.render(WIDTH);
602
+ }
603
+ const rows = rowsOf(view);
604
+ assert.ok(rows.some((row) => row.includes('↑') && row.includes('more')), rows.join('\n'));
605
+ assert.ok(rows.some((row) => row.includes('▸')), 'the focused row is inside the window');
606
+ });
607
+
608
+ test('a body too short for indicators paints only reachable rows', () => {
609
+ const { view } = mount({ scopes: manyScopes(5), rowsAvailable: 8 });
610
+ const rows = rowsOf(view);
611
+ assert.ok(!rows.some((row) => row.includes('more')), rows.join('\n'));
612
+ assert.equal(rows.length, 8);
613
+ });
614
+ });