pi-odd-worktree 3.11.1 → 3.12.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 +6 -4
- package/extensions/odd-worktree-tools.ts +1205 -40
- package/lib/odd-group.js +655 -0
- package/lib/odd-listing.js +349 -0
- package/lib/odd-rail-model.js +150 -0
- package/lib/odd-rail-section.js +453 -0
- package/lib/odd-session.js +231 -0
- package/lib/odd-view.js +500 -46
- package/lib/user-prompt-card.js +68 -10
- package/package.json +1 -1
- package/tools/commands.test.js +58 -0
- package/tools/issues-command.test.js +13 -2
- package/tools/issues-open-guard.test.js +7 -1
- package/tools/issues-section.test.js +11 -2
- package/tools/odd-group.test.js +950 -0
- package/tools/odd-listing.test.js +504 -0
- package/tools/odd-panel-wiring.test.js +1868 -0
- package/tools/odd-rail-model.test.js +184 -0
- package/tools/odd-rail-section.test.js +524 -0
- package/tools/odd-rail-wiring.test.js +572 -0
- package/tools/odd-session-wiring.test.js +419 -0
- package/tools/odd-session.test.js +282 -0
- package/tools/odd-view.test.js +407 -1
- package/tools/package-surface.test.js +22 -6
- package/tools/prs-command.test.js +1 -1
- package/tools/user-prompt-card.test.js +173 -6
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// T3 (odd-rail-section S1, S2, S5, S6): lib/odd-rail-section.js, the pure module.
|
|
3
|
+
//
|
|
4
|
+
// Contract under test, through the public surface only:
|
|
5
|
+
// - the section is the `ODD` card, labelled `odd`, visible by default, and it
|
|
6
|
+
// carries `placement: { after: "agents" }` so the host puts it right after the
|
|
7
|
+
// native AGENTS rail part (S1); it carries no `meta`, because nothing asked for
|
|
8
|
+
// a header count;
|
|
9
|
+
// - the rows are the ODDs `load()` resolved, in THAT order (the walk and the
|
|
10
|
+
// dedupe belong to the caller), at most FOUR of them (S2); more than four adds
|
|
11
|
+
// ONE fifth row reading `N more`, dim and inert, while exactly four paint four
|
|
12
|
+
// rows and no elision row at all;
|
|
13
|
+
// - a row's `meta` is the checkout name VERBATIM — `main` or the worktree name,
|
|
14
|
+
// never a path (S5);
|
|
15
|
+
// - a row click defers `open({ name, docPath, checkoutRoot })` off the paint
|
|
16
|
+
// path, once per click, with the target the row was built from, and never
|
|
17
|
+
// after `dispose()`; a throwing opener never escapes into the host (S6);
|
|
18
|
+
// - `lines()` is a synchronous frame over an in-process snapshot: it never calls
|
|
19
|
+
// `load()` on the paint path, a second frame inside the TTL reloads nothing,
|
|
20
|
+
// and two stale frames queue ONE read;
|
|
21
|
+
// - a failed load keeps every row already on screen, marks the list `stale` and
|
|
22
|
+
// starts no immediate retry; a failed FIRST load is still one error row, and no
|
|
23
|
+
// seam — a throwing clock included — ever throws at the host;
|
|
24
|
+
// - `invalidate()` drops the rows and supersedes a result already on its way
|
|
25
|
+
// (latest-wins), including a frame callback that outlived it;
|
|
26
|
+
// - the floor timer a `refreshFloorMs` asks for is unref'd, fires one read per
|
|
27
|
+
// floor, and stops by itself once the section leaves the drawn window.
|
|
28
|
+
//
|
|
29
|
+
// Every seam is injected: the fake clock the tests own, a manual scheduler and
|
|
30
|
+
// manual timers, so nothing here waits on a real timer and nothing walks a real
|
|
31
|
+
// directory.
|
|
32
|
+
|
|
33
|
+
const { test } = require('node:test');
|
|
34
|
+
const assert = require('node:assert/strict');
|
|
35
|
+
|
|
36
|
+
const { createOddRailSection } = require('../lib/odd-rail-section.js');
|
|
37
|
+
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
// The injected-seam harness.
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
// A manual scheduler: the section's refresh and its click callbacks are queued
|
|
43
|
+
// and only run when a test says so, so "deferred" is observable.
|
|
44
|
+
function manualSchedule() {
|
|
45
|
+
const queue = [];
|
|
46
|
+
return {
|
|
47
|
+
schedule: (fn) => { queue.push(fn); },
|
|
48
|
+
pending: () => queue.length,
|
|
49
|
+
async flush() {
|
|
50
|
+
while (queue.length > 0) {
|
|
51
|
+
const fn = queue.shift();
|
|
52
|
+
await fn();
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Manual timers: the floor timer is observable as an armed handle, and the test
|
|
59
|
+
// decides when it fires. `unref` is recorded because an unref'd timer is what
|
|
60
|
+
// keeps the floor from being a reason for the process to stay alive.
|
|
61
|
+
function manualTimers() {
|
|
62
|
+
const armed = [];
|
|
63
|
+
return {
|
|
64
|
+
setTimer: (fn, ms) => {
|
|
65
|
+
const handle = {
|
|
66
|
+
fn,
|
|
67
|
+
ms,
|
|
68
|
+
cleared: false,
|
|
69
|
+
fired: false,
|
|
70
|
+
unrefs: 0,
|
|
71
|
+
unref() {
|
|
72
|
+
handle.unrefs += 1;
|
|
73
|
+
return handle;
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
armed.push(handle);
|
|
77
|
+
return handle;
|
|
78
|
+
},
|
|
79
|
+
clearTimer: (handle) => {
|
|
80
|
+
if (handle) handle.cleared = true;
|
|
81
|
+
},
|
|
82
|
+
active: () => armed.filter((handle) => !handle.cleared && !handle.fired),
|
|
83
|
+
fire() {
|
|
84
|
+
const next = this.active()[0];
|
|
85
|
+
if (!next) return false;
|
|
86
|
+
next.fired = true;
|
|
87
|
+
next.fn();
|
|
88
|
+
return true;
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function tick() {
|
|
94
|
+
return new Promise((resolve) => setImmediate(resolve));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// One entry of what `load()` resolves: the ODD's name and the checkout it lives
|
|
98
|
+
// in. `docPath` and `checkoutRoot` are the values the click must hand over
|
|
99
|
+
// untouched, and `mtimeMs` stays on the entry the section never reads.
|
|
100
|
+
function oddItem(name, checkoutName, mtimeMs = 1000) {
|
|
101
|
+
const root = checkoutName === 'main' ? '/repo/project' : `/repo/${checkoutName}`;
|
|
102
|
+
return {
|
|
103
|
+
name,
|
|
104
|
+
docPath: `${root}/odd/tasks/${name}.md`,
|
|
105
|
+
checkoutRoot: root,
|
|
106
|
+
checkoutName,
|
|
107
|
+
mtimeMs,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Four ODDs, handed over in an order that is deliberately NOT the `mtimeMs`
|
|
112
|
+
// order: the section draws the order it resolved, never a re-sort.
|
|
113
|
+
const FOUR = [
|
|
114
|
+
oddItem('alpha', 'main', 500),
|
|
115
|
+
oddItem('beta', 'wt-two', 900),
|
|
116
|
+
oddItem('gamma', 'main', 100),
|
|
117
|
+
oddItem('delta', 'wt-three', 700),
|
|
118
|
+
];
|
|
119
|
+
|
|
120
|
+
function targetOf(item) {
|
|
121
|
+
return { name: item.name, docPath: item.docPath, checkoutRoot: item.checkoutRoot };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ---------------------------------------------------------------------------
|
|
125
|
+
// S1: the card.
|
|
126
|
+
// ---------------------------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
test('S1: the section is the ODD card, placed right after the native AGENTS part', () => {
|
|
129
|
+
const handle = createOddRailSection({
|
|
130
|
+
load: async () => [],
|
|
131
|
+
open: () => {},
|
|
132
|
+
schedule: () => {},
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
assert.equal(handle.section.id, 'odd-worktree.odd-rail', 'the section keeps its fixed id');
|
|
136
|
+
assert.equal(handle.section.title, 'ODD', 'the heading reads ODD');
|
|
137
|
+
assert.equal(handle.section.rowLabel, 'odd', 'the collapsed row label reads odd');
|
|
138
|
+
assert.equal(handle.section.defaultVisible, true, 'the card is visible when it is registered');
|
|
139
|
+
assert.deepEqual(handle.section.placement, { after: 'agents' },
|
|
140
|
+
'the placement is exactly what puts the card after the AGENTS part');
|
|
141
|
+
assert.deepEqual(Object.keys(handle.section.placement), ['after']);
|
|
142
|
+
assert.equal(typeof handle.section.lines, 'function', 'the host draws the card through lines()');
|
|
143
|
+
assert.equal('meta' in handle.section, false, 'nothing asked for a header count, so there is no meta');
|
|
144
|
+
assert.deepEqual(Object.keys(handle.section).sort(),
|
|
145
|
+
['defaultVisible', 'id', 'lines', 'placement', 'rowLabel', 'title'],
|
|
146
|
+
'the section is exactly the shape the bridge registers');
|
|
147
|
+
assert.equal(typeof handle.invalidate, 'function', 'the session boundary has its seam');
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// ---------------------------------------------------------------------------
|
|
151
|
+
// S2/S5: the rows.
|
|
152
|
+
// ---------------------------------------------------------------------------
|
|
153
|
+
|
|
154
|
+
test('S2/S5: exactly four ODDs paint four rows in the resolved order, and no elision row', async () => {
|
|
155
|
+
const handle = createOddRailSection({
|
|
156
|
+
load: async () => FOUR,
|
|
157
|
+
open: () => {},
|
|
158
|
+
schedule: () => {},
|
|
159
|
+
});
|
|
160
|
+
await handle.refresh();
|
|
161
|
+
|
|
162
|
+
const rows = handle.section.lines({});
|
|
163
|
+
assert.equal(rows.length, 4, 'four ODDs are four rows — no fifth line at all');
|
|
164
|
+
assert.deepEqual(rows.map((row) => row.label), ['alpha', 'beta', 'gamma', 'delta'],
|
|
165
|
+
'the resolved order is drawn: the section never re-sorts');
|
|
166
|
+
assert.deepEqual(rows.map((row) => row.meta), ['main', 'wt-two', 'main', 'wt-three'],
|
|
167
|
+
'each row carries its checkout name at the far right');
|
|
168
|
+
assert.deepEqual(rows.map((row) => row.tone), ['text', 'text', 'text', 'text']);
|
|
169
|
+
for (const row of rows) {
|
|
170
|
+
assert.equal(typeof row.onClick, 'function', 'every ODD row is clickable (S6)');
|
|
171
|
+
assert.deepEqual(Object.keys(row).sort(), ['label', 'meta', 'onClick', 'tone'],
|
|
172
|
+
'a row is its label, its checkout name, its tone and its click');
|
|
173
|
+
}
|
|
174
|
+
assert.equal(rows.some((row) => row.onClick === null), false, 'no inert row is invented at four');
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test('S2: more than four ODDs paint four rows plus ONE dim, inert `N more` row', async () => {
|
|
178
|
+
const five = [
|
|
179
|
+
oddItem('one', 'main'),
|
|
180
|
+
oddItem('two', 'wt-a'),
|
|
181
|
+
oddItem('three', 'wt-b'),
|
|
182
|
+
oddItem('four', 'main'),
|
|
183
|
+
oddItem('five', 'wt-c'),
|
|
184
|
+
];
|
|
185
|
+
const handle = createOddRailSection({
|
|
186
|
+
load: async () => five,
|
|
187
|
+
open: () => {},
|
|
188
|
+
schedule: () => {},
|
|
189
|
+
});
|
|
190
|
+
await handle.refresh();
|
|
191
|
+
|
|
192
|
+
const rows = handle.section.lines({});
|
|
193
|
+
assert.equal(rows.length, 5, 'four ODD rows and the one elision row');
|
|
194
|
+
assert.deepEqual(rows.slice(0, 4).map((row) => row.label), ['one', 'two', 'three', 'four']);
|
|
195
|
+
assert.equal(rows[4].label, '1 more', 'the elision counts what the four rows left out');
|
|
196
|
+
assert.equal(rows[4].tone, 'dim', 'the elision row is the secondary tone');
|
|
197
|
+
assert.equal(rows[4].onClick, null, 'the elision row is inert: there is nothing to open');
|
|
198
|
+
assert.equal('meta' in rows[4], false, 'the elision row carries no checkout name');
|
|
199
|
+
|
|
200
|
+
const many = Array.from({ length: 32 }, (_, index) => oddItem(`odd-${index + 1}`, index % 2 === 0 ? 'main' : 'wt-x'));
|
|
201
|
+
const wide = createOddRailSection({
|
|
202
|
+
load: async () => many,
|
|
203
|
+
open: () => {},
|
|
204
|
+
schedule: () => {},
|
|
205
|
+
});
|
|
206
|
+
await wide.refresh();
|
|
207
|
+
const wideRows = wide.section.lines({});
|
|
208
|
+
assert.equal(wideRows.length, 5, 'however many ODDs there are, the body stays five rows');
|
|
209
|
+
assert.deepEqual(wideRows.slice(0, 4).map((row) => row.label), ['odd-1', 'odd-2', 'odd-3', 'odd-4'],
|
|
210
|
+
'the four most recent are the four the walk put first');
|
|
211
|
+
assert.equal(wideRows[4].label, '28 more', 'N is the TOTAL beyond the four, not a page size');
|
|
212
|
+
assert.equal(wideRows[4].tone, 'dim');
|
|
213
|
+
assert.equal(wideRows[4].onClick, null);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test('S5: the row meta is the checkout name verbatim — `main` or the worktree name, never a path', async () => {
|
|
217
|
+
const items = [
|
|
218
|
+
oddItem('in-main', 'main'),
|
|
219
|
+
oddItem('in-worktree', 'feat-odd-rail-section'),
|
|
220
|
+
];
|
|
221
|
+
const handle = createOddRailSection({
|
|
222
|
+
load: async () => items,
|
|
223
|
+
open: () => {},
|
|
224
|
+
schedule: () => {},
|
|
225
|
+
});
|
|
226
|
+
await handle.refresh();
|
|
227
|
+
|
|
228
|
+
const rows = handle.section.lines({});
|
|
229
|
+
assert.equal(rows[0].meta, 'main', 'the main checkout keeps its own name');
|
|
230
|
+
assert.equal(rows[1].meta, 'feat-odd-rail-section', 'a worktree is named, never located');
|
|
231
|
+
assert.equal(rows[0].meta, items[0].checkoutName, 'the name goes through verbatim');
|
|
232
|
+
assert.equal(rows[1].meta, items[1].checkoutName);
|
|
233
|
+
for (const row of rows) {
|
|
234
|
+
assert.equal(row.meta.includes('/'), false, `no row meta is a path (saw ${row.meta})`);
|
|
235
|
+
assert.notEqual(row.meta, row.label, 'the checkout name is not the ODD name');
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
// ---------------------------------------------------------------------------
|
|
240
|
+
// S6: the click.
|
|
241
|
+
// ---------------------------------------------------------------------------
|
|
242
|
+
|
|
243
|
+
test('S6: a row click defers open with that row\'s target, once, and never after dispose', async () => {
|
|
244
|
+
const opened = [];
|
|
245
|
+
const sched = manualSchedule();
|
|
246
|
+
const handle = createOddRailSection({
|
|
247
|
+
load: async () => FOUR,
|
|
248
|
+
open: (target) => { opened.push(target); },
|
|
249
|
+
schedule: sched.schedule,
|
|
250
|
+
});
|
|
251
|
+
await handle.refresh();
|
|
252
|
+
const rows = handle.section.lines({});
|
|
253
|
+
|
|
254
|
+
rows[0].onClick();
|
|
255
|
+
assert.deepEqual(opened, [], 'the opener never runs on the paint path');
|
|
256
|
+
await sched.flush();
|
|
257
|
+
assert.deepEqual(opened, [targetOf(FOUR[0])],
|
|
258
|
+
'one click opens that row\'s ODD with its own name, document and checkout root');
|
|
259
|
+
|
|
260
|
+
rows[2].onClick();
|
|
261
|
+
await sched.flush();
|
|
262
|
+
assert.deepEqual(opened, [targetOf(FOUR[0]), targetOf(FOUR[2])],
|
|
263
|
+
'every row carries ITS target, not whatever the snapshot holds when the human clicks');
|
|
264
|
+
assert.deepEqual(Object.keys(opened[0]).sort(), ['checkoutRoot', 'docPath', 'name'],
|
|
265
|
+
'the target is the three fields the opener names');
|
|
266
|
+
|
|
267
|
+
handle.dispose();
|
|
268
|
+
rows[1].onClick();
|
|
269
|
+
assert.equal(sched.pending(), 0, 'a click the host retained schedules nothing after dispose');
|
|
270
|
+
await sched.flush();
|
|
271
|
+
assert.equal(opened.length, 2, 'open is never called after dispose');
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test('S6: a throwing or rejecting opener never escapes the click and never costs the rows', async () => {
|
|
275
|
+
const sched = manualSchedule();
|
|
276
|
+
const handle = createOddRailSection({
|
|
277
|
+
load: async () => FOUR,
|
|
278
|
+
open: () => { throw new Error('no panel here'); },
|
|
279
|
+
schedule: sched.schedule,
|
|
280
|
+
});
|
|
281
|
+
await handle.refresh();
|
|
282
|
+
const ready = handle.section.lines({});
|
|
283
|
+
|
|
284
|
+
let escaped = null;
|
|
285
|
+
try {
|
|
286
|
+
ready[0].onClick();
|
|
287
|
+
await sched.flush();
|
|
288
|
+
} catch (err) {
|
|
289
|
+
escaped = err;
|
|
290
|
+
}
|
|
291
|
+
assert.equal(escaped, null, 'a failure on the click path never reaches the host');
|
|
292
|
+
assert.deepEqual(handle.section.lines({}), ready, 'a failed dispatch never turns the list into an error row');
|
|
293
|
+
|
|
294
|
+
const asyncSched = manualSchedule();
|
|
295
|
+
const asyncHandle = createOddRailSection({
|
|
296
|
+
load: async () => FOUR,
|
|
297
|
+
open: () => Promise.reject(new Error('async opener failed')),
|
|
298
|
+
schedule: asyncSched.schedule,
|
|
299
|
+
});
|
|
300
|
+
await asyncHandle.refresh();
|
|
301
|
+
const asyncRows = asyncHandle.section.lines({});
|
|
302
|
+
asyncRows[1].onClick();
|
|
303
|
+
await asyncSched.flush();
|
|
304
|
+
assert.deepEqual(asyncHandle.section.lines({}), asyncRows,
|
|
305
|
+
'a rejected opener leaves the rows exactly as they were');
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
// ---------------------------------------------------------------------------
|
|
309
|
+
// The frame: one snapshot, one scheduled read, a TTL.
|
|
310
|
+
// ---------------------------------------------------------------------------
|
|
311
|
+
|
|
312
|
+
test('the frame never awaits: lines() schedules the read, and a frame inside the TTL reloads nothing', async () => {
|
|
313
|
+
let clock = 1000;
|
|
314
|
+
let loads = 0;
|
|
315
|
+
let repaints = 0;
|
|
316
|
+
const sched = manualSchedule();
|
|
317
|
+
const handle = createOddRailSection({
|
|
318
|
+
load: async () => {
|
|
319
|
+
loads += 1;
|
|
320
|
+
return FOUR;
|
|
321
|
+
},
|
|
322
|
+
open: () => {},
|
|
323
|
+
schedule: sched.schedule,
|
|
324
|
+
now: () => clock,
|
|
325
|
+
ttlMs: 100,
|
|
326
|
+
requestRepaint: () => { repaints += 1; },
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
assert.deepEqual(handle.section.lines({}), [{ label: 'loading…', tone: 'dim' }],
|
|
330
|
+
'an absent snapshot draws the loading row');
|
|
331
|
+
assert.equal(loads, 0, 'lines() never calls load() synchronously');
|
|
332
|
+
assert.equal(sched.pending(), 1, 'the frame only SCHEDULES the read');
|
|
333
|
+
await sched.flush();
|
|
334
|
+
assert.equal(loads, 1);
|
|
335
|
+
assert.equal(repaints, 1, 'a landed read asks the host to repaint once');
|
|
336
|
+
assert.equal(handle.section.lines({}).length, 4);
|
|
337
|
+
|
|
338
|
+
assert.equal(sched.pending(), 0, 'a frame inside the TTL schedules nothing');
|
|
339
|
+
clock += 99;
|
|
340
|
+
handle.section.lines({});
|
|
341
|
+
assert.equal(sched.pending(), 0, 'still inside the TTL: no reload');
|
|
342
|
+
assert.equal(loads, 1);
|
|
343
|
+
|
|
344
|
+
clock += 1;
|
|
345
|
+
handle.section.lines({});
|
|
346
|
+
assert.equal(sched.pending(), 1, 'at the TTL one frame schedules exactly one read');
|
|
347
|
+
await sched.flush();
|
|
348
|
+
assert.equal(loads, 2);
|
|
349
|
+
|
|
350
|
+
clock += 500;
|
|
351
|
+
handle.section.lines({});
|
|
352
|
+
handle.section.lines({});
|
|
353
|
+
assert.equal(sched.pending(), 1, 'two stale frames still schedule ONE read');
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
test('a failed load keeps the previous rows, marks them stale and starts no immediate retry', async () => {
|
|
357
|
+
let clock = 1000;
|
|
358
|
+
let failing = false;
|
|
359
|
+
const sched = manualSchedule();
|
|
360
|
+
const handle = createOddRailSection({
|
|
361
|
+
load: async () => {
|
|
362
|
+
if (failing) throw new Error('scan failed');
|
|
363
|
+
return FOUR;
|
|
364
|
+
},
|
|
365
|
+
open: () => {},
|
|
366
|
+
schedule: sched.schedule,
|
|
367
|
+
now: () => clock,
|
|
368
|
+
ttlMs: 100,
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
await handle.refresh();
|
|
372
|
+
const ready = handle.section.lines({});
|
|
373
|
+
assert.equal(ready.length, 4);
|
|
374
|
+
|
|
375
|
+
failing = true;
|
|
376
|
+
clock += 200;
|
|
377
|
+
await handle.refresh();
|
|
378
|
+
assert.deepEqual(handle.section.lines({}), ready,
|
|
379
|
+
'the rows stay exactly as they were: same rows, same order, same metadata');
|
|
380
|
+
assert.equal(handle.section.lines({}).length, 4, 'a failure never blanks the card');
|
|
381
|
+
|
|
382
|
+
handle.section.lines({});
|
|
383
|
+
assert.equal(sched.pending(), 0,
|
|
384
|
+
'the failed attempt starts the TTL, so the frame after it retries nothing');
|
|
385
|
+
clock += 100;
|
|
386
|
+
handle.section.lines({});
|
|
387
|
+
assert.equal(sched.pending(), 1, 'once the TTL from the failed attempt passes, the frame retries');
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
test('a failure with nothing on screen is one error row, and no seam throws at the host', async () => {
|
|
391
|
+
const handle = createOddRailSection({
|
|
392
|
+
load: async () => { throw new Error('scan failed'); },
|
|
393
|
+
open: () => {},
|
|
394
|
+
schedule: () => {},
|
|
395
|
+
});
|
|
396
|
+
await handle.refresh();
|
|
397
|
+
assert.deepEqual(handle.section.lines({}), [{ label: 'scan failed', tone: 'error' }],
|
|
398
|
+
'a failed first read is ONE error row, not a throw');
|
|
399
|
+
|
|
400
|
+
// A clock that throws costs the frame, never the host.
|
|
401
|
+
const weird = createOddRailSection({
|
|
402
|
+
load: async () => FOUR,
|
|
403
|
+
open: () => {},
|
|
404
|
+
schedule: () => {},
|
|
405
|
+
now: () => { throw new Error('clock'); },
|
|
406
|
+
});
|
|
407
|
+
let rows = null;
|
|
408
|
+
assert.doesNotThrow(() => { rows = weird.section.lines({}); }, 'a frame never throws');
|
|
409
|
+
assert.ok(Array.isArray(rows), 'and it still answers rows');
|
|
410
|
+
|
|
411
|
+
// A `load` that is not a function is the same one-line failure, never a crash.
|
|
412
|
+
const missing = createOddRailSection({ open: () => {}, schedule: () => {} });
|
|
413
|
+
await missing.refresh();
|
|
414
|
+
assert.equal(missing.section.lines({}).length, 1);
|
|
415
|
+
assert.equal(missing.section.lines({})[0].tone, 'error');
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
// ---------------------------------------------------------------------------
|
|
419
|
+
// The session boundary: latest-wins.
|
|
420
|
+
// ---------------------------------------------------------------------------
|
|
421
|
+
|
|
422
|
+
test('invalidate drops the rows and supersedes a result already on its way', async () => {
|
|
423
|
+
const deferred = [];
|
|
424
|
+
const handle = createOddRailSection({
|
|
425
|
+
load: () => new Promise((resolve) => { deferred.push(resolve); }),
|
|
426
|
+
open: () => {},
|
|
427
|
+
schedule: () => {},
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
const inFlight = handle.refresh();
|
|
431
|
+
assert.deepEqual(handle.section.lines({}), [{ label: 'loading…', tone: 'dim' }]);
|
|
432
|
+
handle.invalidate();
|
|
433
|
+
deferred[0](FOUR);
|
|
434
|
+
await inFlight;
|
|
435
|
+
assert.deepEqual(handle.section.lines({}), [{ label: 'loading…', tone: 'dim' }],
|
|
436
|
+
'the superseded answer never paints the previous session\'s ODDs');
|
|
437
|
+
|
|
438
|
+
// A frame callback already queued when the boundary fired starts no read.
|
|
439
|
+
let loads = 0;
|
|
440
|
+
const sched = manualSchedule();
|
|
441
|
+
const queued = createOddRailSection({
|
|
442
|
+
load: async () => {
|
|
443
|
+
loads += 1;
|
|
444
|
+
return FOUR;
|
|
445
|
+
},
|
|
446
|
+
open: () => {},
|
|
447
|
+
schedule: sched.schedule,
|
|
448
|
+
});
|
|
449
|
+
queued.section.lines({});
|
|
450
|
+
assert.equal(sched.pending(), 1);
|
|
451
|
+
queued.invalidate();
|
|
452
|
+
await sched.flush();
|
|
453
|
+
assert.equal(loads, 0, 'a callback that outlived the invalidate does nothing');
|
|
454
|
+
|
|
455
|
+
// After dispose the boundary is a no-op and never throws.
|
|
456
|
+
queued.dispose();
|
|
457
|
+
assert.doesNotThrow(() => queued.invalidate(), 'invalidate never throws');
|
|
458
|
+
await sched.flush();
|
|
459
|
+
assert.equal(loads, 0, 'a disposed section reads nothing');
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
// ---------------------------------------------------------------------------
|
|
463
|
+
// The floor timer.
|
|
464
|
+
// ---------------------------------------------------------------------------
|
|
465
|
+
|
|
466
|
+
test('the refresh timer is unref\'d, reads once per floor, and stops itself outside the drawn window', async () => {
|
|
467
|
+
let clock = 1000;
|
|
468
|
+
let loads = 0;
|
|
469
|
+
const sched = manualSchedule();
|
|
470
|
+
const timers = manualTimers();
|
|
471
|
+
const handle = createOddRailSection({
|
|
472
|
+
load: async () => {
|
|
473
|
+
loads += 1;
|
|
474
|
+
return FOUR;
|
|
475
|
+
},
|
|
476
|
+
open: () => {},
|
|
477
|
+
schedule: sched.schedule,
|
|
478
|
+
setTimer: timers.setTimer,
|
|
479
|
+
clearTimer: timers.clearTimer,
|
|
480
|
+
now: () => clock,
|
|
481
|
+
refreshFloorMs: 20000,
|
|
482
|
+
drawnWindowMs: 120000,
|
|
483
|
+
ttlMs: 1,
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
handle.section.lines({});
|
|
487
|
+
await sched.flush();
|
|
488
|
+
assert.equal(loads, 1);
|
|
489
|
+
const armed = timers.active();
|
|
490
|
+
assert.equal(armed.length, 1, 'a drawn section arms ONE floor timer');
|
|
491
|
+
assert.equal(armed[0].ms, 20000, 'at the floor the caller named');
|
|
492
|
+
assert.equal(armed[0].unrefs, 1, 'the timer is unref\'d: never a reason for the process to stay alive');
|
|
493
|
+
|
|
494
|
+
// The floor fires while the section is still drawn: one read, and the next
|
|
495
|
+
// floor is armed by that fire itself.
|
|
496
|
+
clock += 20000;
|
|
497
|
+
assert.equal(timers.fire(), true);
|
|
498
|
+
await tick();
|
|
499
|
+
assert.equal(loads, 2, 'the floor timer starts exactly one read');
|
|
500
|
+
assert.equal(timers.active().length, 1, 'and arms the next floor');
|
|
501
|
+
|
|
502
|
+
// The host stops drawing: once the section is outside its drawn window, the
|
|
503
|
+
// timer that fires starts nothing and arms nothing.
|
|
504
|
+
clock += 200000;
|
|
505
|
+
assert.equal(timers.fire(), true);
|
|
506
|
+
await tick();
|
|
507
|
+
assert.equal(loads, 2, 'a section nobody draws reads nothing');
|
|
508
|
+
assert.equal(timers.active().length, 0, 'and the timer stops by itself');
|
|
509
|
+
|
|
510
|
+
// A section disposed while drawn keeps no timer either.
|
|
511
|
+
const disposeTimers = manualTimers();
|
|
512
|
+
const disposed = createOddRailSection({
|
|
513
|
+
load: async () => FOUR,
|
|
514
|
+
open: () => {},
|
|
515
|
+
schedule: () => {},
|
|
516
|
+
setTimer: disposeTimers.setTimer,
|
|
517
|
+
clearTimer: disposeTimers.clearTimer,
|
|
518
|
+
refreshFloorMs: 20000,
|
|
519
|
+
});
|
|
520
|
+
disposed.section.lines({});
|
|
521
|
+
assert.equal(disposeTimers.active().length, 1, 'the drawn section owns a timer');
|
|
522
|
+
disposed.dispose();
|
|
523
|
+
assert.equal(disposeTimers.active().length, 0, 'dispose clears the floor timer');
|
|
524
|
+
});
|