pi-odd-worktree 3.9.2 → 3.10.0

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 (39) hide show
  1. package/README.md +63 -40
  2. package/extensions/odd-worktree-tools.ts +859 -12
  3. package/lib/gh-issues.js +65 -10
  4. package/lib/gh-prs.js +2011 -0
  5. package/lib/issue-preview.js +70 -4
  6. package/lib/issues-cache-store.js +4 -1
  7. package/lib/issues-section.js +346 -23
  8. package/lib/issues-view.js +116 -5
  9. package/lib/pr-preview.js +353 -0
  10. package/lib/prs-rail-store.js +317 -0
  11. package/lib/prs-view.js +2896 -0
  12. package/package.json +1 -1
  13. package/tools/files-bridge.test.js +2 -2
  14. package/tools/files-command.test.js +1 -1
  15. package/tools/fixtures/gh-pr-connection-page.json +84 -0
  16. package/tools/fixtures/gh-pr-metadata.json +29 -0
  17. package/tools/fixtures/gh-pr-rail.json +61 -0
  18. package/tools/fixtures/gh-pr-search-page.json +82 -0
  19. package/tools/fixtures/gh-pr-view.json +159 -0
  20. package/tools/gh-issues.test.js +4 -2
  21. package/tools/gh-prs.test.js +1828 -0
  22. package/tools/issue-preview.test.js +119 -0
  23. package/tools/issues-cache-store.test.js +2 -2
  24. package/tools/issues-command.test.js +10 -3
  25. package/tools/issues-open-guard.test.js +15 -0
  26. package/tools/issues-section.test.js +618 -9
  27. package/tools/issues-view.test.js +297 -0
  28. package/tools/package-surface.test.js +18 -4
  29. package/tools/pr-preview.test.js +281 -0
  30. package/tools/prs-command.test.js +1280 -0
  31. package/tools/prs-rail-store.test.js +343 -0
  32. package/tools/prs-t10-safety.test.js +326 -0
  33. package/tools/prs-t12-safety.test.js +348 -0
  34. package/tools/prs-t13-independent.test.js +378 -0
  35. package/tools/prs-t14-safety.test.js +340 -0
  36. package/tools/prs-t15-independent.test.js +259 -0
  37. package/tools/prs-t18-safety.test.js +217 -0
  38. package/tools/prs-t7-safety.test.js +127 -0
  39. package/tools/prs-view.test.js +1462 -0
