pi-odd-worktree 3.15.1 → 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 (126) 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/package.json +3 -2
  43. package/tools/files-bridge.test.js +81 -66
  44. package/tools/fixtures/load-extension.js +40 -18
  45. package/tools/issues-command.test.js +4 -7
  46. package/tools/issues-section.test.js +71 -81
  47. package/tools/odd-rail-wiring.test.js +16 -10
  48. package/tools/package-surface.test.js +42 -13
  49. package/tools/prs-command.test.js +11 -6
  50. package/tools/shell-api/api.test.ts +484 -0
  51. package/tools/shell-api/apply-on-move.test.ts +529 -0
  52. package/tools/shell-api/arm.test.ts +220 -0
  53. package/tools/shell-api/bridge-ready.test.ts +309 -0
  54. package/tools/shell-api/bus.test.ts +194 -0
  55. package/tools/shell-api/button-rows.test.ts +435 -0
  56. package/tools/shell-api/capture.test.ts +583 -0
  57. package/tools/shell-api/card-style-rows.test.ts +493 -0
  58. package/tools/shell-api/card-style-transform.test.ts +379 -0
  59. package/tools/shell-api/card-style-wiring.test.ts +258 -0
  60. package/tools/shell-api/card-styles-e2e.test.ts +502 -0
  61. package/tools/shell-api/card-styles.test.ts +324 -0
  62. package/tools/shell-api/card-transcript-path.test.ts +178 -0
  63. package/tools/shell-api/compatibility-regression.test.mjs +64 -0
  64. package/tools/shell-api/config.test.ts +93 -0
  65. package/tools/shell-api/docs-coverage.test.ts +82 -0
  66. package/tools/shell-api/e2e.test.ts +562 -0
  67. package/tools/shell-api/example-bash-identity.test.ts +372 -0
  68. package/tools/shell-api/example-bash-matrix.test.ts +856 -0
  69. package/tools/shell-api/example-designs.test.ts +737 -0
  70. package/tools/shell-api/example-neon-transform.test.ts +283 -0
  71. package/tools/shell-api/example-plain-style.test.ts +1225 -0
  72. package/tools/shell-api/extension.test.ts +157 -0
  73. package/tools/shell-api/fake-events.ts +58 -0
  74. package/tools/shell-api/fake-host.ts +211 -0
  75. package/tools/shell-api/fake-sidebar.ts +516 -0
  76. package/tools/shell-api/fake-transcript.ts +486 -0
  77. package/tools/shell-api/frame-telemetry.test.ts +317 -0
  78. package/tools/shell-api/hook.test.ts +196 -0
  79. package/tools/shell-api/manifest.test.ts +47 -0
  80. package/tools/shell-api/message-design-adoption.test.ts +611 -0
  81. package/tools/shell-api/message-design-follows.test.ts +361 -0
  82. package/tools/shell-api/message-design-pi-release.test.mjs +157 -0
  83. package/tools/shell-api/message-design-regressions.test.mjs +162 -0
  84. package/tools/shell-api/message-design-rows.test.ts +266 -0
  85. package/tools/shell-api/message-design-wiring.test.ts +362 -0
  86. package/tools/shell-api/message-designs.test.ts +339 -0
  87. package/tools/shell-api/new-category.test.ts +1101 -0
  88. package/tools/shell-api/notices.test.ts +292 -0
  89. package/tools/shell-api/plugin-integration-lines.test.ts +827 -0
  90. package/tools/shell-api/presence.test.ts +396 -0
  91. package/tools/shell-api/rail-part-hits.test.ts +281 -0
  92. package/tools/shell-api/rail-surfaces.test.ts +402 -0
  93. package/tools/shell-api/rail-width.test.ts +406 -0
  94. package/tools/shell-api/regression.test.ts +434 -0
  95. package/tools/shell-api/repaint-api.test.ts +200 -0
  96. package/tools/shell-api/reserved-categories.test.ts +250 -0
  97. package/tools/shell-api/row-labels.test.ts +261 -0
  98. package/tools/shell-api/row-names.test.ts +407 -0
  99. package/tools/shell-api/row-origin.test.ts +395 -0
  100. package/tools/shell-api/row-placement-regression.test.ts +797 -0
  101. package/tools/shell-api/row-placement.test.ts +410 -0
  102. package/tools/shell-api/section-interaction.test.ts +364 -0
  103. package/tools/shell-api/section-panel.test.ts +430 -0
  104. package/tools/shell-api/section-placement-rail.test.ts +390 -0
  105. package/tools/shell-api/section-placement.test.ts +386 -0
  106. package/tools/shell-api/sections-preview.test.ts +312 -0
  107. package/tools/shell-api/sections-rows.test.ts +293 -0
  108. package/tools/shell-api/sidebar-backend.test.ts +477 -0
  109. package/tools/shell-api/sidebar-e2e.test.ts +317 -0
  110. package/tools/shell-api/sidebar-hover.test.ts +276 -0
  111. package/tools/shell-api/sidebar-independent.test.ts +291 -0
  112. package/tools/shell-api/sidebar-render.test.ts +281 -0
  113. package/tools/shell-api/sidebar-sections.test.ts +247 -0
  114. package/tools/shell-api/store.test.ts +192 -0
  115. package/tools/shell-api/surface-repaint-cost.test.ts +520 -0
  116. package/tools/shell-api/tool-design-card-style.test.ts +308 -0
  117. package/tools/shell-api/tool-design-chain.test.ts +471 -0
  118. package/tools/shell-api/tool-design-cost.test.ts +591 -0
  119. package/tools/shell-api/tool-design-rows.test.ts +627 -0
  120. package/tools/shell-api/tool-design-scopes-audit.test.ts +212 -0
  121. package/tools/shell-api/tool-designs.test.ts +2412 -0
  122. package/tools/shell-api/transform-pipeline.test.ts +376 -0
  123. package/tools/shell-api/transforms-bus.test.ts +233 -0
  124. package/tools/shell-api/verifier-width-regressions.test.ts +147 -0
  125. package/tools/shell-api-rpc.test.js +189 -0
  126. package/tools/user-prompt-card.test.js +8 -9
