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,126 @@
1
+ 'use strict';
2
+ // T11 (S13, issues-startup-performance): the LOCAL executable lookup.
3
+ //
4
+ // Contract under test, through the module's one public function:
5
+ // - a command is answered from the `PATH` it was given, first hit wins, and
6
+ // only an EXECUTABLE file counts — a plain file, a directory and a missing
7
+ // directory are all skipped;
8
+ // - an absolute path is tested itself, with no `PATH` at all;
9
+ // - the answer is the platform's: `;` and `PATHEXT` on win32, `:` and the bare
10
+ // name everywhere else, both injectable so a test never needs that platform;
11
+ // - nothing is read from `process.env` behind the caller's back, nothing is
12
+ // memoized and nothing is executed: the module holds no child process at all,
13
+ // which is the whole point of the check it exists for.
14
+ const { test, after } = require('node:test');
15
+ const assert = require('node:assert/strict');
16
+ const fs = require('node:fs');
17
+ const os = require('node:os');
18
+ const path = require('node:path');
19
+
20
+ const { whichCommand } = require('../lib/which-command.js');
21
+
22
+ const scratch = [];
23
+ function tempDir(tag) {
24
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), `odd-which-${tag}-`));
25
+ scratch.push(dir);
26
+ return dir;
27
+ }
28
+
29
+ // A file at `<dir>/<name>` with the mode a test asks for. 0o755 is what a real
30
+ // installed binary has; 0o644 is the "not a command" case.
31
+ function executable(dir, name, mode = 0o755) {
32
+ const file = path.join(dir, name);
33
+ fs.writeFileSync(file, '#!/bin/sh\nexit 0\n');
34
+ fs.chmodSync(file, mode);
35
+ return file;
36
+ }
37
+
38
+ after(() => {
39
+ for (const dir of scratch) {
40
+ try { fs.rmSync(dir, { recursive: true, force: true }); } catch { /* best effort */ }
41
+ }
42
+ });
43
+
44
+ test('finds an executable of that name on the given PATH and answers its whole path', () => {
45
+ const bin = tempDir('found');
46
+ const file = executable(bin, 'gh');
47
+ assert.equal(whichCommand('gh', { env: { PATH: bin }, isExecutable: (candidate) => fs.existsSync(candidate) }), file);
48
+ });
49
+
50
+ test('a plain file, a directory and a name nothing holds are not the command', () => {
51
+ const bin = tempDir('not-executable');
52
+ executable(bin, 'gh', 0o644);
53
+ fs.mkdirSync(path.join(bin, 'gh-dir'));
54
+ assert.equal(whichCommand('gh', { env: { PATH: bin } }), null, 'a non-executable file is not a command');
55
+ assert.equal(whichCommand('gh-dir', { env: { PATH: bin } }), null, 'a directory is not a command');
56
+ assert.equal(whichCommand('nothing-here', { env: { PATH: bin } }), null, 'and an absent name is null');
57
+ });
58
+
59
+ test('the first PATH entry wins, and empty or missing entries are skipped', () => {
60
+ const first = tempDir('first');
61
+ const second = tempDir('second');
62
+ const firstFile = executable(first, 'gh');
63
+ const secondFile = executable(second, 'gh');
64
+ assert.equal(whichCommand('gh', { env: { PATH: `${first}${path.delimiter}${second}` } }), firstFile);
65
+ assert.equal(whichCommand('gh', { env: { PATH: `:${path.delimiter}${second}` } }), secondFile,
66
+ 'an empty leading entry is not a directory to search');
67
+ assert.equal(whichCommand('gh', { env: { PATH: path.join(first, 'does-not-exist') } }), null,
68
+ 'a PATH entry that does not exist is skipped');
69
+ });
70
+
71
+ test('an absent or empty PATH, and an empty name, answer null instead of throwing', () => {
72
+ assert.equal(whichCommand('gh', { env: {} }), null);
73
+ assert.equal(whichCommand('gh', { env: { PATH: '' } }), null);
74
+ assert.equal(whichCommand('', { env: { PATH: tempDir('empty-name') } }), null);
75
+ assert.equal(whichCommand(null, { env: { PATH: '/usr/bin' } }), null);
76
+ assert.equal(whichCommand('gh'), null, 'no environment named means no PATH to search, never this process\'s own');
77
+ });
78
+
79
+ test('an absolute path is tested itself, with no PATH involved', () => {
80
+ const bin = tempDir('absolute');
81
+ const file = executable(bin, 'gh');
82
+ assert.equal(whichCommand(file, { env: { PATH: '' } }), file);
83
+ assert.equal(whichCommand(path.join(bin, 'missing-gh'), { env: { PATH: bin } }), null);
84
+ });
85
+
86
+ test('win32 searches with `;` and PATHEXT, and answers the extension it found', () => {
87
+ const looked = [];
88
+ const isExecutable = (candidate) => {
89
+ looked.push(candidate);
90
+ return candidate === 'C:\\Tools\\gh.EXE';
91
+ };
92
+ assert.equal(
93
+ whichCommand('gh', {
94
+ env: { PATH: 'C:\\Windows;C:\\Tools', PATHEXT: '.EXE;.CMD' },
95
+ platform: 'win32',
96
+ isExecutable,
97
+ }),
98
+ 'C:\\Tools\\gh.EXE',
99
+ );
100
+ assert.deepEqual(looked, [
101
+ 'C:\\Windows\\gh',
102
+ 'C:\\Windows\\gh.EXE',
103
+ 'C:\\Windows\\gh.CMD',
104
+ 'C:\\Tools\\gh',
105
+ 'C:\\Tools\\gh.EXE',
106
+ ], 'the bare name then every PATHEXT extension, directory by directory');
107
+ });
108
+
109
+ test('the environment it reads is the one it was given, never process.env', () => {
110
+ const bin = tempDir('injected');
111
+ const file = executable(bin, 'gh');
112
+ const saved = process.env.PATH;
113
+ try {
114
+ process.env.PATH = path.join(bin, 'not-on-path');
115
+ assert.equal(whichCommand('gh', { env: { PATH: bin } }), file,
116
+ 'the injected PATH decides, whatever this process happens to have');
117
+ } finally {
118
+ process.env.PATH = saved;
119
+ }
120
+ });
121
+
122
+ test('the probe runs nothing: the module holds no child process', () => {
123
+ const source = fs.readFileSync(path.join(__dirname, '../lib/which-command.js'), 'utf8');
124
+ assert.equal(/child_process/.test(source), false, 'no child process, no spawn, no exec');
125
+ assert.equal(/\bexecFile\b|\bspawnSync\b|\bspawn\(/.test(source), false, 'and no way to run the candidate');
126
+ });
@@ -0,0 +1,551 @@
1
+ 'use strict';
2
+ // S5/S6: the `/warp` message card — the third bash surface — drawn on the same
3
+ // substrate as the two tool cards. Every case reads the public surface of
4
+ // lib/worktree-card.js (`createWarpMessageRenderer`) and asserts what a reader
5
+ // sees: the heading, the two ordered rows, the path and branch an expanded card
6
+ // adds, the ctrl+o hint and the closing rule — in whichever style the shell chose.
7
+ //
8
+ // The style is the process-wide slot Gentle Shell publishes with the user's
9
+ // document as the fallback, so this suite points `GENTLE_PI_CONFIG_HOME` at a
10
+ // scratch directory: no case reads or writes the real user's preference, and the
11
+ // slot is cleared after each case that sets it.
12
+ const { test, before, after } = require('node:test');
13
+ const assert = require('node:assert/strict');
14
+ const { mkdtempSync, readFileSync, rmSync, unlinkSync, writeFileSync } = require('node:fs');
15
+ const { tmpdir } = require('node:os');
16
+ const { join } = require('node:path');
17
+ const { createWarpMessageRenderer, safeText, stripAnsi, wrapTextToWidth } = require('../lib/worktree-card.js');
18
+ const { CARD_STYLE_SCHEMA } = require('../lib/card-style.js');
19
+ const { CARD_CONTENT } = require('../lib/card-content.js');
20
+
21
+ // ---- the style seam: the process-wide slot, and a scratch preference home ----
22
+
23
+ const CARD_STYLE_SLOT = Symbol.for('gentle-pi.card-style');
24
+
25
+ function useStyle(style) {
26
+ if (style === undefined) delete globalThis[CARD_STYLE_SLOT];
27
+ else globalThis[CARD_STYLE_SLOT] = style;
28
+ }
29
+
30
+ function withStyle(style, run) {
31
+ useStyle(style);
32
+ try {
33
+ return run();
34
+ } finally {
35
+ useStyle(undefined);
36
+ }
37
+ }
38
+
39
+ let configHome;
40
+ const savedHome = process.env.GENTLE_PI_CONFIG_HOME;
41
+ const styleDocument = () => join(configHome, 'card-style.json');
42
+
43
+ function writeStyleDocument(contents) {
44
+ writeFileSync(styleDocument(), typeof contents === 'string' ? contents : JSON.stringify(contents));
45
+ }
46
+
47
+ function removeStyleDocument() {
48
+ try {
49
+ unlinkSync(styleDocument());
50
+ } catch {
51
+ // Already absent: the missing document is the case under test.
52
+ }
53
+ }
54
+
55
+ before(() => {
56
+ configHome = mkdtempSync(join(tmpdir(), 'odd-warp-card-style-'));
57
+ process.env.GENTLE_PI_CONFIG_HOME = configHome;
58
+ useStyle(undefined);
59
+ });
60
+
61
+ after(() => {
62
+ useStyle(undefined);
63
+ if (savedHome === undefined) delete process.env.GENTLE_PI_CONFIG_HOME;
64
+ else process.env.GENTLE_PI_CONFIG_HOME = savedHome;
65
+ rmSync(configHome, { recursive: true, force: true });
66
+ });
67
+
68
+ // ---- the injected text seam (fakes: plain ASCII, each character one column) ----
69
+
70
+ const stripAnsiAll = (value) => value.replace(/\x1b\[[0-9;]*m/g, '');
71
+ const chars = (value) => [...stripAnsiAll(value)];
72
+
73
+ const metrics = {
74
+ visibleWidth(value) {
75
+ return chars(value).length;
76
+ },
77
+ truncateToWidth(value, width, ellipsis = '') {
78
+ if (width <= 0) return '';
79
+ if (this.visibleWidth(value) <= width) return value;
80
+ const keep = Math.max(0, width - [...String(ellipsis ?? '')].length);
81
+ return chars(value).slice(0, keep).join('') + String(ellipsis ?? '');
82
+ },
83
+ };
84
+
85
+ // ---- themes ----
86
+
87
+ const plainTheme = {
88
+ fg(_role, value) {
89
+ return value;
90
+ },
91
+ };
92
+
93
+ // The background reset a Pi theme hands back at the end of `bg(role, "")`. It is
94
+ // spelled here, in the test, so the module under test carries no raw escape.
95
+ const BG_RESET = '\x1b[49m';
96
+
97
+ /** A plain theme that can also paint the float background and records every role. */
98
+ function paintTheme() {
99
+ const roles = { fg: [], bg: [] };
100
+ return {
101
+ roles,
102
+ fg(role, value) {
103
+ roles.fg.push(role);
104
+ return value;
105
+ },
106
+ bg(role, value) {
107
+ roles.bg.push(role);
108
+ return `\x1b[48;5;240m${value}${BG_RESET}`;
109
+ },
110
+ };
111
+ }
112
+
113
+ const MESSAGE = {
114
+ customType: 'worktree-warp',
115
+ content: 'session warped to feat-two (this prose never enters the card)',
116
+ details: {
117
+ fromName: 'feat-one',
118
+ name: 'feat-two',
119
+ to: '/clone-worktrees/feat-two',
120
+ branch: 'feat/two',
121
+ },
122
+ };
123
+
124
+ /** The renderer the extension registers, with the host's own text seam injected. */
125
+ function renderer(helpers = metrics) {
126
+ return createWarpMessageRenderer(helpers);
127
+ }
128
+
129
+ function renderWarp(style, { message = MESSAGE, options = {}, theme = plainTheme } = {}) {
130
+ return withStyle(style, () => renderer()(message, options, theme).render(60));
131
+ }
132
+
133
+ /** The card's text with every chrome cell removed: what the reader actually reads. */
134
+ function contentOf(rows) {
135
+ return rows
136
+ .map((row) => stripAnsiAll(row).replace(/[╭╮╰╯─│▎]/g, ' ').replace(/\s+/g, ' ').trim())
137
+ .filter((text) => text.length > 0)
138
+ .join(' | ');
139
+ }
140
+
141
+ // ---- what the renderer serves ----
142
+
143
+ test('the renderer serves this package\'s warp message and nothing else', () => {
144
+ const render = renderer();
145
+ assert.equal(render(undefined, {}, plainTheme), undefined);
146
+ assert.equal(render({ customType: 'other', content: 'x' }, {}, plainTheme), undefined);
147
+ assert.ok(render(MESSAGE, {}, plainTheme), 'the worktree-warp message is drawn');
148
+ });
149
+
150
+ // ---- S6: one style decision per render, in both styles ----
151
+
152
+ test('the neon style draws the rounded frame around the warp card', () => {
153
+ const rows = renderWarp('neon');
154
+ assert.match(stripAnsiAll(rows[0]), /^╭─ \u2387 Warp · moved ─+ ctrl\+o to expand ╮$/, 'the heading rides the top rule with the expand hint');
155
+ assert.equal(stripAnsiAll(rows[rows.length - 1]).startsWith('╰'), true, 'the card closes with a rounded rule');
156
+ for (const row of rows) {
157
+ assert.equal(metrics.visibleWidth(row), 60, `"${stripAnsiAll(row)}" is not 60 wide`);
158
+ assert.doesNotMatch(stripAnsiAll(row), /▎/, 'the neon style draws no accent rail');
159
+ }
160
+ assert.equal(contentOf(rows).includes('this prose never enters the card'), false, 'the prose notice stays out of the card');
161
+ });
162
+
163
+ test('the float style draws the accent rail over the tone background and no box', () => {
164
+ const theme = paintTheme();
165
+ const rows = renderWarp('float', { theme });
166
+ for (const row of rows) {
167
+ assert.equal(metrics.visibleWidth(stripAnsiAll(row)), 60, 'a float row is the width the host asked for');
168
+ assert.ok(row.startsWith(' \x1b[48;5;240m▎'), 'the margin, the background and the rail open every row');
169
+ assert.ok(row.endsWith('\x1b[49m '), 'the background closes before the right margin');
170
+ assert.doesNotMatch(stripAnsiAll(row), /[╭╮╰╯─│]/, 'the float style draws no box');
171
+ }
172
+ assert.deepEqual(theme.roles.bg, ['toolPendingBg'], 'the warp tone paints the pending background');
173
+ assert.equal(theme.roles.fg.includes('warning'), true, 'the rail takes the theme accent');
174
+ });
175
+
176
+ test('the warp card is the same text in float and in neon, and only the chrome differs', () => {
177
+ const theme = paintTheme();
178
+ for (const expanded of [false, true]) {
179
+ const options = { expanded };
180
+ const neon = withStyle('neon', () => renderer()(MESSAGE, options, theme).render(60));
181
+ const float = withStyle('float', () => renderer()(MESSAGE, options, theme).render(60));
182
+ assert.equal(contentOf(float), contentOf(neon), `expanded=${expanded}: same heading, same rows, same order`);
183
+ assert.equal(float.length, neon.length + 2, `expanded=${expanded}: float adds its two padding rows and nothing else`);
184
+ }
185
+ const compact = contentOf(renderWarp('neon'));
186
+ assert.match(compact, /\u2387 Warp · moved/);
187
+ assert.match(compact, /moved: feat-one \| to: feat-two/, 'the two name-only rows keep their order');
188
+ });
189
+
190
+ test('the frame and the label carry the warning tone, the body the text role', () => {
191
+ for (const style of ['neon', 'float']) {
192
+ const theme = paintTheme();
193
+ renderWarp(style, { theme });
194
+ assert.deepEqual([...new Set(theme.roles.fg)].sort(), ['dim', 'muted', 'text', 'warning'], `${style}: the frame and the label take the tone, the body text, the detail muted and the hint dim`);
195
+ for (const role of ['border', 'success', 'error', 'accent']) {
196
+ assert.equal(theme.roles.fg.includes(role), false, `${style}: no role outside the tone, the body, the detail and the hint`);
197
+ }
198
+ }
199
+ });
200
+
201
+ test('a warp body row is painted in the theme text role, like the two tool cards', () => {
202
+ // The fake here only re-wraps a line that does not fit, so the short rows below
203
+ // read back whole with their role intact (the real wrapTextWithAnsi is ANSI aware).
204
+ const ansi = {
205
+ ...metrics,
206
+ wrapTextWithAnsi: (value, width) => (metrics.visibleWidth(value) <= width ? [value] : wrapTextToWidth(value, width, metrics)),
207
+ };
208
+ const tagged = {
209
+ fg: (role, value) => `<${role}>${value}</${role}>`,
210
+ bg: (role, value) => `\x1b[48;5;240m${value}\x1b[49m`,
211
+ };
212
+ for (const style of ['neon', 'float']) {
213
+ const rows = withStyle(style, () => createWarpMessageRenderer(ansi)(MESSAGE, {}, tagged).render(60));
214
+ const body = rows.filter((row) => stripAnsi(row).includes('moved: feat-one'));
215
+ assert.equal(body.length, 1, `${style}: the compact body row is drawn once`);
216
+ assert.match(body[0], /^.*<text>moved: feat-one<\/text>/, `${style}: the body row carries the text role`);
217
+ assert.equal(body[0].includes('<warning>moved'), false, `${style}: the body never borrows the chrome's tone`);
218
+ }
219
+ });
220
+
221
+ test('a real measured span rides the closing rule bottom-right, in both styles', () => {
222
+ const timed = (durationMs) => ({ ...MESSAGE, details: { ...MESSAGE.details, durationMs } });
223
+ const neon = withStyle('neon', () => renderer()(timed(1200), {}, plainTheme).render(60));
224
+ assert.equal(neon[neon.length - 1], `╰${'─'.repeat(52)} 1.2s ╯`, 'the elapsed time is the last thing before the right corner');
225
+ assert.equal(metrics.visibleWidth(neon[neon.length - 1]), 60);
226
+
227
+ const millis = withStyle('neon', () => renderer()(timed(42), {}, plainTheme).render(60));
228
+ assert.equal(millis[millis.length - 1], `╰${'─'.repeat(52)} 42ms ╯`, 'a sub-second span reads in milliseconds');
229
+
230
+ const float = withStyle('float', () => renderer()(timed(1200), {}, paintTheme()).render(60));
231
+ assert.match(stripAnsiAll(float[float.length - 1]), /^ ▎ +1\.2s +$/, 'the float closing row carries the same time, bottom-right');
232
+ for (const row of float) assert.equal(metrics.visibleWidth(stripAnsiAll(row)), 60, 'the timed float card keeps its width');
233
+
234
+ // An expanded card keeps the same closing rule, and the time is not repeated.
235
+ const expanded = withStyle('neon', () => renderer()(timed(1200), { expanded: true }, plainTheme).render(60));
236
+ assert.equal(expanded[expanded.length - 1], `╰${'─'.repeat(52)} 1.2s ╯`);
237
+ assert.equal(expanded.filter((row) => stripAnsiAll(row).includes('1.2s')).length, 1, 'exactly one row carries the time');
238
+ });
239
+
240
+ test('a warp card with no measured span shows no elapsed time at all', () => {
241
+ const closing = (message, style = 'neon') => {
242
+ const rows = withStyle(style, () => renderer()(message, {}, plainTheme).render(60));
243
+ return rows[rows.length - 1];
244
+ };
245
+ const plain = `╰${'─'.repeat(58)}╯`;
246
+ assert.equal(closing(MESSAGE), plain, 'no durationMs: the plain rule, no 0ms and no placeholder');
247
+ for (const value of ['1200', 'soon', null, -5, Number.NaN, Number.POSITIVE_INFINITY, {}, []]) {
248
+ const message = { ...MESSAGE, details: { ...MESSAGE.details, durationMs: value } };
249
+ assert.equal(closing(message), plain, `${JSON.stringify(value)} is not a measured span`);
250
+ }
251
+ const float = withStyle('float', () => renderer()(MESSAGE, {}, paintTheme()).render(60));
252
+ assert.match(stripAnsiAll(float[float.length - 1]), /^ ▎ +$/, 'the float closing row carries nothing either');
253
+ });
254
+
255
+ test('the style is read live: the global slot wins over a float document, once per render', () => {
256
+ writeStyleDocument({ schema: CARD_STYLE_SCHEMA, style: 'float' });
257
+ try {
258
+ const floated = renderWarp(undefined, { theme: paintTheme() });
259
+ assert.match(stripAnsiAll(floated[1]), /^ ▎ \u2387 Warp · moved/, 'the document says float: the rail');
260
+
261
+ const neon = renderWarp('neon');
262
+ assert.match(stripAnsiAll(neon[0]), /^╭─ \u2387 Warp · moved/, 'the slot says neon while the document says float: the frame wins');
263
+ assert.equal(neon.some((row) => row.includes('▎')), false, 'and no row keeps a rail');
264
+
265
+ const float = renderWarp('float', { theme: paintTheme() });
266
+ assert.match(stripAnsiAll(float[1]), /^ ▎ \u2387 Warp · moved/, 'the slot says float: the whole card is float again');
267
+ } finally {
268
+ removeStyleDocument();
269
+ }
270
+ });
271
+
272
+ test('a theme without the background role falls back to the neon outline', () => {
273
+ const rows = renderWarp('float');
274
+ assert.match(stripAnsiAll(rows[0]), /^╭─ \u2387 Warp · moved/, 'no background role: the neon frame');
275
+ assert.equal(rows.some((row) => row.includes('\x1b')), false, 'and no background is painted');
276
+ });
277
+
278
+ // ---- the content level: `minimal` is the heading line alone ----
279
+ //
280
+ // The level is the user's choice in Gentle Shell, published on the process-wide slot
281
+ // `lib/card-content.js` reads — the same level the two tool cards and the CodeGraph
282
+ // card read. The renderer takes the same reader seam (`cardContent`), so a case
283
+ // injects a level instead of touching the real slot, and the default reader stays
284
+ // the real one, asked on every paint.
285
+ const CARD_CONTENT_SLOT = Symbol.for('gentle-pi.card-content');
286
+
287
+ /** Run with the process-wide level slot set, restoring whatever it held. */
288
+ function withContent(level, run) {
289
+ const before = globalThis[CARD_CONTENT_SLOT];
290
+ if (level === undefined) delete globalThis[CARD_CONTENT_SLOT];
291
+ else globalThis[CARD_CONTENT_SLOT] = level;
292
+ try {
293
+ return run();
294
+ } finally {
295
+ if (before === undefined) delete globalThis[CARD_CONTENT_SLOT];
296
+ else globalThis[CARD_CONTENT_SLOT] = before;
297
+ }
298
+ }
299
+
300
+ test('a minimal warp card is the heading line alone, and the level is read on every paint', () => {
301
+ let level = CARD_CONTENT.DEFAULT;
302
+ const render = createWarpMessageRenderer({ ...metrics, cardContent: () => level });
303
+ const component = render(messageFor('feat-two'), {}, plainTheme);
304
+ const compact = component.render(60);
305
+ assert.equal(compact.length, 4, 'the heading, the two name-only rows and the closing rule');
306
+ assert.match(contentOf(compact), /moved: feat-one \| to: feat-two/);
307
+
308
+ level = CARD_CONTENT.MINIMAL;
309
+ const minimal = component.render(60);
310
+ assert.deepEqual(minimal, [compact[0], compact.at(-1)], 'the heading and the closing rule, with no row between them');
311
+ assert.equal(contentOf(minimal).includes('moved: feat-one'), false, 'the minimal card carries no body row');
312
+
313
+ level = CARD_CONTENT.DEFAULT;
314
+ assert.deepEqual(component.render(60), compact, 'and back again, with no new renderer');
315
+ });
316
+
317
+ test('a minimal warp card shows the same heading in float and in neon', () => {
318
+ // No injected reader here: the renderer reads the real slot on every paint.
319
+ const lines = (style) => withStyle(style, () => withContent(CARD_CONTENT.MINIMAL, () => renderer()(messageFor('feat-two'), {}, paintTheme()).render(60)));
320
+ const neon = lines('neon');
321
+ const float = lines('float');
322
+ assert.equal(neon.length, 2, 'the heading and the closing rule');
323
+ assert.equal(float.length, 3, 'float adds its padding row above the heading, and no body row');
324
+ assert.equal(contentOf(float), contentOf(neon), 'the same text in both styles; only the chrome differs');
325
+ assert.equal(contentOf(float).includes('moved: feat-one'), false, 'no body row in either style');
326
+ for (const row of float) assert.equal(metrics.visibleWidth(stripAnsiAll(row)), 60, 'a float row is the width it was given');
327
+ assert.equal(float.some((row) => /[╭╮╰╯─│]/.test(row)), false, 'the float card draws no box');
328
+ });
329
+
330
+ test('an expanded warp card shows everything at either level', () => {
331
+ const expandedAt = (level) => withContent(level, () => renderer()(messageFor('feat-two'), { expanded: true }, plainTheme).render(60));
332
+ const atDefault = expandedAt(CARD_CONTENT.DEFAULT);
333
+ assert.deepEqual(expandedAt(CARD_CONTENT.MINIMAL), atDefault, 'the reader asked for everything and gets everything');
334
+ assert.match(contentOf(atDefault), /path: now in "\/clone-worktrees\/feat-two"/, 'including the path and the branch');
335
+ });
336
+
337
+ // ---- the content the card has always carried ----
338
+
339
+ test('an expanded card appends the path and the branch, in that order and nothing else', () => {
340
+ const withoutHint = (text) => text.replace(/ctrl\+o to (?:expand|collapse)/g, '').replace(/\s+/g, ' ').trim();
341
+ const compact = withoutHint(contentOf(renderWarp('neon')));
342
+ const expanded = withoutHint(contentOf(renderWarp('neon', { options: { expanded: true } })));
343
+ assert.equal(expanded, `${compact} | path: now in "/clone-worktrees/feat-two" | branch: now in "feat/two"`);
344
+ assert.match(stripAnsiAll(renderWarp('neon', { options: { expanded: true } })[0]), /ctrl\+o to collapse/, 'the hint offers the collapse');
345
+ });
346
+
347
+ test('a malformed payload falls back to the message content, never to a throw', () => {
348
+ const broken = { customType: 'worktree-warp', content: 'moved somewhere', details: 'not an object' };
349
+ const rows = withStyle('neon', () => renderer()(broken, {}, plainTheme).render(60));
350
+ assert.match(contentOf(rows), /moved somewhere/);
351
+ const empty = { customType: 'worktree-warp', content: '', details: {} };
352
+ const blank = withStyle('neon', () => renderer()(empty, {}, plainTheme).render(60));
353
+ assert.match(contentOf(blank), /\(no output\)/);
354
+ });
355
+
356
+ test('a legacy payload without fromName reads the source name from fromDesc', () => {
357
+ const legacy = { customType: 'worktree-warp', content: 'moved', details: { fromDesc: 'feat-one · some note', name: 'feat-two' } };
358
+ assert.match(contentOf(withStyle('neon', () => renderer()(legacy, {}, plainTheme).render(60))), /moved: feat-one \| to: feat-two/);
359
+ });
360
+
361
+ test('untrusted text is sanitized before it can reshape the transcript', () => {
362
+ const hostile = { customType: 'worktree-warp', content: 'x', details: { fromName: 'a\x1b[31mred\x1b[0m\r\nb', name: 'n\nsecond' } };
363
+ const rows = withStyle('neon', () => renderer()(hostile, {}, plainTheme).render(60));
364
+ assert.equal(rows.some((row) => row.includes('\x1b')), false, 'no escape reaches the card');
365
+ assert.equal(rows.some((row) => row.includes('\r')), false, 'no carriage return either');
366
+ assert.match(contentOf(rows), /moved: ared \| to: n/);
367
+ assert.equal(safeText('a\x1b[31mb'), 'ab', 'the sanitizer keeps its contract');
368
+ assert.equal(stripAnsi('\x1b[1mx\x1b[0m'), 'x');
369
+ });
370
+
371
+ test('a hostile theme or host helper never breaks the card', () => {
372
+ const throwingTheme = {
373
+ fg() {
374
+ throw new Error('no such role');
375
+ },
376
+ bg() {
377
+ throw new Error('no such role');
378
+ },
379
+ };
380
+ assert.doesNotThrow(() => withStyle('neon', () => renderer()(MESSAGE, {}, throwingTheme).render(60)));
381
+ assert.doesNotThrow(() => withStyle('float', () => renderer()(MESSAGE, {}, throwingTheme).render(60)));
382
+ const noHelpers = withStyle('neon', () => createWarpMessageRenderer(undefined)(MESSAGE, {}, plainTheme).render(40));
383
+ assert.match(contentOf(noHelpers), /moved: feat-one/);
384
+ });
385
+
386
+ test('too narrow for float, the card draws the neon outline instead of a broken rail', () => {
387
+ const float = withStyle('float', () => renderer()(MESSAGE, {}, paintTheme()).render(9));
388
+ assert.match(stripAnsiAll(float[0]), /^╭/, 'nine columns is the neon frame');
389
+ assert.equal(float.some((row) => row.includes('\x1b')), false);
390
+ });
391
+
392
+ test('the host outputPad is applied by the card itself, in both styles', () => {
393
+ for (const style of ['neon', 'float']) {
394
+ const rows = withStyle(style, () => renderer()(MESSAGE, { outputPad: 1 }, paintTheme()).render(60));
395
+ for (const row of rows) {
396
+ assert.ok(row.startsWith(' '), `${style}: the left margin is the host pad`);
397
+ assert.equal(metrics.visibleWidth(stripAnsiAll(row)), 60, `${style}: the padded card still fills the row`);
398
+ }
399
+ }
400
+ });
401
+
402
+ // Every row's content with the chrome cells and the padding removed, joined with
403
+ // no separator: a value the card wrapped therefore reads back as one run.
404
+ function flowedText(rows) {
405
+ return rows
406
+ .map((row) => stripAnsiAll(row).replace(/[╭╮╰╯─│▎]/g, '').replace(/\s+/g, ' ').trim())
407
+ .filter((text) => text.length > 0)
408
+ .join('');
409
+ }
410
+
411
+ test('the compact row clips with an ellipsis and the expanded card wraps instead', () => {
412
+ const long = {
413
+ customType: 'worktree-warp',
414
+ content: 'x',
415
+ details: {
416
+ fromName: 'a'.repeat(120),
417
+ name: 'feat-two',
418
+ to: `/clone-worktrees/${'b'.repeat(120)}`,
419
+ branch: 'feat/two',
420
+ },
421
+ };
422
+ for (const style of ['neon', 'float']) {
423
+ const compact = flowedText(withStyle(style, () => renderer()(long, {}, paintTheme()).render(60)));
424
+ assert.match(compact, /…/, `${style}: the compact row is clipped`);
425
+ assert.equal(compact.includes('a'.repeat(120)), false, `${style}: the overflow stays hidden`);
426
+
427
+ const expanded = flowedText(withStyle(style, () => renderer()(long, { expanded: true }, paintTheme()).render(60)));
428
+ assert.equal(expanded.includes('…'), false, `${style}: nothing is cut when expanded`);
429
+ assert.equal(expanded.includes('a'.repeat(120)), true, `${style}: the whole source name survives the wrap`);
430
+ assert.equal(expanded.includes('b'.repeat(120)), true, `${style}: the whole path survives the wrap`);
431
+ }
432
+ });
433
+
434
+ test('the module keeps its character wrapping helper', () => {
435
+ assert.deepEqual(wrapTextToWidth('abcdef', 3, metrics), ['abc', 'def'], 'wrapping is character based and cuts no character');
436
+ assert.deepEqual(wrapTextToWidth('abc def', 3, metrics), ['abc', ' de', 'f'], 'and it keeps every character, spaces included');
437
+ assert.deepEqual(wrapTextToWidth('abc', 10, metrics), ['abc']);
438
+ assert.deepEqual(wrapTextToWidth('any', 0, metrics), ['any'], 'no budget is one row, never a lost line');
439
+ });
440
+
441
+ // ---- one chrome, no second recipe ----
442
+
443
+ test('the card composes through the substrate and keeps no tone table of its own', () => {
444
+ const source = readFileSync(join(__dirname, '..', 'lib', 'worktree-card.js'), 'utf8');
445
+ assert.match(source, /require\('\.\/shell-card\.js'\)/, 'the card draws on lib/shell-card.js');
446
+ for (const name of ['floatRows', 'cardTop', 'cardLine', 'cardBottom']) {
447
+ assert.match(source, new RegExp(`\\b${name}\\b`), `${name} is the substrate's`);
448
+ }
449
+ for (const forbidden of ['FLOAT_BG_ROLE', 'floatBgRoleForTone', 'toolPendingBg', 'toolSuccessBg', 'toolErrorBg', 'FLOAT_CHROME', 'withChrome']) {
450
+ assert.equal(source.includes(forbidden), false, `${forbidden} lives in lib/shell-card.js only`);
451
+ }
452
+ assert.doesNotMatch(source, /\b(?:neon|float)\b\s*[:=]/, 'the card never names a style itself');
453
+ });
454
+
455
+ // ---- the click area: this message card, and only this one ----
456
+ //
457
+ // The message card reaches the same click protocol as a tool card: pi-tui's
458
+ // `Container` forwards a normalized `TuiMouseEvent` to the renderer component
459
+ // with coordinates local to it, and a click only exists for the component that
460
+ // claimed the press.
461
+ //
462
+ // A message renderer gets no render context to keep state in — only
463
+ // `{expanded, outputPad, keyText, keyHint}` — so the choice is kept beside the
464
+ // message it belongs to, and the card re-reads it on every render: the host
465
+ // repaints a claimed click without rebuilding the renderer. `ctrl+o`, the host's
466
+ // own flag in `options.expanded`, still wins over an earlier click.
467
+
468
+ function mountCard(component, width = 60) {
469
+ const rows = component.render(width);
470
+ const screen = { width, height: rows.length, screenX: 0, screenY: 0, shift: false, alt: false, ctrl: false };
471
+ const send = (type, x, y, patch = {}) => component.handleMouse({ type, button: 'left', x, y, ...screen, ...patch });
472
+ return { rows, send };
473
+ }
474
+
475
+ /** A second warp message: a distinct object, so its card keeps its own choice. */
476
+ /**
477
+ * A fresh warp message: the expansion choice is kept per message object, so every
478
+ * case gets its own and nothing leaks from one case into the next.
479
+ */
480
+ function messageFor(name, details = {}) {
481
+ return {
482
+ customType: 'worktree-warp',
483
+ content: `session warped to ${name} (this prose never enters the card)`,
484
+ details: { fromName: 'feat-one', name, to: `/clone-worktrees/${name}`, branch: `feat/${name}`, ...details },
485
+ };
486
+ }
487
+
488
+ test('a left click expands the warp card it hit and leaves a sibling card collapsed', () => {
489
+ const message = messageFor('feat-two');
490
+ const component = renderer()(message, {}, plainTheme);
491
+ const { rows, send } = mountCard(component);
492
+ const sibling = renderer()(messageFor('feat-three'), {}, plainTheme);
493
+ const siblingRows = sibling.render(60);
494
+
495
+ assert.deepEqual(send('press', 3, 0), { handled: true, render: false }, 'the press claims the card so the host retargets the click');
496
+ assert.deepEqual(send('click', 3, 0), { handled: true }, 'the click on the heading is handled');
497
+
498
+ const expanded = component.render(60);
499
+ assert.ok(expanded.length > rows.length, 'the same component, re-rendered, is now expanded');
500
+ assert.equal(contentOf(expanded).includes('path: now in'), true, 'and shows the path an expanded warp adds');
501
+ assert.deepEqual(sibling.render(60), siblingRows, 'the sibling card is untouched');
502
+ assert.equal(contentOf(sibling.render(60)).includes('path: now in'), false, 'and stays collapsed');
503
+ });
504
+
505
+ test('a second click collapses the same warp card again', () => {
506
+ const component = renderer()(messageFor('feat-two'), {}, plainTheme);
507
+ const { rows, send } = mountCard(component);
508
+ assert.deepEqual(send('click', 3, 0), { handled: true });
509
+ assert.ok(component.render(60).length > rows.length, 'the first click expands');
510
+ assert.deepEqual(send('click', 3, 0), { handled: true });
511
+ assert.deepEqual(component.render(60), rows, 'the second click collapses it back');
512
+ });
513
+
514
+ test('ctrl+o still expands the warp card, and wins over an earlier click', () => {
515
+ const message = messageFor('feat-two');
516
+ const compact = renderer()(message, {}, plainTheme).render(60);
517
+
518
+ // A click expands this card...
519
+ const clicked = renderer()(message, {}, plainTheme);
520
+ const card = mountCard(clicked);
521
+ assert.deepEqual(card.send('click', 3, 0), { handled: true });
522
+ assert.ok(clicked.render(60).length > compact.length, 'the click expanded the card');
523
+
524
+ // ...and the host's own flag still decides, in both directions.
525
+ const byKey = renderer()(message, { expanded: true }, plainTheme).render(60);
526
+ assert.ok(byKey.length > compact.length, 'ctrl+o expands a card a click had already expanded');
527
+ assert.deepEqual(renderer()(message, { expanded: false }, plainTheme).render(60), compact, 'a collapse from ctrl+o wins over the earlier click too');
528
+ });
529
+
530
+ test('the hint cells stay clickable and nothing outside the card is claimed', () => {
531
+ const component = renderer()(messageFor('feat-two'), {}, plainTheme);
532
+ const { rows, send } = mountCard(component);
533
+ const start = stripAnsiAll(rows[0]).indexOf('ctrl+o to expand');
534
+ assert.ok(start > 0, 'the heading carries the hint');
535
+
536
+ assert.equal(send('click', 0, rows.length), undefined, 'a row below the card is the host\'s');
537
+ assert.equal(send('click', 0, -1), undefined, 'a row above the card is the host\'s');
538
+ assert.equal(send('click', 60, 0), undefined, 'a column right of the card is the host\'s');
539
+ assert.equal(send('click', 0, 0, { button: 'right' }), undefined, 'the right button is the host\'s');
540
+ assert.equal(send('drag', 0, 0), undefined, 'a drag is not a click');
541
+ assert.equal(send('move', 0, 0), undefined, 'a move is not a click');
542
+
543
+ assert.deepEqual(send('click', start + 1, 0), { handled: true }, 'the hint cells were already clickable and still are');
544
+ assert.ok(component.render(60).length > rows.length, 'and the hint is on the card, so it moved this one card');
545
+ });
546
+
547
+ test('the warp card claims its own cells with no injected seam', () => {
548
+ const component = renderer()(messageFor('feat-two'), {}, plainTheme);
549
+ assert.equal(typeof component.handleMouse, 'function', 'no host toggle is needed: the card moves itself');
550
+ assert.ok(component.render(60).length > 0, 'the card still draws');
551
+ });