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,157 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+
4
+ import type { RowProvider } from "../../lib/shell-api/types.ts";
5
+ import { resetBusForTests, registerRows } from "../../lib/shell-api/bus.ts";
6
+ import { getCaptureDiagnostics, resetCaptureStateForTests } from "../../lib/shell-api/capture.ts";
7
+ import gentleShellBridge, {
8
+ type BridgeExtensionApi,
9
+ type BridgeSessionContext,
10
+ type BridgeSessionHandler,
11
+ } from "../../lib/shell-api/extension.ts";
12
+ import { resetHookStateForTests } from "../../lib/shell-api/hook.ts";
13
+ import { getPluginLineReport, resetPluginLinesForTests } from "../../lib/shell-api/plugin-lines.ts";
14
+ import { resetRowBuildStateForTests } from "../../lib/shell-api/rows.ts";
15
+ import { resetStoreStateForTests } from "../../lib/shell-api/store.ts";
16
+ import { createFakeHost } from "./fake-host.ts";
17
+
18
+ const HOOK_KEY = Symbol.for("gentle-pi.customize-row-providers");
19
+ const hookSlot = (): Record<symbol, unknown> => globalThis as unknown as Record<symbol, unknown>;
20
+
21
+ test.beforeEach(() => {
22
+ resetBusForTests();
23
+ resetCaptureStateForTests();
24
+ resetRowBuildStateForTests();
25
+ resetHookStateForTests();
26
+ resetStoreStateForTests();
27
+ });
28
+
29
+ test.afterEach(() => {
30
+ delete hookSlot()[HOOK_KEY];
31
+ resetHookStateForTests();
32
+ });
33
+
34
+ const labelsProvider = (id: string, category: string, labels: string[]): RowProvider => ({
35
+ id,
36
+ category,
37
+ rows: () => labels.map((label) => ({ label, action: () => {} })),
38
+ });
39
+
40
+ interface FakeApi {
41
+ api: BridgeExtensionApi;
42
+ sessionHandlers: BridgeSessionHandler[];
43
+ shutdownHandlers: BridgeSessionHandler[];
44
+ commands: Array<{ name: string }>;
45
+ }
46
+
47
+ const createFakeApi = (): FakeApi => {
48
+ const sessionHandlers: BridgeSessionHandler[] = [];
49
+ const shutdownHandlers: BridgeSessionHandler[] = [];
50
+ const commands: Array<{ name: string }> = [];
51
+ const api: BridgeExtensionApi = {
52
+ // The REQUIRED host package is present: `session_start` arms as before (S9/T12).
53
+ getCommands: () => [{ name: "gentle:customize" }],
54
+ on(event, handler) {
55
+ if (event === "session_start") {
56
+ sessionHandlers.push(handler);
57
+ return;
58
+ }
59
+ assert.equal(event, "session_shutdown", `not a session event: ${String(event)}`);
60
+ shutdownHandlers.push(handler);
61
+ },
62
+ // The host's own command registry: a fake that records it proves this factory registers none.
63
+ registerCommand(name: string) {
64
+ commands.push({ name });
65
+ },
66
+ } as unknown as BridgeExtensionApi;
67
+ return { api, sessionHandlers, shutdownHandlers, commands };
68
+ };
69
+
70
+ test("registers one session_start and one session_shutdown handler, and no command", () => {
71
+ const fake = createFakeApi();
72
+
73
+ gentleShellBridge(fake.api);
74
+
75
+ assert.equal(fake.sessionHandlers.length, 1);
76
+ assert.equal(fake.shutdownHandlers.length, 1, "shutdown is how the INTEGRATIONS row is cleared");
77
+ assert.deepEqual(fake.commands, [], "the shell API registers no command of its own");
78
+ });
79
+
80
+ test("a session_start without UI arms nothing", () => {
81
+ const host = createFakeHost();
82
+ registerRows(labelsProvider("p1", "Cards", ["Must not appear"]));
83
+ const fake = createFakeApi();
84
+ gentleShellBridge(fake.api);
85
+
86
+ const context: BridgeSessionContext = { hasUI: false };
87
+ fake.sessionHandlers[0]({}, context);
88
+
89
+ assert.equal(getCaptureDiagnostics().armedTargets, 0);
90
+ host.buildMenu();
91
+ assert.deepEqual(host.rendered().slice(4), []);
92
+ });
93
+
94
+ test("two session_start events with UI arm once and inject once", () => {
95
+ const host = createFakeHost();
96
+ registerRows(labelsProvider("p1", "Cards", ["Once"]));
97
+ const fake = createFakeApi();
98
+ gentleShellBridge(fake.api);
99
+ const context: BridgeSessionContext = { hasUI: true, ui: host.ui };
100
+
101
+ fake.sessionHandlers[0]({}, context);
102
+ fake.sessionHandlers[0]({}, context);
103
+
104
+ assert.equal(getCaptureDiagnostics().armedTargets, 1, "one wrapper across two events");
105
+ host.buildMenu();
106
+ assert.deepEqual(
107
+ host.rendered().filter((label) => label === "Once"),
108
+ ["Once"],
109
+ );
110
+ assert.equal(host.invalidateCount(), 1);
111
+ });
112
+
113
+ test("loading the extension twice still arms the same ui once", () => {
114
+ const host = createFakeHost();
115
+ registerRows(labelsProvider("p1", "Cards", ["Once"]));
116
+ const first = createFakeApi();
117
+ const second = createFakeApi();
118
+ gentleShellBridge(first.api);
119
+ gentleShellBridge(second.api);
120
+ const context: BridgeSessionContext = { hasUI: true, ui: host.ui };
121
+
122
+ first.sessionHandlers[0]({}, context);
123
+ second.sessionHandlers[0]({}, context);
124
+
125
+ assert.equal(getCaptureDiagnostics().armedTargets, 1);
126
+ host.buildMenu();
127
+ assert.deepEqual(
128
+ host.rendered().filter((label) => label === "Once"),
129
+ ["Once"],
130
+ );
131
+ });
132
+
133
+ test("the factory installs the INTEGRATIONS surface on the session's ui", () => {
134
+ registerRows(labelsProvider("loon.banner-source", "Banner", ["Row"]));
135
+ const fake = createFakeApi();
136
+ gentleShellBridge(fake.api);
137
+ const statuses: string[] = [];
138
+ const ui = {
139
+ setStatus: (key: string, text?: string) => {
140
+ if (text !== undefined) statuses.push(`${key}: ${text}`);
141
+ },
142
+ };
143
+
144
+ assert.equal(
145
+ getPluginLineReport().availability,
146
+ "unavailable",
147
+ "before session_start there is no ui to paint a plugin line with",
148
+ );
149
+
150
+ fake.sessionHandlers[0]({}, { hasUI: true, ui });
151
+
152
+ assert.equal(
153
+ getPluginLineReport().availability,
154
+ "installed",
155
+ "session_start installs the INTEGRATIONS surface on the host's own setStatus",
156
+ );
157
+ });
@@ -0,0 +1,58 @@
1
+ /**
2
+ * A fake `pi.events`: Pi's cross-extension bus (`docs/extensions.md:1965-1972`).
3
+ *
4
+ * The real bus (`dist/core/event-bus.js`) is an `EventEmitter` behind two methods: `on(channel,
5
+ * handler)` returns an unsubscribe function, and `emit(channel, data)` calls every handler. The
6
+ * real handler wrapper is `async`, so a handler's *synchronous* work has completed by the time
7
+ * `emit` returns — this fake delivers synchronously for the same reason, which is what lets a
8
+ * bridge that publishes later register its plugin's surfaces during the emit.
9
+ *
10
+ * It counts listeners so a test can prove a plugin that found the API at load leaves nothing
11
+ * behind, and records every emit so "exactly once per process" is observable.
12
+ */
13
+
14
+ export type FakeEventHandler = (payload: unknown) => void;
15
+
16
+ export interface FakeEmit {
17
+ channel: string;
18
+ payload: unknown;
19
+ }
20
+
21
+ export interface FakeEvents {
22
+ on(channel: string, handler: FakeEventHandler): () => void;
23
+ emit(channel: string, payload?: unknown): void;
24
+ /** Every `emit` this bus saw, in order. */
25
+ emitted(): FakeEmit[];
26
+ /** Live listeners on one channel, or on every channel when omitted. */
27
+ listenerCount(channel?: string): number;
28
+ }
29
+
30
+ export function createFakeEvents(): FakeEvents {
31
+ const listeners = new Map<string, Set<FakeEventHandler>>();
32
+ const emits: FakeEmit[] = [];
33
+
34
+ const count = (channel: string): number => listeners.get(channel)?.size ?? 0;
35
+
36
+ return {
37
+ on(channel, handler) {
38
+ const set = listeners.get(channel) ?? new Set<FakeEventHandler>();
39
+ set.add(handler);
40
+ listeners.set(channel, set);
41
+ return () => {
42
+ set.delete(handler);
43
+ };
44
+ },
45
+ emit(channel, payload) {
46
+ emits.push({ channel, payload });
47
+ // A copy, so a listener that unsubscribes while being notified cannot skip its neighbour.
48
+ for (const handler of [...(listeners.get(channel) ?? [])]) handler(payload);
49
+ },
50
+ emitted: () => [...emits],
51
+ listenerCount: (channel) => {
52
+ if (channel !== undefined) return count(channel);
53
+ let total = 0;
54
+ for (const set of listeners.values()) total += set.size;
55
+ return total;
56
+ },
57
+ };
58
+ }
@@ -0,0 +1,211 @@
1
+ /**
2
+ * A fake host for the capture backend, modelling `gentle-shell.ts:3550-3562` and
3
+ * `lib/visual-customize-view.ts:387`.
4
+ *
5
+ * The host builds its `rows` array, derives categories from it with a single synchronous
6
+ * `rows.map(...)` inside the factory — the one moment the array is reachable — and returns a
7
+ * plain adapter `{ render, handleInput, invalidate, dispose }` that hides the rows. Duck-typing
8
+ * `options.rows` on the adapter never matches, exactly like the real host.
9
+ *
10
+ * The adapter renders from the live array, so a test asserts what the user would see after an
11
+ * injection, not just what the bridge captured.
12
+ */
13
+
14
+ import type { CustomizeRow, InlineHelpers, RowLabel } from "../../lib/shell-api/types.ts";
15
+
16
+ export type CustomFactory = (ui: unknown, theme: unknown, ...rest: unknown[]) => unknown;
17
+
18
+ /** The `custom` surface Pi hands extensions; own property or inherited, depending on shape. */
19
+ export interface FakeUiTarget {
20
+ custom?: (factory: CustomFactory, ...rest: unknown[]) => unknown;
21
+ }
22
+
23
+ export interface FakeAdapter {
24
+ render: () => string[];
25
+ handleInput: (data: string) => unknown;
26
+ invalidate: () => void;
27
+ dispose: () => void;
28
+ }
29
+
30
+ export type FakeRow = CustomizeRow;
31
+
32
+ export interface FakeHostOptions {
33
+ /** The host's own menu rows. Must hold at least the capture threshold's worth of rows. */
34
+ rows?: FakeRow[];
35
+ /** `own` puts `custom` on the instance; `prototype` leaves the instance inheriting it. */
36
+ customShape?: "own" | "prototype";
37
+ /** `promise` makes the factory return a promise, as the real chain allows. */
38
+ factoryResult?: "adapter" | "promise";
39
+ /** Throw inside the factory after the category map ran. */
40
+ throwInsideFactory?: boolean;
41
+ /** Map only a non-row array inside the factory, never the rows array. */
42
+ mapOnlyNonRows?: boolean;
43
+ /** Theme handed to the factory as its second argument. */
44
+ theme?: unknown;
45
+ /** Extra factory arguments after the theme (the host passes keybindings and `close`). */
46
+ extraFactoryArgs?: unknown[];
47
+ }
48
+
49
+ export interface FakeHost {
50
+ readonly ui: FakeUiTarget;
51
+ /** The factory itself, so a nesting test can re-enter a wrapped `custom` call. */
52
+ readonly factory: CustomFactory;
53
+ /** What Pi does: `ui.custom(factory, options)`. */
54
+ buildMenu: () => unknown;
55
+ /** The live array the adapter renders from (rows the host built plus anything injected). */
56
+ liveRows: () => FakeRow[];
57
+ /** Categories the host derived from that array in the factory's single `rows.map(...)`. */
58
+ categories: () => string[];
59
+ /** Labels the user would see right now. */
60
+ rendered: () => string[];
61
+ invalidateCount: () => number;
62
+ disposeCount: () => number;
63
+ factoryCallCount: () => number;
64
+ /** Enter on the first visible row whose rendered label matches, including the view's repaint. */
65
+ press: (label: string) => Promise<void>;
66
+ /** Enter on the first visible row matching, so a test can target a choice value, not a label. */
67
+ pressRow: (match: (row: FakeRow) => boolean) => Promise<void>;
68
+ /**
69
+ * The `●`/`○` lines the real renderer draws: consecutive rows sharing `choice.setting` become one
70
+ * line (`lib/visual-customize-view.ts:583-595`), so this reads `current()` at render time.
71
+ */
72
+ toggleLines: () => string[];
73
+ }
74
+
75
+ const inlineHelpers: InlineHelpers = {
76
+ input: async () => undefined,
77
+ confirm: async () => true,
78
+ capture: async () => undefined,
79
+ };
80
+
81
+ const labelText = (label: RowLabel | undefined): string => {
82
+ if (typeof label === "function") return label();
83
+ return label ?? "";
84
+ };
85
+
86
+ const isVisible = (row: FakeRow): boolean => {
87
+ if (row.visible === undefined) return true;
88
+ try {
89
+ return row.visible();
90
+ } catch {
91
+ return false; // the renderer fails closed
92
+ }
93
+ };
94
+
95
+ /** Four rows: one more than the capture threshold, spread over three categories. */
96
+ const defaultRows = (): FakeRow[] => [
97
+ { category: "Cards", label: "Card bash: builtin", action: () => {} },
98
+ { category: "Cards", label: "Card output: builtin", action: () => {} },
99
+ { category: "Banner", label: "Banner: builtin", action: () => {} },
100
+ { category: "Layout", label: "Layout: builtin", action: () => {} },
101
+ ];
102
+
103
+ export function createFakeHost(options: FakeHostOptions = {}): FakeHost {
104
+ const rows: FakeRow[] = options.rows ?? defaultRows();
105
+ const theme = "theme" in options ? options.theme : { name: "fake-theme" };
106
+ const extraFactoryArgs = options.extraFactoryArgs ?? [{ keybindings: "fake" }, () => {}];
107
+ const customShape = options.customShape ?? "own";
108
+ const factoryResult = options.factoryResult ?? "adapter";
109
+
110
+ let invalidateCount = 0;
111
+ let disposeCount = 0;
112
+ let factoryCallCount = 0;
113
+ let categories: string[] = [];
114
+
115
+ const render = (): string[] => rows.filter(isVisible).map((row) => labelText(row.label));
116
+
117
+ const runAction = async (row: FakeRow): Promise<void> => {
118
+ await row.action?.(inlineHelpers);
119
+ invalidateCount += 1; // the real view repaints after any action (visual-customize-view.ts:579)
120
+ };
121
+
122
+ const adapter = (): FakeAdapter => ({
123
+ render,
124
+ handleInput: (data: string) => (data === "enter" ? press(render()[0] ?? "") : undefined),
125
+ invalidate: () => {
126
+ invalidateCount += 1;
127
+ },
128
+ dispose: () => {
129
+ disposeCount += 1;
130
+ },
131
+ });
132
+
133
+ const pressRow = async (match: (row: FakeRow) => boolean): Promise<void> => {
134
+ const row = rows.filter(isVisible).find(match);
135
+ if (row === undefined) throw new Error("fake host: no visible row matched the predicate");
136
+ await runAction(row);
137
+ };
138
+
139
+ const press = (label: string): Promise<void> =>
140
+ pressRow((candidate) => labelText(candidate.label) === label);
141
+
142
+ const toggleLines = (): string[] => {
143
+ const visible = rows.filter(isVisible);
144
+ const lines: string[] = [];
145
+ let index = 0;
146
+ while (index < visible.length) {
147
+ const setting = visible[index].choice?.setting;
148
+ if (setting === undefined) {
149
+ index += 1;
150
+ continue;
151
+ }
152
+ let end = index;
153
+ while (end + 1 < visible.length && visible[end + 1].choice?.setting === setting) end += 1;
154
+ const marks = visible
155
+ .slice(index, end + 1)
156
+ .map((row) => {
157
+ const mark = row.choice?.current() === true ? "●" : "○";
158
+ return `${mark} ${row.choice?.value ?? ""}`.trim();
159
+ });
160
+ lines.push(`${setting} ${marks.join(" ")}`);
161
+ index = end + 1;
162
+ }
163
+ return lines;
164
+ };
165
+
166
+ const factory: CustomFactory = (_ui, _theme, ..._rest) => {
167
+ factoryCallCount += 1;
168
+ // The one reachable moment (lib/visual-customize-view.ts:387): the view derives its
169
+ // categories from `options.rows` with a single synchronous `map` call inside the factory.
170
+ if (options.mapOnlyNonRows === true) {
171
+ [1, 2, 3].map((value) => value * 2); // a plain array of numbers: not a row array
172
+ } else {
173
+ categories = rows.map((row) => row.category ?? "Settings");
174
+ }
175
+ if (options.throwInsideFactory === true) throw new Error("factory exploded");
176
+
177
+ const built = adapter();
178
+ return factoryResult === "promise" ? Promise.resolve(built) : built;
179
+ };
180
+
181
+ const invoke = (factoryArg: CustomFactory): unknown => factoryArg(ui, theme, ...extraFactoryArgs);
182
+
183
+ const ownShape: FakeUiTarget = {
184
+ custom: (factoryArg: CustomFactory) => invoke(factoryArg),
185
+ };
186
+ const prototypeShape: FakeUiTarget = Object.create({
187
+ custom(this: unknown, factoryArg: CustomFactory) {
188
+ return invoke(factoryArg);
189
+ },
190
+ }) as FakeUiTarget;
191
+
192
+ const ui: FakeUiTarget = customShape === "prototype" ? prototypeShape : ownShape;
193
+
194
+ return {
195
+ ui,
196
+ factory,
197
+ buildMenu: () => {
198
+ if (typeof ui.custom !== "function") throw new Error("fake host: ui.custom disappeared");
199
+ return ui.custom(factory);
200
+ },
201
+ liveRows: () => rows,
202
+ categories: () => [...categories],
203
+ rendered: render,
204
+ invalidateCount: () => invalidateCount,
205
+ disposeCount: () => disposeCount,
206
+ factoryCallCount: () => factoryCallCount,
207
+ press,
208
+ pressRow,
209
+ toggleLines,
210
+ };
211
+ }