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
@@ -5828,3 +5828,300 @@ describe('S21: a self-assignment repaints the panel without reopening it', () =>
5828
5828
  assert.ok(leftColumn(view, 160).includes('issue title 21'), 'and the row is still there');
5829
5829
  });
5830
5830
  });
5831
+
5832
+ // S7 (prs: cross-links and Enter): `enter` is the insert key of the issues panel
5833
+ // too. In list mode it puts the SELECTED issue into the chat input as
5834
+ // `ISSUE #63` and closes the panel; inside the search box, the editors and the
5835
+ // close confirmation the same key keeps the meaning it always had there. A host
5836
+ // without the `actions.insert` seam gets no effect and no close.
5837
+ describe('S7: enter inserts the selected issue', () => {
5838
+ function insertView(opts = {}) {
5839
+ const inserted = [];
5840
+ const listRows = Array.isArray(opts.listRows) ? opts.listRows : [issue(1), issue(2)];
5841
+ const calls = [];
5842
+ const { view, done } = makeView({
5843
+ pages: pagesOf([listRows, [], []]),
5844
+ issues: listRows,
5845
+ rows: opts.height || 24,
5846
+ loadIssue: (n) => ({ ok: true, issue: issue(n, { permissions: EDITABLE }), comments: [] }),
5847
+ labels: opts.labels,
5848
+ loadLabels: opts.loadLabels,
5849
+ loadAssignees: opts.loadAssignees,
5850
+ actions: {
5851
+ insert: (text) => inserted.push(text),
5852
+ setState: (n, state) => { calls.push([n, state]); return { ok: true }; },
5853
+ ...(opts.actions || {}),
5854
+ },
5855
+ });
5856
+ return { view, done, inserted, calls };
5857
+ }
5858
+
5859
+ const footerOf = (view, width = 200) => strip(view.render(width).at(-2));
5860
+
5861
+ test('list mode inserts ISSUE #N for the selected issue and closes the panel', async () => {
5862
+ const { view, done, inserted } = insertView();
5863
+ await tick();
5864
+ view.render(120);
5865
+ view.handleInput('enter');
5866
+ assert.deepEqual(inserted, ['ISSUE #1'], 'the focused issue is inserted in the agreed form');
5867
+ assert.equal(done(), null, 'the panel closes once the insertion was offered');
5868
+
5869
+ const second = insertView();
5870
+ await tick();
5871
+ second.view.render(120);
5872
+ second.view.handleInput('down');
5873
+ await tick();
5874
+ second.view.render(120);
5875
+ second.view.handleInput('enter');
5876
+ assert.deepEqual(second.inserted, ['ISSUE #2'], 'the insertion follows the selection, never the first row');
5877
+ });
5878
+
5879
+ test('an empty list and a host with no insert seam are both a no-op', async () => {
5880
+ const empty = insertView({ listRows: [] });
5881
+ await tick();
5882
+ empty.view.render(120);
5883
+ empty.view.handleInput('enter');
5884
+ assert.deepEqual(empty.inserted, [], 'nothing is selected, so nothing is inserted');
5885
+ assert.equal(empty.done(), 'pending', 'and the panel stays open');
5886
+
5887
+ const bare = insertView({ actions: { insert: undefined } });
5888
+ await tick();
5889
+ bare.view.render(120);
5890
+ bare.view.handleInput('enter');
5891
+ assert.equal(bare.done(), 'pending', 'a host with no seam keeps its panel');
5892
+ });
5893
+
5894
+ test('enter in the search box keeps closing it and never inserts', async () => {
5895
+ const { view, inserted } = insertView();
5896
+ await tick();
5897
+ view.render(120);
5898
+ view.handleInput('t');
5899
+ view.handleInput('2');
5900
+ view.handleInput('enter');
5901
+ assert.equal(view.state.searching, false, 'enter still leaves the box');
5902
+ assert.equal(view.state.query, '2', 'and keeps the filter');
5903
+ assert.deepEqual(inserted, [], 'the search box enter is not the insert key');
5904
+ });
5905
+
5906
+ test('enter keeps its meaning in the label, assignee and comment editors and the confirm', async () => {
5907
+ const { view, inserted, calls } = insertView({
5908
+ labels: [{ name: 'bug' }],
5909
+ loadLabels: () => ({ ok: true, labels: [{ name: 'bug' }] }),
5910
+ loadAssignees: () => ({ ok: true, users: [{ login: 'ann' }] }),
5911
+ });
5912
+ await tick();
5913
+ view.render(200);
5914
+
5915
+ view.handleInput('l');
5916
+ await tick();
5917
+ view.handleInput('n');
5918
+ view.handleInput('x');
5919
+ view.handleInput('enter');
5920
+ assert.match(bodyOf(view, 160), /new labels: x/, 'enter still adds the typed label name');
5921
+ view.handleInput('escape');
5922
+ assert.equal(view.state.mode, 'list');
5923
+
5924
+ view.handleInput('a');
5925
+ await tick();
5926
+ view.handleInput('enter');
5927
+ assert.match(bodyOf(view, 160), /\[x\] ann/, 'enter still toggles the focused assignee');
5928
+ view.handleInput('escape');
5929
+ assert.equal(view.state.mode, 'list');
5930
+
5931
+ view.handleInput('c');
5932
+ view.handleInput('h');
5933
+ view.handleInput('enter');
5934
+ assert.equal(view.state.commentText, 'h\n', 'enter still opens a comment line');
5935
+ view.handleInput('escape');
5936
+ view.handleInput('y');
5937
+ assert.equal(view.state.mode, 'list');
5938
+
5939
+ view.handleInput('x');
5940
+ assert.equal(view.state.mode, 'confirm');
5941
+ view.handleInput('enter');
5942
+ await tick();
5943
+ assert.deepEqual(calls, [[1, 'closed']], 'enter still confirms the close');
5944
+ assert.deepEqual(inserted, [], 'no editor and no confirm ever inserted an issue');
5945
+ });
5946
+
5947
+ test('the footer names enter insert in list mode and only there', async () => {
5948
+ const { view } = insertView({
5949
+ labels: [{ name: 'bug' }],
5950
+ loadLabels: () => ({ ok: true, labels: [{ name: 'bug' }] }),
5951
+ });
5952
+ await tick();
5953
+ assert.match(footerOf(view), /enter insert/, 'the reading legend names the insert key');
5954
+
5955
+ view.handleInput('t');
5956
+ assert.match(footerOf(view), /enter\/esc keep/, 'the search box names its own enter');
5957
+ assert.ok(!footerOf(view).includes('enter insert'), 'and not the insert key');
5958
+ view.handleInput('escape');
5959
+
5960
+ view.handleInput('c');
5961
+ assert.match(footerOf(view), /enter newline/, 'the comment editor names its own enter');
5962
+ assert.ok(!footerOf(view).includes('enter insert'), 'and not the insert key');
5963
+ view.handleInput('escape');
5964
+
5965
+ const empty = insertView({ listRows: [] });
5966
+ await tick();
5967
+ assert.ok(!footerOf(empty.view, 220).includes('enter insert'), 'nothing focused, nothing to insert');
5968
+ });
5969
+ });
5970
+
5971
+ // S6 (prs cross-links): in the issues pane each linked PR number is its OWN
5972
+ // click range. A click on one opens exactly that pull request in the PR panel
5973
+ // and never the whole row: the `linked PRs:` label, the separators and the
5974
+ // truncation ellipsis open nothing, the pane maps the click through the row it
5975
+ // shows and its scroll offset, and a host with no `openLinked` seam does
5976
+ // nothing. The geometry is asserted through the view's own `handleMouse`, which
5977
+ // is the seam the host dispatches a mouse event to.
5978
+ describe('S6: each linked PR is its own click range', () => {
5979
+ const PANE_START = (width) => 5 + readerPanes(width).left;
5980
+ const BODY_TOP = 4;
5981
+
5982
+ function paneTextOf(line, width) {
5983
+ const panes = readerPanes(width);
5984
+ return [...strip(line)].slice(PANE_START(width), PANE_START(width) + panes.right).join('');
5985
+ }
5986
+
5987
+ function mouse(overrides = {}) {
5988
+ return {
5989
+ type: 'click',
5990
+ button: 'left',
5991
+ x: 0,
5992
+ y: 0,
5993
+ width: 120,
5994
+ height: 24,
5995
+ shift: false,
5996
+ alt: false,
5997
+ ctrl: false,
5998
+ ...overrides,
5999
+ };
6000
+ }
6001
+
6002
+ // The click cell of `needle` inside the RIGHT pane of the frame the view just
6003
+ // composed, so no assertion guesses at the pane's columns or rows.
6004
+ function clickOn(view, width, needle, offset = 0) {
6005
+ const lines = view.render(width);
6006
+ for (let y = BODY_TOP; y < lines.length - 2; y += 1) {
6007
+ const pane = paneTextOf(lines[y], width);
6008
+ const at = pane.indexOf(needle);
6009
+ if (at < 0) continue;
6010
+ const event = mouse({ x: PANE_START(width) + at + offset, y, width, height: lines.length });
6011
+ return { ...event, result: view.handleMouse(event), pane, at };
6012
+ }
6013
+ assert.fail(`no pane row carries ${JSON.stringify(needle)}`);
6014
+ }
6015
+
6016
+ function linkedDetail(overrides = {}) {
6017
+ return issue(7, {
6018
+ title: 'linked issue',
6019
+ linkedPullRequests: [69, 77],
6020
+ linkedPullRequestsTruncated: false,
6021
+ body: 'the body',
6022
+ ...overrides,
6023
+ });
6024
+ }
6025
+
6026
+ function linkedView(opts = {}) {
6027
+ const calls = [];
6028
+ const listRows = Array.isArray(opts.listRows) ? opts.listRows : [issue(7)];
6029
+ const { view } = makeView({
6030
+ rows: opts.height || 24,
6031
+ pages: pagesOf([listRows, [], []]),
6032
+ issues: listRows,
6033
+ loadIssue: () => ({ ok: true, issue: linkedDetail(opts.detail || {}), comments: opts.comments || [] }),
6034
+ actions: { openLinked: (target) => calls.push(target), ...(opts.actions || {}) },
6035
+ });
6036
+ return { view, calls };
6037
+ }
6038
+
6039
+ test('a click on one number opens exactly that pull request', async () => {
6040
+ const { view, calls } = linkedView();
6041
+ await tick();
6042
+ const first = clickOn(view, 120, '#69');
6043
+ assert.deepEqual(first.result, { handled: true, render: false }, 'the link consumes the click without repainting');
6044
+ assert.deepEqual(calls, [{ kind: 'pullRequest', number: 69 }], 'only the clicked number is opened');
6045
+
6046
+ const second = clickOn(view, 120, '#77');
6047
+ assert.deepEqual(second.result, { handled: true, render: false });
6048
+ assert.deepEqual(
6049
+ calls,
6050
+ [{ kind: 'pullRequest', number: 69 }, { kind: 'pullRequest', number: 77 }],
6051
+ 'each number is its own independent target, never the whole line',
6052
+ );
6053
+ });
6054
+
6055
+ test('the label, the separators and the truncation ellipsis open nothing', async () => {
6056
+ const { view, calls } = linkedView();
6057
+ await tick();
6058
+ assert.equal(clickOn(view, 120, 'linked PRs:').result, undefined, 'the row label is not a link');
6059
+ assert.equal(clickOn(view, 120, '#69,', 3).result, undefined, 'the separator after a number is not a link');
6060
+ assert.deepEqual(calls, [], 'no click on the row itself opened anything');
6061
+
6062
+ const capped = linkedView({ detail: { linkedPullRequests: [69], linkedPullRequestsTruncated: true } });
6063
+ await tick();
6064
+ assert.equal(clickOn(capped.view, 120, '…').result, undefined, 'the truncation ellipsis is not clickable');
6065
+ assert.deepEqual(capped.calls, []);
6066
+ assert.deepEqual(clickOn(capped.view, 120, '#69').result, { handled: true, render: false }, 'the number still is');
6067
+ });
6068
+
6069
+ test('the pane maps a click through its scroll offset, row and column included', async () => {
6070
+ const body = Array.from({ length: 24 }, (_, at) => `body paragraph ${at}`).join('\n\n');
6071
+ const { view, calls } = linkedView({ height: 16, detail: { body } });
6072
+ await tick();
6073
+ const first = clickOn(view, 120, '#69');
6074
+ assert.deepEqual(first.result, { handled: true, render: false });
6075
+ assert.equal(view.state.scroll, 0);
6076
+
6077
+ view.handleInput('ctrl+d');
6078
+ // The frame that clamps the scroll is the one the click has to map against.
6079
+ view.render(120);
6080
+ const scroll = view.state.scroll;
6081
+ assert.ok(scroll > 0, 'the preview scrolled');
6082
+ assert.ok(first.y - scroll >= 4, `the linked row stays inside the body: ${first.y} - ${scroll}`);
6083
+
6084
+ const stale = view.handleMouse(mouse({ x: first.x, y: first.y, width: 120, height: view.render(120).length }));
6085
+ assert.equal(stale, undefined, 'the old position now shows a row that carries no link');
6086
+ const moved = view.handleMouse(mouse({ x: first.x, y: first.y - scroll, width: 120, height: view.render(120).length }));
6087
+ assert.deepEqual(moved, { handled: true, render: false }, 'the number is clickable one window up');
6088
+ assert.deepEqual(calls, [
6089
+ { kind: 'pullRequest', number: 69 },
6090
+ { kind: 'pullRequest', number: 69 },
6091
+ ]);
6092
+ });
6093
+
6094
+ test('a host with no seam does nothing, and a click outside the pane is unhandled', async () => {
6095
+ const bare = linkedView({ actions: { openLinked: undefined } });
6096
+ await tick();
6097
+ assert.deepEqual(clickOn(bare.view, 120, '#69').result, { handled: true, render: false },
6098
+ 'the link is consumed even when nothing can open');
6099
+
6100
+ const { view, calls } = linkedView();
6101
+ await tick();
6102
+ const linked = clickOn(view, 120, '#69');
6103
+ const height = view.render(120).length;
6104
+ assert.equal(view.handleMouse(mouse({ x: 3, y: linked.y, width: 120, height })), undefined,
6105
+ 'the left list column on the same row is not the pane');
6106
+ assert.equal(view.handleMouse(mouse({ x: linked.x, y: 1, width: 120, height })), undefined,
6107
+ 'a header row is never a link');
6108
+ assert.equal(view.handleMouse(mouse({ x: linked.x, y: height - 2, width: 120, height })), undefined,
6109
+ 'the footer is never a link');
6110
+ assert.equal(view.handleMouse(mouse({ x: linked.x, y: linked.y, width: 100, height })), undefined,
6111
+ 'a click carrying another frame geometry is not this frame');
6112
+ assert.equal(view.handleMouse(mouse({ x: linked.x, y: linked.y, type: 'press', width: 120, height })), undefined,
6113
+ 'a press is not the click that opens a link');
6114
+ assert.equal(view.handleMouse(null), undefined, 'an absent event is unhandled');
6115
+ assert.equal(calls.length, 1, 'only the pane link called the seam');
6116
+ });
6117
+
6118
+ test('a number in the title, the body or a comment is not a link', async () => {
6119
+ const { view, calls } = linkedView({
6120
+ detail: { title: 'linked issue', body: 'see #69 for details' },
6121
+ comments: [{ author: 'ann', body: 'also #77', createdAt: new Date().toISOString() }],
6122
+ });
6123
+ await tick();
6124
+ assert.equal(clickOn(view, 120, '#69 for details').result, undefined, 'free text is not a click range');
6125
+ assert.deepEqual(calls, []);
6126
+ });
6127
+ });
@@ -13,7 +13,7 @@
13
13
  // worktree↔issue association.
