pi-odd-worktree 2.1.0 → 3.0.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.
Files changed (160) hide show
  1. package/README.md +54 -319
  2. package/extensions/odd-worktree-tools.ts +4182 -2539
  3. package/lib/bash-activity.js +1166 -0
  4. package/lib/bash-design.js +684 -0
  5. package/lib/bash-destructive.js +559 -0
  6. package/lib/bash-frame.js +205 -0
  7. package/lib/card-content.js +23 -0
  8. package/lib/card-style.js +175 -0
  9. package/lib/checkout-facts.js +30 -70
  10. package/lib/codegraph-card-preview.js +628 -0
  11. package/lib/codegraph-card.js +515 -0
  12. package/lib/codegraph-compaction.js +76 -0
  13. package/lib/codegraph-daemon.js +246 -0
  14. package/lib/codegraph-edit-sync.js +182 -0
  15. package/lib/codegraph-guard-target.js +337 -0
  16. package/lib/codegraph-guard.js +211 -0
  17. package/lib/codegraph-index.js +1020 -0
  18. package/lib/codegraph-lock.js +607 -0
  19. package/lib/codegraph-mcp-config.js +134 -0
  20. package/lib/codegraph-mcp.js +123 -0
  21. package/lib/codegraph-paths.js +69 -0
  22. package/lib/codegraph-prompt/explore.md +11 -0
  23. package/lib/codegraph-prompt/fix.md +6 -0
  24. package/lib/codegraph-prompt/generic.md +8 -0
  25. package/lib/codegraph-prompt/judge.md +9 -0
  26. package/lib/codegraph-prompt/orchestrator.md +17 -0
  27. package/lib/codegraph-prompt/verify.md +10 -0
  28. package/lib/codegraph-prompt/worker.md +7 -0
  29. package/lib/codegraph-role.js +55 -0
  30. package/lib/codegraph-rules.js +146 -0
  31. package/lib/codegraph-session.js +303 -383
  32. package/lib/codegraph-warmup.js +139 -0
  33. package/lib/file-card-markdown.js +141 -0
  34. package/lib/file-card-resolver.js +245 -0
  35. package/lib/file-card.js +659 -0
  36. package/lib/files-index.js +387 -0
  37. package/lib/files-insert.js +81 -0
  38. package/lib/files-orders.js +326 -0
  39. package/lib/files-picker-view.js +1421 -0
  40. package/lib/files-preview.js +338 -0
  41. package/lib/files-scope-detail.js +171 -0
  42. package/lib/files-scope-view.js +507 -0
  43. package/lib/gh-issues.js +323 -15
  44. package/lib/issue-preview.js +26 -7
  45. package/lib/issues-bash-mutation.js +206 -0
  46. package/lib/issues-cache-store.js +261 -0
  47. package/lib/issues-section.js +710 -0
  48. package/lib/issues-view.js +649 -52
  49. package/lib/odd-prompt/odd-worktrees.md +21 -0
  50. package/lib/odd-worktrees-prompt.js +10 -28
  51. package/lib/related-issues-cache.js +210 -0
  52. package/lib/shell-card.js +825 -0
  53. package/lib/skill-odd-card.js +183 -0
  54. package/lib/tui-paint.js +4 -5
  55. package/lib/warp-create-panel.js +538 -250
  56. package/lib/warp-create-picker.js +56 -9
  57. package/lib/warp-delete-confirm.js +19 -28
  58. package/lib/warp-selector-view.js +160 -82
  59. package/lib/which-command.js +90 -0
  60. package/lib/worktree-card.js +267 -897
  61. package/lib/worktree-cards.js +383 -0
  62. package/lib/worktree-create.js +287 -0
  63. package/lib/worktree-git.js +3 -3
  64. package/lib/worktree-guard.js +548 -0
  65. package/lib/worktree-hooks.js +421 -712
  66. package/lib/worktree-list.js +120 -0
  67. package/lib/worktree-registry.js +162 -879
  68. package/lib/{worktree-close-herdr.js → worktree-remove.js} +9 -11
  69. package/lib/worktree-setup-runner.js +376 -0
  70. package/lib/worktree-setup.js +140 -0
  71. package/lib/worktree-warp-busy.js +462 -85
  72. package/lib/worktree-warp-sessions.js +1 -2
  73. package/package.json +2 -2
  74. package/tools/bash-activity.test.js +1420 -0
  75. package/tools/bash-design.test.js +1093 -0
  76. package/tools/bash-destructive.test.js +139 -0
  77. package/tools/card-content.test.js +44 -0
  78. package/tools/card-style.test.js +298 -0
  79. package/tools/codegraph-card-preview.test.js +254 -0
  80. package/tools/codegraph-card.test.js +1080 -0
  81. package/tools/codegraph-compaction.test.js +378 -0
  82. package/tools/codegraph-daemon.test.js +248 -0
  83. package/tools/codegraph-edit-sync.test.js +295 -0
  84. package/tools/codegraph-guard-target.test.js +174 -0
  85. package/tools/codegraph-guard.test.js +238 -0
  86. package/tools/codegraph-index.test.js +1948 -0
  87. package/tools/codegraph-lock.test.js +932 -0
  88. package/tools/codegraph-mcp-config.test.js +181 -0
  89. package/tools/codegraph-mcp.test.js +194 -0
  90. package/tools/codegraph-paths.test.js +44 -0
  91. package/tools/codegraph-role.test.js +30 -0
  92. package/tools/codegraph-rules.test.js +250 -0
  93. package/tools/codegraph-session.test.js +660 -391
  94. package/tools/codegraph-warmup.test.js +316 -0
  95. package/tools/commands.test.js +510 -31
  96. package/tools/file-card-markdown.test.js +188 -0
  97. package/tools/file-card.test.js +1289 -0
  98. package/tools/files-bridge.test.js +575 -0
  99. package/tools/files-command.test.js +613 -0
  100. package/tools/files-index.test.js +473 -0
  101. package/tools/files-insert.test.js +139 -0
  102. package/tools/files-orders.test.js +597 -0
  103. package/tools/files-picker-view.test.js +1889 -0
  104. package/tools/files-preview.test.js +452 -0
  105. package/tools/files-scope-detail.test.js +264 -0
  106. package/tools/files-scope-view.test.js +614 -0
  107. package/tools/fixtures/card-codegraph-outputs.js +110 -0
  108. package/tools/fixtures/codegraph-cli-stub.js +16 -0
  109. package/tools/fixtures/codegraph-init-process.js +28 -0
  110. package/tools/fixtures/codegraph-lock-worker.js +117 -0
  111. package/tools/fixtures/daemon-fake.js +98 -0
  112. package/tools/fixtures/daemon-mcp-server.js +39 -0
  113. package/tools/fixtures/daemon-warmup-worker.js +12 -0
  114. package/tools/fixtures/load-extension.js +136 -3
  115. package/tools/gh-issues-feedback.test.js +31 -7
  116. package/tools/gh-issues.test.js +417 -1
  117. package/tools/issue-preview.test.js +44 -7
  118. package/tools/issues-bash-mutation.test.js +101 -0
  119. package/tools/issues-cache-store.test.js +222 -0
  120. package/tools/issues-command.test.js +1517 -67
  121. package/tools/issues-open-guard.test.js +1096 -0
  122. package/tools/issues-section.test.js +4235 -0
  123. package/tools/issues-seed-query-regression.test.js +67 -0
  124. package/tools/issues-view.test.js +1489 -60
  125. package/tools/odd-worktrees-prompt.test.js +98 -65
  126. package/tools/package-surface.test.js +431 -36
  127. package/tools/related-issues-cache.test.js +324 -0
  128. package/tools/shell-card.test.js +834 -0
  129. package/tools/skill-odd-card.test.js +200 -0
  130. package/tools/t5-session-identity.test.js +44 -22
  131. package/tools/warp-busy-guard.test.js +2565 -309
  132. package/tools/warp-create-panel.test.js +499 -74
  133. package/tools/warp-create-picker.test.js +101 -5
  134. package/tools/warp-delete-confirm.test.js +49 -15
  135. package/tools/warp-empty-session.test.js +0 -1
  136. package/tools/warp-fallback-regression.test.js +533 -0
  137. package/tools/warp-selector-view.test.js +460 -196
  138. package/tools/which-command.test.js +126 -0
  139. package/tools/worktree-card.test.js +551 -0
  140. package/tools/worktree-cards.test.js +845 -0
  141. package/tools/worktree-changes.test.js +4 -5
  142. package/tools/worktree-create.test.js +489 -0
  143. package/tools/worktree-guard.test.js +823 -0
  144. package/tools/worktree-hooks.test.js +905 -0
  145. package/tools/worktree-list.test.js +170 -0
  146. package/tools/worktree-registry.test.js +89 -230
  147. package/tools/worktree-remove-async.test.js +318 -0
  148. package/tools/worktree-setup-runner.test.js +276 -0
  149. package/tools/worktree-setup.test.js +140 -0
  150. package/tools/worktree-tools.test.js +419 -199
  151. package/lib/codegraph-commands.js +0 -165
  152. package/lib/odd-prompt/odd-worktrees.with-codegraph.md +0 -19
  153. package/lib/odd-prompt/odd-worktrees.without-codegraph.md +0 -15
  154. package/lib/worktree-model-result.js +0 -291
  155. package/tools/codegraph-commands.test.js +0 -371
  156. package/tools/herdr-free-create.test.js +0 -161
  157. package/tools/worktree-close-herdr-async.test.js +0 -204
  158. package/tools/worktree-odd-migration.test.js +0 -224
  159. package/tools/worktree-registry-cache.test.js +0 -163
  160. package/tools/worktree-registry-odd.test.js +0 -231
