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

processManager.test.ts

48 lines1.2 KB

1import { describe, expect, it } from "vitest";
2​
3import { ProcessManager } from "./processManager";
4​
5describe("ProcessManager", () => {
6 it("can be instantiated", () => {
7 const pm = new ProcessManager();
8 expect(pm).toBeInstanceOf(ProcessManager);
9 });
10​
11 it("spawns a process and receives output", async () => {
12 const pm = new ProcessManager();
13 const chunks: string[] = [];
14​
15 const done = new Promise<void>((resolve) => {
16 pm.on("output", (chunk) => {
17 chunks.push(chunk.data);
18 });
19 pm.on("exit", () => {
20 resolve();
21 });
22 });
23​
24 const sessionId = pm.spawn({ command: "echo", args: ["hello"] });
25 expect(typeof sessionId).toBe("string");
26 expect(sessionId.length).toBeGreaterThan(0);
27​
28 await done;
29​
30 expect(chunks.join("").trim()).toBe("hello");
31 });
32​
33 it("can kill a spawned process", async () => {
34 const pm = new ProcessManager();
35​
36 const exited = new Promise<number | null>((resolve) => {
37 pm.on("exit", (exit) => {
38 resolve(exit.code);
39 });
40 });
41​
42 const sessionId = pm.spawn({ command: "sleep", args: ["10"] });
43 pm.kill(sessionId);
44​
45 const code = await exited;
46 expect(code).not.toBe(0);
47 });
48});