@blackbelt-technology/pi-dashboard-extension 0.3.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 (57) hide show
  1. package/.pi/skills/pi-dashboard/SKILL.md +134 -0
  2. package/.pi/skills/pi-dashboard/references/api-reference.md +470 -0
  3. package/.pi/skills/pi-dashboard/references/recipes.md +252 -0
  4. package/.pi/skills/pi-dashboard/scripts/dashboard-api.sh +75 -0
  5. package/package.json +38 -0
  6. package/src/__tests__/ask-user-tool.test.ts +432 -0
  7. package/src/__tests__/command-handler.test.ts +712 -0
  8. package/src/__tests__/connection.test.ts +344 -0
  9. package/src/__tests__/credentials-updated.test.ts +26 -0
  10. package/src/__tests__/dashboard-default-adapter.test.ts +77 -0
  11. package/src/__tests__/dev-build.test.ts +79 -0
  12. package/src/__tests__/enrich-model-metadata.test.ts +201 -0
  13. package/src/__tests__/event-forwarder.test.ts +89 -0
  14. package/src/__tests__/fork-entryid-timing.test.ts +100 -0
  15. package/src/__tests__/git-info.test.ts +124 -0
  16. package/src/__tests__/git-link-builder.test.ts +102 -0
  17. package/src/__tests__/openspec-activity-detector.test.ts +232 -0
  18. package/src/__tests__/openspec-poller.test.ts +124 -0
  19. package/src/__tests__/process-metrics.test.ts +47 -0
  20. package/src/__tests__/process-scanner-kill.test.ts +61 -0
  21. package/src/__tests__/process-scanner.test.ts +202 -0
  22. package/src/__tests__/prompt-bus-wiring.test.ts +791 -0
  23. package/src/__tests__/prompt-bus.test.ts +469 -0
  24. package/src/__tests__/prompt-expander.test.ts +54 -0
  25. package/src/__tests__/provider-register-reload.test.ts +394 -0
  26. package/src/__tests__/server-auto-start.test.ts +258 -0
  27. package/src/__tests__/server-launcher.test.ts +61 -0
  28. package/src/__tests__/server-probe.test.ts +25 -0
  29. package/src/__tests__/session-switch.test.ts +139 -0
  30. package/src/__tests__/session-sync.test.ts +55 -0
  31. package/src/__tests__/source-detector.test.ts +73 -0
  32. package/src/__tests__/stats-extractor.test.ts +92 -0
  33. package/src/__tests__/tui-prompt-adapter.test.ts +207 -0
  34. package/src/__tests__/watchdog.test.ts +161 -0
  35. package/src/ask-user-tool.ts +352 -0
  36. package/src/bridge-context.ts +64 -0
  37. package/src/bridge.ts +1182 -0
  38. package/src/command-handler.ts +533 -0
  39. package/src/connection.ts +204 -0
  40. package/src/dashboard-default-adapter.ts +37 -0
  41. package/src/dev-build.ts +39 -0
  42. package/src/event-forwarder.ts +40 -0
  43. package/src/flow-event-wiring.ts +85 -0
  44. package/src/git-info.ts +55 -0
  45. package/src/git-link-builder.ts +112 -0
  46. package/src/model-tracker.ts +56 -0
  47. package/src/pi-env.d.ts +37 -0
  48. package/src/process-metrics.ts +70 -0
  49. package/src/process-scanner.ts +430 -0
  50. package/src/prompt-bus.ts +240 -0
  51. package/src/prompt-expander.ts +108 -0
  52. package/src/provider-register.ts +564 -0
  53. package/src/server-auto-start.ts +113 -0
  54. package/src/server-launcher.ts +126 -0
  55. package/src/server-probe.ts +33 -0
  56. package/src/session-sync.ts +155 -0
  57. package/src/source-detector.ts +26 -0