@@ -0,0 +1,170 @@
1
+ 'use strict';
2
+ // The agent-facing rendering of `worktree_list`: which rows the list shows, in which
3
+ // order and the exact text of every line. Every assertion goes through the module's
4
+ // public interface with the two inputs a real call produces — Git's own worktree
5
+ // listing and the canonical main root — so the text and `details` cannot drift.
6
+ //
7
+ // S2: the default view is the checkouts this package created plus the main one.
8
+ // S2/S3: `all` adds the worktrees Git knows outside `<repo-name>-worktrees`.
9
+ // S3: a row is its directory's basename (the ODD) and its branch — no `odd:` field, no
10
+ // date, no setup or hooks line, no stored state. A directory that exists is a worktree
11
+ // and one that does not is simply not listed.
12
+
13
+ const assert = require('node:assert/strict');
14
+ const test = require('node:test');
15
+
16
+ const {
17
+ renderWorktreeListText,
18
+ worktreeListDetails,
19
+ worktreeListFooters,
20
+ worktreeListView,
21
+ } = require('../lib/worktree-list.js');
22
+
23
+ const MAIN_ROOT = '/work/demo';
24
+ const WORKTREES = '/work/demo-worktrees';
25
+ const PROJECT = 'demo';
26
+
27
+ /** One `git worktree list` entry: the shape lib/worktree-git.js parses. */
28
+ const entry = (path, branch, head = '1a2b3c4d5e') => (branch === null ? { path, head, detached: true } : { path, branch, head });
29
+
30
+ const MAIN = entry(MAIN_ROOT, 'main');
31
+
32
+ const viewOf = (input) => worktreeListView({
33
+ entries: input.entries,
34
+ mainRoot: MAIN_ROOT,
35
+ project: PROJECT,
36
+ all: input.all,
37
+ });
38
+
39
+ /** Every line is plain: no ANSI, no trailing spaces, no padding runs. */
40
+ function assertPlainLines(text) {
41
+ assert.equal(text.includes('\u001b'), false, 'the text carries no ANSI escape');
42
+ for (const line of text.split('\n')) {
43
+ assert.equal(line.trimEnd(), line, `no trailing spaces: ${JSON.stringify(line)}`);
44
+ assert.equal(line.includes(' '), false, `no padding run: ${JSON.stringify(line)}`);
45
+ }
46
+ }
47
+
48
+ // ---- S2: the default view ----
49
+
50
+ test('prints project, path, the unnumbered main checkout and the created worktrees by name', () => {
51
+ const view = viewOf({
52
+ entries: [
53
+ MAIN,
54
+ entry(`${WORKTREES}/zeta`, 'feat/zeta'),
55
+ entry(`${WORKTREES}/alpha`, 'feat/alpha'),
56
+ ],
57
+ });
58
+ assert.equal(view.text, [
59
+ 'project: demo',
60
+ 'path: /work/demo',
61
+ 'base: main@1a2b3c4',
62
+ '1 alpha · feat/alpha',
63
+ '2 zeta · feat/zeta',
64
+ ].join('\n'));
65
+ assertPlainLines(view.text);
66
+ });
67
+
68
+ test('each row is the directory basename — the ODD — and the branch, and nothing else', () => {
69
+ const view = viewOf({ entries: [MAIN, entry(`${WORKTREES}/worktree-surface`, 'feat/surface')] });
70
+ const row = view.details.worktrees[1];
71
+ assert.equal(row.name, 'worktree-surface', 'the ODD is the directory basename');
72
+ assert.equal(row.branch, 'feat/surface');
73
+ assert.deepEqual(Object.keys(row).sort(), ['branch', 'index', 'name', 'path'], 'no odd, no state, no date');
74
+ assert.equal(/odd:/.test(view.text), false, 'the output carries no odd: field');
75
+ assert.equal(/setup:|hooks:|metadata|ready|running|failed|skipped|interrupted|gone|missing|foreign/.test(view.text), false, 'no state vocabulary and no detail lines');
76
+ });
77
+
78
+ test('a linked worktree outside the canonical directory is hidden without all, and counted', () => {
79
+ const view = viewOf({
80
+ entries: [MAIN, entry(`${WORKTREES}/alpha`, 'feat/alpha'), entry('/elsewhere/other', 'feat/other')],
81
+ });
82
+ assert.equal(view.text.split('\n').includes('2 other · feat/other'), false, 'the outside worktree is not listed');
83
+ assert.equal(view.text.split('\n').at(-1), '1 worktrees outside the list (--all to list them)');
84
+ assert.equal(view.details.hiddenOutside, 1);
85
+ });
86
+
87
+ test('a checkout Git no longer lists is simply absent, and a stale directory is absent too', () => {
88
+ // Git's listing is the whole input: a directory that still sits under
89
+ // `<repo-name>-worktrees` but is no longer a worktree is never shown, and no row
90
+ // reports `gone`, `missing` or a date.
91
+ const view = viewOf({ entries: [MAIN, entry(`${WORKTREES}/alpha`, 'feat/alpha')] });
92
+ assert.equal(view.text.split('\n').length, 4, 'project, path, main and one row');
93
+ assert.equal(view.details.worktrees.length, 2);
94
+ });
95
+
96
+ // ---- S2/S3: all lists the outside worktrees ----
97
+
98
+ test('all adds the worktrees outside the canonical directory, after ours and by name', () => {
99
+ const view = viewOf({
100
+ entries: [
101
+ MAIN,
102
+ entry(`${WORKTREES}/alpha`, 'feat/alpha'),
103
+ entry('/elsewhere/beta', 'feat/beta'),
104
+ entry('/elsewhere/gamma', null),
105
+ ],
106
+ all: true,
107
+ });
108
+ assert.equal(view.text.split('\n').join('|'), [
109
+ 'project: demo',
110
+ 'path: /work/demo',
111
+ 'base: main@1a2b3c4',
112
+ '1 alpha · feat/alpha',
113
+ '2 beta · feat/beta',
114
+ '3 gamma · -',
115
+ ].join('|'));
116
+ assert.deepEqual(worktreeListFooters(view.details), [], 'nothing is hidden when everything is listed');
117
+ assert.equal('foreign' in view.details, false, 'no state field decides what a row is');
118
+ });
119
+
120
+ test('a detached checkout prints a dash instead of inventing a branch', () => {
121
+ const view = viewOf({ entries: [MAIN, entry(`${WORKTREES}/alpha`, null)] });
122
+ assert.equal(view.details.worktrees[1].branch, '-');
123
+ assert.equal(view.text.split('\n').at(-1), '1 alpha · -');
124
+ });
125
+
126
+ test('the main row prints first and unnumbered, as Git lists it', () => {
127
+ const view = viewOf({ entries: [MAIN, entry(`${WORKTREES}/alpha`, 'feat/alpha')] });
128
+ assert.equal(view.details.worktrees[0].path, MAIN_ROOT);
129
+ assert.equal(view.details.worktrees[0].index, undefined);
130
+ assert.equal(view.text.split('\n')[2], 'base: main@1a2b3c4');
131
+ });
132
+
133
+ test('the main row names its base the way worktree_create does: branch@sha', () => {
134
+ // The same `base: <branch>@<sha>` shape the create prints for the checkout it forks from.
135
+ assert.equal(viewOf({ entries: [MAIN] }).text.split('\n')[2], 'base: main@1a2b3c4');
136
+
137
+ // A main checkout standing on another branch names that branch, never `main`.
138
+ const elsewhere = viewOf({ entries: [entry(MAIN_ROOT, 'feat/main-checkout')] });
139
+ assert.equal(elsewhere.text.split('\n')[2], 'base: feat/main-checkout@1a2b3c4');
140
+
141
+ // No stored head: the row states the branch alone instead of a dangling `@`.
142
+ const unborn = viewOf({ entries: [entry(MAIN_ROOT, 'main', '')] });
143
+ assert.equal(unborn.text.split('\n')[2], 'base: main');
144
+
145
+ // A detached main checkout prints the same dash the numbered rows print.
146
+ const detached = viewOf({ entries: [entry(MAIN_ROOT, null)] });
147
+ assert.equal(detached.text.split('\n')[2], 'base: -@1a2b3c4');
148
+ assert.equal(/^main:/m.test(viewOf({ entries: [MAIN] }).text), false, 'the old `main:` label is gone');
149
+ });
150
+
151
+
152
+ // ---- The pieces ----
153
+
154
+ test('the footer counts only while something is hidden', () => {
155
+ assert.deepEqual(worktreeListFooters({ hiddenOutside: 0 }), []);
156
+ assert.deepEqual(worktreeListFooters({ hiddenOutside: 2 }), ['2 worktrees outside the list (--all to list them)']);
157
+ });
158
+
159
+ test('the text and the details are built from the same rows', () => {
160
+ const details = worktreeListDetails({
161
+ entries: [MAIN, entry(`${WORKTREES}/alpha`, 'feat/alpha')],
162
+ mainRoot: MAIN_ROOT,
163
+ project: PROJECT,
164
+ all: false,
165
+ });
166
+ assert.equal(renderWorktreeListText(details), viewOf({ entries: [MAIN, entry(`${WORKTREES}/alpha`, 'feat/alpha')] }).text);
167
+ assert.equal(details.worktrees.length, 2);
168
+ assert.equal(details.path, MAIN_ROOT);
169
+ assert.equal(details.project, PROJECT);
170
+ });
@@ -1,244 +1,103 @@
1
1
  'use strict';
