pi-odd-worktree 3.11.2 → 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.
@@ -0,0 +1,950 @@
1
+ 'use strict';
2
+ // T7 (odd-rail-section S10) tests: lib/odd-group.js — the grouped model the ODD
3
+ // panel draws, one group per checkout.
4
+ //
5
+ // Contract under test: an Array of groups, the MAIN checkout first (named `main`,
6
+ // `isMain: true`) carrying EVERY document of its `odd/tasks` with no `status`
7
+ // field, then one group per OWNED worktree (invariant 10: a worktree is a path,
8
+ // not a document) named by the last segment of that path, carrying ONLY the
9
+ // documents that are new or modified relative to main.
10
+ //
11
+ // "New or modified" is two comparisons, both required: the candidate set is what
12
+ // the worktree's branch changed relative to the MERGE BASE with main (`git
13
+ // merge-base HEAD <mainHead>`, then a name-status diff against that base), so a
14
+ // change that is already COMMITTED still counts where a plain working-tree diff
15
+ // would find nothing; and a document whose BYTES equal main's current copy is
16
+ // dropped even when that first comparison names it — the "main advanced, the
17
+ // worktree's copy is byte-identical" case. `status` is `added` when main has no
18
+ // document at that path and `modified` otherwise.
19
+ //
20
+ // Git is the injected `runAsync` seam every neighbouring module already takes, so
21
+ // no test spawns git or touches a real worktree: the fake answers `worktree list`,
22
+ // `rev-parse HEAD`, `merge-base`, `diff --name-status` and `status`, and the
23
+ // assertions read the observed argv instead of the source. The checkouts are real
24
+ // directories under the system temp directory, shaped by the one rule that decides
25
+ // which worktrees are ours: `<root>` is the main checkout and
26
+ // `<root>-worktrees/<name>` are the owned ones.
27
+
28
+ const { test, describe, 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 { groupOddDocs } = require('../lib/odd-group.js');
35
+
36
+ const MAIN_HEAD = 'a'.repeat(40);
37
+ const MERGE_BASE = 'b'.repeat(40);
38
+
39
+ const roots = [];
40
+ after(() => {
41
+ for (const dir of roots) fs.rmSync(dir, { recursive: true, force: true });
42
+ });
43
+
44
+ // A machine-shaped tree: the base directory, the main checkout and the one
45
+ // directory this package creates worktrees in.
46
+ function machine(tag) {
47
+ const base = fs.mkdtempSync(path.join(os.tmpdir(), `odd-group-${tag}-`));
48
+ roots.push(base);
49
+ const mainRoot = path.join(base, 'repo');
50
+ fs.mkdirSync(mainRoot, { recursive: true });
51
+ return {
52
+ base,
53
+ mainRoot,
54
+ worktreesDir: path.join(base, 'repo-worktrees'),
55
+ worktree: (name) => path.join(base, 'repo-worktrees', name),
56
+ outside: (name) => path.join(base, name),
57
+ };
58
+ }
59
+
60
+ // One ODD document with EXACT bytes: the content comparison is the point of the
61
+ // module, so no fixture may carry a stray newline it did not ask for.
62
+ function writeDoc(checkout, name, { body = `# ${name}\n`, at } = {}) {
63
+ const dir = path.join(checkout, 'odd', 'tasks');
64
+ fs.mkdirSync(dir, { recursive: true });
65
+ const target = path.join(dir, `${name}.md`);
66
+ fs.writeFileSync(target, body);
67
+ if (at !== undefined) setMtime(target, at);
68
+ return target;
69
+ }
70
+
71
+ function setMtime(target, iso) {
72
+ const when = new Date(iso);
73
+ fs.utimesSync(target, when, when);
74
+ }
75
+
76
+ const mtimeOf = (target) => fs.statSync(target).mtimeMs;
77
+
78
+ // `git worktree list --porcelain -z` exactly as Git writes it: one NUL-terminated
79
+ // field per line, a blank field between records, main first.
80
+ function porcelain(paths) {
81
+ return paths
82
+ .map((target) => `worktree ${target}\0HEAD ${'0'.repeat(40)}\0branch refs/heads/main\0\0`)
83
+ .join('');
84
+ }
85
+
86
+ // `--name-status -z`: `<letter>\0<path>\0` per record.
87
+ const nameStatus = (records) => records.map(([letter, rel]) => `${letter}\0${rel}\0`).join('');
88
+
89
+ // `status --porcelain=v1 -z`: `<XY> <path>\0` per record.
90
+ const statusOutput = (records) => records.map((entry) => `${entry}\0`).join('');
91
+
92
+ // The git seam: a recording fake answering every read this module makes, per
93
+ // checkout, so the fixed argv and the cwd are asserted from observed calls.
94
+ function stripConfig(argv) {
95
+ const args = [...argv];
96
+ while (args.length > 1 && args[0] === '-c') args.splice(0, 2);
97
+ return args;
98
+ }
99
+
100
+ // Flags that come before the verb of a read, so the fake answers the verb the
101
+ // module actually asked for instead of the last option.
102
+ const OPTION_PREFIXES = new Set(['--no-optional-locks']);
103
+
104
+ function primaryOf(args) {
105
+ for (let index = 0; index < args.length; index += 1) {
106
+ if (args[index] === '-c') {
107
+ index += 1;
108
+ continue;
109
+ }
110
+ if (OPTION_PREFIXES.has(args[index])) continue;
111
+ return args[index];
112
+ }
113
+ return '';
114
+ }
115
+
116
+ function fakeGit(options = {}) {
117
+ const calls = [];
118
+ const deltas = new Map();
119
+ const statuses = new Map();
120
+ const failing = new Set(options.fail || []);
121
+ const runAsync = async (bin, argv, cwd) => {
122
+ calls.push({ bin, argv: [...argv], cwd });
123
+ if (options.throws === true) throw new Error('spawn failed');
124
+ const args = stripConfig(argv);
125
+ const verb = primaryOf(args);
126
+ if (failing.has(verb)) return { status: 128, stdout: '', stderr: `fatal: ${verb} failed` };
127
+ switch (verb) {
128
+ case 'worktree':
129
+ return { status: 0, stdout: porcelain(options.paths || []), stderr: '' };
130
+ case 'rev-parse':
131
+ return { status: 0, stdout: `${options.head === undefined ? MAIN_HEAD : options.head}\n`, stderr: '' };
132
+ case 'merge-base':
133
+ return { status: 0, stdout: `${options.base === undefined ? MERGE_BASE : options.base}\n`, stderr: '' };
134
+ case 'diff':
135
+ return { status: 0, stdout: nameStatus(deltas.get(cwd) || []), stderr: '' };
136
+ case 'status':
137
+ return { status: 0, stdout: statusOutput(statuses.get(cwd) || []), stderr: '' };
138
+ default:
139
+ return { status: 1, stdout: '', stderr: `unexpected read: ${verb}` };
140
+ }
141
+ };
142
+ return {
143
+ calls,
144
+ runAsync,
145
+ delta: (root, records) => deltas.set(root, records),
146
+ status: (root, records) => statuses.set(root, records),
147
+ verbs: () => calls.map((call) => ({
148
+ verb: primaryOf(stripConfig(call.argv)),
149
+ args: stripConfig(call.argv),
150
+ cwd: call.cwd,
151
+ })),
152
+ };
153
+ }
154
+
155
+ const contract = (group) => ({
156
+ checkoutName: group.checkoutName,
157
+ checkoutRoot: group.checkoutRoot,
158
+ isMain: group.isMain,
159
+ docs: group.docs.map((doc) => ({ name: doc.name, status: doc.status })),
160
+ });
161
+
162
+ describe('S10: the main group carries EVERY document, with no status', () => {
163
+ test('every document of main odd/tasks is listed and none carries a status', async () => {
164
+ const box = machine('main-all');
165
+ const older = writeDoc(box.mainRoot, 'alpha', { at: '2020-01-01T00:00:00.000Z' });
166
+ const newer = writeDoc(box.mainRoot, 'beta', { at: '2020-02-01T00:00:00.000Z' });
167
+ const git = fakeGit({ paths: [box.mainRoot] });
168
+
169
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
170
+
171
+ assert.deepEqual(groups, [
172
+ {
173
+ checkoutName: 'main',
174
+ checkoutRoot: box.mainRoot,
175
+ isMain: true,
176
+ docs: [
177
+ { name: 'beta', docPath: newer, mtimeMs: mtimeOf(newer) },
178
+ { name: 'alpha', docPath: older, mtimeMs: mtimeOf(older) },
179
+ ],
180
+ },
181
+ ]);
182
+ assert.deepEqual(Object.keys(groups[0].docs[0]).sort(), ['docPath', 'mtimeMs', 'name']);
183
+ });
184
+
185
+ test('only `odd/tasks/*.md` counts: not nested, not a dotfile, not another suffix', async () => {
186
+ const box = machine('main-rule');
187
+ writeDoc(box.mainRoot, 'real', { at: '2020-01-01T00:00:00.000Z' });
188
+ writeDoc(path.join(box.mainRoot, 'odd', 'tasks', 'nested'), 'deep', { at: '2020-02-01T00:00:00.000Z' });
189
+ writeDoc(box.mainRoot, '.hidden', { at: '2020-03-01T00:00:00.000Z' });
190
+ fs.writeFileSync(path.join(box.mainRoot, 'odd', 'tasks', 'notes.txt'), 'not a document\n');
191
+ const git = fakeGit({ paths: [box.mainRoot] });
192
+
193
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
194
+
195
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['real']);
196
+ });
197
+
198
+ test('main with no odd/tasks is a group with no documents, not a missing group', async () => {
199
+ const box = machine('main-empty');
200
+ const git = fakeGit({ paths: [box.mainRoot] });
201
+
202
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
203
+
204
+ assert.deepEqual(groups, [
205
+ { checkoutName: 'main', checkoutRoot: box.mainRoot, isMain: true, docs: [] },
206
+ ]);
207
+ });
208
+ });
209
+
210
+ describe('S10: a worktree carries only its delta against main', () => {
211
+ test('only the documents git names are listed, and nothing that matches main', async () => {
212
+ const box = machine('delta');
213
+ const worktree = box.worktree('odd-rail');
214
+ writeDoc(box.mainRoot, 'alpha', { body: 'shared\n', at: '2020-01-01T00:00:00.000Z' });
215
+ writeDoc(box.mainRoot, 'beta', { body: 'main version\n', at: '2020-01-02T00:00:00.000Z' });
216
+ writeDoc(worktree, 'alpha', { body: 'shared\n', at: '2020-01-01T00:00:00.000Z' });
217
+ const changed = writeDoc(worktree, 'beta', { body: 'worktree version\n', at: '2020-03-01T00:00:00.000Z' });
218
+ const fresh = writeDoc(worktree, 'gamma', { body: '# gamma\n', at: '2020-04-01T00:00:00.000Z' });
219
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
220
+ git.delta(worktree, [['M', 'odd/tasks/beta.md'], ['A', 'odd/tasks/gamma.md']]);
221
+
222
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
223
+
224
+ assert.deepEqual(contract(groups[1]), {
225
+ checkoutName: 'odd-rail',
226
+ checkoutRoot: worktree,
227
+ isMain: false,
228
+ docs: [
229
+ { name: 'gamma', status: 'added' },
230
+ { name: 'beta', status: 'modified' },
231
+ ],
232
+ });
233
+ assert.deepEqual(groups[1].docs.map((doc) => doc.docPath), [fresh, changed]);
234
+ assert.deepEqual(groups[1].docs.map((doc) => doc.mtimeMs), [mtimeOf(fresh), mtimeOf(changed)]);
235
+ // Main still carries every one of its own documents, alpha included.
236
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['beta', 'alpha']);
237
+ });
238
+
239
+ test('added is a path main does not have; modified is a path it does', async () => {
240
+ const box = machine('statuses');
241
+ const worktree = box.worktree('odd-rail');
242
+ writeDoc(box.mainRoot, 'kept', { body: 'main\n' });
243
+ writeDoc(worktree, 'kept', { body: 'worktree\n', at: '2020-02-01T00:00:00.000Z' });
244
+ writeDoc(worktree, 'brand-new', { body: '# brand-new\n', at: '2020-01-01T00:00:00.000Z' });
245
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
246
+ git.delta(worktree, [['M', 'odd/tasks/kept.md'], ['A', 'odd/tasks/brand-new.md']]);
247
+
248
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
249
+
250
+ assert.deepEqual(contract(groups[1]).docs, [
251
+ { name: 'kept', status: 'modified' },
252
+ { name: 'brand-new', status: 'added' },
253
+ ]);
254
+ });
255
+
256
+ test('a document git does not name is not a difference, even when its bytes differ', async () => {
257
+ // Main moved ahead: the worktree sits at the merge base and holds the OLD
258
+ // copy. Different bytes, but nothing of the worktree's own, so no row.
259
+ const box = machine('unnamed');
260
+ const worktree = box.worktree('odd-rail');
261
+ writeDoc(box.mainRoot, 'shared', { body: 'main advanced\n', at: '2020-05-01T00:00:00.000Z' });
262
+ writeDoc(worktree, 'shared', { body: 'the old copy\n', at: '2020-01-01T00:00:00.000Z' });
263
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
264
+
265
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
266
+
267
+ assert.deepEqual(groups[1].docs, []);
268
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['shared']);
269
+ });
270
+
271
+ test('a byte-identical copy is not a difference even when the merge base names it', async () => {
272
+ // The case the user named: main advanced, and the worktree's copy of the
273
+ // same path ends up byte-identical. The comparison against the merge base
274
+ // still names it, so only the CONTENT comparison can drop it.
275
+ const box = machine('identical');
276
+ const worktree = box.worktree('odd-rail');
277
+ writeDoc(box.mainRoot, 'shared', { body: '# shared\n\nsame bytes\n' });
278
+ writeDoc(worktree, 'shared', { body: '# shared\n\nsame bytes\n' });
279
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
280
+ git.delta(worktree, [['M', 'odd/tasks/shared.md']]);
281
+
282
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
283
+
284
+ assert.deepEqual(groups[1].docs, []);
285
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['shared']);
286
+ });
287
+
288
+ test('a committed change still counts: the diff is against the merge base, not the working tree', async () => {
289
+ const box = machine('committed');
290
+ const worktree = box.worktree('odd-rail');
291
+ writeDoc(box.mainRoot, 'spec', { body: 'before\n' });
292
+ writeDoc(worktree, 'spec', { body: 'after\n', at: '2020-02-01T00:00:00.000Z' });
293
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
294
+ git.delta(worktree, [['M', 'odd/tasks/spec.md']]);
295
+
296
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
297
+
298
+ assert.deepEqual(contract(groups[1]).docs, [{ name: 'spec', status: 'modified' }]);
299
+ // The base is resolved the way lib/worktree-changes.js resolves it: main's
300
+ // HEAD first, then the merge base of the two, INSIDE the worktree.
301
+ assert.deepEqual(
302
+ git.verbs().filter((call) => call.verb === 'rev-parse').map((call) => call),
303
+ [{ verb: 'rev-parse', args: ['rev-parse', 'HEAD'], cwd: box.mainRoot }],
304
+ );
305
+ assert.deepEqual(git.verbs().filter((call) => call.verb === 'merge-base'), [
306
+ { verb: 'merge-base', args: ['merge-base', 'HEAD', MAIN_HEAD], cwd: worktree },
307
+ ]);
308
+ // The diff names paths against that merge base with no second revision, so
309
+ // the comparison is the branch's own work (committed or not), never HEAD.
310
+ assert.deepEqual(git.verbs().filter((call) => call.verb === 'diff'), [
311
+ {
312
+ verb: 'diff',
313
+ args: ['diff', '--name-status', '-z', '--no-renames', MERGE_BASE, '--', 'odd/tasks'],
314
+ cwd: worktree,
315
+ },
316
+ ]);
317
+ });
318
+
319
+ test('an untracked new document counts, as it does in the files panel', async () => {
320
+ const box = machine('untracked');
321
+ const worktree = box.worktree('odd-rail');
322
+ writeDoc(box.mainRoot, 'alpha', { body: 'shared\n' });
323
+ writeDoc(worktree, 'alpha', { body: 'shared\n' });
324
+ writeDoc(worktree, 'draft', { body: '# draft\n', at: '2020-02-01T00:00:00.000Z' });
325
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
326
+ git.status(worktree, ['?? odd/tasks/draft.md']);
327
+
328
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
329
+
330
+ assert.deepEqual(contract(groups[1]).docs, [{ name: 'draft', status: 'added' }]);
331
+ assert.deepEqual(
332
+ git.verbs().filter((call) => call.verb === 'status').map((call) => call.args),
333
+ [['--no-optional-locks', 'status', '--porcelain=v1', '-z', '--untracked-files=all', '--', 'odd/tasks']],
334
+ );
335
+ });
336
+
337
+ test('a main document the worktree does not have is no worktree row at all', async () => {
338
+ const box = machine('deleted');
339
+ const worktree = box.worktree('odd-rail');
340
+ writeDoc(box.mainRoot, 'gone', { body: '# gone\n' });
341
+ writeDoc(worktree, 'kept', { body: '# kept\n', at: '2020-01-01T00:00:00.000Z' });
342
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
343
+ git.delta(worktree, [['D', 'odd/tasks/gone.md'], ['A', 'odd/tasks/kept.md']]);
344
+
345
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
346
+
347
+ assert.deepEqual(contract(groups[1]).docs, [{ name: 'kept', status: 'added' }]);
348
+ });
349
+ });
350
+
351
+ describe('S10: group order and per-group order', () => {
352
+ test('main is first, then the owned worktrees by name; a foreign worktree is not a group', async () => {
353
+ const box = machine('order-groups');
354
+ const oddB = box.worktree('odd-b');
355
+ const oddA = box.worktree('odd-a');
356
+ const outside = box.outside('somewhere-else');
357
+ writeDoc(box.mainRoot, 'main-odd', { at: '2020-01-01T00:00:00.000Z' });
358
+ writeDoc(oddB, 'b-doc', { at: '2020-01-01T00:00:00.000Z' });
359
+ writeDoc(oddA, 'a-doc', { at: '2020-01-01T00:00:00.000Z' });
360
+ writeDoc(outside, 'foreign', { at: '2020-01-01T00:00:00.000Z' });
361
+ const git = fakeGit({ paths: [box.mainRoot, oddB, outside, oddA] });
362
+ git.delta(oddB, [['A', 'odd/tasks/b-doc.md']]);
363
+ git.delta(oddA, [['A', 'odd/tasks/a-doc.md']]);
364
+
365
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
366
+
367
+ assert.deepEqual(groups.map((group) => group.checkoutName), ['main', 'odd-a', 'odd-b']);
368
+ assert.deepEqual(groups.map((group) => group.isMain), [true, false, false]);
369
+ });
370
+
371
+ test('within a group: most recently modified first, then by name in code-unit order', async () => {
372
+ const box = machine('order-docs');
373
+ const worktree = box.worktree('odd-rail');
374
+ const stamp = '2020-03-01T00:00:00.000Z';
375
+ writeDoc(box.mainRoot, 'b', { at: stamp });
376
+ writeDoc(box.mainRoot, 'a', { at: stamp });
377
+ writeDoc(box.mainRoot, 'z', { at: '2020-01-01T00:00:00.000Z' });
378
+ writeDoc(worktree, 'B', { body: 'worktree B\n', at: stamp });
379
+ writeDoc(worktree, 'a', { body: 'worktree a\n', at: stamp });
380
+ writeDoc(worktree, 'older', { body: 'worktree older\n', at: '2020-01-01T00:00:00.000Z' });
381
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
382
+ git.delta(worktree, [
383
+ ['M', 'odd/tasks/B.md'], ['M', 'odd/tasks/a.md'], ['A', 'odd/tasks/older.md'],
384
+ ]);
385
+
386
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
387
+
388
+ // `B` sorts before `a` by code unit, never by locale.
389
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['a', 'b', 'z']);
390
+ assert.deepEqual(groups[1].docs.map((doc) => doc.name), ['B', 'a', 'older']);
391
+ });
392
+ });
393
+
394
+ // S13: the machine holds ~20 worktrees, so computing a merge-base diff for every
395
+ // one of them on every refresh is wasted work. The scope is an INPUT: `roots` is
396
+ // the set of worktrees the session registered, and only what Git lists AND that set
397
+ // names is grouped — a worktree left out is never asked a single git question, which
398
+ // is what these tests read from the fake's argv log. An absent `roots` is the old
399
+ // behaviour, unchanged: every OWNED worktree.
400
+ describe('S13: `roots` scopes the scan to the worktrees a session registered', () => {
401
+ test('without `roots` every owned worktree is grouped, exactly as before', async () => {
402
+ const box = machine('scope-absent');
403
+ const oddA = box.worktree('odd-a');
404
+ const oddB = box.worktree('odd-b');
405
+ writeDoc(box.mainRoot, 'main-doc', { at: '2020-01-01T00:00:00.000Z' });
406
+ writeDoc(oddA, 'a-doc', { at: '2020-01-01T00:00:00.000Z' });
407
+ writeDoc(oddB, 'b-doc', { at: '2020-01-01T00:00:00.000Z' });
408
+ const git = fakeGit({ paths: [box.mainRoot, oddA, oddB] });
409
+ git.delta(oddA, [['A', 'odd/tasks/a-doc.md']]);
410
+ git.delta(oddB, [['A', 'odd/tasks/b-doc.md']]);
411
+
412
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
413
+
414
+ assert.deepEqual(groups.map((group) => group.checkoutName), ['main', 'odd-a', 'odd-b']);
415
+ assert.deepEqual(contract(groups[1]).docs, [{ name: 'a-doc', status: 'added' }]);
416
+ assert.deepEqual(contract(groups[2]).docs, [{ name: 'b-doc', status: 'added' }]);
417
+ assert.deepEqual(
418
+ git.verbs().filter((call) => call.verb === 'merge-base').map((call) => call.cwd),
419
+ [oddA, oddB],
420
+ 'no scope means every owned worktree is compared',
421
+ );
422
+ });
423
+
424
+ test('`roots: undefined` and `roots: null` are the same as no scope at all', async () => {
425
+ const box = machine('scope-null');
426
+ const worktree = box.worktree('odd-rail');
427
+ writeDoc(box.mainRoot, 'main-doc', { at: '2020-01-01T00:00:00.000Z' });
428
+ writeDoc(worktree, 'wt-doc', { at: '2020-02-01T00:00:00.000Z' });
429
+ const answers = [];
430
+ for (const roots of [undefined, null]) {
431
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
432
+ git.delta(worktree, [['A', 'odd/tasks/wt-doc.md']]);
433
+ answers.push({
434
+ groups: await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync, roots }),
435
+ compared: git.verbs().filter((call) => call.verb === 'merge-base').map((call) => call.cwd),
436
+ });
437
+ }
438
+
439
+ assert.deepEqual(answers[0].groups, answers[1].groups);
440
+ assert.deepEqual(answers[0].groups.map((group) => group.checkoutName), ['main', 'odd-rail']);
441
+ assert.deepEqual(answers[1].compared, [worktree]);
442
+ });
443
+
444
+ test('only the listed worktrees are grouped, and an excluded one costs no git read', async () => {
445
+ const box = machine('scope-selects');
446
+ const oddA = box.worktree('odd-a');
447
+ const oddB = box.worktree('odd-b');
448
+ const oddC = box.worktree('odd-c');
449
+ writeDoc(box.mainRoot, 'main-doc', { at: '2020-01-01T00:00:00.000Z' });
450
+ writeDoc(oddA, 'a-doc', { at: '2020-01-01T00:00:00.000Z' });
451
+ writeDoc(oddB, 'b-doc', { at: '2020-01-01T00:00:00.000Z' });
452
+ writeDoc(oddC, 'c-doc', { at: '2020-01-01T00:00:00.000Z' });
453
+ const git = fakeGit({ paths: [box.mainRoot, oddA, oddB, oddC] });
454
+ git.delta(oddA, [['A', 'odd/tasks/a-doc.md']]);
455
+ git.delta(oddB, [['A', 'odd/tasks/b-doc.md']]);
456
+ git.delta(oddC, [['A', 'odd/tasks/c-doc.md']]);
457
+
458
+ const groups = await groupOddDocs({
459
+ repoRoot: box.mainRoot,
460
+ runAsync: git.runAsync,
461
+ roots: [oddA, oddC],
462
+ });
463
+
464
+ assert.deepEqual(groups.map((group) => group.checkoutName), ['main', 'odd-a', 'odd-c']);
465
+ assert.deepEqual(contract(groups[1]).docs, [{ name: 'a-doc', status: 'added' }]);
466
+ assert.deepEqual(contract(groups[2]).docs, [{ name: 'c-doc', status: 'added' }]);
467
+ assert.deepEqual(
468
+ git.verbs().filter((call) => call.cwd === oddB).map((call) => call.args),
469
+ [],
470
+ 'an unregistered worktree is never asked anything, not even its own delta',
471
+ );
472
+ assert.deepEqual(
473
+ git.verbs().filter((call) => call.cwd === oddA).map((call) => call.verb),
474
+ ['merge-base', 'diff', 'status'],
475
+ );
476
+ assert.deepEqual(
477
+ git.verbs().filter((call) => call.cwd === oddC).map((call) => call.verb),
478
+ ['merge-base', 'diff', 'status'],
479
+ );
480
+ });
481
+
482
+ test('a registered worktree outside <repo>-worktrees is included: registration is the authority', async () => {
483
+ const box = machine('scope-outside');
484
+ const outside = box.outside('elsewhere');
485
+ writeDoc(box.mainRoot, 'main-doc', { at: '2020-01-01T00:00:00.000Z' });
486
+ writeDoc(outside, 'foreign-doc', { at: '2020-02-01T00:00:00.000Z' });
487
+ const git = fakeGit({ paths: [box.mainRoot, outside] });
488
+ git.delta(outside, [['A', 'odd/tasks/foreign-doc.md']]);
489
+
490
+ // A trailing separator is the same root: paths compare normalized.
491
+ const groups = await groupOddDocs({
492
+ repoRoot: box.mainRoot,
493
+ runAsync: git.runAsync,
494
+ roots: [`${outside}${path.sep}`],
495
+ });
496
+
497
+ assert.deepEqual(groups.map((group) => [group.checkoutName, group.isMain]), [
498
+ ['main', true],
499
+ ['elsewhere', false],
500
+ ]);
501
+ assert.deepEqual(contract(groups[1]).docs, [{ name: 'foreign-doc', status: 'added' }]);
502
+ });
503
+
504
+ test('a registered root Git does not list contributes nothing and never throws', async () => {
505
+ const box = machine('scope-stale');
506
+ const live = box.worktree('odd-live');
507
+ const stale = box.worktree('odd-stale');
508
+ const clone = box.outside('another-clone');
509
+ writeDoc(box.mainRoot, 'main-doc', { at: '2020-01-01T00:00:00.000Z' });
510
+ writeDoc(live, 'live-doc', { at: '2020-02-01T00:00:00.000Z' });
511
+ fs.mkdirSync(clone, { recursive: true });
512
+ const git = fakeGit({ paths: [box.mainRoot, live] });
513
+ git.delta(live, [['A', 'odd/tasks/live-doc.md']]);
514
+
515
+ const groups = await groupOddDocs({
516
+ repoRoot: box.mainRoot,
517
+ runAsync: git.runAsync,
518
+ roots: [stale, clone, live],
519
+ });
520
+
521
+ assert.deepEqual(groups.map((group) => group.checkoutName), ['main', 'odd-live']);
522
+ assert.deepEqual(contract(groups[1]).docs, [{ name: 'live-doc', status: 'added' }]);
523
+ for (const absent of [stale, clone]) {
524
+ assert.deepEqual(git.verbs().filter((call) => call.cwd === absent), [], 'a stale root is never read');
525
+ }
526
+ });
527
+
528
+ test('an empty `roots` is main only, and no worktree is read', async () => {
529
+ const box = machine('scope-empty');
530
+ const worktree = box.worktree('odd-rail');
531
+ const doc = writeDoc(box.mainRoot, 'main-doc', { at: '2020-01-01T00:00:00.000Z' });
532
+ writeDoc(worktree, 'wt-doc', { at: '2020-02-01T00:00:00.000Z' });
533
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
534
+ git.delta(worktree, [['A', 'odd/tasks/wt-doc.md']]);
535
+
536
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync, roots: [] });
537
+
538
+ assert.deepEqual(groups, [
539
+ {
540
+ checkoutName: 'main',
541
+ checkoutRoot: box.mainRoot,
542
+ isMain: true,
543
+ docs: [{ name: 'main-doc', docPath: doc, mtimeMs: mtimeOf(doc) }],
544
+ },
545
+ ]);
546
+ assert.equal(git.calls.length, 1, 'the one listing read, and no worktree read at all');
547
+ });
548
+
549
+ test('a `roots` that is present but not a list fails closed to main only', async () => {
550
+ const box = machine('scope-malformed');
551
+ const worktree = box.worktree('odd-rail');
552
+ const doc = writeDoc(box.mainRoot, 'main-doc', { at: '2020-01-01T00:00:00.000Z' });
553
+ writeDoc(worktree, 'wt-doc', { at: '2020-02-01T00:00:00.000Z' });
554
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
555
+ git.delta(worktree, [['A', 'odd/tasks/wt-doc.md']]);
556
+
557
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync, roots: 'odd-rail' });
558
+
559
+ assert.deepEqual(groups.map((group) => group.checkoutName), ['main']);
560
+ assert.equal(groups[0].docs[0].docPath, doc);
561
+ assert.equal(git.calls.length, 1, 'the one listing read, and no worktree read at all');
562
+ });
563
+
564
+ test('main is always first and is never duplicated when it is itself in `roots`', async () => {
565
+ const box = machine('scope-main');
566
+ const worktree = box.worktree('odd-rail');
567
+ writeDoc(box.mainRoot, 'main-doc', { at: '2020-01-01T00:00:00.000Z' });
568
+ writeDoc(worktree, 'wt-doc', { at: '2020-02-01T00:00:00.000Z' });
569
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
570
+ git.delta(worktree, [['A', 'odd/tasks/wt-doc.md']]);
571
+
572
+ const groups = await groupOddDocs({
573
+ repoRoot: box.mainRoot,
574
+ runAsync: git.runAsync,
575
+ roots: [`${box.mainRoot}${path.sep}`, worktree],
576
+ });
577
+
578
+ assert.deepEqual(groups.map((group) => [group.checkoutName, group.isMain]), [
579
+ ['main', true],
580
+ ['odd-rail', false],
581
+ ]);
582
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['main-doc']);
583
+ });
584
+ });
585
+
586
+ // S11: `a` in the panel means EVERY worktree of this clone, "registered or not".
587
+ // The default enumeration above is the owned-checkout rule, which is the right
588
+ // default for every existing caller; `all: true` is the explicit, ADDITIVE
589
+ // switch that drops that filter and groups every worktree Git lists except main.
590
+ // It wins over `roots` when a caller passes both, and it changes nothing about
591
+ // failure: an unreadable checkout still contributes no document and never throws.
592
+ describe('S11: `all` groups every worktree Git lists, not just the owned ones', () => {
593
+ test('`all: true` groups a worktree outside <repo>-worktrees, which the default excludes', async () => {
594
+ const box = machine('all-outside');
595
+ const outside = box.outside('elsewhere');
596
+ writeDoc(box.mainRoot, 'main-doc', { at: '2020-01-01T00:00:00.000Z' });
597
+ writeDoc(outside, 'foreign-doc', { at: '2020-02-01T00:00:00.000Z' });
598
+
599
+ // The premise: the unchanged default really omits it, and asks it nothing.
600
+ const byDefault = fakeGit({ paths: [box.mainRoot, outside] });
601
+ byDefault.delta(outside, [['A', 'odd/tasks/foreign-doc.md']]);
602
+ const scoped = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: byDefault.runAsync });
603
+ assert.deepEqual(scoped.map((group) => group.checkoutName), ['main']);
604
+ assert.deepEqual(byDefault.verbs().filter((call) => call.cwd === outside), [],
605
+ 'the owned-checkout rule never asks a foreign worktree anything');
606
+
607
+ const git = fakeGit({ paths: [box.mainRoot, outside] });
608
+ git.delta(outside, [['A', 'odd/tasks/foreign-doc.md']]);
609
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync, all: true });
610
+
611
+ assert.deepEqual(groups.map((group) => [group.checkoutName, group.isMain]), [
612
+ ['main', true],
613
+ ['elsewhere', false],
614
+ ]);
615
+ assert.deepEqual(contract(groups[1]).docs, [{ name: 'foreign-doc', status: 'added' }]);
616
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['main-doc'], 'main stays first and whole');
617
+ });
618
+
619
+ test('`all` wins over `roots` when a caller passes both', async () => {
620
+ const box = machine('all-wins');
621
+ const owned = box.worktree('odd-rail');
622
+ const outside = box.outside('elsewhere');
623
+ writeDoc(box.mainRoot, 'main-doc', { at: '2020-01-01T00:00:00.000Z' });
624
+ writeDoc(owned, 'owned-doc', { at: '2020-01-01T00:00:00.000Z' });
625
+ writeDoc(outside, 'foreign-doc', { at: '2020-02-01T00:00:00.000Z' });
626
+ const git = fakeGit({ paths: [box.mainRoot, owned, outside] });
627
+ git.delta(owned, [['A', 'odd/tasks/owned-doc.md']]);
628
+ git.delta(outside, [['A', 'odd/tasks/foreign-doc.md']]);
629
+
630
+ const groups = await groupOddDocs({
631
+ repoRoot: box.mainRoot,
632
+ runAsync: git.runAsync,
633
+ roots: [owned],
634
+ all: true,
635
+ });
636
+
637
+ assert.deepEqual(groups.map((group) => group.checkoutName), ['main', 'elsewhere', 'odd-rail']);
638
+ assert.deepEqual(contract(groups[1]).docs, [{ name: 'foreign-doc', status: 'added' }]);
639
+ assert.deepEqual(contract(groups[2]).docs, [{ name: 'owned-doc', status: 'added' }]);
640
+ });
641
+
642
+ test('a stale worktree Git still lists is skipped under `all` and never throws', async () => {
643
+ const box = machine('all-stale');
644
+ const gone = box.outside('gone');
645
+ const live = box.outside('live');
646
+ writeDoc(box.mainRoot, 'main-doc', { at: '2020-01-01T00:00:00.000Z' });
647
+ writeDoc(live, 'live-doc', { at: '2020-02-01T00:00:00.000Z' });
648
+ const git = fakeGit({ paths: [box.mainRoot, gone, live] });
649
+ git.delta(live, [['A', 'odd/tasks/live-doc.md']]);
650
+
651
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync, all: true });
652
+
653
+ assert.deepEqual(groups.map((group) => group.checkoutName), ['main', 'gone', 'live']);
654
+ assert.deepEqual(groups[1].docs, [], 'an unreadable checkout contributes no document');
655
+ assert.deepEqual(contract(groups[2]).docs, [{ name: 'live-doc', status: 'added' }]);
656
+ assert.deepEqual(git.verbs().filter((call) => call.cwd === gone), [],
657
+ 'and it is never asked a git question');
658
+ });
659
+
660
+ test('`roots` absent and `all` absent is exactly today’s owned-worktree enumeration', async () => {
661
+ const box = machine('all-absent');
662
+ const owned = box.worktree('odd-rail');
663
+ const outside = box.outside('elsewhere');
664
+ writeDoc(box.mainRoot, 'main-doc', { at: '2020-01-01T00:00:00.000Z' });
665
+ writeDoc(owned, 'owned-doc', { at: '2020-01-01T00:00:00.000Z' });
666
+ writeDoc(outside, 'foreign-doc', { at: '2020-02-01T00:00:00.000Z' });
667
+ const shapes = [undefined, false];
668
+ for (const all of shapes) {
669
+ const git = fakeGit({ paths: [box.mainRoot, owned, outside] });
670
+ git.delta(owned, [['A', 'odd/tasks/owned-doc.md']]);
671
+ git.delta(outside, [['A', 'odd/tasks/foreign-doc.md']]);
672
+
673
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync, all });
674
+
675
+ assert.deepEqual(groups.map((group) => group.checkoutName), ['main', 'odd-rail'],
676
+ `all: ${all} keeps the default scope`);
677
+ assert.deepEqual(git.verbs().filter((call) => call.cwd === outside), [],
678
+ 'the foreign worktree is still never read');
679
+ }
680
+ });
681
+ });
682
+
683
+ describe('fail closed, never throw', () => {
684
+ test('an empty delta keeps the group with no documents: the caller decides not to draw it', async () => {
685
+ const box = machine('empty-delta');
686
+ const worktree = box.worktree('odd-rail');
687
+ writeDoc(box.mainRoot, 'alpha', { body: 'shared\n' });
688
+ writeDoc(worktree, 'alpha', { body: 'shared\n' });
689
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
690
+
691
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
692
+
693
+ assert.equal(groups.length, 2);
694
+ assert.deepEqual(groups[1], {
695
+ checkoutName: 'odd-rail',
696
+ checkoutRoot: worktree,
697
+ isMain: false,
698
+ docs: [],
699
+ });
700
+ });
701
+
702
+ test('a worktree without odd/tasks keeps its group and contributes nothing', async () => {
703
+ const box = machine('absent-dir');
704
+ const worktree = box.worktree('odd-rail');
705
+ fs.mkdirSync(worktree, { recursive: true });
706
+ writeDoc(box.mainRoot, 'mine', { at: '2020-01-01T00:00:00.000Z' });
707
+ const git = fakeGit({ paths: [box.mainRoot, worktree] });
708
+
709
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
710
+
711
+ assert.deepEqual(groups.map((group) => group.checkoutName), ['main', 'odd-rail']);
712
+ assert.deepEqual(groups[1].docs, []);
713
+ assert.equal(git.calls.length, 1, 'no document, no git read');
714
+ });
715
+
716
+ test('an unreadable checkout keeps its group and contributes nothing', async () => {
717
+ const box = machine('unreadable');
718
+ const gone = box.worktree('odd-gone');
719
+ const here = box.worktree('odd-here');
720
+ writeDoc(here, 'here', { at: '2020-01-01T00:00:00.000Z' });
721
+ const git = fakeGit({ paths: [box.mainRoot, gone, here] });
722
+ git.delta(here, [['A', 'odd/tasks/here.md']]);
723
+
724
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
725
+
726
+ assert.deepEqual(groups.map((group) => group.checkoutName), ['main', 'odd-gone', 'odd-here']);
727
+ assert.deepEqual(groups[1].docs, []);
728
+ assert.deepEqual(contract(groups[2]).docs, [{ name: 'here', status: 'added' }]);
729
+ assert.equal(
730
+ git.verbs().filter((call) => call.cwd === gone).length,
731
+ 0,
732
+ 'a checkout with no documents to compare is never asked about',
733
+ );
734
+ });
735
+
736
+ test('a merge base that cannot be resolved empties the worktree group and never throws', async () => {
737
+ const box = machine('no-base');
738
+ const worktree = box.worktree('odd-rail');
739
+ writeDoc(box.mainRoot, 'old', { at: '2020-01-01T00:00:00.000Z' });
740
+ writeDoc(worktree, 'fresh', { at: '2020-02-01T00:00:00.000Z' });
741
+ const git = fakeGit({ paths: [box.mainRoot, worktree], fail: ['merge-base'] });
742
+
743
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
744
+
745
+ assert.deepEqual(groups[1].docs, []);
746
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['old']);
747
+ });
748
+
749
+ test('a failing diff read or status read empties the worktree group and never throws', async () => {
750
+ for (const verb of ['diff', 'status']) {
751
+ const box = machine(`fail-${verb}`);
752
+ const worktree = box.worktree('odd-rail');
753
+ writeDoc(box.mainRoot, 'shared', { body: 'main\n' });
754
+ writeDoc(worktree, 'shared', { body: 'worktree\n', at: '2020-02-01T00:00:00.000Z' });
755
+ const git = fakeGit({ paths: [box.mainRoot, worktree], fail: [verb] });
756
+ git.delta(worktree, [['M', 'odd/tasks/shared.md']]);
757
+ git.status(worktree, [' M odd/tasks/shared.md']);
758
+
759
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
760
+
761
+ assert.deepEqual(groups[1].docs, [], `${verb} failed, so nothing is claimed`);
762
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['shared']);
763
+ }
764
+ });
765
+
766
+ test('a failing worktree listing leaves main as the only group', async () => {
767
+ const box = machine('list-fails');
768
+ const worktree = box.worktree('odd-rail');
769
+ writeDoc(box.mainRoot, 'mine', { at: '2020-01-01T00:00:00.000Z' });
770
+ writeDoc(worktree, 'theirs', { at: '2020-02-01T00:00:00.000Z' });
771
+ const git = fakeGit({ paths: [box.mainRoot, worktree], fail: ['worktree'] });
772
+
773
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
774
+
775
+ assert.deepEqual(groups.map((group) => group.checkoutName), ['main']);
776
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['mine']);
777
+ });
778
+
779
+ test('S13: `failClosed` answers no model where the default answers a synthetic main', async () => {
780
+ const box = machine('list-fails-closed');
781
+ const worktree = box.worktree('odd-rail');
782
+ writeDoc(box.mainRoot, 'mine', { at: '2020-01-01T00:00:00.000Z' });
783
+ writeDoc(worktree, 'theirs', { at: '2020-02-01T00:00:00.000Z' });
784
+ const git = fakeGit({ paths: [box.mainRoot, worktree], fail: ['worktree'] });
785
+
786
+ // The default is the documented degradation, unchanged: the caller's own
787
+ // root is the only checkout there is, reported as main.
788
+ const degraded = await groupOddDocs({ repoRoot: worktree, runAsync: git.runAsync });
789
+ assert.deepEqual(degraded.map((group) => group.checkoutName), ['main']);
790
+ assert.deepEqual(degraded[0].docs.map((doc) => doc.name), ['theirs'],
791
+ 'the default still answers the session checkout as main');
792
+
793
+ // `failClosed` is S13's strict reading: a listing that cannot be read is no
794
+ // model, never a linked checkout dressed as main.
795
+ assert.equal(await groupOddDocs({ repoRoot: worktree, runAsync: git.runAsync, failClosed: true }), null);
796
+ // False (and absent) are the default, byte for byte.
797
+ assert.deepEqual(await groupOddDocs({ repoRoot: worktree, runAsync: git.runAsync, failClosed: false }), degraded);
798
+ });
799
+
800
+ test('a git seam that throws is the same silence', async () => {
801
+ const box = machine('git-throws');
802
+ writeDoc(box.mainRoot, 'mine', { at: '2020-01-01T00:00:00.000Z' });
803
+ const git = fakeGit({ paths: [box.mainRoot], throws: true });
804
+
805
+ const groups = await groupOddDocs({ repoRoot: box.mainRoot, runAsync: git.runAsync });
806
+
807
+ assert.deepEqual(groups.map((group) => group.checkoutName), ['main']);
808
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['mine']);
809
+ });
810
+
811
+ test('a root that is not a usable path answers no groups and reads no git', async () => {
812
+ const git = fakeGit({ paths: [] });
813
+
814
+ assert.deepEqual(await groupOddDocs({ repoRoot: '', runAsync: git.runAsync }), []);
815
+ assert.deepEqual(await groupOddDocs({ repoRoot: 42, runAsync: git.runAsync }), []);
816
+ assert.deepEqual(await groupOddDocs({}), []);
817
+ assert.equal(git.calls.length, 0, 'no git read without a root to read');
818
+ });
819
+ });
820
+
821
+ // ---------------------------------------------------------------------------
822
+ // T17/F1 (L11): MAIN is the first listed entry that is a WORKING TREE. Git's
823
+ // listing is not "the main checkout first" for every repository shape: a
824
+ // repository created with `git init --separate-git-dir` has its METADATA directory
825
+ // listed where the main checkout would be (Git derives that record from the common
826
+ // dir), so electing `entries[0]` blindly made main a directory with no `odd/tasks`
827
+ // — and bare `/odd` reported "No odd documents under odd/tasks/ in this checkout."
828
+ // for a checkout that owns a document. These tests build the real repositories
829
+ // through the real CLI and read them with the module's own git seam, because the
830
+ // shape under test is Git's, not the parser's.
831
+ //
832
+ // A standard repository keeps today's answer byte for byte: the pinned standard
833
+ // case below and every fake-seam case above it.
834
+ describe('T17/F1: main is elected from the listing’s attributes, never its position alone', () => {
835
+ const { spawnSync } = require('node:child_process');
836
+
837
+ function runGit(args, cwd) {
838
+ const answer = spawnSync('git', args, { cwd, encoding: 'utf8' });
839
+ if (answer.status !== 0) throw new Error(`git ${args.join(' ')} failed: ${answer.stderr}`);
840
+ return answer.stdout;
841
+ }
842
+
843
+ // The module's seam, run against the real binary: the same contract as the fake
844
+ // (`{status, stdout, stderr, error}`), so the reads under test are Git's own.
845
+ function realSeam() {
846
+ return (bin, argv, cwd) => Promise.resolve().then(() => {
847
+ const answer = spawnSync(bin, argv, { cwd, encoding: 'utf8' });
848
+ return {
849
+ status: answer.status,
850
+ stdout: answer.stdout === null ? '' : answer.stdout,
851
+ stderr: answer.stderr === null ? '' : answer.stderr,
852
+ error: answer.error,
853
+ };
854
+ });
855
+ }
856
+
857
+ function configure(root) {
858
+ runGit(['config', 'user.email', 'test@example.test'], root);
859
+ runGit(['config', 'user.name', 'Test'], root);
860
+ }
861
+
862
+ function commitAll(root, message) {
863
+ runGit(['add', '-A'], root);
864
+ runGit(['commit', '-m', message], root);
865
+ }
866
+
867
+ // A real repository whose git dir is NOT the checkout: `git init
868
+ // --separate-git-dir <meta> <checkout>` leaves `<checkout>/.git` a link file and
869
+ // makes Git print `<meta>` as the worktree record.
870
+ function separateGitDirRepo(tag) {
871
+ const base = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), `odd-group-sep-${tag}-`)));
872
+ roots.push(base);
873
+ const meta = path.join(base, 'git-meta');
874
+ const checkout = path.join(base, 'checkout');
875
+ runGit(['init', '--separate-git-dir', meta, checkout], base);
876
+ configure(checkout);
877
+ writeDoc(checkout, 'main-one', { at: '2020-01-01T00:00:00.000Z' });
878
+ commitAll(checkout, 'init');
879
+ return { base, meta, checkout };
880
+ }
881
+
882
+ test('F1: a separate git dir keeps the working tree as main, with its documents', async (t) => {
883
+ const repo = separateGitDirRepo('listing');
884
+ const listed = runGit(['worktree', 'list', '--porcelain'], repo.checkout);
885
+ t.diagnostic(`git's own record: ${JSON.stringify(listed)}`);
886
+
887
+ const groups = await groupOddDocs({ repoRoot: repo.checkout, runAsync: realSeam(), failClosed: true });
888
+
889
+ assert.ok(Array.isArray(groups), 'the listing answered, so there is a model');
890
+ assert.equal(groups.length, 1, 'main is the only checkout there is');
891
+ assert.equal(groups[0].checkoutName, 'main');
892
+ assert.equal(groups[0].checkoutRoot, repo.checkout,
893
+ 'main is the WORKING TREE the listing could not name')
894
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['main-one'],
895
+ 'and its own document is listed');
896
+ assert.deepEqual(Object.keys(groups[0].docs[0]).sort(), ['docPath', 'mtimeMs', 'name'],
897
+ 'the row shape is unchanged (no invented status for main)');
898
+ });
899
+
900
+ // T18/F1 (L11): the METADATA record is never a checkout. A separate git dir lists
901
+ // that directory in the record Git would print for the working tree, and a session
902
+ // standing in a LINKED checkout of such a repository used to elect it: bare /odd
903
+ // answered "no documents" for a repository whose main checkout owns one, and a
904
+ // document written into the metadata tree appeared as main's instead of the real
905
+ // one. Main is the working tree that OWNS the common dir.
906
+ test('F1: a linked session of a separate git dir elects the real working tree, never the metadata tree', async (t) => {
907
+ const repo = separateGitDirRepo('linked-session');
908
+ const owned = path.join(repo.base, 'checkout-worktrees', 'linked-one');
909
+ runGit(['worktree', 'add', '-b', 'feat/linked-one', owned], repo.checkout);
910
+ writeDoc(owned, 'private', { at: '2020-03-01T00:00:00.000Z' });
911
+ // The metadata tree is not a checkout, so a document under its own `odd/tasks` is
912
+ // nobody's document — the case that made it "appear as main".
913
+ writeDoc(repo.meta, 'metadata-only', { at: '2020-05-01T00:00:00.000Z' });
914
+ const listed = runGit(['worktree', 'list', '--porcelain'], owned);
915
+ t.diagnostic(`git's own record: ${JSON.stringify(listed)}`);
916
+
917
+ const groups = await groupOddDocs({ repoRoot: owned, roots: [], runAsync: realSeam(), failClosed: true });
918
+
919
+ assert.ok(Array.isArray(groups), 'the listing answered, so there is a model');
920
+ assert.equal(groups.length, 1, 'nothing is registered, so main is the only group');
921
+ assert.equal(groups[0].checkoutRoot, repo.checkout,
922
+ 'main is the working tree that owns the common dir, not the metadata record');
923
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['main-one'],
924
+ 'and the metadata tree is not a checkout: its document is not main\u2019s');
925
+ });
926
+
927
+ test('F1 preserve: a standard repository elects the same main it always did', async () => {
928
+ const base = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'odd-group-standard-')));
929
+ roots.push(base);
930
+ const root = path.join(base, 'repo');
931
+ runGit(['init', '-b', 'main', root], base);
932
+ configure(root);
933
+ writeDoc(root, 'feature', { at: '2020-01-01T00:00:00.000Z' });
934
+ commitAll(root, 'init');
935
+ const owned = path.join(base, 'repo-worktrees', 'odd-rail');
936
+ runGit(['worktree', 'add', '-b', 'feat/odd-rail', owned, 'main'], root);
937
+ writeDoc(owned, 'delta', { at: '2020-02-01T00:00:00.000Z' });
938
+ runGit(['add', '-A'], owned);
939
+ runGit(['commit', '-m', 'delta'], owned);
940
+
941
+ const groups = await groupOddDocs({ repoRoot: root, runAsync: realSeam(), failClosed: true });
942
+
943
+ assert.deepEqual(groups.map((group) => [group.checkoutName, group.isMain, group.checkoutRoot]), [
944
+ ['main', true, root],
945
+ ['odd-rail', false, owned],
946
+ ], 'the main record is still main, and the worktree still follows it');
947
+ assert.deepEqual(groups[0].docs.map((doc) => doc.name), ['feature']);
948
+ assert.deepEqual(groups[1].docs.map((doc) => [doc.name, doc.status]), [['delta', 'added']]);
949
+ });
950
+ });