@@ -0,0 +1,386 @@
1
+ /**
2
+ * Section placement (S1, S2, S3, S5, S6): where a section's card goes in the rail, and what the
3
+ * order inside one slot is made of.
4
+ *
5
+ * The placement is REGISTRATION data, like `order`: nothing here is persisted, and nothing here
6
+ * depends on the order the plugins happened to load in. The renderer (`./sidebar-render.ts`) and the
7
+ * rail wrapper (`./sidebar.ts`) only consume the ordered list this resolver returns; the fallbacks
8
+ * are decided once, here, so the diagnostics record and the frame cannot disagree about where a card went.
9
+ */
10
+
11
+ import assert from "node:assert/strict";
12
+ import test from "node:test";
13
+
14
+ import {
15
+ RAIL_PARTS,
16
+ getSectionPlacementRejections,
17
+ registerSection,
18
+ resetBusForTests,
19
+ resetSectionsForTests,
20
+ resolveSectionPlacements,
21
+ snapshotSections,
22
+ } from "../../lib/shell-api/bus.ts";
23
+ import type { RailPart, SidebarSection } from "../../lib/shell-api/types.ts";
24
+
25
+ const section = (id: string, extra: Partial<SidebarSection> = {}): SidebarSection => ({
26
+ id,
27
+ title: id.toUpperCase(),
28
+ lines: () => [`body ${id}`],
29
+ ...extra,
30
+ });
31
+
32
+ const register = (...sections: SidebarSection[]): void => {
33
+ for (const entry of sections) registerSection(entry);
34
+ };
35
+
36
+ /** `id@side part` for every section, in draw order: the whole answer in one string per section. */
37
+ const places = (
38
+ sections: readonly SidebarSection[] = snapshotSections(),
39
+ painted: readonly RailPart[] = RAIL_PARTS,
40
+ ): string[] =>
41
+ resolveSectionPlacements(sections, painted).map(
42
+ (entry) => `${entry.section.id}@${entry.side} ${entry.part}`,
43
+ );
44
+
45
+ const resolutionOf = (id: string, painted: readonly RailPart[] = RAIL_PARTS) =>
46
+ resolveSectionPlacements(snapshotSections(), painted).find((entry) => entry.section.id === id);
47
+
48
+ test.beforeEach(() => {
49
+ resetBusForTests();
50
+ resetSectionsForTests();
51
+ });
52
+
53
+ /**
54
+ * S2. The registration index is gone from the section tiebreak: two sections that differ only in
55
+ * load order must draw in the same order, or a user's rail changes between sessions for no reason.
56
+ */
57
+ test("equal orders and missing orders are broken by id, never by load order", () => {
58
+ const first = [
59
+ section("b", { order: 5 }),
60
+ section("a", { order: 5 }),
61
+ section("z"),
62
+ section("m"),
63
+ ];
64
+ register(...first);
65
+ const fromOneOrder = snapshotSections().map((entry) => entry.id);
66
+
67
+ resetSectionsForTests();
68
+ register(...first.slice().reverse());
69
+ const fromAnotherOrder = snapshotSections().map((entry) => entry.id);
70
+
71
+ assert.deepEqual(fromOneOrder, ["a", "b", "m", "z"]);
72
+ assert.deepEqual(fromAnotherOrder, fromOneOrder, "the same registrations in any load order");
73
+ });
74
+
75
+ test("finite orders come first ascending, then the rest by id", () => {
76
+ register(
77
+ section("no-order-b"),
78
+ section("second", { order: 2 }),
79
+ section("no-order-a"),
80
+ section("first", { order: -1 }),
81
+ section("zero", { order: 0 }),
82
+ section("not-sortable", { order: Number.NaN }),
83
+ );
84
+
85
+ assert.deepEqual(snapshotSections().map((entry) => entry.id), [
86
+ "first",
87
+ "zero",
88
+ "second",
89
+ "no-order-a",
90
+ "no-order-b",
91
+ "not-sortable",
92
+ ]);
93
+ });
94
+
95
+ test("the id tiebreak is UTF-16 code units, not a locale collation", () => {
96
+ // In a locale-aware comparison `ä` sorts near `a`; by code unit it sorts after `z`.
97
+ register(section("ä"), section("a"), section("Z"));
98
+
99
+ assert.deepEqual(snapshotSections().map((entry) => entry.id), ["Z", "a", "ä"]);
100
+ });
101
+
102
+ /** S1. Absent placement is the historical behaviour: after every native section. */
103
+ test("a section without a placement lands after every native part", () => {
104
+ register(section("plain"));
105
+
106
+ assert.deepEqual(places(), ["plain@after footer"]);
107
+ const resolved = resolutionOf("plain");
108
+ assert.equal(resolved?.requested, "end");
109
+ assert.equal(resolved?.reason, undefined, "the default is honoured, so there is nothing to report");
110
+ });
111
+
112
+ test("an explicit end lands after every native part", () => {
113
+ register(section("last", { placement: "end" }));
114
+
115
+ assert.deepEqual(places(), ["last@after footer"]);
116
+ });
117
+
118
+ test("end is the footer part itself, whatever else the host paints", () => {
119
+ // `"end"` is `{ after: "footer" }` (S4): a rail missing its middle parts still ends at the footer,
120
+ // and a footer that paints no frame draws none of our rows — the rule this surface always had.
121
+ register(section("plain"), section("explicit", { placement: "end" }));
122
+
123
+ assert.deepEqual(places(snapshotSections(), ["todo", "footer"]), [
124
+ "explicit@after footer",
125
+ "plain@after footer",
126
+ ]);
127
+ assert.deepEqual(places(snapshotSections(), []), [
128
+ "explicit@after footer",
129
+ "plain@after footer",
130
+ ]);
131
+ });
132
+
133
+ /** S5. `"start"` goes before the first painted native part. */
134
+ test("start lands before the first painted native part", () => {
135
+ register(section("top", { placement: "start" }));
136
+
137
+ assert.deepEqual(places(), ["top@before todo"]);
138
+ assert.equal(resolutionOf("top")?.reason, undefined);
139
+ });
140
+
141
+ test("start follows the first painted part when an earlier one is not painted", () => {
142
+ register(section("top", { placement: "start" }));
143
+
144
+ assert.deepEqual(places(snapshotSections(), ["jobs", "agents", "footer"]), ["top@before jobs"]);
145
+ });
146
+
147
+ /** S4/S5. `after` accepts a native rail part, resolved by render. */
148
+ test("after a native part lands after that part's own rows", () => {
149
+ register(section("news", { placement: { after: "agents" } }));
150
+
151
+ assert.deepEqual(places(), ["news@after agents"]);
152
+ });
153
+
154
+ test("after a native part that is not painted falls to the next painted native slot", () => {
155
+ register(section("news", { placement: { after: "todo" } }));
156
+
157
+ assert.deepEqual(places(snapshotSections(), ["jobs", "agents", "footer"]), ["news@after jobs"]);
158
+ const resolved = resolutionOf("news", ["jobs", "agents", "footer"]);
159
+ assert.equal(
160
+ resolved?.reason,
161
+ 'the native rail part "todo" is not painted; it landed after jobs',
162
+ "the fallback explains itself",
163
+ );
164
+ });
165
+
166
+ test("after a native part with nothing painted after it lands at the end", () => {
167
+ register(section("news", { placement: { after: "agents" } }));
168
+
169
+ assert.deepEqual(places(snapshotSections(), ["todo", "jobs"]), ["news@after footer"]);
170
+ assert.match(resolutionOf("news", ["todo", "jobs"])?.reason ?? "", /is not painted/);
171
+ });
172
+
173
+ /** S3. Several sections after the same anchor are one block, ordered by the S2 rule. */
174
+ test("several sections after one anchor sit as a block right after it, ordered by id", () => {
175
+ register(
176
+ section("anchor", { placement: "start" }),
177
+ section("second", { placement: { after: "anchor" } }),
178
+ section("first", { placement: { after: "anchor" } }),
179
+ section("tail"),
180
+ );
181
+
182
+ assert.deepEqual(places(), [
183
+ "anchor@before todo",
184
+ "first@before todo",
185
+ "second@before todo",
186
+ "tail@after footer",
187
+ ]);
188
+ });
189
+
190
+ test("a block after a section keeps a finite order ahead of the plain ids", () => {
191
+ register(
192
+ section("anchor"),
193
+ section("plain-b", { placement: { after: "anchor" } }),
194
+ section("plain-a", { placement: { after: "anchor" } }),
195
+ section("numbered", { placement: { after: "anchor" }, order: 3 }),
196
+ );
197
+
198
+ assert.deepEqual(places(), [
199
+ "anchor@after footer",
200
+ "numbered@after footer",
201
+ "plain-a@after footer",
202
+ "plain-b@after footer",
203
+ ]);
204
+ });
205
+
206
+ test("an after-anchor block is resolved transitively", () => {
207
+ register(
208
+ section("a", { placement: "start" }),
209
+ section("b", { placement: { after: "a" } }),
210
+ section("c", { placement: { after: "b" } }),
211
+ );
212
+
213
+ assert.deepEqual(places(), ["a@before todo", "b@before todo", "c@before todo"]);
214
+ assert.equal(resolutionOf("c")?.anchor, '"b"');
215
+ });
216
+
217
+ /** S5. A hidden or collapsed anchor keeps its place: hiding X must not move its followers. */
218
+ test("a follower keeps the slot of an anchor that draws nothing", () => {
219
+ register(
220
+ section("hidden", { placement: "start", defaultVisible: false }),
221
+ section("follower", { placement: { after: "hidden" } }),
222
+ section("plain"),
223
+ );
224
+
225
+ assert.deepEqual(places(), [
226
+ "hidden@before todo",
227
+ "follower@before todo",
228
+ "plain@after footer",
229
+ ]);
230
+ });
231
+
232
+ /** S5. A cycle, or a section after itself, falls back to the end. */
233
+ test("two sections that would sit after each other both fall back to the end", () => {
234
+ register(
235
+ section("a", { placement: { after: "b" } }),
236
+ section("b", { placement: { after: "a" } }),
237
+ );
238
+
239
+ assert.deepEqual(places(), ["a@after footer", "b@after footer"]);
240
+ assert.match(resolutionOf("a")?.reason ?? "", /cycle/);
241
+ assert.match(resolutionOf("b")?.reason ?? "", /cycle/);
242
+ });
243
+
244
+ test("a section that asks to sit after itself falls back to the end", () => {
245
+ register(section("loop", { placement: { after: "loop" } }));
246
+
247
+ assert.deepEqual(places(), ["loop@after footer"]);
248
+ assert.match(resolutionOf("loop")?.reason ?? "", /cycle/);
249
+ });
250
+
251
+ test("a section hanging off a broken cycle resolves against the fixed placement", () => {
252
+ register(
253
+ section("a", { placement: { after: "b" } }),
254
+ section("b", { placement: { after: "a" } }),
255
+ section("c", { placement: { after: "a" } }),
256
+ );
257
+
258
+ assert.deepEqual(places(), ["a@after footer", "c@after footer", "b@after footer"]);
259
+ assert.equal(resolutionOf("c")?.reason, undefined, "its own request is honoured");
260
+ });
261
+
262
+ /** S5. An anchor nobody registered falls back to the end, and says so. */
263
+ test("after an id that is neither a section nor a native part falls back to the end", () => {
264
+ register(section("lost", { placement: { after: "nobody" } }));
265
+
266
+ assert.deepEqual(places(), ["lost@after footer"]);
267
+ assert.match(resolutionOf("lost")?.reason ?? "", /neither a registered section nor a native/);
268
+ });
269
+
270
+ /**
271
+ * The id collision the constraints ask about: a section id is free to be `todo`, so one string can
272
+ * name a section AND a native part. The native part wins; the report says so.
273
+ */
274
+ test("a native part wins over a section that carries its id", () => {
275
+ register(
276
+ section("todo", { placement: "start" }),
277
+ section("follower", { placement: { after: "todo" } }),
278
+ );
279
+
280
+ assert.deepEqual(places(), ["todo@before todo", "follower@after todo"]);
281
+ assert.match(resolutionOf("follower")?.reason ?? "", /native rail part "todo".*wins/);
282
+ });
283
+
284
+ /** S5. A malformed placement is treated as `end` by the resolver, whatever got stored. */
285
+ test("a malformed placement resolves to the end", () => {
286
+ // SAFETY: a plugin can pass anything at runtime; the registration reports it and the resolver
287
+ // must still place the section instead of dropping it.
288
+ const malformed = { after: "" } as unknown as SidebarSection["placement"];
289
+ register(section("broken", { placement: malformed }));
290
+
291
+ assert.deepEqual(places(), ["broken@after footer"]);
292
+ assert.match(resolutionOf("broken")?.reason ?? "", /malformed/);
293
+ });
294
+
295
+ test("a placement shape the type does not allow still registers and is reported", () => {
296
+ const cases: unknown[] = [
297
+ 7,
298
+ null,
299
+ [],
300
+ {},
301
+ { after: "" },
302
+ { after: 5 },
303
+ { after: "todo", extra: true },
304
+ ];
305
+
306
+ for (const [index, value] of cases.entries()) {
307
+ resetBusForTests();
308
+ resetSectionsForTests();
309
+ const id = `bad-${index}`;
310
+ // SAFETY: the point of the case is a value the type forbids.
311
+ registerSection(section(id, { placement: value as SidebarSection["placement"] }));
312
+
313
+ assert.deepEqual(
314
+ snapshotSections().map((entry) => entry.id),
315
+ [id],
316
+ `${JSON.stringify(value)}: the section still registers`,
317
+ );
318
+ assert.deepEqual(places(), [`${id}@after footer`], "and lands at the end");
319
+ const rejection = getSectionPlacementRejections().find((entry) => entry.sectionId === id);
320
+ assert.ok(rejection !== undefined, `${JSON.stringify(value)}: the bad placement is reported`);
321
+ assert.equal(rejection.value, JSON.stringify(value), "the value is reported verbatim");
322
+ }
323
+ });
324
+
325
+ test("a conforming re-registration clears the placement rejection", () => {
326
+ const bad = { after: "" } as unknown as SidebarSection["placement"];
327
+ registerSection(section("music", { placement: bad }));
328
+ assert.equal(getSectionPlacementRejections().length, 1);
329
+
330
+ registerSection(section("music", { placement: { after: "todo" } }));
331
+
332
+ assert.deepEqual(getSectionPlacementRejections(), []);
333
+ assert.deepEqual(places(), ["music@after todo"]);
334
+ });
335
+
336
+ test("a valid placement is stored on the section exactly as written", () => {
337
+ const registered = section("music", { placement: { after: "loon.news" } });
338
+ registerSection(registered);
339
+
340
+ assert.equal(snapshotSections()[0], registered);
341
+ assert.deepEqual(snapshotSections()[0].placement, { after: "loon.news" });
342
+ });
343
+
344
+ test("nothing painted leaves every section at the end, and start says why", () => {
345
+ register(section("top", { placement: "start" }), section("plain"));
346
+
347
+ assert.deepEqual(places(snapshotSections(), []), ["top@after footer", "plain@after footer"]);
348
+ assert.match(resolutionOf("top", [])?.reason ?? "", /no native rail part is painted/);
349
+ });
350
+
351
+ test("the same registrations in any load order draw the same rail", () => {
352
+ const registrations = [
353
+ section("start-b", { placement: "start" }),
354
+ section("start-a", { placement: "start" }),
355
+ section("after-a", { placement: { after: "start-a" } }),
356
+ section("plain-b"),
357
+ section("plain-a"),
358
+ section("native", { placement: { after: "jobs" } }),
359
+ section("ordered", { order: 1 }),
360
+ ];
361
+
362
+ const permutations = [
363
+ registrations,
364
+ [...registrations].reverse(),
365
+ [registrations[3], registrations[0], registrations[6], registrations[2], registrations[5], registrations[1], registrations[4]],
366
+ ];
367
+
368
+ const answers = permutations.map((permutation) => {
369
+ resetBusForTests();
370
+ resetSectionsForTests();
371
+ register(...permutation);
372
+ return places();
373
+ });
374
+
375
+ assert.deepEqual(answers[1], answers[0], "reverse load order, same rail");
376
+ assert.deepEqual(answers[2], answers[0], "one more order, same rail");
377
+ assert.deepEqual(answers[0], [
378
+ "start-a@before todo",
379
+ "after-a@before todo",
380
+ "start-b@before todo",
381
+ "native@after jobs",
382
+ "ordered@after footer",
383
+ "plain-a@after footer",
384
+ "plain-b@after footer",
385
+ ]);
386
+ });
@@ -0,0 +1,312 @@
1
+ /**
2
+ * The shared `Sections` preview (S1, S2, S4): one list for every section row.
3
+ *
4
+ * The host draws ONE preview for its `Sections` rows — a title naming the row and a sample listing
5
+ * the sections that are active under that hypothetical (`[changes] [rdd] [agents] …`) — and a
6
+ * section registered through the bridge must appear in that same list instead of drawing a card
7
+ * picture of its own. These tests drive the real capture path with host `Sections` rows shaped
8
+ * exactly like the host's own inline closures (`extensions/gentle-shell.ts:3522-3524`, L3) and
9
+ * assert what `preview()` returns to the renderer.
10
+ */
11
+
12
+ import assert from "node:assert/strict";
13
+ import { mkdtempSync, rmSync } from "node:fs";
14
+ import { tmpdir } from "node:os";
15
+ import { join } from "node:path";
16
+ import test from "node:test";
17
+
18
+ import {
19
+ registerCardStyle,
20
+ registerSection,
21
+ resetBusForTests,
22
+ resetCardStylesForTests,
23
+ resetSectionsForTests,
24
+ } from "../../lib/shell-api/bus.ts";
25
+ import {
26
+ disableCaptureBackend,
27
+ installCaptureBackend,
28
+ resetCaptureStateForTests,
29
+ } from "../../lib/shell-api/capture.ts";
30
+ import { resetHookStateForTests } from "../../lib/shell-api/hook.ts";
31
+ import { resetRowBuildStateForTests } from "../../lib/shell-api/rows.ts";
32
+ import { installSectionRows } from "../../lib/shell-api/sections-rows.ts";
33
+ import { resetStoreStateForTests } from "../../lib/shell-api/store.ts";
34
+ import type { CustomizeRow, InlineHelpers, SidebarSection } from "../../lib/shell-api/types.ts";
35
+ import { setSectionVisible } from "../../lib/shell-api/visibility.ts";
36
+ import { createFakeHost } from "./fake-host.ts";
37
+
38
+ let configHome: string;
39
+ let previousConfigHome: string | undefined;
40
+
41
+ test.before(() => {
42
+ configHome = mkdtempSync(join(tmpdir(), "gsb-preview-"));
43
+ previousConfigHome = process.env.GENTLE_PI_CONFIG_HOME;
44
+ process.env.GENTLE_PI_CONFIG_HOME = configHome;
45
+ });
46
+
47
+ test.after(() => {
48
+ if (previousConfigHome === undefined) delete process.env.GENTLE_PI_CONFIG_HOME;
49
+ else process.env.GENTLE_PI_CONFIG_HOME = previousConfigHome;
50
+ rmSync(configHome, { recursive: true, force: true });
51
+ });
52
+
53
+ test.beforeEach(() => {
54
+ rmSync(join(configHome, "gentle-shell-bridge"), { recursive: true, force: true });
55
+ resetBusForTests();
56
+ resetSectionsForTests();
57
+ resetCardStylesForTests();
58
+ resetStoreStateForTests();
59
+ resetCaptureStateForTests();
60
+ resetHookStateForTests();
61
+ resetRowBuildStateForTests();
62
+ });
63
+
64
+ test.afterEach(() => {
65
+ disableCaptureBackend();
66
+ });
67
+
68
+ const section = (id: string, extra: Partial<SidebarSection> = {}): SidebarSection => ({
69
+ id,
70
+ title: id.toUpperCase(),
71
+ lines: () => [`body ${id}`],
72
+ ...extra,
73
+ });
74
+
75
+ const inlineHelpers: InlineHelpers = {
76
+ input: async () => undefined,
77
+ confirm: async () => true,
78
+ capture: async () => undefined,
79
+ };
80
+
81
+ /** The host's persisted section visibility for these tests: one section hidden, two shown. */
82
+ const VISIBILITY: Record<string, boolean> = { changes: true, rdd: false, agents: true };
83
+
84
+ /**
85
+ * The host's own `Sections` rows (`extensions/gentle-shell.ts:3522-3524`): two per key, each with the
86
+ * host's inline preview closure over its own visibility map.
87
+ */
88
+ const hostSectionsRows = (visibility: Record<string, boolean>): CustomizeRow[] => {
89
+ const rows: CustomizeRow[] = [];
90
+ for (const key of ["changes", "rdd", "agents"]) {
91
+ for (const shown of [true, false]) {
92
+ const value = shown ? "shown" : "hidden";
93
+ rows.push({
94
+ category: "Sections",
95
+ label: () => key,
96
+ choice: {
97
+ setting: key,
98
+ value,
99
+ current: () => visibility[key] === shown,
100
+ applyOnMove: false,
101
+ },
102
+ preview: () => {
103
+ const hypothetical = { ...visibility, [key]: shown };
104
+ const active = Object.entries(hypothetical)
105
+ .filter(([, on]) => on)
106
+ .map(([name]) => name);
107
+ return {
108
+ title: `Sections · ${key} ${value}`,
109
+ sample: `[${active.join("] [") || "no optional sections"}]`,
110
+ };
111
+ },
112
+ action: () => {},
113
+ });
114
+ }
115
+ }
116
+ return rows;
117
+ };
118
+
119
+ /** The host's own `Card style` rows (`extensions/gentle-shell.ts:3398-3419`). */
120
+ const hostCardStyleRows = (): CustomizeRow[] =>
121
+ ["neon", "float"].map((value) => ({
122
+ category: "Cards",
123
+ label: () => value,
124
+ choice: {
125
+ setting: "Card style",
126
+ value,
127
+ current: () => value === "neon",
128
+ applyOnMove: false,
129
+ },
130
+ action: () => {},
131
+ }));
132
+
133
+ /** One registered style, so the capture splices a value row into the host's `Card style` line. */
134
+ const CARD_STYLE = {
135
+ id: "loon.frame",
136
+ label: "frame",
137
+ frame: { top: () => "╭", line: () => "│", bottom: () => "╰" },
138
+ };
139
+
140
+ const rowFor = (
141
+ rows: readonly CustomizeRow[],
142
+ match: (row: CustomizeRow) => boolean,
143
+ ): CustomizeRow => {
144
+ const found = rows.find(match);
145
+ assert.ok(found !== undefined, "the menu holds the row this test writes about");
146
+ return found;
147
+ };
148
+
149
+ const toggleRow = (rows: readonly CustomizeRow[], setting: string, value: string): CustomizeRow =>
150
+ rowFor(rows, (row) => row.choice?.setting === setting && row.choice.value === value);
151
+
152
+ test("the host's own Sections row gains the bridge labels and keeps its title", () => {
153
+ registerSection(section("loon.issues", { title: "ISSUES" }));
154
+ registerSection(section("loon.music", { title: "MUSIC" }));
155
+ installSectionRows();
156
+ const host = createFakeHost({ rows: hostSectionsRows(VISIBILITY) });
157
+ installCaptureBackend(host.ui);
158
+
159
+ host.buildMenu();
160
+
161
+ const rows = host.liveRows();
162
+ const hostRow = toggleRow(rows, "changes", "shown");
163
+ assert.deepEqual(hostRow.preview?.(), {
164
+ title: "Sections · changes shown",
165
+ sample: "[changes] [agents] [issues] [music]",
166
+ });
167
+ assert.deepEqual(
168
+ hostRow.preview?.(),
169
+ hostRow.preview?.(),
170
+ "reading the preview twice never double-appends the bridge's labels",
171
+ );
172
+
173
+ assert.deepEqual(toggleRow(rows, "issues", "shown").preview?.(), {
174
+ title: "Sections · issues shown",
175
+ sample: "[changes] [agents] [issues] [music]",
176
+ });
177
+ assert.deepEqual(toggleRow(rows, "issues", "hidden").preview?.(), {
178
+ title: "Sections · issues hidden",
179
+ sample: "[changes] [agents] [music]",
180
+ });
181
+ });
182
+
183
+ test("a hidden bridge section leaves the list, and its own row puts it back", async () => {
184
+ registerSection(section("loon.issues", { title: "ISSUES" }));
185
+ registerSection(section("loon.music", { title: "MUSIC" }));
186
+ installSectionRows();
187
+ const host = createFakeHost({ rows: hostSectionsRows(VISIBILITY) });
188
+ installCaptureBackend(host.ui);
189
+ host.buildMenu();
190
+
191
+ const rows = host.liveRows();
192
+ const hideIssues = toggleRow(rows, "issues", "hidden");
193
+ assert.ok(typeof hideIssues.action === "function");
194
+ await hideIssues.action(inlineHelpers);
195
+ assert.deepEqual(
196
+ [toggleRow(rows, "issues", "shown").choice?.current(), toggleRow(rows, "issues", "hidden").choice?.current()],
197
+ [false, true],
198
+ "the press persisted hidden",
199
+ );
200
+
201
+ assert.equal(
202
+ toggleRow(rows, "issues", "shown").preview?.().sample,
203
+ "[changes] [agents] [issues] [music]",
204
+ "the focused row previews its OWN hypothetical, on top of the persisted state of the rest",
205
+ );
206
+ assert.equal(
207
+ toggleRow(rows, "issues", "hidden").preview?.().sample,
208
+ "[changes] [agents] [music]",
209
+ "and the hidden hypothetical leaves it out",
210
+ );
211
+ assert.equal(
212
+ toggleRow(rows, "changes", "shown").preview?.().sample,
213
+ "[changes] [agents] [music]",
214
+ "the host's own row follows the persisted state, not a hypothetical",
215
+ );
216
+ });
217
+
218
+ test("a throwing host preview cannot break the menu", () => {
219
+ registerSection(section("loon.issues", { title: "ISSUES" }));
220
+ installSectionRows();
221
+ const rows = hostSectionsRows(VISIBILITY);
222
+ const broken = rows[0];
223
+ assert.ok(broken !== undefined);
224
+ broken.preview = () => {
225
+ throw new Error("host preview exploded");
226
+ };
227
+ const host = createFakeHost({ rows });
228
+ installCaptureBackend(host.ui);
229
+
230
+ host.buildMenu();
231
+
232
+ const hostRow = toggleRow(host.liveRows(), "changes", "shown");
233
+ const preview = hostRow.preview?.();
234
+ assert.ok(preview !== undefined, "the row still has a preview");
235
+ assert.equal(preview.title, "Sections", "the title it could not read falls back to the category");
236
+ assert.equal(preview.sample, "[issues]", "and the bridge's own list is still previewable");
237
+ });
238
+
239
+ test("a factory that throws leaves the host's own preview functions exactly as they were", () => {
240
+ registerSection(section("loon.issues", { title: "ISSUES" }));
241
+ installSectionRows();
242
+ // A registered style makes the capture splice the host's `Card style` line, and those rows sit
243
+ // BEFORE the `Sections` rows: the patch's indices only survive a rollback taken in reverse order,
244
+ // so without this the two edits could be undone in either order and the test would not notice.
245
+ registerCardStyle(CARD_STYLE);
246
+ const rows = [...hostCardStyleRows(), ...hostSectionsRows(VISIBILITY)];
247
+ const originals = rows.map((row) => row.preview);
248
+ const identities = rows.slice();
249
+ const host = createFakeHost();
250
+ installCaptureBackend(host.ui);
251
+
252
+ let duringBuild: CustomizeRow[] = [];
253
+ assert.throws(
254
+ () =>
255
+ host.ui.custom?.((_ui, _theme) => {
256
+ rows.map((row) => row.category ?? "Settings");
257
+ duringBuild = rows.slice();
258
+ throw new Error("factory exploded");
259
+ }),
260
+ /factory exploded/u,
261
+ );
262
+
263
+ // The two host rows, the one value row the splice inserts, and the bridge's two toggle rows.
264
+ assert.equal(
265
+ duringBuild.length,
266
+ identities.length + 3,
267
+ "the early injection spliced, patched and appended",
268
+ );
269
+ const firstSectionsRow = 3;
270
+ assert.equal(
271
+ duringBuild[firstSectionsRow]?.choice?.setting,
272
+ "changes",
273
+ "the splice shifted the Sections rows by the one value row this test assumes",
274
+ );
275
+ assert.notEqual(
276
+ duringBuild[firstSectionsRow],
277
+ identities[2],
278
+ "the host's row is patched before the host's own map reads it",
279
+ );
280
+ assert.equal(
281
+ duringBuild[firstSectionsRow]?.preview?.().sample,
282
+ "[changes] [agents] [issues]",
283
+ "and the bridge's label is in its sample during the build",
284
+ );
285
+
286
+ assert.equal(rows.length, identities.length, "the appended rows and the spliced value are gone");
287
+ for (let index = 0; index < identities.length; index++) {
288
+ assert.equal(rows[index], identities[index], "the host's own row is back in place");
289
+ assert.equal(rows[index]?.preview, originals[index], "and its own preview is untouched");
290
+ }
291
+ });
292
+
293
+ test("hiding a section through the store removes it from the host's sample", () => {
294
+ registerSection(section("loon.issues", { title: "ISSUES" }));
295
+ installSectionRows();
296
+ const host = createFakeHost({ rows: hostSectionsRows(VISIBILITY) });
297
+ installCaptureBackend(host.ui);
298
+ host.buildMenu();
299
+ assert.equal(
300
+ toggleRow(host.liveRows(), "changes", "shown").preview?.().sample,
301
+ "[changes] [agents] [issues]",
302
+ "the section is in the shared list while it is shown",
303
+ );
304
+
305
+ setSectionVisible("loon.issues", false);
306
+
307
+ assert.equal(
308
+ toggleRow(host.liveRows(), "changes", "shown").preview?.().sample,
309
+ "[changes] [agents]",
310
+ "a hidden section is out of the list the host's row draws",
311
+ );
312
+ });