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

ProcessDiagnostics.test.ts

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