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.
- package/README.md +48 -25
- package/extensions/odd-worktree-tools.ts +64 -211
- package/lib/shell-api/api.ts +523 -0
- package/lib/shell-api/arm.ts +83 -0
- package/lib/shell-api/bus.ts +1389 -0
- package/lib/shell-api/capture.ts +1340 -0
- package/lib/shell-api/card-style-line.ts +112 -0
- package/lib/shell-api/card-style-rows.ts +390 -0
- package/lib/shell-api/card-style-transform.ts +862 -0
- package/lib/shell-api/card-style-wiring.ts +477 -0
- package/lib/shell-api/card-styles.ts +153 -0
- package/lib/shell-api/config.ts +71 -0
- package/lib/shell-api/extension.ts +247 -0
- package/lib/shell-api/hook.ts +322 -0
- package/lib/shell-api/index.ts +88 -0
- package/lib/shell-api/message-design-adoption.ts +549 -0
- package/lib/shell-api/message-design-rows.ts +313 -0
- package/lib/shell-api/message-design-wiring.ts +369 -0
- package/lib/shell-api/message-designs.ts +746 -0
- package/lib/shell-api/notices.ts +137 -0
- package/lib/shell-api/plugin-lines.ts +877 -0
- package/lib/shell-api/presence.ts +258 -0
- package/lib/shell-api/repaint.ts +351 -0
- package/lib/shell-api/row-labels.ts +146 -0
- package/lib/shell-api/row-names.ts +273 -0
- package/lib/shell-api/row-origin.ts +144 -0
- package/lib/shell-api/rows.ts +591 -0
- package/lib/shell-api/section-panel.ts +378 -0
- package/lib/shell-api/section-pointer.ts +164 -0
- package/lib/shell-api/sections-preview.ts +275 -0
- package/lib/shell-api/sections-rows.ts +133 -0
- package/lib/shell-api/sidebar-render.ts +705 -0
- package/lib/shell-api/sidebar.ts +1126 -0
- package/lib/shell-api/store.ts +279 -0
- package/lib/shell-api/tool-design-adoption.ts +263 -0
- package/lib/shell-api/tool-design-rows.ts +307 -0
- package/lib/shell-api/tool-design-wiring.ts +481 -0
- package/lib/shell-api/tool-designs.ts +1411 -0
- package/lib/shell-api/transform-pipeline.ts +351 -0
- package/lib/shell-api/types.ts +803 -0
- package/lib/shell-api/visibility.ts +154 -0
- package/package.json +3 -2
- package/tools/files-bridge.test.js +81 -66
- package/tools/fixtures/load-extension.js +40 -18
- package/tools/issues-command.test.js +4 -7
- package/tools/issues-section.test.js +71 -81
- package/tools/odd-rail-wiring.test.js +16 -10
- package/tools/package-surface.test.js +42 -13
- package/tools/prs-command.test.js +11 -6
- package/tools/shell-api/api.test.ts +484 -0
- package/tools/shell-api/apply-on-move.test.ts +529 -0
- package/tools/shell-api/arm.test.ts +220 -0
- package/tools/shell-api/bridge-ready.test.ts +309 -0
- package/tools/shell-api/bus.test.ts +194 -0
- package/tools/shell-api/button-rows.test.ts +435 -0
- package/tools/shell-api/capture.test.ts +583 -0
- package/tools/shell-api/card-style-rows.test.ts +493 -0
- package/tools/shell-api/card-style-transform.test.ts +379 -0
- package/tools/shell-api/card-style-wiring.test.ts +258 -0
- package/tools/shell-api/card-styles-e2e.test.ts +502 -0
- package/tools/shell-api/card-styles.test.ts +324 -0
- package/tools/shell-api/card-transcript-path.test.ts +178 -0
- package/tools/shell-api/compatibility-regression.test.mjs +64 -0
- package/tools/shell-api/config.test.ts +93 -0
- package/tools/shell-api/docs-coverage.test.ts +82 -0
- package/tools/shell-api/e2e.test.ts +562 -0
- package/tools/shell-api/example-bash-identity.test.ts +372 -0
- package/tools/shell-api/example-bash-matrix.test.ts +856 -0
- package/tools/shell-api/example-designs.test.ts +737 -0
- package/tools/shell-api/example-neon-transform.test.ts +283 -0
- package/tools/shell-api/example-plain-style.test.ts +1225 -0
- package/tools/shell-api/extension.test.ts +157 -0
- package/tools/shell-api/fake-events.ts +58 -0
- package/tools/shell-api/fake-host.ts +211 -0
- package/tools/shell-api/fake-sidebar.ts +516 -0
- package/tools/shell-api/fake-transcript.ts +486 -0
- package/tools/shell-api/frame-telemetry.test.ts +317 -0
- package/tools/shell-api/hook.test.ts +196 -0
- package/tools/shell-api/manifest.test.ts +47 -0
- package/tools/shell-api/message-design-adoption.test.ts +611 -0
- package/tools/shell-api/message-design-follows.test.ts +361 -0
- package/tools/shell-api/message-design-pi-release.test.mjs +157 -0
- package/tools/shell-api/message-design-regressions.test.mjs +162 -0
- package/tools/shell-api/message-design-rows.test.ts +266 -0
- package/tools/shell-api/message-design-wiring.test.ts +362 -0
- package/tools/shell-api/message-designs.test.ts +339 -0
- package/tools/shell-api/new-category.test.ts +1101 -0
- package/tools/shell-api/notices.test.ts +292 -0
- package/tools/shell-api/plugin-integration-lines.test.ts +827 -0
- package/tools/shell-api/presence.test.ts +396 -0
- package/tools/shell-api/rail-part-hits.test.ts +281 -0
- package/tools/shell-api/rail-surfaces.test.ts +402 -0
- package/tools/shell-api/rail-width.test.ts +406 -0
- package/tools/shell-api/regression.test.ts +434 -0
- package/tools/shell-api/repaint-api.test.ts +200 -0
- package/tools/shell-api/reserved-categories.test.ts +250 -0
- package/tools/shell-api/row-labels.test.ts +261 -0
- package/tools/shell-api/row-names.test.ts +407 -0
- package/tools/shell-api/row-origin.test.ts +395 -0
- package/tools/shell-api/row-placement-regression.test.ts +797 -0
- package/tools/shell-api/row-placement.test.ts +410 -0
- package/tools/shell-api/section-interaction.test.ts +364 -0
- package/tools/shell-api/section-panel.test.ts +430 -0
- package/tools/shell-api/section-placement-rail.test.ts +390 -0
- package/tools/shell-api/section-placement.test.ts +386 -0
- package/tools/shell-api/sections-preview.test.ts +312 -0
- package/tools/shell-api/sections-rows.test.ts +293 -0
- package/tools/shell-api/sidebar-backend.test.ts +477 -0
- package/tools/shell-api/sidebar-e2e.test.ts +317 -0
- package/tools/shell-api/sidebar-hover.test.ts +276 -0
- package/tools/shell-api/sidebar-independent.test.ts +291 -0
- package/tools/shell-api/sidebar-render.test.ts +281 -0
- package/tools/shell-api/sidebar-sections.test.ts +247 -0
- package/tools/shell-api/store.test.ts +192 -0
- package/tools/shell-api/surface-repaint-cost.test.ts +520 -0
- package/tools/shell-api/tool-design-card-style.test.ts +308 -0
- package/tools/shell-api/tool-design-chain.test.ts +471 -0
- package/tools/shell-api/tool-design-cost.test.ts +591 -0
- package/tools/shell-api/tool-design-rows.test.ts +627 -0
- package/tools/shell-api/tool-design-scopes-audit.test.ts +212 -0
- package/tools/shell-api/tool-designs.test.ts +2412 -0
- package/tools/shell-api/transform-pipeline.test.ts +376 -0
- package/tools/shell-api/transforms-bus.test.ts +233 -0
- package/tools/shell-api/verifier-width-regressions.test.ts +147 -0
- package/tools/shell-api-rpc.test.js +189 -0
- package/tools/user-prompt-card.test.js +8 -9
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
|
|
4
|
+
import { armBridge } from "../../lib/shell-api/arm.ts";
|
|
5
|
+
import { registerRows, resetBusForTests } from "../../lib/shell-api/bus.ts";
|
|
6
|
+
import { getCaptureDiagnostics, resetCaptureStateForTests } from "../../lib/shell-api/capture.ts";
|
|
7
|
+
import { getHookDiagnostics, resetHookStateForTests } from "../../lib/shell-api/hook.ts";
|
|
8
|
+
import type { CustomizeRow, RowProvider } from "../../lib/shell-api/types.ts";
|
|
9
|
+
import { createFakeHost } from "./fake-host.ts";
|
|
10
|
+
|
|
11
|
+
const HOOK_KEY = Symbol.for("gentle-pi.customize-row-providers");
|
|
12
|
+
const CAPTURE_WRAPPED = Symbol.for("gentle-shell-bridge.capture.wrapped.v1");
|
|
13
|
+
|
|
14
|
+
const hookSlot = (): Record<symbol, unknown> => globalThis as unknown as Record<symbol, unknown>;
|
|
15
|
+
const clearHook = (): void => {
|
|
16
|
+
delete hookSlot()[HOOK_KEY];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
test.beforeEach(() => {
|
|
20
|
+
resetBusForTests();
|
|
21
|
+
resetCaptureStateForTests();
|
|
22
|
+
resetHookStateForTests();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test.afterEach(() => {
|
|
26
|
+
clearHook();
|
|
27
|
+
resetHookStateForTests();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const labelsProvider = (id: string, category: string, labels: string[]): RowProvider => ({
|
|
31
|
+
id,
|
|
32
|
+
category,
|
|
33
|
+
rows: () => labels.map((label) => ({ label, action: () => {} })),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
interface PushedProvider {
|
|
37
|
+
id: string;
|
|
38
|
+
rows: (ctx?: unknown) => CustomizeRow[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
test("selects the hook backend and patches nothing on ui", () => {
|
|
42
|
+
const registry: PushedProvider[] = [];
|
|
43
|
+
hookSlot()[HOOK_KEY] = registry;
|
|
44
|
+
const host = createFakeHost();
|
|
45
|
+
registerRows(labelsProvider("p1", "Cards", ["Hooked row"]));
|
|
46
|
+
const customBefore = host.ui.custom;
|
|
47
|
+
|
|
48
|
+
const result = armBridge(host.ui);
|
|
49
|
+
|
|
50
|
+
assert.deepEqual(result, { outcome: "hooked", backend: "hook" });
|
|
51
|
+
assert.equal(host.ui.custom, customBefore, "ui.custom identity is unchanged");
|
|
52
|
+
assert.equal(Object.hasOwn(host.ui, CAPTURE_WRAPPED), false, "no marker on the instance");
|
|
53
|
+
const proto: object | null = Object.getPrototypeOf(host.ui) as object | null;
|
|
54
|
+
assert.equal(
|
|
55
|
+
proto === null ? false : Object.hasOwn(proto, CAPTURE_WRAPPED),
|
|
56
|
+
false,
|
|
57
|
+
"no marker on the prototype",
|
|
58
|
+
);
|
|
59
|
+
assert.equal(getCaptureDiagnostics().armedTargets, 0, "the capture backend never armed");
|
|
60
|
+
assert.equal(registry.length, 1);
|
|
61
|
+
|
|
62
|
+
// Rows land through the registry's own call path.
|
|
63
|
+
assert.deepEqual(
|
|
64
|
+
registry[0].rows({}).map((row) => row.label),
|
|
65
|
+
["Hooked row"],
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("an unusable hook shape falls back to capture and records why", () => {
|
|
70
|
+
hookSlot()[HOOK_KEY] = { register: 5 };
|
|
71
|
+
const host = createFakeHost();
|
|
72
|
+
registerRows(labelsProvider("p1", "Cards", ["Fallback row"]));
|
|
73
|
+
|
|
74
|
+
const result = armBridge(host.ui);
|
|
75
|
+
|
|
76
|
+
assert.deepEqual(result, {
|
|
77
|
+
outcome: "fallback-capture",
|
|
78
|
+
backend: "capture",
|
|
79
|
+
reason: "hook-unusable",
|
|
80
|
+
});
|
|
81
|
+
assert.equal(getHookDiagnostics().shape, "unusable");
|
|
82
|
+
|
|
83
|
+
host.buildMenu();
|
|
84
|
+
assert.deepEqual(host.rendered().slice(4), ["Fallback row"]);
|
|
85
|
+
assert.equal(host.invalidateCount(), 1);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("arms capture when the host exposes no registry", () => {
|
|
89
|
+
const host = createFakeHost();
|
|
90
|
+
registerRows(labelsProvider("p1", "Cards", ["Captured row"]));
|
|
91
|
+
|
|
92
|
+
const result = armBridge(host.ui);
|
|
93
|
+
|
|
94
|
+
assert.deepEqual(result, { outcome: "capture-armed", backend: "capture" });
|
|
95
|
+
host.buildMenu();
|
|
96
|
+
assert.deepEqual(host.rendered().slice(4), ["Captured row"]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("two session_start arms wrap once and inject once", () => {
|
|
100
|
+
const host = createFakeHost();
|
|
101
|
+
registerRows(labelsProvider("p1", "Cards", ["Once"]));
|
|
102
|
+
|
|
103
|
+
const first = armBridge(host.ui);
|
|
104
|
+
const second = armBridge(host.ui);
|
|
105
|
+
|
|
106
|
+
assert.deepEqual(first, { outcome: "capture-armed", backend: "capture" });
|
|
107
|
+
assert.deepEqual(second, { outcome: "already-armed", backend: "capture" });
|
|
108
|
+
assert.equal(getCaptureDiagnostics().armedTargets, 1, "one wrapper, not two");
|
|
109
|
+
|
|
110
|
+
host.buildMenu();
|
|
111
|
+
assert.deepEqual(
|
|
112
|
+
host.rendered().filter((label) => label === "Once"),
|
|
113
|
+
["Once"],
|
|
114
|
+
);
|
|
115
|
+
assert.equal(host.invalidateCount(), 1, "one invalidate per menu build");
|
|
116
|
+
|
|
117
|
+
host.buildMenu();
|
|
118
|
+
assert.deepEqual(
|
|
119
|
+
host.rendered().filter((label) => label === "Once"),
|
|
120
|
+
["Once"],
|
|
121
|
+
"a second build of the same menu still injects once",
|
|
122
|
+
);
|
|
123
|
+
assert.equal(host.invalidateCount(), 1);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("a prototype-only ui wraps where custom was found, and a second arm is a no-op", () => {
|
|
127
|
+
const host = createFakeHost({ customShape: "prototype" });
|
|
128
|
+
registerRows(labelsProvider("p1", "Cards", ["Proto row"]));
|
|
129
|
+
|
|
130
|
+
const first = armBridge(host.ui);
|
|
131
|
+
const second = armBridge(host.ui);
|
|
132
|
+
|
|
133
|
+
assert.deepEqual(first, { outcome: "capture-armed", backend: "capture" });
|
|
134
|
+
assert.deepEqual(second, { outcome: "already-armed", backend: "capture" });
|
|
135
|
+
assert.equal(Object.hasOwn(host.ui, "custom"), false, "the instance gained no own custom");
|
|
136
|
+
const proto: object | null = Object.getPrototypeOf(host.ui) as object | null;
|
|
137
|
+
assert.equal(
|
|
138
|
+
proto === null ? false : Object.hasOwn(proto, CAPTURE_WRAPPED),
|
|
139
|
+
true,
|
|
140
|
+
"the prototype carries the wrapper",
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
host.buildMenu();
|
|
144
|
+
assert.deepEqual(
|
|
145
|
+
host.rendered().filter((label) => label === "Proto row"),
|
|
146
|
+
["Proto row"],
|
|
147
|
+
);
|
|
148
|
+
assert.equal(host.invalidateCount(), 1);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("a no-UI context is a reported no-op, not a throw", () => {
|
|
152
|
+
assert.deepEqual(armBridge(undefined), {
|
|
153
|
+
outcome: "no-ui",
|
|
154
|
+
backend: "none",
|
|
155
|
+
reason: "ui-missing",
|
|
156
|
+
});
|
|
157
|
+
assert.deepEqual(armBridge(null), {
|
|
158
|
+
outcome: "no-ui",
|
|
159
|
+
backend: "none",
|
|
160
|
+
reason: "ui-missing",
|
|
161
|
+
});
|
|
162
|
+
assert.deepEqual(armBridge({}), {
|
|
163
|
+
outcome: "no-ui",
|
|
164
|
+
backend: "none",
|
|
165
|
+
reason: "custom-missing",
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test("importing the arm module has no side effects", async () => {
|
|
170
|
+
const armedBefore = getCaptureDiagnostics().armedTargets;
|
|
171
|
+
const mapBefore = Array.prototype.map;
|
|
172
|
+
|
|
173
|
+
// The query string is what makes a second module instance, so the specifier is computed: `tsc`
|
|
174
|
+
// types a non-literal dynamic import as a value import, not as a path to resolve on disk.
|
|
175
|
+
const freshSpecifier = "../../lib/shell-api/arm.ts?fresh=1";
|
|
176
|
+
const fresh: typeof import("../../lib/shell-api/arm.ts") = await import(freshSpecifier);
|
|
177
|
+
|
|
178
|
+
assert.equal(getCaptureDiagnostics().armedTargets, armedBefore, "import arms nothing");
|
|
179
|
+
assert.equal(Array.prototype.map, mapBefore, "import patches nothing");
|
|
180
|
+
assert.equal(typeof fresh.armBridge, "function");
|
|
181
|
+
|
|
182
|
+
// A menu built without arming must show no provider rows: nothing armed itself on import.
|
|
183
|
+
const host = createFakeHost();
|
|
184
|
+
registerRows(labelsProvider("p1", "Cards", ["Must not appear"]));
|
|
185
|
+
host.buildMenu();
|
|
186
|
+
assert.deepEqual(host.rendered().slice(4), []);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("the same providers produce the same rows through either backend", () => {
|
|
190
|
+
const providers = (): void => {
|
|
191
|
+
registerRows(labelsProvider("first", "Cards", ["First a"]));
|
|
192
|
+
registerRows({
|
|
193
|
+
id: "boom",
|
|
194
|
+
category: "Banner",
|
|
195
|
+
rows: () => {
|
|
196
|
+
throw new Error("shared provider exploded");
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
registerRows(labelsProvider("second", "Layout", ["Second b"]));
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
// Capture backend.
|
|
203
|
+
providers();
|
|
204
|
+
const host = createFakeHost();
|
|
205
|
+
armBridge(host.ui);
|
|
206
|
+
host.buildMenu();
|
|
207
|
+
const capturedLabels = host.rendered().slice(4);
|
|
208
|
+
|
|
209
|
+
// Hook backend, fresh bus with the same providers.
|
|
210
|
+
resetBusForTests();
|
|
211
|
+
resetCaptureStateForTests();
|
|
212
|
+
providers();
|
|
213
|
+
const registry: PushedProvider[] = [];
|
|
214
|
+
hookSlot()[HOOK_KEY] = registry;
|
|
215
|
+
armBridge(createFakeHost().ui);
|
|
216
|
+
const hookedLabels = registry[0].rows({}).map((row) => String(row.label));
|
|
217
|
+
|
|
218
|
+
assert.deepEqual(capturedLabels, ["First a", "Second b"]);
|
|
219
|
+
assert.deepEqual(hookedLabels, capturedLabels, "same providers, same rows, same order");
|
|
220
|
+
});
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The announcement: a plugin loaded BEFORE this package can still be served.
|
|
3
|
+
*
|
|
4
|
+
* The old rule — "the symbol's presence at `session_start` is the complete answer" — is wrong for
|
|
5
|
+
* a plugin Pi loads earlier than the package. The example's real TUI report showed it: the global
|
|
6
|
+
* extension's `session_start` handler ran first, found no symbol, and said "not installed" while
|
|
7
|
+
* the bridge went on to publish. So the bridge now tells the process it published, on Pi's own
|
|
8
|
+
* cross-extension bus, exactly once, and only when this copy really owns a compatible slot.
|
|
9
|
+
*
|
|
10
|
+
* These tests pin the event name and payload, the once-per-process rule across package copies, the
|
|
11
|
+
* silence for an incompatible slot, and `onBridgeApi` — the helper that gives an importing plugin
|
|
12
|
+
* both forms: immediate when the API is here, subscribed when it is coming.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import assert from "node:assert/strict";
|
|
16
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
17
|
+
import { tmpdir } from "node:os";
|
|
18
|
+
import { join } from "node:path";
|
|
19
|
+
import test from "node:test";
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
API_SCHEMA,
|
|
23
|
+
API_VERSION,
|
|
24
|
+
BRIDGE_API_KEY,
|
|
25
|
+
BRIDGE_READY_EVENT,
|
|
26
|
+
VERSION,
|
|
27
|
+
announceBridgeReady,
|
|
28
|
+
bridgeApi,
|
|
29
|
+
onBridgeApi,
|
|
30
|
+
publishBridgeApi,
|
|
31
|
+
resetBridgeApiForTests,
|
|
32
|
+
type BridgeApi,
|
|
33
|
+
} from "../../lib/shell-api/api.ts";
|
|
34
|
+
import {
|
|
35
|
+
resetBusForTests,
|
|
36
|
+
resetSectionsForTests,
|
|
37
|
+
snapshotProviders,
|
|
38
|
+
} from "../../lib/shell-api/bus.ts";
|
|
39
|
+
import gentleShellBridge, { type BridgeExtensionApi } from "../../lib/shell-api/extension.ts";
|
|
40
|
+
import { resetNoticesForTests } from "../../lib/shell-api/notices.ts";
|
|
41
|
+
import { resetStoreStateForTests } from "../../lib/shell-api/store.ts";
|
|
42
|
+
import type { RowProvider } from "../../lib/shell-api/types.ts";
|
|
43
|
+
import { createFakeEvents, type FakeEvents } from "./fake-events.ts";
|
|
44
|
+
|
|
45
|
+
const hostSlot = (): Record<symbol, unknown> => globalThis as unknown as Record<symbol, unknown>;
|
|
46
|
+
|
|
47
|
+
const provider = (id: string): RowProvider => ({ id, category: "Cards", rows: () => [] });
|
|
48
|
+
|
|
49
|
+
/** Every terminal write the process would show; nothing here may produce one. */
|
|
50
|
+
const captureConsole = (): { writes: string[]; restore: () => void } => {
|
|
51
|
+
const writes: string[] = [];
|
|
52
|
+
const sink = (globalThis as unknown as { console: { log: unknown; warn: unknown; error: unknown } })
|
|
53
|
+
.console;
|
|
54
|
+
const original = { log: sink.log, warn: sink.warn, error: sink.error };
|
|
55
|
+
const record =
|
|
56
|
+
(method: string) =>
|
|
57
|
+
(...args: unknown[]): void => {
|
|
58
|
+
writes.push(`${method}: ${args.map((arg) => String(arg)).join(" ")}`);
|
|
59
|
+
};
|
|
60
|
+
sink.log = record("log");
|
|
61
|
+
sink.warn = record("warn");
|
|
62
|
+
sink.error = record("error");
|
|
63
|
+
return {
|
|
64
|
+
writes,
|
|
65
|
+
restore: () => {
|
|
66
|
+
sink.log = original.log;
|
|
67
|
+
sink.warn = original.warn;
|
|
68
|
+
sink.error = original.error;
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
let configHome: string;
|
|
74
|
+
let previousConfigHome: string | undefined;
|
|
75
|
+
|
|
76
|
+
test.before(() => {
|
|
77
|
+
configHome = mkdtempSync(join(tmpdir(), "gsb-ready-"));
|
|
78
|
+
previousConfigHome = process.env.GENTLE_PI_CONFIG_HOME;
|
|
79
|
+
process.env.GENTLE_PI_CONFIG_HOME = configHome;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test.after(() => {
|
|
83
|
+
if (previousConfigHome === undefined) delete process.env.GENTLE_PI_CONFIG_HOME;
|
|
84
|
+
else process.env.GENTLE_PI_CONFIG_HOME = previousConfigHome;
|
|
85
|
+
rmSync(configHome, { recursive: true, force: true });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test.beforeEach(() => {
|
|
89
|
+
rmSync(join(configHome, "gentle-shell-bridge"), { recursive: true, force: true });
|
|
90
|
+
resetBridgeApiForTests();
|
|
91
|
+
resetNoticesForTests();
|
|
92
|
+
resetBusForTests();
|
|
93
|
+
resetSectionsForTests();
|
|
94
|
+
resetStoreStateForTests();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// ---------------------------------------------------------------------------------------------
|
|
98
|
+
// The event: name, payload, once per process
|
|
99
|
+
// ---------------------------------------------------------------------------------------------
|
|
100
|
+
|
|
101
|
+
test("the bridge announces itself with the schema, the major and the publishing release", () => {
|
|
102
|
+
const bus = createFakeEvents();
|
|
103
|
+
publishBridgeApi();
|
|
104
|
+
|
|
105
|
+
assert.equal(announceBridgeReady(bus), true);
|
|
106
|
+
|
|
107
|
+
assert.deepEqual(
|
|
108
|
+
bus.emitted().map((entry) => entry.channel),
|
|
109
|
+
[BRIDGE_READY_EVENT],
|
|
110
|
+
"one event, on the channel plugins listen to",
|
|
111
|
+
);
|
|
112
|
+
assert.equal(BRIDGE_READY_EVENT, "gentle-shell-bridge:ready", "the name is the contract");
|
|
113
|
+
|
|
114
|
+
const payload = bus.emitted()[0].payload as Record<string, unknown>;
|
|
115
|
+
assert.deepEqual(payload, { schema: API_SCHEMA, version: API_VERSION, VERSION });
|
|
116
|
+
assert.equal(payload.schema, "gentle-shell-bridge/api");
|
|
117
|
+
assert.equal(payload.version, 1);
|
|
118
|
+
assert.equal(payload.VERSION, "0.16.2");
|
|
119
|
+
assert.ok(Object.isFrozen(payload), "a listener cannot rewrite the announcement in place");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("the announcement happens once per process, however often this copy is asked", () => {
|
|
123
|
+
const bus = createFakeEvents();
|
|
124
|
+
publishBridgeApi();
|
|
125
|
+
|
|
126
|
+
assert.equal(announceBridgeReady(bus), true);
|
|
127
|
+
assert.equal(announceBridgeReady(bus), false, "the second ask is not an announcement");
|
|
128
|
+
assert.equal(announceBridgeReady(bus), false);
|
|
129
|
+
|
|
130
|
+
assert.equal(bus.emitted().length, 1, "one event for the process, not one per call");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("two loaded copies of the package announce once between them", async () => {
|
|
134
|
+
const first = createFakeEvents();
|
|
135
|
+
const second = createFakeEvents();
|
|
136
|
+
publishBridgeApi();
|
|
137
|
+
|
|
138
|
+
// Pi loads every extension with `moduleCache: false`, so the package really exists twice.
|
|
139
|
+
const copyPath = new URL("../../lib/shell-api/api.ts?second-copy", import.meta.url).href;
|
|
140
|
+
const copy = (await import(copyPath)) as typeof import("../../lib/shell-api/api.ts");
|
|
141
|
+
|
|
142
|
+
assert.equal(announceBridgeReady(first), true, "the copy that owns the slot announces");
|
|
143
|
+
assert.equal(copy.announceBridgeReady(second), false, "and the other one stays quiet");
|
|
144
|
+
assert.equal(first.emitted().length, 1);
|
|
145
|
+
assert.deepEqual(second.emitted(), [], "no second announcement, on any bus");
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("an incompatible slot is never announced", () => {
|
|
149
|
+
const bus = createFakeEvents();
|
|
150
|
+
const incompatible = { schema: "someone-else/api", version: 1 };
|
|
151
|
+
hostSlot()[BRIDGE_API_KEY] = incompatible;
|
|
152
|
+
|
|
153
|
+
publishBridgeApi();
|
|
154
|
+
assert.equal(announceBridgeReady(bus), false);
|
|
155
|
+
|
|
156
|
+
assert.deepEqual(bus.emitted(), [], "no plugin is told to look at a slot it cannot use");
|
|
157
|
+
assert.equal(hostSlot()[BRIDGE_API_KEY], incompatible, "and the slot is left exactly as it was");
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("a Pi without an event bus is skipped silently: the symbol still works", () => {
|
|
161
|
+
publishBridgeApi();
|
|
162
|
+
const captured = captureConsole();
|
|
163
|
+
let announced: boolean | undefined;
|
|
164
|
+
try {
|
|
165
|
+
assert.doesNotThrow(() => {
|
|
166
|
+
announced = announceBridgeReady(undefined);
|
|
167
|
+
});
|
|
168
|
+
} finally {
|
|
169
|
+
captured.restore();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
assert.equal(announced, false);
|
|
173
|
+
assert.deepEqual(captured.writes, [], "nothing lands in the terminal");
|
|
174
|
+
assert.equal(bridgeApi()?.VERSION, VERSION, "a plugin that reads the symbol is unaffected");
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// ---------------------------------------------------------------------------------------------
|
|
178
|
+
// onBridgeApi: immediate when it is here, subscribed when it is coming
|
|
179
|
+
// ---------------------------------------------------------------------------------------------
|
|
180
|
+
|
|
181
|
+
const apiHandler = (): { calls: BridgeApi[]; handler: (api: BridgeApi) => void } => {
|
|
182
|
+
const calls: BridgeApi[] = [];
|
|
183
|
+
return { calls, handler: (api) => calls.push(api) };
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
test("onBridgeApi calls the handler immediately when the API is already published, without listening", () => {
|
|
187
|
+
const bus = createFakeEvents();
|
|
188
|
+
const api = publishBridgeApi();
|
|
189
|
+
const { calls, handler } = apiHandler();
|
|
190
|
+
|
|
191
|
+
const dispose = onBridgeApi(handler, bus);
|
|
192
|
+
|
|
193
|
+
assert.deepEqual(calls, [api], "the plugin is served in the same turn, before any event");
|
|
194
|
+
assert.equal(bus.listenerCount(), 0, "and nothing is left subscribed: no listener leak");
|
|
195
|
+
assert.doesNotThrow(() => dispose());
|
|
196
|
+
assert.equal(calls.length, 1, "disposing after the fact cannot call the handler again");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test("onBridgeApi serves a plugin loaded first: one delivery when the announcement arrives", () => {
|
|
200
|
+
const bus = createFakeEvents();
|
|
201
|
+
const { calls, handler } = apiHandler();
|
|
202
|
+
|
|
203
|
+
const dispose = onBridgeApi(handler, bus);
|
|
204
|
+
assert.deepEqual(calls, [], "absent at subscription time: nothing to hand over yet");
|
|
205
|
+
assert.equal(bus.listenerCount(BRIDGE_READY_EVENT), 1);
|
|
206
|
+
|
|
207
|
+
const api = publishBridgeApi();
|
|
208
|
+
assert.equal(announceBridgeReady(bus), true);
|
|
209
|
+
|
|
210
|
+
assert.deepEqual(calls, [api], "the announcement delivered the published object, once");
|
|
211
|
+
assert.equal(bus.listenerCount(BRIDGE_READY_EVENT), 0, "and the subscription cleaned itself up");
|
|
212
|
+
|
|
213
|
+
assert.equal(announceBridgeReady(bus), false, "a second announcement is never emitted");
|
|
214
|
+
assert.equal(calls.length, 1, "so the handler cannot run twice for one process");
|
|
215
|
+
|
|
216
|
+
dispose();
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test("an unsubscribe before the announcement means the handler never runs", () => {
|
|
220
|
+
const bus = createFakeEvents();
|
|
221
|
+
const { calls, handler } = apiHandler();
|
|
222
|
+
|
|
223
|
+
const dispose = onBridgeApi(handler, bus);
|
|
224
|
+
dispose();
|
|
225
|
+
|
|
226
|
+
publishBridgeApi();
|
|
227
|
+
announceBridgeReady(bus);
|
|
228
|
+
|
|
229
|
+
assert.deepEqual(calls, [], "the plugin withdrew its interest and hears nothing");
|
|
230
|
+
assert.equal(bus.listenerCount(BRIDGE_READY_EVENT), 0);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test("onBridgeApi without a bus does not pretend to wait: a safe disposer, no call", () => {
|
|
234
|
+
const { calls, handler } = apiHandler();
|
|
235
|
+
|
|
236
|
+
const dispose = onBridgeApi(handler);
|
|
237
|
+
|
|
238
|
+
assert.deepEqual(calls, []);
|
|
239
|
+
assert.doesNotThrow(() => dispose());
|
|
240
|
+
assert.equal(bridgeApi(), undefined, "and nothing was published behind its back");
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("onBridgeApi ignores an announcement that is not backed by a published object", () => {
|
|
244
|
+
const bus = createFakeEvents();
|
|
245
|
+
const { calls, handler } = apiHandler();
|
|
246
|
+
onBridgeApi(handler, bus);
|
|
247
|
+
|
|
248
|
+
bus.emit(BRIDGE_READY_EVENT, { schema: "someone-else/api", version: 1 });
|
|
249
|
+
|
|
250
|
+
assert.deepEqual(calls, [], "the symbol is the authority, not the payload");
|
|
251
|
+
assert.equal(bus.listenerCount(BRIDGE_READY_EVENT), 1, "it keeps waiting for the real one");
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test("a handler that throws cannot break the announcement for the next listener", () => {
|
|
255
|
+
const bus = createFakeEvents();
|
|
256
|
+
const { calls, handler } = apiHandler();
|
|
257
|
+
const captured = captureConsole();
|
|
258
|
+
try {
|
|
259
|
+
onBridgeApi(() => {
|
|
260
|
+
throw new Error("plugin exploded");
|
|
261
|
+
}, bus);
|
|
262
|
+
onBridgeApi(handler, bus);
|
|
263
|
+
publishBridgeApi();
|
|
264
|
+
assert.doesNotThrow(() => announceBridgeReady(bus));
|
|
265
|
+
} finally {
|
|
266
|
+
captured.restore();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
assert.equal(calls.length, 1, "the second plugin was still served");
|
|
270
|
+
assert.deepEqual(captured.writes, [], "and the failure was not printed to the terminal");
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
// ---------------------------------------------------------------------------------------------
|
|
274
|
+
// Through the entry: the extension is the only place `pi` exists
|
|
275
|
+
// ---------------------------------------------------------------------------------------------
|
|
276
|
+
|
|
277
|
+
const bridgePi = (events: FakeEvents | undefined): BridgeExtensionApi => ({
|
|
278
|
+
on() {},
|
|
279
|
+
registerCommand() {},
|
|
280
|
+
...(events === undefined ? {} : { events }),
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
test("the extension entry announces through the Pi it was handed, once", () => {
|
|
284
|
+
const bus = createFakeEvents();
|
|
285
|
+
|
|
286
|
+
gentleShellBridge(bridgePi(bus));
|
|
287
|
+
gentleShellBridge(bridgePi(bus));
|
|
288
|
+
|
|
289
|
+
assert.deepEqual(
|
|
290
|
+
bus.emitted().map((entry) => entry.channel),
|
|
291
|
+
[BRIDGE_READY_EVENT],
|
|
292
|
+
"loading the package makes the process hear about it exactly once",
|
|
293
|
+
);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test("the extension entry still publishes when it is handed no event bus", () => {
|
|
297
|
+
const captured = captureConsole();
|
|
298
|
+
try {
|
|
299
|
+
assert.doesNotThrow(() => gentleShellBridge(bridgePi(undefined)));
|
|
300
|
+
} finally {
|
|
301
|
+
captured.restore();
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
assert.deepEqual(captured.writes, []);
|
|
305
|
+
const api = bridgeApi();
|
|
306
|
+
assert.ok(api !== undefined, "the symbol is the handshake, with or without a bus");
|
|
307
|
+
api.registerRows(provider("after-entry"));
|
|
308
|
+
assert.deepEqual(snapshotProviders().map((registered) => registered.id), ["after-entry"]);
|
|
309
|
+
});
|