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

nodeRuntime.test.ts

193 lines7.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6import * as Result from "effect/Result";
7​
8import {
9 HostProcessExecutablePath,
10 HostProcessIsExecutable,
11 HostProcessPlatform,
12} from "./hostProcess.ts";
13import { resolveNodeExecutable } from "./nodeRuntime.ts";
14import { symlinksSupported } from "./testing/symlinks.ts";
15​
16describe("Node runtime selection", () => {
17 it.effect("keeps the current Node or Electron runtime without requiring Node on PATH", () =>
18 Effect.gen(function* () {
19 for (const executable of ["/runtime/node", "/Applications/T3 Code.app/Electron"]) {
20 expect(
21 yield* resolveNodeExecutable("Local device support", { PATH: "" }).pipe(
22 Effect.provideService(HostProcessExecutablePath, executable),
23 Effect.provideService(HostProcessIsExecutable, false),
24 ),
25 ).toBe(executable);
26 }
27 }).pipe(Effect.provide(NodeServices.layer)),
28 );
29​
30 it.effect("uses installed Node instead of the standalone T3 executable", () =>
31 Effect.gen(function* () {
32 const path = yield* Path.Path;
33 expect(
34 yield* resolveNodeExecutable("Local device support", {
35 PATH: path.dirname(process.execPath),
36 }),
37 ).toBe(process.execPath);
38 }).pipe(
39 Effect.provideService(HostProcessExecutablePath, "/packaged/t3"),
40 Effect.provideService(HostProcessIsExecutable, true),
41 Effect.provide(NodeServices.layer),
42 ),
43 );
44​
45 it.effect("explains how to install Node when a standalone helper has no runtime", () =>
46 Effect.gen(function* () {
47 const error = yield* resolveNodeExecutable("Local device support", { PATH: "" }).pipe(
48 Effect.flip,
49 );
50 expect(error._tag).toBe("NodeRuntimeUnavailableError");
51 expect(error.message).toContain("Local device support requires Node.js");
52 expect(error.message).toContain("Install Node.js");
53 }).pipe(
54 Effect.provideService(HostProcessIsExecutable, true),
55 Effect.provide(NodeServices.layer),
56 ),
57 );
58​
59 it.effect("finds a newly installed runtime immediately after a failed lookup", () =>
60 Effect.gen(function* () {
61 const fs = yield* FileSystem.FileSystem;
62 const path = yield* Path.Path;
63 const directory = yield* fs.makeTempDirectoryScoped();
64 const platform = yield* HostProcessPlatform;
65 const node = path.join(directory, platform === "win32" ? "node.exe" : "node");
66 const env = { PATH: directory };
67 expect(
68 Result.isFailure(
69 yield* resolveNodeExecutable("Local device support", env).pipe(Effect.result),
70 ),
71 ).toBe(true);
72 yield* fs.copyFile(process.execPath, node);
73 yield* fs.chmod(node, 0o755);
74 expect(yield* resolveNodeExecutable("Local device support", env)).toBe(node);
75 }).pipe(
76 Effect.scoped,
77 Effect.provideService(HostProcessExecutablePath, "/packaged/t3"),
78 Effect.provideService(HostProcessIsExecutable, true),
79 Effect.provide(NodeServices.layer),
80 ),
81 );
82​
83 it.effect("uses node.exe even when Windows batch wrappers appear first on PATH", () =>
84 Effect.gen(function* () {
85 const fs = yield* FileSystem.FileSystem;
86 const path = yield* Path.Path;
87 const directory = yield* fs.makeTempDirectoryScoped();
88 const wrappers = path.join(directory, "wrappers");
89 const runtime = path.join(directory, "runtime");
90 yield* fs.makeDirectory(wrappers);
91 yield* fs.makeDirectory(runtime);
92 yield* fs.writeFileString(path.join(wrappers, "node.cmd"), "@echo off");
93 yield* fs.writeFileString(path.join(wrappers, "node.bat"), "@echo off");
94 const node = path.join(runtime, "node.exe");
95 yield* fs.copyFile(process.execPath, node);
96 expect(
97 yield* resolveNodeExecutable("Local device support", {
98 PATH: `${wrappers};${runtime}`,
99 PATHEXT: ".CMD;.BAT",
100 }),
101 ).toBe(node);
102 }).pipe(
103 Effect.scoped,
104 Effect.provideService(HostProcessExecutablePath, "/packaged/t3"),
105 Effect.provideService(HostProcessIsExecutable, true),
106 Effect.provideService(HostProcessPlatform, "win32"),
107 Effect.provide(NodeServices.layer),
108 ),
109 );
110​
111 it.effect("reports install guidance when Windows only has batch runtime wrappers", () =>
112 Effect.gen(function* () {
113 const fs = yield* FileSystem.FileSystem;
114 const path = yield* Path.Path;
115 const directory = yield* fs.makeTempDirectoryScoped();
116 yield* fs.writeFileString(path.join(directory, "node.cmd"), "@echo off");
117 yield* fs.writeFileString(path.join(directory, "node.bat"), "@echo off");
118 const error = yield* resolveNodeExecutable("Local device support", {
119 PATH: directory,
120 PATHEXT: ".CMD;.BAT;.EXE",
121 }).pipe(Effect.flip);
122 expect(error._tag).toBe("NodeRuntimeUnavailableError");
123 expect(error.message).toContain("Install Node.js");
124 }).pipe(
125 Effect.scoped,
126 Effect.provideService(HostProcessIsExecutable, true),
127 Effect.provideService(HostProcessPlatform, "win32"),
128 Effect.provide(NodeServices.layer),
129 ),
130 );
131​
132 it.effect("rejects a hard-linked node alias pointing back at the standalone app", () =>
133 Effect.gen(function* () {
134 const fs = yield* FileSystem.FileSystem;
135 const path = yield* Path.Path;
136 const directory = yield* fs.makeTempDirectoryScoped();
137 const platform = yield* HostProcessPlatform;
138 const executable = path.join(directory, platform === "win32" ? "t3.exe" : "t3");
139 const node = path.join(directory, platform === "win32" ? "node.exe" : "node");
140 yield* fs.writeFileString(executable, "standalone executable fixture");
141 yield* fs.chmod(executable, 0o755);
142 yield* fs.link(executable, node);
143 const error = yield* resolveNodeExecutable("Local device support", { PATH: directory }).pipe(
144 Effect.provideService(HostProcessExecutablePath, executable),
145 Effect.flip,
146 );
147 expect(error._tag).toBe("NodeRuntimeUnavailableError");
148 expect(error.message).toContain("Install Node.js");
149 }).pipe(
150 Effect.scoped,
151 Effect.provideService(HostProcessIsExecutable, true),
152 Effect.provide(NodeServices.layer),
153 ),
154 );
155​
156 it.effect.skipIf(!symlinksSupported)("preserves the node alias used by runtime launchers", () =>
157 Effect.gen(function* () {
158 const fs = yield* FileSystem.FileSystem;
159 const path = yield* Path.Path;
160 const directory = yield* fs.makeTempDirectoryScoped();
161 const platform = yield* HostProcessPlatform;
162 const node = path.join(directory, platform === "win32" ? "node.exe" : "node");
163 yield* fs.symlink(process.execPath, node);
164 expect(yield* resolveNodeExecutable("Local device support", { PATH: directory })).toBe(node);
165 }).pipe(
166 Effect.scoped,
167 Effect.provideService(HostProcessExecutablePath, "/packaged/t3"),
168 Effect.provideService(HostProcessIsExecutable, true),
169 Effect.provide(NodeServices.layer),
170 ),
171 );
172​
173 it.effect.skipIf(!symlinksSupported)(
174 "rejects a node alias pointing back at the standalone app",
175 () =>
176 Effect.gen(function* () {
177 const fs = yield* FileSystem.FileSystem;
178 const path = yield* Path.Path;
179 const directory = yield* fs.makeTempDirectoryScoped();
180 const platform = yield* HostProcessPlatform;
181 const node = path.join(directory, platform === "win32" ? "node.exe" : "node");
182 yield* fs.symlink(process.execPath, node);
183 const error = yield* resolveNodeExecutable("Local device support", {
184 PATH: directory,
185 }).pipe(Effect.flip);
186 expect(error.message).toContain("Install Node.js");
187 }).pipe(
188 Effect.scoped,
189 Effect.provideService(HostProcessIsExecutable, true),
190 Effect.provide(NodeServices.layer),
191 ),
192 );
193});