pi-odd-worktree 3.15.0 → 3.16.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 (127) hide show
  1. package/README.md +48 -25
  2. package/extensions/odd-worktree-tools.ts +64 -211
  3. package/lib/shell-api/api.ts +523 -0
  4. package/lib/shell-api/arm.ts +83 -0
  5. package/lib/shell-api/bus.ts +1389 -0
  6. package/lib/shell-api/capture.ts +1340 -0
  7. package/lib/shell-api/card-style-line.ts +112 -0
  8. package/lib/shell-api/card-style-rows.ts +390 -0
  9. package/lib/shell-api/card-style-transform.ts +862 -0
  10. package/lib/shell-api/card-style-wiring.ts +477 -0
  11. package/lib/shell-api/card-styles.ts +153 -0
  12. package/lib/shell-api/config.ts +71 -0
  13. package/lib/shell-api/extension.ts +247 -0
  14. package/lib/shell-api/hook.ts +322 -0
  15. package/lib/shell-api/index.ts +88 -0
  16. package/lib/shell-api/message-design-adoption.ts +549 -0
  17. package/lib/shell-api/message-design-rows.ts +313 -0
  18. package/lib/shell-api/message-design-wiring.ts +369 -0
  19. package/lib/shell-api/message-designs.ts +746 -0
  20. package/lib/shell-api/notices.ts +137 -0
  21. package/lib/shell-api/plugin-lines.ts +877 -0
  22. package/lib/shell-api/presence.ts +258 -0
  23. package/lib/shell-api/repaint.ts +351 -0
  24. package/lib/shell-api/row-labels.ts +146 -0
  25. package/lib/shell-api/row-names.ts +273 -0
  26. package/lib/shell-api/row-origin.ts +144 -0
  27. package/lib/shell-api/rows.ts +591 -0
  28. package/lib/shell-api/section-panel.ts +378 -0
  29. package/lib/shell-api/section-pointer.ts +164 -0
  30. package/lib/shell-api/sections-preview.ts +275 -0
  31. package/lib/shell-api/sections-rows.ts +133 -0
  32. package/lib/shell-api/sidebar-render.ts +705 -0
  33. package/lib/shell-api/sidebar.ts +1126 -0
  34. package/lib/shell-api/store.ts +279 -0
  35. package/lib/shell-api/tool-design-adoption.ts +263 -0
  36. package/lib/shell-api/tool-design-rows.ts +307 -0
  37. package/lib/shell-api/tool-design-wiring.ts +481 -0
  38. package/lib/shell-api/tool-designs.ts +1411 -0
  39. package/lib/shell-api/transform-pipeline.ts +351 -0
  40. package/lib/shell-api/types.ts +803 -0
  41. package/lib/shell-api/visibility.ts +154 -0
  42. package/lib/user-prompt-card.js +48 -2
  43. package/package.json +3 -2
  44. package/tools/files-bridge.test.js +81 -66
  45. package/tools/fixtures/load-extension.js +40 -18
  46. package/tools/issues-command.test.js +4 -7
  47. package/tools/issues-section.test.js +71 -81
  48. package/tools/odd-rail-wiring.test.js +16 -10
  49. package/tools/package-surface.test.js +42 -13
  50. package/tools/prs-command.test.js +11 -6
  51. package/tools/shell-api/api.test.ts +484 -0
  52. package/tools/shell-api/apply-on-move.test.ts +529 -0
  53. package/tools/shell-api/arm.test.ts +220 -0
  54. package/tools/shell-api/bridge-ready.test.ts +309 -0
  55. package/tools/shell-api/bus.test.ts +194 -0
  56. package/tools/shell-api/button-rows.test.ts +435 -0
  57. package/tools/shell-api/capture.test.ts +583 -0
  58. package/tools/shell-api/card-style-rows.test.ts +493 -0
  59. package/tools/shell-api/card-style-transform.test.ts +379 -0
  60. package/tools/shell-api/card-style-wiring.test.ts +258 -0
  61. package/tools/shell-api/card-styles-e2e.test.ts +502 -0
  62. package/tools/shell-api/card-styles.test.ts +324 -0
  63. package/tools/shell-api/card-transcript-path.test.ts +178 -0
  64. package/tools/shell-api/compatibility-regression.test.mjs +64 -0
  65. package/tools/shell-api/config.test.ts +93 -0
  66. package/tools/shell-api/docs-coverage.test.ts +82 -0
  67. package/tools/shell-api/e2e.test.ts +562 -0
  68. package/tools/shell-api/example-bash-identity.test.ts +372 -0
  69. package/tools/shell-api/example-bash-matrix.test.ts +856 -0
  70. package/tools/shell-api/example-designs.test.ts +737 -0
  71. package/tools/shell-api/example-neon-transform.test.ts +283 -0
  72. package/tools/shell-api/example-plain-style.test.ts +1225 -0
  73. package/tools/shell-api/extension.test.ts +157 -0
  74. package/tools/shell-api/fake-events.ts +58 -0
  75. package/tools/shell-api/fake-host.ts +211 -0
  76. package/tools/shell-api/fake-sidebar.ts +516 -0
  77. package/tools/shell-api/fake-transcript.ts +486 -0
  78. package/tools/shell-api/frame-telemetry.test.ts +317 -0
  79. package/tools/shell-api/hook.test.ts +196 -0
  80. package/tools/shell-api/manifest.test.ts +47 -0
  81. package/tools/shell-api/message-design-adoption.test.ts +611 -0
  82. package/tools/shell-api/message-design-follows.test.ts +361 -0
  83. package/tools/shell-api/message-design-pi-release.test.mjs +157 -0
  84. package/tools/shell-api/message-design-regressions.test.mjs +162 -0
  85. package/tools/shell-api/message-design-rows.test.ts +266 -0
  86. package/tools/shell-api/message-design-wiring.test.ts +362 -0
  87. package/tools/shell-api/message-designs.test.ts +339 -0
  88. package/tools/shell-api/new-category.test.ts +1101 -0
  89. package/tools/shell-api/notices.test.ts +292 -0
  90. package/tools/shell-api/plugin-integration-lines.test.ts +827 -0
  91. package/tools/shell-api/presence.test.ts +396 -0
  92. package/tools/shell-api/rail-part-hits.test.ts +281 -0
  93. package/tools/shell-api/rail-surfaces.test.ts +402 -0
  94. package/tools/shell-api/rail-width.test.ts +406 -0
  95. package/tools/shell-api/regression.test.ts +434 -0
  96. package/tools/shell-api/repaint-api.test.ts +200 -0
  97. package/tools/shell-api/reserved-categories.test.ts +250 -0
  98. package/tools/shell-api/row-labels.test.ts +261 -0
  99. package/tools/shell-api/row-names.test.ts +407 -0
  100. package/tools/shell-api/row-origin.test.ts +395 -0
  101. package/tools/shell-api/row-placement-regression.test.ts +797 -0
  102. package/tools/shell-api/row-placement.test.ts +410 -0
  103. package/tools/shell-api/section-interaction.test.ts +364 -0
  104. package/tools/shell-api/section-panel.test.ts +430 -0
  105. package/tools/shell-api/section-placement-rail.test.ts +390 -0
  106. package/tools/shell-api/section-placement.test.ts +386 -0
  107. package/tools/shell-api/sections-preview.test.ts +312 -0
  108. package/tools/shell-api/sections-rows.test.ts +293 -0
  109. package/tools/shell-api/sidebar-backend.test.ts +477 -0
  110. package/tools/shell-api/sidebar-e2e.test.ts +317 -0
  111. package/tools/shell-api/sidebar-hover.test.ts +276 -0
  112. package/tools/shell-api/sidebar-independent.test.ts +291 -0
  113. package/tools/shell-api/sidebar-render.test.ts +281 -0
  114. package/tools/shell-api/sidebar-sections.test.ts +247 -0
  115. package/tools/shell-api/store.test.ts +192 -0
  116. package/tools/shell-api/surface-repaint-cost.test.ts +520 -0
  117. package/tools/shell-api/tool-design-card-style.test.ts +308 -0
  118. package/tools/shell-api/tool-design-chain.test.ts +471 -0
  119. package/tools/shell-api/tool-design-cost.test.ts +591 -0
  120. package/tools/shell-api/tool-design-rows.test.ts +627 -0
  121. package/tools/shell-api/tool-design-scopes-audit.test.ts +212 -0
  122. package/tools/shell-api/tool-designs.test.ts +2412 -0
  123. package/tools/shell-api/transform-pipeline.test.ts +376 -0
  124. package/tools/shell-api/transforms-bus.test.ts +233 -0
  125. package/tools/shell-api/verifier-width-regressions.test.ts +147 -0
  126. package/tools/shell-api-rpc.test.js +189 -0
  127. package/tools/user-prompt-card.test.js +80 -10
