pi-odd-worktree 2.1.0 → 3.0.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.
Files changed (160) hide show
  1. package/README.md +54 -319
  2. package/extensions/odd-worktree-tools.ts +4145 -2529
  3. package/lib/bash-activity.js +1164 -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 +1416 -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 +308 -14
  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 +1419 -0
  75. package/tools/bash-design.test.js +1091 -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 +610 -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 +1871 -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 +391 -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 +1512 -66
  121. package/tools/issues-open-guard.test.js +1096 -0
  122. package/tools/issues-section.test.js +4195 -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,1091 @@
1
+ 'use strict';
2
+ // The bash activity design: what a caller of the bridge's `registerToolDesign` observes.
3
+ const { test, describe } = require('node:test');
4
+ const assert = require('node:assert/strict');
5
+ const {
6
+ BASH_DESIGN_ID,
7
+ BASH_DESIGN_LABEL,
8
+ applyBashActivityHeading,
9
+ bashActivityDesign,
10
+ registerBashDesign,
11
+ seedBashDesignSelection,
12
+ } = require('../lib/bash-design.js');
13
+
14
+ const CALL = { tool: 'bash', part: 'call', width: 90 };
15
+ const strip = (row) => row.replace(/\u001b\[[0-9;]*m/g, '');
16
+ const cols = (row) => Array.from(strip(row)).length;
17
+ const textWidth = (text) => Array.from(text).length;
18
+
19
+ // The host's two frames, as the bridge hands them for a collapsed call.
20
+ const neon = (command, hint = 'ctrl+o to expand') => {
21
+ const used = `╭─ $ ${command} `.length + ` ${hint} ─╮`.length;
22
+ return `╭─ $ ${command} ${'─'.repeat(Math.max(2, 90 - used))} ${hint} ─╮`;
23
+ };
24
+ const float = (command) => ` $ ${command}${' '.repeat(Math.max(2, 90 - command.length - 4))}ctrl+o to expand`;
25
+
26
+ describe('the heading', () => {
27
+ test('a named command swaps the mark for its activity and keeps the row width', () => {
28
+ for (const [frame, mk] of [['neon', neon], ['float', float]]) {
29
+ const row = mk("sed -n '1,40p' lib/app.js");
30
+ const [out] = applyBashActivityHeading([row], CALL);
31
+ assert.match(out, /⌕ exploring/, frame);
32
+ assert.doesNotMatch(strip(out), /\$/, frame);
33
+ assert.equal(cols(out), cols(row), `${frame} keeps its width`);
34
+ assert.match(out, /sed -n '1,40p' lib\/app\.js/, 'the command is untouched');
35
+ assert.match(out, /ctrl\+o to expand/, 'the hint is untouched');
36
+ }
37
+ });
38
+
39
+ test('the other families get their own mark and word', () => {
40
+ const cases = [['node --test tools/*.test.js', '✓ checking'], ['rm -rf build', '⚠ destructive'], ['git commit -m wip', '⎇ versioning']];
41
+ for (const [command, heading] of cases) {
42
+ const [out] = applyBashActivityHeading([neon(command)], CALL);
43
+ assert.ok(strip(out).includes(heading), `${command} -> ${heading}`);
44
+ }
45
+ });
46
+
47
+ test('host paint around the mark survives', () => {
48
+ const row = `\u001b[36m╭─ \u001b[1m$ ls -la\u001b[22m ${'─'.repeat(30)} ctrl+o to expand ─╮\u001b[39m`;
49
+ const [out] = applyBashActivityHeading([row], CALL);
50
+ assert.ok(out.startsWith('\u001b[36m╭─ \u001b[1m⌕ exploring'));
51
+ assert.ok(out.endsWith('╮\u001b[39m'));
52
+ });
53
+
54
+ test('an unnamed command is headed `$ bash`, never an activity', () => {
55
+ for (const command of ['cd /tmp', 'frobnicate --now']) {
56
+ const [out] = applyBashActivityHeading([neon(command)], CALL);
57
+ assert.match(strip(out), new RegExp(`\\$ bash ${command}`));
58
+ }
59
+ });
60
+
61
+ test('a heading that does not prove the whole command is never named', () => {
62
+ const unproven = [
63
+ neon('sed -n 1,40p a.ts … +3 lines'),
64
+ neon('ls -la some/very/long/pa…'),
65
+ float('ls -la && rm -rf x…'),
66
+ ];
67
+ for (const row of unproven) {
68
+ assert.doesNotMatch(strip(applyBashActivityHeading([row], CALL)[0]), /exploring|destructive/, row);
69
+ }
70
+ // A continuation row means the heading is only the first line of the command.
71
+ const multi = [neon('ls'), '│ rm -rf build │'];
72
+ assert.doesNotMatch(strip(applyBashActivityHeading(multi, CALL)[0]), /exploring/);
73
+ });
74
+
75
+ test('text after the command that is not the host fill or hint is never named', () => {
76
+ const row = ' $ ls && rm -rf build' + ' '.repeat(20);
77
+ assert.doesNotMatch(strip(applyBashActivityHeading([row], CALL)[0]), /exploring|destructive/);
78
+ });
79
+
80
+ test('a row with no fill to pay for the word is left alone', () => {
81
+ const row = '$ ls -la';
82
+ assert.deepEqual(applyBashActivityHeading([row], CALL), [row]);
83
+ });
84
+
85
+ test('results, other tools and result-like rows are never rewritten', () => {
86
+ const row = neon('ls -la');
87
+ assert.deepEqual(applyBashActivityHeading([row], { ...CALL, part: 'result' }), [row]);
88
+ assert.deepEqual(applyBashActivityHeading([row], { ...CALL, tool: 'read' }), [row]);
89
+ const spacer = ['', neon('ls -la')];
90
+ assert.deepEqual(applyBashActivityHeading(spacer, CALL), spacer);
91
+ const body = ['│ $ ls -la │'];
92
+ assert.deepEqual(applyBashActivityHeading(body, CALL), body);
93
+ });
94
+
95
+ test('it never throws and never mutates its input', () => {
96
+ const input = Object.freeze([neon('ls -la')]);
97
+ assert.doesNotThrow(() => applyBashActivityHeading(input, CALL));
98
+ assert.doesNotThrow(() => applyBashActivityHeading(undefined, CALL));
99
+ assert.doesNotThrow(() => applyBashActivityHeading([null], CALL));
100
+ });
101
+ });
102
+
103
+ // A theme as Pi hands it: `fg(role, text)` wraps the text in that role's colour as SGR escapes
104
+ // (zero columns). One distinct code per role, so a test reads which role painted which cells.
105
+ const ROLE_CODE = { mdLink: 31, mdHeading: 32, error: 33, success: 34, warning: 35, syntaxFunction: 36, bashMode: 37, dim: 90 };
106
+ const theme = { fg: (role, text) => `\u001b[${ROLE_CODE[role]}m${text}\u001b[39m` };
107
+ const painted = (role, text) => `\u001b[${ROLE_CODE[role]}m${text}\u001b[39m`;
108
+ const THEMED = { ...CALL, theme };
109
+
110
+ describe('role colour', () => {
111
+ test('each family paints its mark and word in its own theme role', () => {
112
+ const cases = [
113
+ ["sed -n '1,40p' a.js", 'mdLink', '⌕ exploring'],
114
+ ['node --test tools/*.test.js', 'mdHeading', '✓ checking'],
115
+ ['rm -rf build', 'error', '⚠ destructive'],
116
+ ['git push origin main', 'success', '↑ publishing'],
117
+ ['git commit -m wip', 'warning', '⎇ versioning'],
118
+ ['prettier --write .', 'syntaxFunction', '≋ formatting'],
119
+ ];
120
+ for (const [command, role, heading] of cases) {
121
+ for (const mk of [neon, float]) {
122
+ const [out] = applyBashActivityHeading([mk(command)], THEMED);
123
+ assert.ok(out.includes(painted(role, heading)), `${command} -> ${role}`);
124
+ }
125
+ }
126
+ });
127
+
128
+ test('the colour changes no column and leaves the host paint after the mark in force', () => {
129
+ const row = `\u001b[36m╭─ \u001b[1m$ ls -la\u001b[22m ${'─'.repeat(30)} ctrl+o to expand ─╮\u001b[39m`;
130
+ const [out] = applyBashActivityHeading([row], THEMED);
131
+ assert.equal(cols(out), cols(row));
132
+ // After the role-painted label the host's own escapes that preceded the mark are re-issued.
133
+ assert.ok(out.includes(`${painted('mdLink', '⌕ exploring')}\u001b[36m\u001b[1m`), out);
134
+ assert.ok(out.endsWith('╮\u001b[39m'));
135
+ });
136
+
137
+ test('a theme that cannot paint falls back to the plain label', () => {
138
+ for (const bad of [undefined, null, {}, { fg: () => { throw new Error('x'); } }, { fg: () => 42 }]) {
139
+ const [out] = applyBashActivityHeading([neon('ls -la')], { ...CALL, theme: bad });
140
+ assert.ok(strip(out).includes('⌕ exploring'), String(bad));
141
+ assert.doesNotMatch(out, /\u001b\[31m/);
142
+ }
143
+ });
144
+ });
145
+
146
+ describe('the generic heading', () => {
147
+ test('a command nothing names is headed `$ bash` in the bashMode role, width kept', () => {
148
+ for (const mk of [neon, float]) {
149
+ for (const command of ['cd /tmp', 'frobnicate --now', 'node scripts/x.mjs']) {
150
+ const row = mk(command);
151
+ const [out] = applyBashActivityHeading([row], THEMED);
152
+ assert.ok(out.includes(painted('bashMode', '$ bash')), command);
153
+ assert.match(strip(out), new RegExp(command.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&')));
154
+ assert.equal(cols(out), cols(row), `${command} keeps its width`);
155
+ }
156
+ }
157
+ });
158
+
159
+ test('without a theme it still reads `$ bash` and keeps the width', () => {
160
+ const row = neon('frobnicate --now');
161
+ const [out] = applyBashActivityHeading([row], CALL);
162
+ assert.match(out, /\$ bash frobnicate --now/);
163
+ assert.equal(cols(out), cols(row));
164
+ });
165
+
166
+ test('a heading that hides part of the command is `$ bash`, never an activity', () => {
167
+ for (const row of [neon('sed -n 1,40p a.ts … +3 lines'), neon('ls -la some/very/long/pa…'), float('ls -la && rm -rf x…')]) {
168
+ const [out] = applyBashActivityHeading([row], THEMED);
169
+ assert.doesNotMatch(strip(out), /exploring|destructive/, row);
170
+ assert.match(strip(out), /\$ bash /, row);
171
+ assert.equal(cols(out), cols(row), row);
172
+ }
173
+ });
174
+
175
+ test('a multi-row call is headed `$ bash` on its first row and its other rows are untouched', () => {
176
+ const rows = [neon('ls'), '│ rm -rf build │'];
177
+ const out = applyBashActivityHeading(rows, THEMED);
178
+ assert.ok(out[0].includes(painted('bashMode', '$ bash')));
179
+ assert.equal(out[1], rows[1]);
180
+ });
181
+ });
182
+
183
+ // What the bridge hands a design about the call itself (`ctx.render`).
184
+ const render = (command, extra = {}) => ({ toolCallId: 'c1', args: { command }, isPartial: false, isError: false, ...extra });
185
+ const RESULT = { tool: 'bash', part: 'result', width: 90, theme };
186
+ const closing = (extra = '') => `╰${'─'.repeat(88 - extra.length)}${extra}╯`;
187
+ const sgr = (code, text) => `\u001b[${code}m${text}\u001b[39m`;
188
+
189
+ describe('the call, not the drawn heading', () => {
190
+ test('a command the host truncated is still named from its real arguments', () => {
191
+ const long = `grep -rn "needle" ${'src/'.repeat(30)} | head`;
192
+ const row = neon(`${long.slice(0, 50)}…`);
193
+ const [out] = applyBashActivityHeading([row], { ...THEMED, render: render(long) });
194
+ assert.ok(out.includes(painted('mdLink', '⌕ exploring')), strip(out));
195
+ assert.equal(cols(out), cols(row));
196
+ });
197
+
198
+ test('a multi-line or wrapped heading is named from its arguments too', () => {
199
+ const rows = [neon('cd /tmp'), '│ echo ---; grep -rn x . │'];
200
+ const out = applyBashActivityHeading(rows, { ...THEMED, render: render('cd /tmp && echo ---; grep -rn x .') });
201
+ assert.ok(out[0].includes(painted('mdLink', '⌕ exploring')));
202
+ assert.equal(out[1].replace(/\u001b\[[0-9;]*m/g, ''), rows[1]);
203
+ });
204
+
205
+ test('arguments the classifier cannot name keep `$ bash`, even when the heading looks nameable', () => {
206
+ const [out] = applyBashActivityHeading([neon('ls')], { ...THEMED, render: render('frobnicate --now') });
207
+ assert.ok(out.includes(painted('bashMode', '$ bash')));
208
+ assert.doesNotMatch(strip(out), /exploring/);
209
+ });
210
+
211
+ test('without arguments the drawn heading is still the evidence', () => {
212
+ const [out] = applyBashActivityHeading([neon('ls -la')], { ...THEMED, render: { isPartial: false } });
213
+ assert.ok(out.includes(painted('mdLink', '⌕ exploring')));
214
+ });
215
+ });
216
+
217
+ describe('room for the label', () => {
218
+ const LONG = `grep -rn "activity" ${'lib/'.repeat(14)} | head -40; echo ---; grep -rn x . -l`;
219
+ test('a long heading with almost no fill gives up the tail of the command, not the label', () => {
220
+ const row = neon(LONG.slice(0, 50) + '…');
221
+ for (const mk of [(c) => neon(c), (c) => float(c)]) {
222
+ const shown = mk(LONG.slice(0, 55) + '…');
223
+ const [out] = applyBashActivityHeading([shown], { ...THEMED, render: render(LONG) });
224
+ const text = strip(out);
225
+ assert.ok(text.includes('⌕ exploring'), text);
226
+ assert.equal(cols(out), cols(shown), 'the row keeps the width the host made it');
227
+ assert.ok(text.includes('grep -rn "activity"'), 'the start of the command survives');
228
+ assert.match(text, /…/, 'the cut is shown as an ellipsis');
229
+ assert.match(text, /ctrl\+o to expand/, 'the hint survives');
230
+ }
231
+ assert.ok(row.length > 0);
232
+ });
233
+
234
+ test('the timeout badge is never shortened: the command gives up the columns instead', () => {
235
+ // The collapsed float row with nothing left to pay with: two fill columns, and the badge the
236
+ // host drew after the command in its own colour is fixed call metadata, not command text.
237
+ const row = float(LONG.slice(0, 68) + '… (timeout 120s)');
238
+ const [out] = applyBashActivityHeading([row], { ...THEMED, render: render(LONG) });
239
+ const text = strip(out);
240
+ assert.ok(text.includes('⌕ exploring'), text);
241
+ assert.ok(text.includes('(timeout 120s)'), `the badge is whole: ${text}`);
242
+ assert.equal(cols(out), cols(row), 'the row keeps the width the host made it');
243
+ assert.match(text, /…/, 'the cut is shown as an ellipsis');
244
+ assert.match(text, /ctrl\+o to expand/, 'the hint survives');
245
+ assert.doesNotMatch(text.slice(text.indexOf('(timeout')), /…/, 'the badge carries no ellipsis');
246
+ const cut = text.slice(text.indexOf('⌕ exploring') + '⌕ exploring'.length, text.indexOf(' (timeout 120s)')).trim();
247
+ const full = strip(row).slice(strip(row).indexOf('$ ') + 2, strip(row).indexOf(' (timeout 120s)')).trim();
248
+ assert.ok(cut.length < full.length, `the command gave up the columns: ${cut} / ${full}`);
249
+ });
250
+
251
+ test('a row with no command left to give and a timeout badge is left exactly as it is', () => {
252
+ // Less command before the badge than the label needs, and no fill to pay with: the host's row,
253
+ // badge included, comes back untouched rather than with a clipped badge.
254
+ const row = ' $ ls -la … (timeout 120s) ctrl+o to expand';
255
+ assert.deepEqual(applyBashActivityHeading([row], { ...THEMED, render: render('ls -la') }), [row]);
256
+ });
257
+
258
+ test('a command shown in full that does not leave room is cut with an ellipsis', () => {
259
+ const cmd = `grep -rn "needle" ${'src/'.repeat(8)} | head -40`;
260
+ const row = neon(cmd);
261
+ const [out] = applyBashActivityHeading([row], { ...THEMED, render: render(cmd) });
262
+ const text = strip(out);
263
+ assert.ok(text.includes('⌕ exploring'), text);
264
+ assert.equal(cols(out), cols(row));
265
+ assert.match(text, /…/);
266
+ });
267
+
268
+ test('the generic `$ bash` also makes room the same way', () => {
269
+ const cmd = `./deploy.sh ${'--flag '.repeat(10)}`.trim();
270
+ const row = neon(cmd);
271
+ const [out] = applyBashActivityHeading([row], { ...THEMED, render: render(cmd) });
272
+ assert.ok(out.includes(painted('bashMode', '$ bash')), strip(out));
273
+ assert.equal(cols(out), cols(row));
274
+ });
275
+
276
+ test('when even the cut would leave too little of the command the row is left alone', () => {
277
+ const row = '$ ls -la';
278
+ assert.deepEqual(applyBashActivityHeading([row], { ...THEMED, render: render('ls -la') }), [row]);
279
+ });
280
+
281
+ test('the colour is applied to the label, never to the cut command text', () => {
282
+ const [out] = applyBashActivityHeading([neon(LONG.slice(0, 55) + '…')], { ...THEMED, render: render(LONG) });
283
+ assert.ok(out.includes(painted('mdLink', '⌕ exploring')));
284
+ assert.ok(!out.includes(painted('mdLink', '…')), 'only the frame and the label are painted');
285
+ });
286
+ });
287
+
288
+ describe('escapes the design re-issues', () => {
289
+ test('a host row with no paint gets no empty open/close pairs from the design', () => {
290
+ const [out] = applyBashActivityHeading([neon('ls -la')], { ...THEMED, render: render('ls -la') });
291
+ assert.doesNotMatch(out, /\u001b\[[0-9;]*m\u001b\[39m/);
292
+ });
293
+
294
+ test('the host paint in force is issued once after each thing the design paints, never the design\'s own', () => {
295
+ const row = `\u001b[36m╭─ \u001b[1m$ ls -la\u001b[22m ${'─'.repeat(30)} ctrl+o to expand ─╮\u001b[39m`;
296
+ const [out] = applyBashActivityHeading([row], { ...THEMED, render: render('ls -la') });
297
+ // Nothing the design painted (a role colour) is ever re-issued as if it were the host's.
298
+ for (const code of [31, 37]) assert.equal((out.match(new RegExp(`\\u001b\\[${code}m`, 'g')) ?? []).length, (out.match(new RegExp(`\\u001b\\[${code}m`, 'g')) ?? []).length);
299
+ const stray = out.replace(/\u001b\[(?:3[1-7]|1|22|39)m/g, '').replace(/[^\u001b]/g, '');
300
+ assert.equal(stray, '', 'only the host and role SGRs appear');
301
+ assert.equal(cols(out), cols(row));
302
+ });
303
+
304
+ test('state tracking: a host reset or colour change before the run is honoured', () => {
305
+ const row = `\u001b[36m╭─ \u001b[39m\u001b[32m$ ls -la\u001b[39m ${'─'.repeat(30)} ctrl+o to expand ─╮`;
306
+ const [out] = applyBashActivityHeading([row], { ...THEMED, render: render('ls -la') });
307
+ assert.equal(cols(out), cols(row));
308
+ // Right after the label the host's green (32) is back in force; the cyan it had closed is not.
309
+ const afterLabel = out.slice(out.indexOf('⌕ exploring') + '⌕ exploring'.length);
310
+ assert.ok(afterLabel.startsWith('\u001b[39m\u001b[32m') || afterLabel.startsWith('\u001b[32m'), JSON.stringify(afterLabel.slice(0, 20)));
311
+ assert.ok(!afterLabel.startsWith('\u001b[36m'));
312
+ });
313
+ });
314
+
315
+ describe('the frame', () => {
316
+ const FRAME = ['╭─ ⌕ exploring ls ───────────── ctrl+o to expand ─╮', '│ out │', closing()];
317
+ test('an idle card paints every frame cell in the activity role, text untouched', () => {
318
+ const out = applyBashActivityHeading(FRAME.slice(1), { ...RESULT, render: render('ls -la') });
319
+ assert.ok(out[0].startsWith(sgr(31, '│')), out[0]);
320
+ assert.ok(out[0].includes('out'), 'body text is not repainted');
321
+ assert.ok(out[1].startsWith('\u001b[31m╰'), out[1]);
322
+ assert.ok(out[1].includes('╯\u001b[39m'), out[1]);
323
+ assert.equal(cols(out[1]), cols(FRAME[2]));
324
+ });
325
+
326
+ test('the top row of the call paints the corners and rules, and the label keeps its own role', () => {
327
+ const [out] = applyBashActivityHeading([neon('ls -la')], { ...THEMED, render: render('ls -la') });
328
+ assert.ok(out.startsWith('\u001b[31m╭─'), out);
329
+ assert.ok(out.includes(painted('mdLink', '⌕ exploring')));
330
+ assert.ok(out.includes('─╮\u001b[39m'), out);
331
+ });
332
+
333
+ test('a running call owns the whole frame in warning, a failed one in error, and the label keeps its activity', () => {
334
+ for (const [extra, role, code] of [[{ isPartial: true }, 'warning', 35], [{ isError: true }, 'error', 33]]) {
335
+ const [out] = applyBashActivityHeading([neon('ls -la')], { ...THEMED, render: render('ls -la', extra) });
336
+ assert.ok(out.startsWith(`\u001b[${code}m╭─`), role);
337
+ assert.ok(out.includes(painted('mdLink', '⌕ exploring')), `${role}: the label keeps its activity colour`);
338
+ }
339
+ });
340
+
341
+ test('a generic call paints its frame in the bashMode role', () => {
342
+ const [out] = applyBashActivityHeading([closing()], { ...RESULT, render: render('frobnicate') });
343
+ assert.ok(out.startsWith('\u001b[37m╰'), out);
344
+ });
345
+
346
+ test('the float bar is painted and the row keeps its width', () => {
347
+ const row = `▎ out${' '.repeat(80)}`;
348
+ const [out] = applyBashActivityHeading([row], { ...RESULT, render: render('ls') });
349
+ assert.ok(out.startsWith(sgr(31, '▎')), out);
350
+ assert.equal(cols(out), cols(row));
351
+ });
352
+
353
+ test('no theme, or no render context: frame bytes are the host\'s', () => {
354
+ const rows = [...FRAME];
355
+ assert.deepEqual(applyBashActivityHeading(rows.slice(1), { tool: 'bash', part: 'result', width: 90, render: render('ls') }), rows.slice(1));
356
+ assert.deepEqual(applyBashActivityHeading(rows.slice(1), { ...RESULT }), rows.slice(1));
357
+ });
358
+ });
359
+
360
+ describe('elapsed time on the closing rule', () => {
361
+ test('neon: a finished call shows its duration right-aligned against the corner, width kept', () => {
362
+ const [out] = applyBashActivityHeading([closing()], { ...RESULT, render: render('ls', { durationMs: 142 }) });
363
+ assert.match(strip(out), /^╰─+ 142ms ╯$/);
364
+ assert.equal(cols(out), 90);
365
+ assert.ok(out.includes(sgr(90, '142ms')), 'the duration is dim, not in the frame role');
366
+ });
367
+
368
+ test('seconds read with one decimal', () => {
369
+ const [out] = applyBashActivityHeading([closing()], { ...RESULT, render: render('ls', { durationMs: 1234 }) });
370
+ assert.match(strip(out), / 1\.2s ╯$/);
371
+ });
372
+
373
+ test('float: the duration rides the bottom row after the bar', () => {
374
+ const row = `▎${' '.repeat(89)}`;
375
+ const [out] = applyBashActivityHeading([row], { ...RESULT, render: render('ls', { durationMs: 87 }) });
376
+ assert.match(strip(out), /^▎ +87ms $/);
377
+ assert.equal(cols(out), 90);
378
+ });
379
+
380
+ test('never while running, never without a duration, never when there is no room, never on the call half', () => {
381
+ const none = (extra, ctx = {}) => strip(applyBashActivityHeading([closing()], { ...RESULT, ...ctx, render: render('ls', extra) })[0]);
382
+ assert.doesNotMatch(none({ isPartial: true, durationMs: 5 }), /ms/);
383
+ assert.doesNotMatch(none({}), /ms/);
384
+ assert.doesNotMatch(none({ durationMs: Number.NaN }), /ms/);
385
+ assert.doesNotMatch(none({ durationMs: -1 }), /ms/);
386
+ const tight = '╰──╯';
387
+ const [narrow] = applyBashActivityHeading([tight], { ...RESULT, width: 4, render: render('ls', { durationMs: 5 }) });
388
+ assert.doesNotMatch(strip(narrow), /ms/);
389
+ assert.doesNotMatch(strip(applyBashActivityHeading([closing()], { ...CALL, theme, render: render('ls', { durationMs: 5 }) })[0]), /ms/);
390
+ });
391
+
392
+ test('a body row that only looks like a closing rule inside the output is not given a duration', () => {
393
+ const rows = ['│ ╰───╯ │', closing()];
394
+ const out = applyBashActivityHeading(rows, { ...RESULT, render: render('ls', { durationMs: 9 }) });
395
+ assert.doesNotMatch(strip(out[0]), /ms/);
396
+ assert.match(strip(out[1]), /9ms/);
397
+ });
398
+ });
399
+
400
+ describe('repeated frames', () => {
401
+ const ctxFor = (command, extra = {}) => ({ ...THEMED, render: render(command, extra) });
402
+ const row = neon('grep -rn needle src | head -40');
403
+
404
+ test('the same call drawn twice is the same rows, as a fresh array each time', () => {
405
+ const first = applyBashActivityHeading([row], ctxFor('grep -rn needle src | head -40'));
406
+ const second = applyBashActivityHeading([row], ctxFor('grep -rn needle src | head -40'));
407
+ assert.deepEqual(second, first);
408
+ assert.notEqual(second, first, 'a caller may keep or change the array it gets');
409
+ first[0] = 'changed by the caller';
410
+ assert.notEqual(applyBashActivityHeading([row], ctxFor('grep -rn needle src | head -40'))[0], 'changed by the caller');
411
+ });
412
+
413
+ test('anything the drawing depends on changes the answer, never a stale one', () => {
414
+ const base = applyBashActivityHeading([closing()], { ...RESULT, render: render('ls', { durationMs: 100 }) })[0];
415
+ assert.notEqual(applyBashActivityHeading([closing()], { ...RESULT, render: render('ls', { durationMs: 200 }) })[0], base, 'duration');
416
+ assert.notEqual(applyBashActivityHeading([closing()], { ...RESULT, render: render('ls', { durationMs: 100, isError: true }) })[0], base, 'error');
417
+ assert.notEqual(applyBashActivityHeading([closing()], { ...RESULT, render: render('ls', { durationMs: 100, isPartial: true }) })[0], base, 'running');
418
+ assert.notEqual(applyBashActivityHeading([closing()], { ...RESULT, render: render('rm -rf x', { durationMs: 100 }) })[0], base, 'family');
419
+ assert.notEqual(applyBashActivityHeading([closing().replace('─', '━')], { ...RESULT, render: render('ls', { durationMs: 100 }) })[0], base, 'rows');
420
+ });
421
+
422
+ test('a theme switched under the same object repaints with the new colours', () => {
423
+ const live = { palette: 31, fg(role, text) { return `\u001b[${this.palette}m${text}\u001b[39m`; } };
424
+ const ctx = { tool: 'bash', part: 'call', width: 90, theme: live, render: render('ls -la') };
425
+ const before = applyBashActivityHeading([neon('ls -la')], ctx)[0];
426
+ live.palette = 34;
427
+ const after = applyBashActivityHeading([neon('ls -la')], ctx)[0];
428
+ assert.notEqual(after, before);
429
+ assert.ok(after.includes('\u001b[34m'));
430
+ });
431
+
432
+ test('a long session of repeated cards stays cheap', () => {
433
+ const ctx = ctxFor('grep -rn needle src | head -40');
434
+ const start = performance.now();
435
+ for (let index = 0; index < 30000; index += 1) applyBashActivityHeading([row], ctx);
436
+ const elapsed = performance.now() - start;
437
+ assert.ok(elapsed < 1000, `30000 frames took ${elapsed}ms`);
438
+ });
439
+
440
+ test('the cache is bounded: many different commands never grow it without limit', () => {
441
+ for (let index = 0; index < 3000; index += 1) {
442
+ assert.doesNotThrow(() => applyBashActivityHeading([neon(`ls -la dir${index}`)], ctxFor(`ls -la dir${index}`)));
443
+ }
444
+ });
445
+ });
446
+
447
+ describe('choosing the shape', () => {
448
+ const fake = (version, acceptsLine) => {
449
+ const calls = [];
450
+ const disposed = [];
451
+ return {
452
+ calls,
453
+ disposed,
454
+ api: {
455
+ VERSION: version,
456
+ registerToolDesign: (design) => {
457
+ calls.push(design);
458
+ if (Array.isArray(design.tools) && !acceptsLine) throw new Error('refused: tool is required');
459
+ const dispose = () => disposed.push(design.id ?? design.tool);
460
+ return dispose;
461
+ },
462
+ },
463
+ };
464
+ };
465
+
466
+ test('a readable release decides alone: no probe is ever registered', () => {
467
+ for (const [version, grouped] of [['0.6.0', true], ['0.7.1', true], ['1.0.0', true], ['0.5.9', false], ['0.5.0', false]]) {
468
+ const { api, calls } = fake(version, true);
469
+ registerBashDesign(api)();
470
+ assert.equal(calls.length, 1, version);
471
+ assert.equal(Array.isArray(calls[0].tools), grouped, version);
472
+ }
473
+ });
474
+
475
+ test('a release string that cannot be read is asked instead, and the probe is disposed', () => {
476
+ for (const version of ['nightly', '', 'v0.6.0', 'six.zero', undefined, null, 6]) {
477
+ const accepting = fake(version, true);
478
+ const disposeProbe = registerBashDesign(accepting.api);
479
+ assert.equal(typeof disposeProbe, 'function', String(version));
480
+ disposeProbe();
481
+ assert.equal(accepting.calls.length, 2, `${String(version)}: probe then ours`);
482
+ assert.equal(accepting.calls[0].id, 'odd-worktree.capability-probe');
483
+ assert.equal(Array.isArray(accepting.calls[1].tools), true, String(version));
484
+ assert.equal(accepting.disposed[0], 'odd-worktree.capability-probe', String(version));
485
+ }
486
+ });
487
+
488
+ test('a bridge that refuses the line is probed once and keeps the per-tool shape', () => {
489
+ const refusing = fake('nightly', false);
490
+ const dispose = registerBashDesign(refusing.api);
491
+ assert.equal(typeof dispose, 'function');
492
+ dispose();
493
+ assert.equal(refusing.calls.at(-1).tool, 'bash', 'the probe threw, so the old shape is registered');
494
+ assert.equal(Array.isArray(refusing.calls.at(-1).tools), false);
495
+ assert.ok(!refusing.disposed.includes('odd-worktree.capability-probe'), 'a refused probe has nothing to dispose');
496
+ });
497
+
498
+ test('the probe is never selected, never labelled and never named', () => {
499
+ const accepting = fake('nightly', true);
500
+ registerBashDesign(accepting.api)();
501
+ const probe = accepting.calls[0];
502
+ assert.equal(probe.label, 'probe');
503
+ assert.equal(probe.name, undefined, 'it must not claim a plugin line');
504
+ assert.notEqual(probe.id, BASH_DESIGN_ID);
505
+ assert.deepEqual(probe.tools, ['odd-worktree-probe']);
506
+ assert.equal(probe.line, 'odd-worktree-probe');
507
+ });
508
+ });
509
+
510
+ describe('the registration', () => {
511
+ test('is one bash design with the label the bridge line shows and a preview', () => {
512
+ const design = bashActivityDesign();
513
+ assert.equal(design.tool, 'bash');
514
+ assert.equal(design.id, BASH_DESIGN_ID);
515
+ assert.equal(design.label, BASH_DESIGN_LABEL);
516
+ assert.equal(BASH_DESIGN_LABEL, 'activity');
517
+ const preview = design.preview();
518
+ assert.equal(preview.title, 'Cards · activity');
519
+ // One line like every other Cards row, and one that fits a narrow panel.
520
+ assert.equal(preview.sample.split('\n').length, 1);
521
+ assert.ok(textWidth(preview.sample) <= 72, preview.sample);
522
+ for (const word of ['exploring', 'checking', 'versioning', 'publishing', 'destructive']) {
523
+ assert.ok(preview.sample.includes(word), word);
524
+ }
525
+ preview.title = 'changed';
526
+ assert.equal(design.preview().title, 'Cards · activity');
527
+ });
528
+
529
+ test('registers once per process, returns an idempotent disposer, and tolerates a bridge without the API', () => {
530
+ const seen = [];
531
+ const api = { registerToolDesign: (d) => { seen.push(d); return () => seen.pop(); } };
532
+ assert.equal(registerBashDesign({}), null);
533
+ assert.equal(registerBashDesign(null), null);
534
+ const dispose = registerBashDesign(api);
535
+ assert.equal(typeof dispose, 'function');
536
+ assert.equal(registerBashDesign(api), dispose, 'a second call is the same registration');
537
+ assert.equal(seen.length, 1);
538
+ dispose();
539
+ dispose();
540
+ assert.equal(seen.length, 0);
541
+ assert.equal(registerBashDesign({ registerToolDesign: () => { throw new Error('refused'); } }), null);
542
+ });
543
+ });
544
+
545
+ // A bridge whose `createStore` keeps one in-memory file per plugin id, like the real one.
546
+ function fakeBridge(files = {}) {
547
+ const writes = [];
548
+ const createStore = (id) => ({
549
+ get: (key, fallback) => (files[id] && Object.hasOwn(files[id], key) ? files[id][key] : fallback),
550
+ set: (key, value) => { writes.push([id, key]); files[id] = { ...files[id], [key]: value }; },
551
+ });
552
+ return { api: { registerToolDesign: () => () => {}, createStore }, files, writes };
553
+ }
554
+ const SELECTIONS = ['bridge-tool-designs', 'selections'];
555
+
556
+ describe('the first-run selection', () => {
557
+ test('a first load with no choice selects activity for bash and keeps the other tools', () => {
558
+ const { api, files } = fakeBridge({ 'bridge-tool-designs': { selections: { write: 'x.write' } } });
559
+ assert.equal(seedBashDesignSelection(api), true);
560
+ assert.deepEqual(files['bridge-tool-designs'].selections, { write: 'x.write', bash: BASH_DESIGN_ID });
561
+ });
562
+
563
+ test('a fresh install with no store file at all selects activity', () => {
564
+ const { api, files } = fakeBridge();
565
+ assert.equal(seedBashDesignSelection(api), true);
566
+ assert.deepEqual(files['bridge-tool-designs'].selections, { bash: BASH_DESIGN_ID });
567
+ });
568
+
569
+ test('a choice the user already made is never overwritten, native included', () => {
570
+ for (const chosen of ['native', 'someone.else']) {
571
+ const { api, files } = fakeBridge({ 'bridge-tool-designs': { selections: { bash: chosen } } });
572
+ assert.equal(seedBashDesignSelection(api), false, chosen);
573
+ assert.equal(files['bridge-tool-designs'].selections.bash, chosen);
574
+ }
575
+ });
576
+
577
+ test('it seeds once: native chosen afterwards stays native on the next load', () => {
578
+ const { api, files } = fakeBridge();
579
+ assert.equal(seedBashDesignSelection(api), true);
580
+ files['bridge-tool-designs'] = { selections: { bash: 'native' } };
581
+ assert.equal(seedBashDesignSelection(api), false);
582
+ // Even with the selection removed entirely, the marker says it was already offered.
583
+ files['bridge-tool-designs'] = { selections: {} };
584
+ assert.equal(seedBashDesignSelection(api), false);
585
+ assert.deepEqual(files['bridge-tool-designs'].selections, {});
586
+ });
587
+
588
+ test('a bridge without createStore, a malformed file or a failing store seeds nothing and never throws', () => {
589
+ assert.equal(seedBashDesignSelection({}), false);
590
+ assert.equal(seedBashDesignSelection(null), false);
591
+ const malformed = fakeBridge({ 'bridge-tool-designs': { selections: 'oops' } });
592
+ assert.doesNotThrow(() => seedBashDesignSelection(malformed.api));
593
+ const failing = { createStore: () => ({ get: (_k, f) => f, set: () => { throw new Error('disk full'); } }) };
594
+ assert.equal(seedBashDesignSelection(failing), false);
595
+ });
596
+
597
+ test('a failed write leaves no marker, so the next load tries again', () => {
598
+ let fail = true;
599
+ const files = {};
600
+ const createStore = (id) => ({
601
+ get: (key, fallback) => (files[id] && Object.hasOwn(files[id], key) ? files[id][key] : fallback),
602
+ set: (key, value) => { if (fail && id === SELECTIONS[0]) throw new Error('disk full'); files[id] = { ...files[id], [key]: value }; },
603
+ });
604
+ assert.equal(seedBashDesignSelection({ createStore }), false);
605
+ fail = false;
606
+ assert.equal(seedBashDesignSelection({ createStore }), true);
607
+ assert.equal(files['bridge-tool-designs'].selections.bash, BASH_DESIGN_ID);
608
+ });
609
+
610
+ test('registering the design on a bridge seeds it, and a refused registration registers nothing', () => {
611
+ const accepted = fakeBridge();
612
+ const dispose = registerBashDesign(accepted.api);
613
+ assert.equal(typeof dispose, 'function');
614
+ assert.equal(accepted.files['bridge-tool-designs'].selections.bash, BASH_DESIGN_ID);
615
+ dispose();
616
+ // The seed runs BEFORE the registration (so a 0.6.0 bridge inherits it), which means a
617
+ // refusal can only arrive after the choice is stored: the bridge then holds a value whose
618
+ // design is not registered, which its own report calls `missing` and draws as the host's card.
619
+ const refused = fakeBridge();
620
+ refused.api.registerToolDesign = () => { throw new Error('refused'); };
621
+ assert.equal(registerBashDesign(refused.api), null);
622
+ assert.equal(refused.files['bridge-tool-designs'].selections.bash, BASH_DESIGN_ID);
623
+ });
624
+ });
625
+
626
+ // The read-only SHELL tools the one `activity` value heads. The host heads their cards
627
+ // `<glyph> <tool> <arguments>`; every one of them is a shell command that only reads, so the word is
628
+ // always the same. `read` is on the same line for coverage alone — the question the file card asks
629
+ // the bridge — and this design paints nothing for it: a read is the file card's row or the host's.
630
+ const READ_GLYPH = { grep: '⌕', find: '⌖', ls: '☷', read: '≡' };
631
+ const READ_ARGS = {
632
+ grep: '/needle/ in src/app.js',
633
+ find: '*.test.js in tools',
634
+ ls: 'src/lib',
635
+ read: 'lib/bash-design.js:1-40',
636
+ };
637
+ const READ_TOOLS = ['grep', 'find', 'ls'];
638
+ const escapeRe = (text) => text.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&');
639
+ const toolHead = (tool, args) => `${READ_GLYPH[tool]} ${tool} ${args}`;
640
+
641
+ // The host's two frames, with a read-only tool's heading instead of bash's `$`.
642
+ const readNeon = (tool, args = READ_ARGS[tool], hint = 'ctrl+o to expand') => {
643
+ const head = toolHead(tool, args);
644
+ const fill = Math.max(2, 90 - (3 + head.length + 1 + hint.length + 4));
645
+ return `╭─ ${head} ${'─'.repeat(fill)} ${hint} ─╮`;
646
+ };
647
+ const readFloat = (tool, args = READ_ARGS[tool]) => {
648
+ const head = toolHead(tool, args);
649
+ return ` ${head}${' '.repeat(Math.max(2, 90 - head.length - 4))}ctrl+o to expand`;
650
+ };
651
+ const readCtx = (tool, extra = {}) => ({ tool, part: 'call', width: 90, theme, ...extra });
652
+ const readArgs = () => ({ pattern: 'needle', path: 'src' });
653
+
654
+ describe('the read-only tools', () => {
655
+ test('every one of them is headed `⌕ exploring` in the mdLink role, width and arguments kept', () => {
656
+ for (const tool of READ_TOOLS) {
657
+ for (const [frame, mk] of [['neon', readNeon], ['float', readFloat]]) {
658
+ const row = mk(tool);
659
+ const [out] = applyBashActivityHeading([row], readCtx(tool));
660
+ assert.ok(out.includes(painted('mdLink', '⌕ exploring')), `${tool}/${frame}: ${strip(out)}`);
661
+ assert.equal(cols(out), cols(row), `${tool}/${frame} keeps its width`);
662
+ // The glyph and the tool's own word are gone; what it reads is untouched.
663
+ assert.doesNotMatch(strip(out), new RegExp(escapeRe(`${READ_GLYPH[tool]} ${tool}`)), `${tool}: own heading gone`);
664
+ assert.ok(strip(out).includes(`⌕ exploring ${READ_ARGS[tool]}`), `${tool}/${frame}: ${strip(out)}`);
665
+ assert.match(strip(out), /ctrl\+o to expand/, `${tool}/${frame}: the hint is untouched`);
666
+ }
667
+ }
668
+ });
669
+
670
+ test('a read is not a shell command, so its card is handed back exactly as the host drew it', () => {
671
+ // `exploring` names what a command does, and a read runs no command: the design paints nothing
672
+ // for it, so the host's own `≡ read …` heading, its colour and its width are whole.
673
+ const rows = [
674
+ ` ≡ read ~/dev/projects/x/lib/bash-design.js:261-650${' '.repeat(20)}ctrl+o to expand`,
675
+ `\u001b[36m╭─ \u001b[1m≡ read lib/bash-design.js:261-650\u001b[22m ${'─'.repeat(30)} ctrl+o to expand ─╮\u001b[39m`,
676
+ ];
677
+ const render = { args: { path: '~/dev/projects/x/lib/bash-design.js', offset: 261, limit: 390 }, isPartial: false, durationMs: 142 };
678
+ for (const row of rows) {
679
+ const out = applyBashActivityHeading([row], readCtx('read', { render }));
680
+ assert.deepEqual(out, [row]);
681
+ assert.doesNotMatch(strip(out[0]), /⌕|exploring/);
682
+ assert.ok(!out[0].includes(painted('mdLink', '⌕ exploring')), 'no colour was added');
683
+ assert.equal(cols(out[0]), cols(row), 'and no width changed');
684
+ }
685
+ });
686
+
687
+ test('the host paint in force where the arguments begin is re-issued after the label', () => {
688
+ for (const tool of READ_TOOLS) {
689
+ const row = `\u001b[36m╭─ \u001b[1m${READ_GLYPH[tool]} ${tool} /needle/ in src\u001b[22m ${'─'.repeat(30)} ctrl+o to expand ─╮\u001b[39m`;
690
+ const [out] = applyBashActivityHeading([row], readCtx(tool));
691
+ assert.ok(out.startsWith('\u001b[36m╭─ \u001b[1m'), tool);
692
+ assert.ok(out.includes(`${painted('mdLink', '⌕ exploring')}\u001b[36m\u001b[1m`), `${tool}: ${out}`);
693
+ assert.ok(out.endsWith('╮\u001b[39m'), tool);
694
+ assert.equal(cols(out), cols(row), tool);
695
+ }
696
+ });
697
+
698
+ test('a tool the line does not cover is returned byte for byte', () => {
699
+ for (const tool of ['edit', 'write', 'web_search', 'bash', '']) {
700
+ const rows = [`╭─ ✎ edit lib/a.js ${'─'.repeat(40)} ctrl+o to expand ─╮`];
701
+ assert.deepEqual(applyBashActivityHeading(rows, readCtx(tool, { render: { args: { path: 'lib/a.js' } } })), rows, tool);
702
+ }
703
+ });
704
+
705
+ test('a heading the host wrapped or cut is still headed when the bridge hands the arguments', () => {
706
+ for (const tool of READ_TOOLS) {
707
+ // Wrapped: the heading ends where the host ran out of rule, the rest continues below.
708
+ const wrapped = [`╭─ ${toolHead(tool, '/needle/ in a/very')} ╮`, '│ long/path │'];
709
+ const out = applyBashActivityHeading(wrapped, readCtx(tool, { render: { args: readArgs() } }));
710
+ const head = out[0];
711
+ assert.ok(head.includes(painted('mdLink', '⌕ exploring')), `${tool}: ${head}`);
712
+ assert.equal(cols(head), cols(wrapped[0]), `${tool} keeps its width`);
713
+ assert.equal(strip(out[1]), wrapped[1], `${tool}: the continuation row's text is untouched`);
714
+ // Abbreviated by the host: still the same constant word.
715
+ const cut = [readNeon(tool, '/needle/ in some/lo…')];
716
+ assert.ok(applyBashActivityHeading(cut, readCtx(tool, { render: { args: readArgs() } }))[0].includes(painted('mdLink', '⌕ exploring')), tool);
717
+ }
718
+ });
719
+
720
+ test('without the arguments the drawn heading must prove itself, and a read-only tool never guesses', () => {
721
+ for (const tool of READ_TOOLS) {
722
+ const unproven = [readNeon(tool, '/needle/ in some/lo…')];
723
+ assert.deepEqual(applyBashActivityHeading(unproven, readCtx(tool)), unproven, tool);
724
+ const wrapped = [`╭─ ${toolHead(tool, '/needle/ in a/very')} ╮`, '│ long/path │'];
725
+ assert.deepEqual(applyBashActivityHeading(wrapped, readCtx(tool)), wrapped, tool);
726
+ // An old bridge that hands no arguments leaves a provable heading alone only for its own row.
727
+ const proven = [readNeon(tool)];
728
+ assert.ok(applyBashActivityHeading(proven, readCtx(tool))[0].includes(painted('mdLink', '⌕ exploring')), tool);
729
+ }
730
+ });
731
+
732
+ test('a row with no room to pay for the word is left as the host drew it', () => {
733
+ for (const tool of READ_TOOLS) {
734
+ const row = `${READ_GLYPH[tool]} ${tool} /needle/`;
735
+ assert.deepEqual(applyBashActivityHeading([row], readCtx(tool, { render: { args: readArgs() } })), [row], tool);
736
+ }
737
+ });
738
+
739
+ test('S6: a read is the file card\'s row or the host\'s, for both parts, byte for byte', () => {
740
+ // The design paints nothing for a read — it declares it for coverage alone — so every read row
741
+ // is the host's own, or the file card's for a skill or an ODD artifact (lib/skill-odd-card.js):
742
+ // it draws the heading, the frame and the body, so no status line of this design lands over or
743
+ // under it, whichever resolver the host runs first.
744
+ const call = [readNeon('read', 'lib/skills/one/SKILL.md:1-40')];
745
+ const result = [`│ ${'x'.repeat(86)} │`, `╰${'─'.repeat(88)}╯`];
746
+ const paths = [
747
+ 'lib/skills/one/SKILL.md', 'odd/tasks/design.md', '/abs/odd/tasks/a.md', 'C:\\repo\\skills\\x\\SKILL.md',
748
+ 'src/app.js', 'lib/skillset/notes.md', 'docs/oddity.md', 'odd/work/a.md', 'notes/SKILL.md',
749
+ ];
750
+ for (const path of paths) {
751
+ const render = { args: { path }, isPartial: false, durationMs: 142 };
752
+ assert.deepEqual(applyBashActivityHeading(call, readCtx('read', { render })), call, `${path} (call)`);
753
+ assert.deepEqual(applyBashActivityHeading(result, readCtx('read', { render, part: 'result' })), result, `${path} (result)`);
754
+ }
755
+ // The path rule is the file card's own, and so is the list of tools it claims: only `read`,
756
+ // `edit` and `write` can be that card's row, so a `grep` or an `ls` that names a skill or an ODD
757
+ // artifact is still this design's to head and frame.
758
+ const grep = [readNeon('grep', '/needle/ in odd/tasks/design.md')];
759
+ const grepRender = (path) => ({ args: { pattern: 'needle', path }, isPartial: false });
760
+ assert.ok(
761
+ applyBashActivityHeading(grep, readCtx('grep', { render: grepRender('odd/tasks/design.md') }))[0].includes(painted('mdLink', '⌕ exploring')),
762
+ 'a covered tool keeps its row: the file card claims no grep',
763
+ );
764
+ assert.ok(applyBashActivityHeading(grep, readCtx('grep', { render: grepRender('src/app.js') }))[0].includes(painted('mdLink', '⌕ exploring')), 'an ordinary grep is still headed');
765
+ });
766
+
767
+ test('the frame wears one role and the call state speaks first, exactly as bash', () => {
768
+ const closing = `╰${'─'.repeat(88)}╯`;
769
+ const result = (extra) => ({ tool: 'grep', part: 'result', width: 90, theme, render: { args: readArgs(), isPartial: false, ...extra } });
770
+ assert.ok(applyBashActivityHeading([closing], result({}))[0].startsWith(`\u001b[${ROLE_CODE.mdLink}m╰`), 'idle: mdLink');
771
+ assert.ok(applyBashActivityHeading([closing], result({ isPartial: true }))[0].startsWith(`\u001b[${ROLE_CODE.warning}m╰`), 'running: warning');
772
+ assert.ok(applyBashActivityHeading([closing], result({ isError: true }))[0].startsWith(`\u001b[${ROLE_CODE.error}m╰`), 'failed: error');
773
+ const timed = applyBashActivityHeading([closing], result({ durationMs: 142 }))[0];
774
+ assert.match(strip(timed), / 142ms ╯$/);
775
+ assert.ok(timed.includes(painted('dim', '142ms')));
776
+ assert.equal(cols(timed), 90);
777
+ });
778
+
779
+ test('never while the call runs, and never without a duration', () => {
780
+ const closing = `╰${'─'.repeat(88)}╯`;
781
+ const result = (extra) => ({ tool: 'ls', part: 'result', width: 90, theme, render: { args: { path: 'src' }, isPartial: false, ...extra } });
782
+ assert.doesNotMatch(strip(applyBashActivityHeading([closing], result({ isPartial: true, durationMs: 5 }))[0]), /ms/);
783
+ assert.doesNotMatch(strip(applyBashActivityHeading([closing], result({}))[0]), /ms/);
784
+ });
785
+
786
+ test('no theme: the word is there unpainted and the row keeps its width', () => {
787
+ for (const tool of READ_TOOLS) {
788
+ const row = readNeon(tool);
789
+ const [out] = applyBashActivityHeading([row], { tool, part: 'call', width: 90, render: { args: readArgs() } });
790
+ assert.ok(strip(out).includes('⌕ exploring'), tool);
791
+ assert.doesNotMatch(out, /\u001b\[3[1-7]m/, tool);
792
+ assert.equal(cols(out), cols(row), tool);
793
+ }
794
+ });
795
+
796
+ test('no render context: the frame rows come back byte for byte', () => {
797
+ for (const tool of READ_TOOLS) {
798
+ const rows = [`╰${'─'.repeat(88)}╯`];
799
+ assert.deepEqual(applyBashActivityHeading(rows, { tool, part: 'result', width: 90, theme }), rows, tool);
800
+ }
801
+ });
802
+
803
+ test('a repeated read-only card is a cache hit, not a second drawing every frame', () => {
804
+ const row = readNeon('grep', '/needle/ in src/app.js');
805
+ const ctx = readCtx('grep', { render: { args: readArgs(), isPartial: false } });
806
+ const first = applyBashActivityHeading([row], ctx);
807
+ const second = applyBashActivityHeading([row], ctx);
808
+ assert.deepEqual(second, first);
809
+ assert.notEqual(second, first, 'a caller may keep or change the array it gets');
810
+ const start = performance.now();
811
+ for (let index = 0; index < 30000; index += 1) applyBashActivityHeading([row], ctx);
812
+ const elapsed = performance.now() - start;
813
+ assert.ok(elapsed < 1000, `30000 grep frames took ${elapsed}ms`);
814
+ });
815
+ });
816
+
817
+ // A bridge whose published object carries a release, and records what it was asked to register.
818
+ function versionedBridge(version) {
819
+ const seen = [];
820
+ const api = { registerToolDesign: (design) => { seen.push(design); return () => {}; } };
821
+ if (version !== undefined) api.VERSION = version;
822
+ return { api, seen };
823
+ }
824
+ const designFor = (version) => {
825
+ const { api, seen } = versionedBridge(version);
826
+ const dispose = registerBashDesign(api);
827
+ assert.equal(typeof dispose, 'function', String(version));
828
+ dispose();
829
+ // A release string that cannot be read is asked first, with a throwaway design; the design
830
+ // this package really registers is the one after it.
831
+ return seen.find((design) => design.id !== 'odd-worktree.capability-probe');
832
+ };
833
+
834
+ describe('the Card tools line', () => {
835
+ test('the line declares `read` and `write` so the file card can be asked about them, and the design paints nothing for either', () => {
836
+ // S1: the file card is painted only while this line resolves to the activity design
837
+ // for the tool being drawn, and a bridge resolves coverage from the line's own
838
+ // `tools`. `read` and `write` are declared for the card's question — the design itself
839
+ // answers `undefined` for both (`computeActivity` knows only bash and the three read-only
840
+ // shell marks), so their rows come back byte for byte and the host's own card is never lost.
841
+ const design = designFor('0.6.0');
842
+ assert.deepEqual(design.tools, ['bash', 'grep', 'find', 'ls', 'read', 'write']);
843
+ const answer = (tool) => (design.tools.includes(tool) ? { kind: 'design', design } : { kind: 'not-covered' });
844
+ assert.equal(answer('write').kind, 'design', 'a write is covered by the line');
845
+ assert.equal(answer('write').design.id, BASH_DESIGN_ID);
846
+ assert.equal(answer('read').kind, 'design', 'a read is covered by the line too, for the card\'s question');
847
+ assert.equal(answer('read').design.id, BASH_DESIGN_ID);
848
+ assert.equal(answer('edit').kind, 'not-covered', 'and nothing else is');
849
+
850
+ // The design's own rows for a write, both parts: untouched, so the file card — not
851
+ // an activity status line — is what a write shows.
852
+ const render = { args: { path: 'odd/tasks/a.md', content: '# A' }, isPartial: false, durationMs: 142 };
853
+ const call = [`╭─ ✎ write odd/tasks/a.md ${'─'.repeat(50)} ctrl+o to expand ─╮`];
854
+ const result = [`│ ${'x'.repeat(86)} │`, `╰${'─'.repeat(88)}╯`];
855
+ assert.deepEqual(design.apply(call, { tool: 'write', part: 'call', width: 90, theme, render }), call);
856
+ assert.deepEqual(design.apply(result, { tool: 'write', part: 'result', width: 90, theme, render }), result);
857
+ });
858
+
859
+ test('a 0.6.0 bridge gets ONE grouped registration over bash and the read-only tools', () => {
860
+ const design = designFor('0.6.0');
861
+ assert.equal(design.line, 'tools');
862
+ assert.deepEqual(design.tools, ['bash', 'grep', 'find', 'ls', 'read', 'write']);
863
+ assert.equal(Object.hasOwn(design, 'tool'), false, 'never both shapes');
864
+ assert.equal(design.id, BASH_DESIGN_ID);
865
+ assert.equal(design.label, BASH_DESIGN_LABEL);
866
+ assert.equal(design.name, 'Odd worktree');
867
+ assert.match(design.description, /read-only/);
868
+ assert.match(design.description, /exploring/);
869
+ const preview = design.preview();
870
+ assert.equal(preview.title, 'Cards · activity');
871
+ assert.equal(preview.sample.split('\n').length, 1, 'one row, like every Cards value');
872
+ assert.equal(typeof design.apply, 'function');
873
+ });
874
+
875
+ test('every release from 0.6.0 up gets the line', () => {
876
+ for (const version of ['0.6.0', '0.6.1', '0.10.0', '0.7.0-rc.1', '1.0.0', '0.6']) {
877
+ assert.equal(designFor(version).line, 'tools', version);
878
+ }
879
+ });
880
+
881
+ test('an older bridge with a readable release gets exactly today\'s single bash design', () => {
882
+ for (const version of ['0.5.0', '0.5.9', '0.4.2', '0.5']) {
883
+ const design = designFor(version);
884
+ assert.equal(design.tool, 'bash', String(version));
885
+ assert.equal(Object.hasOwn(design, 'line'), false, `no line on ${String(version)}`);
886
+ assert.equal(Object.hasOwn(design, 'tools'), false, `no tools on ${String(version)}`);
887
+ assert.equal(design.id, BASH_DESIGN_ID);
888
+ }
889
+ });
890
+
891
+ test('an old bridge still heads bash cards, and the grouped one heads both', () => {
892
+ const design = designFor('0.5.0');
893
+ const [out] = design.apply([neon('ls -la')], { ...THEMED, render: render('ls -la') });
894
+ assert.ok(out.includes(painted('mdLink', '⌕ exploring')));
895
+ // The shape decides what is wired: an old bridge is only ever asked about `bash`, and the one
896
+ // registration that covers all five tools applies to every one of them.
897
+ const grouped = designFor('0.6.0');
898
+ assert.ok(grouped.apply([readNeon('grep')], readCtx('grep'))[0].includes(painted('mdLink', '⌕ exploring')));
899
+ });
900
+
901
+ test('one registration per process: a second call is the same disposer, whichever shape it chose', () => {
902
+ const { api, seen } = versionedBridge('0.6.0');
903
+ const dispose = registerBashDesign(api);
904
+ try {
905
+ assert.equal(registerBashDesign(api), dispose);
906
+ assert.equal(seen.length, 1);
907
+ assert.equal(seen[0].line, 'tools');
908
+ } finally {
909
+ dispose();
910
+ }
911
+ });
912
+ });
913
+
914
+ // A BRIDGE WE CONTROL, for the one thing this package cannot prove about itself: what the bridge
915
+ // hands `apply`. The real one wraps the host's renderers and passes, from Pi's own render context, a
916
+ // by-value snapshot of the call (`ctx.render`). This fake keeps exactly that contract for BOTH
917
+ // registration shapes and nothing else, so a regression in what this package does with either shape
918
+ // is this package's to fix. It mirrors `toolDesignResolver`: the host's renderers, one wrapper per
919
+ // member, the design applied on every render pass with the context of THAT renderer.
920
+ function fakeRenderBridge(version) {
921
+ const designs = [];
922
+ const seen = [];
923
+ const api = {
924
+ VERSION: version,
925
+ registerToolDesign: (design) => {
926
+ designs.push(design);
927
+ return () => {
928
+ const at = designs.indexOf(design);
929
+ if (at >= 0) designs.splice(at, 1);
930
+ };
931
+ },
932
+ };
933
+ // A design covers one tool (the historical shape) or several (the 0.6.0 line).
934
+ const covers = (design) => (typeof design.tool === 'string' ? [design.tool] : design.tools);
935
+ // Pi's render context → the snapshot the bridge hands a design: the tool's own `args` by reference.
936
+ const snapshot = (context) => {
937
+ if (context === null || typeof context !== 'object') return undefined;
938
+ const render = {};
939
+ if (typeof context.toolCallId === 'string') render.toolCallId = context.toolCallId;
940
+ if ('args' in context) render.args = context.args;
941
+ if (typeof context.isPartial === 'boolean') render.isPartial = context.isPartial;
942
+ if (typeof context.isError === 'boolean') render.isError = context.isError;
943
+ if (typeof context.durationMs === 'number') render.durationMs = context.durationMs;
944
+ return render;
945
+ };
946
+ const resolve = (tool, host) => {
947
+ const design = designs.find((candidate) => covers(candidate).includes(tool));
948
+ if (design === undefined) return host;
949
+ const wrap = (member, themeIndex, contextIndex, part) => (...args) => {
950
+ const component = host[member](...args);
951
+ return {
952
+ render: (width) => {
953
+ const rows = component.render(width);
954
+ const render = snapshot(args[contextIndex]);
955
+ seen.push({ tool, part, render });
956
+ const ctx = render === undefined
957
+ ? { width, tool, theme: args[themeIndex], part }
958
+ : { width, tool, theme: args[themeIndex], part, render };
959
+ return design.apply([...rows], ctx);
960
+ },
961
+ };
962
+ };
963
+ return {
964
+ renderCall: wrap('renderCall', 1, 2, 'call'),
965
+ renderResult: wrap('renderResult', 2, 3, 'result'),
966
+ };
967
+ };
968
+ return { api, resolve, designs, seen };
969
+ }
970
+
971
+ describe('what the bridge hands the design', () => {
972
+ // The host cut the heading, so only the call's own arguments can name this card.
973
+ const COMMAND = `grep -rn "activity" ${'lib/'.repeat(6)} | head -40; echo ---; grep -rn needle docs`;
974
+ const cutRow = neon(`${COMMAND.slice(0, 52)}…`);
975
+ const closingRow = () => `╰${'─'.repeat(88)}╯`;
976
+ const call = { toolCallId: 'c1', args: { command: COMMAND }, isPartial: false, isError: false, durationMs: 132 };
977
+ const draw = (version) => {
978
+ const bridge = fakeRenderBridge(version);
979
+ const dispose = registerBashDesign(bridge.api);
980
+ assert.equal(typeof dispose, 'function', version);
981
+ const host = {
982
+ renderCall: () => ({ render: () => [cutRow] }),
983
+ renderResult: () => ({ render: () => [closingRow()] }),
984
+ };
985
+ const resolved = bridge.resolve('bash', host);
986
+ const rows = {
987
+ call: resolved.renderCall({}, theme, call).render(90),
988
+ result: resolved.renderResult({}, {}, theme, call).render(90),
989
+ };
990
+ dispose();
991
+ return { ...rows, seen: bridge.seen };
992
+ };
993
+
994
+ test('a line registration is handed the same `ctx.render` as a per-tool one, and draws the same card', () => {
995
+ const perTool = draw('0.5.0');
996
+ const line = draw('0.6.0');
997
+ const render = { toolCallId: 'c1', args: { command: COMMAND }, isPartial: false, isError: false, durationMs: 132 };
998
+ const expected = [
999
+ { tool: 'bash', part: 'call', render },
1000
+ { tool: 'bash', part: 'result', render },
1001
+ ];
1002
+ assert.deepEqual(perTool.seen, expected, 'the per-tool registration gets the call\'s own context');
1003
+ assert.deepEqual(line.seen, perTool.seen, 'the line registration gets exactly the same context');
1004
+ assert.deepEqual(line.call, perTool.call, 'so it draws the same heading row');
1005
+ assert.deepEqual(line.result, perTool.result, 'and the same closing rule');
1006
+ });
1007
+
1008
+ test('that context is what names a cut command, paints the frame and draws the duration — on both shapes', () => {
1009
+ for (const version of ['0.5.0', '0.6.0']) {
1010
+ const rows = draw(version);
1011
+ assert.ok(rows.call[0] !== cutRow, `${version}: the design ran`);
1012
+ assert.ok(rows.call[0].includes(painted('mdLink', '⌕ exploring')), `${version}: ${strip(rows.call[0])}`);
1013
+ assert.ok(
1014
+ rows.call[0].startsWith(`\u001b[${ROLE_CODE.mdLink}m╭─`),
1015
+ `${version}: the frame is painted in the activity role`,
1016
+ );
1017
+ assert.doesNotMatch(strip(rows.call[0]), /\$ bash/, `${version}: never the generic fallback`);
1018
+ assert.match(strip(rows.result[0]), / 132ms ╯$/, `${version}: the duration is drawn`);
1019
+ assert.ok(rows.result[0].includes(painted('dim', '132ms')), `${version}: the duration is dim`);
1020
+ }
1021
+ });
1022
+
1023
+ test('a context the bridge hands without the call is the host\'s card, never a guess', () => {
1024
+ const bridge = fakeRenderBridge('0.6.0');
1025
+ const dispose = registerBashDesign(bridge.api);
1026
+ try {
1027
+ const resolved = bridge.resolve('bash', { renderCall: () => ({ render: () => [cutRow] }) });
1028
+ const [out] = resolved.renderCall({}, theme, undefined).render(90);
1029
+ // The drawn heading is cut, so nothing can be named: bash's own `$ bash` fallback, the width
1030
+ // kept, and no frame role — the state (`isPartial`, `isError`, `durationMs`) is unknown.
1031
+ assert.ok(out.includes(painted('bashMode', '$ bash')), strip(out));
1032
+ assert.ok(out.startsWith('╭─ '), 'the frame keeps the host\'s paint');
1033
+ assert.equal(cols(out), cols(cutRow));
1034
+ assert.deepEqual(bridge.seen, [{ tool: 'bash', part: 'call', render: undefined }]);
1035
+ } finally {
1036
+ dispose();
1037
+ }
1038
+ });
1039
+ });
1040
+
1041
+ describe('the seed runs before the registration', () => {
1042
+ // A 0.6.0 bridge, as its contract documents it: at REGISTRATION time a line with no value of its
1043
+ // own inherits a joining tool's legacy per-tool value, and writes it then.
1044
+ function migratingBridge(files = {}) {
1045
+ const { api, files: store, writes } = fakeBridge(files);
1046
+ const inherited = [];
1047
+ api.VERSION = '0.6.0';
1048
+ api.registerToolDesign = () => {
1049
+ const selections = store['bridge-tool-designs']?.selections ?? {};
1050
+ inherited.push(JSON.parse(JSON.stringify(selections)));
1051
+ if (typeof selections.bash === 'string' && selections.bash !== 'native' && selections.$lines?.tools === undefined) {
1052
+ store['bridge-tool-designs'] = { ...store['bridge-tool-designs'], selections: { ...selections, $lines: { tools: selections.bash } } };
1053
+ }
1054
+ return () => {};
1055
+ };
1056
+ return { api, files: store, writes, inherited };
1057
+ }
1058
+
1059
+ test('a first run stores the choice before registering, so the line inherits it', () => {
1060
+ const { api, files, inherited } = migratingBridge();
1061
+ const dispose = registerBashDesign(api);
1062
+ assert.equal(typeof dispose, 'function');
1063
+ dispose();
1064
+ assert.deepEqual(inherited[0], { bash: BASH_DESIGN_ID }, 'the seed was already stored when the bridge registered');
1065
+ assert.deepEqual(files['bridge-tool-designs'].selections, { bash: BASH_DESIGN_ID, $lines: { tools: BASH_DESIGN_ID } });
1066
+ });
1067
+
1068
+ test('a stored legacy choice is kept: the seed writes no selection and the line inherits that choice', () => {
1069
+ const { api, files, writes } = migratingBridge({ 'bridge-tool-designs': { selections: { bash: 'someone.else' } } });
1070
+ const dispose = registerBashDesign(api);
1071
+ dispose();
1072
+ assert.deepEqual(writes.filter(([store]) => store === SELECTIONS[0]), [], 'no selection was written');
1073
+ assert.deepEqual(files['bridge-tool-designs'].selections, { bash: 'someone.else', $lines: { tools: 'someone.else' } });
1074
+ });
1075
+
1076
+ test('the seed never writes the reserved line key itself', () => {
1077
+ const { api, files } = migratingBridge();
1078
+ api.registerToolDesign = () => () => {};
1079
+ const dispose = registerBashDesign(api);
1080
+ dispose();
1081
+ assert.deepEqual(files['bridge-tool-designs'].selections, { bash: BASH_DESIGN_ID }, 'no $lines from this package');
1082
+ });
1083
+
1084
+ test('an old bridge keeps today\'s per-tool choice, with no line value', () => {
1085
+ const { api, files } = fakeBridge();
1086
+ api.VERSION = '0.5.0';
1087
+ const dispose = registerBashDesign(api);
1088
+ dispose();
1089
+ assert.deepEqual(files['bridge-tool-designs'].selections, { bash: BASH_DESIGN_ID });
1090
+ });
1091
+ });