@@ -0,0 +1,344 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+ import { ConnectionManager } from "../connection.js";
3
+
4
+ // Mock WebSocket
5
+ class MockWebSocket {
6
+ static instances: MockWebSocket[] = [];
7
+ readyState = 0; // CONNECTING
8
+ onopen: (() => void) | null = null;
9
+ onclose: (() => void) | null = null;
10
+ onmessage: ((ev: { data: string }) => void) | null = null;
11
+ onerror: ((ev: unknown) => void) | null = null;
12
+ sentMessages: string[] = [];
13
+
14
+ constructor(public url: string) {
15
+ MockWebSocket.instances.push(this);
16
+ }
17
+
18
+ send(data: string) {
19
+ this.sentMessages.push(data);
20
+ }
21
+
22
+ close() {
23
+ this.readyState = 3; // CLOSED
24
+ this.onclose?.();
25
+ }
26
+
27
+ simulateOpen() {
28
+ this.readyState = 1; // OPEN
29
+ this.onopen?.();
30
+ }
31
+
32
+ simulateClose() {
33
+ this.readyState = 3;
34
+ this.onclose?.();
35
+ }
36
+
37
+ simulateMessage(data: string) {
38
+ this.onmessage?.({ data });
39
+ }
40
+ }
41
+
42
+ describe("ConnectionManager", () => {
43
+ beforeEach(() => {
44
+ MockWebSocket.instances = [];
45
+ vi.useFakeTimers();
46
+ });
47
+
48
+ it("should connect to the configured URL", () => {
49
+ const cm = new ConnectionManager({
50
+ url: "ws://localhost:9999",
51
+ WebSocketImpl: MockWebSocket as any,
52
+ });
53
+ cm.connect();
54
+
55
+ expect(MockWebSocket.instances).toHaveLength(1);
56
+ expect(MockWebSocket.instances[0].url).toBe("ws://localhost:9999");
57
+ cm.disconnect();
58
+ });
59
+
60
+ it("should send buffered messages after connecting", () => {
61
+ const cm = new ConnectionManager({
62
+ url: "ws://localhost:9999",
63
+ WebSocketImpl: MockWebSocket as any,
64
+ });
65
+
66
+ cm.send({ type: "session_heartbeat", sessionId: "s1" });
67
+ cm.connect();
68
+
69
+ const ws = MockWebSocket.instances[0];
70
+ ws.simulateOpen();
71
+
72
+ expect(ws.sentMessages).toHaveLength(1);
73
+ expect(JSON.parse(ws.sentMessages[0]).type).toBe("session_heartbeat");
74
+ cm.disconnect();
75
+ });
76
+
77
+ it("should reconnect with exponential backoff", async () => {
78
+ const cm = new ConnectionManager({
79
+ url: "ws://localhost:9999",
80
+ WebSocketImpl: MockWebSocket as any,
81
+ });
82
+ cm.connect();
83
+
84
+ const ws1 = MockWebSocket.instances[0];
85
+ ws1.simulateOpen();
86
+ ws1.simulateClose();
87
+
88
+ // First reconnect: 1s
89
+ expect(MockWebSocket.instances).toHaveLength(1);
90
+ vi.advanceTimersByTime(1000);
91
+ expect(MockWebSocket.instances).toHaveLength(2);
92
+
93
+ MockWebSocket.instances[1].simulateClose();
94
+
95
+ // Second reconnect: 2s
96
+ vi.advanceTimersByTime(1000);
97
+ expect(MockWebSocket.instances).toHaveLength(2);
98
+ vi.advanceTimersByTime(1000);
99
+ expect(MockWebSocket.instances).toHaveLength(3);
100
+
101
+ MockWebSocket.instances[2].simulateClose();
102
+
103
+ // Third reconnect: 4s
104
+ vi.advanceTimersByTime(3000);
105
+ expect(MockWebSocket.instances).toHaveLength(3);
106
+ vi.advanceTimersByTime(1000);
107
+ expect(MockWebSocket.instances).toHaveLength(4);
108
+
109
+ cm.disconnect();
110
+ });
111
+
112
+ it("should cap backoff at 30s", () => {
113
+ const cm = new ConnectionManager({
114
+ url: "ws://localhost:9999",
115
+ WebSocketImpl: MockWebSocket as any,
116
+ });
117
+ cm.connect();
118
+
119
+ // Simulate many disconnects to exceed 30s cap
120
+ for (let i = 0; i < 10; i++) {
121
+ const ws = MockWebSocket.instances[MockWebSocket.instances.length - 1];
122
+ ws.simulateOpen();
123
+ ws.simulateClose();
124
+ vi.advanceTimersByTime(30000);
125
+ }
126
+
127
+ // The backoff should never exceed 30s
128
+ // After 10 reconnects: 1, 2, 4, 8, 16, 30, 30, 30, 30, 30
129
+ cm.disconnect();
130
+ });
131
+
132
+ it("should reset backoff on successful connect", () => {
133
+ const cm = new ConnectionManager({
134
+ url: "ws://localhost:9999",
135
+ WebSocketImpl: MockWebSocket as any,
136
+ });
137
+ cm.connect();
138
+
139
+ // First connect and disconnect
140
+ MockWebSocket.instances[0].simulateOpen();
141
+ MockWebSocket.instances[0].simulateClose();
142
+
143
+ // Wait for first reconnect (1s)
144
+ vi.advanceTimersByTime(1000);
145
+ expect(MockWebSocket.instances).toHaveLength(2);
146
+
147
+ // Successful reconnect resets backoff
148
+ MockWebSocket.instances[1].simulateOpen();
149
+ MockWebSocket.instances[1].simulateClose();
150
+
151
+ // Next reconnect should be 1s again (reset)
152
+ vi.advanceTimersByTime(1000);
153
+ expect(MockWebSocket.instances).toHaveLength(3);
154
+
155
+ cm.disconnect();
156
+ });
157
+
158
+ it("should buffer up to 1000 events during disconnect", () => {
159
+ const cm = new ConnectionManager({
160
+ url: "ws://localhost:9999",
161
+ WebSocketImpl: MockWebSocket as any,
162
+ maxBufferSize: 1000,
163
+ });
164
+
165
+ // Send 1001 messages while disconnected
166
+ for (let i = 0; i < 1001; i++) {
167
+ cm.send({ type: "event_forward", sessionId: "s1", event: { i } });
168
+ }
169
+
170
+ cm.connect();
171
+ const ws = MockWebSocket.instances[0];
172
+ ws.simulateOpen();
173
+
174
+ // Should only have 1000 messages (oldest dropped)
175
+ expect(ws.sentMessages).toHaveLength(1000);
176
+ cm.disconnect();
177
+ });
178
+
179
+ it("should schedule reconnect when WebSocket constructor throws", () => {
180
+ let callCount = 0;
181
+ const ThrowingWebSocket = function (url: string) {
182
+ callCount++;
183
+ if (callCount <= 2) {
184
+ throw new Error("Connection refused");
185
+ }
186
+ return new MockWebSocket(url);
187
+ } as any;
188
+
189
+ const cm = new ConnectionManager({
190
+ url: "ws://localhost:9999",
191
+ WebSocketImpl: ThrowingWebSocket,
192
+ });
193
+
194
+ // Should not throw
195
+ expect(() => cm.connect()).not.toThrow();
196
+ expect(callCount).toBe(1);
197
+
198
+ // First retry after 1s — still throws
199
+ vi.advanceTimersByTime(1000);
200
+ expect(callCount).toBe(2);
201
+
202
+ // Second retry after 2s — succeeds
203
+ vi.advanceTimersByTime(2000);
204
+ expect(callCount).toBe(3);
205
+ expect(MockWebSocket.instances).toHaveLength(1);
206
+
207
+ cm.disconnect();
208
+ });
209
+
210
+ it("should buffer message when ws.send() throws", () => {
211
+ const cm = new ConnectionManager({
212
+ url: "ws://localhost:9999",
213
+ WebSocketImpl: MockWebSocket as any,
214
+ });
215
+ cm.connect();
216
+
217
+ const ws = MockWebSocket.instances[0];
218
+ ws.simulateOpen();
219
+
220
+ // Make send() throw
221
+ ws.send = () => { throw new Error("Connection reset"); };
222
+
223
+ // Should not throw — buffers instead
224
+ expect(() => cm.send({ type: "test" })).not.toThrow();
225
+
226
+ // Reconnect and verify the buffered message is flushed
227
+ ws.simulateClose();
228
+ vi.advanceTimersByTime(1000);
229
+ const ws2 = MockWebSocket.instances[1];
230
+ ws2.simulateOpen();
231
+
232
+ expect(ws2.sentMessages).toHaveLength(1);
233
+ expect(JSON.parse(ws2.sentMessages[0]).type).toBe("test");
234
+
235
+ cm.disconnect();
236
+ });
237
+
238
+ it("should reconnect when onerror fires without onclose (Node 22 built-in WebSocket)", () => {
239
+ const cm = new ConnectionManager({
240
+ url: "ws://localhost:9999",
241
+ WebSocketImpl: MockWebSocket as any,
242
+ });
243
+ cm.connect();
244
+
245
+ const ws1 = MockWebSocket.instances[0];
246
+ // Simulate Node 22 behavior: onerror fires but onclose does NOT
247
+ ws1.onerror?.({});
248
+ // ws1.onclose is NOT called
249
+
250
+ // Should still schedule reconnect after 1s
251
+ expect(MockWebSocket.instances).toHaveLength(1);
252
+ vi.advanceTimersByTime(1000);
253
+ expect(MockWebSocket.instances).toHaveLength(2);
254
+
255
+ // New connection should work
256
+ MockWebSocket.instances[1].simulateOpen();
257
+ expect(cm.isConnected).toBe(true);
258
+
259
+ cm.disconnect();
260
+ });
261
+
262
+ it("should not double-reconnect when both onerror and onclose fire", () => {
263
+ const cm = new ConnectionManager({
264
+ url: "ws://localhost:9999",
265
+ WebSocketImpl: MockWebSocket as any,
266
+ });
267
+ cm.connect();
268
+
269
+ const ws1 = MockWebSocket.instances[0];
270
+ ws1.simulateOpen();
271
+
272
+ // Simulate normal ws package behavior: onerror then onclose
273
+ ws1.onerror?.({});
274
+ // onerror clears onclose and handles reconnect, so onclose won't fire again
275
+
276
+ vi.advanceTimersByTime(1000);
277
+ // Should only create ONE new connection, not two
278
+ expect(MockWebSocket.instances).toHaveLength(2);
279
+
280
+ cm.disconnect();
281
+ });
282
+
283
+ it("should call onReconnect when reconnecting", () => {
284
+ const onReconnect = vi.fn();
285
+ const cm = new ConnectionManager({
286
+ url: "ws://localhost:9999",
287
+ WebSocketImpl: MockWebSocket as any,
288
+ onReconnect,
289
+ });
290
+ cm.connect();
291
+
292
+ MockWebSocket.instances[0].simulateOpen();
293
+ expect(onReconnect).not.toHaveBeenCalled();
294
+
295
+ MockWebSocket.instances[0].simulateClose();
296
+ vi.advanceTimersByTime(1000);
297
+
298
+ MockWebSocket.instances[1].simulateOpen();
299
+ expect(onReconnect).toHaveBeenCalledTimes(1);
300
+
301
+ cm.disconnect();
302
+ });
303
+
304
+ describe("updateUrl", () => {
305
+ it("triggers reconnect when URL changes", () => {
306
+ const cm = new ConnectionManager({
307
+ url: "ws://localhost:9999",
308
+ WebSocketImpl: MockWebSocket,
309
+ watchdogTimeout: 0,
310
+ });
311
+ cm.connect();
312
+ MockWebSocket.instances[0].simulateOpen();
313
+ expect(MockWebSocket.instances).toHaveLength(1);
314
+
315
+ cm.updateUrl("ws://remote:9999");
316
+
317
+ // Old connection closed, reconnect scheduled
318
+ vi.advanceTimersByTime(1000);
319
+ expect(MockWebSocket.instances.length).toBeGreaterThan(1);
320
+ const last = MockWebSocket.instances[MockWebSocket.instances.length - 1];
321
+ expect(last.url).toBe("ws://remote:9999");
322
+
323
+ cm.disconnect();
324
+ });
325
+
326
+ it("is a no-op when URL is the same", () => {
327
+ const cm = new ConnectionManager({
328
+ url: "ws://localhost:9999",
329
+ WebSocketImpl: MockWebSocket,
330
+ watchdogTimeout: 0,
331
+ });
332
+ cm.connect();
333
+ MockWebSocket.instances[0].simulateOpen();
334
+ const countBefore = MockWebSocket.instances.length;
335
+
336
+ cm.updateUrl("ws://localhost:9999");
337
+
338
+ vi.advanceTimersByTime(2000);
339
+ expect(MockWebSocket.instances.length).toBe(countBefore);
340
+
341
+ cm.disconnect();
342
+ });
343
+ });
344
+ });
@@ -0,0 +1,26 @@
1
+ import { describe, it, expect, vi } from "vitest";
2
+
3
+ describe("bridge credentials_updated handler", () => {
4
+ it("calls authStorage.reload() when modelRegistry is available", () => {
5
+ const reload = vi.fn();
6
+ const modelRegistry = { authStorage: { reload } };
7
+
8
+ // Simulate the bridge handler logic
9
+ const msg = { type: "credentials_updated" as const };
10
+ if (msg.type === "credentials_updated") {
11
+ try { modelRegistry?.authStorage?.reload?.(); } catch { /* ignore */ }
12
+ }
13
+
14
+ expect(reload).toHaveBeenCalledOnce();
15
+ });
16
+
17
+ it("does not throw when modelRegistry is null", () => {
18
+ const modelRegistry = null;
19
+ const msg = { type: "credentials_updated" as const };
20
+ expect(() => {
21
+ if (msg.type === "credentials_updated") {
22
+ try { (modelRegistry as any)?.authStorage?.reload?.(); } catch { /* ignore */ }
23
+ }
24
+ }).not.toThrow();
25
+ });
26
+ });
@@ -0,0 +1,77 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { DashboardDefaultAdapter } from "../dashboard-default-adapter.js";
3
+ import type { PromptRequest } from "../prompt-bus.js";
4
+
5
+ function makePrompt(overrides: Partial<PromptRequest> = {}): PromptRequest {
6
+ return {
7
+ id: "test-1",
8
+ pipeline: "command",
9
+ type: "select",
10
+ question: "Pick one:",
11
+ options: ["A", "B"],
12
+ ...overrides,
13
+ };
14
+ }
15
+
16
+ describe("DashboardDefaultAdapter", () => {
17
+ it("has name 'dashboard-default'", () => {
18
+ const adapter = new DashboardDefaultAdapter();
19
+ expect(adapter.name).toBe("dashboard-default");
20
+ });
21
+
22
+ it("claims all prompts with generic-dialog component", () => {
23
+ const adapter = new DashboardDefaultAdapter();
24
+ const claim = adapter.onRequest(makePrompt());
25
+
26
+ expect(claim).toEqual({
27
+ component: {
28
+ type: "generic-dialog",
29
+ props: {
30
+ question: "Pick one:",
31
+ type: "select",
32
+ options: ["A", "B"],
33
+ defaultValue: undefined,
34
+ },
35
+ },
36
+ placement: "inline",
37
+ });
38
+ });
39
+
40
+ it("claims input prompts with correct props", () => {
41
+ const adapter = new DashboardDefaultAdapter();
42
+ const claim = adapter.onRequest(makePrompt({
43
+ type: "input",
44
+ question: "Name:",
45
+ options: undefined,
46
+ defaultValue: "default",
47
+ }));
48
+
49
+ expect(claim.component!.type).toBe("generic-dialog");
50
+ expect(claim.component!.props.type).toBe("input");
51
+ expect(claim.component!.props.defaultValue).toBe("default");
52
+ });
53
+
54
+ it("claims confirm prompts", () => {
55
+ const adapter = new DashboardDefaultAdapter();
56
+ const claim = adapter.onRequest(makePrompt({ type: "confirm", question: "Sure?" }));
57
+
58
+ expect(claim.component!.type).toBe("generic-dialog");
59
+ expect(claim.component!.props.type).toBe("confirm");
60
+ });
61
+
62
+ it("placement is always inline", () => {
63
+ const adapter = new DashboardDefaultAdapter();
64
+ const claim = adapter.onRequest(makePrompt({ pipeline: "architect-new" }));
65
+ expect(claim.placement).toBe("inline");
66
+ });
67
+
68
+ it("onResponse does not throw", () => {
69
+ const adapter = new DashboardDefaultAdapter();
70
+ expect(() => adapter.onResponse({ id: "x", answer: "A", source: "tui" })).not.toThrow();
71
+ });
72
+
73
+ it("onCancel does not throw", () => {
74
+ const adapter = new DashboardDefaultAdapter();
75
+ expect(() => adapter.onCancel("x")).not.toThrow();
76
+ });
77
+ });
@@ -0,0 +1,79 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
+ import { runDevBuild } from "../dev-build.js";
3
+
4
+ describe("runDevBuild", () => {
5
+ let logSpy: ReturnType<typeof vi.spyOn>;
6
+ let mockExecSync: ReturnType<typeof vi.fn>;
7
+ let mockFetch: ReturnType<typeof vi.fn>;
8
+
9
+ beforeEach(() => {
10
+ logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
11
+ mockExecSync = vi.fn();
12
+ mockFetch = vi.fn().mockReturnValue(Promise.resolve({ ok: true }));
13
+ });
14
+
15
+ afterEach(() => {
16
+ logSpy.mockRestore();
17
+ });
18
+
19
+ function run(overrides: Partial<Parameters<typeof runDevBuild>[0]> = {}) {
20
+ runDevBuild({
21
+ packageRoot: "/my/project",
22
+ serverPort: 8000,
23
+ _execSync: mockExecSync as any,
24
+ _fetch: mockFetch as any,
25
+ ...overrides,
26
+ });
27
+ }
28
+
29
+ it("should build client and request server shutdown", () => {
30
+ run();
31
+
32
+ expect(mockExecSync).toHaveBeenCalledWith("npm run build", {
33
+ cwd: "/my/project",
34
+ stdio: "inherit",
35
+ });
36
+ expect(mockFetch).toHaveBeenCalledWith(
37
+ "http://localhost:8000/api/shutdown",
38
+ { method: "POST" },
39
+ );
40
+ });
41
+
42
+ it("should log progress messages", () => {
43
+ run();
44
+
45
+ const logs = logSpy.mock.calls.map((c: any) => c[0]);
46
+ expect(logs).toContain("🔨 Dashboard: building client...");
47
+ expect(logs).toContain("✅ Dashboard: client built");
48
+ expect(logs).toContain("🛑 Dashboard: stopping server...");
49
+ expect(logs).toContain("✅ Dashboard: server stopped");
50
+ });
51
+
52
+ it("should continue with shutdown even if build fails", () => {
53
+ mockExecSync.mockImplementation(() => { throw new Error("build error"); });
54
+
55
+ run();
56
+
57
+ const logs = logSpy.mock.calls.map((c: any) => c[0]);
58
+ expect(logs).toContain("❌ Dashboard: build failed — build error");
59
+ expect(mockFetch).toHaveBeenCalledWith(
60
+ "http://localhost:8000/api/shutdown",
61
+ { method: "POST" },
62
+ );
63
+ });
64
+
65
+ it("should not throw if fetch fails synchronously", () => {
66
+ mockFetch.mockImplementation(() => { throw new Error("connection refused"); });
67
+
68
+ expect(() => run()).not.toThrow();
69
+ });
70
+
71
+ it("should use custom serverPort", () => {
72
+ run({ serverPort: 3000 });
73
+
74
+ expect(mockFetch).toHaveBeenCalledWith(
75
+ "http://localhost:3000/api/shutdown",
76
+ { method: "POST" },
77
+ );
78
+ });
79
+ });