pi-odd-worktree 3.11.2 → 3.12.1

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.
@@ -0,0 +1,504 @@
1
+ 'use strict';
2
+ // T14 (odd-rail-section L11's redesign): lib/odd-listing.js, the ONE listing value
3
+ // the ODD panel's list and the bare `/odd` menu both consume, so their labels and
4
+ // their ORDER can never diverge again and no two rows can collide.
5
+ //
6
+ // What is asserted here, through the module's TWO public exports:
7
+ // - Identity is the RESOLVED ABSOLUTE PATH (L11): the same document reached from
8
+ // two groups, or spelled with a `./` or a trailing separator, is ONE entry and
9
+ // ONE menu row, and the first occurrence is the one that is kept.
10
+ // - Labels (L11 defect: two worktrees sharing a basename both read
11
+ // `collision (same checkout)`): a unique name stays EXACTLY the name; a name
12
+ // that appears twice is labelled `<name> (<checkoutName>)`; two checkouts with
13
+ // the SAME `checkoutName` are distinguished by the SHORTEST distinguishing
14
+ // trailing path segments (`a/proj-x` vs `b/proj-x`, and deeper when two roots
15
+ // still end alike); a generated label that still hits a literal document name
16
+ // (or another label) takes ` #2`, ` #3`. Every label in the listing is unique.
17
+ // - Order: ONE comparator (`compareEntries`) — `mtimeMs` descending, then name in
18
+ // code units, then path in code units, exactly the rule lib/odd-docs.js's
19
+ // `orderDocs` and lib/odd-group.js's `compareDocs` already use. A caller that
20
+ // hands an entry a finite `order` keeps ITS sequence (the extension passes main's
21
+ // lib/odd-docs.js rank, whose commit recency is not mtime), with
22
+ // `compareEntries` breaking ties; a caller that hands nothing gets the
23
+ // comparator's order, exactly as before. The menu is the panel's order flattened
24
+ // group by group, main's group first, so picking the first entry opens what the
25
+ // panel's first row shows (L11 defect: `/odd`'s degraded menu order had regressed
26
+ // against 53ae2b2, whose menu read a main checkout's documents in that rank).
27
+ // - Fail closed: junk is skipped, never thrown on; a document with no readable
28
+ // mtime is the oldest (`mtimeMs` 0) and still appears; an empty group vanishes.
29
+ // - Pure: no fs, no git, no clock, and the inputs are never mutated.
30
+
31
+ const { test, describe } = require('node:test');
32
+ const assert = require('node:assert/strict');
33
+ const fs = require('node:fs');
34
+ const path = require('node:path');
35
+
36
+ const listing = require('../lib/odd-listing.js');
37
+ const { buildListing, compareEntries } = listing;
38
+
39
+ const SRC = path.join(__dirname, '..', 'lib', 'odd-listing.js');
40
+ const MAIN_ROOT = path.resolve('/odd-listing/checkout');
41
+ const TASKS = path.join(MAIN_ROOT, 'odd', 'tasks');
42
+ const rootOf = (...parts) => path.resolve('/odd-listing', ...parts);
43
+
44
+ // A grouped-model document (`odd-group.js`'s shape) and its group.
45
+ const doc = (root, name, mtimeMs) => ({ name, docPath: path.join(root, 'odd', 'tasks', `${name}.md`), mtimeMs });
46
+ const checkoutGroup = (checkoutName, checkoutRoot, docs, isMain = false) => ({
47
+ checkoutName,
48
+ checkoutRoot,
49
+ isMain,
50
+ docs,
51
+ });
52
+
53
+ // Labels in the order the listing draws them, which is what the panel shows.
54
+ const labelsOf = (result) => result.groups.flatMap((group) => group.entries.map((entry) => entry.label));
55
+ // The panel's order flattened: name and label of every menu row, in order.
56
+ const menuOf = (result) => result.menu.map((row) => [row.name, row.label]);
57
+ // The panel list's labels; the menu carries the very same ones (asserted below).
58
+ const everyLabel = (result) => labelsOf(result);
59
+
60
+ describe('identity is the resolved absolute path', () => {
61
+ test('the same path twice is emitted ONCE, and the first occurrence is kept', () => {
62
+ const result = buildListing({
63
+ groups: [
64
+ checkoutGroup('main', MAIN_ROOT, [
65
+ doc(MAIN_ROOT, 'alpha', 10),
66
+ { name: 'alpha', docPath: path.join(MAIN_ROOT, 'odd', 'tasks', './alpha.md'), mtimeMs: 99 },
67
+ ], true),
68
+ ],
69
+ });
70
+ assert.equal(result.menu.length, 1, 'one document, one menu row');
71
+ assert.deepEqual(result.groups[0].entries, [
72
+ { path: path.join(TASKS, 'alpha.md'), name: 'alpha', mtimeMs: 10, label: 'alpha' },
73
+ ], 'the FIRST occurrence supplies name, time and label');
74
+ });
75
+
76
+ test('the same path reached from two groups is ONE entry in the first group', () => {
77
+ const shared = path.join(TASKS, 'alpha.md');
78
+ const result = buildListing({
79
+ groups: [
80
+ checkoutGroup('main', MAIN_ROOT, [doc(MAIN_ROOT, 'alpha', 10)], true),
81
+ checkoutGroup('wt-a', rootOf('checkout-worktrees', 'wt-a'), [
82
+ { name: 'alpha', docPath: `${shared}/`, mtimeMs: 20 },
83
+ { name: 'beta', docPath: path.join(rootOf('checkout-worktrees', 'wt-a'), 'odd', 'tasks', 'beta.md'), mtimeMs: 5 },
84
+ ]),
85
+ ],
86
+ });
87
+ assert.deepEqual(result.menu.map((row) => row.path), [
88
+ shared,
89
+ path.join(rootOf('checkout-worktrees', 'wt-a'), 'odd', 'tasks', 'beta.md'),
90
+ ], 'the duplicate path is not offered twice, and it stays in the group that first held it');
91
+ assert.deepEqual(result.groups.map((group) => group.entries.length), [1, 1]);
92
+ assert.equal(result.menu.filter((row) => row.label === 'alpha').length, 1);
93
+ });
94
+
95
+ test('a group whose only document was already emitted vanishes (no empty group)', () => {
96
+ const result = buildListing({
97
+ groups: [
98
+ checkoutGroup('main', MAIN_ROOT, [doc(MAIN_ROOT, 'alpha', 10)], true),
99
+ checkoutGroup('wt-a', rootOf('checkout-worktrees', 'wt-a'), [
100
+ { name: 'alpha', docPath: path.join(MAIN_ROOT, 'odd', 'tasks', 'alpha.md'), mtimeMs: 10 },
101
+ ]),
102
+ ],
103
+ });
104
+ assert.deepEqual(result.groups.map((group) => group.checkoutName), ['main']);
105
+ });
106
+ });
107
+
108
+ describe('labels: derived, unique, and stable', () => {
109
+ test('a unique name stays EXACTLY the name, with no suffix and no decoration', () => {
110
+ const result = buildListing({
111
+ groups: [
112
+ checkoutGroup('main', MAIN_ROOT, [doc(MAIN_ROOT, 'alpha', 10)], true),
113
+ checkoutGroup('wt-a', rootOf('checkout-worktrees', 'wt-a'), [doc(rootOf('checkout-worktrees', 'wt-a'), 'beta', 20)]),
114
+ ],
115
+ });
116
+ assert.deepEqual(labelsOf(result), ['alpha', 'beta'], 'the baseline’s plain text survives');
117
+ assert.deepEqual(menuOf(result), [['alpha', 'alpha'], ['beta', 'beta']]);
118
+ });
119
+
120
+ test('a name that appears more than once is labelled `<name> (<checkoutName>)`', () => {
121
+ const wtRoot = rootOf('checkout-worktrees', 'wt-a');
122
+ const result = buildListing({
123
+ groups: [
124
+ checkoutGroup('main', MAIN_ROOT, [doc(MAIN_ROOT, 'collision', 10)], true),
125
+ checkoutGroup('wt-a', wtRoot, [doc(wtRoot, 'collision', 20)]),
126
+ ],
127
+ });
128
+ assert.deepEqual(labelsOf(result), ['collision (main)', 'collision (wt-a)']);
129
+ });
130
+
131
+ test('(a) two checkouts sharing a basename get DISTINCT, stable labels (fixture of L11)', () => {
132
+ const a = rootOf('a', 'proj-x');
133
+ const b = rootOf('b', 'proj-x');
134
+ const groups = [
135
+ checkoutGroup('proj-x', a, [doc(a, 'collision', 10)]),
136
+ checkoutGroup('proj-x', b, [doc(b, 'collision', 20)]),
137
+ ];
138
+ const result = buildListing({ groups });
139
+ assert.deepEqual(labelsOf(result), ['collision (a/proj-x)', 'collision (b/proj-x)'],
140
+ 'the shortest trailing segments that tell the two roots apart, never `checkoutName` twice');
141
+ assert.equal(new Set(labelsOf(result)).size, 2, 'distinct labels');
142
+ assert.deepEqual(
143
+ result.menu.map((row) => [row.path, row.label]),
144
+ [
145
+ [path.join(a, 'odd', 'tasks', 'collision.md'), 'collision (a/proj-x)'],
146
+ [path.join(b, 'odd', 'tasks', 'collision.md'), 'collision (b/proj-x)'],
147
+ ],
148
+ 'and distinct menu rows, each pointing at its own copy',
149
+ );
150
+ // Stable: the same model answers the same labels, and the answer is the model's.
151
+ assert.deepEqual(labelsOf(buildListing({ groups })), labelsOf(result));
152
+ });
153
+
154
+ test('the shortest distinguishing segments are chosen per checkout, not per pair', () => {
155
+ const a = rootOf('a', 'proj-x');
156
+ const b = rootOf('b', 'proj-x');
157
+ const sub = rootOf('a', 'sub', 'proj-x');
158
+ const result = buildListing({
159
+ groups: [
160
+ checkoutGroup('proj-x', a, [doc(a, 'collision', 10)]),
161
+ checkoutGroup('proj-x', b, [doc(b, 'collision', 20)]),
162
+ checkoutGroup('proj-x', sub, [doc(sub, 'collision', 30)]),
163
+ ],
164
+ });
165
+ assert.deepEqual(labelsOf(result), [
166
+ 'collision (a/proj-x)',
167
+ 'collision (b/proj-x)',
168
+ 'collision (sub/proj-x)',
169
+ ], 'two trailing segments already tell all three roots apart');
170
+ });
171
+
172
+ test('when two segments still end alike, the labels go one segment deeper', () => {
173
+ const x = rootOf('x', 'p', 'proj-x');
174
+ const y = rootOf('y', 'p', 'proj-x');
175
+ const result = buildListing({
176
+ groups: [
177
+ checkoutGroup('proj-x', x, [doc(x, 'collision', 10)]),
178
+ checkoutGroup('proj-x', y, [doc(y, 'collision', 20)]),
179
+ ],
180
+ });
181
+ assert.deepEqual(labelsOf(result), ['collision (x/p/proj-x)', 'collision (y/p/proj-x)']);
182
+ });
183
+
184
+ test('(b) a document literally named like a generated label does not collide', () => {
185
+ const wtRoot = rootOf('checkout-worktrees', 'wt');
186
+ const literal = path.join(TASKS, 'x (main).md');
187
+ const result = buildListing({
188
+ groups: [
189
+ checkoutGroup('main', MAIN_ROOT, [
190
+ doc(MAIN_ROOT, 'x', 100),
191
+ { name: 'x (main)', docPath: literal, mtimeMs: 50 },
192
+ ], true),
193
+ checkoutGroup('wt', wtRoot, [doc(wtRoot, 'x', 90)]),
194
+ ],
195
+ });
196
+ const labels = labelsOf(result);
197
+ assert.equal(new Set(labels).size, labels.length, `every label is unique: ${labels.join(' | ')}`);
198
+ assert.equal(result.menu.length, 3, 'no row was dropped or merged to reach uniqueness');
199
+ assert.deepEqual(labels, ['x (main) #2', 'x (main)', 'x (wt)'],
200
+ 'the literal document keeps its exact name; the generated label is the one that moves');
201
+ // The menu is unique too, and carries the same labels as the panel list.
202
+ assert.deepEqual(menuOf(result), [
203
+ ['x', 'x (main) #2'],
204
+ ['x (main)', 'x (main)'],
205
+ ['x', 'x (wt)'],
206
+ ]);
207
+ assert.equal(new Set(result.menu.map((row) => row.label)).size, 3);
208
+ });
209
+
210
+ test('two documents with one name inside one checkout are numbered', () => {
211
+ const first = path.join(TASKS, 'same.md');
212
+ const second = path.join(MAIN_ROOT, 'odd', 'archive', 'same.md');
213
+ const result = buildListing({
214
+ groups: [
215
+ checkoutGroup('main', MAIN_ROOT, [
216
+ { name: 'same', docPath: second, mtimeMs: 10 },
217
+ { name: 'same', docPath: first, mtimeMs: 10 },
218
+ ], true),
219
+ ],
220
+ });
221
+ assert.deepEqual(result.groups[0].entries.map((entry) => [entry.path, entry.label]), [
222
+ [second, 'same (main)'],
223
+ [first, 'same (main) #2'],
224
+ ], 'the path tie-break decides the order (`archive` before `tasks`), and the label counter follows it');
225
+ });
226
+
227
+ test('every label in a mixed listing is unique, panel list and menu alike', () => {
228
+ const a = rootOf('a', 'proj-x');
229
+ const b = rootOf('b', 'proj-x');
230
+ const wtRoot = rootOf('checkout-worktrees', 'wt-a');
231
+ const result = buildListing({
232
+ groups: [
233
+ checkoutGroup('main', MAIN_ROOT, [
234
+ doc(MAIN_ROOT, 'alpha', 100),
235
+ doc(MAIN_ROOT, 'collision', 90),
236
+ { name: 'collision (main)', docPath: path.join(TASKS, 'collision (main).md'), mtimeMs: 80 },
237
+ doc(MAIN_ROOT, 'proj-x', 70),
238
+ ], true),
239
+ checkoutGroup('proj-x', a, [doc(a, 'collision', 60), doc(a, 'proj-x', 50)]),
240
+ checkoutGroup('proj-x', b, [doc(b, 'collision', 40), doc(b, 'proj-x', 30)]),
241
+ checkoutGroup('wt-a', wtRoot, [doc(wtRoot, 'collision', 20)]),
242
+ ],
243
+ });
244
+ const labels = everyLabel(result);
245
+ assert.equal(new Set(labels).size, labels.length, `unique across the whole listing: ${labels.join(' | ')}`);
246
+ assert.equal(result.menu.length, 9, 'every document exactly once');
247
+ // A literal name still wins its exact text.
248
+ assert.equal(
249
+ result.groups[0].entries.find((entry) => entry.path === path.join(TASKS, 'collision (main).md')).label,
250
+ 'collision (main)',
251
+ );
252
+ });
253
+ });
254
+
255
+ describe('order: ONE comparator, flattened group by group', () => {
256
+ test('a main-only listing is mtime DESC, then name (the pre-redesign baseline)', () => {
257
+ const result = buildListing({
258
+ groups: [checkoutGroup('main', MAIN_ROOT, [
259
+ doc(MAIN_ROOT, 'older', 1000),
260
+ doc(MAIN_ROOT, 'newest', 3000),
261
+ doc(MAIN_ROOT, 'middle', 2000),
262
+ ], true)],
263
+ });
264
+ assert.deepEqual(result.menu.map((row) => row.name), ['newest', 'middle', 'older']);
265
+ assert.deepEqual(result.menu.map((row) => row.path), [
266
+ path.join(TASKS, 'newest.md'),
267
+ path.join(TASKS, 'middle.md'),
268
+ path.join(TASKS, 'older.md'),
269
+ ]);
270
+ assert.deepEqual(result.groups[0].entries.map((entry) => entry.name), ['newest', 'middle', 'older']);
271
+ });
272
+
273
+ test('a main-only listing with tied mtimes orders by name in code units, never locale', () => {
274
+ const result = buildListing({
275
+ groups: [checkoutGroup('main', MAIN_ROOT, [
276
+ doc(MAIN_ROOT, 'a', 5000),
277
+ doc(MAIN_ROOT, 'B', 5000),
278
+ doc(MAIN_ROOT, 'b', 5000),
279
+ ], true)],
280
+ });
281
+ assert.deepEqual(result.menu.map((row) => row.name), ['B', 'a', 'b'],
282
+ 'code units put B (66) before a (97); localeCompare would not');
283
+ });
284
+
285
+ test('an equal time and name are broken by the path in code units', () => {
286
+ const early = path.join(MAIN_ROOT, 'a', 'x.md');
287
+ const late = path.join(MAIN_ROOT, 'b', 'x.md');
288
+ const result = buildListing({
289
+ groups: [checkoutGroup('main', MAIN_ROOT, [
290
+ { name: 'x', docPath: late, mtimeMs: 7 },
291
+ { name: 'x', docPath: early, mtimeMs: 7 },
292
+ ], true)],
293
+ });
294
+ assert.deepEqual(result.groups[0].entries.map((entry) => entry.path), [early, late]);
295
+ });
296
+
297
+ test('the menu is main’s group first, then each group in the order given', () => {
298
+ const wtA = rootOf('checkout-worktrees', 'wt-a');
299
+ const wtB = rootOf('checkout-worktrees', 'wt-b');
300
+ const result = buildListing({
301
+ groups: [
302
+ checkoutGroup('wt-a', wtA, [doc(wtA, 'a-old', 10), doc(wtA, 'a-new', 30)]),
303
+ checkoutGroup('main', MAIN_ROOT, [doc(MAIN_ROOT, 'root', 20)], true),
304
+ checkoutGroup('wt-b', wtB, [doc(wtB, 'b-only', 99)]),
305
+ ],
306
+ });
307
+ assert.deepEqual(result.groups.map((group) => group.checkoutName), ['main', 'wt-a', 'wt-b']);
308
+ assert.deepEqual(result.menu.map((row) => row.name), ['root', 'a-new', 'a-old', 'b-only'],
309
+ 'each group’s entries in comparator order; the groups keep the panel’s order');
310
+ assert.deepEqual(result.menu.map((row) => row.name), result.groups.flatMap((group) => group.entries.map((entry) => entry.name)),
311
+ 'the menu is exactly the panel list flattened — one order, never two');
312
+ });
313
+
314
+ // L11/T16: the order a caller HANDS OVER is preserved. Main's documents are
315
+ // ranked by lib/odd-docs.js (commit recency, then display name) and the caller
316
+ // passes that rank as each entry's `order`; the listing must not re-rank them by
317
+ // mtime, or the menu's first entry opens a different file than the rank named.
318
+ test('a caller’s per-entry order is the PRIMARY key, and compareEntries only breaks ties', () => {
319
+ const result = buildListing({
320
+ groups: [checkoutGroup('main', MAIN_ROOT, [
321
+ { ...doc(MAIN_ROOT, 'main-two', 2000), order: 1 },
322
+ { ...doc(MAIN_ROOT, 'main-one', 1000), order: 0 },
323
+ { ...doc(MAIN_ROOT, 'main-three', 3000), order: 2 },
324
+ ], true)],
325
+ });
326
+ assert.deepEqual(result.menu.map((row) => row.name), ['main-one', 'main-two', 'main-three'],
327
+ 'mtime alone would read main-three, main-two, main-one');
328
+ assert.deepEqual(result.groups[0].entries.map((entry) => entry.name), ['main-one', 'main-two', 'main-three'],
329
+ 'the panel list and the menu draw the one preserved order');
330
+ assert.deepEqual(Object.keys(result.groups[0].entries[0]).sort(), ['label', 'mtimeMs', 'name', 'path'],
331
+ 'the order signal stays out of the entry value');
332
+ });
333
+
334
+ test('a tied order falls back to compareEntries, and an unordered entry comes last', () => {
335
+ const result = buildListing({
336
+ groups: [checkoutGroup('main', MAIN_ROOT, [
337
+ doc(MAIN_ROOT, 'unranked-old', 9000),
338
+ { ...doc(MAIN_ROOT, 'tied-b', 500), order: 0 },
339
+ { ...doc(MAIN_ROOT, 'tied-a', 700), order: 0 },
340
+ ], true)],
341
+ });
342
+ assert.deepEqual(result.menu.map((row) => row.name), ['tied-a', 'tied-b', 'unranked-old'],
343
+ 'a tie is compareEntries’ answer, and a caller that ordered nothing stays behind what was ordered');
344
+ });
345
+
346
+ // The fallback half of the same rule: a caller that gives NO order signal gets
347
+ // exactly the comparator order this module answered before the signal existed.
348
+ test('a caller who gives NO order gets compareEntries’ order', () => {
349
+ const docs = [
350
+ doc(MAIN_ROOT, 'main-two', 2000),
351
+ doc(MAIN_ROOT, 'main-one', 1000),
352
+ doc(MAIN_ROOT, 'main-three', 3000),
353
+ ];
354
+ const result = buildListing({ groups: [checkoutGroup('main', MAIN_ROOT, docs, true)] });
355
+ assert.deepEqual(result.menu.map((row) => row.name), ['main-three', 'main-two', 'main-one']);
356
+ assert.deepEqual(result.menu.map((row) => row.name), docs.slice().sort(compareEntries).map((entry) => entry.name));
357
+ });
358
+
359
+ test('compareEntries is the ONE comparator and it sorts a copy without mutating it', () => {
360
+ const entries = [
361
+ { name: 'b', path: '/p/b', mtimeMs: 5 },
362
+ { name: 'a', path: '/p/a', mtimeMs: 5 },
363
+ { name: 'z', path: '/p/z', mtimeMs: 9 },
364
+ { name: 'a', path: '/p/a', mtimeMs: 5 },
365
+ ];
366
+ const before = JSON.stringify(entries);
367
+ const sorted = entries.slice().sort(compareEntries);
368
+ assert.deepEqual(sorted.map((entry) => `${entry.name}:${entry.mtimeMs}`), ['z:9', 'a:5', 'a:5', 'b:5']);
369
+ assert.equal(JSON.stringify(entries), before, 'the comparator is pure');
370
+ assert.equal(compareEntries(entries[1], { ...entries[1] }), 0, 'equal keys answer 0');
371
+ assert.equal(compareEntries(entries[2], entries[1]) < 0, true, 'a newer document sorts first');
372
+ assert.equal(compareEntries(entries[1], entries[2]) > 0, true, 'and the reverse is the mirror');
373
+ // A new entry with an unreadable time is the oldest, exactly as the listing stores it.
374
+ assert.equal(compareEntries({ name: 'a', path: '/p/a', mtimeMs: 0 }, entries[1]) > 0, true,
375
+ 'a name tie at mtime 0 loses to a real time');
376
+ // The menu of a one-group listing IS this sort applied to its entries.
377
+ const result = buildListing({
378
+ groups: [checkoutGroup('main', MAIN_ROOT, [
379
+ doc(MAIN_ROOT, 'b', 5),
380
+ doc(MAIN_ROOT, 'z', 9),
381
+ doc(MAIN_ROOT, 'a', 5),
382
+ ], true)],
383
+ });
384
+ assert.deepEqual(
385
+ result.menu.map((row) => row.path),
386
+ result.groups[0].entries.slice().sort(compareEntries).map((entry) => entry.path),
387
+ );
388
+ });
389
+ });
390
+
391
+ describe('fail closed on junk', () => {
392
+ test('malformed input answers an empty listing, never a throw', () => {
393
+ for (const input of [undefined, null, {}, { groups: 'nope' }, { groups: null }, { groups: [] }]) {
394
+ assert.deepEqual(buildListing(input), { groups: [], menu: [] });
395
+ }
396
+ assert.deepEqual(buildListing({ groups: [null, 7, 'group', { docs: 'nope' }, { entries: 5 }] }), { groups: [], menu: [] });
397
+ });
398
+
399
+ test('a malformed group or entry is skipped and the rest still lists', () => {
400
+ const result = buildListing({
401
+ groups: [
402
+ null,
403
+ checkoutGroup('main', MAIN_ROOT, [
404
+ doc(MAIN_ROOT, 'alpha', 10),
405
+ null,
406
+ 42,
407
+ { name: 7, docPath: path.join(TASKS, 'numeric.md'), mtimeMs: 5 },
408
+ { name: 'no-path' },
409
+ { docPath: path.join(TASKS, 'anonymous.md'), mtimeMs: 5 },
410
+ { name: '', docPath: path.join(TASKS, 'blank.md'), mtimeMs: 5 },
411
+ ], true),
412
+ { checkoutName: 'junk', checkoutRoot: MAIN_ROOT, docs: [undefined] },
413
+ ],
414
+ });
415
+ assert.deepEqual(result.menu.map((row) => row.name), ['alpha']);
416
+ assert.deepEqual(result.groups.map((group) => group.checkoutName), ['main'], 'a group with no usable entry vanishes');
417
+ });
418
+
419
+ test('a document with no readable mtime is the oldest and still appears', () => {
420
+ const result = buildListing({
421
+ groups: [checkoutGroup('main', MAIN_ROOT, [
422
+ { name: 'dated', docPath: path.join(TASKS, 'dated.md'), mtimeMs: 4000 },
423
+ { name: 'no-time', docPath: path.join(TASKS, 'no-time.md') },
424
+ { name: 'nonsense', docPath: path.join(TASKS, 'nonsense.md'), mtimeMs: 'nonsense' },
425
+ { name: 'infinite', docPath: path.join(TASKS, 'infinite.md'), mtimeMs: Number.POSITIVE_INFINITY },
426
+ ], true)],
427
+ });
428
+ assert.deepEqual(result.menu.map((row) => [row.name, row.label]), [
429
+ ['dated', 'dated'],
430
+ ['infinite', 'infinite'],
431
+ ['no-time', 'no-time'],
432
+ ['nonsense', 'nonsense'],
433
+ ], 'the three untimed documents tie at 0 and order by name in code units (`-` is 45, `n` is 110)');
434
+ assert.deepEqual(result.groups[0].entries.map((entry) => entry.mtimeMs), [4000, 0, 0, 0],
435
+ 'no time is reported as 0, never as a time the document never had');
436
+ });
437
+
438
+ test('empty groups vanish and an all-empty model answers no group and no menu', () => {
439
+ assert.deepEqual(buildListing({ groups: [checkoutGroup('main', MAIN_ROOT, [], true)] }), { groups: [], menu: [] });
440
+ const result = buildListing({
441
+ groups: [
442
+ checkoutGroup('main', MAIN_ROOT, [doc(MAIN_ROOT, 'alpha', 10)], true),
443
+ checkoutGroup('wt-a', rootOf('checkout-worktrees', 'wt-a'), []),
444
+ checkoutGroup('wt-b', rootOf('checkout-worktrees', 'wt-b'), []),
445
+ ],
446
+ });
447
+ assert.deepEqual(result.groups.map((group) => group.checkoutName), ['main']);
448
+ });
449
+ });
450
+
451
+ describe('the value shape, and purity', () => {
452
+ test('the listing carries exactly the documented fields', () => {
453
+ const result = buildListing({
454
+ groups: [checkoutGroup('main', MAIN_ROOT, [doc(MAIN_ROOT, 'alpha', 10)], true)],
455
+ });
456
+ assert.deepEqual(Object.keys(result).sort(), ['groups', 'menu']);
457
+ assert.deepEqual(Object.keys(result.groups[0]).sort(), ['checkoutName', 'checkoutRoot', 'entries', 'isMain']);
458
+ assert.deepEqual(Object.keys(result.groups[0].entries[0]).sort(), ['label', 'mtimeMs', 'name', 'path']);
459
+ assert.deepEqual(Object.keys(result.menu[0]).sort(), ['label', 'name', 'path']);
460
+ assert.equal(result.groups[0].isMain, true);
461
+ assert.equal(result.groups[0].checkoutRoot, MAIN_ROOT);
462
+ });
463
+
464
+ test('a group without a checkoutName names itself after its root', () => {
465
+ const wt = rootOf('checkout-worktrees', 'wt-a');
466
+ const result = buildListing({ groups: [{ checkoutRoot: wt, docs: [doc(wt, 'alpha', 10)] }] });
467
+ assert.equal(result.groups[0].checkoutName, 'wt-a');
468
+ assert.equal(result.groups[0].isMain, false);
469
+ });
470
+
471
+ test('a listing handed back in is the same listing (round trip is idempotent)', () => {
472
+ const wt = rootOf('checkout-worktrees', 'wt-a');
473
+ const first = buildListing({
474
+ groups: [
475
+ checkoutGroup('main', MAIN_ROOT, [doc(MAIN_ROOT, 'alpha', 10), doc(MAIN_ROOT, 'beta', 30)], true),
476
+ checkoutGroup('wt-a', wt, [doc(wt, 'alpha', 20)]),
477
+ ],
478
+ });
479
+ assert.deepEqual(buildListing({ groups: first.groups }), first);
480
+ });
481
+
482
+ test('the inputs are never mutated', () => {
483
+ const wt = rootOf('checkout-worktrees', 'wt-a');
484
+ const input = {
485
+ groups: [
486
+ checkoutGroup('main', MAIN_ROOT, [doc(MAIN_ROOT, 'alpha', 10), doc(MAIN_ROOT, 'beta', 30)], true),
487
+ checkoutGroup('proj-x', rootOf('a', 'proj-x'), [doc(rootOf('a', 'proj-x'), 'alpha', 20)]),
488
+ checkoutGroup('proj-x', rootOf('b', 'proj-x'), [doc(rootOf('b', 'proj-x'), 'alpha', 25)]),
489
+ checkoutGroup('wt-a', wt, []),
490
+ ],
491
+ };
492
+ const before = JSON.stringify(input);
493
+ buildListing(input);
494
+ assert.equal(JSON.stringify(input), before);
495
+ });
496
+
497
+ test('it is pure: no fs, no git, no clock, and only the two documented exports', () => {
498
+ const source = fs.readFileSync(SRC, 'utf8');
499
+ for (const forbidden of ['node:fs', 'node:child_process', 'node:os', 'Date.now', 'new Date', 'localeCompare(']) {
500
+ assert.equal(source.includes(forbidden), false, `lib/odd-listing.js must not use ${forbidden}`);
501
+ }
502
+ assert.deepEqual(Object.keys(listing).sort(), ['buildListing', 'compareEntries']);
503
+ });
504
+ });