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

nodeRuntime.test.ts

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