@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.
- package/.pi/skills/pi-dashboard/SKILL.md +134 -0
- package/.pi/skills/pi-dashboard/references/api-reference.md +470 -0
- package/.pi/skills/pi-dashboard/references/recipes.md +252 -0
- package/.pi/skills/pi-dashboard/scripts/dashboard-api.sh +75 -0
- package/package.json +38 -0
- package/src/__tests__/ask-user-tool.test.ts +432 -0
- package/src/__tests__/command-handler.test.ts +712 -0
- package/src/__tests__/connection.test.ts +344 -0
- package/src/__tests__/credentials-updated.test.ts +26 -0
- package/src/__tests__/dashboard-default-adapter.test.ts +77 -0
- package/src/__tests__/dev-build.test.ts +79 -0
- package/src/__tests__/enrich-model-metadata.test.ts +201 -0
- package/src/__tests__/event-forwarder.test.ts +89 -0
- package/src/__tests__/fork-entryid-timing.test.ts +100 -0
- package/src/__tests__/git-info.test.ts +124 -0
- package/src/__tests__/git-link-builder.test.ts +102 -0
- package/src/__tests__/openspec-activity-detector.test.ts +232 -0
- package/src/__tests__/openspec-poller.test.ts +124 -0
- package/src/__tests__/process-metrics.test.ts +47 -0
- package/src/__tests__/process-scanner-kill.test.ts +61 -0
- package/src/__tests__/process-scanner.test.ts +202 -0
- package/src/__tests__/prompt-bus-wiring.test.ts +791 -0
- package/src/__tests__/prompt-bus.test.ts +469 -0
- package/src/__tests__/prompt-expander.test.ts +54 -0
- package/src/__tests__/provider-register-reload.test.ts +394 -0
- package/src/__tests__/server-auto-start.test.ts +258 -0
- package/src/__tests__/server-launcher.test.ts +61 -0
- package/src/__tests__/server-probe.test.ts +25 -0
- package/src/__tests__/session-switch.test.ts +139 -0
- package/src/__tests__/session-sync.test.ts +55 -0
- package/src/__tests__/source-detector.test.ts +73 -0
- package/src/__tests__/stats-extractor.test.ts +92 -0
- package/src/__tests__/tui-prompt-adapter.test.ts +207 -0
- package/src/__tests__/watchdog.test.ts +161 -0
- package/src/ask-user-tool.ts +352 -0
- package/src/bridge-context.ts +64 -0
- package/src/bridge.ts +1182 -0
- package/src/command-handler.ts +533 -0
- package/src/connection.ts +204 -0
- package/src/dashboard-default-adapter.ts +37 -0
- package/src/dev-build.ts +39 -0
- package/src/event-forwarder.ts +40 -0
- package/src/flow-event-wiring.ts +85 -0
- package/src/git-info.ts +55 -0
- package/src/git-link-builder.ts +112 -0
- package/src/model-tracker.ts +56 -0
- package/src/pi-env.d.ts +37 -0
- package/src/process-metrics.ts +70 -0
- package/src/process-scanner.ts +430 -0
- package/src/prompt-bus.ts +240 -0
- package/src/prompt-expander.ts +108 -0
- package/src/provider-register.ts +564 -0
- package/src/server-auto-start.ts +113 -0
- package/src/server-launcher.ts +126 -0
- package/src/server-probe.ts +33 -0
- package/src/session-sync.ts +155 -0
- package/src/source-detector.ts +26 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { detectOpenSpecActivity } from "@blackbelt-technology/pi-dashboard-shared/openspec-activity-detector.js";
|
|
3
|
+
|
|
4
|
+
describe("detectOpenSpecActivity", () => {
|
|
5
|
+
describe("phase detection from skill file reads", () => {
|
|
6
|
+
it("detects apply phase from SKILL.md read", () => {
|
|
7
|
+
const result = detectOpenSpecActivity("read", {
|
|
8
|
+
path: ".pi/skills/openspec-apply-change/SKILL.md",
|
|
9
|
+
});
|
|
10
|
+
expect(result).toEqual({ phase: "apply" });
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("detects explore phase", () => {
|
|
14
|
+
const result = detectOpenSpecActivity("read", {
|
|
15
|
+
path: ".pi/skills/openspec-explore/SKILL.md",
|
|
16
|
+
});
|
|
17
|
+
expect(result).toEqual({ phase: "explore" });
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("detects new phase", () => {
|
|
21
|
+
const result = detectOpenSpecActivity("read", {
|
|
22
|
+
path: ".pi/skills/openspec-new-change/SKILL.md",
|
|
23
|
+
});
|
|
24
|
+
expect(result).toEqual({ phase: "new" });
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("detects continue phase", () => {
|
|
28
|
+
const result = detectOpenSpecActivity("read", {
|
|
29
|
+
path: ".pi/skills/openspec-continue-change/SKILL.md",
|
|
30
|
+
});
|
|
31
|
+
expect(result).toEqual({ phase: "continue" });
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("detects ff phase", () => {
|
|
35
|
+
const result = detectOpenSpecActivity("read", {
|
|
36
|
+
path: ".pi/skills/openspec-ff-change/SKILL.md",
|
|
37
|
+
});
|
|
38
|
+
expect(result).toEqual({ phase: "ff" });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("detects verify phase", () => {
|
|
42
|
+
const result = detectOpenSpecActivity("read", {
|
|
43
|
+
path: ".pi/skills/openspec-verify-change/SKILL.md",
|
|
44
|
+
});
|
|
45
|
+
expect(result).toEqual({ phase: "verify" });
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("detects archive phase", () => {
|
|
49
|
+
const result = detectOpenSpecActivity("read", {
|
|
50
|
+
path: ".pi/skills/openspec-archive-change/SKILL.md",
|
|
51
|
+
});
|
|
52
|
+
expect(result).toEqual({ phase: "archive" });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("detects sync-specs phase", () => {
|
|
56
|
+
const result = detectOpenSpecActivity("read", {
|
|
57
|
+
path: ".pi/skills/openspec-sync-specs/SKILL.md",
|
|
58
|
+
});
|
|
59
|
+
expect(result).toEqual({ phase: "sync-specs" });
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("detects onboard phase", () => {
|
|
63
|
+
const result = detectOpenSpecActivity("read", {
|
|
64
|
+
path: ".pi/skills/openspec-onboard/SKILL.md",
|
|
65
|
+
});
|
|
66
|
+
expect(result).toEqual({ phase: "onboard" });
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("handles absolute paths", () => {
|
|
70
|
+
const result = detectOpenSpecActivity("read", {
|
|
71
|
+
path: "/Users/dev/project/.pi/skills/openspec-apply-change/SKILL.md",
|
|
72
|
+
});
|
|
73
|
+
expect(result).toEqual({ phase: "apply" });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("returns null for non-openspec skill reads", () => {
|
|
77
|
+
const result = detectOpenSpecActivity("read", {
|
|
78
|
+
path: ".pi/skills/some-other-skill/SKILL.md",
|
|
79
|
+
});
|
|
80
|
+
expect(result).toBeNull();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("returns null for non-SKILL.md reads in openspec dirs", () => {
|
|
84
|
+
const result = detectOpenSpecActivity("read", {
|
|
85
|
+
path: ".pi/skills/openspec-apply-change/README.md",
|
|
86
|
+
});
|
|
87
|
+
expect(result).toBeNull();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe("change name detection from CLI calls", () => {
|
|
92
|
+
it("detects change name from openspec status command", () => {
|
|
93
|
+
const result = detectOpenSpecActivity("bash", {
|
|
94
|
+
command: 'openspec status --change "session-sync" --json',
|
|
95
|
+
});
|
|
96
|
+
expect(result).toEqual({ changeName: "session-sync", isActive: true });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("detects change name from openspec instructions command", () => {
|
|
100
|
+
const result = detectOpenSpecActivity("bash", {
|
|
101
|
+
command: 'openspec instructions apply --change "my-feature" --json',
|
|
102
|
+
});
|
|
103
|
+
expect(result).toEqual({ changeName: "my-feature", isActive: true });
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("detects change name without quotes", () => {
|
|
107
|
+
const result = detectOpenSpecActivity("bash", {
|
|
108
|
+
command: "openspec status --change session-sync --json",
|
|
109
|
+
});
|
|
110
|
+
expect(result).toEqual({ changeName: "session-sync", isActive: true });
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("detects change name from openspec archive command", () => {
|
|
114
|
+
const result = detectOpenSpecActivity("bash", {
|
|
115
|
+
command: "openspec archive session-sync",
|
|
116
|
+
});
|
|
117
|
+
expect(result).toEqual({ changeName: "session-sync", isActive: true });
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("returns null for non-openspec bash commands", () => {
|
|
121
|
+
const result = detectOpenSpecActivity("bash", {
|
|
122
|
+
command: "npm test",
|
|
123
|
+
});
|
|
124
|
+
expect(result).toBeNull();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("detects change name from openspec new change with quoted name", () => {
|
|
128
|
+
const result = detectOpenSpecActivity("bash", {
|
|
129
|
+
command: 'openspec new change "add-auth"',
|
|
130
|
+
});
|
|
131
|
+
expect(result).toEqual({ changeName: "add-auth", isActive: true });
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("detects change name from openspec new change with unquoted name", () => {
|
|
135
|
+
const result = detectOpenSpecActivity("bash", {
|
|
136
|
+
command: "openspec new change add-auth",
|
|
137
|
+
});
|
|
138
|
+
expect(result).toEqual({ changeName: "add-auth", isActive: true });
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("detects change name from openspec new change with cd prefix", () => {
|
|
142
|
+
const result = detectOpenSpecActivity("bash", {
|
|
143
|
+
command: 'cd /Users/dev/project && openspec new change "my-feature"',
|
|
144
|
+
});
|
|
145
|
+
expect(result).toEqual({ changeName: "my-feature", isActive: true });
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("returns null for openspec list (no change name)", () => {
|
|
149
|
+
const result = detectOpenSpecActivity("bash", {
|
|
150
|
+
command: "openspec list --json",
|
|
151
|
+
});
|
|
152
|
+
expect(result).toBeNull();
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
describe("change name detection from file reads", () => {
|
|
157
|
+
it("detects change name from openspec change file read as passive", () => {
|
|
158
|
+
const result = detectOpenSpecActivity("read", {
|
|
159
|
+
path: "openspec/changes/session-sync/tasks.md",
|
|
160
|
+
});
|
|
161
|
+
expect(result).toEqual({ changeName: "session-sync", isActive: false });
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("detects change name from absolute path as passive", () => {
|
|
165
|
+
const result = detectOpenSpecActivity("read", {
|
|
166
|
+
path: "/Users/dev/project/openspec/changes/my-feature/proposal.md",
|
|
167
|
+
});
|
|
168
|
+
expect(result).toEqual({ changeName: "my-feature", isActive: false });
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("returns null for non-openspec file reads", () => {
|
|
172
|
+
const result = detectOpenSpecActivity("read", {
|
|
173
|
+
path: "src/server/server.ts",
|
|
174
|
+
});
|
|
175
|
+
expect(result).toBeNull();
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe("change name detection from file writes", () => {
|
|
180
|
+
it("detects change name from openspec change file write as active", () => {
|
|
181
|
+
const result = detectOpenSpecActivity("write", {
|
|
182
|
+
path: "openspec/changes/session-sync/proposal.md",
|
|
183
|
+
});
|
|
184
|
+
expect(result).toEqual({ changeName: "session-sync", isActive: true });
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("detects change name from absolute path write as active", () => {
|
|
188
|
+
const result = detectOpenSpecActivity("write", {
|
|
189
|
+
path: "/Users/dev/project/openspec/changes/my-feature/spec.md",
|
|
190
|
+
});
|
|
191
|
+
expect(result).toEqual({ changeName: "my-feature", isActive: true });
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("returns null for non-openspec file writes", () => {
|
|
195
|
+
const result = detectOpenSpecActivity("write", {
|
|
196
|
+
path: "src/server/server.ts",
|
|
197
|
+
});
|
|
198
|
+
expect(result).toBeNull();
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
describe("edge cases", () => {
|
|
203
|
+
it("handles capitalized tool names (backward compatibility)", () => {
|
|
204
|
+
expect(detectOpenSpecActivity("Read", {
|
|
205
|
+
path: "openspec/changes/my-feature/proposal.md",
|
|
206
|
+
})).toEqual({ changeName: "my-feature", isActive: false });
|
|
207
|
+
|
|
208
|
+
expect(detectOpenSpecActivity("Bash", {
|
|
209
|
+
command: 'openspec status --change "add-auth" --json',
|
|
210
|
+
})).toEqual({ changeName: "add-auth", isActive: true });
|
|
211
|
+
|
|
212
|
+
expect(detectOpenSpecActivity("Write", {
|
|
213
|
+
path: "openspec/changes/my-feature/design.md",
|
|
214
|
+
})).toEqual({ changeName: "my-feature", isActive: true });
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("returns null for unknown tool names", () => {
|
|
218
|
+
const result = detectOpenSpecActivity("unknown", { path: "foo.ts" });
|
|
219
|
+
expect(result).toBeNull();
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("returns null when args are missing", () => {
|
|
223
|
+
const result = detectOpenSpecActivity("read", undefined);
|
|
224
|
+
expect(result).toBeNull();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("returns null when args are empty", () => {
|
|
228
|
+
const result = detectOpenSpecActivity("read", {});
|
|
229
|
+
expect(result).toBeNull();
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for openspec-poller.ts — the higher-level aggregator that combines
|
|
3
|
+
* `openspec list` + per-change `openspec status` into the dashboard's
|
|
4
|
+
* `OpenSpecData` shape.
|
|
5
|
+
*
|
|
6
|
+
* The file now delegates to `platform/openspec.ts` for the subprocess work.
|
|
7
|
+
* We mock that module so the tests focus on the aggregation logic
|
|
8
|
+
* (empty results, artifact mapping, per-change status failures) without
|
|
9
|
+
* spawning openspec.
|
|
10
|
+
*
|
|
11
|
+
* See change: platform-command-executor.
|
|
12
|
+
*/
|
|
13
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
14
|
+
|
|
15
|
+
const { listOr, statusOr } = vi.hoisted(() => ({
|
|
16
|
+
listOr: vi.fn(),
|
|
17
|
+
statusOr: vi.fn(),
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
vi.mock("@blackbelt-technology/pi-dashboard-shared/platform/openspec.js", () => ({
|
|
21
|
+
listOr,
|
|
22
|
+
statusOr,
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
import { pollOpenSpec } from "@blackbelt-technology/pi-dashboard-shared/openspec-poller.js";
|
|
26
|
+
|
|
27
|
+
describe("pollOpenSpec", () => {
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
listOr.mockReset();
|
|
30
|
+
statusOr.mockReset();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("returns initialized=false when list fails", () => {
|
|
34
|
+
listOr.mockReturnValue(null);
|
|
35
|
+
expect(pollOpenSpec("/test")).toEqual({ initialized: false, changes: [] });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("returns initialized=false when list returns non-array changes", () => {
|
|
39
|
+
listOr.mockReturnValue({ changes: "not an array" });
|
|
40
|
+
expect(pollOpenSpec("/test")).toEqual({ initialized: false, changes: [] });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("returns initialized=true with changes on success", () => {
|
|
44
|
+
listOr.mockReturnValue({
|
|
45
|
+
changes: [
|
|
46
|
+
{ name: "add-auth", status: "in-progress", completedTasks: 3, totalTasks: 10 },
|
|
47
|
+
],
|
|
48
|
+
});
|
|
49
|
+
statusOr.mockReturnValue({
|
|
50
|
+
artifacts: [
|
|
51
|
+
{ id: "proposal", status: "done" },
|
|
52
|
+
{ id: "tasks", status: "ready" },
|
|
53
|
+
],
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const result = pollOpenSpec("/test");
|
|
57
|
+
expect(result.initialized).toBe(true);
|
|
58
|
+
expect(result.changes).toHaveLength(1);
|
|
59
|
+
expect(result.changes[0]).toMatchObject({
|
|
60
|
+
name: "add-auth",
|
|
61
|
+
status: "in-progress",
|
|
62
|
+
completedTasks: 3,
|
|
63
|
+
totalTasks: 10,
|
|
64
|
+
});
|
|
65
|
+
expect(result.changes[0].artifacts).toEqual([
|
|
66
|
+
{ id: "proposal", status: "done" },
|
|
67
|
+
{ id: "tasks", status: "ready" },
|
|
68
|
+
]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("handles status call failure gracefully (empty artifacts)", () => {
|
|
72
|
+
listOr.mockReturnValue({
|
|
73
|
+
changes: [
|
|
74
|
+
{ name: "x", status: "complete", completedTasks: 5, totalTasks: 5 },
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
statusOr.mockReturnValue(null); // status failed
|
|
78
|
+
|
|
79
|
+
const result = pollOpenSpec("/test");
|
|
80
|
+
expect(result.initialized).toBe(true);
|
|
81
|
+
expect(result.changes[0].artifacts).toEqual([]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("normalizes unknown status values to 'no-tasks'", () => {
|
|
85
|
+
listOr.mockReturnValue({
|
|
86
|
+
changes: [
|
|
87
|
+
{ name: "x", status: "weird-future-status", completedTasks: 0, totalTasks: 0 },
|
|
88
|
+
],
|
|
89
|
+
});
|
|
90
|
+
statusOr.mockReturnValue(null);
|
|
91
|
+
|
|
92
|
+
const result = pollOpenSpec("/test");
|
|
93
|
+
expect(result.changes[0].status).toBe("no-tasks");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("normalizes unknown artifact statuses to 'blocked'", () => {
|
|
97
|
+
listOr.mockReturnValue({
|
|
98
|
+
changes: [
|
|
99
|
+
{ name: "x", status: "in-progress", completedTasks: 0, totalTasks: 1 },
|
|
100
|
+
],
|
|
101
|
+
});
|
|
102
|
+
statusOr.mockReturnValue({
|
|
103
|
+
artifacts: [{ id: "proposal", status: "some-new-state" }],
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const result = pollOpenSpec("/test");
|
|
107
|
+
expect(result.changes[0].artifacts[0].status).toBe("blocked");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("calls statusOr once per change, with the change name", () => {
|
|
111
|
+
listOr.mockReturnValue({
|
|
112
|
+
changes: [
|
|
113
|
+
{ name: "a", status: "in-progress", completedTasks: 0, totalTasks: 1 },
|
|
114
|
+
{ name: "b", status: "complete", completedTasks: 2, totalTasks: 2 },
|
|
115
|
+
],
|
|
116
|
+
});
|
|
117
|
+
statusOr.mockReturnValue({ artifacts: [] });
|
|
118
|
+
|
|
119
|
+
pollOpenSpec("/test");
|
|
120
|
+
expect(statusOr).toHaveBeenCalledTimes(2);
|
|
121
|
+
expect(statusOr).toHaveBeenNthCalledWith(1, { cwd: "/test", change: "a" });
|
|
122
|
+
expect(statusOr).toHaveBeenNthCalledWith(2, { cwd: "/test", change: "b" });
|
|
123
|
+
});
|
|
124
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach } from "vitest";
|
|
2
|
+
import { startMetricsMonitor, stopMetricsMonitor, collectMetrics } from "../process-metrics.js";
|
|
3
|
+
|
|
4
|
+
describe("process-metrics", () => {
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
stopMetricsMonitor();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("collectMetrics returns valid shape without monitor", () => {
|
|
10
|
+
const m = collectMetrics();
|
|
11
|
+
expect(m.rss).toBeGreaterThan(0);
|
|
12
|
+
expect(m.heapUsed).toBeGreaterThan(0);
|
|
13
|
+
expect(m.heapTotal).toBeGreaterThan(0);
|
|
14
|
+
expect(typeof m.cpuPercent).toBe("number");
|
|
15
|
+
expect(typeof m.loadAvg1m).toBe("number");
|
|
16
|
+
// eventLoopMaxMs is undefined without monitor
|
|
17
|
+
expect(m.eventLoopMaxMs).toBeUndefined();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("collectMetrics includes eventLoopMaxMs with monitor", async () => {
|
|
21
|
+
startMetricsMonitor();
|
|
22
|
+
// Let the event loop tick to capture some delay
|
|
23
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
24
|
+
const m = collectMetrics();
|
|
25
|
+
expect(typeof m.eventLoopMaxMs).toBe("number");
|
|
26
|
+
expect(m.eventLoopMaxMs).toBeGreaterThanOrEqual(0);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("cpuPercent computes delta on second call", () => {
|
|
30
|
+
const first = collectMetrics();
|
|
31
|
+
// Do some CPU work
|
|
32
|
+
let x = 0;
|
|
33
|
+
for (let i = 0; i < 1_000_000; i++) x += Math.sqrt(i);
|
|
34
|
+
const second = collectMetrics();
|
|
35
|
+
// Both should be numbers, second should show some cpu
|
|
36
|
+
expect(typeof first.cpuPercent).toBe("number");
|
|
37
|
+
expect(typeof second.cpuPercent).toBe("number");
|
|
38
|
+
void x;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("startMetricsMonitor is idempotent", () => {
|
|
42
|
+
startMetricsMonitor();
|
|
43
|
+
startMetricsMonitor(); // should not throw
|
|
44
|
+
const m = collectMetrics();
|
|
45
|
+
expect(m.rss).toBeGreaterThan(0);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests that `killProcessByPgid` routes through the platform's
|
|
3
|
+
* `killPidWithGroup` helper (not raw `process.kill`).
|
|
4
|
+
*
|
|
5
|
+
* See change: route-kill-paths-through-platform.
|
|
6
|
+
*/
|
|
7
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
8
|
+
|
|
9
|
+
const killPidWithGroupSpy = vi.fn((_pid: number, _sig: any, _opts?: any) => undefined);
|
|
10
|
+
|
|
11
|
+
vi.mock("@blackbelt-technology/pi-dashboard-shared/platform/process.js", async () => {
|
|
12
|
+
const actual = await vi.importActual<typeof import("@blackbelt-technology/pi-dashboard-shared/platform/process.js")>(
|
|
13
|
+
"@blackbelt-technology/pi-dashboard-shared/platform/process.js",
|
|
14
|
+
);
|
|
15
|
+
return {
|
|
16
|
+
...actual,
|
|
17
|
+
killPidWithGroup: (pid: number, sig: any, opts?: any) => killPidWithGroupSpy(pid, sig, opts),
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const { killProcessByPgid } = await import("../process-scanner.js");
|
|
22
|
+
|
|
23
|
+
describe("killProcessByPgid platform routing", () => {
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
killPidWithGroupSpy.mockClear();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("invokes killPidWithGroup with the resolved platform on Unix", () => {
|
|
29
|
+
const ok = killProcessByPgid(4242, { _platform: "linux" } as any);
|
|
30
|
+
expect(ok).toBe(true);
|
|
31
|
+
expect(killPidWithGroupSpy).toHaveBeenCalledTimes(1);
|
|
32
|
+
const [pid, sig, opts] = killPidWithGroupSpy.mock.calls[0];
|
|
33
|
+
expect(pid).toBe(4242);
|
|
34
|
+
expect(sig).toBe("SIGTERM");
|
|
35
|
+
expect(opts?.platform).toBe("linux");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("invokes killPidWithGroup with platform=darwin for macOS pgids", () => {
|
|
39
|
+
killProcessByPgid(9999, { _platform: "darwin" } as any);
|
|
40
|
+
const [pid, , opts] = killPidWithGroupSpy.mock.calls[0];
|
|
41
|
+
expect(pid).toBe(9999);
|
|
42
|
+
expect(opts?.platform).toBe("darwin");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("does NOT call process.kill directly on Unix", () => {
|
|
46
|
+
const processKillSpy = vi.spyOn(process, "kill");
|
|
47
|
+
try {
|
|
48
|
+
killProcessByPgid(1234, { _platform: "linux" } as any);
|
|
49
|
+
} catch { /* ignore */ }
|
|
50
|
+
expect(processKillSpy).not.toHaveBeenCalled();
|
|
51
|
+
processKillSpy.mockRestore();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("reports failure if killPidWithGroup throws", () => {
|
|
55
|
+
killPidWithGroupSpy.mockImplementationOnce(() => {
|
|
56
|
+
throw new Error("ESRCH");
|
|
57
|
+
});
|
|
58
|
+
const ok = killProcessByPgid(4242, { _platform: "linux" } as any);
|
|
59
|
+
expect(ok).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import { parseEtime, scanChildProcesses, captureChildPgids, scanTrackedProcesses, killProcessByPgid, scanWindowsProcesses, type SpawnSyncFn } from "../process-scanner.js";
|
|
3
|
+
import type { SpawnSyncReturns } from "node:child_process";
|
|
4
|
+
|
|
5
|
+
function mockResult(stdout: string, status = 0): SpawnSyncReturns<string> {
|
|
6
|
+
return { status, stdout, stderr: "", pid: 0, output: [], signal: null };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function fail(): SpawnSyncReturns<string> {
|
|
10
|
+
return mockResult("", 1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Helper: ps -eo pid=,ppid= output for a set of pid→ppid pairs */
|
|
14
|
+
function psChildOutput(pairs: [number, number][]): string {
|
|
15
|
+
return pairs.map(([pid, ppid]) => ` ${pid} ${ppid}`).join("\n") + "\n";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe("parseEtime", () => {
|
|
19
|
+
it("parses mm:ss format", () => expect(parseEtime("02:15")).toBe(135000));
|
|
20
|
+
it("parses hh:mm:ss format", () => expect(parseEtime("01:30:00")).toBe(5400000));
|
|
21
|
+
it("parses dd-hh:mm:ss format", () => expect(parseEtime("2-03:00:00")).toBe(183600000));
|
|
22
|
+
it("parses 00:05 as 5 seconds", () => expect(parseEtime("00:05")).toBe(5000));
|
|
23
|
+
it("parses 1-00:00:00 as 1 day", () => expect(parseEtime("1-00:00:00")).toBe(86400000));
|
|
24
|
+
it("returns 0 for empty string", () => expect(parseEtime("")).toBe(0));
|
|
25
|
+
it("returns 0 for invalid format", () => expect(parseEtime("garbage")).toBe(0));
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("captureChildPgids", () => {
|
|
29
|
+
it("captures PGIDs of leaf children", () => {
|
|
30
|
+
const mock: SpawnSyncFn = (cmd, args) => {
|
|
31
|
+
// getChildPids for parent 100: finds child 200
|
|
32
|
+
if (cmd === "ps" && args[0] === "-eo" && args[1] === "pid=,ppid=") {
|
|
33
|
+
return mockResult(psChildOutput([[200, 100], [300, 999]]));
|
|
34
|
+
}
|
|
35
|
+
// getChildPids for child 200: no grandchildren (same ps call, no match)
|
|
36
|
+
// ps -p 200 -o pgid=
|
|
37
|
+
if (cmd === "ps" && args[0] === "-p" && args[1] === "200") {
|
|
38
|
+
return mockResult(" 200\n");
|
|
39
|
+
}
|
|
40
|
+
return fail();
|
|
41
|
+
};
|
|
42
|
+
const tracked = new Set<number>();
|
|
43
|
+
captureChildPgids(100, tracked, { _spawnSync: mock });
|
|
44
|
+
expect(tracked.has(200)).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("does nothing on Windows", () => {
|
|
48
|
+
const origPlatform = Object.getOwnPropertyDescriptor(process, "platform");
|
|
49
|
+
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
|
|
50
|
+
try {
|
|
51
|
+
const tracked = new Set<number>();
|
|
52
|
+
captureChildPgids(100, tracked);
|
|
53
|
+
expect(tracked.size).toBe(0);
|
|
54
|
+
} finally {
|
|
55
|
+
if (origPlatform) Object.defineProperty(process, "platform", origPlatform);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("scanTrackedProcesses", () => {
|
|
61
|
+
it("returns alive processes matching tracked PGIDs", () => {
|
|
62
|
+
const mock: SpawnSyncFn = (cmd) => {
|
|
63
|
+
if (cmd === "ps") {
|
|
64
|
+
return mockResult(
|
|
65
|
+
" 300 200 02:00 node vitest\n 200 200 02:00 /bin/bash -c npm test\n 400 400 01:00 node vite\n"
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return fail();
|
|
69
|
+
};
|
|
70
|
+
const tracked = new Set([200]);
|
|
71
|
+
const result = scanTrackedProcesses(tracked, 0, { _spawnSync: mock });
|
|
72
|
+
expect(result.some((p) => p.command === "node vitest")).toBe(true);
|
|
73
|
+
expect(result.some((p) => p.command.includes("bash"))).toBe(false);
|
|
74
|
+
expect(result.some((p) => p.pgid === 400)).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("removes dead PGIDs from tracked set", () => {
|
|
78
|
+
const mock: SpawnSyncFn = (cmd) => {
|
|
79
|
+
if (cmd === "ps") return mockResult(" 500 500 01:00 node other\n");
|
|
80
|
+
return fail();
|
|
81
|
+
};
|
|
82
|
+
const tracked = new Set([300]);
|
|
83
|
+
scanTrackedProcesses(tracked, 0, { _spawnSync: mock });
|
|
84
|
+
expect(tracked.has(300)).toBe(false);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("filters by minElapsedMs", () => {
|
|
88
|
+
const mock: SpawnSyncFn = (cmd) => {
|
|
89
|
+
if (cmd === "ps") return mockResult(" 300 200 00:05 node vitest\n");
|
|
90
|
+
return fail();
|
|
91
|
+
};
|
|
92
|
+
const tracked = new Set([200]);
|
|
93
|
+
const result = scanTrackedProcesses(tracked, 30000, { _spawnSync: mock });
|
|
94
|
+
expect(result).toHaveLength(0);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("returns empty on Windows", () => {
|
|
98
|
+
const origPlatform = Object.getOwnPropertyDescriptor(process, "platform");
|
|
99
|
+
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
|
|
100
|
+
try {
|
|
101
|
+
const tracked = new Set([200]);
|
|
102
|
+
expect(scanTrackedProcesses(tracked, 0)).toEqual([]);
|
|
103
|
+
} finally {
|
|
104
|
+
if (origPlatform) Object.defineProperty(process, "platform", origPlatform);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("scanChildProcesses (combined)", () => {
|
|
110
|
+
it("captures and returns processes in one call", () => {
|
|
111
|
+
let callCount = 0;
|
|
112
|
+
const mock: SpawnSyncFn = (cmd, args) => {
|
|
113
|
+
// Phase 1 (capture): getChildPids finds child 200 of parent 100
|
|
114
|
+
if (cmd === "ps" && args[0] === "-eo" && args[1] === "pid=,ppid=") {
|
|
115
|
+
callCount++;
|
|
116
|
+
// First call: find children of 100 → 200
|
|
117
|
+
// Second call: find children of 200 → none (leaf)
|
|
118
|
+
if (callCount === 1) return mockResult(psChildOutput([[200, 100], [999, 888]]));
|
|
119
|
+
if (callCount === 2) return mockResult(psChildOutput([[999, 888]])); // no children of 200
|
|
120
|
+
// Phase 2: full process list
|
|
121
|
+
return mockResult(" 200 200 02:00 node vitest\n 999 888 01:00 unrelated\n");
|
|
122
|
+
}
|
|
123
|
+
// Phase 1: get PGID for captured PID 200
|
|
124
|
+
if (cmd === "ps" && args[0] === "-p" && args[1] === "200" && args[2] === "-o" && args[3] === "pgid=") {
|
|
125
|
+
return mockResult(" 200\n");
|
|
126
|
+
}
|
|
127
|
+
// Phase 2: full scan
|
|
128
|
+
if (cmd === "ps" && args[0] === "-eo" && args[1] === "pid=,pgid=,etime=,args=") {
|
|
129
|
+
return mockResult(" 200 200 02:00 node vitest\n 999 888 01:00 unrelated\n");
|
|
130
|
+
}
|
|
131
|
+
return fail();
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const tracked = new Set<number>();
|
|
135
|
+
const result = scanChildProcesses(100, tracked, 0, { _spawnSync: mock });
|
|
136
|
+
expect(tracked.has(200)).toBe(true);
|
|
137
|
+
expect(result.some(p => p.command === "node vitest")).toBe(true);
|
|
138
|
+
expect(result.some(p => p.pgid === 888)).toBe(false); // unrelated PGID not tracked
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("returns empty on Windows", () => {
|
|
142
|
+
const origPlatform = Object.getOwnPropertyDescriptor(process, "platform");
|
|
143
|
+
Object.defineProperty(process, "platform", { value: "win32", configurable: true });
|
|
144
|
+
try {
|
|
145
|
+
const tracked = new Set<number>();
|
|
146
|
+
expect(scanChildProcesses(100, tracked, 0)).toEqual([]);
|
|
147
|
+
} finally {
|
|
148
|
+
if (origPlatform) Object.defineProperty(process, "platform", origPlatform);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe("killProcessByPgid", () => {
|
|
154
|
+
it("returns false for non-existent process group", () => {
|
|
155
|
+
expect(killProcessByPgid(99999)).toBe(false);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("uses taskkill on Windows", () => {
|
|
159
|
+
const mockSpawn = vi.fn().mockReturnValue({ status: 0, stdout: "" });
|
|
160
|
+
expect(killProcessByPgid(1234, { _spawnSync: mockSpawn, _platform: "win32" } as any)).toBe(true);
|
|
161
|
+
expect(mockSpawn).toHaveBeenCalledWith(
|
|
162
|
+
"taskkill",
|
|
163
|
+
["/PID", "1234", "/T", "/F"],
|
|
164
|
+
expect.any(Object),
|
|
165
|
+
);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe("Windows process scanning", () => {
|
|
170
|
+
it("scanChildProcesses delegates to scanWindowsProcesses on win32", () => {
|
|
171
|
+
const wmicOutput = [
|
|
172
|
+
"CommandLine=node server.js",
|
|
173
|
+
"CreationDate=20260410220000.000000+000",
|
|
174
|
+
"ParentProcessId=100",
|
|
175
|
+
"ProcessId=200",
|
|
176
|
+
"",
|
|
177
|
+
].join("\n");
|
|
178
|
+
const mockSpawn = vi.fn().mockReturnValue({ status: 0, stdout: wmicOutput });
|
|
179
|
+
const tracked = new Set<number>();
|
|
180
|
+
const result = scanChildProcesses(100, tracked, 0, { _spawnSync: mockSpawn, _platform: "win32" } as any);
|
|
181
|
+
expect(result.length).toBeGreaterThan(0);
|
|
182
|
+
expect(result[0].pid).toBe(200);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("falls back to PowerShell when wmic fails", () => {
|
|
186
|
+
const psOutput = JSON.stringify([{ ProcessId: 300, CommandLine: "npm test", CreationDate: new Date(Date.now() - 60000).toISOString() }]);
|
|
187
|
+
const mockSpawn = vi.fn()
|
|
188
|
+
.mockReturnValueOnce({ status: 1, stdout: "" }) // wmic fails
|
|
189
|
+
.mockReturnValueOnce({ status: 0, stdout: psOutput }); // powershell succeeds
|
|
190
|
+
const tracked = new Set<number>();
|
|
191
|
+
const result = scanChildProcesses(100, tracked, 0, { _spawnSync: mockSpawn, _platform: "win32" } as any);
|
|
192
|
+
expect(result.length).toBeGreaterThan(0);
|
|
193
|
+
expect(result[0].pid).toBe(300);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("returns empty when both wmic and PowerShell fail", () => {
|
|
197
|
+
const mockSpawn = vi.fn().mockReturnValue({ status: 1, stdout: "" });
|
|
198
|
+
const tracked = new Set<number>();
|
|
199
|
+
const result = scanChildProcesses(100, tracked, 0, { _spawnSync: mockSpawn, _platform: "win32" } as any);
|
|
200
|
+
expect(result).toEqual([]);
|
|
201
|
+
});
|
|
202
|
+
});
|