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

NodePtyAdapter.test.ts

151 lines4.3 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import { HostProcessArchitecture, HostProcessPlatform } from "@t3tools/shared/hostProcess";
4import * as Cause from "effect/Cause";
5import * as Effect from "effect/Effect";
6import * as Exit from "effect/Exit";
7import * as Layer from "effect/Layer";
8import { vi } from "vite-plus/test";
9​
10import * as NodePtyAdapter from "./NodePtyAdapter.ts";
11import * as PtyAdapter from "./PtyAdapter.ts";
12​
13const spawn = vi.fn(() => ({
14 pid: 42,
15 write: vi.fn(),
16 resize: vi.fn(),
17 kill: vi.fn(),
18 onData: vi.fn(() => ({ dispose: vi.fn() })),
19 onExit: vi.fn(() => ({ dispose: vi.fn() })),
20}));
21​
22vi.mock("node-pty", () => ({ spawn }));
23​
24const makeTestLayer = (platform: NodeJS.Platform = "win32") =>
25 NodePtyAdapter.layer.pipe(
26 Layer.provide(
27 Layer.mergeAll(
28 NodeServices.layer,
29 Layer.succeed(HostProcessPlatform, platform),
30 Layer.succeed(HostProcessArchitecture, "x64"),
31 ),
32 ),
33 );
34​
35const testLayer = makeTestLayer();
36​
37for (const platform of ["win32", "linux", "darwin"] as const) {
38 it.effect(`terminates through node-pty using ${platform} semantics`, () =>
39 Effect.gen(function* () {
40 const adapter = yield* PtyAdapter.PtyAdapter;
41 const process = yield* adapter.spawn({
42 shell: "test-shell",
43 cwd: ".",
44 cols: 80,
45 rows: 24,
46 env: {},
47 });
48 const nativeProcess = spawn.mock.results.at(-1)!.value;
49 nativeProcess.kill.mockImplementation((signal?: string) => {
50 if (platform === "win32" && signal) {
51 throw new Error("Signals not supported on windows.");
52 }
53 });
54​
55 process.kill("SIGTERM");
56 process.kill("SIGKILL");
57 process.kill();
58​
59 assert.deepEqual(
60 nativeProcess.kill.mock.calls,
61 platform === "win32"
62 ? [[undefined], [undefined], [undefined]]
63 : [["SIGTERM"], ["SIGKILL"], [undefined]],
64 );
65 }).pipe(Effect.provide(makeTestLayer(platform))),
66 );
68​
69it.effect("spawns through the public adapter with the provided host references", () =>
70 Effect.gen(function* () {
71 spawn.mockClear();
72 const adapter = yield* PtyAdapter.PtyAdapter;
73 const process = yield* adapter.spawn({
74 shell: "powershell.exe",
75 args: ["-NoLogo"],
76 cwd: "C:\\workspace",
77 cols: 120,
78 rows: 40,
79 env: {},
80 });
81​
82 assert.equal(process.pid, 42);
83 assert.equal(spawn.mock.calls.length, 1);
84 assert.deepEqual(spawn.mock.calls[0], [
85 "powershell.exe",
86 ["-NoLogo"],
87 {
88 cwd: "C:\\workspace",
89 cols: 120,
90 rows: 40,
91 env: { TERM: "xterm-256color" },
92 name: "xterm-256color",
93 },
94 ]);
95 }).pipe(Effect.provide(testLayer)),
96);
97​
98it.effect("preserves a caller-provided TERM in the spawn env on win32", () =>
99 Effect.gen(function* () {
100 spawn.mockClear();
101 const adapter = yield* PtyAdapter.PtyAdapter;
102 yield* adapter.spawn({
103 shell: "powershell.exe",
104 cwd: "C:\\workspace",
105 cols: 80,
106 rows: 24,
107 env: { TERM: "xterm-direct" },
108 });
109​
110 assert.equal(spawn.mock.calls.length, 1);
111 assert.deepEqual(spawn.mock.calls[0], [
112 "powershell.exe",
113 [],
114 {
115 cwd: "C:\\workspace",
116 cols: 80,
117 rows: 24,
118 env: { TERM: "xterm-direct" },
119 name: "xterm-256color",
120 },
121 ]);
122 }).pipe(Effect.provide(testLayer)),
123);
124​
125it.effect("reports native module load failures as structured startup defects", () =>
126 Effect.gen(function* () {
127 const cause = new Error("native binding could not be loaded");
128 const exit = yield* NodePtyAdapter.make(() => Promise.reject(cause)).pipe(Effect.exit);
129​
130 assert.isTrue(Exit.isFailure(exit));
131 if (Exit.isFailure(exit)) {
132 assert.isTrue(Cause.hasDies(exit.cause));
133 const error = Cause.squash(exit.cause);
134 assert.instanceOf(error, NodePtyAdapter.NodePtyModuleLoadError);
135 assert.deepInclude(error, {
136 _tag: "NodePtyModuleLoadError",
137 platform: "win32",
138 architecture: "x64",
139 });
140 assert.equal(error.message, "Failed to load node-pty for win32-x64.");
141 }
142 }).pipe(
143 Effect.provide(
144 Layer.mergeAll(
145 NodeServices.layer,
146 Layer.succeed(HostProcessPlatform, "win32"),
147 Layer.succeed(HostProcessArchitecture, "x64"),
148 ),
149 ),
150 ),
151);