@@ -0,0 +1,1462 @@
1
+ 'use strict';
2
+ // T2 (prs-panel S1,S2,S3,S6,S7): lib/prs-view.js — the pure pull request panel,
3
+ // cloned from lib/issues-view.js: the same framed reader (search row, tabs, note
4
+ // row, list | pane, one footer legend), the same server-page and search seams,
5
+ // the same capability-gated editors and the same error handling — with the
6
+ // pull request buckets, the merge mode, the linked-issues editor and the two
7
+ // cross-surface seams the contract adds (`actions.insert` on Enter,
8
+ // `actions.openLinked` on a click over one linked issue number).
9
+ //
10
+ // Everything asserted here is observable output: the painted frame, the state a
11
+ // key leaves behind, and the exact arguments the injected seams receive.
12
+
13
+ const { test, describe } = require('node:test');
14
+ const assert = require('node:assert/strict');
15
+
16
+ const { createPrsView } = require('../lib/prs-view.js');
17
+ const layout = require('../lib/view-layout.js');
18
+
19
+ function strip(value) {
20
+ return String(value).replace(/\x1b\[[0-9;]*m/g, '');
21
+ }
22
+
23
+ const helpers = {
24
+ visibleWidth: (text) => [...strip(text)].length,
25
+ truncateToWidth: (text, width) => {
26
+ const chars = [...String(text)];
27
+ return chars.length > width ? chars.slice(0, Math.max(0, width - 1)).join('') + '…' : String(text);
28
+ },
29
+ matchesKey: (key, name) => key === name,
30
+ isKeyRelease: (key) => key === 'release',
31
+ };
32
+
33
+ const plainTheme = { fg: (_role, text) => String(text), bg: (_role, text) => String(text) };
34
+ const tick = () => new Promise((resolve) => setTimeout(resolve, 0));
35
+
36
+ // Every permission a pull request surface gates on, confirmed true. A fixture
37
+ // that omits it is a repository where nothing may be written (S2).
38
+ const EDITABLE = {
39
+ canEditLabels: true,
40
+ canCreateLabels: true,
41
+ canEditAssignees: true,
42
+ canClosePullRequests: true,
43
+ canMerge: true,
44
+ canEditLinkedIssues: true,
45
+ };
46
+
47
+ const NOW = Date.now();
48
+
49
+ function pullRequest(number, overrides = {}) {
50
+ return {
51
+ number,
52
+ title: `pull request ${number}`,
53
+ state: 'open',
54
+ isDraft: false,
55
+ author: 'octo',
56
+ labels: [{ name: 'bug' }],
57
+ assignees: [{ login: 'ana' }],
58
+ createdAt: new Date(NOW - 3600_000).toISOString(),
59
+ updatedAt: new Date(NOW - 1800_000).toISOString(),
60
+ commentsCount: 0,
61
+ reactions: { plusOne: 2 },
62
+ url: `https://example.test/p/${number}`,
63
+ reviewers: [{ login: 'cara', state: 'APPROVED' }],
64
+ linkedIssues: [12, 14],
65
+ additions: 40,
66
+ deletions: 5,
67
+ changedFiles: 3,
68
+ commits: 7,
69
+ committedAt: new Date(NOW - 7200_000).toISOString(),
70
+ checks: { total: 5, passed: 4, failed: 1, pending: 0, state: 'FAILURE', failing: ['build'] },
71
+ mergeable: true,
72
+ mergeStateStatus: 'CLEAN',
73
+ reviewDecision: 'APPROVED',
74
+ mergeMethods: { merge: true, squash: false, rebase: false },
75
+ mergeDefaults: { merge: { subject: `Merge pull request #${number} from octo/branch`, body: `pull request ${number}` } },
76
+ body: `body of pull request ${number}`,
77
+ ...overrides,
78
+ };
79
+ }
80
+
81
+ // One page answer per bucket, as the `loadPullRequestPage` seam delivers it
82
+ // (`{ok, rows, totalCount, hasMore, cursor}`). Every request is recorded.
83
+ function pageSource(map, options = {}) {
84
+ const calls = [];
85
+ const cursorRows = options.cursorRows || null;
86
+ const load = async (request) => {
87
+ calls.push(request);
88
+ const all = map[request.bucketId] || [];
89
+ if (cursorRows && cursorRows[request.bucketId]) {
90
+ const pages = cursorRows[request.bucketId];
91
+ // `cursor` is the cursor that continues AFTER that page, so a request
92
+ // carrying one reads the page after the page it came from.
93
+ const at = request.cursor === null
94
+ ? 0
95
+ : pages.findIndex((page) => page.cursor === request.cursor) + 1;
96
+ const page = pages[at] || { rows: [], cursor: null };
97
+ return { ok: true, rows: page.rows, totalCount: all.length, hasMore: page.cursor !== null, cursor: page.cursor };
98
+ }
99
+ if (options.byQuery && typeof options.byQuery[request.query] !== 'undefined') {
100
+ const rows = options.byQuery[request.query];
101
+ return { ok: true, rows, totalCount: rows.length, hasMore: false, cursor: null };
102
+ }
103
+ return { ok: true, rows: all, totalCount: all.length, hasMore: false, cursor: null };
104
+ };
105
+ return { load, calls };
106
+ }
107
+
108
+ function makeView(opts = {}) {
109
+ const paints = [];
110
+ let settled = 'pending';
111
+ const actions = opts.actions || {};
112
+ const view = createPrsView({
113
+ pages: opts.pages,
114
+ loadPullRequestPage: opts.loadPullRequestPage,
115
+ loadPullRequest: opts.loadPullRequest
116
+ || ((number) => ({ ok: true, pullRequest: pullRequest(number), comments: [] })),
117
+ loadLabels: opts.loadLabels,
118
+ loadAssignees: opts.loadAssignees,
119
+ permissions: opts.permissions,
120
+ labels: opts.labels,
121
+ actions,
122
+ helpers: opts.helpers || helpers,
123
+ theme: opts.theme || plainTheme,
124
+ rowsAvailable: () => opts.rows || 24,
125
+ requestRender: () => { paints.push(true); },
126
+ done: (result) => { settled = result; },
127
+ ghAvailable: opts.ghAvailable,
128
+ loadError: opts.loadError,
129
+ prefetchBuckets: opts.prefetchBuckets,
130
+ initialBucket: opts.initialBucket,
131
+ initialPullRequest: opts.initialPullRequest,
132
+ scheduleQuery: opts.scheduleQuery,
133
+ cancelSchedule: opts.cancelSchedule,
134
+ });
135
+ return { view, done: () => settled, paints };
136
+ }
137
+
138
+ // A scheduler the test owns: nothing fires until `fire()` is called, so the
139
+ // debounce window is fully deterministic.
140
+ function manualScheduler() {
141
+ const entries = [];
142
+ return {
143
+ entries,
144
+ schedule: (fn, delayMs) => {
145
+ const entry = { fn, delayMs, cancelled: false };
146
+ entries.push(entry);
147
+ return entry;
148
+ },
149
+ cancel: (entry) => { if (entry) entry.cancelled = true; },
150
+ live: () => entries.filter((entry) => !entry.cancelled),
151
+ fire() {
152
+ const live = entries.filter((entry) => !entry.cancelled);
153
+ for (const entry of live) entry.fn();
154
+ return live.length;
155
+ },
156
+ };
157
+ }
158
+
159
+ function deferred() {
160
+ let resolve = () => {};
161
+ const promise = new Promise((res) => { resolve = res; });
162
+ return { promise, resolve };
163
+ }
164
+
165
+ // S3: the reader asks view-layout for the smaller 30% list share, so every
166
+ // geometry helper here reads the same allocation the view paints.
167
+ function readerPanes(width) {
168
+ return layout.paneWidths(layout.innerWidth(width), { listShare: layout.READER_LIST_SHARE });
169
+ }
170
+
171
+ function bodyOf(view, width) {
172
+ const lines = view.render(width);
173
+ for (const line of lines) assert.equal(helpers.visibleWidth(line), width, `not ${width} wide: "${strip(line)}"`);
174
+ return strip(lines.join('\n'));
175
+ }
176
+
177
+ // The RIGHT (pane) side of every body row, so an assertion about the preview can
178
+ // never be satisfied by the list on the left.
179
+ function rightRows(view, width, rows = 24) {
180
+ const panes = readerPanes(width);
181
+ return view.render(width)
182
+ .slice(4, Math.max(4, rows - 2))
183
+ .map((line) => [...strip(line)].slice(5 + panes.left, 5 + panes.left + panes.right).join(''));
184
+ }
185
+
186
+ function rightOf(view, width, rows = 24) {
187
+ return rightRows(view, width, rows).join('\n');
188
+ }
189
+
190
+ function leftOf(view, width, rows = 24) {
191
+ const panes = readerPanes(width);
192
+ return view.render(width)
193
+ .slice(4, Math.max(4, rows - 2))
194
+ .map((line) => [...strip(line)].slice(2, 2 + panes.left).join(''))
195
+ .join('\n');
196
+ }
197
+
198
+ // The frame coordinates of a painted token inside the pane, so a click test
199
+ // names the cell the reader would hit instead of assuming a layout.
200
+ function paneCell(view, width, needle, rows = 24) {
201
+ const panes = readerPanes(width);
202
+ const lines = view.render(width);
203
+ for (let y = 4; y < Math.min(4 + (rows - 6), lines.length); y += 1) {
204
+ const cells = [...strip(lines[y])];
205
+ const right = cells.slice(5 + panes.left, 5 + panes.left + panes.right).join('');
206
+ const at = right.indexOf(needle);
207
+ if (at >= 0) return { x: 5 + panes.left + at, y, widthCells: width, rows };
208
+ }
209
+ throw new Error(`not painted in the pane: ${needle}`);
210
+ }
211
+
212
+ function click(view, cell, needle) {
213
+ return view.handleMouse({
214
+ type: 'click',
215
+ button: 'left',
216
+ x: cell.x + (needle || 0),
217
+ y: cell.y,
218
+ width: cell.widthCells,
219
+ height: cell.rows,
220
+ });
221
+ }
222
+
223
+ describe('prs view buckets, list and search (S1, S3)', () => {
224
+ test('renders the framed reader with the three pull request tabs and their counts', async () => {
225
+ const source = pageSource({
226
+ mine: [pullRequest(1), pullRequest(2)],
227
+ review: [pullRequest(3)],
228
+ involved: [],
229
+ });
230
+ const { view } = makeView({ loadPullRequestPage: source.load });
231
+ await tick();
232
+ const body = bodyOf(view, 150);
233
+ assert.match(body, /◈ Pull requests/);
234
+ assert.match(body, /My Pull Request.*2/, 'the first tab names the count it lists');
235
+ assert.match(body, /Needs My Review.*–/, 'a bucket nobody has read is unknown, never an invented zero');
236
+ assert.match(body, /Involved.*–/);
237
+ assert.doesNotMatch(body, /All \(/, 'the lazy view is a shortcut, not a fourth tab');
238
+ assert.match(body, /pull request 1/);
239
+ assert.match(body, /enter insert/, 'S7: the footer names the insert key');
240
+ assert.match(body, /g all prs/);
241
+ assert.match(body, /esc close/);
242
+ assert.equal(source.calls.length, 1, 'opening reads the bucket in view only');
243
+ assert.deepEqual(source.calls[0], { bucketId: 'mine', cursor: null, limit: 18, query: '' });
244
+ });
245
+
246
+ test('prefetchBuckets reads every scoped tab once, so no tab sits at a false zero', async () => {
247
+ const source = pageSource({
248
+ mine: [pullRequest(1), pullRequest(2)],
249
+ review: [pullRequest(3)],
250
+ involved: [],
251
+ });
252
+ const { view } = makeView({ loadPullRequestPage: source.load, prefetchBuckets: true });
253
+ await tick();
254
+ const body = bodyOf(view, 150);
255
+ assert.match(body, /My Pull Request.*2/);
256
+ assert.match(body, /Needs My Review.*1/);
257
+ assert.match(body, /Involved.*0/);
258
+ assert.deepEqual(source.calls.map((request) => request.bucketId).sort(), ['involved', 'mine', 'review']);
259
+ assert.equal(source.calls.length, 3, 'the lazy page is not prefetched');
260
+ });
261
+
262
+ test('tab and the arrows switch buckets; shift+tab and left go back', async () => {
263
+ const source = pageSource({ mine: [pullRequest(1)], review: [pullRequest(2)], involved: [pullRequest(3)] });
264
+ const { view } = makeView({ loadPullRequestPage: source.load });
265
+ await tick();
266
+ view.handleInput('tab');
267
+ await tick();
268
+ assert.equal(view.state.page, 1);
269
+ assert.match(bodyOf(view, 150), /pull request 2/);
270
+ view.handleInput('right');
271
+ await tick();
272
+ assert.equal(view.state.page, 2);
273
+ view.handleInput('left');
274
+ await tick();
275
+ assert.equal(view.state.page, 1);
276
+ view.handleInput('shift+tab');
277
+ await tick();
278
+ assert.equal(view.state.page, 0);
279
+ });
280
+
281
+ test('a bucket that already answered paints its rows again instead of refetching', async () => {
282
+ const source = pageSource({ mine: [pullRequest(1)], review: [pullRequest(2)], involved: [] });
283
+ const { view } = makeView({ loadPullRequestPage: source.load });
284
+ await tick();
285
+ view.handleInput('tab');
286
+ await tick();
287
+ const calls = source.calls.length;
288
+ view.handleInput('shift+tab');
289
+ await tick();
290
+ assert.equal(source.calls.length, calls, 'a visited tab reuses its rows');
291
+ assert.match(bodyOf(view, 150), /pull request 1/);
292
+ });
293
+
294
+ test('only open pull requests are listed, and a closed row never inflates the count', async () => {
295
+ const source = pageSource({ mine: [pullRequest(1), pullRequest(2, { state: 'CLOSED' })], review: [], involved: [] });
296
+ const { view } = makeView({ loadPullRequestPage: source.load });
297
+ await tick();
298
+ const left = leftOf(view, 150);
299
+ assert.match(left, /pull request 1/);
300
+ assert.doesNotMatch(left, /pull request 2/);
301
+ assert.match(bodyOf(view, 150), /My Pull Request.*1/, 'the count describes the rows actually listed');
302
+ });
303
+
304
+ test('j/k, the arrows, home and end move the selection', async () => {
305
+ const source = pageSource({ mine: [pullRequest(1), pullRequest(2), pullRequest(3)], review: [], involved: [] });
306
+ const { view } = makeView({ loadPullRequestPage: source.load });
307
+ await tick();
308
+ view.handleInput('down');
309
+ assert.equal(view.state.index, 1);
310
+ view.handleInput('j');
311
+ assert.equal(view.state.index, 2);
312
+ view.handleInput('up');
313
+ assert.equal(view.state.index, 1);
314
+ view.handleInput('k');
315
+ assert.equal(view.state.index, 0);
316
+ view.handleInput('end');
317
+ assert.equal(view.state.index, 2);
318
+ view.handleInput('home');
319
+ assert.equal(view.state.index, 0);
320
+ });
321
+
322
+ test('S3: ] and [ scroll the one list window a screenful at a time', async () => {
323
+ const rows = [];
324
+ for (let at = 1; at <= 30; at += 1) rows.push(pullRequest(at));
325
+ const source = pageSource({ mine: rows, review: [], involved: [] });
326
+ const { view } = makeView({ loadPullRequestPage: source.load, rows: 10 });
327
+ await tick();
328
+ assert.equal(view.state.listStart, 0);
329
+ view.handleInput(']');
330
+ assert.ok(view.state.listStart > 0, 'the window moved into the list');
331
+ const moved = view.state.listStart;
332
+ view.handleInput('[');
333
+ assert.ok(view.state.listStart < moved, 'the same key pair scrolls back');
334
+ });
335
+
336
+ test('S3: reaching the last loaded row continues the read after its cursor', async () => {
337
+ const pages = [
338
+ { cursor: 'c1', rows: [pullRequest(1), pullRequest(2), pullRequest(3)] },
339
+ { cursor: null, rows: [pullRequest(4)] },
340
+ ];
341
+ const source = pageSource({ mine: pages[0].rows.concat(pages[1].rows), review: [], involved: [] }, { cursorRows: { mine: pages } });
342
+ const { view } = makeView({ loadPullRequestPage: source.load, rows: 12 });
343
+ await tick();
344
+ const firstLimit = source.calls[0].limit;
345
+ view.handleInput('down');
346
+ view.handleInput('down');
347
+ await tick();
348
+ assert.equal(source.calls.length, 2, 'one continued read');
349
+ assert.equal(source.calls[1].cursor, 'c1');
350
+ assert.equal(source.calls[1].bucketId, 'mine');
351
+ assert.equal(source.calls[1].limit, firstLimit);
352
+ const left = leftOf(view, 150, 12);
353
+ assert.match(left, /pull request 1/);
354
+ assert.match(left, /pull request 4/);
355
+ });
356
+
357
+ test('S1: g opens all prs over the connection read, and the same key returns', async () => {
358
+ const source = pageSource({ mine: [pullRequest(1)], review: [pullRequest(2)], involved: [], all: [pullRequest(9)] });
359
+ const { view } = makeView({ loadPullRequestPage: source.load });
360
+ await tick();
361
+ view.handleInput('g');
362
+ await tick();
363
+ assert.equal(view.state.page, 3, 'the lazy page owns one state');
364
+ assert.deepEqual(source.calls.at(-1), { bucketId: 'all', cursor: null, limit: 18, query: '' });
365
+ const inside = bodyOf(view, 150);
366
+ assert.match(inside, /▸ All/);
367
+ assert.doesNotMatch(inside, /My Pull Request/, 'inside the lazy view the bar names only it');
368
+ assert.match(inside, /g related prs/);
369
+ assert.match(inside, /pull request 9/);
370
+ const reads = source.calls.length;
371
+ view.handleInput('g');
372
+ await tick();
373
+ assert.equal(view.state.page, 0, 'the same key returns to the tab the reader came from');
374
+ assert.equal(source.calls.length, reads, 'returning reuses the rows already read');
375
+ assert.match(bodyOf(view, 150), /g all prs/);
376
+ });
377
+
378
+ test('S3: the search reads the server with the query instead of re-filtering locally', async () => {
379
+ const scheduler = manualScheduler();
380
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] }, {
381
+ byQuery: { '': [pullRequest(1)], ok: [pullRequest(5, { title: 'unrelated title 5' })] },
382
+ });
383
+ const { view } = makeView({ loadPullRequestPage: source.load, scheduleQuery: scheduler.schedule, cancelSchedule: scheduler.cancel });
384
+ await tick();
385
+ view.handleInput('t');
386
+ view.handleInput('o');
387
+ view.handleInput('k');
388
+ assert.equal(view.state.query, 'ok', 'the query applies at once');
389
+ assert.equal(scheduler.live().length, 1, 'one debounced read for the settled query');
390
+ view.handleInput('enter');
391
+ await tick();
392
+ assert.equal(view.state.searching, false, 'enter keeps the filter and leaves the box');
393
+ const searched = source.calls.filter((request) => request.query === 'ok');
394
+ assert.ok(searched.length >= 1, 'the query reached the server');
395
+ assert.equal(searched[0].limit, 18);
396
+ const left = leftOf(view, 150);
397
+ assert.match(left, /unrelated title 5/, 'the server answer is the result, never re-filtered as literal text');
398
+ view.handleInput('t');
399
+ view.handleInput('delete');
400
+ assert.equal(view.state.query, '', 'delete clears the whole query');
401
+ });
402
+
403
+ test('S3: leaving the search box with esc keeps the filter', async () => {
404
+ const scheduler = manualScheduler();
405
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
406
+ const { view } = makeView({ loadPullRequestPage: source.load, scheduleQuery: scheduler.schedule, cancelSchedule: scheduler.cancel });
407
+ await tick();
408
+ view.handleInput('t');
409
+ view.handleInput('7');
410
+ view.handleInput('escape');
411
+ assert.equal(view.state.searching, false);
412
+ assert.equal(view.state.query, '7');
413
+ await tick();
414
+ assert.ok(source.calls.some((request) => request.query === '7'));
415
+ });
416
+
417
+ test('S3: the pane scrolls with the shared preview bindings', async () => {
418
+ const body = Array.from({ length: 60 }, (_value, at) => `paragraph ${at}`).join('\n');
419
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
420
+ const { view } = makeView({
421
+ loadPullRequestPage: source.load,
422
+ loadPullRequest: (number) => ({ ok: true, pullRequest: pullRequest(number, { body }), comments: [] }),
423
+ });
424
+ await tick();
425
+ // The pane's page size is a fact of the painted frame, so the reader has
426
+ // already seen the first frame before these keys mean anything.
427
+ assert.match(rightOf(view, 150), /paragraph 0/);
428
+ assert.equal(view.state.scroll, 0);
429
+ view.handleInput('ctrl+j');
430
+ assert.ok(view.state.scroll > 0, 'a page down moves the pane');
431
+ const down = view.state.scroll;
432
+ view.handleInput('ctrl+k');
433
+ assert.ok(view.state.scroll < down, 'a page up moves it back');
434
+ view.handleInput('ctrl+d');
435
+ assert.ok(view.state.scroll > 0 && view.state.scroll < down, 'ctrl+d is a half page');
436
+ });
437
+
438
+ test('S3: the pane carries the Changes and Checks summary blocks', async () => {
439
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
440
+ const { view } = makeView({ loadPullRequestPage: source.load });
441
+ await tick();
442
+ const pane = rightOf(view, 150);
443
+ assert.match(pane, /3 files changed \+40 -5/);
444
+ assert.match(pane, /7 commits 2h ago/);
445
+ assert.match(pane, /5 checks · 1 failed/);
446
+ assert.match(pane, /failing: build/);
447
+ });
448
+ });
449
+
450
+ describe('prs view capabilities (S2)', () => {
451
+ test('a read-only repository gets no editor, merge or close hint at all', async () => {
452
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
453
+ const { view } = makeView({
454
+ loadPullRequestPage: source.load,
455
+ loadPullRequest: (number) => ({ ok: true, pullRequest: pullRequest(number), comments: [] }),
456
+ });
457
+ await tick();
458
+ const body = bodyOf(view, 150);
459
+ assert.doesNotMatch(body, /l labels/);
460
+ assert.doesNotMatch(body, /a assign/);
461
+ assert.doesNotMatch(body, /x close/);
462
+ assert.doesNotMatch(body, /m merge/);
463
+ assert.doesNotMatch(body, /i linked/);
464
+ assert.match(body, /c comment/, 'commenting needs no capability beyond read access');
465
+ assert.match(body, /enter insert/);
466
+ });
467
+
468
+ test('each refused key names the missing permission and calls no seam', async () => {
469
+ const calls = { labels: 0, assignees: 0, close: 0, merge: 0, linked: 0 };
470
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
471
+ const { view } = makeView({
472
+ loadPullRequestPage: source.load,
473
+ loadPullRequest: (number) => ({ ok: true, pullRequest: pullRequest(number), comments: [] }),
474
+ actions: {
475
+ editLabels: () => { calls.labels += 1; return { ok: true }; },
476
+ editAssignees: () => { calls.assignees += 1; return { ok: true }; },
477
+ close: () => { calls.close += 1; return { ok: true }; },
478
+ merge: () => { calls.merge += 1; return { ok: true }; },
479
+ editLinkedIssues: () => { calls.linked += 1; return { ok: true }; },
480
+ },
481
+ });
482
+ await tick();
483
+ view.handleInput('l');
484
+ assert.equal(view.state.mode, 'list', 'no editor opened');
485
+ assert.match(bodyOf(view, 150), /labels cannot be edited: this repository permission is missing/);
486
+ view.handleInput('a');
487
+ assert.match(bodyOf(view, 150), /assignees cannot be edited: this repository permission is missing/);
488
+ view.handleInput('x');
489
+ assert.equal(view.state.mode, 'list', 'no confirmation opened');
490
+ assert.match(bodyOf(view, 150), /pull requests cannot be closed: this repository permission is missing/);
491
+ view.handleInput('m');
492
+ assert.equal(view.state.mode, 'list');
493
+ assert.match(bodyOf(view, 150), /pull requests cannot be merged: this repository permission is missing/);
494
+ view.handleInput('i');
495
+ assert.equal(view.state.mode, 'list');
496
+ assert.match(bodyOf(view, 150), /linked issues cannot be edited: this repository permission is missing/);
497
+ assert.deepEqual(calls, { labels: 0, assignees: 0, close: 0, merge: 0, linked: 0 });
498
+ });
499
+
500
+ test('a confirmed capability set puts every hint on the footer', async () => {
501
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
502
+ const { view } = makeView({
503
+ loadPullRequestPage: source.load,
504
+ loadPullRequest: (number) => ({
505
+ ok: true,
506
+ pullRequest: pullRequest(number, { permissions: EDITABLE }),
507
+ comments: [],
508
+ }),
509
+ });
510
+ await tick();
511
+ const body = bodyOf(view, 170);
512
+ for (const hint of ['l labels', 'a assign', 'x close', 'm merge', 'i linked', 'o web', 'enter insert']) {
513
+ assert.ok(body.includes(hint), `the footer names ${hint}`);
514
+ }
515
+ });
516
+ });
517
+
518
+ describe('prs view merge mode (S2)', () => {
519
+ test('m opens the merge step over only the methods the repository allows', async () => {
520
+ const source = pageSource({ mine: [pullRequest(87)], review: [], involved: [] });
521
+ const { view } = makeView({
522
+ loadPullRequestPage: source.load,
523
+ loadPullRequest: (number) => ({
524
+ ok: true,
525
+ pullRequest: pullRequest(number, { permissions: EDITABLE }),
526
+ comments: [],
527
+ }),
528
+ });
529
+ await tick();
530
+ view.handleInput('m');
531
+ assert.equal(view.state.mode, 'merge');
532
+ const pane = rightOf(view, 150);
533
+ assert.match(pane, /Type: merge/);
534
+ assert.doesNotMatch(pane, /squash|rebase/, 'a method the repository does not allow is never offered');
535
+ assert.match(pane, /Commit message:/);
536
+ assert.match(pane, /Merge pull request #87 from octo\/branch/, "GitHub's own proposal prefills the message");
537
+ assert.match(pane, /Description:/);
538
+ assert.match(pane, /pull request 87/, 'the description is prefilled too');
539
+ assert.match(pane, /Confirm merge/);
540
+ assert.match(pane, /Cancel/);
541
+ assert.match(bodyOf(view, 150), /ctrl\+s merge/);
542
+ });
543
+
544
+ test('the message and the description are editable and the confirm sends them', async () => {
545
+ const merged = [];
546
+ const source = pageSource({ mine: [pullRequest(87)], review: [], involved: [] });
547
+ const { view } = makeView({
548
+ loadPullRequestPage: source.load,
549
+ loadPullRequest: (number) => ({
550
+ ok: true,
551
+ pullRequest: pullRequest(number, {
552
+ permissions: EDITABLE,
553
+ mergeDefaults: { merge: { subject: 'subject A', body: 'body A' } },
554
+ }),
555
+ comments: [],
556
+ }),
557
+ actions: { merge: (number, options) => { merged.push({ number, options }); return { ok: true }; } },
558
+ });
559
+ await tick();
560
+ view.handleInput('m');
561
+ view.handleInput('down');
562
+ view.handleInput('backspace');
563
+ view.handleInput('backspace');
564
+ view.handleInput('Z');
565
+ view.handleInput('down');
566
+ view.handleInput('!');
567
+ const pane = rightOf(view, 150);
568
+ assert.match(pane, /subjectZ/);
569
+ assert.match(pane, /body A!/);
570
+ view.handleInput('ctrl+s');
571
+ await tick();
572
+ assert.equal(merged.length, 1);
573
+ assert.deepEqual(merged[0], { number: 87, options: { method: 'merge', subject: 'subjectZ', body: 'body A!' } });
574
+ assert.equal(view.state.mode, 'list');
575
+ assert.match(bodyOf(view, 150), /merged/, 'the outcome is named');
576
+ assert.doesNotMatch(leftOf(view, 150), /pull request 87/, 'a merged pull request leaves the open list');
577
+ });
578
+
579
+ test('enter walks the fields and confirms from the Confirm merge row', async () => {
580
+ const merged = [];
581
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
582
+ const { view } = makeView({
583
+ loadPullRequestPage: source.load,
584
+ loadPullRequest: (number) => ({ ok: true, pullRequest: pullRequest(number, { permissions: EDITABLE }), comments: [] }),
585
+ actions: { merge: (number, options) => { merged.push({ number, options }); return { ok: true }; } },
586
+ });
587
+ await tick();
588
+ view.handleInput('m');
589
+ view.handleInput('enter');
590
+ view.handleInput('enter');
591
+ view.handleInput('enter');
592
+ assert.equal(view.state.mergeField, 3, 'the Confirm merge row has the focus');
593
+ view.handleInput('enter');
594
+ await tick();
595
+ assert.equal(merged.length, 1);
596
+ assert.equal(merged[0].options.method, 'merge');
597
+ });
598
+
599
+ test('esc cancels without reaching the merge seam', async () => {
600
+ let calls = 0;
601
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
602
+ const { view } = makeView({
603
+ loadPullRequestPage: source.load,
604
+ loadPullRequest: (number) => ({ ok: true, pullRequest: pullRequest(number, { permissions: EDITABLE }), comments: [] }),
605
+ actions: { merge: () => { calls += 1; return { ok: true }; } },
606
+ });
607
+ await tick();
608
+ view.handleInput('m');
609
+ view.handleInput('escape');
610
+ assert.equal(calls, 0);
611
+ assert.equal(view.state.mode, 'list');
612
+ assert.match(rightOf(view, 150), /body of pull request 1/, 'the pane is the preview again');
613
+ });
614
+
615
+ test('the offered type cycles only through the allowed methods, with their own defaults', async () => {
616
+ const merged = [];
617
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
618
+ const { view } = makeView({
619
+ loadPullRequestPage: source.load,
620
+ loadPullRequest: (number) => ({
621
+ ok: true,
622
+ pullRequest: pullRequest(number, {
623
+ permissions: EDITABLE,
624
+ mergeMethods: { merge: true, squash: true, rebase: false },
625
+ mergeDefaults: {
626
+ merge: { subject: 'merge subject', body: 'merge body' },
627
+ squash: { subject: 'squash subject', body: 'squash body' },
628
+ },
629
+ }),
630
+ comments: [],
631
+ }),
632
+ actions: { merge: (number, options) => { merged.push({ number, options }); return { ok: true }; } },
633
+ });
634
+ await tick();
635
+ view.handleInput('m');
636
+ assert.match(rightOf(view, 150), /Type: ◂ merge ▸/, 'the only reachable method is the one on screen');
637
+ view.handleInput('right');
638
+ const pane = rightOf(view, 150);
639
+ assert.match(pane, /Type: ◂ squash ▸/);
640
+ assert.match(pane, /squash subject/);
641
+ assert.doesNotMatch(pane, /merge subject/);
642
+ view.handleInput('ctrl+s');
643
+ await tick();
644
+ assert.deepEqual(merged[0].options, { method: 'squash', subject: 'squash subject', body: 'squash body' });
645
+ });
646
+
647
+ test('a rebase merge sends the method alone', async () => {
648
+ const merged = [];
649
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
650
+ const { view } = makeView({
651
+ loadPullRequestPage: source.load,
652
+ loadPullRequest: (number) => ({
653
+ ok: true,
654
+ pullRequest: pullRequest(number, {
655
+ permissions: EDITABLE,
656
+ mergeMethods: { merge: false, squash: false, rebase: true },
657
+ mergeDefaults: { rebase: {} },
658
+ }),
659
+ comments: [],
660
+ }),
661
+ actions: { merge: (number, options) => { merged.push({ number, options }); return { ok: true }; } },
662
+ });
663
+ await tick();
664
+ view.handleInput('m');
665
+ view.handleInput('ctrl+s');
666
+ await tick();
667
+ assert.deepEqual(merged[0], { number: 1, options: { method: 'rebase' } });
668
+ });
669
+
670
+ test('a draft pull request is never mergeable, whatever the permission says', async () => {
671
+ const source = pageSource({ mine: [pullRequest(1, { isDraft: true })], review: [], involved: [] });
672
+ const { view } = makeView({
673
+ loadPullRequestPage: source.load,
674
+ loadPullRequest: (number) => ({
675
+ ok: true,
676
+ pullRequest: pullRequest(number, { isDraft: true, permissions: EDITABLE }),
677
+ comments: [],
678
+ }),
679
+ });
680
+ await tick();
681
+ assert.doesNotMatch(bodyOf(view, 170), /m merge/);
682
+ view.handleInput('m');
683
+ assert.equal(view.state.mode, 'list');
684
+ assert.match(bodyOf(view, 150), /pull requests cannot be merged: this repository permission is missing/);
685
+ });
686
+
687
+ test('a repository that allows no merge method refuses the same way', async () => {
688
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
689
+ const { view } = makeView({
690
+ loadPullRequestPage: source.load,
691
+ loadPullRequest: (number) => ({
692
+ ok: true,
693
+ pullRequest: pullRequest(number, {
694
+ permissions: EDITABLE,
695
+ mergeMethods: { merge: false, squash: false, rebase: false },
696
+ }),
697
+ comments: [],
698
+ }),
699
+ });
700
+ await tick();
701
+ view.handleInput('m');
702
+ assert.equal(view.state.mode, 'list');
703
+ assert.match(bodyOf(view, 150), /pull requests cannot be merged: this repository permission is missing/);
704
+ });
705
+
706
+ test('the confirmed number is the one the step names and the row it highlights', async () => {
707
+ const merged = [];
708
+ const rows = [pullRequest(87), pullRequest(88)];
709
+ const pending = deferred();
710
+ let reads = 0;
711
+ const loadPullRequestPage = () => {
712
+ reads += 1;
713
+ return reads === 1 ? { ok: true, rows } : pending.promise;
714
+ };
715
+ const { view } = makeView({
716
+ loadPullRequestPage,
717
+ loadPullRequest: (number) => ({
718
+ ok: true,
719
+ pullRequest: pullRequest(number, {
720
+ permissions: EDITABLE,
721
+ mergeDefaults: { merge: { subject: `subject ${number}`, body: `body ${number}` } },
722
+ }),
723
+ comments: [],
724
+ }),
725
+ actions: { merge: (number, options) => { merged.push({ number, options }); return { ok: true }; } },
726
+ });
727
+ await tick();
728
+ view.handleInput('r');
729
+ view.handleInput('m');
730
+ pending.resolve({ ok: true, rows: [rows[1], rows[0]] });
731
+ await tick();
732
+ assert.match(rightOf(view, 150), /Confirm merge #87/, 'the confirmation names the number it will merge');
733
+ const left = leftOf(view, 150);
734
+ assert.match(left, /▸ pull request 87/, 'the row that will merge carries the cursor');
735
+ assert.doesNotMatch(left, /▸ pull request 88/, 'no other row is highlighted by the merge step');
736
+ view.handleInput('ctrl+s');
737
+ await tick();
738
+ assert.deepEqual(merged, [{ number: 87, options: { method: 'merge', subject: 'subject 87', body: 'body 87' } }]);
739
+ });
740
+
741
+ test('a rebase merge offers neither a commit message nor a description', async () => {
742
+ const merged = [];
743
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
744
+ const { view } = makeView({
745
+ loadPullRequestPage: source.load,
746
+ loadPullRequest: (number) => ({
747
+ ok: true,
748
+ pullRequest: pullRequest(number, {
749
+ permissions: EDITABLE,
750
+ mergeMethods: { rebase: true },
751
+ mergeDefaults: { rebase: {} },
752
+ }),
753
+ comments: [],
754
+ }),
755
+ actions: { merge: (number, options) => { merged.push({ number, options }); return { ok: true }; } },
756
+ });
757
+ await tick();
758
+ view.handleInput('m');
759
+ const pane = rightOf(view, 150);
760
+ assert.match(pane, /Type: rebase/);
761
+ assert.doesNotMatch(pane, /Commit message|Description/, 'a rebase merge takes neither field, so neither is offered');
762
+ view.handleInput('down');
763
+ assert.equal(view.state.mergeField, 3, 'down reaches the confirm row without passing a hidden field');
764
+ view.handleInput('enter');
765
+ await tick();
766
+ assert.deepEqual(merged, [{ number: 1, options: { method: 'rebase' } }]);
767
+ });
768
+
769
+ test('the merge fields stay reachable up and down without landing on a hidden one', async () => {
770
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
771
+ const { view } = makeView({
772
+ loadPullRequestPage: source.load,
773
+ loadPullRequest: (number) => ({
774
+ ok: true,
775
+ pullRequest: pullRequest(number, {
776
+ permissions: EDITABLE,
777
+ mergeMethods: { rebase: true },
778
+ mergeDefaults: { rebase: {} },
779
+ }),
780
+ comments: [],
781
+ }),
782
+ });
783
+ await tick();
784
+ view.handleInput('m');
785
+ const visited = new Set();
786
+ for (let at = 0; at < 5; at += 1) {
787
+ visited.add(view.state.mergeField);
788
+ view.handleInput('down');
789
+ }
790
+ assert.deepEqual([...visited].sort((left, right) => left - right), [0, 3, 4],
791
+ 'only the type, the confirm and the cancel rows hold the focus');
792
+ });
793
+
794
+ test('a refused merge keeps the confirmation on screen and names the error', async () => {
795
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
796
+ const { view } = makeView({
797
+ loadPullRequestPage: source.load,
798
+ loadPullRequest: (number) => ({ ok: true, pullRequest: pullRequest(number, { permissions: EDITABLE }), comments: [] }),
799
+ actions: { merge: () => ({ ok: false, error: 'branch protection refuses this merge' }) },
800
+ });
801
+ await tick();
802
+ view.handleInput('m');
803
+ view.handleInput('ctrl+s');
804
+ await tick();
805
+ assert.equal(view.state.mode, 'merge', 'the step stays open so the reader can retry');
806
+ assert.match(bodyOf(view, 150), /branch protection refuses this merge/);
807
+ });
808
+ });
809
+
810
+ describe('prs view writes (S2)', () => {
811
+ test('x asks once and then closes the pull request', async () => {
812
+ const closed = [];
813
+ const source = pageSource({ mine: [pullRequest(7)], review: [], involved: [] });
814
+ const { view } = makeView({
815
+ loadPullRequestPage: source.load,
816
+ loadPullRequest: (number) => ({ ok: true, pullRequest: pullRequest(number, { permissions: EDITABLE }), comments: [] }),
817
+ actions: { close: (number) => { closed.push(number); return { ok: true }; } },
818
+ });
819
+ await tick();
820
+ view.handleInput('x');
821
+ assert.equal(view.state.mode, 'confirm');
822
+ assert.match(rightOf(view, 150), /Confirm close #7/);
823
+ assert.match(rightOf(view, 150), /This will close the pull request on GitHub\./);
824
+ view.handleInput('escape');
825
+ assert.equal(view.state.mode, 'list');
826
+ assert.equal(closed.length, 0, 'a cancelled confirmation writes nothing');
827
+ view.handleInput('x');
828
+ view.handleInput('enter');
829
+ await tick();
830
+ assert.deepEqual(closed, [7]);
831
+ assert.equal(view.state.mode, 'list');
832
+ assert.doesNotMatch(leftOf(view, 150), /pull request 7/);
833
+ });
834
+
835
+ test('c posts a comment and names the outcome; a failure keeps the draft', async () => {
836
+ const posted = [];
837
+ let answer = { ok: true };
838
+ const source = pageSource({ mine: [pullRequest(5)], review: [], involved: [] });
839
+ const { view } = makeView({
840
+ loadPullRequestPage: source.load,
841
+ actions: { postComment: (number, body) => { posted.push({ number, body }); return answer; } },
842
+ });
843
+ await tick();
844
+ view.handleInput('c');
845
+ assert.equal(view.state.mode, 'comment');
846
+ view.handleInput('h');
847
+ view.handleInput('i');
848
+ view.handleInput('ctrl+s');
849
+ await tick();
850
+ assert.deepEqual(posted, [{ number: 5, body: 'hi' }]);
851
+ assert.equal(view.state.mode, 'list');
852
+ assert.match(bodyOf(view, 150), /comment posted/);
853
+
854
+ answer = { ok: false, error: 'comment refused' };
855
+ view.handleInput('c');
856
+ view.handleInput('n');
857
+ view.handleInput('o');
858
+ view.handleInput('ctrl+s');
859
+ await tick();
860
+ assert.equal(view.state.mode, 'comment', 'a refused post keeps the editor');
861
+ assert.equal(view.state.commentText, 'no', 'and keeps what was typed');
862
+ assert.match(bodyOf(view, 150), /comment refused/);
863
+ });
864
+
865
+ test('the comment editor posts the number it opened on, whatever a reload did', async () => {
866
+ const posted = [];
867
+ let reads = 0;
868
+ let finish = () => {};
869
+ const rows = [pullRequest(42), pullRequest(43)];
870
+ const { view } = makeView({
871
+ loadPullRequestPage: () => {
872
+ reads += 1;
873
+ return reads === 1 ? { ok: true, rows } : new Promise((resolve) => { finish = resolve; });
874
+ },
875
+ actions: { postComment: (number, body) => { posted.push({ number, body }); return { ok: true }; } },
876
+ });
877
+ await tick();
878
+ view.handleInput('r');
879
+ view.handleInput('c');
880
+ view.handleInput('h');
881
+ view.handleInput('i');
882
+ // The reload lands with the OTHER pull request under the cursor, then drops
883
+ // the row the composer named altogether.
884
+ finish({ ok: true, rows: [rows[1]] });
885
+ await tick();
886
+ assert.equal(view.state.index, 0);
887
+ view.handleInput('ctrl+s');
888
+ await tick();
889
+ assert.deepEqual(posted, [{ number: 42, body: 'hi' }], 'the composer posted to the pull request it named');
890
+ });
891
+
892
+ test('a refused comment keeps both the draft and the number it was opened on', async () => {
893
+ const posted = [];
894
+ let answer = { ok: false, error: 'comment refused' };
895
+ const source = pageSource({ mine: [pullRequest(7), pullRequest(8)], review: [], involved: [] });
896
+ const { view } = makeView({
897
+ loadPullRequestPage: source.load,
898
+ actions: { postComment: (number, body) => { posted.push({ number, body }); return answer; } },
899
+ });
900
+ await tick();
901
+ view.handleInput('c');
902
+ view.handleInput('h');
903
+ view.handleInput('i');
904
+ view.handleInput('ctrl+s');
905
+ await tick();
906
+ await tick();
907
+ answer = { ok: true };
908
+ view.handleInput('ctrl+s');
909
+ await tick();
910
+ assert.deepEqual(posted, [{ number: 7, body: 'hi' }, { number: 7, body: 'hi' }],
911
+ 'a retry posts to the same pull request the composer named');
912
+ });
913
+
914
+ test('l applies label add/remove deltas and can create a label the repository allows', async () => {
915
+ const applied = [];
916
+ const source = pageSource({ mine: [pullRequest(3)], review: [], involved: [] });
917
+ const { view } = makeView({
918
+ loadPullRequestPage: source.load,
919
+ loadPullRequest: (number) => ({ ok: true, pullRequest: pullRequest(number, { permissions: EDITABLE }), comments: [] }),
920
+ loadLabels: () => ({ ok: true, labels: [{ name: 'bug' }, { name: 'docs' }] }),
921
+ actions: { editLabels: (number, add, remove, created) => { applied.push({ number, add, remove, created }); return { ok: true }; } },
922
+ });
923
+ await tick();
924
+ view.handleInput('l');
925
+ await tick();
926
+ assert.equal(view.state.mode, 'labels');
927
+ let pane = rightOf(view, 150);
928
+ assert.match(pane, /\[x\] bug/, "the row's own label starts selected");
929
+ assert.match(pane, /\[ \] docs/);
930
+ view.handleInput('space');
931
+ view.handleInput('down');
932
+ view.handleInput('space');
933
+ view.handleInput('n');
934
+ view.handleInput('u');
935
+ view.handleInput('enter');
936
+ view.handleInput('ctrl+s');
937
+ await tick();
938
+ assert.equal(applied.length, 1);
939
+ assert.equal(applied[0].number, 3);
940
+ assert.deepEqual(applied[0].add, ['docs', 'u']);
941
+ assert.deepEqual(applied[0].remove, ['bug']);
942
+ assert.deepEqual(applied[0].created, ['u']);
943
+ assert.equal(view.state.mode, 'list');
944
+ });
945
+
946
+ test('a repository that may label but not create never offers creation', async () => {
947
+ const applied = [];
948
+ const source = pageSource({ mine: [pullRequest(3)], review: [], involved: [] });
949
+ const { view } = makeView({
950
+ loadPullRequestPage: source.load,
951
+ loadPullRequest: (number) => ({
952
+ ok: true,
953
+ pullRequest: pullRequest(number, { permissions: { canEditLabels: true, canCreateLabels: false } }),
954
+ comments: [],
955
+ }),
956
+ loadLabels: () => ({ ok: true, labels: [{ name: 'bug' }, { name: 'docs' }] }),
957
+ actions: { editLabels: (number, add, remove, created) => { applied.push({ number, add, remove, created }); return { ok: true }; } },
958
+ });
959
+ await tick();
960
+ view.handleInput('l');
961
+ await tick();
962
+ const pane = rightOf(view, 150);
963
+ assert.match(pane, /\[x\] bug/);
964
+ assert.doesNotMatch(pane, /create new/, 'the picker offers no creation without the capability');
965
+ view.handleInput('n');
966
+ await tick();
967
+ assert.doesNotMatch(rightOf(view, 150), /new:/, 'n opens no creation row without the capability');
968
+ view.handleInput('ctrl+s');
969
+ await tick();
970
+ assert.deepEqual(applied, [{ number: 3, add: [], remove: [], created: [] }],
971
+ 'the draft was applied with no created label');
972
+ });
973
+
974
+ test('a toggled assignee sends the delta that assigns and unassigns the viewer', async () => {
975
+ const applied = [];
976
+ const source = pageSource({ mine: [pullRequest(4)], review: [], involved: [] });
977
+ const viewerOnly = (number) => ({
978
+ ok: true,
979
+ pullRequest: pullRequest(number, { permissions: EDITABLE, assignees: [{ login: 'octo' }] }),
980
+ comments: [],
981
+ });
982
+ const unassigned = (number) => ({
983
+ ok: true,
984
+ pullRequest: pullRequest(number, { permissions: EDITABLE, assignees: [] }),
985
+ comments: [],
986
+ });
987
+ let detail = viewerOnly;
988
+ const { view } = makeView({
989
+ loadPullRequestPage: source.load,
990
+ loadPullRequest: (number) => detail(number),
991
+ loadAssignees: () => ({ ok: true, users: [{ login: 'octo', name: 'Octo' }] }),
992
+ actions: { editAssignees: (number, delta) => { applied.push({ number, delta }); return { ok: true }; } },
993
+ });
994
+ await tick();
995
+ view.handleInput('a');
996
+ await tick();
997
+ assert.equal(view.state.mode, 'assignees');
998
+ assert.match(rightOf(view, 150), /\[x\] octo \(Octo\)/);
999
+ view.handleInput('space');
1000
+ view.handleInput('ctrl+s');
1001
+ await tick();
1002
+ assert.deepEqual(applied, [{ number: 4, delta: { add: [], remove: ['octo'] } }]);
1003
+
1004
+ detail = unassigned;
1005
+ view.handleInput('r');
1006
+ await tick();
1007
+ view.handleInput('a');
1008
+ await tick();
1009
+ assert.match(rightOf(view, 150), /\[ \] octo \(Octo\)/);
1010
+ view.handleInput('space');
1011
+ view.handleInput('ctrl+s');
1012
+ await tick();
1013
+ assert.deepEqual(applied[1], { number: 4, delta: { add: ['octo'], remove: [] } });
1014
+ });
1015
+
1016
+ test('i edits the linked issues as add/remove deltas', async () => {
1017
+ const applied = [];
1018
+ const source = pageSource({ mine: [pullRequest(6)], review: [], involved: [] });
1019
+ const { view } = makeView({
1020
+ loadPullRequestPage: source.load,
1021
+ loadPullRequest: (number) => ({ ok: true, pullRequest: pullRequest(number, { permissions: EDITABLE }), comments: [] }),
1022
+ actions: { editLinkedIssues: (number, delta) => { applied.push({ number, delta }); return { ok: true }; } },
1023
+ });
1024
+ await tick();
1025
+ view.handleInput('i');
1026
+ assert.equal(view.state.mode, 'linkedIssues');
1027
+ let pane = rightOf(view, 150);
1028
+ assert.match(pane, /\[x\] #12/);
1029
+ assert.match(pane, /\[x\] #14/);
1030
+ view.handleInput('down');
1031
+ view.handleInput('space');
1032
+ view.handleInput('n');
1033
+ view.handleInput('7');
1034
+ view.handleInput('7');
1035
+ view.handleInput('enter');
1036
+ pane = rightOf(view, 150);
1037
+ assert.match(pane, /\[x\] #77/, 'the added number is part of the draft');
1038
+ view.handleInput('ctrl+s');
1039
+ await tick();
1040
+ assert.deepEqual(applied, [{ number: 6, delta: { add: [77], remove: [14] } }]);
1041
+ assert.equal(view.state.mode, 'list');
1042
+ });
1043
+
1044
+ test('o opens the pull request on the web and r asks the list to be read again', async () => {
1045
+ const opened = [];
1046
+ const source = pageSource({ mine: [pullRequest(8)], review: [], involved: [] });
1047
+ const { view } = makeView({
1048
+ loadPullRequestPage: source.load,
1049
+ actions: { openBrowser: (number) => opened.push(number) },
1050
+ });
1051
+ await tick();
1052
+ view.handleInput('o');
1053
+ assert.deepEqual(opened, [8]);
1054
+ const reads = source.calls.length;
1055
+ view.handleInput('r');
1056
+ await tick();
1057
+ assert.ok(source.calls.length > reads, 'r re-reads the bucket on screen');
1058
+ assert.equal(view.state.index, 0, 'the refresh keeps the reader on the row');
1059
+ });
1060
+
1061
+ test('a write in flight owns the keyboard until it settles', async () => {
1062
+ const pending = deferred();
1063
+ const source = pageSource({ mine: [pullRequest(2)], review: [], involved: [] });
1064
+ const { view } = makeView({
1065
+ loadPullRequestPage: source.load,
1066
+ loadPullRequest: (number) => ({ ok: true, pullRequest: pullRequest(number, { permissions: EDITABLE }), comments: [] }),
1067
+ actions: { close: () => pending.promise },
1068
+ });
1069
+ await tick();
1070
+ view.handleInput('x');
1071
+ view.handleInput('enter');
1072
+ assert.equal(view.state.saving, 'close');
1073
+ assert.match(bodyOf(view, 150), /closing pull request…/);
1074
+ view.handleInput('escape');
1075
+ view.handleInput('x');
1076
+ assert.equal(view.state.mode, 'confirm', 'no second write and no close while the first is in flight');
1077
+ pending.resolve({ ok: true });
1078
+ await tick();
1079
+ assert.equal(view.state.saving, null);
1080
+ assert.equal(view.state.mode, 'list');
1081
+ });
1082
+ });
1083
+
1084
+ describe('prs view cross-links and insert (S6, S7)', () => {
1085
+ test('S7: enter inserts the focused pull request and closes the panel', async () => {
1086
+ const inserted = [];
1087
+ const source = pageSource({ mine: [pullRequest(87)], review: [], involved: [] });
1088
+ const { view, done } = makeView({
1089
+ loadPullRequestPage: source.load,
1090
+ actions: { insert: (text) => inserted.push(text) },
1091
+ });
1092
+ await tick();
1093
+ view.handleInput('enter');
1094
+ assert.deepEqual(inserted, ['PR #87']);
1095
+ assert.equal(done(), null, 'the panel closes after the insert');
1096
+ });
1097
+
1098
+ test('S7: enter inside the search box keeps the filter and inserts nothing', async () => {
1099
+ const inserted = [];
1100
+ const scheduler = manualScheduler();
1101
+ const source = pageSource({ mine: [pullRequest(87)], review: [], involved: [] });
1102
+ const { view, done } = makeView({
1103
+ loadPullRequestPage: source.load,
1104
+ scheduleQuery: scheduler.schedule,
1105
+ cancelSchedule: scheduler.cancel,
1106
+ actions: { insert: (text) => inserted.push(text) },
1107
+ });
1108
+ await tick();
1109
+ view.handleInput('t');
1110
+ view.handleInput('enter');
1111
+ assert.deepEqual(inserted, []);
1112
+ assert.equal(view.state.searching, false);
1113
+ assert.equal(done(), 'pending', 'the panel stays open');
1114
+ });
1115
+
1116
+ test('S7: enter without an insert seam refuses instead of closing', async () => {
1117
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
1118
+ const { view, done } = makeView({ loadPullRequestPage: source.load });
1119
+ await tick();
1120
+ view.handleInput('enter');
1121
+ assert.equal(done(), 'pending');
1122
+ assert.match(bodyOf(view, 150), /insert is unavailable/);
1123
+ });
1124
+
1125
+ test('S6: each linked issue number is its own click target', async () => {
1126
+ const opened = [];
1127
+ const source = pageSource({ mine: [pullRequest(87)], review: [], involved: [] });
1128
+ const { view } = makeView({
1129
+ loadPullRequestPage: source.load,
1130
+ actions: { openLinked: (target) => opened.push(target) },
1131
+ });
1132
+ await tick();
1133
+ const first = paneCell(view, 150, '#12');
1134
+ assert.deepEqual(click(view, first), { handled: true, render: false });
1135
+ assert.deepEqual(opened, [{ kind: 'issue', number: 12 }]);
1136
+ const second = paneCell(view, 150, '#14');
1137
+ assert.deepEqual(click(view, second), { handled: true, render: false });
1138
+ assert.deepEqual(opened, [{ kind: 'issue', number: 12 }, { kind: 'issue', number: 14 }]);
1139
+ });
1140
+
1141
+ test('S6: a click that is not on one of those numbers opens nothing', async () => {
1142
+ const opened = [];
1143
+ const source = pageSource({ mine: [pullRequest(87)], review: [], involved: [] });
1144
+ const { view } = makeView({
1145
+ loadPullRequestPage: source.load,
1146
+ actions: { openLinked: (target) => opened.push(target) },
1147
+ });
1148
+ await tick();
1149
+ const first = paneCell(view, 150, '#12');
1150
+ // The separator right after `#12`, and the pull request's own number in the
1151
+ // title, are not linked issue ranges.
1152
+ assert.equal(click(view, first, 3), undefined);
1153
+ const title = paneCell(view, 150, '#87');
1154
+ assert.equal(click(view, title), undefined);
1155
+ assert.deepEqual(opened, []);
1156
+ });
1157
+ });
1158
+
1159
+ describe('prs view states and startup (S1, S3)', () => {
1160
+ test('without gh the frame names the missing CLI and lists no rows', async () => {
1161
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
1162
+ const { view } = makeView({ loadPullRequestPage: source.load, ghAvailable: false });
1163
+ const body = bodyOf(view, 150);
1164
+ assert.match(body, /gh unavailable: install gh and authenticate to use \/prs/);
1165
+ assert.doesNotMatch(body, /pull request 1/);
1166
+ });
1167
+
1168
+ test('a failed read states the failure instead of painting fake rows', async () => {
1169
+ const { view } = makeView({
1170
+ loadPullRequestPage: async () => ({ ok: false, error: 'boom' }),
1171
+ });
1172
+ await tick();
1173
+ assert.match(bodyOf(view, 150), /failed to load pull requests: boom/);
1174
+ assert.doesNotMatch(bodyOf(view, 150), /no pull requests/);
1175
+ });
1176
+
1177
+ test('an empty bucket says so honestly', async () => {
1178
+ const source = pageSource({ mine: [], review: [], involved: [] });
1179
+ const { view } = makeView({ loadPullRequestPage: source.load });
1180
+ await tick();
1181
+ const body = bodyOf(view, 150);
1182
+ assert.match(body, /no pull requests/);
1183
+ assert.match(body, /nothing to show here/);
1184
+ });
1185
+
1186
+ test('initialBucket and initialPullRequest open on that bucket with that pull request', async () => {
1187
+ const source = pageSource({
1188
+ mine: [pullRequest(1)],
1189
+ review: [pullRequest(2), pullRequest(3)],
1190
+ involved: [],
1191
+ });
1192
+ const { view } = makeView({
1193
+ loadPullRequestPage: source.load,
1194
+ initialBucket: 'review',
1195
+ initialPullRequest: 3,
1196
+ });
1197
+ await tick();
1198
+ assert.equal(view.state.page, 1, 'the requested bucket is on screen');
1199
+ assert.equal(source.calls[0].bucketId, 'review');
1200
+ assert.equal(view.state.index, 1, 'the requested pull request holds the focus');
1201
+ assert.match(rightOf(view, 150), /#3 pull request 3/);
1202
+ });
1203
+
1204
+ test('a numbered open whose own read fails says where it looked', async () => {
1205
+ const source = pageSource({ mine: [pullRequest(1)], review: [pullRequest(2)], involved: [] });
1206
+ const { view } = makeView({
1207
+ loadPullRequestPage: source.load,
1208
+ loadPullRequest: () => ({ ok: false, error: 'Could not resolve to a PullRequest' }),
1209
+ initialBucket: 'mine',
1210
+ initialPullRequest: 99,
1211
+ });
1212
+ await tick();
1213
+ assert.match(bodyOf(view, 150), /#99 is not in My Pull Request/);
1214
+ assert.equal(view.state.index, 0, 'no invented row holds the focus');
1215
+ });
1216
+
1217
+ test('the lazy view names which read is on screen', async () => {
1218
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [], all: [pullRequest(9)] });
1219
+ const { view } = makeView({ loadPullRequestPage: source.load });
1220
+ await tick();
1221
+ view.handleInput('g');
1222
+ await tick();
1223
+ assert.match(bodyOf(view, 150), /via connection \(exact total\)/);
1224
+ });
1225
+ });
1226
+
1227
+ // ---------------------------------------------------------------------------
1228
+ // S5/S6 (D7): a numbered open is answered by the NUMBER, not by whatever the
1229
+ // scoped buckets happen to hold — a merged pull request or another author's
1230
+ // open one is fetched by number, shown with its state, and never offered a
1231
+ // write it cannot take.
1232
+ // ---------------------------------------------------------------------------
1233
+ describe('prs view numbered opens by number (S5, S6)', () => {
1234
+ test('a numbered open of a merged pull request fetches it by number and shows its state read-only', async () => {
1235
+ const fetched = [];
1236
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
1237
+ const { view } = makeView({
1238
+ loadPullRequestPage: source.load,
1239
+ loadPullRequest: (number) => {
1240
+ fetched.push(number);
1241
+ return {
1242
+ ok: true,
1243
+ pullRequest: pullRequest(number, { state: 'MERGED', permissions: EDITABLE }),
1244
+ comments: [],
1245
+ };
1246
+ },
1247
+ initialBucket: 'mine',
1248
+ initialPullRequest: 87,
1249
+ });
1250
+ await tick();
1251
+ assert.ok(fetched.includes(87), 'the detail seam is asked for exactly the number');
1252
+ assert.match(rightOf(view, 150), /#87 pull request 87/);
1253
+ assert.match(rightOf(view, 150), /\[merged\]/, "GitHub's state is shown, not invented as open");
1254
+ assert.match(leftOf(view, 150), /pull request 87/, 'the fetched pull request is in the list');
1255
+ const body = bodyOf(view, 170);
1256
+ assert.match(body, /c comment/, 'the footer is painted; only its write hints are gone');
1257
+ for (const hint of ['l labels', 'a assign', 'x close', 'm merge', 'i linked']) {
1258
+ assert.ok(!body.includes(hint), `a merged pull request offers no ${hint}`);
1259
+ }
1260
+ for (const key of ['l', 'a', 'x', 'm', 'i']) view.handleInput(key);
1261
+ assert.equal(view.state.mode, 'list', 'no editor, confirmation or merge step opens');
1262
+ });
1263
+
1264
+ test("a numbered open of another author's open pull request shows it with the writes the role allows", async () => {
1265
+ const source = pageSource({ mine: [pullRequest(1)], review: [], involved: [] });
1266
+ const { view } = makeView({
1267
+ loadPullRequestPage: source.load,
1268
+ loadPullRequest: (number) => ({
1269
+ ok: true,
1270
+ pullRequest: pullRequest(number, { author: 'someone-else', permissions: EDITABLE }),
1271
+ comments: [],
1272
+ }),
1273
+ initialBucket: 'mine',
1274
+ initialPullRequest: 42,
1275
+ });
1276
+ await tick();
1277
+ assert.match(rightOf(view, 150), /#42 pull request 42/);
1278
+ assert.match(rightOf(view, 150), /by someone-else/);
1279
+ assert.match(rightOf(view, 150), /\[open\]/);
1280
+ assert.match(bodyOf(view, 170), /x close/, 'an open pull request keeps the writes the role allows');
1281
+ view.handleInput('x');
1282
+ assert.equal(view.state.mode, 'confirm');
1283
+ assert.match(rightOf(view, 150), /Confirm close #42/);
1284
+ });
1285
+
1286
+ test('the pinned pull request leaves the list when the reader picks another tab', async () => {
1287
+ const source = pageSource({ mine: [pullRequest(1)], review: [pullRequest(2)], involved: [] });
1288
+ const { view } = makeView({
1289
+ loadPullRequestPage: source.load,
1290
+ loadPullRequest: (number) => ({
1291
+ ok: true,
1292
+ pullRequest: pullRequest(number, { state: 'MERGED', permissions: EDITABLE }),
1293
+ comments: [],
1294
+ }),
1295
+ initialBucket: 'mine',
1296
+ initialPullRequest: 87,
1297
+ });
1298
+ await tick();
1299
+ assert.match(leftOf(view, 150), /pull request 87/);
1300
+ view.handleInput('tab');
1301
+ await tick();
1302
+ const left = leftOf(view, 150);
1303
+ assert.doesNotMatch(left, /pull request 87/, "another tab paints its own bucket");
1304
+ assert.match(left, /pull request 2/);
1305
+ });
1306
+ });
1307
+
1308
+ // ---------------------------------------------------------------------------
1309
+ // S2 (D1): a confirmation acts on the number it showed, whatever an in-flight
1310
+ // reload does to the rows behind it.
1311
+ // ---------------------------------------------------------------------------
1312
+ describe('prs view confirmations keep their target (S2)', () => {
1313
+ function reloadInFlight() {
1314
+ const rows = [pullRequest(87), pullRequest(88)];
1315
+ let reads = 0;
1316
+ const pending = deferred();
1317
+ const loadPullRequestPage = () => {
1318
+ reads += 1;
1319
+ return reads === 1 ? { ok: true, rows } : pending.promise;
1320
+ };
1321
+ return { rows, pending, loadPullRequestPage };
1322
+ }
1323
+
1324
+ test('the close confirmation writes the number it showed, never the substituted row', async () => {
1325
+ const closed = [];
1326
+ const reload = reloadInFlight();
1327
+ const { view } = makeView({
1328
+ loadPullRequestPage: reload.loadPullRequestPage,
1329
+ loadPullRequest: (number) => ({
1330
+ ok: true,
1331
+ pullRequest: pullRequest(number, { permissions: EDITABLE }),
1332
+ comments: [],
1333
+ }),
1334
+ actions: { close: (number) => { closed.push(number); return { ok: true }; } },
1335
+ });
1336
+ await tick();
1337
+ view.handleInput('r');
1338
+ view.handleInput('x');
1339
+ assert.match(rightOf(view, 150), /Confirm close #87/);
1340
+ reload.pending.resolve({ ok: true, rows: [reload.rows[1], reload.rows[0]] });
1341
+ await tick();
1342
+ assert.equal(view.state.index, 0, 'the reload put the OTHER pull request under the focus');
1343
+ view.handleInput('enter');
1344
+ await tick();
1345
+ assert.deepEqual(closed, [87], 'the confirmation closed the pull request it named');
1346
+ });
1347
+
1348
+ test('the merge confirmation merges the number its step opened on', async () => {
1349
+ const merged = [];
1350
+ const reload = reloadInFlight();
1351
+ const { view } = makeView({
1352
+ loadPullRequestPage: reload.loadPullRequestPage,
1353
+ loadPullRequest: (number) => ({
1354
+ ok: true,
1355
+ pullRequest: pullRequest(number, {
1356
+ permissions: EDITABLE,
1357
+ mergeDefaults: { merge: { subject: `subject ${number}`, body: `body ${number}` } },
1358
+ }),
1359
+ comments: [],
1360
+ }),
1361
+ actions: { merge: (number, options) => { merged.push({ number, options }); return { ok: true }; } },
1362
+ });
1363
+ await tick();
1364
+ view.handleInput('r');
1365
+ view.handleInput('m');
1366
+ assert.equal(view.state.mode, 'merge');
1367
+ reload.pending.resolve({ ok: true, rows: [reload.rows[1], reload.rows[0]] });
1368
+ await tick();
1369
+ assert.equal(view.state.index, 0, 'the reload put the OTHER pull request under the focus');
1370
+ view.handleInput('ctrl+s');
1371
+ await tick();
1372
+ assert.equal(merged.length, 1);
1373
+ assert.equal(merged[0].number, 87, 'the merge step merged the pull request it opened on');
1374
+ assert.deepEqual(merged[0].options, { method: 'merge', subject: 'subject 87', body: 'body 87' });
1375
+ });
1376
+
1377
+ test('j and k are text in the merge fields, and the arrows still move between them', async () => {
1378
+ const merged = [];
1379
+ const source = pageSource({ mine: [pullRequest(87)], review: [], involved: [] });
1380
+ const { view } = makeView({
1381
+ loadPullRequestPage: source.load,
1382
+ loadPullRequest: (number) => ({
1383
+ ok: true,
1384
+ pullRequest: pullRequest(number, {
1385
+ permissions: EDITABLE,
1386
+ mergeDefaults: { merge: { subject: 'S', body: 'B' } },
1387
+ }),
1388
+ comments: [],
1389
+ }),
1390
+ actions: { merge: (number, options) => { merged.push({ number, options }); return { ok: true }; } },
1391
+ });
1392
+ await tick();
1393
+ view.handleInput('m');
1394
+ view.handleInput('tab');
1395
+ assert.equal(view.state.mergeField, 1, 'the commit message holds the focus');
1396
+ view.handleInput('j');
1397
+ view.handleInput('k');
1398
+ assert.equal(view.state.mergeField, 1, 'typing j and k never navigates the form');
1399
+ view.handleInput('down');
1400
+ assert.equal(view.state.mergeField, 2, 'the arrow still moves to the next field');
1401
+ view.handleInput('j');
1402
+ const pane = rightOf(view, 150);
1403
+ assert.match(pane, /Sjk/);
1404
+ assert.match(pane, /Bj/);
1405
+ view.handleInput('ctrl+s');
1406
+ await tick();
1407
+ assert.deepEqual(merged, [{ number: 87, options: { method: 'merge', subject: 'Sjk', body: 'Bj' } }]);
1408
+ });
1409
+ });
1410
+
1411
+ // ---------------------------------------------------------------------------
1412
+ // S2 (D3): an editor re-reads its capability for its OWN number at save time,
1413
+ // so a reload that revoked it writes nothing.
1414
+ // ---------------------------------------------------------------------------
1415
+ describe('prs view editors re-check their capability at save time (S2)', () => {
1416
+ const EDITORS = [
1417
+ ['l', 'canEditLabels', 'editLabels', ['space'],
1418
+ 'labels cannot be edited: this repository permission is missing'],
1419
+ ['a', 'canEditAssignees', 'editAssignees', ['space'],
1420
+ 'assignees cannot be edited: this repository permission is missing'],
1421
+ ['i', 'canEditLinkedIssues', 'editLinkedIssues', ['n', '1', '2', 'enter'],
1422
+ 'linked issues cannot be edited: this repository permission is missing'],
1423
+ ];
1424
+
1425
+ for (const [key, capability, seam, edit, sentence] of EDITORS) {
1426
+ test(`${seam} writes nothing when the reload revoked its capability`, async () => {
1427
+ const writes = [];
1428
+ let permission = true;
1429
+ let reads = 0;
1430
+ let finish = () => {};
1431
+ const rows = [pullRequest(87)];
1432
+ const { view } = makeView({
1433
+ loadPullRequestPage: () => {
1434
+ reads += 1;
1435
+ if (reads === 1) return { ok: true, rows };
1436
+ return new Promise((resolve) => { finish = resolve; });
1437
+ },
1438
+ loadPullRequest: (number) => ({
1439
+ ok: true,
1440
+ pullRequest: pullRequest(number, { permissions: { [capability]: permission } }),
1441
+ comments: [],
1442
+ }),
1443
+ labels: [{ name: 'bug' }],
1444
+ loadAssignees: () => ({ ok: true, users: [{ login: 'alice' }] }),
1445
+ actions: { [seam]: (...args) => { writes.push([seam, args]); return { ok: true }; } },
1446
+ });
1447
+ await tick();
1448
+ view.handleInput('r');
1449
+ view.handleInput(key);
1450
+ await tick();
1451
+ for (const step of edit) view.handleInput(step);
1452
+ permission = false;
1453
+ finish({ ok: true, rows });
1454
+ await tick();
1455
+ view.handleInput('ctrl+s');
1456
+ await tick();
1457
+ assert.deepEqual(writes, [], 'a lapsed capability never reaches the write seam');
1458
+ assert.equal(view.state.mode, 'list', 'the editor closed instead of writing');
1459
+ assert.match(bodyOf(view, 150), new RegExp(sentence));
1460
+ });
1461
+ }
1462
+ });