forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocalProcesses.test.ts
More file actions
63 lines (53 loc) · 1.91 KB
/
Copy pathlocalProcesses.test.ts
File metadata and controls
63 lines (53 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { describe, expect, it, vi } from "vitest";
import {
parseListeningPidList,
probeLocalPorts,
stopLocalPorts,
type LocalProcessControls,
} from "./localProcesses.ts";
function makeControls(overrides: Partial<LocalProcessControls> = {}): LocalProcessControls {
return {
currentPid: 999,
listListeningPids: vi.fn(async () => []),
killPid: vi.fn(),
...overrides,
};
}
describe("localProcesses", () => {
it("parses and deduplicates listening process ids", () => {
expect(parseListeningPidList("123\n456\n123 ignored\n")).toEqual([123, 456]);
});
it("stops unique pids for unique ports", async () => {
const controls = makeControls({
listListeningPids: vi.fn(async (port) => (port === 5173 ? [111, 222, 111] : [])),
killPid: vi.fn(),
});
await expect(stopLocalPorts({ ports: [5173, 5173, 3000] }, controls)).resolves.toEqual({
results: [
{ port: 5173, killedPids: [111, 222], errors: [] },
{ port: 3000, killedPids: [], errors: [] },
],
});
expect(controls.killPid).toHaveBeenCalledTimes(2);
});
it("refuses to stop the current T3 Code process", async () => {
const controls = makeControls({
currentPid: 111,
listListeningPids: vi.fn(async () => [111]),
killPid: vi.fn(),
});
const result = await stopLocalPorts({ ports: [5173] }, controls);
expect(result.results[0]?.killedPids).toEqual([]);
expect(result.results[0]?.errors[0]).toContain("Refusing to stop");
expect(controls.killPid).not.toHaveBeenCalled();
});
it("reports the current T3 Code process as listening when probing ports", async () => {
const controls = makeControls({
currentPid: 111,
listListeningPids: vi.fn(async () => [111]),
});
await expect(probeLocalPorts({ ports: [5173] }, controls)).resolves.toEqual({
results: [{ port: 5173, isListening: true, pids: [111], error: null }],
});
});
});