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,184 @@
1
+ 'use strict';
2
+ // T4 (odd-rail-section S9, S12, S13 and S5's tie rule): lib/odd-rail-model.js, the
3
+ // pure row model the rail's section draws from.
4
+ //
5
+ // What is asserted here, through the module's ONE public function:
6
+ // - ONE row per ODD NAME, never one per checkout (S12): a name present in main and
7
+ // in a worktree is one entry, and the elected copy is the one the click opens.
8
+ // - The election is Option A, the user's decision: the MOST RECENT `mtimeMs` wins;
9
+ // on an exact tie MAIN wins; between two worktrees the FIRST GROUP in order keeps
10
+ // it — so the answer never depends on the order a caller happened to sort with.
11
+ // - Order (S9): the names `oddSession.touched()` names first, by the touch's `at`
12
+ // most recent first, then everything else by `mtimeMs` descending, then by name
13
+ // in code units (never `localeCompare`, so every host agrees).
14
+ // - Attribution only ORDERS: a touched name no group lists produces no row, and a
15
+ // touch never adds a row, removes one or duplicates one.
16
+ // - A worktree group with `docs: []` contributes nothing (S13), and a malformed
17
+ // group, document or touch is skipped instead of thrown on.
18
+ // - Pure: no fs, no git, no clock, and the inputs are never mutated.
19
+
20
+ const { test, describe } = require('node:test');
21
+ const assert = require('node:assert/strict');
22
+ const fs = require('node:fs');
23
+ const path = require('node:path');
24
+
25
+ const { railRows } = require('../lib/odd-rail-model.js');
26
+
27
+ const MAIN = 'main';
28
+ const DOC_DIR = path.resolve('/odd-rail-model/checkout/odd/tasks');
29
+ const WT_DIR = path.resolve('/odd-rail-model/checkout-worktrees/wt-a');
30
+ const WT_B = path.resolve('/odd-rail-model/checkout-worktrees/wt-b');
31
+
32
+ // The three fields a row carries besides its own `mtimeMs`, so an assertion about
33
+ // WHICH copy was elected reads as one comparison.
34
+ const picked = (rows) => rows.map((row) => [row.name, row.checkoutName, row.docPath]);
35
+
36
+ function doc(name, root, mtimeMs) {
37
+ return { name, docPath: path.join(root, 'odd', 'tasks', `${name}.md`), mtimeMs };
38
+ }
39
+
40
+ // The main group always carries every document; a worktree carries only its delta.
41
+ function mainGroup(names, times = {}) {
42
+ return {
43
+ checkoutName: MAIN,
44
+ checkoutRoot: path.resolve('/odd-rail-model/checkout'),
45
+ isMain: true,
46
+ docs: names.map((name) => doc(name, path.resolve('/odd-rail-model/checkout'), times[name] ?? 0)),
47
+ };
48
+ }
49
+
50
+ function worktreeGroup(name, root, docs) {
51
+ return { checkoutName: name, checkoutRoot: root, isMain: false, docs };
52
+ }
53
+
54
+ describe('one row per ODD name (S12)', () => {
55
+ test('a name in main and in a worktree is ONE row, and the newest copy is elected', () => {
56
+ const groups = [
57
+ mainGroup(['alpha', 'beta'], { alpha: 1000, beta: 2000 }),
58
+ worktreeGroup('wt-a', WT_DIR, [doc('alpha', WT_DIR, 3000)]),
59
+ ];
60
+ const rows = railRows({ groups, touched: [] });
61
+ assert.deepEqual(
62
+ picked(rows),
63
+ [
64
+ ['alpha', 'wt-a', path.join(WT_DIR, 'odd', 'tasks', 'alpha.md')],
65
+ ['beta', MAIN, path.join('/odd-rail-model/checkout/odd/tasks', 'beta.md')],
66
+ ],
67
+ 'one row per name, the elected copy named by `checkoutName`',
68
+ );
69
+ // Order is recency: the elected alpha (3000) is newer than beta (2000).
70
+ assert.equal(rows[0].mtimeMs, 3000);
71
+ assert.equal(rows[1].mtimeMs, 2000);
72
+ });
73
+
74
+ test('an exact tie between main and a worktree keeps main (S5)', () => {
75
+ const groups = [
76
+ mainGroup(['alpha'], { alpha: 5000 }),
77
+ worktreeGroup('wt-a', WT_DIR, [doc('alpha', WT_DIR, 5000)]),
78
+ ];
79
+ assert.deepEqual(picked(railRows({ groups, touched: [] })), [
80
+ ['alpha', MAIN, path.join('/odd-rail-model/checkout/odd/tasks', 'alpha.md')],
81
+ ]);
82
+ });
83
+
84
+ test('an exact tie between two worktrees keeps the FIRST group in order', () => {
85
+ const groups = [
86
+ mainGroup([], {}),
87
+ worktreeGroup('wt-a', WT_DIR, [doc('alpha', WT_DIR, 7000)]),
88
+ worktreeGroup('wt-b', WT_B, [doc('alpha', WT_B, 7000)]),
89
+ ];
90
+ assert.deepEqual(picked(railRows({ groups, touched: [] })), [
91
+ ['alpha', 'wt-a', path.join(WT_DIR, 'odd', 'tasks', 'alpha.md')],
92
+ ]);
93
+ });
94
+
95
+ test('a newer worktree copy beats an older main copy, and main is never an override', () => {
96
+ const groups = [
97
+ mainGroup(['alpha'], { alpha: 10 }),
98
+ worktreeGroup('wt-a', WT_DIR, [doc('alpha', WT_DIR, 20)]),
99
+ ];
100
+ assert.deepEqual(picked(railRows({ groups, touched: [] })), [
101
+ ['alpha', 'wt-a', path.join(WT_DIR, 'odd', 'tasks', 'alpha.md')],
102
+ ]);
103
+ });
104
+
105
+ test('a worktree group with no delta contributes nothing (S13)', () => {
106
+ const groups = [
107
+ mainGroup(['alpha', 'beta'], { alpha: 200, beta: 100 }),
108
+ worktreeGroup('wt-a', WT_DIR, []),
109
+ ];
110
+ const rows = railRows({ groups, touched: [] });
111
+ assert.deepEqual(rows.map((row) => [row.name, row.checkoutName]), [['alpha', MAIN], ['beta', MAIN]],
112
+ 'main only, and no row for the empty worktree');
113
+ });
114
+ });
115
+
116
+ describe('order: attribution first, then recency, then name (S9)', () => {
117
+ test('a touched name comes first, ordered by its touch, most recent first', () => {
118
+ const groups = [mainGroup(['alpha', 'beta', 'gamma'], { alpha: 900, beta: 800, gamma: 700 })];
119
+ const touched = [
120
+ { path: path.join(DOC_DIR, 'gamma.md'), name: 'gamma', at: 20 },
121
+ { path: path.join(DOC_DIR, 'beta.md'), name: 'beta', at: 10 },
122
+ ];
123
+ assert.deepEqual(railRows({ groups, touched }).map((row) => row.name), ['gamma', 'beta', 'alpha'],
124
+ 'the MOST recent touch is first, then the older one, then plain recency');
125
+ });
126
+
127
+ test('a touched name no group lists produces no row, and never a duplicate (S9)', () => {
128
+ const groups = [mainGroup(['alpha'], { alpha: 1 })];
129
+ const touched = [{ path: path.join(DOC_DIR, 'ghost.md'), name: 'ghost', at: 99 }];
130
+ assert.deepEqual(railRows({ groups, touched }).map((row) => row.name), ['alpha'],
131
+ 'attribution only orders: it adds nothing and removes nothing');
132
+ });
133
+
134
+ test('equal mtimes order by name in code units, never locale', () => {
135
+ const groups = [mainGroup(['a', 'B', 'b'], { a: 1, B: 1, b: 1 })];
136
+ assert.deepEqual(railRows({ groups, touched: [] }).map((row) => row.name), ['B', 'a', 'b'],
137
+ 'code units put B (66) before a (97); localeCompare would not');
138
+ });
139
+
140
+ test('an equal touch instant keeps the record’s own order', () => {
141
+ const groups = [mainGroup(['alpha', 'beta'], { alpha: 10, beta: 20 })];
142
+ const touched = [
143
+ { path: path.join(DOC_DIR, 'beta.md'), name: 'beta', at: 50 },
144
+ { path: path.join(DOC_DIR, 'alpha.md'), name: 'alpha', at: 50 },
145
+ ];
146
+ assert.deepEqual(railRows({ groups, touched }).map((row) => row.name), ['beta', 'alpha']);
147
+ });
148
+ });
149
+
150
+ describe('malformed input is skipped, never thrown on', () => {
151
+ test('garbage groups, documents and touches answer an empty or partial list', () => {
152
+ assert.deepEqual(railRows({}), []);
153
+ assert.deepEqual(railRows(), []);
154
+ assert.deepEqual(railRows({ groups: 'nope', touched: 'nope' }), []);
155
+ assert.deepEqual(railRows({ groups: [null, 7, {}, { docs: 'nope' }], touched: [null, 5] }), []);
156
+ const rows = railRows({
157
+ groups: [
158
+ { docs: [{ name: 'alpha', docPath: '/x/alpha.md', mtimeMs: 4 }, null, { name: 5 }, { docPath: '/y/z.md' }] },
159
+ { docs: [{ name: 'beta', docPath: '/x/beta.md', mtimeMs: 'nonsense' }] },
160
+ ],
161
+ touched: [{ name: 'alpha' }, null, { at: 3 }, { name: '', at: 1 }],
162
+ });
163
+ assert.deepEqual(rows.map((row) => [row.name, row.mtimeMs]), [['alpha', 4], ['beta', 0]],
164
+ 'a document with no usable time sorts oldest and reports 0; a touch with no instant is ignored');
165
+ });
166
+
167
+ test('the inputs are never mutated', () => {
168
+ const groups = [mainGroup(['alpha', 'beta'], { alpha: 2, beta: 1 })];
169
+ const touched = [{ path: path.join(DOC_DIR, 'beta.md'), name: 'beta', at: 9 }];
170
+ const groupsBefore = JSON.stringify(groups);
171
+ const touchedBefore = JSON.stringify(touched);
172
+ railRows({ groups, touched });
173
+ assert.equal(JSON.stringify(groups), groupsBefore);
174
+ assert.equal(JSON.stringify(touched), touchedBefore);
175
+ });
176
+
177
+ test('it is pure: the module loads nothing but the model it exports', () => {
178
+ const source = fs.readFileSync(path.join(__dirname, '..', 'lib', 'odd-rail-model.js'), 'utf8');
179
+ for (const forbidden of ['node:fs', 'node:child_process', 'node:os', 'Date.now', 'localeCompare(']) {
180
+ assert.equal(source.includes(forbidden), false, `lib/odd-rail-model.js must not use ${forbidden}`);
181
+ }
182
+ assert.deepEqual(Object.keys(require('../lib/odd-rail-model.js')), ['railRows']);
183
+ });
184
+ });