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