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.
- package/README.md +63 -40
- package/extensions/odd-worktree-tools.ts +859 -12
- package/lib/gh-issues.js +65 -10
- package/lib/gh-prs.js +2011 -0
- package/lib/issue-preview.js +70 -4
- package/lib/issues-cache-store.js +4 -1
- package/lib/issues-section.js +346 -23
- package/lib/issues-view.js +116 -5
- package/lib/pr-preview.js +353 -0
- package/lib/prs-rail-store.js +317 -0
- package/lib/prs-view.js +2896 -0
- package/package.json +1 -1
- package/tools/files-bridge.test.js +2 -2
- package/tools/files-command.test.js +1 -1
- package/tools/fixtures/gh-pr-connection-page.json +84 -0
- package/tools/fixtures/gh-pr-metadata.json +29 -0
- package/tools/fixtures/gh-pr-rail.json +61 -0
- package/tools/fixtures/gh-pr-search-page.json +82 -0
- package/tools/fixtures/gh-pr-view.json +159 -0
- package/tools/gh-issues.test.js +4 -2
- package/tools/gh-prs.test.js +1828 -0
- package/tools/issue-preview.test.js +119 -0
- package/tools/issues-cache-store.test.js +2 -2
- package/tools/issues-command.test.js +10 -3
- package/tools/issues-open-guard.test.js +15 -0
- package/tools/issues-section.test.js +618 -9
- package/tools/issues-view.test.js +297 -0
- package/tools/package-surface.test.js +18 -4
- package/tools/pr-preview.test.js +281 -0
- package/tools/prs-command.test.js +1280 -0
- package/tools/prs-rail-store.test.js +343 -0
- package/tools/prs-t10-safety.test.js +326 -0
- package/tools/prs-t12-safety.test.js +348 -0
- package/tools/prs-t13-independent.test.js +378 -0
- package/tools/prs-t14-safety.test.js +340 -0
- package/tools/prs-t15-independent.test.js +259 -0
- package/tools/prs-t18-safety.test.js +217 -0
- package/tools/prs-t7-safety.test.js +127 -0
- package/tools/prs-view.test.js +1462 -0
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// T4 (S4, S5): lib/prs-rail-store.js — the DISK half of the rail's pull-request
|
|
3
|
+
// read.
|
|
4
|
+
//
|
|
5
|
+
// Contract under test, through the public surface only (`load`, `save`,
|
|
6
|
+
// `directory`):
|
|
7
|
+
// - ONE small versioned JSON document holds an entry per repository identity,
|
|
8
|
+
// keyed exactly as the rail keys its reads (`<host>/<owner>/<repo>`) and under
|
|
9
|
+
// `$XDG_CACHE_HOME/odd-worktree/` (falling back to `~/.cache/odd-worktree/`),
|
|
10
|
+
// so a cold start paints the `pr` count before any child runs;
|
|
11
|
+
// - an entry round-trips the two buckets the rail counts — the viewer's own pull
|
|
12
|
+
// requests (`mine`) and the ones they must review (`review`) — each row's
|
|
13
|
+
// number and the issues it links (`linkedIssues`), plus `syncedAt`;
|
|
14
|
+
// - the count is DERIVED from those rows (the distinct numbers in mine ∪
|
|
15
|
+
// review), so a stored count can never contradict the rows it is painted from
|
|
16
|
+
// and persistence cannot turn a stale entry into a fresh one;
|
|
17
|
+
// - the document is bounded: the oldest identities are dropped past eight;
|
|
18
|
+
// - every failure is silence, never an exception and never a half-written
|
|
19
|
+
// document: a malformed document, a wrong version, a hostile entry, an
|
|
20
|
+
// unreadable directory, a failed atomic write and a machine with no home all
|
|
21
|
+
// answer `null`/`false`;
|
|
22
|
+
// - the write is atomic: a temp file in the same directory and a rename, so a
|
|
23
|
+
// failure leaves the previous document exactly as it was.
|
|
24
|
+
//
|
|
25
|
+
// The `fs` seam and the directory are injected, so nothing here touches a real
|
|
26
|
+
// cache directory and every temp tree lives under os.tmpdir() and is removed.
|
|
27
|
+
|
|
28
|
+
const { test, after } = require('node:test');
|
|
29
|
+
const assert = require('node:assert/strict');
|
|
30
|
+
const fs = require('node:fs');
|
|
31
|
+
const os = require('node:os');
|
|
32
|
+
const path = require('node:path');
|
|
33
|
+
|
|
34
|
+
const { createPrsRailStore } = require('../lib/prs-rail-store.js');
|
|
35
|
+
|
|
36
|
+
const FILE = 'prs-rail.json';
|
|
37
|
+
const IDENTITY = 'github.com/myfork/project';
|
|
38
|
+
const OTHER = 'github.com/myfork/other';
|
|
39
|
+
|
|
40
|
+
const roots = [];
|
|
41
|
+
after(() => {
|
|
42
|
+
for (const dir of roots) {
|
|
43
|
+
try { fs.rmSync(dir, { recursive: true, force: true }); } catch {}
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
function tempDir(tag = 'store') {
|
|
48
|
+
const dir = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), `odd-prs-${tag}-`)));
|
|
49
|
+
roots.push(dir);
|
|
50
|
+
return dir;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function row(number, linkedIssues = []) {
|
|
54
|
+
return { number, title: `Pull ${number}`, updatedAt: '2024-01-01T00:00:00Z', linkedIssues };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// What the loader hands over after a successful rail read: the two buckets and
|
|
58
|
+
// when it read them.
|
|
59
|
+
function snapshot(overrides = {}) {
|
|
60
|
+
return {
|
|
61
|
+
mine: [row(69, [63])],
|
|
62
|
+
review: [row(70, [64, 65]), row(69, [63])],
|
|
63
|
+
syncedAt: 1000,
|
|
64
|
+
...overrides,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function documentAt(dir) {
|
|
69
|
+
return JSON.parse(fs.readFileSync(path.join(dir, FILE), 'utf8'));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function store(dir, extra = {}) {
|
|
73
|
+
return createPrsRailStore({ dir, env: {}, ...extra });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
test('S4: an entry round-trips through ONE small versioned document', async () => {
|
|
77
|
+
const dir = tempDir('store-roundtrip');
|
|
78
|
+
const cache = store(dir);
|
|
79
|
+
assert.equal(await cache.save(IDENTITY, snapshot()), true);
|
|
80
|
+
|
|
81
|
+
const entry = await cache.load(IDENTITY);
|
|
82
|
+
assert.ok(entry, 'the entry the save wrote is the entry the load answers');
|
|
83
|
+
assert.deepEqual(entry.mine.map((item) => item.number), [69]);
|
|
84
|
+
assert.deepEqual(entry.review.map((item) => item.number), [70, 69],
|
|
85
|
+
'a pull request in both buckets keeps its place in both');
|
|
86
|
+
assert.deepEqual(entry.mine[0].linkedIssues, [63], 'the linked issues survive, not just the number');
|
|
87
|
+
assert.deepEqual(entry.review[0].linkedIssues, [64, 65]);
|
|
88
|
+
assert.equal(entry.syncedAt, 1000);
|
|
89
|
+
|
|
90
|
+
assert.deepEqual(fs.readdirSync(dir), [FILE], 'exactly one document, no stray file');
|
|
91
|
+
const doc = documentAt(dir);
|
|
92
|
+
assert.equal(doc.version, 2, 'the document is versioned');
|
|
93
|
+
assert.equal(doc.identities.length, 1);
|
|
94
|
+
assert.equal(doc.identities[0].identity, IDENTITY, 'the entry is keyed by the repository identity');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// S4: the header paints the distinct numbers in mine ∪ review, never a sum and
|
|
98
|
+
// never a number read out of the document — 69 is in both buckets and counts
|
|
99
|
+
// once.
|
|
100
|
+
test('S4: the count is the distinct union of the two buckets, derived from the rows', async () => {
|
|
101
|
+
const dir = tempDir('store-count');
|
|
102
|
+
const cache = store(dir);
|
|
103
|
+
await cache.save(IDENTITY, snapshot());
|
|
104
|
+
assert.equal((await cache.load(IDENTITY)).count, 2, '69 in both buckets is one pull request');
|
|
105
|
+
|
|
106
|
+
await cache.save(IDENTITY, snapshot({ mine: [], review: [] }));
|
|
107
|
+
assert.equal((await cache.load(IDENTITY)).count, 0, 'no pull request is a real count');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test('S4: a stored count that contradicts its rows is never trusted', async () => {
|
|
111
|
+
const dir = tempDir('store-forged-count');
|
|
112
|
+
const cache = store(dir);
|
|
113
|
+
await cache.save(IDENTITY, snapshot());
|
|
114
|
+
const doc = documentAt(dir);
|
|
115
|
+
doc.identities[0].count = 99;
|
|
116
|
+
doc.identities[0].mine = [row(69), row(71)];
|
|
117
|
+
fs.writeFileSync(path.join(dir, FILE), JSON.stringify(doc));
|
|
118
|
+
|
|
119
|
+
const entry = await cache.load(IDENTITY);
|
|
120
|
+
assert.equal(entry.count, 3, 'the rows decide the count (69, 70, 71), never the 99 written beside them');
|
|
121
|
+
assert.deepEqual(entry.mine.map((item) => item.number), [69, 71]);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test('S4: the document lives under $XDG_CACHE_HOME, else under the injected home', async () => {
|
|
125
|
+
const base = tempDir('store-xdg');
|
|
126
|
+
const fromXdg = createPrsRailStore({ env: { XDG_CACHE_HOME: base } });
|
|
127
|
+
assert.equal(fromXdg.directory(), path.join(base, 'odd-worktree'));
|
|
128
|
+
await fromXdg.save(IDENTITY, snapshot());
|
|
129
|
+
assert.ok(fs.existsSync(path.join(base, 'odd-worktree', FILE)), 'the document is inside the XDG directory');
|
|
130
|
+
|
|
131
|
+
const home = tempDir('store-home');
|
|
132
|
+
const fromHome = createPrsRailStore({ env: {}, home });
|
|
133
|
+
assert.equal(fromHome.directory(), path.join(home, '.cache', 'odd-worktree'),
|
|
134
|
+
'a machine without XDG falls back to ~/.cache');
|
|
135
|
+
await fromHome.save(IDENTITY, snapshot());
|
|
136
|
+
assert.ok(fs.existsSync(path.join(home, '.cache', 'odd-worktree', FILE)));
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test('S4: the document is bounded — the oldest identities are dropped past the cap', async () => {
|
|
140
|
+
const dir = tempDir('store-bound');
|
|
141
|
+
const cache = store(dir);
|
|
142
|
+
for (let index = 1; index <= 9; index += 1) {
|
|
143
|
+
await cache.save(`github.com/myfork/project-${index}`, snapshot({ syncedAt: index }));
|
|
144
|
+
}
|
|
145
|
+
assert.equal(await cache.load('github.com/myfork/project-1'), null, 'the oldest identity is gone');
|
|
146
|
+
for (let index = 2; index <= 9; index += 1) {
|
|
147
|
+
assert.ok(await cache.load(`github.com/myfork/project-${index}`), `identity ${index} is kept`);
|
|
148
|
+
}
|
|
149
|
+
assert.equal(documentAt(dir).identities.length, 8, 'the document holds eight identities at most');
|
|
150
|
+
|
|
151
|
+
// Re-saving an identity moves it to the newest end, so it outlives a later save.
|
|
152
|
+
await cache.save('github.com/myfork/project-2', snapshot({ syncedAt: 20 }));
|
|
153
|
+
await cache.save('github.com/myfork/project-10', snapshot({ syncedAt: 10 }));
|
|
154
|
+
assert.ok(await cache.load('github.com/myfork/project-2'), 'a refreshed identity is the newest, not the oldest');
|
|
155
|
+
assert.equal(await cache.load('github.com/myfork/project-3'), null, 'and the identity that was oldest goes');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test('S4: a malformed document is silence, and the next save replaces it', async () => {
|
|
159
|
+
const dir = tempDir('store-malformed');
|
|
160
|
+
fs.writeFileSync(path.join(dir, FILE), '{ this is not json');
|
|
161
|
+
const cache = store(dir);
|
|
162
|
+
assert.equal(await cache.load(IDENTITY), null, 'a document that does not parse answers nothing');
|
|
163
|
+
assert.equal(await cache.save(IDENTITY, snapshot()), true);
|
|
164
|
+
assert.ok(await cache.load(IDENTITY));
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test('S4: a wrong version is silence, and the next save writes the current one', async () => {
|
|
168
|
+
const dir = tempDir('store-version');
|
|
169
|
+
fs.writeFileSync(path.join(dir, FILE), JSON.stringify({
|
|
170
|
+
version: 99,
|
|
171
|
+
identities: [{ identity: IDENTITY, mine: [row(69)], review: [], syncedAt: 1 }],
|
|
172
|
+
}));
|
|
173
|
+
const cache = store(dir);
|
|
174
|
+
assert.equal(await cache.load(IDENTITY), null, 'a version this module does not know is never read');
|
|
175
|
+
assert.equal(await cache.save(IDENTITY, snapshot()), true);
|
|
176
|
+
assert.equal(documentAt(dir).version, 2);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('S4: a hostile entry is dropped and every other identity still answers', async () => {
|
|
180
|
+
const dir = tempDir('store-hostile');
|
|
181
|
+
const cache = store(dir);
|
|
182
|
+
await cache.save(OTHER, snapshot());
|
|
183
|
+
const doc = documentAt(dir);
|
|
184
|
+
doc.identities.push({ identity: IDENTITY, mine: 'not rows', review: [], syncedAt: 1 });
|
|
185
|
+
fs.writeFileSync(path.join(dir, FILE), JSON.stringify(doc));
|
|
186
|
+
|
|
187
|
+
assert.equal(await cache.load(IDENTITY), null, 'an entry whose shape is wrong is not a cached read');
|
|
188
|
+
assert.ok(await cache.load(OTHER), 'the neighbour it was written beside is untouched');
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// S4: a row the rail would drop is dropped here too, so nothing counts a pull
|
|
192
|
+
// request that has no number or links an issue that is not one.
|
|
193
|
+
test('S4: unusable rows and linked issues are dropped, and the count follows the survivors', async () => {
|
|
194
|
+
const dir = tempDir('store-rows');
|
|
195
|
+
const cache = store(dir);
|
|
196
|
+
await cache.save(IDENTITY, {
|
|
197
|
+
mine: [
|
|
198
|
+
{ number: 69, linkedIssues: [63, 'x', 0, 63, 64] },
|
|
199
|
+
{ number: 0, linkedIssues: [1] },
|
|
200
|
+
{ number: 'seventy', linkedIssues: [] },
|
|
201
|
+
'not a row',
|
|
202
|
+
],
|
|
203
|
+
review: [{ number: 70, linkedIssues: 'not links' }],
|
|
204
|
+
syncedAt: 2000,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const entry = await cache.load(IDENTITY);
|
|
208
|
+
assert.deepEqual(entry.mine.map((item) => item.number), [69], 'only a positive integer number is a row');
|
|
209
|
+
assert.deepEqual(entry.mine[0].linkedIssues, [63, 64], 'links are positive integers, in order, without repeats');
|
|
210
|
+
assert.deepEqual(entry.review[0].linkedIssues, [], 'a bucket that is not a link list links nothing');
|
|
211
|
+
assert.equal(entry.count, 2, 'the dropped rows count nothing');
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test('S4: a failed atomic write leaves the previous document and never throws', async () => {
|
|
215
|
+
const dir = tempDir('store-atomic');
|
|
216
|
+
const cache = store(dir);
|
|
217
|
+
await cache.save(IDENTITY, snapshot({ syncedAt: 1000 }));
|
|
218
|
+
|
|
219
|
+
const failing = store(dir, {
|
|
220
|
+
fs: {
|
|
221
|
+
...fs.promises,
|
|
222
|
+
rename: async () => { throw Object.assign(new Error('EXDEV: cross-device link'), { code: 'EXDEV' }); },
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
assert.equal(await failing.save(IDENTITY, snapshot({ syncedAt: 9000 })), false,
|
|
226
|
+
'a write that cannot be committed answers false instead of throwing');
|
|
227
|
+
|
|
228
|
+
const entry = await cache.load(IDENTITY);
|
|
229
|
+
assert.equal(entry.syncedAt, 1000, 'the previous document is exactly as it was');
|
|
230
|
+
assert.deepEqual(fs.readdirSync(dir), [FILE], 'and the temp file it wrote is not left behind');
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test('S4: an unreadable directory or a missing document is silence', async () => {
|
|
234
|
+
const dir = tempDir('store-unreadable');
|
|
235
|
+
const missing = store(path.join(dir, 'not-created-yet'));
|
|
236
|
+
assert.equal(await missing.load(IDENTITY), null, 'a missing document is nothing to paint');
|
|
237
|
+
|
|
238
|
+
const unreadable = store(dir, {
|
|
239
|
+
fs: {
|
|
240
|
+
...fs.promises,
|
|
241
|
+
readFile: async () => { throw Object.assign(new Error('EACCES: permission denied'), { code: 'EACCES' }); },
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
assert.equal(await unreadable.load(IDENTITY), null, 'an unreadable directory answers nothing');
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test('S4: an fs seam that throws from every call is silence, never an exception', async () => {
|
|
248
|
+
const thrower = async () => { throw new Error('EIO: i/o error'); };
|
|
249
|
+
const broken = createPrsRailStore({
|
|
250
|
+
dir: tempDir('store-broken'),
|
|
251
|
+
env: {},
|
|
252
|
+
fs: { readFile: thrower, writeFile: thrower, mkdir: thrower, rename: thrower, unlink: thrower },
|
|
253
|
+
});
|
|
254
|
+
assert.equal(await broken.load(IDENTITY), null);
|
|
255
|
+
assert.equal(await broken.save(IDENTITY, snapshot()), false);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test('S4: a machine with no home and no XDG has no cache directory, and says so by silence', async () => {
|
|
259
|
+
const cache = createPrsRailStore({ env: {}, home: null });
|
|
260
|
+
assert.equal(cache.directory(), null, 'there is no directory to name');
|
|
261
|
+
assert.equal(await cache.load(IDENTITY), null);
|
|
262
|
+
assert.equal(await cache.save(IDENTITY, snapshot()), false);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
test('S4: an identity this module cannot key is never read or written', async () => {
|
|
266
|
+
const dir = tempDir('store-identity');
|
|
267
|
+
const cache = store(dir);
|
|
268
|
+
assert.equal(await cache.load(''), null);
|
|
269
|
+
assert.equal(await cache.load(null), null);
|
|
270
|
+
assert.equal(await cache.save('', snapshot()), false);
|
|
271
|
+
assert.equal(await cache.save(IDENTITY, null), false, 'a snapshot that is not one is not written');
|
|
272
|
+
assert.deepEqual(fs.readdirSync(dir), [], 'nothing was written at all');
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// ---------------------------------------------------------------------------
|
|
276
|
+
// T10b (S4): D15 — the per-bucket TOTAL is stored beside the rows.
|
|
277
|
+
//
|
|
278
|
+
// A live read carries a window of rows plus an exact `issueCount` per bucket
|
|
279
|
+
// (D8): 50 rows with a total of 52 paints `52 pr`. The document used to keep the
|
|
280
|
+
// rows alone, so a restart painted `50 pr` — a different number for the same
|
|
281
|
+
// repository. The total now sleeps beside its rows, and the count is still
|
|
282
|
+
// DERIVED on load (rows plus the remainder of each total), never stored as a
|
|
283
|
+
// header count: a total below the rows it came with, or one that is not a count,
|
|
284
|
+
// is ignored and the rows stay the floor.
|
|
285
|
+
// ---------------------------------------------------------------------------
|
|
286
|
+
|
|
287
|
+
test('S4 (T10b, D15): a bucket total round-trips beside its rows, and the derived count keeps its remainder', async () => {
|
|
288
|
+
const dir = tempDir('store-totals');
|
|
289
|
+
const cache = store(dir);
|
|
290
|
+
const rows = Array.from({ length: 50 }, (_, index) => row(index + 1));
|
|
291
|
+
assert.equal(await cache.save(IDENTITY, {
|
|
292
|
+
mine: rows,
|
|
293
|
+
review: [],
|
|
294
|
+
counts: { mine: 52, review: 0 },
|
|
295
|
+
syncedAt: 1000,
|
|
296
|
+
}), true);
|
|
297
|
+
|
|
298
|
+
const entry = await cache.load(IDENTITY);
|
|
299
|
+
assert.deepEqual(entry.counts, { mine: 52, review: 0 }, 'each total is stored beside the rows it counts');
|
|
300
|
+
assert.equal(entry.count, 52,
|
|
301
|
+
'the count the header paints is derived on load: the 50 rows plus the 2 the window cut');
|
|
302
|
+
assert.equal(entry.mine.length, 50, 'and the rows are still exactly what the read carried');
|
|
303
|
+
assert.deepEqual(Object.keys(documentAt(dir).identities[0]).includes('count'), false,
|
|
304
|
+
'no header count is written: the document keeps rows and totals only');
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
test('S4 (T10b, D15): a total below its row count is ignored, and the rows stay the floor', async () => {
|
|
308
|
+
const dir = tempDir('store-totals-floor');
|
|
309
|
+
const cache = store(dir);
|
|
310
|
+
const rows = Array.from({ length: 50 }, (_, index) => row(index + 1));
|
|
311
|
+
await cache.save(IDENTITY, {
|
|
312
|
+
mine: rows,
|
|
313
|
+
review: [row(69)],
|
|
314
|
+
counts: { mine: 3, review: 'many' },
|
|
315
|
+
syncedAt: 1000,
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
const entry = await cache.load(IDENTITY);
|
|
319
|
+
assert.equal(entry.counts.mine, null, 'a total below the rows it came with proves nothing');
|
|
320
|
+
assert.equal(entry.counts.review, null, 'and neither does one that is not a count');
|
|
321
|
+
assert.equal(entry.count, 51, 'the distinct numbers the rows prove stay the floor');
|
|
322
|
+
|
|
323
|
+
// A document that carries the unproven totals itself is read the same way.
|
|
324
|
+
const doc = documentAt(dir);
|
|
325
|
+
doc.identities[0].counts = { mine: 2, review: 0 };
|
|
326
|
+
fs.writeFileSync(path.join(dir, FILE), JSON.stringify(doc));
|
|
327
|
+
assert.equal((await cache.load(IDENTITY)).count, 51, 'a hostile document cannot shrink the count either');
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
test('S4 (T10b, D15): a document of the previous version is silence, never an error', async () => {
|
|
331
|
+
const dir = tempDir('store-previous-version');
|
|
332
|
+
fs.writeFileSync(path.join(dir, FILE), JSON.stringify({
|
|
333
|
+
version: 1,
|
|
334
|
+
identities: [{ identity: IDENTITY, mine: [row(69)], review: [], syncedAt: 1 }],
|
|
335
|
+
}));
|
|
336
|
+
const cache = store(dir);
|
|
337
|
+
assert.equal(await cache.load(IDENTITY), null,
|
|
338
|
+
'the version that never carried the totals is not read as if it had');
|
|
339
|
+
|
|
340
|
+
assert.equal(await cache.save(IDENTITY, snapshot()), true);
|
|
341
|
+
assert.equal(documentAt(dir).version, 2, 'and the next write is the current format');
|
|
342
|
+
assert.ok(await cache.load(IDENTITY));
|
|
343
|
+
});
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// T10a (prs-panel S2): the defects the T9 independent verification recorded as
|
|
3
|
+
// D4 (again), D11, D12, D13 and D14, plus the decision that opens that verdict:
|
|
4
|
+
// comments stay available in every pull request state, while the editor, close
|
|
5
|
+
// and merge keys belong to an OPEN pull request.
|
|
6
|
+
//
|
|
7
|
+
// Every case here reproduces the defect through a public seam — the client's
|
|
8
|
+
// exported writes, or the panel's keyboard and painted frame — and never through
|
|
9
|
+
// the modules' internal storage.
|
|
10
|
+
|
|
11
|
+
const { test } = require('node:test');
|
|
12
|
+
const assert = require('node:assert/strict');
|
|
13
|
+
const { createPrsClient } = require('../lib/gh-prs');
|
|
14
|
+
const { createPrsView } = require('../lib/prs-view');
|
|
15
|
+
|
|
16
|
+
const pin = { scope: 'github.com/octo/repo' };
|
|
17
|
+
const ok = (x) => ({ status: 0, stdout: JSON.stringify(x), stderr: '', error: null });
|
|
18
|
+
const tick = () => new Promise((resolve) => setImmediate(resolve));
|
|
19
|
+
const row = (number, extra = {}) => ({ number, title: `PR ${number}`, state: 'open', isDraft: false, ...extra });
|
|
20
|
+
const send = (view, ...keys) => keys.forEach((key) => view.handleInput(key));
|
|
21
|
+
const frame = (view) => view.render(140).join('\n');
|
|
22
|
+
|
|
23
|
+
// The body read answers the pull request STATE with the body (S2/T18): the read
|
|
24
|
+
// right before the write refuses a pull request that is no longer open.
|
|
25
|
+
function mockClient(repository, bodyRead = ok({ body: 'PR body', state: 'OPEN' }), children = []) {
|
|
26
|
+
const writes = [];
|
|
27
|
+
const runGh = async (argv) => {
|
|
28
|
+
children.push(argv.slice());
|
|
29
|
+
// The role read answers the fresh pull request state in the same round trip
|
|
30
|
+
// (S2/D20), so a write is refused for a target that is no longer open.
|
|
31
|
+
if (argv[0] === 'api') return ok({ data: { repository: { ...repository, pullRequest: repository.pullRequest || { state: 'OPEN' } } } });
|
|
32
|
+
if (argv[0] === 'pr' && argv[1] === 'view') return bodyRead;
|
|
33
|
+
writes.push(argv);
|
|
34
|
+
return ok({});
|
|
35
|
+
};
|
|
36
|
+
return { client: createPrsClient({ issues: { runGh } }), writes };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// D4 (S2), amended by D27: the body edit no longer interprets Markdown. Every
|
|
41
|
+
// line the panel writes carries a marker, only a marked line is ever removed,
|
|
42
|
+
// and a body whose fence runs do not pair up is refused instead of guessed at.
|
|
43
|
+
// These cases keep their original bodies: the panel's own line now carries the
|
|
44
|
+
// marker, and the code examples are untouched because they are never candidates.
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
const MARK = '<!-- odd-worktree:linked -->';
|
|
47
|
+
test('S2: a four-backtick fence holding three backticks and Closes #19 is code', async () => {
|
|
48
|
+
const body = [
|
|
49
|
+
'Intro',
|
|
50
|
+
'````md',
|
|
51
|
+
'```text',
|
|
52
|
+
'Closes #19',
|
|
53
|
+
'```',
|
|
54
|
+
'````',
|
|
55
|
+
`Closes #19 ${MARK}`,
|
|
56
|
+
].join('\n');
|
|
57
|
+
const { client, writes } = mockClient({ viewerPermission: 'WRITE' }, ok({ body, state: 'OPEN' }));
|
|
58
|
+
|
|
59
|
+
const res = await client.editLinkedIssues(87, { remove: [19] }, pin);
|
|
60
|
+
|
|
61
|
+
assert.equal(res.ok, true, res.error);
|
|
62
|
+
assert.deepEqual(writes, [['pr', 'edit', '87', '--body', `${[
|
|
63
|
+
'Intro',
|
|
64
|
+
'````md',
|
|
65
|
+
'```text',
|
|
66
|
+
'Closes #19',
|
|
67
|
+
'```',
|
|
68
|
+
'````',
|
|
69
|
+
].join('\n')}\n`]], 'only the marked line outside the fence is removed');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('S2: a fence structure the reader cannot pair up refuses the edit outright', async () => {
|
|
73
|
+
// D27/T18: the runs of three or more backticks and tildes must PAIR UP by
|
|
74
|
+
// character and length — the tilde run that opens here is never closed (the
|
|
75
|
+
// backtick run is another character and `~~` is not a run at all) — so the
|
|
76
|
+
// body is refused instead of appended to, marked line and all.
|
|
77
|
+
const body = [
|
|
78
|
+
'~~~text',
|
|
79
|
+
'```',
|
|
80
|
+
'Closes #19',
|
|
81
|
+
'~~',
|
|
82
|
+
'Closes #20',
|
|
83
|
+
`Closes #20 ${MARK}`,
|
|
84
|
+
].join('\n');
|
|
85
|
+
const { client, writes } = mockClient({ viewerPermission: 'WRITE' }, ok({ body, state: 'OPEN' }));
|
|
86
|
+
|
|
87
|
+
const res = await client.editLinkedIssues(87, { remove: [20] }, pin);
|
|
88
|
+
|
|
89
|
+
assert.equal(res.ok, false);
|
|
90
|
+
assert.equal(res.error, 'the pull request body ends inside an unclosed code fence or comment');
|
|
91
|
+
assert.deepEqual(writes, [], 'a body this reader cannot pair up is never rewritten');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('S2: an added link is a marked line on a line of its own', async () => {
|
|
95
|
+
const body = [' ```', ' Closes #19', ' ```', 'Closes #20'].join('\n');
|
|
96
|
+
const { client, writes } = mockClient({ viewerPermission: 'WRITE' }, ok({ body, state: 'OPEN' }));
|
|
97
|
+
|
|
98
|
+
const res = await client.editLinkedIssues(87, { add: [19] }, pin);
|
|
99
|
+
|
|
100
|
+
assert.equal(res.ok, true, res.error);
|
|
101
|
+
assert.deepEqual(writes, [['pr', 'edit', '87', '--body', `${body}\n\nCloses #19 ${MARK}\n`]],
|
|
102
|
+
'the panel appends its own marked line, separated by a blank line');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('S2: a closing fence may carry trailing spaces and be longer than the opening one', async () => {
|
|
106
|
+
const body = ['```', 'Closes #19', '````` ', `Closes #19 ${MARK}`].join('\n');
|
|
107
|
+
const { client, writes } = mockClient({ viewerPermission: 'WRITE' }, ok({ body, state: 'OPEN' }));
|
|
108
|
+
|
|
109
|
+
const res = await client.editLinkedIssues(87, { remove: [19] }, pin);
|
|
110
|
+
|
|
111
|
+
assert.equal(res.ok, true, res.error);
|
|
112
|
+
assert.deepEqual(writes, [['pr', 'edit', '87', '--body', `${['```', 'Closes #19', '````` '].join('\n')}\n`]]);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
// D11 (S2): the composer captures the number it was opened for, exactly like
|
|
117
|
+
// the close and merge confirmations capture theirs.
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
test('S2: a comment is posted to the pull request the composer was opened on', async () => {
|
|
120
|
+
const rows = [row(42), row(43)];
|
|
121
|
+
let calls = 0;
|
|
122
|
+
let finish;
|
|
123
|
+
const posted = [];
|
|
124
|
+
const view = createPrsView({
|
|
125
|
+
loadPullRequestPage: () => (++calls === 1
|
|
126
|
+
? { ok: true, rows }
|
|
127
|
+
: new Promise((resolve) => { finish = resolve; })),
|
|
128
|
+
loadPullRequest: (number) => ({ ok: true, pullRequest: { ...row(number), permissions: { canEditLabels: true } } }),
|
|
129
|
+
actions: { postComment: (number, body) => { posted.push({ number, body }); return { ok: true }; } },
|
|
130
|
+
rowsAvailable: () => 30,
|
|
131
|
+
});
|
|
132
|
+
await tick();
|
|
133
|
+
send(view, 'r', 'c');
|
|
134
|
+
send(view, 'h', 'i');
|
|
135
|
+
finish({ ok: true, rows: [rows[1], rows[0]] });
|
|
136
|
+
await tick();
|
|
137
|
+
send(view, 'ctrl+s');
|
|
138
|
+
await tick();
|
|
139
|
+
assert.deepEqual(posted, [{ number: 42, body: 'hi' }], 'the composer posted to the number it named');
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// ---------------------------------------------------------------------------
|
|
143
|
+
// D12 (S2): the merge step names and highlights the pull request it will merge,
|
|
144
|
+
// and a rebase merge offers neither a commit message nor a description.
|
|
145
|
+
// ---------------------------------------------------------------------------
|
|
146
|
+
test('S2: the merge step names the captured number and highlights that row', async () => {
|
|
147
|
+
const rows = [row(87), row(88)];
|
|
148
|
+
let calls = 0;
|
|
149
|
+
let finish;
|
|
150
|
+
const merged = [];
|
|
151
|
+
const view = createPrsView({
|
|
152
|
+
loadPullRequestPage: () => (++calls === 1
|
|
153
|
+
? { ok: true, rows }
|
|
154
|
+
: new Promise((resolve) => { finish = resolve; })),
|
|
155
|
+
loadPullRequest: (number) => ({
|
|
156
|
+
ok: true,
|
|
157
|
+
pullRequest: {
|
|
158
|
+
...row(number),
|
|
159
|
+
permissions: { canMerge: true },
|
|
160
|
+
mergeMethods: { merge: true },
|
|
161
|
+
mergeDefaults: { merge: { subject: `subject ${number}`, body: `body ${number}` } },
|
|
162
|
+
},
|
|
163
|
+
}),
|
|
164
|
+
actions: { merge: (number, options) => { merged.push({ number, options }); return { ok: true }; } },
|
|
165
|
+
rowsAvailable: () => 30,
|
|
166
|
+
});
|
|
167
|
+
await tick();
|
|
168
|
+
send(view, 'r', 'm');
|
|
169
|
+
finish({ ok: true, rows: [rows[1], rows[0]] });
|
|
170
|
+
await tick();
|
|
171
|
+
const painted = frame(view);
|
|
172
|
+
assert.match(painted, /Confirm merge #87/, 'the confirmation names the number it will merge');
|
|
173
|
+
const left = view.render(140).map((line) => String(line).replace(/\x1b\[[0-9;]*m/g, '').split('│')[1] || '').join('\n');
|
|
174
|
+
assert.match(left, /▸ PR 87/, 'the row that will merge carries the cursor');
|
|
175
|
+
assert.doesNotMatch(left, /▸ PR 88/, 'no other row is highlighted');
|
|
176
|
+
send(view, 'ctrl+s');
|
|
177
|
+
await tick();
|
|
178
|
+
assert.deepEqual(merged, [{ number: 87, options: { method: 'merge', subject: 'subject 87', body: 'body 87' } }]);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test('S2: a rebase-only repository offers no commit message and no description', async () => {
|
|
182
|
+
const merged = [];
|
|
183
|
+
const view = createPrsView({
|
|
184
|
+
pages: [{
|
|
185
|
+
id: 'mine',
|
|
186
|
+
rows: [{
|
|
187
|
+
...row(87),
|
|
188
|
+
permissions: { canMerge: true },
|
|
189
|
+
mergeMethods: { rebase: true },
|
|
190
|
+
mergeDefaults: { rebase: {} },
|
|
191
|
+
}],
|
|
192
|
+
}],
|
|
193
|
+
actions: { merge: (number, options) => { merged.push({ number, options }); return { ok: true }; } },
|
|
194
|
+
rowsAvailable: () => 30,
|
|
195
|
+
});
|
|
196
|
+
send(view, 'm');
|
|
197
|
+
const painted = frame(view);
|
|
198
|
+
assert.match(painted, /Type: rebase/);
|
|
199
|
+
assert.match(painted, /Confirm merge #87/);
|
|
200
|
+
assert.doesNotMatch(painted, /Commit message|Description/, 'neither field is offered');
|
|
201
|
+
send(view, 'down', 'enter');
|
|
202
|
+
await tick();
|
|
203
|
+
assert.deepEqual(merged, [{ number: 87, options: { method: 'rebase' } }], 'the confirm row is reachable without them');
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// ---------------------------------------------------------------------------
|
|
207
|
+
// D13 (S2): the role read and the write it authorizes share ONE resolved
|
|
208
|
+
// repository scope, so a remote that moves between them cannot authorize one
|
|
209
|
+
// repository and write under another.
|
|
210
|
+
// ---------------------------------------------------------------------------
|
|
211
|
+
test('S2: close reads the role and writes in one resolved scope', async () => {
|
|
212
|
+
const resolved = [];
|
|
213
|
+
const children = [];
|
|
214
|
+
const checkoutRepoScope = async () => {
|
|
215
|
+
resolved.push(`github.com/octo/repo-${resolved.length + 1}`);
|
|
216
|
+
return { ok: true, identity: resolved.at(-1), nameWithOwner: `octo/repo-${resolved.length}` };
|
|
217
|
+
};
|
|
218
|
+
// The injected module's own child seam, resolving a checkout exactly like
|
|
219
|
+
// lib/gh-issues.js: an explicit pin wins, a checkout is resolved again.
|
|
220
|
+
const runGh = async (argv, opts = {}) => {
|
|
221
|
+
let identity = null;
|
|
222
|
+
if (opts.scope !== undefined) {
|
|
223
|
+
identity = opts.scope === null || opts.scope === '' ? null : String(opts.scope);
|
|
224
|
+
} else if (typeof opts.cwd === 'string' && opts.cwd !== '') {
|
|
225
|
+
const scope = await checkoutRepoScope({ ...opts, fresh: opts.freshScope === true });
|
|
226
|
+
if (scope.ok !== true) return { status: null, stdout: '', stderr: '', error: scope.error };
|
|
227
|
+
identity = scope.identity;
|
|
228
|
+
}
|
|
229
|
+
children.push({ argv: argv.slice(), identity });
|
|
230
|
+
if (argv[0] === 'repo') return ok({ nameWithOwner: 'octo/base' });
|
|
231
|
+
if (argv[0] === 'api') {
|
|
232
|
+
return ok({ data: { repository: { viewerPermission: 'WRITE', pullRequest: { state: 'OPEN' } } } });
|
|
233
|
+
}
|
|
234
|
+
return ok('');
|
|
235
|
+
};
|
|
236
|
+
const client = createPrsClient({ issues: { runGh, checkoutRepoScope } });
|
|
237
|
+
|
|
238
|
+
const res = await client.closePullRequest(42, { cwd: '/checkout' });
|
|
239
|
+
|
|
240
|
+
assert.equal(res.ok, true, res.error);
|
|
241
|
+
assert.equal(resolved.length, 1, 'the checkout is resolved once for the whole write');
|
|
242
|
+
const run = children.filter((child) => child.argv[0] !== 'repo');
|
|
243
|
+
assert.equal(run.length, 2, 'one role read and one close');
|
|
244
|
+
assert.equal(run[0].identity, 'github.com/octo/repo-1', 'the role read proved repo-1');
|
|
245
|
+
assert.equal(run[1].identity, run[0].identity,
|
|
246
|
+
`the close ran in the scope the role read proved, not ${run[1].identity}`);
|
|
247
|
+
assert.deepEqual(run[1].argv, ['pr', 'close', '42']);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// ---------------------------------------------------------------------------
|
|
251
|
+
// D14 (S2): creating a repository label is a capability of its own, carried by
|
|
252
|
+
// the metadata and re-checked by the editor at save time.
|
|
253
|
+
// ---------------------------------------------------------------------------
|
|
254
|
+
test('S2: the capability map confirms label creation at WRITE and up', () => {
|
|
255
|
+
assert.equal(createPrsClient({}).pullRequestPermissions('WRITE').canCreateLabels, true);
|
|
256
|
+
for (const role of ['MAINTAIN', 'ADMIN']) {
|
|
257
|
+
assert.equal(createPrsClient({}).pullRequestPermissions(role).canCreateLabels, true, `${role} may create a label`);
|
|
258
|
+
}
|
|
259
|
+
for (const role of ['TRIAGE', 'READ', null, undefined, true, {}]) {
|
|
260
|
+
assert.equal(createPrsClient({}).pullRequestPermissions(role).canCreateLabels, false,
|
|
261
|
+
`${JSON.stringify(role)} confirms no label creation`);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
test('S2: the label editor re-checks label creation at save time', async () => {
|
|
266
|
+
const writes = [];
|
|
267
|
+
let calls = 0;
|
|
268
|
+
let finish;
|
|
269
|
+
let canCreateLabels = true;
|
|
270
|
+
const rows = [row(87)];
|
|
271
|
+
const view = createPrsView({
|
|
272
|
+
loadPullRequestPage: () => (++calls === 1
|
|
273
|
+
? { ok: true, rows }
|
|
274
|
+
: new Promise((resolve) => { finish = resolve; })),
|
|
275
|
+
loadPullRequest: (number) => ({
|
|
276
|
+
ok: true,
|
|
277
|
+
pullRequest: { ...row(number), permissions: { canEditLabels: true, canCreateLabels } },
|
|
278
|
+
}),
|
|
279
|
+
labels: [{ name: 'bug' }],
|
|
280
|
+
actions: {
|
|
281
|
+
editLabels: (...args) => { writes.push(args); return { ok: true }; },
|
|
282
|
+
createLabel: (label) => ({ ok: true, label }),
|
|
283
|
+
},
|
|
284
|
+
rowsAvailable: () => 30,
|
|
285
|
+
});
|
|
286
|
+
await tick();
|
|
287
|
+
send(view, 'r', 'l');
|
|
288
|
+
await tick();
|
|
289
|
+
send(view, 'n', 'n', 'e', 'w', 'enter');
|
|
290
|
+
canCreateLabels = false;
|
|
291
|
+
finish({ ok: true, rows });
|
|
292
|
+
await tick();
|
|
293
|
+
send(view, 'ctrl+s');
|
|
294
|
+
await tick();
|
|
295
|
+
assert.deepEqual(writes, [], 'a lapsed creation capability writes nothing');
|
|
296
|
+
assert.match(frame(view), /new labels cannot be created: this repository permission is missing/);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// ---------------------------------------------------------------------------
|
|
300
|
+
// The decision that opens the T9 verdict (S2): comments stay available in every
|
|
301
|
+
// state; the editor, close and merge keys belong to an open pull request.
|
|
302
|
+
// ---------------------------------------------------------------------------
|
|
303
|
+
test('S2: a merged pull request keeps comment and hides the editor, close and merge keys', async () => {
|
|
304
|
+
const posted = [];
|
|
305
|
+
const view = createPrsView({
|
|
306
|
+
loadPullRequestPage: () => ({ ok: true, rows: [row(88)] }),
|
|
307
|
+
loadPullRequest: (number) => ({
|
|
308
|
+
ok: true,
|
|
309
|
+
pullRequest: {
|
|
310
|
+
...row(number, { state: 'MERGED' }),
|
|
311
|
+
permissions: {
|
|
312
|
+
canEditLabels: true, canCreateLabels: true, canEditAssignees: true,
|
|
313
|
+
canClosePullRequests: true, canMerge: true, canEditLinkedIssues: true,
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
}),
|
|
317
|
+
actions: { postComment: (number, body) => { posted.push({ number, body }); return { ok: true }; } },
|
|
318
|
+
rowsAvailable: () => 30,
|
|
319
|
+
});
|
|
320
|
+
await tick();
|
|
321
|
+
assert.match(frame(view), /c comment/, 'a merged pull request can still be commented on');
|
|
322
|
+
send(view, 'c', 'n', 'i', 'c', 'e');
|
|
323
|
+
send(view, 'ctrl+s');
|
|
324
|
+
await tick();
|
|
325
|
+
assert.deepEqual(posted, [{ number: 88, body: 'nice' }], 'the comment posts, ungated by the state');
|
|
326
|
+
});
|