pi-odd-worktree 3.9.1 → 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 (41) 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/modules.js +5 -0
  10. package/lib/pr-preview.js +353 -0
  11. package/lib/prs-rail-store.js +317 -0
  12. package/lib/prs-view.js +2896 -0
  13. package/package.json +1 -1
  14. package/tools/files-bridge.test.js +2 -2
  15. package/tools/files-command.test.js +1 -1
  16. package/tools/fixtures/gh-pr-connection-page.json +84 -0
  17. package/tools/fixtures/gh-pr-metadata.json +29 -0
  18. package/tools/fixtures/gh-pr-rail.json +61 -0
  19. package/tools/fixtures/gh-pr-search-page.json +82 -0
  20. package/tools/fixtures/gh-pr-view.json +159 -0
  21. package/tools/gh-issues.test.js +4 -2
  22. package/tools/gh-prs.test.js +1828 -0
  23. package/tools/issue-preview.test.js +119 -0
  24. package/tools/issues-cache-store.test.js +2 -2
  25. package/tools/issues-command.test.js +10 -3
  26. package/tools/issues-open-guard.test.js +15 -0
  27. package/tools/issues-section.test.js +618 -9
  28. package/tools/issues-view.test.js +297 -0
  29. package/tools/modules-wiring.test.js +5 -0
  30. package/tools/package-surface.test.js +18 -4
  31. package/tools/pr-preview.test.js +281 -0
  32. package/tools/prs-command.test.js +1280 -0
  33. package/tools/prs-rail-store.test.js +343 -0
  34. package/tools/prs-t10-safety.test.js +326 -0
  35. package/tools/prs-t12-safety.test.js +348 -0
  36. package/tools/prs-t13-independent.test.js +378 -0
  37. package/tools/prs-t14-safety.test.js +340 -0
  38. package/tools/prs-t15-independent.test.js +259 -0
  39. package/tools/prs-t18-safety.test.js +217 -0
  40. package/tools/prs-t7-safety.test.js +127 -0
  41. package/tools/prs-view.test.js +1462 -0