2
- // S1: the worktree registry never destroys data a write was not asked to
3
- // change.
2
+ // Which checkouts this package owns — the directory rule, with no document.
4
3
  //
5
- // A registry row can still hold the legacy `odd[]` array an older version
6
- // wrote (S8b), and it can hold fields this version knows nothing about. A
7
- // write that addresses ONE row used to rewrite the whole file, normalizing
8
- // every other row on the way through: the stored documents of rows nobody
9
- // touched were destroyed, and the writer had to report that loss. S1 removes
10
- // the association from the product, so the report has no place left to
11
- // travel — which means the loss itself must go.
12
- //
13
- // The contract these tests pin, through the public API plus the bytes on
14
- // disk:
15
- // - a row a write does NOT address keeps its stored association, its
16
- // `issues` list and every unknown field, byte for byte, including a
17
- // legacy `odd[]` array and a row that never had the field at all;
18
- // - the row a write DOES address keeps the one-document rule: a supplied
19
- // legacy list collapses to its first entry and the rest are reported
20
- // once (S8b), and an omitted association leaves the stored value as it
21
- // is on disk;
22
- // - an unreadable (malformed) registry is still replaced with a best-effort
23
- // backup rather than merged.
24
- //
25
- // Every test builds its own isolated fixture repository under os.tmpdir().
4
+ // S3/S4: `<git-common-dir>/pi-worktrees.json` is gone. A create always lands at
5
+ // `<repo-parent>/<repo-name>-worktrees/<odd>`, so the path answers what a stored row
6
+ // used to: which checkouts are ours and which were made outside. Every case here is a
7
+ // pure function of an explicit path and a `git worktree list` text; nothing reads or
8
+ // writes a file.
26
9
 