@@ -0,0 +1,266 @@
1
+ /**
2
+ * S1 — the customize line for message designs, asserted without a host.
3
+ *
4
+ * The rows are built per menu build from the live registry (`./message-design-rows.ts`), so this file
5
+ * drives that builder directly: the line's name, `default` first, one value per design, the preview, the
6
+ * action that persists, and the "no design, no line" rule that keeps a session without design plugins
7
+ * byte-identical.
8
+ */
9
+
10
+ import assert from "node:assert/strict";
11
+ import { mkdtempSync, rmSync } from "node:fs";
12
+ import { tmpdir } from "node:os";
13
+ import { join } from "node:path";
14
+ import test from "node:test";
15
+
16
+ import { resetBusForTests, snapshotProviders } from "../../lib/shell-api/bus.ts";
17
+ import {
18
+ MESSAGE_DESIGN_DEFAULT_LABEL,
19
+ MESSAGE_DESIGN_NATIVE,
20
+ getMessageDesignLineSelection,
21
+ registerMessageDesign,
22
+ resetMessageDesignsForTests,
23
+ setMessageDesignSelection,
24
+ } from "../../lib/shell-api/message-designs.ts";
25
+ import {
26
+ MESSAGE_DESIGN_ROWS_PROVIDER_ID,
27
+ installMessageDesignRows,
28
+ messageDesignLineNames,
29
+ messageDesignRows,
30
+ resetMessageDesignRowStateForTests,
31
+ } from "../../lib/shell-api/message-design-rows.ts";
32
+ import { resetNoticesForTests } from "../../lib/shell-api/notices.ts";
33
+ import { resetPluginLinesForTests } from "../../lib/shell-api/plugin-lines.ts";
34
+ import { resetRepaintForTests } from "../../lib/shell-api/repaint.ts";
35
+ import { resetRowNameRejectionsForTests, rowNameProblem } from "../../lib/shell-api/row-names.ts";
36
+ import { resetRowBuildStateForTests } from "../../lib/shell-api/rows.ts";
37
+ import { resetStoreStateForTests } from "../../lib/shell-api/store.ts";
38
+ import type { CustomizeRow, InlineHelpers, MessageDesignRegistration } from "../../lib/shell-api/types.ts";
39
+
40
+ let configHome: string;
41
+ let previousConfigHome: string | undefined;
42
+
43
+ test.before(() => {
44
+ configHome = mkdtempSync(join(tmpdir(), "gsb-message-rows-"));
45
+ previousConfigHome = process.env.GENTLE_PI_CONFIG_HOME;
46
+ process.env.GENTLE_PI_CONFIG_HOME = configHome;
47
+ });
48
+
49
+ test.after(() => {
50
+ if (previousConfigHome === undefined) delete process.env.GENTLE_PI_CONFIG_HOME;
51
+ else process.env.GENTLE_PI_CONFIG_HOME = previousConfigHome;
52
+ rmSync(configHome, { recursive: true, force: true });
53
+ });
54
+
55
+ test.beforeEach(() => {
56
+ rmSync(join(configHome, "gentle-shell-bridge"), { recursive: true, force: true });
57
+ resetMessageDesignsForTests();
58
+ resetMessageDesignRowStateForTests();
59
+ resetRepaintForTests();
60
+ resetStoreStateForTests();
61
+ resetNoticesForTests();
62
+ resetPluginLinesForTests();
63
+ resetRowBuildStateForTests();
64
+ resetRowNameRejectionsForTests();
65
+ resetBusForTests();
66
+ });
67
+
68
+ const BUDGET_TYPE = "gentle-pi.exploration-budget";
69
+
70
+ const design = (extra: Partial<MessageDesignRegistration> = {}): MessageDesignRegistration => ({
71
+ customType: BUDGET_TYPE,
72
+ id: "loon.budget",
73
+ label: "budget",
74
+ apply: (lines: readonly string[]) => [...lines],
75
+ ...extra,
76
+ });
77
+
78
+ const settingOf = (rows: readonly CustomizeRow[]): string | undefined => rows[0]?.choice?.setting;
79
+
80
+ // ---------------------------------------------------------------------------------------------
81
+ // No design, no line
82
+ // ---------------------------------------------------------------------------------------------
83
+
84
+ test("S1: with no design registered the provider contributes no row at all", () => {
85
+ assert.deepEqual(messageDesignRows(), [], "an empty line would be worse than no line");
86
+ assert.deepEqual(messageDesignLineNames(), []);
87
+ });
88
+
89
+ // ---------------------------------------------------------------------------------------------
90
+ // The line and its values
91
+ // ---------------------------------------------------------------------------------------------
92
+
93
+ test("S1: one design yields `default` first and the design second, on ONE setting", () => {
94
+ registerMessageDesign(design());
95
+
96
+ const rows = messageDesignRows();
97
+ assert.equal(rows.length, 2, "default plus one value");
98
+ assert.equal(rows[0].choice?.value, MESSAGE_DESIGN_DEFAULT_LABEL, "default is the first value");
99
+ assert.equal(rows[1].choice?.value, "budget", "then the design's label, never its id");
100
+ assert.equal(rows[0].choice?.setting, rows[1].choice?.setting, "one line, coalesced by the host");
101
+ assert.equal(rows[0].category, rows[1].category, "and both in the Cards category");
102
+ });
103
+
104
+ test("S1: the line's name is `Message <words>` and passes the row-name rule", () => {
105
+ registerMessageDesign(design());
106
+
107
+ const name = messageDesignLineNames()[0].name;
108
+ assert.match(name, /^Message /u, "the message surface's own word");
109
+ assert.equal(
110
+ rowNameProblem(name),
111
+ undefined,
112
+ `the derived name must satisfy the row-name rule: ${name}`,
113
+ );
114
+ assert.equal(messageDesignRows()[0].choice?.setting, name, "the row carries that name");
115
+ });
116
+
117
+ test("S1: several designs of one line are the values of ONE line, in registration order", () => {
118
+ registerMessageDesign({
119
+ line: "reminders",
120
+ types: [BUDGET_TYPE],
121
+ id: "loon.one",
122
+ label: "one",
123
+ apply: (lines: readonly string[]) => [...lines],
124
+ });
125
+ registerMessageDesign({
126
+ line: "reminders",
127
+ types: ["gentle-pi.verifier-settle-reminder"],
128
+ id: "loon.two",
129
+ label: "two",
130
+ apply: (lines: readonly string[]) => [...lines],
131
+ });
132
+
133
+ const rows = messageDesignRows();
134
+ assert.deepEqual(
135
+ rows.map((row: CustomizeRow) => row.choice?.value),
136
+ [MESSAGE_DESIGN_DEFAULT_LABEL, "one", "two"],
137
+ "default, then the designs in the order they were registered",
138
+ );
139
+ assert.deepEqual(
140
+ [...new Set(rows.map((row: CustomizeRow) => row.choice?.setting))],
141
+ [settingOf(rows)],
142
+ "all of them share the line's one setting",
143
+ );
144
+ });
145
+
146
+ test("S1: two lines are told apart, and neither name breaks the rule", () => {
147
+ registerMessageDesign(design({ customType: BUDGET_TYPE, id: "loon.budget" }));
148
+ registerMessageDesign(design({ customType: "gentle-pi.verifier-settle-reminder", id: "loon.reminder" }));
149
+
150
+ const names = messageDesignLineNames().map((line) => line.name);
151
+ assert.equal(new Set(names).size, 2, "two distinct names");
152
+ for (const name of names) assert.equal(rowNameProblem(name), undefined, name);
153
+ });
154
+
155
+ // ---------------------------------------------------------------------------------------------
156
+ // The bullet, the preview and the action
157
+ // ---------------------------------------------------------------------------------------------
158
+
159
+ test("S2: `current()` reads the live selection, and `default` is the identity `native`", () => {
160
+ registerMessageDesign(design());
161
+ assert.equal(messageDesignRows()[0].choice?.current(), true, "nothing chosen yet");
162
+ assert.equal(messageDesignRows()[1].choice?.current(), false);
163
+
164
+ setMessageDesignSelection(BUDGET_TYPE, "loon.budget");
165
+ const rows = messageDesignRows();
166
+ assert.equal(rows[0].choice?.current(), false, "the bullet left default");
167
+ assert.equal(rows[1].choice?.current(), true, "and sits on the design");
168
+ });
169
+
170
+ test("S2: an action persists FIRST, and a press that changes nothing is a no-op", () => {
171
+ registerMessageDesign(design());
172
+ const [defaultRow, designRow] = messageDesignRows();
173
+ // A row action receives the host's inline helpers; this one never opens a dialog, so an empty
174
+ // object is enough and proves the signatures agree.
175
+ const inline = {} as unknown as InlineHelpers;
176
+
177
+ designRow.action?.(inline);
178
+ assert.equal(
179
+ getMessageDesignLineSelection("type", BUDGET_TYPE),
180
+ "loon.budget",
181
+ "the value is written before anything repaints",
182
+ );
183
+
184
+ // Pressing the same value again must not rewrite the store.
185
+ const stored = getMessageDesignLineSelection("type", BUDGET_TYPE);
186
+ designRow.action?.(inline);
187
+ assert.equal(getMessageDesignLineSelection("type", BUDGET_TYPE), stored);
188
+
189
+ defaultRow.action?.(inline);
190
+ assert.equal(getMessageDesignLineSelection("type", BUDGET_TYPE), MESSAGE_DESIGN_NATIVE);
191
+ });
192
+
193
+ test("S2: a preview shows one row and says what the value paints", () => {
194
+ registerMessageDesign(design({ description: "spends the budget as a bar" }));
195
+
196
+ const preview = messageDesignRows()[1].preview?.();
197
+ assert.equal(preview?.sample, "spends the budget as a bar", "the design's own description");
198
+ assert.match(String(preview?.title), /budget/u);
199
+ assert.equal(preview?.sample.split("\n").length, 1, "one row, never two");
200
+ });
201
+
202
+ test("S2: a throwing or malformed preview loses its own picture and nothing else", () => {
203
+ registerMessageDesign(
204
+ design({
205
+ preview: () => {
206
+ throw new Error("kaboom");
207
+ },
208
+ }),
209
+ );
210
+ assert.doesNotThrow(() => messageDesignRows()[1].preview?.());
211
+ assert.match(String(messageDesignRows()[1].preview?.().sample), /budget/u, "the fallback still says");
212
+
213
+ resetMessageDesignsForTests();
214
+ registerMessageDesign(
215
+ design({
216
+ preview: () => ({ title: 1, sample: 2 }) as unknown as { title: string; sample: string },
217
+ }),
218
+ );
219
+ assert.match(String(messageDesignRows()[1].preview?.().sample), /budget/u);
220
+ });
221
+
222
+ // ---------------------------------------------------------------------------------------------
223
+ // The provider
224
+ // ---------------------------------------------------------------------------------------------
225
+
226
+ test("S4: arming with no design contributes NO provider, so nothing pays per pass", () => {
227
+ // With no design there is no provider at all: Pi credits one fewer registration, the menu runs
228
+ // nothing extra, and the diagnostics record keeps its historical output. The id is reserved, so no plugin
229
+ // can hold it and hand it back — which is why lazy is safe.
230
+ installMessageDesignRows();
231
+ assert.deepEqual(
232
+ snapshotProviders().filter((provider) => provider.id === MESSAGE_DESIGN_ROWS_PROVIDER_ID),
233
+ [],
234
+ "a session that never registered a design gains no provider at all",
235
+ );
236
+ assert.deepEqual(messageDesignRows(), [], "and therefore no row");
237
+ });
238
+
239
+ test("S4: the FIRST design installs the line, with no second arm", () => {
240
+ installMessageDesignRows();
241
+ registerMessageDesign(design());
242
+
243
+ const providers = snapshotProviders().filter(
244
+ (provider) => provider.id === MESSAGE_DESIGN_ROWS_PROVIDER_ID,
245
+ );
246
+ assert.equal(providers.length, 1, "a design appeared, so the line is there to show it");
247
+ assert.equal(messageDesignRows().length, 2, "default plus the design");
248
+ });
249
+
250
+ test("S1: installing twice contributes the line once, and the disposer withdraws it", () => {
251
+ registerMessageDesign(design());
252
+ const dispose = installMessageDesignRows();
253
+ installMessageDesignRows();
254
+
255
+ const providers = snapshotProviders().filter(
256
+ (provider) => provider.id === MESSAGE_DESIGN_ROWS_PROVIDER_ID,
257
+ );
258
+ assert.equal(providers.length, 1, "one provider, however many times the bridge armed");
259
+
260
+ dispose();
261
+ assert.equal(
262
+ snapshotProviders().filter((provider) => provider.id === MESSAGE_DESIGN_ROWS_PROVIDER_ID).length,
263
+ 0,
264
+ "and the disposer withdraws it",
265
+ );
266
+ });
@@ -0,0 +1,362 @@
1
+ /**
2
+ * T2 — the renderer policy for a claimed custom message, asserted without a host (S3, S4).
3
+ *
4
+ * `./message-design-wiring.ts` is the whole rule: the host's renderer runs first, and only its ROWS
5
+ * ever reach a design. This file drives it with a fake host renderer, so the three cases are pinned
6
+ * by themselves: a type with a host renderer (rows in, rows out), a type with none (the design owns
7
+ * the box), and a type nobody claimed (the host's answer, byte for byte, including `undefined`).
8
+ */
9
+
10
+ import assert from "node:assert/strict";
11
+ import { mkdtempSync, rmSync } from "node:fs";
12
+ import { tmpdir } from "node:os";
13
+ import { join } from "node:path";
14
+ import test from "node:test";
15
+
16
+ import { resolveCardStyleSelection } from "../../lib/shell-api/card-styles.ts";
17
+ import {
18
+ getMessageDesignFailures,
19
+ registerMessageDesign,
20
+ resetMessageDesignsForTests,
21
+ setMessageDesignSelection,
22
+ } from "../../lib/shell-api/message-designs.ts";
23
+ import { messageDesignRenderer } from "../../lib/shell-api/message-design-wiring.ts";
24
+ import { resetStoreStateForTests } from "../../lib/shell-api/store.ts";
25
+ import type { MessageDesignContext, MessageDesignRegistration } from "../../lib/shell-api/types.ts";
26
+
27
+ let configHome: string;
28
+ let previousConfigHome: string | undefined;
29
+
30
+ test.before(() => {
31
+ configHome = mkdtempSync(join(tmpdir(), "gsb-message-wiring-"));
32
+ previousConfigHome = process.env.GENTLE_PI_CONFIG_HOME;
33
+ process.env.GENTLE_PI_CONFIG_HOME = configHome;
34
+ });
35
+
36
+ test.after(() => {
37
+ if (previousConfigHome === undefined) delete process.env.GENTLE_PI_CONFIG_HOME;
38
+ else process.env.GENTLE_PI_CONFIG_HOME = previousConfigHome;
39
+ rmSync(configHome, { recursive: true, force: true });
40
+ });
41
+
42
+ test.beforeEach(() => {
43
+ rmSync(join(configHome, "gentle-shell-bridge"), { recursive: true, force: true });
44
+ resetMessageDesignsForTests();
45
+ resetStoreStateForTests();
46
+ });
47
+
48
+ const TYPE = "gentle-pi.exploration-budget";
49
+ const REMINDER = "gentle-pi.verifier-settle-reminder";
50
+
51
+ /** A host component: it answers the rows it was built with, tagged with the width it was asked for. */
52
+ const hostComponent = (rows: readonly string[]) => ({
53
+ render: (width: number): readonly string[] => rows.map((row) => `${row}@${width}`),
54
+ });
55
+
56
+ const hostRendererFor = (rows: readonly string[]) => {
57
+ const component = hostComponent(rows);
58
+ return {
59
+ component,
60
+ renderer: (message: unknown, options: unknown, theme?: unknown): unknown => {
61
+ void message;
62
+ void options;
63
+ void theme;
64
+ return component;
65
+ },
66
+ };
67
+ };
68
+
69
+ const design = (
70
+ extra: Partial<MessageDesignRegistration> = {},
71
+ ): MessageDesignRegistration => ({
72
+ customType: TYPE,
73
+ id: "loon.budget",
74
+ label: "budget",
75
+ apply: (lines: readonly string[]) => [...lines],
76
+ ...extra,
77
+ });
78
+
79
+ const options = { expanded: false, outputPad: 1 };
80
+
81
+ // ---------------------------------------------------------------------------------------------
82
+ // S4 — nobody claimed the type
83
+ // ---------------------------------------------------------------------------------------------
84
+
85
+ test("S4: a type with no design hands the host's own component back by identity", () => {
86
+ const host = hostRendererFor(["host frame"]);
87
+ const renderer = messageDesignRenderer(TYPE, host.renderer);
88
+
89
+ const built = renderer({ customType: TYPE }, options, {});
90
+
91
+ assert.equal(built, host.component, "the host's component, not a copy and not a wrapper");
92
+ assert.deepEqual(
93
+ (built as { render(width: number): readonly string[] }).render(40),
94
+ ["host frame@40"],
95
+ "and it draws exactly what it drew before the bridge existed",
96
+ );
97
+ });
98
+
99
+ test("S4: a type with no design AND no host renderer answers `undefined` — Pi's own box", () => {
100
+ const renderer = messageDesignRenderer(TYPE, undefined);
101
+ assert.equal(renderer({ customType: TYPE }, options, {}), undefined, "the host's default box");
102
+ });
103
+
104
+ test("S4: resolving no design costs ONE store read per build", () => {
105
+ const renderer = messageDesignRenderer(TYPE, undefined);
106
+ const before = getMessageDesignFailures().length;
107
+ renderer({ customType: TYPE }, options, {});
108
+ assert.equal(getMessageDesignFailures().length, before, "no failure is invented for a quiet type");
109
+ });
110
+
111
+ // ---------------------------------------------------------------------------------------------
112
+ // S3 — a type WITH a host renderer: rows in, rows out, the frame stays the host's
113
+ // ---------------------------------------------------------------------------------------------
114
+
115
+ test("S3: the design receives the host's rows AT the host's width, and returns the rows to draw", () => {
116
+ registerMessageDesign(
117
+ design({
118
+ apply: (lines: readonly string[], ctx: MessageDesignContext) => [
119
+ ...lines,
120
+ `design ${ctx.customType} ${ctx.width}`,
121
+ ],
122
+ }),
123
+ );
124
+ setMessageDesignSelection(TYPE, "loon.budget");
125
+
126
+ const host = hostRendererFor(["host frame"]);
127
+ const renderer = messageDesignRenderer(TYPE, host.renderer);
128
+ const component = renderer({ customType: TYPE }, options, {}) as {
129
+ render(width: number): readonly string[];
130
+ };
131
+
132
+ assert.deepEqual(component.render(40), ["host frame@40", "design gentle-pi.exploration-budget 40"]);
133
+ });
134
+
135
+ test("S3: the design gets a COPY of the host's rows, so a mutation cannot corrupt the frame", () => {
136
+ registerMessageDesign(
137
+ design({
138
+ apply: (lines: string[]) => {
139
+ lines.push("mutated");
140
+ return lines;
141
+ },
142
+ }),
143
+ );
144
+ setMessageDesignSelection(TYPE, "loon.budget");
145
+
146
+ const host = hostRendererFor(["host frame"]);
147
+ const component = messageDesignRenderer(TYPE, host.renderer)(
148
+ { customType: TYPE },
149
+ options,
150
+ {},
151
+ ) as { render(width: number): readonly string[] };
152
+
153
+ assert.deepEqual(component.render(10), ["host frame@10", "mutated"]);
154
+ assert.deepEqual(
155
+ hostComponent(["host frame"]).render(10),
156
+ ["host frame@10"],
157
+ "the host's own component still answers its own rows",
158
+ );
159
+ });
160
+
161
+ test("S3: a design that throws leaves the host's rows on screen and is reported", () => {
162
+ registerMessageDesign(
163
+ design({
164
+ apply: () => {
165
+ throw new Error("kaboom");
166
+ },
167
+ }),
168
+ );
169
+ setMessageDesignSelection(TYPE, "loon.budget");
170
+
171
+ const host = hostRendererFor(["host frame"]);
172
+ const component = messageDesignRenderer(TYPE, host.renderer)(
173
+ { customType: TYPE },
174
+ options,
175
+ {},
176
+ ) as { render(width: number): readonly string[] };
177
+
178
+ assert.deepEqual(component.render(20), ["host frame@20"], "the host's frame, untouched");
179
+ assert.deepEqual(
180
+ getMessageDesignFailures().map((failure) => failure.message),
181
+ ["kaboom"],
182
+ "and one failure recorded",
183
+ );
184
+ });
185
+
186
+ test("S3: a design that returns something other than rows loses its own output only", () => {
187
+ registerMessageDesign(
188
+ design({ apply: (() => "not rows") as unknown as MessageDesignRegistration["apply"] }),
189
+ );
190
+ setMessageDesignSelection(TYPE, "loon.budget");
191
+
192
+ const host = hostRendererFor(["host frame"]);
193
+ const component = messageDesignRenderer(TYPE, host.renderer)(
194
+ { customType: TYPE },
195
+ options,
196
+ {},
197
+ ) as { render(width: number): readonly string[] };
198
+
199
+ assert.deepEqual(component.render(20), ["host frame@20"]);
200
+ assert.equal(getMessageDesignFailures().length, 1, "reported, not swallowed");
201
+ });
202
+
203
+ // ---------------------------------------------------------------------------------------------
204
+ // The live selection and the context
205
+ // ---------------------------------------------------------------------------------------------
206
+
207
+ test("S1: the selection is read on EVERY pass, so a switch reaches a component already mounted", () => {
208
+ registerMessageDesign(design());
209
+ const host = hostRendererFor(["host frame"]);
210
+ setMessageDesignSelection(TYPE, "loon.budget");
211
+
212
+ const component = messageDesignRenderer(TYPE, host.renderer)(
213
+ { customType: TYPE },
214
+ options,
215
+ {},
216
+ ) as { render(width: number): readonly string[] };
217
+ assert.deepEqual(component.render(30), ["host frame@30"]);
218
+
219
+ // The user picks `default` AFTER the component was built: the next pass is the host's own rows.
220
+ setMessageDesignSelection(TYPE, "native");
221
+ assert.deepEqual(component.render(30), ["host frame@30"]);
222
+
223
+ // And a different design takes over on the very next pass.
224
+ registerMessageDesign(design({ id: "loon.other", label: "other", apply: () => ["other"] }));
225
+ setMessageDesignSelection(TYPE, "loon.other");
226
+ assert.deepEqual(component.render(30), ["other"]);
227
+ });
228
+
229
+ test("S1: the context carries the width, the type, Pi's options and the message by reference", () => {
230
+ let seen: MessageDesignContext | undefined;
231
+ registerMessageDesign(
232
+ design({
233
+ apply: (lines: readonly string[], ctx: MessageDesignContext) => {
234
+ seen = ctx;
235
+ return [...lines];
236
+ },
237
+ }),
238
+ );
239
+ setMessageDesignSelection(TYPE, "loon.budget");
240
+
241
+ const message = { customType: TYPE, content: "hello" };
242
+ const theme = { name: "theme" };
243
+ const component = messageDesignRenderer(TYPE, hostRendererFor(["frame"]).renderer)(
244
+ message,
245
+ { expanded: true, outputPad: 2 },
246
+ theme,
247
+ ) as { render(width: number): readonly string[] };
248
+ component.render(72);
249
+
250
+ assert.equal(seen?.width, 72);
251
+ assert.equal(seen?.customType, TYPE);
252
+ assert.equal(seen?.theme, theme, "the host's object, forwarded");
253
+ assert.equal(seen?.render.message, message, "the message by reference, never a clone");
254
+ assert.equal(seen?.render.expanded, true);
255
+ assert.equal(seen?.render.outputPad, 2);
256
+ });
257
+
258
+ test("S1: `cardStyle` is resolved on access, at most once per pass", () => {
259
+ setMessageDesignSelection(TYPE, "loon.budget");
260
+
261
+ let first: unknown;
262
+ let second: unknown;
263
+ registerMessageDesign(
264
+ design({
265
+ apply: (lines: readonly string[], ctx: MessageDesignContext) => {
266
+ first = ctx.cardStyle;
267
+ second = ctx.cardStyle;
268
+ return [...lines];
269
+ },
270
+ }),
271
+ );
272
+
273
+ const component = messageDesignRenderer(TYPE, hostRendererFor(["frame"]).renderer)(
274
+ { customType: TYPE },
275
+ options,
276
+ {},
277
+ ) as { render(width: number): readonly string[] };
278
+ component.render(10);
279
+
280
+ assert.equal(first, second, "one answer per pass");
281
+ assert.deepEqual(first, resolveCardStyleSelection(), "the same selection a tool design reads");
282
+ });
283
+
284
+ // ---------------------------------------------------------------------------------------------
285
+ // The second motivating type
286
+ // ---------------------------------------------------------------------------------------------
287
+
288
+ test("S2: a claimed type with NO host renderer is drawn by the design, not by default", () => {
289
+ registerMessageDesign(
290
+ design({
291
+ customType: REMINDER,
292
+ id: "loon.reminder",
293
+ label: "reminder",
294
+ apply: () => ["╭─ reminder ─╮", "│ settle │", "╰───────────╯"],
295
+ }),
296
+ );
297
+ setMessageDesignSelection(REMINDER, "loon.reminder");
298
+
299
+ const component = messageDesignRenderer(REMINDER, undefined)(
300
+ { customType: REMINDER },
301
+ options,
302
+ {},
303
+ ) as { render(width: number): readonly string[] };
304
+
305
+ assert.deepEqual(component.render(40), ["╭─ reminder ─╮", "│ settle │", "╰───────────╯"]);
306
+ });
307
+
308
+ test("S2: a host-less type hands the design the message's own body rows, plainly", () => {
309
+ const seen: Array<readonly string[]> = [];
310
+ registerMessageDesign(
311
+ design({
312
+ customType: REMINDER,
313
+ id: "loon.reminder",
314
+ label: "reminder",
315
+ apply: (lines: readonly string[]) => {
316
+ seen.push(lines);
317
+ return ["own box"];
318
+ },
319
+ }),
320
+ );
321
+ setMessageDesignSelection(REMINDER, "loon.reminder");
322
+
323
+ const component = messageDesignRenderer(REMINDER, undefined)(
324
+ { customType: REMINDER, content: "settle\nthe reminder" },
325
+ options,
326
+ {},
327
+ ) as { render(width: number): readonly string[] };
328
+ component.render(40);
329
+
330
+ assert.deepEqual(
331
+ seen,
332
+ [[`[${REMINDER}]`, "", "settle", "the reminder"]],
333
+ "the body and its label, plainly: the design owns the frame around them",
334
+ );
335
+ });
336
+
337
+ test("S2: a failing own-the-box design leaves the message body on screen", () => {
338
+ // The whole point of these messages is that the agent READS them: a design that throws must not
339
+ // take the body with it. The rows it was handed are the fallback.
340
+ registerMessageDesign(
341
+ design({
342
+ customType: REMINDER,
343
+ id: "loon.reminder",
344
+ label: "reminder",
345
+ apply: () => {
346
+ throw new Error("boom");
347
+ },
348
+ }),
349
+ );
350
+ setMessageDesignSelection(REMINDER, "loon.reminder");
351
+
352
+ const component = messageDesignRenderer(REMINDER, undefined)(
353
+ { customType: REMINDER, content: "the body the agent must read" },
354
+ options,
355
+ {},
356
+ ) as { render(width: number): readonly string[] };
357
+
358
+ const rows = component.render(40);
359
+ assert.ok(rows.some((row) => row.includes(REMINDER)), "the type is still on screen");
360
+ assert.ok(rows.some((row) => row.includes("the body the agent must read")), "and the body");
361
+ assert.deepEqual(getMessageDesignFailures().map((failure) => failure.message), ["boom"]);
362
+ });