@@ -0,0 +1,217 @@
1
+ 'use strict';
2
+ // T18 (prs-panel S2, D27): the four safety defects an independent verification
3
+ // found in the marker-based linked-issue body edit, reproduced through the
4
+ // public client seam with a recorder for EVERY child.
5
+ //
6
+ // The rule the four cases below pin is one sentence: this panel may only ever
7
+ // delete the exact line it wrote, only where that line is not an example, only
8
+ // when the body's fences and comments pair up, and only while the pull request
9
+ // is still OPEN — otherwise it refuses and no `pr edit` child runs at all.
10
+
11
+ const { test } = require('node:test');
12
+ const assert = require('node:assert/strict');
13
+ const { createPrsClient } = require('../lib/gh-prs');
14
+
15
+ const pin = { scope: 'github.com/octo/repo' };
16
+ const ok = (doc) => ({ status: 0, stdout: JSON.stringify(doc), stderr: '', error: null });
17
+ const MARK = '<!-- odd-worktree:linked -->';
18
+ const UNREADABLE = 'the pull request body ends inside an unclosed code fence or comment';
19
+ const LINK_NOT_OURS = 'that link was not added by this panel; remove it on GitHub';
20
+ const MARKUP_LINK = 'that link sits inside a code fence or comment in the body; remove it on GitHub';
21
+ const BODY_CHANGED = 'the pull request body changed while it was being edited';
22
+ const NOT_OPEN = 'pull request #87 is not open; its linked issues cannot be edited';
23
+
24
+ // The recorder: the role read and the body reads answer from the fixture, and
25
+ // every OTHER child is a write this panel would have run. A refused edit
26
+ // therefore proves on its own that no `pr edit` child left the client.
27
+ function recorder(options = {}) {
28
+ const children = [];
29
+ const writes = [];
30
+ let reads = 0;
31
+ // The injected issue client's own runner seam: `(argv, options)`, so every
32
+ // child the client spawns is recorded here.
33
+ const run = async (argv, runOpts = {}) => {
34
+ children.push({ argv: argv.slice(), env: { ...(runOpts.env || {}) } });
35
+ const head = argv.slice(0, 2).join(' ');
36
+ if (head === 'api graphql') {
37
+ return ok({ data: { repository: { viewerPermission: 'WRITE', pullRequest: { state: 'OPEN' } } } });
38
+ }
39
+ if (head === 'pr view') {
40
+ reads += 1;
41
+ const answer = typeof options.read === 'function' ? options.read(reads) : { body: options.body, state: 'OPEN' };
42
+ return ok(answer);
43
+ }
44
+ writes.push(argv.slice());
45
+ return ok({});
46
+ };
47
+ const client = createPrsClient({ issues: { runGh: run } });
48
+ return { client, children, writes };
49
+ }
50
+
51
+ function bodyReads(children) {
52
+ return children.filter((child) => child.argv.slice(0, 2).join(' ') === 'pr view');
53
+ }
54
+
55
+ // ---------------------------------------------------------------------------
56
+ // Defect 1: a removal deleted the panel's own line even when it sat INSIDE a
57
+ // fenced or HTML-commented example of that very line.
58
+ // ---------------------------------------------------------------------------
59
+ test('T18/D27: a marked line inside a paired fence is never deleted', async () => {
60
+ const body = `Intro\n\`\`\`text\nCloses #12 ${MARK}\n\`\`\`\nTail\n`;
61
+ const { client, writes } = recorder({ body });
62
+
63
+ const res = await client.editLinkedIssues(87, { remove: [12] }, pin);
64
+
65
+ assert.deepEqual(res, { ok: false, error: MARKUP_LINK },
66
+ 'the line is an example of what this panel writes, not a declaration it may delete');
67
+ assert.deepEqual(writes, [], 'no pr edit child runs for a refused removal');
68
+ });
69
+
70
+ test('T18/D27: a marked line inside an HTML comment is never deleted', async () => {
71
+ const body = `Intro\n<!--\ntemplate example:\nCloses #12 ${MARK}\n-->\nTail\n`;
72
+ const { client, writes } = recorder({ body });
73
+
74
+ const res = await client.editLinkedIssues(87, { remove: [12] }, pin);
75
+
76
+ assert.deepEqual(res, { ok: false, error: MARKUP_LINK }, 'a commented-out example is not a declaration');
77
+ assert.deepEqual(writes, [], 'no pr edit child runs for a refused removal');
78
+ });
79
+
80
+ test('T18/D27: a marked line outside the fence is still the line a removal deletes', async () => {
81
+ const body = `Intro\n\`\`\`text\nCloses #12\n\`\`\`\nCloses #12 ${MARK}\nTail\n`;
82
+ const { client, writes } = recorder({ body });
83
+
84
+ const res = await client.editLinkedIssues(87, { remove: [12] }, pin);
85
+
86
+ assert.equal(res.ok, true, res.error);
87
+ assert.deepEqual(writes, [['pr', 'edit', '87', '--body', 'Intro\n```text\nCloses #12\n```\nTail\n']],
88
+ 'only the real marked line outside the example is removed');
89
+ });
90
+
91
+ // ---------------------------------------------------------------------------
92
+ // Defect 2: a human line that merely LOOKED like the panel's line was deleted.
93
+ // Only `Closes #N <!-- odd-worktree:linked -->`, byte for byte, is the panel's.
94
+ // ---------------------------------------------------------------------------
95
+ test('T18/D27: a look-alike line is refused, never deleted', async () => {
96
+ const lookAlikes = [
97
+ `CLOSES #12 ${MARK}`,
98
+ `closes #12 ${MARK}`,
99
+ `Fix #12 ${MARK}`,
100
+ `Closes #12 ${MARK}`,
101
+ `Closes #12 ${MARK}`,
102
+ `Closes #12 ${MARK} `,
103
+ `Closes #12 ${MARK}\t`,
104
+ `Closes #012 ${MARK}`,
105
+ ];
106
+ for (const lookAlike of lookAlikes) {
107
+ const body = `Intro\n${lookAlike}\nTail\n`;
108
+ const { client, writes } = recorder({ body });
109
+
110
+ const res = await client.editLinkedIssues(87, { remove: [12] }, pin);
111
+
112
+ assert.deepEqual(res, { ok: false, error: LINK_NOT_OURS }, `${JSON.stringify(lookAlike)} is not this panel's line`);
113
+ assert.deepEqual(writes, [], `${JSON.stringify(lookAlike)} is never rewritten`);
114
+ }
115
+ });
116
+
117
+ test('T18/D27: the exact marked line is deleted, CRLF ending included', async () => {
118
+ const body = `Intro\r\nCloses #12 ${MARK}\r\nTail\r\n`;
119
+ const { client, writes } = recorder({ body });
120
+
121
+ const res = await client.editLinkedIssues(87, { remove: [12] }, pin);
122
+
123
+ assert.equal(res.ok, true, res.error);
124
+ assert.deepEqual(writes, [['pr', 'edit', '87', '--body', 'Intro\r\nTail\r\n']],
125
+ 'a CRLF line is the panel\'s own line too, and the CR that ends it is tolerated');
126
+ });
127
+
128
+ // ---------------------------------------------------------------------------
129
+ // Defect 3: an ADD appended its line inside an unclosed four-backtick fence,
130
+ // because the structure check counted runs instead of pairing them.
131
+ // ---------------------------------------------------------------------------
132
+ test('T18/D27: an unclosed four-backtick fence refuses the add, and the removal too', async () => {
133
+ const bodies = [
134
+ `Intro\n\`\`\`\`markdown\n\`\`\`sh\nmake install\n`,
135
+ `Intro\n\`\`\`\`text\n~~~\na tilde example\n`,
136
+ `Intro\n\`\`\`\`\n\`\`\`\nCloses #12 ${MARK}\n`,
137
+ ];
138
+ for (const body of bodies) {
139
+ for (const changes of [{ add: [14] }, { remove: [12] }]) {
140
+ const { client, writes } = recorder({ body });
141
+
142
+ const res = await client.editLinkedIssues(87, changes, pin);
143
+
144
+ assert.deepEqual(res, { ok: false, error: UNREADABLE }, `${JSON.stringify(changes)} on ${JSON.stringify(body)}`);
145
+ assert.deepEqual(writes, [], 'nothing is written into a fence that is still open');
146
+ }
147
+ }
148
+ });
149
+
150
+ test('T18/D27: a paired four-backtick fence still lets the real line go', async () => {
151
+ const body = `Intro\n\`\`\`\`md\n\`\`\`text\nCloses #19\n\`\`\`\n\`\`\`\`\nCloses #12 ${MARK}\n`;
152
+ const { client, writes } = recorder({ body });
153
+
154
+ const res = await client.editLinkedIssues(87, { remove: [12] }, pin);
155
+
156
+ assert.equal(res.ok, true, res.error);
157
+ assert.deepEqual(writes, [['pr', 'edit', '87', '--body', 'Intro\n````md\n```text\nCloses #19\n```\n````\n']],
158
+ 'the inner three-backtick run is content of the four-backtick fence');
159
+ });
160
+
161
+ // ---------------------------------------------------------------------------
162
+ // Defect 4: the body was written after the pull request had been closed or
163
+ // merged during the re-reads.
164
+ // ---------------------------------------------------------------------------
165
+ test('T18/D27: the write-time re-read carries the state', async () => {
166
+ const { client, children, writes } = recorder({ body: 'Intro\nTail\n' });
167
+
168
+ const res = await client.editLinkedIssues(87, { add: [14] }, pin);
169
+
170
+ assert.equal(res.ok, true, res.error);
171
+ assert.equal(writes.length, 1, 'the edit ran once');
172
+ const reads = bodyReads(children);
173
+ assert.equal(reads.length, 2, 'the body is read twice');
174
+ assert.ok(reads[1].argv.includes('body,state'),
175
+ `the re-read asks for the state too, not ${JSON.stringify(reads[1].argv)}`);
176
+ });
177
+
178
+ test('T18/D27: a pull request that is no longer open refuses the write and writes nothing', async () => {
179
+ for (const state of ['MERGED', 'CLOSED']) {
180
+ let reads = 0;
181
+ const { client, writes } = recorder({
182
+ read: () => (++reads === 1 ? { body: 'Intro\nTail\n', state: 'OPEN' } : { body: 'Intro\nTail\n', state }),
183
+ });
184
+
185
+ const res = await client.editLinkedIssues(87, { add: [14] }, pin);
186
+
187
+ assert.deepEqual(res, { ok: false, error: NOT_OPEN }, `${state} refuses the body edit`);
188
+ assert.deepEqual(writes, [], `no pr edit child runs for a ${state} pull request`);
189
+ }
190
+ });
191
+
192
+ test('T18/D27: the re-read must prove the state, never assume it', async () => {
193
+ let reads = 0;
194
+ const { client, writes } = recorder({
195
+ read: () => (++reads === 1 ? { body: 'Intro\nTail\n', state: 'OPEN' } : { body: 'Intro\nTail\n' }),
196
+ });
197
+
198
+ const res = await client.editLinkedIssues(87, { add: [14] }, pin);
199
+
200
+ assert.equal(res.ok, false, 'a re-read without a state proves nothing');
201
+ assert.match(String(res.error), /returned an unexpected shape/);
202
+ assert.deepEqual(writes, [], 'no pr edit child runs on a re-read that could not be read');
203
+ });
204
+
205
+ test('T18/D27: the body comparison stays exact', async () => {
206
+ let reads = 0;
207
+ const { client, writes } = recorder({
208
+ read: () => (++reads === 1
209
+ ? { body: 'Intro\nTail\n', state: 'OPEN' }
210
+ : { body: 'Intro\nTail ', state: 'OPEN' }),
211
+ });
212
+
213
+ const res = await client.editLinkedIssues(87, { add: [14] }, pin);
214
+
215
+ assert.deepEqual(res, { ok: false, error: BODY_CHANGED }, 'one byte of difference is still a difference');
216
+ assert.deepEqual(writes, [], 'an author\'s edit is never overwritten');
217
+ });
@@ -0,0 +1,127 @@
1
+ 'use strict';
2
+ const { test } = require('node:test');
3
+ const assert = require('node:assert/strict');
4
+ const { createPrsClient } = require('../lib/gh-prs');
5
+ const { createPrsView } = require('../lib/prs-view');
6
+
7
+ const pin = { scope: 'github.com/octo/repo' };
8
+ const ok = (x) => ({ status: 0, stdout: JSON.stringify(x) });
9
+ const tick = () => new Promise((resolve) => setImmediate(resolve));
10
+ const row = (number) => ({ number, title: `PR ${number}`, state: 'open', isDraft: false });
11
+ const send = (view, ...keys) => keys.forEach((key) => view.handleInput(key));
12
+ // The body read answers the pull request STATE with the body (S2/T18).
13
+ function mockClient(repository, bodyRead = ok({body: 'PR body', state: 'OPEN'})) {
14
+ const writes = [];
15
+ const runGh = async (argv) => {
16
+ // The role read answers the fresh pull request state in the same round trip
17
+ // (S2/D20), so a write is refused for a target that is no longer open.
18
+ if (argv[0] === 'api') return ok({data: {repository: {...repository, pullRequest: repository.pullRequest || {state: 'OPEN'}}}});
19
+ if (argv[0] === 'pr' && argv[1] === 'view') return bodyRead;
20
+ writes.push(argv);
21
+ return ok({});
22
+ };
23
+ return { client: createPrsClient({issues: {runGh}}), writes };
24
+ }
25
+
26
+ for (const [action, call] of Object.entries({
27
+ close: (c) => c.closePullRequest(87, pin),
28
+ createLabel: (c) => c.createLabel({name:'needs-work'}, pin),
29
+ editLabels: (c) => c.editLabels(87, {add:['needs-work']}, pin),
30
+ editLinkedIssues: (c) => c.editLinkedIssues(87, {add:[12]}, pin),
31
+ editAssignees: (c) => c.editAssignees(87, {remove:['alice']}, pin),
32
+ })) {
33
+ test(`S2: ${action} rechecks READ before issuing a write`, async () => {
34
+ const {client, writes} = mockClient({viewerPermission:'READ'});
35
+ const result = await call(client);
36
+ assert.deepEqual(writes, []);
37
+ assert.equal(result.ok, false);
38
+ });
39
+ }
40
+
41
+ test('S2: merge refuses missing draft metadata', async () => {
42
+ const {client, writes} = mockClient({viewerPermission:'WRITE', mergeCommitAllowed:true, pullRequest:{}});
43
+ const result = await client.mergePullRequest(87, {method:'merge'}, pin);
44
+ assert.deepEqual(writes, []);
45
+ assert.equal(result.ok, false);
46
+ });
47
+
48
+ // S2 (D27): the body edit no longer interprets Markdown. A line the panel wrote
49
+ // carries its marker, only a marked line is ever removed, and the fenced example
50
+ // — which carries none — survives because it was never a candidate. The added
51
+ // link is a marked line on a line of its own.
52
+ test('S2: a linked edit removes only the marked line, never a fenced Markdown example', async () => {
53
+ const body = 'Intro 🧪\r\n```text\r\nCloses #12\r\n```\nCloses #12 <!-- odd-worktree:linked -->\r\nKeep me \t\nend';
54
+ const wanted = `${body.replace('```\nCloses #12 <!-- odd-worktree:linked -->\r\n','```\n')}\r\n\r\nCloses #14 <!-- odd-worktree:linked -->\r\n`;
55
+ const {client, writes} = mockClient({viewerPermission:'WRITE'}, ok({body, state: 'OPEN'}));
56
+ await client.editLinkedIssues(87, {add:[14], remove:[12]}, pin);
57
+ assert.deepEqual(writes, [['pr','edit','87','--body', wanted]]);
58
+ });
59
+
60
+ test('S2: close never acts on an asynchronously substituted PR', async () => {
61
+ const rows = [row(87),row(88)];
62
+ let calls = 0, finish;
63
+ const closed = [];
64
+ const view = createPrsView({
65
+ loadPullRequestPage: () => ++calls === 1 ? {ok:true, rows} : new Promise(resolve => { finish = resolve; }),
66
+ loadPullRequest: (number) => ({ok:true, pullRequest:{...row(number), permissions:{canClosePullRequests:true}}}),
67
+ actions: {close: (n) => { closed.push(n); return {ok:true}; }},
68
+ rowsAvailable: () => 30,
69
+ });
70
+ await tick();
71
+ send(view,'down','up','r','x');
72
+ assert.match(view.render(120).join('\n'),/Confirm close #87/);
73
+ finish({ok:true, rows:[rows[1],rows[0]]});
74
+ await tick();
75
+ send(view,'enter');
76
+ assert.ok(closed.length === 0 || closed[0] === 87, `closed ${closed}`);
77
+ });
78
+
79
+ test('S2: typing letters in the merge editor does not navigate its fields', async () => {
80
+ const writes = [];
81
+ const view = createPrsView({pages:[{id:'mine',rows:[{
82
+ ...row(87), permissions:{canMerge:true}, mergeMethods:{merge:true},
83
+ mergeDefaults:{merge:{subject:'M',body:'B'}},
84
+ }]}], actions:{merge:(...args)=>{ writes.push(args); return {ok:true}; }}});
85
+ send(view,'m','tab','backspace','j','k','tab','backspace','j','k','ctrl+s');
86
+ // S2/D20: the merge re-reads its own detail before the seam is reached, so
87
+ // the write settles on the next turn rather than inside the key press.
88
+ await tick();
89
+ assert.deepEqual(writes, [[87,{method:'merge',subject:'jk',body:'jk'}]]);
90
+ });
91
+
92
+ test('S2: an open editor cannot save after a capability is revoked', async () => {
93
+ const writes = [];
94
+ for (const [key, capability, seam, edit] of [
95
+ ['l','canEditLabels','editLabels',['space']],
96
+ ['a','canEditAssignees','editAssignees',['space']],
97
+ ['i','canEditLinkedIssues','editLinkedIssues',['n','1','2','enter']],
98
+ ]) {
99
+ let calls = 0, finish, permission = true;
100
+ const rows = [row(87)];
101
+ const view = createPrsView({
102
+ loadPullRequestPage: () => ++calls === 1 ? {ok:true, rows} : new Promise(resolve => { finish = resolve; }),
103
+ loadPullRequest: (n) => ({ok:true,pullRequest:{...row(n),permissions:{[capability]:permission}}}),
104
+ labels:[{name:'bug'}], loadAssignees:()=>({ok:true,users:[{login:'alice'}]}),
105
+ actions:{[seam]:(...args)=>{writes.push([seam,args]);return {ok:true};}},
106
+ });
107
+ await tick();
108
+ send(view,'r',key);
109
+ await tick();
110
+ send(view,...edit);
111
+ permission = false;
112
+ finish({ok:true, rows});
113
+ await tick();
114
+ send(view,'ctrl+s');
115
+ }
116
+ assert.deepEqual(writes, []);
117
+ });
118
+
119
+ test('S6: numbered open fetches a linked PR outside every scoped bucket', async () => {
120
+ const fetched = [];
121
+ createPrsView({initialPullRequest:87,
122
+ loadPullRequestPage:({bucketId})=>({ok:true,rows:bucketId==='all'?[row(87)]:[row(88)]}),
123
+ loadPullRequest:(n)=>{fetched.push(n);return {ok:true,pullRequest:row(n)};},
124
+ });
125
+ await tick();
126
+ assert.ok(fetched.includes(87));
127
+ });