27
- const { test, describe, after } = require('node:test');
28
10
  const assert = require('node:assert/strict');
29
- const fs = require('node:fs');
30
- const os = require('node:os');
31
- const path = require('node:path');
32
- const { spawnSync } = require('node:child_process');
33
-
34
- const registryLib = require('../lib/worktree-registry.js');
35
-
36
- const LEGACY_ODD = ['odd/tasks/first.md', 'odd/tasks/second.md', 'odd/tasks/third.md'];
37
-
38
- const roots = [];
39
- after(() => {
40
- for (const dir of roots) {
41
- try {
42
- fs.rmSync(dir, { recursive: true, force: true });
43
- } catch {}
44
- }
11
+ const test = require('node:test');
12
+
13
+ const {
14
+ checkoutRows,
15
+ findCheckoutByBranch,
16
+ findCheckoutByPath,
17
+ isInsideDirectory,
18
+ isOwnedCheckout,
19
+ outsideCheckouts,
20
+ ownedCheckouts,
21
+ parseWorktreePorcelain,
22
+ worktreeDirectoryPath,
23
+ } = require('../lib/worktree-registry.js');
24
+
25
+ const MAIN_ROOT = '/work/demo';
26
+ const WORKTREES = '/work/demo-worktrees';
27
+
28
+ // ---- S3: the canonical directory is the rule ----
29
+
30
+ test('the canonical directory sits beside the main checkout and is named after it', () => {
31
+ assert.equal(worktreeDirectoryPath(MAIN_ROOT), WORKTREES);
32
+ assert.equal(worktreeDirectoryPath('/a/b/c'), '/a/b/c-worktrees');
45
33
  });
46
34
 
47
- function track(dir) {
48
- roots.push(dir);
49
- return dir;
50
- }
51
-
52
- function git(cwd, args) {
53
- const result = spawnSync('git', args, { cwd, encoding: 'utf8' });
54
- assert.equal(result.status, 0, `git ${args.join(' ')} failed: ${result.stderr}`);
55
- return (result.stdout || '').trim();
56
- }
57
-
58
- function makeRepo(tag) {
59
- const dir = track(fs.mkdtempSync(path.join(os.tmpdir(), `wt-reg-${tag}-`)));
60
- git(dir, ['init']);
61
- git(dir, ['config', 'user.email', 'test@example.com']);
62
- git(dir, ['config', 'user.name', 'Test']);
63
- git(dir, ['commit', '--allow-empty', '-m', 'init']);
64
- return fs.realpathSync(dir);
65
- }
66
-
67
- function checkoutPath(root, slug) {
68
- return path.join(path.dirname(root), `${path.basename(root)}-${slug}`);
69
- }
70
-
71
- function row(branch, root) {
72
- return { branch, path: checkoutPath(root, branch.replace(/[^a-z0-9]+/gi, '-')), thread: `thread-${branch}`, issues: [] };
73
- }
74
-
75
- // A row exactly as an older version left it: the legacy association list,
76
- // an `issues` list, and a field this version does not model at all.
77
- function legacyRow(root) {
78
- return {
79
- branch: 'feat/legacy',
80
- path: checkoutPath(root, 'legacy'),
81
- thread: 'thread-legacy',
82
- issues: [11, 12],
83
- odd: LEGACY_ODD.slice(),
84
- note: 'kept verbatim',
85
- createdAt: '2024-01-01T00:00:00.000Z',
86
- };
87
- }
88
-
89
- function writeRegistryFile(root, rows) {
90
- const file = registryLib.registryPath(root);
91
- fs.mkdirSync(path.dirname(file), { recursive: true });
92
- fs.writeFileSync(file, `${JSON.stringify({ version: registryLib.REGISTRY_VERSION, worktrees: rows }, null, 2)}\n`);
93
- return file;
94
- }
95
-
96
- function legacyRepo(tag) {
97
- const root = makeRepo(tag);
98
- const other = row('feat/other', root);
99
- const legacy = legacyRow(root);
100
- writeRegistryFile(root, [legacy, { ...other, odd: 'odd/tasks/other.md' }]);
101
- return { root, legacy, other };
102
- }
103
-
104
- function storedRow(root, branch) {
105
- const raw = fs.readFileSync(registryLib.registryPath(root), 'utf8');
106
- return JSON.parse(raw).worktrees.find((entry) => entry.branch === branch);
107
- }
108
-
109
- // Byte-for-byte: the parsed row equals the original in values AND key order.
110
- function assertKeptVerbatim(root, original) {
111
- const stored = storedRow(root, original.branch);
112
- assert.deepEqual(stored, original, `the unaddressed row is preserved verbatim: ${JSON.stringify(stored)}`);
113
- assert.equal(
114
- JSON.stringify(stored),
115
- JSON.stringify(original),
116
- 'not one byte of the stored row changed, key order included',
117
- );
118
- }
119
-
120
- // Each entry is one whole-file write that addresses a DIFFERENT row.
121
- const UNRELATED_WRITES = [
122
- ['upsertWorktree', ({ root, other }) => registryLib.upsertWorktree(root, { ...other, issues: [7] })],
123
- ['appendWorktree', ({ root, other }) => registryLib.appendWorktree(root, { ...other, issues: [7] })],
124
- ['updateWorktree', ({ root }) => registryLib.updateWorktree(root, 'feat/other', { issues: [7, 8] })],
125
- ['removeWorktree', ({ root }) => registryLib.removeWorktree(root, 'feat/other')],
126
- ['removeWorktreeByPath', ({ root, other }) => registryLib.removeWorktreeByPath(root, other.path)],
127
- ['removeWorktreeExact', ({ root, other }) => registryLib.removeWorktreeExact(root, other.branch, other.path)],
128
- ['pruneWorktrees', ({ root, other }) => registryLib.pruneWorktrees(root, [other.path])],
129
- ['registerMissingWorktrees', ({ root }) => {
130
- const live = checkoutPath(root, 'new-live');
131
- fs.mkdirSync(live, { recursive: true });
132
- roots.push(live);
133
- return registryLib.registerMissingWorktrees(root, [{ branch: 'feat/new-live', path: live }], null);
134
- }],
135
- ];
136
-
137
- describe('S1: a registry write never destroys an association it does not address', { concurrency: false }, () => {
138
- for (const [name, run] of UNRELATED_WRITES) {
139
- test(`${name} leaves another row's stored documents, issues and fields byte for byte`, () => {
140
- const fixture = legacyRepo(`keep-${name}`);
141
- const before = fs.readFileSync(registryLib.registryPath(fixture.root), 'utf8');
142
- const result = run(fixture);
143
- assert.equal(result.ok, true, `expected the write to succeed: ${JSON.stringify(result)}`);
144
- assert.equal('dropped' in result, false, 'nothing was lost, so nothing is reported');
145
- assertKeptVerbatim(fixture.root, fixture.legacy);
146
- // The row that WAS addressed is the only one that changed.
147
- assert.notEqual(fs.readFileSync(registryLib.registryPath(fixture.root), 'utf8'), before);
148
- assert.equal(registryLib.findByBranch(fixture.root, 'feat/legacy').odd, LEGACY_ODD[0],
149
- 'reads still resolve the legacy list to ONE document in memory');
150
- });
151
- }
152
-
153
- test('a row without an odd or issues field gains neither', () => {
154
- const root = makeRepo('bare-row');
155
- const other = row('feat/other', root);
156
- const bare = { branch: 'feat/bare', path: checkoutPath(root, 'bare'), thread: 'thread-bare' };
157
- writeRegistryFile(root, [bare, { ...other, odd: null }]);
158
- const result = registryLib.upsertWorktree(root, { ...other, issues: [3] });
159
- assert.equal(result.ok, true, JSON.stringify(result));
160
- const stored = storedRow(root, 'feat/bare');
161
- assert.deepEqual(Object.keys(stored), ['branch', 'path', 'thread'],
162
- 'a write invents no `issues`/`odd` on a row it did not address');
163
- assert.equal(JSON.stringify(stored), JSON.stringify(bare), 'the row is byte-identical');
164
- });
165
-
166
- test('a malformed registry is still replaced behind a backup, never merged', () => {
167
- const root = makeRepo('malformed');
168
- const file = registryLib.registryPath(root);
169
- fs.mkdirSync(path.dirname(file), { recursive: true });
170
- fs.writeFileSync(file, '{ not json');
171
- const written = registryLib.upsertWorktree(root, { branch: 'feat/new', path: checkoutPath(root, 'new'), thread: 't', issues: [] });
172
- assert.equal(written.ok, true, JSON.stringify(written));
173
- assert.equal(typeof written.backup, 'string', 'the damaged bytes are preserved beside the registry');
174
- assert.equal(fs.readFileSync(written.backup, 'utf8'), '{ not json', 'the backup holds the previous bytes');
175
- assert.deepEqual(
176
- JSON.parse(fs.readFileSync(file, 'utf8')).worktrees.map((entry) => entry.branch),
177
- ['feat/new'],
178
- 'the incoming row is the only trustworthy data',
179
- );
180
- });
35
+ test('a checkout under the canonical directory is ours; anything else is not', () => {
36
+ assert.equal(isOwnedCheckout(`${WORKTREES}/alpha`, MAIN_ROOT), true);
37
+ assert.equal(isOwnedCheckout(`${WORKTREES}/nested/beta`, MAIN_ROOT), true);
38
+ assert.equal(isOwnedCheckout('/elsewhere/alpha', MAIN_ROOT), false);
39
+ assert.equal(isOwnedCheckout('/work/demo-worktrees-other/alpha', MAIN_ROOT), false, 'a prefix is not the directory');
40
+ assert.equal(isOwnedCheckout(MAIN_ROOT, MAIN_ROOT), false, 'the main checkout is never one of ours');
41
+ assert.equal(isOwnedCheckout(undefined, MAIN_ROOT), false);
181
42
  });
182
43
 
183
- describe('S1/S8: the row a write addresses keeps the one-document rule', { concurrency: false }, () => {
184
- test('an addressed legacy row collapses to its FIRST entry and reports the rest once', () => {
185
- const { root } = legacyRepo('addressed-update');
186
- const updated = registryLib.updateWorktree(root, 'feat/legacy', { issues: [11] });
187
- assert.equal(updated.ok, true, JSON.stringify(updated));
188
- assert.deepEqual(updated.dropped, LEGACY_ODD.slice(1), 'the collapse of the addressed row is reported');
189
- assert.equal(storedRow(root, 'feat/legacy').odd, LEGACY_ODD[0], 'the file holds one document');
190
- const again = registryLib.updateWorktree(root, 'feat/legacy', { issues: [11, 12] });
191
- assert.equal('dropped' in again, false, 'reported once, never on every later write');
192
- });
44
+ test('the listing is partitioned into ours and the outside worktrees, with the main checkout removed', () => {
45
+ const entries = [
46
+ { path: MAIN_ROOT, branch: 'main' },
47
+ { path: `${WORKTREES}/alpha`, branch: 'feat/alpha' },
48
+ { path: '/elsewhere/beta', branch: 'feat/beta' },
49
+ ];
50
+ assert.deepEqual(ownedCheckouts(entries.slice(1), MAIN_ROOT).map((entry) => entry.path), [`${WORKTREES}/alpha`]);
51
+ assert.deepEqual(outsideCheckouts(entries.slice(1), MAIN_ROOT).map((entry) => entry.path), ['/elsewhere/beta']);
52
+ });
193
53
 
194
- test('an upsert that omits the association leaves the stored list exactly as it is', () => {
195
- const { root, legacy } = legacyRepo('addressed-omitted');
196
- const written = registryLib.upsertWorktree(root, {
197
- branch: legacy.branch, path: legacy.path, thread: legacy.thread, issues: [11, 12, 13],
198
- });
199
- assert.equal(written.ok, true, JSON.stringify(written));
200
- assert.equal('dropped' in written, false, 'an association nobody changed is not a loss');
201
- const stored = storedRow(root, legacy.branch);
202
- assert.deepEqual(stored.odd, LEGACY_ODD, 'the stored list is untouched');
203
- assert.deepEqual(stored.issues, [11, 12, 13], 'the supplied field is the one that changes');
204
- assert.equal(stored.note, 'kept verbatim', 'a field this version does not model survives an addressed write too');
205
- });
54
+ test('inside means the directory itself or a real descendant, never a sibling sharing the prefix', () => {
55
+ assert.equal(isInsideDirectory('/work/demo-worktrees', WORKTREES), true);
56
+ assert.equal(isInsideDirectory('/work/demo-worktrees/alpha', WORKTREES), true);
57
+ assert.equal(isInsideDirectory('/work/demo-worktreesX/alpha', WORKTREES), false);
58
+ assert.equal(isInsideDirectory('/work/demo', WORKTREES), false);
59
+ });
206
60
 
207
- test('a supplied legacy list still collapses to ONE document and reports the rest', () => {
208
- const root = makeRepo('supplied-list');
209
- const target = row('feat/supplied', root);
210
- const written = registryLib.upsertWorktree(root, {
211
- ...target, issues: [], odd: ['odd/tasks/x.md', 'odd/tasks/y.md'],
212
- });
213
- assert.equal(written.ok, true, JSON.stringify(written));
214
- assert.deepEqual(written.dropped, ['odd/tasks/y.md'], 'the caller-supplied list keeps its first entry only');
215
- assert.equal(storedRow(root, 'feat/supplied').odd, 'odd/tasks/x.md');
216
- });
61
+ // ---- The rows the name resolvers use ----
62
+
63
+ test('a row is the path and the branch Git reports, and a detached checkout simply has no branch', () => {
64
+ assert.deepEqual(
65
+ checkoutRows([
66
+ { path: MAIN_ROOT, branch: 'main', head: 'abc' },
67
+ { path: `${WORKTREES}/alpha`, head: 'def', detached: true },
68
+ { branch: 'no-path' },
69
+ ]),
70
+ [{ path: MAIN_ROOT, branch: 'main' }, { path: `${WORKTREES}/alpha` }],
71
+ );
72
+ });
217
73
 
218
- test('addOddDoc replaces the addressed row\'s document and reports the replacement', () => {
219
- const { root } = legacyRepo('addressed-add');
220
- const added = registryLib.addOddDoc(root, 'feat/legacy', 'odd/tasks/chosen.md');
221
- assert.equal(added.ok, true, JSON.stringify(added));
222
- assert.equal(added.entry.odd, 'odd/tasks/chosen.md');
223
- assert.equal(added.replaced, LEGACY_ODD[0], 'the document that lost the association is named');
224
- assert.deepEqual(added.dropped, LEGACY_ODD.slice(1), 'and the legacy entries the selection replaced');
225
- assert.equal(storedRow(root, 'feat/legacy').odd, 'odd/tasks/chosen.md');
226
- // The other row of the fixture is untouched by the addressed write.
227
- assert.equal(storedRow(root, 'feat/other').odd, 'odd/tasks/other.md');
228
- });
74
+ test('a path key must be absolute; a branch key matches its exact name', () => {
75
+ const rows = checkoutRows([{ path: MAIN_ROOT, branch: 'main' }, { path: `${WORKTREES}/alpha`, branch: 'feat/alpha' }]);
76
+ assert.equal(findCheckoutByPath(rows, MAIN_ROOT).branch, 'main');
77
+ assert.equal(findCheckoutByPath(rows, 'demo'), null, 'a relative key names nothing');
78
+ assert.equal(findCheckoutByPath(rows, '/elsewhere'), null);
79
+ assert.equal(findCheckoutByBranch(rows, 'feat/alpha').path, `${WORKTREES}/alpha`);
80
+ assert.equal(findCheckoutByBranch(rows, 'feat/nope'), null);
229
81
  });
230
82
 
231
- describe('S1: the registry file carries no report and no marker', { concurrency: false }, () => {
232
- test('neither the drop report nor the migration stamp ever reaches the file', () => {
233
- const { root, other } = legacyRepo('serialize');
234
- registryLib.upsertWorktree(root, { ...other, issues: [2] });
235
- registryLib.removeWorktree(root, 'feat/other');
236
- const raw = fs.readFileSync(registryLib.registryPath(root), 'utf8');
237
- assert.ok(!raw.includes('oddDropped'), `the non-enumerable marker never reaches the file:\n${raw}`);
238
- assert.ok(!raw.includes('"dropped"'), 'and neither does the report');
239
- const reread = registryLib.readRegistry(root).worktrees.find((entry) => entry.branch === 'feat/legacy');
240
- assert.equal(Object.keys(reread).includes('oddDropped'), false, 'the stamp is never an observable field');
241
- assert.equal(JSON.stringify(reread).includes('oddDropped'), false, 'and never serializes');
242
- assert.equal(reread.odd, LEGACY_ODD[0], 'the in-memory view keeps the single-document rule');
243
- });
83
+ // ---- The one listing parser ----
84
+
85
+ test('git worktree list --porcelain parses in both spellings, main first and in listing order', () => {
86
+ const text = [
87
+ 'worktree /work/demo',
88
+ 'HEAD 1a2b3c4d5e',
89
+ 'branch refs/heads/main',
90
+ '',
91
+ 'worktree /work/demo-worktrees/alpha',
92
+ 'HEAD 5f6e7d8c9b',
93
+ 'detached',
94
+ '',
95
+ ].join('\n');
96
+ const entries = parseWorktreePorcelain(text);
97
+ assert.deepEqual(entries, [
98
+ { path: '/work/demo', head: '1a2b3c4d5e', branch: 'main' },
99
+ { path: '/work/demo-worktrees/alpha', head: '5f6e7d8c9b', detached: true },
100
+ ]);
101
+ assert.deepEqual(parseWorktreePorcelain(text.replaceAll('\n', '\0')), entries, 'the -z spelling parses the same');
102
+ assert.deepEqual(parseWorktreePorcelain(''), []);
244
103
  });