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,282 @@
1
+ 'use strict';
2
+ // T8 (odd-rail-section S8, S9): lib/odd-session.js, the record of the ODD
3
+ // documents THIS session wrote or edited.
4
+ //
5
+ // Contract under test, through the public surface only:
6
+ // - ONLY a `write` or an `edit` registers (S8). `read`, `bash`, `grep`, a missing
7
+ // tool name and every other tool never do, and neither does a `.md` that is not
8
+ // directly under an `odd/tasks` directory: a document outside it, one nested
9
+ // below it, a dotfile, a non-Markdown file. The rule is lib/odd-docs.js's scan
10
+ // rule (`<root>/odd/tasks/*.md`, non-recursive, dotfiles skipped, the `.md`
11
+ // suffix case-insensitive), stated lexically so no file is read.
12
+ // - The event is the shape the extension's own readers accept (mirrored here):
13
+ // `toolName`/`tool`/`name`, `paths`/`path`, a `cwd` for a relative target, and
14
+ // `isError` for the FAILED call that registers nothing.
15
+ // - A relative target resolves against the call's `cwd`; the stored path is
16
+ // ABSOLUTE and normalized, and a relative one without a cwd registers nothing.
17
+ // - Idempotent per absolute path: a second touch moves the document to the front
18
+ // and updates `at`, never a second row. One tool call is ONE timestamp.
19
+ // - `touched()` is most recently touched first and hands out copies, so a caller
20
+ // cannot mutate the record.
21
+ // - `entryOf(touch)` is the persisted entry body, and `TOUCH_ENTRY_TYPE` is the
22
+ // customType every reader filters on.
23
+ // - `restore(entries)` rebuilds from what `ctx.sessionManager.getEntries()`
24
+ // returned: only the touch entries, one per path (newest `at` wins), newest
25
+ // first, and EVERY garbage entry — a non-array answer included — is skipped
26
+ // without throwing. It replaces the record rather than merging into it.
27
+ // - The clock is the injected one: nothing here reads wall time, and a call that
28
+ // registers nothing reads no clock at all.
29
+ //
30
+ // No fs, no git, no timer: every case is in-memory.
31
+
32
+ const { test, describe } = require('node:test');
33
+ const assert = require('node:assert/strict');
34
+ const path = require('node:path');
35
+
36
+ const { createOddSession, TOUCH_ENTRY_TYPE } = require('../lib/odd-session.js');
37
+
38
+ const CWD = '/home/me/repo';
39
+ const ENTRY_TYPE = 'odd-worktree.odd-touch';
40
+
41
+ // A clock the tests own, so `at` is an observable and never wall time. `calls`
42
+ // pins that a call which registers nothing reads it zero times.
43
+ function clock(start = 1000) {
44
+ let at = start;
45
+ let calls = 0;
46
+ const now = () => {
47
+ calls += 1;
48
+ return at;
49
+ };
50
+ now.tick = (ms = 10) => {
51
+ at += ms;
52
+ return at;
53
+ };
54
+ now.read = () => at;
55
+ now.calls = () => calls;
56
+ return now;
57
+ }
58
+
59
+ function session(now) {
60
+ return createOddSession({ now: now ?? clock() });
61
+ }
62
+
63
+ const abs = (rel) => path.join(CWD, rel);
64
+ const noteWrite = (target, extra = {}) => ({ toolName: 'write', paths: [target], cwd: CWD, ...extra });
65
+
66
+ describe('what counts as a touch (S8)', () => {
67
+ test('a write or an edit on an odd/tasks document registers; every other tool never does', () => {
68
+ const touched = session();
69
+ assert.equal(touched.note({ toolName: 'write', paths: ['odd/tasks/alpha.md'], cwd: CWD }), true);
70
+ assert.equal(touched.note({ toolName: 'edit', paths: [abs('odd/tasks/beta.md')], cwd: CWD }), true);
71
+ for (const toolName of ['read', 'bash', 'grep', 'find', 'ls', 'worktree_create', '', undefined, 42]) {
72
+ assert.equal(touched.note({ toolName, paths: ['odd/tasks/gamma.md'], cwd: CWD }), false, String(toolName));
73
+ }
74
+ assert.deepEqual(
75
+ touched.touched().map((touch) => touch.name),
76
+ ['beta', 'alpha'],
77
+ );
78
+ });
79
+
80
+ test('a .md that is not directly under odd/tasks never registers', () => {
81
+ const touched = session();
82
+ for (const target of [
83
+ 'docs/alpha.md',
84
+ 'alpha.md',
85
+ 'odd/alpha.md',
86
+ 'odd/tasks/nested/alpha.md',
87
+ 'odd/tasks/deeper/nested/alpha.md',
88
+ 'odd/tasks/.alpha.md',
89
+ 'odd/tasks/alpha.txt',
90
+ 'odd/tasks/alpha.md.bak',
91
+ 'odd/tasks',
92
+ 'odd/tasks/',
93
+ 'odd/tasks/alpha',
94
+ ]) {
95
+ assert.equal(touched.note(noteWrite(target)), false, target);
96
+ }
97
+ // ... while the document of ANY checkout root does: the segments are the rule,
98
+ // not the prefix (`<root>/odd/tasks/*.md` for whichever root the session sits in).
99
+ assert.equal(touched.note(noteWrite('src/odd/tasks/alpha.md')), true);
100
+ assert.equal(touched.note(noteWrite('/elsewhere/odd/tasks/beta.MD')), true);
101
+ assert.deepEqual(touched.touched().map((touch) => touch.name), ['beta', 'alpha']);
102
+ });
103
+
104
+ test('a relative target resolves against the call cwd and is stored absolute and normalized', () => {
105
+ const touched = session();
106
+ assert.equal(touched.note(noteWrite('./odd/tasks/../tasks/alpha.md')), true);
107
+ assert.deepEqual(touched.touched(), [{ path: abs('odd/tasks/alpha.md'), name: 'alpha', at: 1000 }]);
108
+ // The same document by its absolute spelling is the SAME row, not a second one.
109
+ assert.equal(touched.note(noteWrite(abs('odd/tasks/alpha.md'))), true);
110
+ assert.equal(touched.touched().length, 1);
111
+ // A relative target with no cwd cannot be resolved: it is not guessed from
112
+ // the process's own directory.
113
+ assert.equal(touched.note({ toolName: 'write', paths: ['odd/tasks/beta.md'] }), false);
114
+ assert.equal(touched.touched().length, 1);
115
+ });
116
+
117
+ test('a FAILED call registers nothing', () => {
118
+ const touched = session();
119
+ assert.equal(touched.note({ ...noteWrite('odd/tasks/alpha.md'), isError: true }), false);
120
+ assert.deepEqual(touched.touched(), []);
121
+ assert.equal(touched.note({ ...noteWrite('odd/tasks/alpha.md'), isError: false }), true);
122
+ });
123
+
124
+ test('the event accessors mirror the extension’s own readers', () => {
125
+ const touched = session();
126
+ // `oddToolNameOf`/`oddTouchedPaths` accept these spellings, so a host that
127
+ // sends another one is still the same call.
128
+ assert.equal(touched.note({ tool: 'write', path: 'odd/tasks/alpha.md', cwd: CWD }), true);
129
+ assert.equal(touched.note({ name: 'edit', paths: ['odd/tasks/beta.md'], cwd: CWD }), true);
130
+ assert.deepEqual(touched.touched().map((touch) => touch.path), [abs('odd/tasks/beta.md'), abs('odd/tasks/alpha.md')]);
131
+ });
132
+
133
+ test('a re-touch moves the document to the front and updates `at`, never a second row', () => {
134
+ const now = clock();
135
+ const touched = session(now);
136
+ touched.note(noteWrite('odd/tasks/alpha.md'));
137
+ now.tick();
138
+ touched.note(noteWrite('odd/tasks/beta.md'));
139
+ now.tick();
140
+ touched.note(noteWrite('odd/tasks/gamma.md'));
141
+ assert.deepEqual(touched.touched().map((touch) => touch.name), ['gamma', 'beta', 'alpha']);
142
+ const at = now.tick();
143
+ assert.equal(touched.note(noteWrite('odd/tasks/alpha.md')), true);
144
+ assert.deepEqual(touched.touched().map((touch) => touch.name), ['alpha', 'gamma', 'beta']);
145
+ assert.equal(touched.touched()[0].at, at);
146
+ assert.equal(touched.touched().length, 3, 'one row per absolute path');
147
+ });
148
+
149
+ test('one tool call is ONE timestamp, and a call that registers nothing reads no clock', () => {
150
+ const now = clock();
151
+ const touched = session(now);
152
+ assert.equal(touched.note({ toolName: 'write', paths: ['odd/tasks/alpha.md', 'odd/tasks/beta.md'], cwd: CWD }), true);
153
+ const [beta, alpha] = touched.touched();
154
+ assert.equal(alpha.at, 1000);
155
+ assert.equal(beta.at, 1000);
156
+ assert.equal(now.calls(), 1);
157
+ touched.note({ toolName: 'read', paths: ['odd/tasks/alpha.md'], cwd: CWD });
158
+ touched.note(noteWrite('docs/alpha.md'));
159
+ assert.equal(now.calls(), 1);
160
+ });
161
+
162
+ test('touched() hands out copies, so a caller cannot mutate the record', () => {
163
+ const touched = session();
164
+ touched.note(noteWrite('odd/tasks/alpha.md'));
165
+ const rows = touched.touched();
166
+ rows.push({ path: '/elsewhere/odd/tasks/fake.md', name: 'fake', at: 1 });
167
+ rows[0].path = '/tampered';
168
+ assert.deepEqual(touched.touched(), [{ path: abs('odd/tasks/alpha.md'), name: 'alpha', at: 1000 }]);
169
+ });
170
+
171
+ test('has(path) answers for the stored absolute path', () => {
172
+ const touched = session();
173
+ touched.note(noteWrite('odd/tasks/alpha.md'));
174
+ assert.equal(touched.has(abs('odd/tasks/alpha.md')), true);
175
+ assert.equal(touched.has('odd/tasks/alpha.md'), false, 'the stored key is absolute');
176
+ assert.equal(touched.has(abs('odd/tasks/beta.md')), false);
177
+ assert.equal(touched.has(undefined), false);
178
+ });
179
+
180
+ test('entryOf(touch) is the persisted body, under the one customType', () => {
181
+ const touched = session();
182
+ touched.note(noteWrite('odd/tasks/alpha.md'));
183
+ assert.equal(TOUCH_ENTRY_TYPE, ENTRY_TYPE);
184
+ const entry = touched.entryOf(touched.touched()[0]);
185
+ assert.deepEqual(entry, {
186
+ customType: ENTRY_TYPE,
187
+ data: { path: abs('odd/tasks/alpha.md'), at: 1000 },
188
+ });
189
+ });
190
+
191
+ test('clear() forgets everything', () => {
192
+ const touched = session();
193
+ touched.note(noteWrite('odd/tasks/alpha.md'));
194
+ touched.clear();
195
+ assert.deepEqual(touched.touched(), []);
196
+ assert.equal(touched.has(abs('odd/tasks/alpha.md')), false);
197
+ });
198
+ });
199
+
200
+ describe('restore (S8/S9)', () => {
201
+ // The entries `ctx.sessionManager.getEntries()` returns: Pi's CustomEntry shape
202
+ // (`{ type: "custom", id, parentId, timestamp, customType, data }`, docs/
203
+ // session-format.md), which is the shape every reader in the extension filters on.
204
+ const entry = (id, file, at, customType = ENTRY_TYPE, type = 'custom') => ({
205
+ type,
206
+ id,
207
+ parentId: null,
208
+ timestamp: new Date(at).toISOString(),
209
+ customType,
210
+ data: { path: abs(`odd/tasks/${file}.md`), at },
211
+ });
212
+
213
+ test('keeps the touch entries, newest first, one per path, and returns how many it restored', () => {
214
+ const touched = session();
215
+ const restored = touched.restore([
216
+ entry('a', 'alpha', 1000),
217
+ entry('b', 'beta', 3000),
218
+ entry('c', 'alpha', 2000),
219
+ ]);
220
+ assert.equal(restored, 2);
221
+ assert.deepEqual(touched.touched(), [
222
+ { path: abs('odd/tasks/beta.md'), name: 'beta', at: 3000 },
223
+ { path: abs('odd/tasks/alpha.md'), name: 'alpha', at: 2000 },
224
+ ]);
225
+ });
226
+
227
+ test('skips every garbage entry without throwing', () => {
228
+ const touched = session();
229
+ const restored = touched.restore([
230
+ null,
231
+ undefined,
232
+ 'odd-worktree.odd-touch',
233
+ 42,
234
+ {},
235
+ entry('a', 'alpha', 1000),
236
+ { type: 'custom', customType: ENTRY_TYPE },
237
+ { type: 'custom', customType: ENTRY_TYPE, data: null },
238
+ { type: 'custom', customType: ENTRY_TYPE, data: {} },
239
+ { type: 'custom', customType: ENTRY_TYPE, data: { path: 42, at: 1000 } },
240
+ { type: 'custom', customType: ENTRY_TYPE, data: { path: abs('odd/tasks/gamma.md'), at: Number.NaN } },
241
+ { type: 'custom', customType: ENTRY_TYPE, data: { path: abs('odd/tasks/delta.md'), at: Number.POSITIVE_INFINITY } },
242
+ { type: 'custom', customType: ENTRY_TYPE, data: { path: '', at: 1000 } },
243
+ { type: 'custom', customType: 'odd-worktree.other', data: { path: abs('odd/tasks/beta.md'), at: 1000 } },
244
+ { type: 'custom_message', customType: ENTRY_TYPE, data: { path: abs('odd/tasks/beta.md'), at: 1000 } },
245
+ { type: 'message', id: 'm', role: 'user' },
246
+ ]);
247
+ assert.equal(restored, 1);
248
+ assert.deepEqual(touched.touched(), [{ path: abs('odd/tasks/alpha.md'), name: 'alpha', at: 1000 }]);
249
+ });
250
+
251
+ test('a non-array answer restores nothing instead of throwing', () => {
252
+ for (const answer of [undefined, null, 'entries', 42, {}]) {
253
+ const touched = session();
254
+ touched.note(noteWrite('odd/tasks/alpha.md'));
255
+ assert.equal(touched.restore(answer), 0, String(answer));
256
+ assert.deepEqual(touched.touched(), []);
257
+ }
258
+ });
259
+
260
+ test('replaces the record instead of merging into it', () => {
261
+ const touched = session();
262
+ touched.note(noteWrite('odd/tasks/alpha.md'));
263
+ touched.restore([entry('b', 'beta', 3000)]);
264
+ assert.deepEqual(touched.touched().map((touch) => touch.name), ['beta']);
265
+ });
266
+
267
+ test('a restored record then behaves exactly like a noted one', () => {
268
+ const now = clock(5000);
269
+ const touched = session(now);
270
+ touched.restore([entry('a', 'alpha', 1000), entry('b', 'beta', 2000)]);
271
+ assert.equal(touched.has(abs('odd/tasks/alpha.md')), true);
272
+ assert.deepEqual(touched.entryOf(touched.touched()[0]), {
273
+ customType: ENTRY_TYPE,
274
+ data: { path: abs('odd/tasks/beta.md'), at: 2000 },
275
+ });
276
+ touched.note(noteWrite('odd/tasks/alpha.md'));
277
+ assert.deepEqual(touched.touched(), [
278
+ { path: abs('odd/tasks/alpha.md'), name: 'alpha', at: 5000 },
279
+ { path: abs('odd/tasks/beta.md'), name: 'beta', at: 2000 },
280
+ ]);
281
+ });
282
+ });