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

NodePtyAdapter.test.ts

116 lines3.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 testLayer = NodePtyAdapter.layer.pipe(
25 Layer.provide(
26 Layer.mergeAll(
27 NodeServices.layer,
28 Layer.succeed(HostProcessPlatform, "win32"),
29 Layer.succeed(HostProcessArchitecture, "x64"),
30 ),
31 ),
32);
33​
34it.effect("spawns through the public adapter with the provided host references", () =>
35 Effect.gen(function* () {
36 spawn.mockClear();
37 const adapter = yield* PtyAdapter.PtyAdapter;
38 const process = yield* adapter.spawn({
39 shell: "powershell.exe",
40 args: ["-NoLogo"],
41 cwd: "C:\\workspace",
42 cols: 120,
43 rows: 40,
44 env: {},
45 });
46​
47 assert.equal(process.pid, 42);
48 assert.equal(spawn.mock.calls.length, 1);
49 assert.deepEqual(spawn.mock.calls[0], [
50 "powershell.exe",
51 ["-NoLogo"],
52 {
53 cwd: "C:\\workspace",
54 cols: 120,
55 rows: 40,
56 env: { TERM: "xterm-256color" },
57 name: "xterm-256color",
58 },
59 ]);
60 }).pipe(Effect.provide(testLayer)),
61);
62​
63it.effect("preserves a caller-provided TERM in the spawn env on win32", () =>
64 Effect.gen(function* () {
65 spawn.mockClear();
66 const adapter = yield* PtyAdapter.PtyAdapter;
67 yield* adapter.spawn({
68 shell: "powershell.exe",
69 cwd: "C:\\workspace",
70 cols: 80,
71 rows: 24,
72 env: { TERM: "xterm-direct" },
73 });
74​
75 assert.equal(spawn.mock.calls.length, 1);
76 assert.deepEqual(spawn.mock.calls[0], [
77 "powershell.exe",
78 [],
79 {
80 cwd: "C:\\workspace",
81 cols: 80,
82 rows: 24,
83 env: { TERM: "xterm-direct" },
84 name: "xterm-256color",
85 },
86 ]);
87 }).pipe(Effect.provide(testLayer)),
88);
89​
90it.effect("reports native module load failures as structured startup defects", () =>
91 Effect.gen(function* () {
92 const cause = new Error("native binding could not be loaded");
93 const exit = yield* NodePtyAdapter.make(() => Promise.reject(cause)).pipe(Effect.exit);
94​
95 assert.isTrue(Exit.isFailure(exit));
96 if (Exit.isFailure(exit)) {
97 assert.isTrue(Cause.hasDies(exit.cause));
98 const error = Cause.squash(exit.cause);
99 assert.instanceOf(error, NodePtyAdapter.NodePtyModuleLoadError);
100 assert.deepInclude(error, {
101 _tag: "NodePtyModuleLoadError",
102 platform: "win32",
103 architecture: "x64",
104 });
105 assert.equal(error.message, "Failed to load node-pty for win32-x64.");
106 }
107 }).pipe(
108 Effect.provide(
109 Layer.mergeAll(
110 NodeServices.layer,
111 Layer.succeed(HostProcessPlatform, "win32"),
112 Layer.succeed(HostProcessArchitecture, "x64"),
113 ),
114 ),
115 ),
116);