@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,432 @@
1
+ import { describe, it, expect, vi } from "vitest";
2
+
3
+ // Mock modules before importing
4
+ vi.mock("@sinclair/typebox", () => ({
5
+ Type: {
6
+ Object: vi.fn(() => ({})),
7
+ String: vi.fn(() => ({})),
8
+ Optional: vi.fn((x: any) => x),
9
+ Array: vi.fn(() => ({})),
10
+ Union: vi.fn(() => ({})),
11
+ Literal: vi.fn(() => ({})),
12
+ },
13
+ }));
14
+
15
+ vi.mock("@mariozechner/pi-ai", () => ({
16
+ StringEnum: vi.fn(() => ({})),
17
+ }));
18
+
19
+ import { registerAskUserTool } from "../ask-user-tool.js";
20
+
21
+ function createMockPi() {
22
+ return {
23
+ registerTool: vi.fn(),
24
+ };
25
+ }
26
+
27
+ describe("registerAskUserTool", () => {
28
+ it("registers ask_user tool", () => {
29
+ const pi = createMockPi();
30
+ registerAskUserTool(pi as any);
31
+
32
+ expect(pi.registerTool).toHaveBeenCalledTimes(1);
33
+ expect(pi.registerTool.mock.calls[0][0].name).toBe("ask_user");
34
+ });
35
+
36
+ it("registers with correct methods", () => {
37
+ const pi = createMockPi();
38
+ registerAskUserTool(pi as any);
39
+
40
+ const tool = pi.registerTool.mock.calls[0][0];
41
+ expect(tool.name).toBe("ask_user");
42
+ expect(tool.execute).toBeTypeOf("function");
43
+ expect(tool.promptGuidelines).toBeDefined();
44
+ expect(tool.promptGuidelines.length).toBeGreaterThan(0);
45
+ });
46
+
47
+ describe("message passthrough", () => {
48
+ function getToolAndMockCtx() {
49
+ const pi = createMockPi();
50
+ registerAskUserTool(pi as any);
51
+ const tool = pi.registerTool.mock.calls[0][0];
52
+ const ctx = {
53
+ ui: {
54
+ confirm: vi.fn().mockResolvedValue(true),
55
+ select: vi.fn().mockResolvedValue("A"),
56
+ input: vi.fn().mockResolvedValue("hello"),
57
+ multiselect: vi.fn().mockResolvedValue(["A"]),
58
+ },
59
+ };
60
+ return { tool, ctx };
61
+ }
62
+
63
+ it("passes message through opts for input", async () => {
64
+ const { tool, ctx } = getToolAndMockCtx();
65
+ await tool.execute("id", { method: "input", title: "Q", message: "Details here" }, undefined, undefined, ctx);
66
+ expect(ctx.ui.input).toHaveBeenCalledWith("Q", undefined, { message: "Details here" });
67
+ });
68
+
69
+ it("passes message through opts for select", async () => {
70
+ const { tool, ctx } = getToolAndMockCtx();
71
+ await tool.execute("id", { method: "select", title: "Pick", message: "Context", options: ["A", "B"] }, undefined, undefined, ctx);
72
+ expect(ctx.ui.select).toHaveBeenCalledWith("Pick", ["A", "B"], { message: "Context" });
73
+ });
74
+
75
+ it("passes message through opts for multiselect", async () => {
76
+ const { tool, ctx } = getToolAndMockCtx();
77
+ await tool.execute("id", { method: "multiselect", title: "Multi", message: "Info", options: ["A"] }, undefined, undefined, ctx);
78
+ expect(ctx.ui.multiselect).toHaveBeenCalledWith("Multi", ["A"], { message: "Info" });
79
+ });
80
+
81
+ it("does not pass opts when message is undefined", async () => {
82
+ const { tool, ctx } = getToolAndMockCtx();
83
+ await tool.execute("id", { method: "input", title: "Q" }, undefined, undefined, ctx);
84
+ expect(ctx.ui.input).toHaveBeenCalledWith("Q", undefined, undefined);
85
+ });
86
+
87
+ it("falls back to message when title is missing", async () => {
88
+ const { tool, ctx } = getToolAndMockCtx();
89
+ await tool.execute("id", { method: "input", message: "Detailed question" }, undefined, undefined, ctx);
90
+ expect(ctx.ui.input).toHaveBeenCalledWith("Detailed question", undefined, { message: "Detailed question" });
91
+ });
92
+
93
+ it("falls back to 'Question' when both title and message are missing", async () => {
94
+ const { tool, ctx } = getToolAndMockCtx();
95
+ await tool.execute("id", { method: "confirm" }, undefined, undefined, ctx);
96
+ expect(ctx.ui.confirm).toHaveBeenCalledWith("Question", "");
97
+ });
98
+
99
+ it("parses options from JSON string", async () => {
100
+ const { tool, ctx } = getToolAndMockCtx();
101
+ await tool.execute("id", { method: "select", title: "Pick", options: '["A", "B"]' }, undefined, undefined, ctx);
102
+ expect(ctx.ui.select).toHaveBeenCalledWith("Pick", ["A", "B"], undefined);
103
+ });
104
+
105
+ it("throws when select reaches execute with unparseable options string", async () => {
106
+ const { tool, ctx } = getToolAndMockCtx();
107
+ await expect(
108
+ tool.execute("id", { method: "select", title: "Pick", options: "not json" }, undefined, undefined, ctx),
109
+ ).rejects.toThrow(/options/i);
110
+ expect(ctx.ui.select).not.toHaveBeenCalled();
111
+ });
112
+
113
+ it("throws when select is invoked with empty options array", async () => {
114
+ const { tool, ctx } = getToolAndMockCtx();
115
+ await expect(
116
+ tool.execute("id", { method: "select", title: "Pick", options: [] }, undefined, undefined, ctx),
117
+ ).rejects.toThrow(/options.*input/is);
118
+ expect(ctx.ui.select).not.toHaveBeenCalled();
119
+ });
120
+
121
+ it("throws when multiselect is invoked without options", async () => {
122
+ const { tool, ctx } = getToolAndMockCtx();
123
+ await expect(
124
+ tool.execute("id", { method: "multiselect", title: "Pick" }, undefined, undefined, ctx),
125
+ ).rejects.toThrow(/options/i);
126
+ expect(ctx.ui.multiselect).not.toHaveBeenCalled();
127
+ });
128
+ });
129
+
130
+ describe("prepareArguments", () => {
131
+ function getTool() {
132
+ const pi = createMockPi();
133
+ registerAskUserTool(pi as any);
134
+ return pi.registerTool.mock.calls[0][0];
135
+ }
136
+
137
+ it("parses stringified options array", () => {
138
+ const tool = getTool();
139
+ const result = tool.prepareArguments({ method: "select", title: "Pick", options: '["A", "B"]' });
140
+ expect(result.options).toEqual(["A", "B"]);
141
+ });
142
+
143
+ it("leaves real array options unchanged", () => {
144
+ const tool = getTool();
145
+ const result = tool.prepareArguments({ method: "select", title: "Pick", options: ["A", "B"] });
146
+ expect(result.options).toEqual(["A", "B"]);
147
+ });
148
+
149
+ it("leaves malformed string as-is", () => {
150
+ const tool = getTool();
151
+ const result = tool.prepareArguments({ method: "select", title: "Pick", options: "not json" });
152
+ expect(result.options).toBe("not json");
153
+ });
154
+
155
+ it("unwraps stringified params wrapper", () => {
156
+ const tool = getTool();
157
+ const result = tool.prepareArguments({
158
+ method: "select",
159
+ params: '{"title":"X","options":["a","b"]}',
160
+ });
161
+ expect(result.method).toBe("select");
162
+ expect(result.title).toBe("X");
163
+ expect(result.options).toEqual(["a", "b"]);
164
+ expect(result.params).toBeUndefined();
165
+ });
166
+
167
+ it("unwraps object-form params wrapper", () => {
168
+ const tool = getTool();
169
+ const result = tool.prepareArguments({
170
+ method: "select",
171
+ params: { title: "X", options: ["a", "b"] },
172
+ });
173
+ expect(result.method).toBe("select");
174
+ expect(result.title).toBe("X");
175
+ expect(result.options).toEqual(["a", "b"]);
176
+ expect(result.params).toBeUndefined();
177
+ });
178
+
179
+ it("copies question into title when title is absent", () => {
180
+ const tool = getTool();
181
+ const result = tool.prepareArguments({ method: "input", question: "Your name?" });
182
+ expect(result.title).toBe("Your name?");
183
+ });
184
+
185
+ it("does not overwrite explicit title with question", () => {
186
+ const tool = getTool();
187
+ const result = tool.prepareArguments({ method: "input", title: "T", question: "Q" });
188
+ expect(result.title).toBe("T");
189
+ });
190
+
191
+ it("top-level fields win over params wrapper", () => {
192
+ const tool = getTool();
193
+ const result = tool.prepareArguments({
194
+ method: "select",
195
+ title: "OuterTitle",
196
+ params: { title: "InnerTitle", options: ["a", "b"] },
197
+ });
198
+ expect(result.title).toBe("OuterTitle");
199
+ expect(result.options).toEqual(["a", "b"]);
200
+ });
201
+
202
+ it("rescues options JSON string inside params wrapper", () => {
203
+ const tool = getTool();
204
+ const result = tool.prepareArguments({
205
+ method: "select",
206
+ params: '{"title":"X","options":"[\\"a\\",\\"b\\"]"}',
207
+ });
208
+ expect(result.options).toEqual(["a", "b"]);
209
+ });
210
+
211
+ // ── batch rescue ────────────────────────────────────────────────
212
+
213
+ it("parses stringified questions array and synthesizes method=batch", () => {
214
+ const tool = getTool();
215
+ const result = tool.prepareArguments({
216
+ questions:
217
+ '[{"title":"Pick","method":"select","options":["a","b"]}]',
218
+ });
219
+ expect(result.method).toBe("batch");
220
+ expect(Array.isArray(result.questions)).toBe(true);
221
+ expect(result.questions).toHaveLength(1);
222
+ expect(result.title).toBe("Pick");
223
+ });
224
+
225
+ it("backfills missing outer title on explicit method=batch call", () => {
226
+ const tool = getTool();
227
+ const result = tool.prepareArguments({
228
+ method: "batch",
229
+ questions: [
230
+ { method: "confirm", question: "Proceed?" },
231
+ { method: "select", question: "Scope?", options: ["A", "B"] },
232
+ ],
233
+ });
234
+ expect(result.title).toBe("Proceed?");
235
+ expect(result.questions[0].title).toBe("Proceed?"); // sub-question rename also fired
236
+ expect(result.questions[1].title).toBe("Scope?");
237
+ });
238
+
239
+ it("bare questions array with no method synthesizes method=batch and pulls title", () => {
240
+ const tool = getTool();
241
+ const result = tool.prepareArguments({
242
+ questions: [{ method: "confirm", title: "Proceed?" }],
243
+ });
244
+ expect(result.method).toBe("batch");
245
+ expect(result.title).toBe("Proceed?");
246
+ });
247
+
248
+ it("pulls title from question or header if sub-question lacks title", () => {
249
+ const tool = getTool();
250
+ const result = tool.prepareArguments({
251
+ questions: [{ method: "input", question: "Your name?" }],
252
+ });
253
+ expect(result.method).toBe("batch");
254
+ expect(result.title).toBe("Your name?");
255
+ });
256
+
257
+ it("flattens input_type wrapper inside a sub-question", () => {
258
+ const tool = getTool();
259
+ const result = tool.prepareArguments({
260
+ method: "batch",
261
+ title: "T",
262
+ questions: [
263
+ {
264
+ title: "Pick",
265
+ input_type: { method: "select", options: ["a", "b"] },
266
+ },
267
+ ],
268
+ });
269
+ const sq = result.questions[0];
270
+ expect(sq.method).toBe("select");
271
+ expect(sq.options).toEqual(["a", "b"]);
272
+ expect(sq.input_type).toBeUndefined();
273
+ });
274
+
275
+ it("converts {label, value} options to labels and records a warning", () => {
276
+ const tool = getTool();
277
+ const result = tool.prepareArguments({
278
+ method: "batch",
279
+ title: "T",
280
+ questions: [
281
+ {
282
+ method: "select",
283
+ title: "Pick",
284
+ options: [
285
+ { label: "Sync now", value: "sync" },
286
+ { label: "Skip", value: "skip" },
287
+ ],
288
+ },
289
+ ],
290
+ });
291
+ expect(result.questions[0].options).toEqual(["Sync now", "Skip"]);
292
+ const warnings = (result as any).__normalizations as string[];
293
+ expect(warnings).toBeDefined();
294
+ expect(warnings.length).toBeGreaterThan(0);
295
+ expect(warnings[0]).toMatch(/label.*value/);
296
+ });
297
+
298
+ it("renames sub-question header to title", () => {
299
+ const tool = getTool();
300
+ const result = tool.prepareArguments({
301
+ method: "batch",
302
+ title: "T",
303
+ questions: [{ method: "input", header: "Enter name" }],
304
+ });
305
+ expect(result.questions[0].title).toBe("Enter name");
306
+ });
307
+ });
308
+
309
+ describe("batch execution", () => {
310
+ function getToolAndMockCtx() {
311
+ const pi = createMockPi();
312
+ registerAskUserTool(pi as any);
313
+ const tool = pi.registerTool.mock.calls[0][0];
314
+ const ctx = {
315
+ ui: {
316
+ confirm: vi.fn().mockResolvedValue(true),
317
+ select: vi.fn().mockResolvedValue("A"),
318
+ input: vi.fn().mockResolvedValue("hello"),
319
+ multiselect: vi.fn().mockResolvedValue(["A"]),
320
+ },
321
+ };
322
+ return { tool, ctx };
323
+ }
324
+
325
+ it("invokes ctx.ui primitives sequentially for each sub-question", async () => {
326
+ const { tool, ctx } = getToolAndMockCtx();
327
+ const result = await tool.execute(
328
+ "id",
329
+ {
330
+ method: "batch",
331
+ title: "Setup",
332
+ questions: [
333
+ { method: "input", title: "Name?" },
334
+ { method: "select", title: "Lang?", options: ["TS", "Py"] },
335
+ { method: "confirm", title: "Init git?" },
336
+ ],
337
+ },
338
+ undefined,
339
+ undefined,
340
+ ctx,
341
+ );
342
+ expect(ctx.ui.input).toHaveBeenCalledTimes(1);
343
+ expect(ctx.ui.select).toHaveBeenCalledTimes(1);
344
+ expect(ctx.ui.confirm).toHaveBeenCalledTimes(1);
345
+ expect(result.details.method).toBe("batch");
346
+ expect(result.details.results).toEqual(["hello", "A", true]);
347
+ expect(result.details.cancelled).toBe(false);
348
+ });
349
+
350
+ it("prepends batch title to sub-question titles", async () => {
351
+ const { tool, ctx } = getToolAndMockCtx();
352
+ await tool.execute(
353
+ "id",
354
+ {
355
+ method: "batch",
356
+ title: "Setup",
357
+ questions: [{ method: "input", title: "Name?" }],
358
+ },
359
+ undefined,
360
+ undefined,
361
+ ctx,
362
+ );
363
+ const firstCallTitle = ctx.ui.input.mock.calls[0][0];
364
+ expect(firstCallTitle).toContain("Setup");
365
+ expect(firstCallTitle).toContain("Name?");
366
+ });
367
+
368
+ it("stops on cancellation and returns partial results with cancelled=true", async () => {
369
+ const { tool, ctx } = getToolAndMockCtx();
370
+ // First sub-question returns a value; second cancels (undefined); third should not be called.
371
+ ctx.ui.input.mockResolvedValueOnce("first");
372
+ ctx.ui.select.mockResolvedValueOnce(undefined); // cancel
373
+ const result = await tool.execute(
374
+ "id",
375
+ {
376
+ method: "batch",
377
+ title: "T",
378
+ questions: [
379
+ { method: "input", title: "Q1" },
380
+ { method: "select", title: "Q2", options: ["a", "b"] },
381
+ { method: "confirm", title: "Q3" },
382
+ ],
383
+ },
384
+ undefined,
385
+ undefined,
386
+ ctx,
387
+ );
388
+ expect(result.details.cancelled).toBe(true);
389
+ expect(result.details.results).toEqual(["first", null]);
390
+ expect(ctx.ui.confirm).not.toHaveBeenCalled();
391
+ });
392
+
393
+ it("surfaces __normalizations warnings in details.warnings", async () => {
394
+ const { tool, ctx } = getToolAndMockCtx();
395
+ const prepared = tool.prepareArguments({
396
+ method: "batch",
397
+ title: "T",
398
+ questions: [
399
+ {
400
+ method: "select",
401
+ title: "Pick",
402
+ options: [
403
+ { label: "A", value: "a" },
404
+ { label: "B", value: "b" },
405
+ ],
406
+ },
407
+ ],
408
+ });
409
+ const result = await tool.execute("id", prepared, undefined, undefined, ctx);
410
+ expect(result.details.warnings).toBeDefined();
411
+ expect(result.details.warnings.length).toBeGreaterThan(0);
412
+ expect(result.details.warnings[0]).toMatch(/label.*value/);
413
+ });
414
+
415
+ it("throws if a batch sub-question is select with empty options", async () => {
416
+ const { tool, ctx } = getToolAndMockCtx();
417
+ await expect(
418
+ tool.execute(
419
+ "id",
420
+ {
421
+ method: "batch",
422
+ title: "T",
423
+ questions: [{ method: "select", title: "Pick", options: [] }],
424
+ },
425
+ undefined,
426
+ undefined,
427
+ ctx,
428
+ ),
429
+ ).rejects.toThrow(/options/i);
430
+ });
431
+ });
432
+ });