14
14
  //
15
15
  // The bash activity is not a preference here: it is a design on gentle-shell-bridge's
16
- // own `Card activity` line, so the shipped surface is exactly four commands, two tools, one
16
+ // own `Card activity` line, so the shipped surface is exactly five commands, two tools, one
17
17
  // sidebar section, one tools design and the one `Cards` row group the two file-card
18
18
  // switches carry.
19
19
  //
@@ -73,7 +73,7 @@ describe('manifest', () => {
73
73
  // their own four modules — a classifier, the card, its resolver and the document
74
74
  // body Pi's Markdown draws — and own no preference at all (S1): they are gated by
75
75
  // the bridge's `Card activity` line, which is the bridge's, not ours.
76
- for (const kept of ['gh-issues.js', 'issue-preview.js', 'issues-view.js', 'warp-selector-view.js', 'odd-view.js', 'worktree-git.js', 'worktree-registry.js', 'worktree-session-guard.js', 'skill-read-guard.js', 'bash-activity.js', 'bash-inline-script.js', 'bash-design.js', 'bash-frame.js', 'diff-design.js', 'skill-odd-card.js', 'file-card.js', 'file-card-resolver.js', 'file-card-markdown.js']) {
76
+ for (const kept of ['gh-issues.js', 'issue-preview.js', 'issues-view.js', 'gh-prs.js', 'pr-preview.js', 'prs-view.js', 'prs-rail-store.js', 'issues-section.js', 'warp-selector-view.js', 'odd-view.js', 'worktree-git.js', 'worktree-registry.js', 'worktree-session-guard.js', 'skill-read-guard.js', 'bash-activity.js', 'bash-inline-script.js', 'bash-design.js', 'bash-frame.js', 'diff-design.js', 'skill-odd-card.js', 'file-card.js', 'file-card-resolver.js', 'file-card-markdown.js']) {
77
77
  assert.ok(shipped.includes(kept), `lib/${kept} is shipped`);
78
78
  }
79
79
  for (const gone of ['card-files-mode.js', 'cards-customize.js']) {
@@ -83,11 +83,11 @@ describe('manifest', () => {
83
83
  });
84
84
 
85
85
  describe('registration', () => {
86
- test('registers /warp, /odd, /files, /issues and the two worktree tools', async () => {
86
+ test('registers /warp, /odd, /files, /issues, /prs and the two worktree tools', async () => {
87
87
  delete process.env.GENTLE_WORKTREE_THREAD;
88
88
  const { commands, tools, messageRenderers } = await loadExtension({});
89
89
  const registered = [...commands.keys()].sort();
90
- assert.deepEqual(registered, ['files', 'issues', 'odd', 'warp'], 'the command list is exactly the four panels');
90
+ assert.deepEqual(registered, ['files', 'issues', 'odd', 'prs', 'warp'], 'the command list is exactly the five panels');
91
91
  assert.deepEqual([...tools.keys()].sort(), ['worktree_create', 'worktree_list'], 'exactly two agent tools');
92
92
  assert.deepEqual([...messageRenderers.keys()], ['worktree-warp']);
93
93
  });
@@ -323,6 +323,20 @@ describe('removed surface', () => {
323
323
  assert.ok(source.includes('libRequire("issues-view.js")'), 'the reader mounts the migrated view');
324
324
  });
325
325
 
326
+ test('the /prs command keeps its own client and view (T5, S1)', () => {
327
+ const source = stubbedSource();
328
+ assert.ok(source.includes('pi.registerCommand("prs"'), 'the /prs command is registered');
329
+ assert.ok(source.includes('function registerPrsCommand'), 'its registration is a named step');
330
+ assert.ok(source.includes('libRequire("gh-prs.js")'), 'the reader uses the migrated pull-request client');
331
+ assert.ok(source.includes('createPrsClient'), 'the client is built with the issues client INJECTED');
332
+ assert.ok(source.includes('libRequire("prs-view.js")'), 'the reader mounts the migrated pull-request view');
333
+ // S4 (T5): the rail's pull-request count is a SECOND READ on the one
334
+ // GITHUB section, never a second section.
335
+ assert.equal(source.split('api.registerSection(').length - 1, 1, 'still exactly one sidebar section');
336
+ assert.ok(source.includes('loadPullRequests'), 'the one section reads pull requests for its header');
337
+ assert.ok(source.includes('libRequire("prs-rail-store.js")'), 'the count is persisted by the loader');
338
+ });
339
+
326
340
  test('the loaded extension holds no reference to the removed packages', async () => {
327
341
  const { commands } = await loadExtension({});
328
342
  // `odd` is NOT in this list any more: it is the odd viewer's own name now. What must
@@ -0,0 +1,281 @@
1
+ 'use strict';
2
+ // T2 (prs-panel S3,S6): the pull request preview formatter, lib/pr-preview.js.
3
+ // It is the issues preview's row/pane design applied to a pull request: the
4
+ // same identity block, the same omission of absent metadata, the same pending
5
+ // rendering, plus the two blocks S3 asks for — `Changes` and `Checks` — and the
6
+ // `linked issues:` row whose every number S6 makes its own click target. These
7
+ // tests pin the ORDER, the exact wording, the omitted empties and the click
8
+ // ranges the pane publishes, so the view that consumes them cannot drift.
9
+
10
+ const { test, describe } = require('node:test');
11
+ const assert = require('node:assert/strict');
12
+
13
+ const { pullRequestDetailLines } = require('../lib/pr-preview.js');
14
+
15
+ const plain = (_role, text) => String(text);
16
+ const bold = (text) => String(text);
17
+ const NOW = Date.parse('2026-03-01T00:00:00.000Z');
18
+
19
+ function render(pullRequest, comments, options = {}) {
20
+ return pullRequestDetailLines(pullRequest, comments, {
21
+ width: 80,
22
+ paint: plain,
23
+ emphasis: bold,
24
+ now: NOW,
25
+ ...options,
26
+ }).join('\n');
27
+ }
28
+
29
+ const FULL = {
30
+ number: 87,
31
+ title: 'parity title',
32
+ state: 'open',
33
+ isDraft: false,
34
+ author: 'octo',
35
+ labels: [{ name: 'bug' }, 'docs'],
36
+ assignees: [{ login: 'ana' }, 'beto'],
37
+ createdAt: '2026-02-25T00:00:00.000Z',
38
+ updatedAt: '2026-02-26T00:00:00.000Z',
39
+ commentsCount: 2,
40
+ reactions: { plusOne: 3 },
41
+ reviewers: [
42
+ { login: 'cara', state: 'APPROVED' },
43
+ { login: 'dan', state: 'CHANGES_REQUESTED' },
44
+ { login: 'eve', state: 'REQUESTED' },
45
+ { login: 'fay', state: 'COMMENTED' },
46
+ ],
47
+ linkedIssues: [12, 14],
48
+ additions: 40,
49
+ deletions: 5,
50
+ changedFiles: 3,
51
+ commits: 7,
52
+ committedAt: '2026-02-27T00:00:00.000Z',
53
+ checks: { total: 5, passed: 4, failed: 1, pending: 0, state: 'FAILURE', failing: ['build'] },
54
+ body: 'the body',
55
+ };
56
+
57
+ function linesOf(pullRequest, comments = [], options = {}) {
58
+ return pullRequestDetailLines(pullRequest, comments, {
59
+ width: 80,
60
+ paint: plain,
61
+ emphasis: bold,
62
+ now: NOW,
63
+ ...options,
64
+ });
65
+ }
66
+
67
+ describe('pull request preview formatter (S3)', () => {
68
+ test('renders the summary fields in the contract order', () => {
69
+ const lines = linesOf(FULL, [
70
+ { author: 'ana', body: 'first comment', createdAt: '2026-02-28T00:00:00.000Z' },
71
+ { author: 'beto', body: 'second comment', createdAt: '2026-02-28T12:00:00.000Z' },
72
+ ]);
73
+ const at = (needle) => {
74
+ const index = lines.findIndex((line) => line.includes(needle));
75
+ assert.notEqual(index, -1, `not painted: ${needle}`);
76
+ return index;
77
+ };
78
+ assert.equal(lines[0], '#87 parity title');
79
+ assert.match(lines[1], /\[open\]/);
80
+ assert.match(lines.join('\n'), /labels: bug, docs/);
81
+ assert.match(lines.join('\n'), /assignees: ana, beto/);
82
+ assert.match(
83
+ lines.join('\n').replace(/\s+/g, ' '),
84
+ /reviewers: cara \(approved\), dan \(changes requested\), eve \(review requested\), fay \(commented\)/,
85
+ 'every reviewer carries its own state mark',
86
+ );
87
+ assert.match(lines.join('\n'), /by octo · created 4d ago · updated 3d ago/);
88
+ assert.match(lines.join('\n'), /👍 3 · 2 comments/);
89
+ assert.match(lines.join('\n'), /linked issues: #12, #14/);
90
+ assert.match(lines.join('\n'), /3 files changed \+40 -5/);
91
+ assert.match(lines.join('\n'), /7 commits 2d ago/);
92
+ assert.match(lines.join('\n'), /5 checks · 1 failed/);
93
+ assert.match(lines.join('\n'), /failing: build/);
94
+ assert.match(lines.join('\n'), /the body/);
95
+ assert.match(lines.join('\n'), /─ ana · 1d ago ─/);
96
+ assert.match(lines.join('\n'), /first comment/);
97
+ assert.match(lines.join('\n'), /second comment/);
98
+ assert.equal((lines.join('\n').match(/\(end of pull request\)/g) || []).length, 1);
99
+
100
+ // S3's order: identity, relations, then the two summary blocks, then the
101
+ // body the reader came for.
102
+ assert.ok(at('[open]') < at('labels:'), 'labels follow the state row');
103
+ assert.ok(at('assignees:') < at('reviewers:'), 'reviewers follow assignees');
104
+ assert.ok(at('reviewers:') < at('by octo'), 'the author row follows the reviewers');
105
+ assert.ok(at('by octo') < at('linked issues:'), 'linked issues follow the author row');
106
+ assert.ok(at('linked issues:') < at('Changes'), 'the Changes block follows the relations');
107
+ assert.ok(at('Changes') < at('Checks'), 'the Checks block follows Changes');
108
+ assert.ok(at('Checks') < at('the body'), 'the body closes the pane');
109
+ });
110
+
111
+ test('a draft is marked as a draft, never as an open pull request', () => {
112
+ assert.match(render({ ...FULL, isDraft: true }), /\[draft\]/);
113
+ assert.doesNotMatch(linesOf({ ...FULL, isDraft: true }).join('\n'), /\[open\]/);
114
+ assert.match(linesOf({ ...FULL, state: 'closed' }).join('\n'), /\[closed\]/);
115
+ });
116
+
117
+ test('a reviewer without a known state is named without an invented mark', () => {
118
+ const text = render({ ...FULL, reviewers: [{ login: 'gus' }, { login: 'hal', state: 'DISMISSED' }] });
119
+ assert.match(text, /reviewers: gus, hal \(dismissed\)/);
120
+ });
121
+
122
+ test('omits empty optional metadata instead of printing a zero or "none"', () => {
123
+ const text = render(
124
+ {
125
+ ...FULL,
126
+ labels: [],
127
+ assignees: [],
128
+ reviewers: [],
129
+ linkedIssues: [],
130
+ commentsCount: 0,
131
+ reactions: { plusOne: 0 },
132
+ },
133
+ [],
134
+ );
135
+ assert.doesNotMatch(text, /assignees:/);
136
+ assert.doesNotMatch(text, /reviewers:/);
137
+ assert.doesNotMatch(text, /linked issues:/);
138
+ assert.match(text, /\(no labels\)/, 'the empty label row stays explicit, as in issues');
139
+ assert.match(text, /👍 0 · 0 comments/);
140
+ assert.match(text, /\(end of pull request\)/);
141
+ });
142
+
143
+ test('the Changes and Checks blocks are omitted when the read carried nothing', () => {
144
+ const bare = {
145
+ number: 5,
146
+ title: 'bare',
147
+ additions: undefined,
148
+ deletions: undefined,
149
+ changedFiles: undefined,
150
+ commits: undefined,
151
+ committedAt: undefined,
152
+ checks: undefined,
153
+ };
154
+ const text = render(bare, []);
155
+ assert.doesNotMatch(text, /Changes/);
156
+ assert.doesNotMatch(text, /files changed/);
157
+ assert.doesNotMatch(text, /Checks/);
158
+ assert.doesNotMatch(text, /checks/);
159
+ });
160
+
161
+ test('a green and a running checks read each say what they are', () => {
162
+ const green = render(
163
+ { ...FULL, checks: { total: 5, passed: 5, failed: 0, pending: 0, state: 'SUCCESS', failing: [] } },
164
+ [],
165
+ );
166
+ assert.match(green, /5 checks · all passed/);
167
+ assert.doesNotMatch(green, /failing:/);
168
+ const running = render(
169
+ { ...FULL, checks: { total: 3, passed: 1, failed: 0, pending: 2, state: 'PENDING', failing: [] } },
170
+ [],
171
+ );
172
+ assert.match(running, /3 checks · pending/);
173
+ const none = render({ ...FULL, checks: { total: 0, passed: 0, failed: 0, pending: 0, state: 'NONE', failing: [] } }, []);
174
+ assert.match(none, /no checks/);
175
+ const several = render(
176
+ { ...FULL, checks: { total: 6, passed: 3, failed: 3, pending: 0, state: 'FAILURE', failing: ['build', 'lint'] } },
177
+ [],
178
+ );
179
+ assert.match(several, /6 checks · 3 failed/);
180
+ assert.match(several, /failing: build, lint/, 'every failing check is named');
181
+ });
182
+
183
+ test('a single commit and a single check are not pluralised', () => {
184
+ const text = render(
185
+ { ...FULL, commits: 1, checks: { total: 1, passed: 1, failed: 0, pending: 0, state: 'SUCCESS', failing: [] } },
186
+ [],
187
+ );
188
+ assert.match(text, /1 commit 2d ago/);
189
+ assert.match(text, /1 check · all passed/);
190
+ });
191
+
192
+ test('a pending read paints the known fields and names the read', () => {
193
+ const text = render(
194
+ {
195
+ number: 12,
196
+ title: 'pull request title 12',
197
+ author: 'octo',
198
+ labels: [{ name: 'bug' }],
199
+ assignees: [{ login: 'ana' }],
200
+ reviewers: [{ login: 'cara', state: 'APPROVED' }],
201
+ },
202
+ [],
203
+ { pending: true },
204
+ );
205
+ assert.match(text, /#12 pull request title 12/);
206
+ assert.match(text, /labels: bug/);
207
+ assert.match(text, /assignees: ana/);
208
+ assert.match(text, /reviewers: cara \(approved\)/);
209
+ assert.match(text, /by octo/);
210
+ assert.match(text, /loading…/);
211
+ assert.doesNotMatch(text, /the body|no description|\(end of pull request\)|Checks|Changes/);
212
+ });
213
+
214
+ test('a capped linked-issue page is marked instead of passing for the whole list', () => {
215
+ assert.doesNotMatch(render({ ...FULL, linkedIssues: [] }, []), /linked issues:/);
216
+ assert.match(
217
+ render({ ...FULL, linkedIssues: [12], linkedIssuesTruncated: true }, []),
218
+ /linked issues: #12, …/,
219
+ );
220
+ });
221
+
222
+ test('ages come from the injected clock, so no assertion depends on the host time', () => {
223
+ const text = render({ ...FULL, createdAt: '2026-02-28T23:59:30.000Z', updatedAt: '2026-02-28T23:00:00.000Z' });
224
+ assert.match(text, /created just now · updated 1h ago/);
225
+ });
226
+
227
+ test('an unreadable relation paints no note about it', () => {
228
+ const text = render({ ...FULL, unavailable: ['checks'], metadataError: 'boom' }, []);
229
+ assert.doesNotMatch(text, /metadata|boom|unavailable/i);
230
+ assert.match(text, /linked issues: #12, #14/, 'the relations that did resolve still render');
231
+ });
232
+ });
233
+
234
+ // S6: every linked issue number is its own click target. The formatter
235
+ // publishes the cell ranges instead of leaving the view to guess them by
236
+ // searching the painted text, so a number that wraps, a truncated list and a
237
+ // narrow pane all keep exact targets.
238
+ describe('linked issue click ranges (S6)', () => {
239
+ test('each number publishes its own range on the painted line', () => {
240
+ const links = [];
241
+ const lines = linesOf(FULL, [], { links });
242
+ assert.equal(links.length, 2, 'one range per linked issue, never one per line');
243
+ assert.deepEqual(links.map((link) => link.number), [12, 14]);
244
+ for (const link of links) {
245
+ const line = lines[link.line];
246
+ assert.ok(line !== undefined, `range points at a painted line: ${JSON.stringify(link)}`);
247
+ assert.equal(line.slice(link.start, link.end), `#${link.number}`,
248
+ 'the range covers exactly the number and nothing else');
249
+ }
250
+ const [first, second] = links;
251
+ assert.ok(first.end <= second.start || first.line < second.line, 'the ranges never overlap');
252
+ });
253
+
254
+ test('a narrow pane still gives every number its own exact range', () => {
255
+ const links = [];
256
+ const lines = linesOf(FULL, [], { width: 14, links });
257
+ assert.equal(links.length, 2);
258
+ for (const link of links) {
259
+ const line = lines[link.line];
260
+ assert.ok(line !== undefined);
261
+ assert.equal(line.slice(link.start, link.end), `#${link.number}`);
262
+ assert.ok([...line].length <= 14, `"${line}" fits the pane`);
263
+ }
264
+ assert.ok(lines.some((line) => line.includes('linked issues:')), 'the row survives the wrap');
265
+ });
266
+
267
+ test('a truncated list publishes no range for the ellipsis', () => {
268
+ const links = [];
269
+ linesOf({ ...FULL, linkedIssues: [12], linkedIssuesTruncated: true }, [], { links });
270
+ assert.deepEqual(links.map((link) => link.number), [12]);
271
+ });
272
+
273
+ test('a pending read and a missing relation publish no range at all', () => {
274
+ const pendingLinks = [];
275
+ linesOf({ number: 9, title: 'x', linkedIssues: [12, 14] }, [], { pending: true, links: pendingLinks });
276
+ assert.deepEqual(pendingLinks, [], 'a pending row carries no linked issues to click');
277
+ const emptyLinks = [];
278
+ linesOf({ ...FULL, linkedIssues: [] }, [], { links: emptyLinks });
279
+ assert.deepEqual(emptyLinks, []);
280
+ });
281
+ });