Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProcessDiagnostics.test.ts

291 lines8.9 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as DateTime from "effect/DateTime";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as Sink from "effect/Sink";
7import * as Stream from "effect/Stream";
8import { ChildProcessSpawner } from "effect/unstable/process";
9import { HostProcessPlatform } from "@v12/shared/hostProcess";
10​
11import * as ProcessDiagnostics from "./ProcessDiagnostics.ts";
12​
13const encoder = new TextEncoder();
14​
15function mockHandle(result: {
16 readonly stdout?: string;
17 readonly stderr?: string;
18 readonly code?: number;
19}) {
20 return ChildProcessSpawner.makeHandle({
21 pid: ChildProcessSpawner.ProcessId(1),
22 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(result.code ?? 0)),
23 isRunning: Effect.succeed(false),
24 kill: () => Effect.void,
25 unref: Effect.succeed(Effect.void),
26 stdin: Sink.drain,
27 stdout: Stream.make(encoder.encode(result.stdout ?? "")),
28 stderr: Stream.make(encoder.encode(result.stderr ?? "")),
29 all: Stream.empty,
30 getInputFd: () => Sink.drain,
31 getOutputFd: () => Stream.empty,
32 });
34​
35describe("ProcessDiagnostics", () => {
36 it.effect("parses POSIX ps rows with full commands", () =>
37 Effect.sync(() => {
38 const rows = ProcessDiagnostics.parsePosixProcessRows(
39 [
40 " 10 1 10 Ss 0.0 1024 01:02.03 /usr/bin/node server.js",
41 " 11 10 10 S+ 12.5 20480 00:04 codex app-server --config /tmp/one two",
42 ].join("\n"),
43 );
44​
45 expect(rows).toEqual([
46 {
47 pid: 10,
48 ppid: 1,
49 pgid: 10,
50 status: "Ss",
51 cpuPercent: 0,
52 rssBytes: 1024 * 1024,
53 elapsed: "01:02.03",
54 command: "/usr/bin/node server.js",
55 },
56 {
57 pid: 11,
58 ppid: 10,
59 pgid: 10,
60 status: "S+",
61 cpuPercent: 12.5,
62 rssBytes: 20480 * 1024,
63 elapsed: "00:04",
64 command: "codex app-server --config /tmp/one two",
65 },
66 ]);
67 }),
68 );
69​
70 it.effect("aggregates only descendants of the server process", () =>
71 Effect.sync(() => {
72 const diagnostics = ProcessDiagnostics.aggregateProcessDiagnostics({
73 serverPid: 100,
74 readAt: DateTime.makeUnsafe("2026-05-05T10:00:00.000Z"),
75 rows: [
76 {
77 pid: 100,
78 ppid: 1,
79 pgid: 100,
80 status: "S",
81 cpuPercent: 0,
82 rssBytes: 1_000,
83 elapsed: "01:00",
84 command: "v12 server",
85 },
86 {
87 pid: 101,
88 ppid: 100,
89 pgid: 100,
90 status: "S",
91 cpuPercent: 1.5,
92 rssBytes: 2_000,
93 elapsed: "00:20",
94 command: "codex app-server",
95 },
96 {
97 pid: 102,
98 ppid: 101,
99 pgid: 100,
100 status: "R",
101 cpuPercent: 3.25,
102 rssBytes: 4_000,
103 elapsed: "00:05",
104 command: "git status",
105 },
106 {
107 pid: 200,
108 ppid: 1,
109 pgid: 200,
110 status: "S",
111 cpuPercent: 99,
112 rssBytes: 8_000,
113 elapsed: "00:01",
114 command: "unrelated",
115 },
116 {
117 pid: 201,
118 ppid: 100,
119 pgid: 100,
120 status: "R",
121 cpuPercent: 9,
122 rssBytes: 9_000,
123 elapsed: "00:00",
124 command: "ps -axo pid=,ppid=,pgid=,stat=,pcpu=,rss=,etime=,command=",
125 },
126 ],
127 });
128​
129 expect(diagnostics.serverPid).toBe(100);
130 expect(DateTime.formatIso(diagnostics.readAt)).toBe("2026-05-05T10:00:00.000Z");
131 expect(diagnostics.processCount).toBe(2);
132 expect(diagnostics.totalRssBytes).toBe(6_000);
133 expect(diagnostics.totalCpuPercent).toBe(4.75);
134 expect(diagnostics.processes.map((process) => process.pid)).toEqual([101, 102]);
135 expect(diagnostics.processes.map((process) => process.depth)).toEqual([0, 1]);
136 expect(Option.getOrNull(diagnostics.processes[0]!.pgid)).toBe(100);
137 expect(diagnostics.processes[0]?.childPids).toEqual([102]);
138 }),
139 );
140​
141 it.effect("preserves ascending sibling order for nested descendants", () =>
142 Effect.sync(() => {
143 const diagnostics = ProcessDiagnostics.aggregateProcessDiagnostics({
144 serverPid: 100,
145 readAt: DateTime.makeUnsafe("2026-05-05T10:00:00.000Z"),
146 rows: [
147 {
148 pid: 101,
149 ppid: 100,
150 pgid: 100,
151 status: "S",
152 cpuPercent: 0,
153 rssBytes: 100,
154 elapsed: "00:10",
155 command: "agent",
156 },
157 {
158 pid: 103,
159 ppid: 101,
160 pgid: 100,
161 status: "S",
162 cpuPercent: 0,
163 rssBytes: 100,
164 elapsed: "00:10",
165 command: "child-b",
166 },
167 {
168 pid: 102,
169 ppid: 101,
170 pgid: 100,
171 status: "S",
172 cpuPercent: 0,
173 rssBytes: 100,
174 elapsed: "00:10",
175 command: "child-a",
176 },
177 ],
178 });
179​
180 expect(diagnostics.processes.map((process) => process.pid)).toEqual([101, 102, 103]);
181 }),
182 );
183​
184 it.effect("queries processes through the ChildProcessSpawner service", () =>
185 Effect.gen(function* () {
186 const commands: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }> =
187 [];
188 const spawnerLayer = Layer.succeed(
189 ChildProcessSpawner.ChildProcessSpawner,
190 ChildProcessSpawner.make((command) => {
191 const childProcess = command as unknown as {
192 readonly command: string;
193 readonly args: ReadonlyArray<string>;
194 };
195 commands.push({ command: childProcess.command, args: childProcess.args });
196 return Effect.succeed(
197 mockHandle({
198 stdout: [
199 ` ${process.pid} 1 ${process.pid} Ss 0.0 1024 01:02.03 v12 server`,
200 ` 4242 ${process.pid} ${process.pid} S 1.5 2048 00:04 agent`,
201 ].join("\n"),
202 }),
203 );
204 }),
205 );
206 const layer = ProcessDiagnostics.layer.pipe(Layer.provide(spawnerLayer));
207​
208 const diagnostics = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe(
209 Effect.flatMap((pd) => pd.read),
210 Effect.provide(layer),
211 );
212​
213 expect(diagnostics.processes.map((process) => process.pid)).toEqual([4242]);
214 expect(commands).toEqual([
215 {
216 command: "ps",
217 args: ["-axo", "pid=,ppid=,pgid=,stat=,pcpu=,rss=,etime=,command="],
218 },
219 ]);
220 }),
221 );
222​
223 it.effect("keeps bounded command diagnostics when the process query exits unsuccessfully", () =>
224 Effect.gen(function* () {
225 const spawnerLayer = Layer.succeed(
226 ChildProcessSpawner.ChildProcessSpawner,
227 ChildProcessSpawner.make(() =>
228 Effect.succeed(
229 mockHandle({
230 code: 17,
231 stdout: "partial process output",
232 stderr: "process access denied",
233 }),
234 ),
235 ),
236 );
237​
238 const error = yield* ProcessDiagnostics.readProcessRows.pipe(
239 Effect.provide(spawnerLayer),
240 Effect.provideService(HostProcessPlatform, "linux"),
241 Effect.flip,
242 );
243​
244 expect(error).toMatchObject({
245 _tag: "ProcessDiagnosticsQueryFailedError",
246 command: "ps",
247 argCount: 2,
248 cwd: process.cwd(),
249 exitCode: 17,
250 stdoutBytes: 22,
251 stderrBytes: 21,
252 stdoutTruncated: false,
253 stderrTruncated: false,
254 });
255 expect(error.message).toBe(
256 `Process diagnostics query 'ps' failed with exit code 17 in '${process.cwd()}'.`,
257 );
258 }),
259 );
260​
261 it.effect("does not allow signaling the diagnostics query process", () =>
262 Effect.gen(function* () {
263 const spawnerLayer = Layer.succeed(
264 ChildProcessSpawner.ChildProcessSpawner,
265 ChildProcessSpawner.make(() =>
266 Effect.succeed(
267 mockHandle({
268 stdout: [
269 ` ${process.pid} 1 ${process.pid} Ss 0.0 1024 01:02.03 v12 server`,
270 ` 4242 ${process.pid} ${process.pid} R 1.5 2048 00:00 ps -axo pid=,ppid=,pgid=,stat=,pcpu=,rss=,etime=,command=`,
271 ].join("\n"),
272 }),
273 ),
274 ),
275 );
276 const layer = ProcessDiagnostics.layer.pipe(Layer.provide(spawnerLayer));
277​
278 const result = yield* Effect.service(ProcessDiagnostics.ProcessDiagnostics).pipe(
279 Effect.flatMap((pd) => pd.signal({ pid: 4242, signal: "SIGINT" })),
280 Effect.provide(layer),
281 );
282​
283 expect(result).toEqual({
284 pid: 4242,
285 signal: "SIGINT",
286 signaled: false,
287 message: Option.some("Process 4242 is not a live descendant of the V12 server."),
288 });
289 }),
290 );
291});