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,845 @@
1
+ 'use strict';
2
+ // S5 / T5b: the two worktree tools' call and result cards, drawn on the substrate
3
+ // of lib/shell-card.js (T5a). Every case goes through the module's public surface
4
+ // — `createWorktreeCards({ metrics })` and the renderer it hands back per tool —
5
+ // and reads the lines an injected component produces for a width.
6
+ //
7
+ // The text seam and the themes are the same fakes tools/shell-card.test.js uses:
8
+ // this package has no node_modules, so width measurement, truncation and wrapping
9
+ // are injected, and colour only ever arrives as `theme.fg(role, text)`.
10
+ const assert = require('node:assert/strict');
11
+ const { mkdtempSync, readFileSync, rmSync } = require('node:fs');
12
+ const { tmpdir } = require('node:os');
13
+ const { join } = require('node:path');
14
+ const { before, after, test } = require('node:test');
15
+
16
+ const { ELLIPSIS, WORKTREE_CARD_TOOLS, createWorktreeCards } = require('../lib/worktree-cards.js');
17
+ const { worktreeListView } = require('../lib/worktree-list.js');
18
+ const { CARD_STYLE } = require('../lib/card-style.js');
19
+ const { CARD_CONTENT } = require('../lib/card-content.js');
20
+
21
+ // ---- the injected text seam (fakes: plain ASCII, each character one column) ----
22
+
23
+ const ANSI = /\x1b\[[0-9;]*m/g;
24
+ const stripAnsi = (value) => value.replace(ANSI, '');
25
+ const chars = (value) => [...stripAnsi(value)];
26
+
27
+ const metrics = {
28
+ visibleWidth(value) {
29
+ return chars(value).length;
30
+ },
31
+ truncateToWidth(value, width, ellipsis = '') {
32
+ if (width <= 0) return '';
33
+ if (this.visibleWidth(value) <= width) return value;
34
+ const tail = ellipsis ?? '';
35
+ const keep = Math.max(0, width - [...tail].length);
36
+ return chars(value).slice(0, keep).join('') + tail;
37
+ },
38
+ wrapTextWithAnsi(value, width) {
39
+ const words = stripAnsi(value).split(' ');
40
+ if (width <= 0) return [stripAnsi(value)];
41
+ const lines = [];
42
+ let current = '';
43
+ for (const word of words) {
44
+ const candidate = current === '' ? word : `${current} ${word}`;
45
+ if ([...candidate].length <= width) {
46
+ current = candidate;
47
+ continue;
48
+ }
49
+ if (current !== '') {
50
+ lines.push(current);
51
+ current = '';
52
+ }
53
+ if ([...word].length <= width) {
54
+ current = word;
55
+ continue;
56
+ }
57
+ const rest = [...word];
58
+ while (rest.length > width) lines.push(rest.splice(0, width).join(''));
59
+ current = rest.join('');
60
+ }
61
+ lines.push(current);
62
+ return lines;
63
+ },
64
+ };
65
+
66
+ // ---- themes: a theme is an injected fg(role, text) ----
67
+
68
+ const plainTheme = {
69
+ fg(_role, value) {
70
+ return value;
71
+ },
72
+ };
73
+
74
+ const taggedTheme = {
75
+ fg(role, value) {
76
+ return `<${role}>${value}</${role}>`;
77
+ },
78
+ };
79
+
80
+ function recordingTheme() {
81
+ const roles = [];
82
+ return {
83
+ roles,
84
+ fg(role, value) {
85
+ roles.push(role);
86
+ return value;
87
+ },
88
+ };
89
+ }
90
+
91
+ // S5: the style seam — the process-wide slot Gentle Shell publishes — and a
92
+ // scratch preference home, so no case reads or writes the real user's document.
93
+ const CARD_STYLE_SLOT = Symbol.for('gentle-pi.card-style');
94
+ const BG_RESET = '\x1b[49m';
95
+
96
+ function useStyle(style) {
97
+ if (style === undefined) delete globalThis[CARD_STYLE_SLOT];
98
+ else globalThis[CARD_STYLE_SLOT] = style;
99
+ }
100
+
101
+ function withStyle(style, run) {
102
+ useStyle(style);
103
+ try {
104
+ return run();
105
+ } finally {
106
+ useStyle(undefined);
107
+ }
108
+ }
109
+
110
+ /** A plain theme that can also paint the float background, so the text stays readable. */
111
+ function paintTheme() {
112
+ const roles = { fg: [], bg: [] };
113
+ return {
114
+ roles,
115
+ fg(role, value) {
116
+ roles.fg.push(role);
117
+ return value;
118
+ },
119
+ bg(role, value) {
120
+ roles.bg.push(role);
121
+ return `\x1b[48;5;240m${value}${BG_RESET}`;
122
+ },
123
+ };
124
+ }
125
+
126
+ const savedHome = process.env.GENTLE_PI_CONFIG_HOME;
127
+ let configHome;
128
+
129
+ before(() => {
130
+ configHome = mkdtempSync(join(tmpdir(), 'odd-worktree-card-style-'));
131
+ process.env.GENTLE_PI_CONFIG_HOME = configHome;
132
+ useStyle(undefined);
133
+ });
134
+
135
+ after(() => {
136
+ useStyle(undefined);
137
+ if (savedHome === undefined) delete process.env.GENTLE_PI_CONFIG_HOME;
138
+ else process.env.GENTLE_PI_CONFIG_HOME = savedHome;
139
+ rmSync(configHome, { recursive: true, force: true });
140
+ });
141
+
142
+ // ---- the factory, and the two calls every case makes ----
143
+
144
+ const cards = createWorktreeCards({ metrics });
145
+
146
+ function callRows(tool, args, theme, context, width) {
147
+ return cards.renderer(tool).renderCall(args, theme, context).render(width);
148
+ }
149
+
150
+ function resultRows(tool, result, options, theme, context, width) {
151
+ return cards.renderer(tool).renderResult(result, options, theme, context).render(width);
152
+ }
153
+
154
+ const call = (tool, args, theme, context, width) => callRows(tool, args, theme, context ?? {}, width);
155
+
156
+ /** Joining wrapped rows back into one string: only the whitespace the wrap introduced is ignored. */
157
+ const squash = (value) => value.replace(/\s+/g, '');
158
+
159
+ function textResult(text, isError) {
160
+ return { content: [{ type: 'text', text }], details: {}, ...(isError ? { isError: true } : {}) };
161
+ }
162
+
163
+ /** The visible content of a body row, without the frame. */
164
+ function bodyText(row) {
165
+ return stripAnsi(row).replace(/^│ /, '').replace(/ +│$/, '');
166
+ }
167
+
168
+ /** Every row the card draws must be exactly the width the host asked for. */
169
+ function assertFramed(rows, width) {
170
+ for (const row of rows) assert.equal(metrics.visibleWidth(row), width, `"${stripAnsi(row)}" is not ${width} wide`);
171
+ }
172
+
173
+ const CREATE_ARGS = { branch: 'feat/cards-v2' };
174
+ const CREATE_TEXT = [
175
+ 'worktree created: /home/jona/dev/projects/odd-worktree-worktrees/cards-v2',
176
+ 'branch: feat/cards-v2 (created)',
177
+ 'odd: -',
178
+ 'base: main@1a2b3c4',
179
+ 'worktree register in session',
180
+ 'setup: pnpm install · log: /home/jona/dev/projects/odd-worktree-worktrees/cards-v2/.worktree-setup.log',
181
+ ].join('\n');
182
+ const REFUSAL_TEXT = [
183
+ 'branch feat/cards-v2 is already checked out in /home/jona/dev/projects/odd-worktree-worktrees/cards-v2',
184
+ 'Use a different branch, or work in that worktree.',
185
+ ].join('\n');
186
+
187
+ function listViewOf(all) {
188
+ // S3: the list's real input — Git's own worktree listing and the canonical main
189
+ // root. The main checkout is listed first and unnumbered; the checkout under
190
+ // `<repo-name>-worktrees` is one this package created, named by its directory.
191
+ return worktreeListView({
192
+ project: 'gentle-shell',
193
+ all,
194
+ mainRoot: '/clone',
195
+ entries: [
196
+ { path: '/clone', branch: 'main', head: '1a2b3c4' },
197
+ { path: '/clone-worktrees/cards-v2', branch: 'feat/cards-v2', head: '9f8e7d6' },
198
+ ],
199
+ });
200
+ }
201
+
202
+ // ---- the factory ----
203
+
204
+ test('the factory hands one renderer per worktree tool and refuses anything else', () => {
205
+ assert.deepEqual(WORKTREE_CARD_TOOLS, ['worktree_create', 'worktree_list'], 'the two tools this module draws');
206
+ assert.equal(ELLIPSIS, '…', 'the overflow mark is the substrate literal, never a three dot ellipsis');
207
+ const made = createWorktreeCards({ metrics });
208
+ for (const tool of ['worktree_create', 'worktree_list']) {
209
+ const renderer = made.renderer(tool);
210
+ assert.equal(typeof renderer.renderCall, 'function', `${tool} renders its call`);
211
+ assert.equal(typeof renderer.renderResult, 'function', `${tool} renders its result`);
212
+ assert.equal(typeof renderer.renderCall({}, plainTheme, {}).render, 'function', `${tool} returns a component`);
213
+ }
214
+ assert.throws(() => made.renderer('worktree_delete'), /unknown worktree tool: worktree_delete/, 'the removed tools have no card');
215
+ assert.throws(() => made.renderer('worktree_warp'), /unknown worktree tool/);
216
+ assert.throws(() => made.renderer(undefined), /unknown worktree tool/);
217
+ });
218
+
219
+ test('the factory requires the injected text seam and says which measure is missing', () => {
220
+ assert.throws(() => createWorktreeCards(), /metrics/);
221
+ assert.throws(() => createWorktreeCards({}), /metrics/);
222
+ for (const name of ['visibleWidth', 'truncateToWidth', 'wrapTextWithAnsi']) {
223
+ const broken = { ...metrics, [name]: undefined };
224
+ assert.throws(() => createWorktreeCards({ metrics: broken }), new RegExp(name));
225
+ }
226
+ });
227
+
228
+ // ---- the call card ----
229
+
230
+ test('a collapsed call is one heading row naming the tool and its detail', () => {
231
+ const rows = call('worktree_create', CREATE_ARGS, plainTheme, { isPartial: false }, 60);
232
+ assert.equal(rows.length, 1, 'a finished call draws its heading and lets the result close the frame');
233
+ assert.equal(rows[0], `╭─ ⎇ worktree create · feat/cards-v2 ${'─'.repeat(22)}╮`);
234
+ assertFramed(rows, 60);
235
+
236
+ const all = call('worktree_list', { all: true }, plainTheme, { isPartial: false }, 60);
237
+ assert.equal(all[0], `╭─ ⎇ worktree list · all ${'─'.repeat(34)}╮`);
238
+
239
+ const plain = call('worktree_list', {}, plainTheme, { isPartial: false }, 60);
240
+ assert.equal(plain[0], `╭─ ⎇ worktree list ${'─'.repeat(40)}╮`, 'an omitted all adds no detail');
241
+ const omitted = call('worktree_list', { all: false }, plainTheme, { isPartial: false }, 60);
242
+ assert.equal(omitted[0], plain[0], 'all: false is not the detail the flag names');
243
+
244
+ const wide = call('worktree_create', { branch: 'feat/a-very-long-branch-name-that-cannot-fit-the-rule-anywhere' }, plainTheme, { isPartial: false }, 60);
245
+ assert.equal(wide.length, 1, 'a collapsed call never grows a second row');
246
+ assert.match(wide[0], /… ╮$/, 'an over-wide detail is truncated with a literal ellipsis');
247
+ assert.doesNotMatch(wide[0], /anywhere/, 'only what fits stays visible');
248
+ assertFramed(wide, 60);
249
+ });
250
+
251
+ test('an expanded call wraps the heading onto body rows instead of losing the detail', () => {
252
+ const branch = 'feat/a-very-long-branch-name-that-cannot-fit-the-rule-anywhere';
253
+ const rows = call('worktree_create', { branch }, plainTheme, { isPartial: false, expanded: true }, 60);
254
+ assert.ok(rows.length > 1, 'the detail continues inside the frame');
255
+ assert.match(rows[0], /^╭─ ⎇ worktree create ─+╮$/, 'the title keeps the top rule');
256
+ assert.ok(!rows[0].includes('feat/'), 'a detail too wide for the rule never crowds the title');
257
+ assertFramed(rows, 60);
258
+ const shown = rows.slice(1).map(bodyText).join('');
259
+ assert.equal(squash(shown), squash(`· ${branch}`), 'the complete detail stays visible');
260
+ });
261
+
262
+ test('a running call draws one muted running row and closes its own frame in warning', () => {
263
+ const rows = call('worktree_create', CREATE_ARGS, plainTheme, { isPartial: true }, 40);
264
+ assert.equal(rows.length, 3, 'a heading, the running row and the closed frame');
265
+ assert.equal(rows[0], `╭─ ⎇ worktree create · feat/cards-v2 ${'─'.repeat(2)}╮`);
266
+ assert.equal(rows[1], `│ running…${' '.repeat(28)} │`);
267
+ assert.equal(rows[2], `╰${'─'.repeat(38)}╯`);
268
+ assert.equal(rows[1].includes('\x1b'), false, 'the running text carries no escape of its own');
269
+
270
+ const recorder = recordingTheme();
271
+ call('worktree_create', CREATE_ARGS, recorder, { isPartial: true }, 40);
272
+ assert.deepEqual([...new Set(recorder.roles)].sort(), ['muted', 'warning'], 'the running text is muted and the frame keeps the tone');
273
+
274
+ const tagged = call('worktree_create', CREATE_ARGS, taggedTheme, { isPartial: true }, 40);
275
+ assert.match(tagged[0], /^<warning>╭<\/warning>/, 'pending is the warning tone');
276
+ assert.match(tagged[1], /^<warning>│<\/warning> <muted>running…<\/muted>/, 'the running text is the muted role');
277
+
278
+ // Settling keeps the very same heading, in the same tone, and drops the running row.
279
+ const settledRows = call('worktree_create', CREATE_ARGS, taggedTheme, { isPartial: false }, 40);
280
+ assert.match(settledRows[0], /^<warning>╭<\/warning>/, 'a finished card wears the warp tone, never the success role');
281
+ assert.match(settledRows[0], /<warning>⎇ worktree create<\/warning>/, 'a settled heading labels itself in its own tone, never in the accent');
282
+ assert.equal(settledRows.some((row) => row.includes('running')), false);
283
+
284
+ const failed = call('worktree_create', CREATE_ARGS, taggedTheme, { isPartial: false, isError: true }, 40);
285
+ assert.match(failed[0], /^<error>╭<\/error>/, 'a failed call is the error tone');
286
+ assert.match(failed[0], /<error>⎇ worktree create<\/error>/);
287
+ assert.match(tagged[0], /<warning>⎇ worktree create<\/warning>/, 'a pending call labels itself in the warning tone');
288
+ });
289
+
290
+ test('a call shows the host key hint only while it is settled and the row has room', () => {
291
+ const keyHint = (id, text) => (id === 'app.tools.expand' ? `ctrl+o ${text}` : '');
292
+ const rows = call('worktree_create', CREATE_ARGS, plainTheme, { isPartial: false, keyHint }, 60);
293
+ assert.equal(rows.length, 1);
294
+ assert.match(rows[0], /─+ ctrl\+o to expand ╮$/);
295
+ assertFramed(rows, 60);
296
+
297
+ const narrow = call('worktree_create', CREATE_ARGS, plainTheme, { isPartial: false, keyHint }, 30);
298
+ assert.equal(narrow.length, 1);
299
+ assert.doesNotMatch(narrow[0], /ctrl\+o/, 'the hint yields its columns before the detail loses its identity');
300
+ assert.match(narrow[0], /^╭─ ⎇ worktree create · feat… ╮$/, 'the detail keeps an ellipsis and the heading keeps its title');
301
+ assertFramed(narrow, 30);
302
+
303
+ const running = call('worktree_create', CREATE_ARGS, plainTheme, { isPartial: true, keyHint }, 60);
304
+ assert.doesNotMatch(running[0], /ctrl\+o/, 'a running row offers no expand affordance');
305
+
306
+ const withoutHint = call('worktree_create', CREATE_ARGS, plainTheme, { isPartial: false }, 60);
307
+ const silent = call('worktree_create', CREATE_ARGS, plainTheme, { isPartial: false, keyHint: () => '' }, 60);
308
+ assert.equal(silent[0], withoutHint[0], 'an empty hint is not painted');
309
+ });
310
+
311
+ test('a call and its result share one frame, whichever render the host arrives at first', () => {
312
+ // Pending call, no result yet: the call closes its own frame.
313
+ const pending = call('worktree_create', CREATE_ARGS, plainTheme, { isPartial: true }, 40);
314
+ assert.equal(pending.at(-1).startsWith('╰'), true);
315
+
316
+ // The result marks the shared row state, so the call above stops closing.
317
+ const state = {};
318
+ const result = resultRows('worktree_create', textResult(CREATE_TEXT), { expanded: false, isPartial: false }, plainTheme, { state }, 40);
319
+ assert.equal(result.at(-1).startsWith('╰'), true, 'the result draws the closing rule');
320
+ const afterResult = call('worktree_create', CREATE_ARGS, plainTheme, { isPartial: true, state }, 40);
321
+ assert.equal(afterResult.some((row) => row.startsWith('╰')), false, 'the result owns the only closing rule');
322
+ assert.equal(afterResult[0].startsWith('╭'), true);
323
+ assert.deepEqual(Object.keys(state), [], 'the shared mark is not ordinary row state');
324
+ });
325
+
326
+ // ---- the result card ----
327
+
328
+ test("a result body is the tool's own text, wrapped, padded and closed by one rule", () => {
329
+ const rows = resultRows('worktree_create', textResult(CREATE_TEXT), { expanded: true, isPartial: false }, plainTheme, {}, 60);
330
+ assert.match(rows[0], /^│ /, 'the call above owns the header, so the result starts at its body');
331
+ assert.equal(rows.at(-1), `╰${'─'.repeat(58)}╯`);
332
+ assertFramed(rows, 60);
333
+ assert.ok(!rows.some((row) => row.includes('╭')), 'a result never draws a second header');
334
+ const body = rows.slice(0, -1).map(bodyText);
335
+ assert.equal(squash(body.join('')), squash(CREATE_TEXT), 'every character of the text survives the wrap');
336
+
337
+ // The list result carries the list view's own text, unchanged in order.
338
+ const view = listViewOf(true);
339
+ const listRows = resultRows('worktree_list', textResult(view.text), { expanded: true, isPartial: false }, plainTheme, {}, 40);
340
+ assertFramed(listRows, 40);
341
+ const listBody = listRows.slice(0, -1).map(bodyText);
342
+ assert.equal(listBody[0], 'project: gentle-shell');
343
+ assert.equal(listBody[1], 'path: /clone');
344
+ assert.equal(listBody[2], 'base: main@1a2b3c4', 'the main row names its base and carries no stored state suffix');
345
+ assert.equal(listBody[3], '1 cards-v2 · feat/cards-v2', 'the row carries the checkout name and its branch');
346
+ });
347
+
348
+ test('a collapsed result shows at most three body rows and marks what it hides', () => {
349
+ const short = 'worktree created: /clone-worktrees/cards-v2';
350
+ const shortRows = resultRows('worktree_create', textResult(short), { expanded: false, isPartial: false }, plainTheme, {}, 60);
351
+ assert.equal(shortRows.length, 2, 'one body row and the closing rule');
352
+ assert.equal(bodyText(shortRows[0]), short);
353
+ assert.equal(shortRows[0].includes('…'), false, 'nothing hidden, no overflow mark');
354
+
355
+ const collapsed = resultRows('worktree_create', textResult(CREATE_TEXT), { expanded: false, isPartial: false }, plainTheme, {}, 60);
356
+ assert.equal(collapsed.length, 4, 'three body rows and the closing rule');
357
+ for (const row of collapsed.slice(0, 3)) assert.equal(bodyText(row).endsWith('…'), row === collapsed[2], 'only the last preview row marks the hidden text');
358
+ assert.match(bodyText(collapsed[2]), /…$/, 'the reader is told the text continues');
359
+
360
+ const expanded = resultRows('worktree_create', textResult(CREATE_TEXT), { expanded: true, isPartial: false }, plainTheme, {}, 60);
361
+ assert.ok(expanded.length > collapsed.length, 'expanded shows all of it');
362
+ assert.equal(expanded.some((row) => bodyText(row).endsWith('…')), false, 'an expanded body hides nothing');
363
+ assert.deepEqual(
364
+ collapsed.slice(0, 2).map(bodyText),
365
+ expanded.slice(0, 2).map(bodyText),
366
+ 'the collapsed body is the beginning of the expanded one',
367
+ );
368
+ });
369
+
370
+ // ---- the content level: `minimal` is the call alone, `default` the preview ----
371
+ //
372
+ // The level is the user's choice in Gentle Shell, published on the process-wide slot
373
+ // `lib/card-content.js` reads, exactly as the CodeGraph card reads it. The factory
374
+ // takes the same reader seam (`cardContent`), so a case injects a level instead of
375
+ // touching the real slot, and the default reader stays the real one.
376
+ const CARD_CONTENT_SLOT = Symbol.for('gentle-pi.card-content');
377
+
378
+ /** Run with the process-wide level slot set, restoring whatever it held. */
379
+ function withContent(level, run) {
380
+ const before = globalThis[CARD_CONTENT_SLOT];
381
+ if (level === undefined) delete globalThis[CARD_CONTENT_SLOT];
382
+ else globalThis[CARD_CONTENT_SLOT] = level;
383
+ try {
384
+ return run();
385
+ } finally {
386
+ if (before === undefined) delete globalThis[CARD_CONTENT_SLOT];
387
+ else globalThis[CARD_CONTENT_SLOT] = before;
388
+ }
389
+ }
390
+
391
+ test('S2: a minimal worktree card keeps its three info rows, exactly as default does', () => {
392
+ // S2: the content level no longer withholds these rows. The reader's own level is
393
+ // still published on the slot, and both tools draw the same preview under either.
394
+ const createAt = (level) => withContent(level, () => resultRows('worktree_create', textResult(CREATE_TEXT), { expanded: false, isPartial: false }, plainTheme, {}, 120));
395
+ const createDefault = createAt(CARD_CONTENT.DEFAULT);
396
+ assert.equal(createDefault.length, 4, 'three body rows and the closing rule');
397
+ assert.deepEqual(createAt(CARD_CONTENT.MINIMAL), createDefault, 'minimal does not withhold the three rows');
398
+ const createBody = createDefault.slice(0, -1).map(bodyText);
399
+ assert.equal(createBody[0], 'worktree created: /home/jona/dev/projects/odd-worktree-worktrees/cards-v2', 'the created checkout path');
400
+ assert.equal(createBody[1], 'branch: feat/cards-v2 (created)', 'the branch and how it was obtained');
401
+ assert.equal(createBody[2], 'odd: -…', 'the odd, marked because more rows follow');
402
+ assertFramed(createDefault, 120);
403
+
404
+ const listText = listViewOf(true).text;
405
+ const listAt = (level) => withContent(level, () => resultRows('worktree_list', textResult(listText), { expanded: false, isPartial: false }, plainTheme, {}, 120));
406
+ const listDefault = listAt(CARD_CONTENT.DEFAULT);
407
+ assert.equal(listDefault.length, 4, 'three body rows and the closing rule');
408
+ assert.deepEqual(listAt(CARD_CONTENT.MINIMAL), listDefault, 'minimal does not withhold the three rows');
409
+ assert.deepEqual(
410
+ listDefault.slice(0, -1).map(bodyText),
411
+ ['project: gentle-shell', 'path: /clone', 'base: main@1a2b3c4…'],
412
+ 'the preview is the first three rows of the expanded list, in the tool\'s own order, marked because more follow',
413
+ );
414
+ assertFramed(listDefault, 120);
415
+ });
416
+
417
+ test('S2: a collapsed worktree_list preview is the first three rows of the expanded list', () => {
418
+ const view = listViewOf(true);
419
+ const collapsed = resultRows('worktree_list', textResult(view.text), { expanded: false, isPartial: false }, plainTheme, {}, 120);
420
+ const collapsedBody = collapsed.slice(0, -1).map(bodyText);
421
+ const expanded = resultRows('worktree_list', textResult(view.text), { expanded: true, isPartial: false }, plainTheme, {}, 120);
422
+ const expandedBody = expanded.slice(0, -1).map(bodyText);
423
+ assert.deepEqual(expandedBody, view.text.split('\n'), 'expanded shows every row, in the tool order');
424
+ assert.deepEqual(
425
+ collapsedBody,
426
+ [expandedBody[0], expandedBody[1], `${expandedBody[2]}…`],
427
+ 'collapsed is exactly the first three expanded rows, the last one marked because more follow',
428
+ );
429
+ assert.equal(collapsedBody[0].startsWith('project: '), true, 'the project leads, as it does when expanded');
430
+ assert.equal(collapsedBody.some((row) => /^\d+ cards-v2/.test(row)), false, 'no worktree row jumps ahead of the metadata');
431
+ });
432
+
433
+ test('a minimal refusal still says why', () => {
434
+ // No injected reader here: the shared factory reads the real slot on every paint.
435
+ const refused = withContent(CARD_CONTENT.MINIMAL, () => resultRows('worktree_list', textResult(REFUSAL_TEXT, true), { expanded: false, isPartial: false }, plainTheme, {}, 60));
436
+ assert.ok(refused.length > 1, 'a failure keeps its reason at the minimal level');
437
+ assert.match(bodyText(refused[0]), /^branch feat\/cards-v2 is/, 'the reason opens the body');
438
+ assert.equal(refused.at(-1), `╰${'─'.repeat(58)}╯`, 'and the card still closes with its rule');
439
+ assertFramed(refused, 60);
440
+
441
+ // S2: a successful result keeps its rows too, at the same level.
442
+ const quiet = withContent(CARD_CONTENT.MINIMAL, () => resultRows('worktree_list', textResult(REFUSAL_TEXT), { expanded: false, isPartial: false }, plainTheme, {}, 60));
443
+ assert.ok(quiet.length > 1, 'a successful minimal result still draws its info rows');
444
+ assert.match(bodyText(quiet[0]), /^branch feat\/cards-v2 is/, 'and the first row is the tool text');
445
+
446
+ // A minimal card that is still streaming draws no closing rule: it belongs to the
447
+ // result that settles.
448
+ const streaming = withContent(CARD_CONTENT.MINIMAL, () => resultRows('worktree_list', textResult('worktree list: 2 checkouts'), { expanded: false, isPartial: true }, plainTheme, {}, 60));
449
+ assert.equal(streaming.length, 1, 'a streamed result draws its row but no closing rule');
450
+ });
451
+
452
+ test('an expanded card shows everything at either level', () => {
453
+ const expandedAt = (level) => withContent(level, () => resultRows('worktree_create', textResult(CREATE_TEXT), { expanded: true, isPartial: false }, plainTheme, {}, 60));
454
+ const atDefault = expandedAt(CARD_CONTENT.DEFAULT);
455
+ assert.deepEqual(expandedAt(CARD_CONTENT.MINIMAL), atDefault, 'the reader asked for everything and gets everything');
456
+ assert.equal(squash(atDefault.slice(0, -1).map(bodyText).join('')), squash(CREATE_TEXT), 'and everything is the whole text');
457
+ });
458
+
459
+ test('a refusal stays fully readable expanded and keeps its reason collapsed', () => { for (const width of [60, 30]) {
460
+ const expanded = resultRows('worktree_list', textResult(REFUSAL_TEXT, true), { expanded: true, isPartial: false }, plainTheme, {}, width);
461
+ assertFramed(expanded, width);
462
+ const body = expanded.slice(0, -1).map(bodyText);
463
+ assert.equal(squash(body.join('')), squash(REFUSAL_TEXT), `no character of the reason is lost at ${width}`);
464
+ assert.equal(body.some((row) => row.includes('…')), false, `the expanded refusal is clipped nowhere at ${width}`);
465
+ assert.match(body[0], /^branch feat\/cards-v2 is/, `the reason opens the body at ${width}`);
466
+ assert.ok(body.some((row) => row.includes('work in that worktree')), `the second line of the refusal survives at ${width}`);
467
+
468
+ const collapsed = resultRows('worktree_list', textResult(REFUSAL_TEXT, true), { expanded: false, isPartial: false }, plainTheme, {}, width);
469
+ assertFramed(collapsed, width);
470
+ const preview = collapsed.slice(0, -1).map(bodyText);
471
+ const budget = Math.min(3, body.length);
472
+ assert.equal(preview.length, budget, `the preview budget holds at ${width}`);
473
+ assert.match(preview[0], /^branch feat\/cards-v2 is/, `the collapsed reason is recognisable at ${width}`);
474
+ assert.deepEqual(preview.slice(0, budget - 1), body.slice(0, budget - 1), `the collapsed refusal is the expanded refusal's beginning at ${width}`);
475
+ if (body.length > budget) {
476
+ assert.ok(preview[budget - 1].endsWith('…'), `the collapsed refusal says it continues at ${width}`);
477
+ assert.ok(body[budget - 1].startsWith(preview[budget - 1].slice(0, -1)), `the mark replaces the tail of one row at ${width}`);
478
+ } else {
479
+ assert.equal(preview[budget - 1], body[budget - 1], `a reason that fits the preview is shown whole at ${width}`);
480
+ }
481
+ }
482
+ });
483
+
484
+ test('the tone follows the state: the warp warning once finished or pending, error on failure', () => {
485
+ const result = (tool, value, options, context) => resultRows(tool, value, options, taggedTheme, context ?? {}, 40);
486
+
487
+ assert.match(result('worktree_create', textResult(CREATE_TEXT), { expanded: false, isPartial: false })[0], /^<warning>│<\/warning>/, 'a finished result wears the warp tone, not success');
488
+ assert.match(result('worktree_list', textResult('project: x'), { expanded: false, isPartial: false })[0], /^<warning>│<\/warning>/, 'a list is not a failure and wears the same tone');
489
+ assert.match(result('worktree_create', textResult('boom', true), { expanded: false, isPartial: false })[0], /^<error>│<\/error>/, 'the tool own isError decides');
490
+ assert.match(result('worktree_create', textResult('boom'), { expanded: false, isPartial: false }, { isError: true })[0], /^<error>│<\/error>/, 'so does the row isError');
491
+ assert.match(result('worktree_create', textResult(CREATE_TEXT), { expanded: false, isPartial: true })[0], /^<warning>│<\/warning>/, 'a streamed result is still pending');
492
+
493
+ // The call's heading wears the same tone as its result: one card, one state.
494
+ assert.match(call('worktree_create', CREATE_ARGS, taggedTheme, { isPartial: false }, 40)[0], /^<warning>╭<\/warning>/, 'a finished create call is the warp tone');
495
+ assert.match(call('worktree_list', {}, taggedTheme, { isPartial: false }, 40)[0], /^<warning>╭<\/warning>/, 'and so is a finished list call');
496
+ assert.match(call('worktree_list', {}, taggedTheme, { isPartial: false, isError: true }, 40)[0], /^<error>╭<\/error>/, 'a failed call is still the error tone');
497
+
498
+ const recorder = recordingTheme();
499
+ resultRows('worktree_list', textResult('project: x'), { expanded: false, isPartial: false }, recorder, {}, 40);
500
+ assert.deepEqual([...new Set(recorder.roles)].sort(), ['text', 'warning'], 'a settled card paints the warp tone and the body text role, and no success role');
501
+ });
502
+
503
+ test('a result body is painted in the theme text role, and the heading keeps the tone', () => {
504
+ // The body is the tool's own text. The rows reach the substrate PLAIN, which wraps,
505
+ // clips and pads them and paints each one in the body role as its last step, so a
506
+ // short row reads back whole and inside its role. The fake here only re-wraps a
507
+ // line that does not fit.
508
+ const ansiMetrics = {
509
+ ...metrics,
510
+ wrapTextWithAnsi: (value, width) => (metrics.visibleWidth(value) <= width ? [value] : metrics.wrapTextWithAnsi(value, width)),
511
+ };
512
+ const made = createWorktreeCards({ metrics: ansiMetrics });
513
+ const settled = made.renderer('worktree_list').renderResult(textResult('project: x'), { isPartial: false }, taggedTheme, {}).render(30);
514
+ assert.match(settled[0], /^<warning>│<\/warning> <text>project: x<\/text>/, 'the body row carries the text role inside the tone frame');
515
+ assert.match(settled[0], /<warning>│<\/warning>$/, 'and the tone role closes it');
516
+ assert.equal(settled[0].includes('<success>'), false, 'no role outside the tone and the body text paints a row');
517
+
518
+ const failed = made.renderer('worktree_list').renderResult(textResult('boom', true), { isPartial: false }, taggedTheme, {}).render(30);
519
+ assert.match(failed[0], /^<error>│<\/error> <text>boom<\/text>/, 'a refusal body is text too, inside the error frame');
520
+ });
521
+
522
+ test('the elapsed time rides the closing rule and yields when the rule has no room', () => {
523
+ const withDuration = (durationMs, width) =>
524
+ resultRows('worktree_list', textResult('project: x'), { expanded: false, isPartial: false }, plainTheme, { durationMs }, width);
525
+
526
+ assert.equal(withDuration(1200, 40).at(-1), `╰${'─'.repeat(32)} 1.2s ╯`);
527
+ assert.equal(withDuration(42, 40).at(-1), `╰${'─'.repeat(32)} 42ms ╯`);
528
+ assert.equal(withDuration(1200, 12).at(-1), `╰──── 1.2s ╯`, 'three free fill columns are enough beyond the one the rule pads with');
529
+ assert.equal(withDuration(1200, 10).at(-1), `╰${'─'.repeat(8)}╯`, 'two free columns are not: the time yields to the plain rule');
530
+ assert.equal(withDuration(undefined, 40).at(-1), `╰${'─'.repeat(38)}╯`, 'no duration, no time on the rule');
531
+ const partial = resultRows('worktree_list', textResult('working'), { expanded: false, isPartial: true }, plainTheme, { durationMs: 1200 }, 40);
532
+ assert.equal(partial.at(-1).startsWith('╰'), false, 'a partial result does not close the frame yet');
533
+ });
534
+
535
+ // ---- purity ----
536
+
537
+ test('the module draws no ANSI and measures every row to the width it was given', () => {
538
+ for (const tool of ['worktree_create', 'worktree_list']) {
539
+ for (const width of [0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 30, 40, 60, 100]) {
540
+ const callRowsForWidth = [
541
+ ...call(tool, CREATE_ARGS, plainTheme, { isPartial: true }, width),
542
+ ...call(tool, CREATE_ARGS, plainTheme, { isPartial: false, expanded: true }, width),
543
+ ...resultRows(tool, textResult(CREATE_TEXT), { expanded: false, isPartial: false }, plainTheme, {}, width),
544
+ ...resultRows(tool, textResult(REFUSAL_TEXT, true), { expanded: true, isPartial: false }, plainTheme, { durationMs: 1200 }, width),
545
+ ...resultRows(tool, textResult(''), { expanded: false, isPartial: false }, plainTheme, {}, width),
546
+ ];
547
+ for (const row of callRowsForWidth) {
548
+ assert.equal(row.includes('\x1b'), false, `no escape at width ${width}`);
549
+ assert.equal(row.includes('\r'), false, `no carriage return at width ${width}`);
550
+ assert.ok(metrics.visibleWidth(row) <= width, `"${row}" overflows ${width}`);
551
+ }
552
+ }
553
+ }
554
+ });
555
+
556
+ test('the same inputs draw the same lines, and the source imports no host package', () => {
557
+ const once = resultRows('worktree_create', textResult(CREATE_TEXT), { expanded: true, isPartial: false }, taggedTheme, { durationMs: 1200 }, 60);
558
+ const twice = resultRows('worktree_create', textResult(CREATE_TEXT), { expanded: true, isPartial: false }, taggedTheme, { durationMs: 1200 }, 60);
559
+ assert.deepEqual(twice, once);
560
+
561
+ const source = readFileSync(join(__dirname, '..', 'lib', 'worktree-cards.js'), 'utf8');
562
+ assert.doesNotMatch(source, /@earendil-works/, 'no host package is required');
563
+ assert.doesNotMatch(source, /\\u001b|\\x1b/, 'no escape sequence is written down');
564
+ assert.doesNotMatch(source, /require\(['"](?!\.\/|node:)/, 'the only imports are the substrate and Node builtins, if any');
565
+ for (const forbidden of ['Date', 'process.', 'Math.random']) {
566
+ assert.doesNotMatch(source, new RegExp(forbidden.replace('.', '\\.')), `${forbidden} stays out: the module is a pure function of its inputs`);
567
+ }
568
+ });
569
+
570
+ // ---- S5: the two styles, one card ----
571
+
572
+ /** The card's text with every chrome cell removed: what the reader actually reads. */
573
+ function contentOf(rows) {
574
+ return rows
575
+ .map((row) => stripAnsi(row).replace(/[╭╮╰╯─│▎]/g, ' ').replace(/\s+/g, ' ').trim())
576
+ .filter((text) => text.length > 0)
577
+ .join(' | ');
578
+ }
579
+
580
+ // Short lines only, so neither style's two-column difference can move a wrap
581
+ // boundary: this is the "same text, different chrome" card.
582
+ const SHORT_LIST = ['project: gentle-shell', 'path: /clone', 'base: main@1a2b3c4', '1 cards-v2 · feat/cards-v2'].join('\n');
583
+ const CREATE_SHORT = ['worktree created: /clone-worktrees/cards-v2', 'branch: feat/cards-v2 (created)'].join('\n');
584
+
585
+ test('the same result card is the same text in float and in neon, and only the chrome differs', () => {
586
+ const theme = paintTheme();
587
+ for (const [tool, text] of [['worktree_list', SHORT_LIST], ['worktree_create', CREATE_SHORT]]) {
588
+ for (const expanded of [false, true]) {
589
+ const neon = withStyle(CARD_STYLE.NEON, () => resultRows(tool, textResult(text), { expanded, isPartial: false }, theme, { durationMs: 1200 }, 60));
590
+ const float = withStyle(CARD_STYLE.FLOAT, () => resultRows(tool, textResult(text), { expanded, isPartial: false }, theme, { durationMs: 1200 }, 60));
591
+ assert.equal(contentOf(float), contentOf(neon), `${tool} expanded=${expanded}: same body, same elapsed time`);
592
+ assert.equal(neon.some((row) => row.includes('▎')), false, `${tool}: neon draws no rail`);
593
+ assert.equal(float.some((row) => /[╭╮╰╯─│]/.test(row)), false, `${tool}: float draws no box`);
594
+ assert.ok(contentOf(float).endsWith('1.2s'), `${tool}: the elapsed time survives in both styles`);
595
+ for (const row of float) assert.equal(metrics.visibleWidth(row), 60, `${tool}: a float row is the width it was given`);
596
+ }
597
+ }
598
+ });
599
+
600
+ test('the collapsed three-row preview and its overflow mark survive the float style', () => {
601
+ const theme = paintTheme();
602
+ const long = Array.from({ length: 6 }, (_, index) => `row ${index + 1}`).join('\n');
603
+ const neon = withStyle(CARD_STYLE.NEON, () => resultRows('worktree_list', textResult(long), { expanded: false, isPartial: false }, theme, {}, 40));
604
+ const float = withStyle(CARD_STYLE.FLOAT, () => resultRows('worktree_list', textResult(long), { expanded: false, isPartial: false }, theme, {}, 40));
605
+ assert.equal(contentOf(float), contentOf(neon), 'three preview rows and the same overflow mark');
606
+ assert.match(contentOf(float), /row 3…/);
607
+ assert.equal(contentOf(float).includes('row 4'), false, 'the fourth row is still hidden');
608
+ });
609
+
610
+ test('a float card paints the accent rail and the tone background, and draws no frame', () => {
611
+ const theme = paintTheme();
612
+ const pending = withStyle(CARD_STYLE.FLOAT, () => call('worktree_create', CREATE_ARGS, theme, { isPartial: true }, 40));
613
+ for (const row of pending) {
614
+ assert.equal(metrics.visibleWidth(row), 40, `"${stripAnsi(row)}" is not 40 wide`);
615
+ assert.ok(row.startsWith(' \x1b[48;5;240m▎'), 'the margin, the background and the rail open every row');
616
+ assert.ok(row.endsWith('\x1b[49m '), 'the background closes before the right margin');
617
+ }
618
+ assert.deepEqual(theme.roles.bg, ['toolPendingBg'], 'the pending tone paints the pending background');
619
+ assert.equal(theme.roles.fg.includes('warning'), true, 'the rail and the label take the theme accent');
620
+ assert.equal(theme.roles.fg.includes('error'), false);
621
+ const visible = pending.map(stripAnsi);
622
+ assert.equal(visible[0].trim(), '▎', 'a padding row sits above the heading');
623
+ assert.match(visible[1], /^ ▎ ⎇ worktree create · feat\/cards-v2 +$/, 'the heading rides the rail');
624
+ assert.equal(visible[2].trim(), '▎', 'and a separator row sits between the heading and the body');
625
+ assert.match(visible[3], /^ ▎ running… +$/, 'the running row is painted like the rest of the card');
626
+
627
+ // The state still reaches the background; the accent stays the theme orange.
628
+ const settled = paintTheme();
629
+ withStyle(CARD_STYLE.FLOAT, () => resultRows('worktree_list', textResult(SHORT_LIST), { expanded: true, isPartial: false }, settled, {}, 40));
630
+ assert.deepEqual(settled.roles.bg, ['toolPendingBg'], 'a finished card keeps the warp tone, and with it the warp background');
631
+ assert.equal(settled.roles.fg.includes('warning'), true, 'a settled card paints the warp role on its rail and its label');
632
+ assert.equal(settled.roles.fg.includes('success'), false, 'the success role is gone from the card, in both styles');
633
+
634
+ const failed = paintTheme();
635
+ withStyle(CARD_STYLE.FLOAT, () => resultRows('worktree_list', textResult(REFUSAL_TEXT, true), { expanded: true, isPartial: false }, failed, {}, 40));
636
+ assert.deepEqual(failed.roles.bg, ['toolErrorBg']);
637
+ assert.equal(failed.roles.fg.includes('error'), true, 'a refusal keeps its tone role and its error background');
638
+ assert.equal(failed.roles.fg.includes('warning'), false);
639
+ });
640
+
641
+ test('a call and its result share one float card, set off by the separator row', () => {
642
+ const theme = paintTheme();
643
+ const result = withStyle(CARD_STYLE.FLOAT, () => resultRows('worktree_list', textResult(SHORT_LIST), { expanded: true, isPartial: false }, theme, {}, 40));
644
+ assert.equal(stripAnsi(result[0]).trim(), '▎', 'the result body opens with the separator row under the call above');
645
+ assert.match(stripAnsi(result[1]), /^ ▎ project: gentle-shell +$/);
646
+ assert.match(stripAnsi(result.at(-1)), /^ ▎ +$/, 'the closing row is the painted rule');
647
+
648
+ const partial = withStyle(CARD_STYLE.FLOAT, () => resultRows('worktree_list', textResult(SHORT_LIST), { expanded: true, isPartial: true }, theme, {}, 40));
649
+ assert.equal(stripAnsi(partial.at(-1)).includes('▎'), true, 'a streamed result still paints its rows');
650
+ assert.equal(partial.length, result.length - 1, 'but leaves the closing row to the final result');
651
+ });
652
+
653
+ test('too narrow for float, or a theme without the background, the neon card answers', () => {
654
+ const theme = paintTheme();
655
+ const narrow = withStyle(CARD_STYLE.FLOAT, () => call('worktree_list', {}, theme, { isPartial: false }, 9));
656
+ assert.match(stripAnsi(narrow[0]), /^╭─ /, 'nine columns is the neon frame');
657
+ assert.equal(narrow.some((row) => row.includes('\x1b')), false, 'and nothing is painted behind it');
658
+ assert.deepEqual(theme.roles.bg, [], 'the narrow card never asks for a background');
659
+
660
+ const noBackground = withStyle(CARD_STYLE.FLOAT, () => call('worktree_list', {}, plainTheme, { isPartial: false }, 40));
661
+ assert.match(stripAnsi(noBackground[0]), /^╭─ /, 'a theme with no bg keeps the frame');
662
+ });
663
+
664
+ test('the style is read on every render, so a live change reaches the very next card', () => {
665
+ const theme = paintTheme();
666
+ useStyle(CARD_STYLE.NEON);
667
+ try {
668
+ assert.match(stripAnsi(call('worktree_list', {}, theme, { isPartial: false }, 40)[0]), /^╭─ /);
669
+ useStyle(CARD_STYLE.FLOAT);
670
+ const float = call('worktree_list', {}, theme, { isPartial: false }, 40);
671
+ assert.match(stripAnsi(float[0]), /^ ▎ /, 'the same renderer, the next style');
672
+ useStyle(CARD_STYLE.NEON);
673
+ assert.match(stripAnsi(call('worktree_list', {}, theme, { isPartial: false }, 40)[0]), /^╭─ /, 'and back again, with no rebuild');
674
+ } finally {
675
+ useStyle(undefined);
676
+ }
677
+ });
678
+
679
+ // ---- the click area: one card, and only that card ----
680
+ //
681
+ // The installed Pi build delivers a click only to the component that CLAIMED the
682
+ // press (`@earendil-works/pi-tui` tui-alt-screen.js: `mousePressTarget` is
683
+ // recorded from the press dispatch and the synthesized `click` is retargeted to
684
+ // it), and the host's own `MouseRegion` around a tool card reacts to `click`
685
+ // alone — so a card that claims nothing is never clicked at all.
686
+ //
687
+ // A click moves THIS card only. The choice lives in the row's own `state` — the
688
+ // host hands one `rendererState` to every render of one execution and to no
689
+ // other (`tool-execution.js`) — and `ctrl+o`, the host's own session-wide flag,
690
+ // still wins over it.
691
+
692
+ /** The host's own expansion flag: `ctrl+o` flips it for every row at once. */
693
+ function hostFlag() {
694
+ return { expanded: false };
695
+ }
696
+
697
+ /**
698
+ * One tool row as the installed host drives it (`tool-execution.js`): ONE
699
+ * `rendererState` for the execution, a fresh render context per render — so the
700
+ * host flag arrives as a snapshot — and an `invalidate` that repaints only this
701
+ * row, by running the same render hook again against the same state.
702
+ */
703
+ function hostRow({ flag, tool = 'worktree_create', result = textResult(CREATE_TEXT), withInvalidate = true } = {}) {
704
+ const state = {};
705
+ const counter = { invalidations: 0, renders: 0 };
706
+ const build = () => {
707
+ const context = { isPartial: false, expanded: flag.expanded, state };
708
+ if (withInvalidate) context.invalidate = () => { counter.invalidations += 1; };
709
+ counter.renders += 1;
710
+ return cards.renderer(tool).renderResult(result, { expanded: flag.expanded, isPartial: false }, plainTheme, context);
711
+ };
712
+ const component = build();
713
+ const shown = component.render(60);
714
+ const send = (type, x = 2, y = 0, patch = {}) => component.handleMouse({
715
+ type,
716
+ button: 'left',
717
+ x,
718
+ y,
719
+ width: 60,
720
+ height: shown.length,
721
+ screenX: 0,
722
+ screenY: 0,
723
+ shift: false,
724
+ alt: false,
725
+ ctrl: false,
726
+ ...patch,
727
+ });
728
+ return {
729
+ component,
730
+ rows: shown,
731
+ counter,
732
+ press: (x, y) => send('press', x, y),
733
+ click: (x, y, patch) => send('click', x, y, patch),
734
+ repaint: () => build().render(60),
735
+ };
736
+ }
737
+
738
+ test('a click expands the card it hit and leaves its sibling collapsed', () => {
739
+ const flag = hostFlag();
740
+ const clicked = hostRow({ flag });
741
+ const sibling = hostRow({ flag });
742
+ const collapsedRows = clicked.rows.length;
743
+ assert.ok(collapsedRows > 1 && sibling.rows.length > 1, 'both cards draw before the click');
744
+
745
+ assert.deepEqual(clicked.press(2, 0), { handled: true, render: false }, 'the press claims the clicked card so the host retargets the click');
746
+ assert.equal(clicked.counter.invalidations, 0, 'a press alone never repaints');
747
+ assert.deepEqual(clicked.click(2, 0), { handled: true }, 'the click on the card is handled');
748
+ assert.equal(clicked.counter.invalidations, 1, 'the click repaints the card it hit');
749
+ assert.equal(sibling.counter.invalidations, 0, 'and never the card beside it');
750
+
751
+ assert.ok(clicked.repaint().length > collapsedRows, 'the clicked card is expanded');
752
+ assert.deepEqual(sibling.repaint(), sibling.rows, 'the sibling beside it is still exactly collapsed');
753
+ assert.equal(flag.expanded, false, 'the session-wide flag is left to ctrl+o');
754
+ });
755
+
756
+ test('a second click collapses the same card again', () => {
757
+ const flag = hostFlag();
758
+ const row = hostRow({ flag });
759
+ const collapsedRows = row.rows.length;
760
+
761
+ assert.deepEqual(row.click(2, 0), { handled: true });
762
+ assert.ok(row.repaint().length > collapsedRows, 'the first click expands');
763
+ assert.deepEqual(row.click(2, 0), { handled: true });
764
+ assert.deepEqual(row.repaint(), row.rows, 'the second click collapses it back');
765
+ assert.equal(row.counter.invalidations, 2, 'each click repainted this one card once');
766
+ });
767
+
768
+ test('ctrl+o still expands every card, and wins over an earlier click', () => {
769
+ const flag = hostFlag();
770
+ const clicked = hostRow({ flag });
771
+ const untouched = hostRow({ flag });
772
+ const collapsedRows = clicked.rows.length;
773
+
774
+ assert.deepEqual(clicked.click(2, 0), { handled: true });
775
+ assert.ok(clicked.repaint().length > collapsedRows, 'the click expanded its own card');
776
+
777
+ // ctrl+o: the host sets its flag on every expandable child.
778
+ flag.expanded = true;
779
+ const clickedByKey = clicked.repaint();
780
+ const untouchedByKey = untouched.repaint();
781
+ assert.deepEqual(clickedByKey, untouchedByKey, 'after ctrl+o both cards draw the same expanded body');
782
+ assert.ok(clickedByKey.length > collapsedRows, 'ctrl+o expanded the card a click had already expanded');
783
+
784
+ flag.expanded = false;
785
+ assert.deepEqual(clicked.repaint(), clicked.rows, 'a collapse from ctrl+o wins over the earlier click too');
786
+ assert.deepEqual(untouched.repaint(), untouched.rows, 'and the card no one clicked follows the host in both directions');
787
+ assert.equal(flag.expanded, false);
788
+ });
789
+
790
+ test('a click on a card ctrl+o expanded collapses only that card', () => {
791
+ const flag = hostFlag();
792
+ flag.expanded = true;
793
+ const clicked = hostRow({ flag });
794
+ const untouched = hostRow({ flag });
795
+ const expandedRows = clicked.rows.length;
796
+
797
+ assert.deepEqual(clicked.click(2, 0), { handled: true });
798
+ assert.ok(clicked.repaint().length < expandedRows, 'the click collapsed the card it hit');
799
+ assert.deepEqual(untouched.repaint(), untouched.rows, 'the card beside it stays expanded, exactly as ctrl+o left it');
800
+ });
801
+
802
+ test('a render context with no invalidate still expands on click and never throws', () => {
803
+ const flag = hostFlag();
804
+ const row = hostRow({ flag, withInvalidate: false });
805
+
806
+ assert.deepEqual(row.click(2, 0), { handled: true }, 'a host without the seam still gets the click');
807
+ assert.equal(row.counter.renders, 1, 'and the card never repainted itself behind a host that has no seam');
808
+ assert.ok(row.repaint().length > row.rows.length, 'the choice is applied by the next render the host does');
809
+ });
810
+
811
+ test('the whole drawn card is clickable, and nothing outside it is claimed', () => {
812
+ const flag = hostFlag();
813
+ const row = hostRow({ flag });
814
+ for (const y of [0, 1, row.rows.length - 1]) {
815
+ assert.deepEqual(row.press(2, y), { handled: true, render: false }, `row ${y} is on the card`);
816
+ assert.deepEqual(row.click(2, y), { handled: true }, `row ${y} is clickable`);
817
+ }
818
+ assert.equal(row.counter.invalidations, 3, 'each click repainted this one card once');
819
+
820
+ assert.equal(row.click(0, row.rows.length), undefined, 'a row below the card is the host\'s');
821
+ assert.equal(row.click(60, 0), undefined, 'a column right of the card is the host\'s');
822
+ assert.equal(row.click(0, 0, { button: 'right' }), undefined, 'the right button is the host\'s');
823
+ assert.equal(row.click(0, 0, { button: 'middle' }), undefined);
824
+ for (const type of ['move', 'drag', 'release', 'wheel']) {
825
+ assert.equal(row.click(0, 0, { type }), undefined, `a ${type} is not a click`);
826
+ }
827
+ assert.equal(row.counter.invalidations, 3, 'nothing outside the card moved it');
828
+
829
+ // The heading's hint cells are inside the card, so they are clicked like any other.
830
+ const hint = 'ctrl+o to expand';
831
+ const hinted = cards.renderer('worktree_create').renderCall({}, plainTheme, { isPartial: false, keyHint: () => hint, state: {}, expanded: false });
832
+ const hintRows = hinted.render(60);
833
+ const hintStart = stripAnsi(hintRows[0]).indexOf(hint);
834
+ assert.ok(hintStart > 0, 'the heading carries the hint');
835
+ assert.deepEqual(hinted.handleMouse({ type: 'click', button: 'left', x: hintStart + 2, y: 0, width: 60, height: hintRows.length, screenX: 0, screenY: 0, shift: false, alt: false, ctrl: false }), { handled: true }, 'the hint cells are on the card');
836
+ });
837
+
838
+ test('both halves of a card claim their own cells with no injected seam', () => {
839
+ for (const tool of WORKTREE_CARD_TOOLS) {
840
+ const callComponent = cards.renderer(tool).renderCall({}, plainTheme, { isPartial: false, state: {} });
841
+ const resultComponent = cards.renderer(tool).renderResult(textResult(CREATE_TEXT), {}, plainTheme, { state: {} });
842
+ assert.equal(typeof callComponent.handleMouse, 'function', `${tool} call: the card claims its own cells`);
843
+ assert.equal(typeof resultComponent.handleMouse, 'function', `${tool} result: the card claims its own cells`);
844
+ }
845
+ });