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

invocation.test.ts

406 lines18.1 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Path from "effect/Path";
7​
8import packageJson from "../../package.json" with { type: "json" };
9import { posixLauncherScript } from "../cloud/pinnedRuntime.ts";
10import {
11 hostShellWord,
12 resolveHostCliCommand,
13 resolveRootCliCommand,
14 resolveServerInstallation,
15} from "./invocation.ts";
16​
17it.layer(NodeServices.layer)("root CLI commands", (it) => {
18 const rootCommand = (input: {
19 readonly node: string;
20 readonly entry: string;
21 readonly path?: string;
22 readonly env?: Record<string, string>;
23 readonly executable?: boolean;
24 readonly args?: ReadonlyArray<string>;
25 }) =>
26 resolveRootCliCommand("browser setup", input.args).pipe(
27 Effect.provideService(HostProcess.ExecutablePath, input.node),
28 Effect.provideService(HostProcess.Arguments, [input.node, input.entry]),
29 Effect.provideService(HostProcess.IsExecutable, input.executable ?? false),
30 Effect.provideService(HostProcess.Platform, "linux"),
31 Effect.provideService(HostProcess.Environment, { PATH: input.path ?? "", ...input.env }),
32 );
33​
34 const pathWithT3 = Effect.gen(function* () {
35 const fs = yield* FileSystem.FileSystem;
36 const path = yield* Path.Path;
37 const bin = yield* fs.makeTempDirectoryScoped();
38 yield* fs.writeFileString(path.join(bin, "t3"), "#!/bin/sh\n", { mode: 0o755 });
39 return bin;
40 });
41​
42 it.effect("keeps a user-installed Node reachable when the command runs under sudo", () =>
43 Effect.gen(function* () {
44 const npx = "/home/theo/.npm/_npx/abc/node_modules/t3/dist/bin.mjs";
45 expect(yield* rootCommand({ node: "/usr/bin/node", entry: npx })).toBe(
46 "sudo npx t3 browser setup",
47 );
48 expect(
49 yield* rootCommand({ node: "/home/theo/.nvm/versions/node/v24/bin/node", entry: npx }),
50 ).toBe('sudo env "PATH=$PATH" npx t3 browser setup');
51 expect(
52 yield* rootCommand({
53 node: "/home/theo/.local/node/bin/node",
54 entry: "/home/theo/.local/lib/node_modules/t3/dist/bin.mjs",
55 path: yield* pathWithT3,
56 }),
57 ).toBe('sudo env "PATH=$PATH" t3 browser setup');
58 }),
59 );
60​
61 it.effect("keeps the arguments, so root runs setup on the same T3 home", () =>
62 Effect.gen(function* () {
63 const args = ["--base-dir", "/srv/t3 home"];
64 expect(
65 yield* rootCommand({
66 node: "/home/theo/.local/node/bin/node",
67 entry: "/home/theo/.local/lib/node_modules/t3/dist/bin.mjs",
68 path: yield* pathWithT3,
69 args,
70 }),
71 ).toBe(`sudo env "PATH=$PATH" t3 browser setup --base-dir '/srv/t3 home'`);
72 expect(
73 yield* rootCommand({ node: "/opt/t3/t3", entry: "/opt/t3/t3", executable: true, args }),
74 ).toBe("sudo /opt/t3/t3 browser setup --base-dir '/srv/t3 home'");
75 }),
76 );
77​
78 it.effect("names this install's launcher when t3 is not on PATH", () =>
79 Effect.gen(function* () {
80 const fs = yield* FileSystem.FileSystem;
81 const path = yield* Path.Path;
82 const home = yield* fs.makeTempDirectoryScoped();
83 const shim = path.join(home, ".local bin", "t3");
84 yield* fs.makeDirectory(path.dirname(shim), { recursive: true });
85 yield* fs.writeFileString(shim, "#!/bin/sh\n", { mode: 0o755 });
86 const desktop = {
87 node: "/tmp/.mount_T3abc/t3code",
88 entry: "/tmp/.mount_T3abc/resources/app.asar/apps/server/dist/bin.mjs",
89 env: { T3CODE_CLI_PATH: shim },
90 };
91​
92 expect(yield* rootCommand(desktop)).toBe(`sudo '${shim}' browser setup`);
93​
94 // An unrelated `t3` on PATH, such as an older npm global, is another install's CLI.
95 expect(yield* rootCommand({ ...desktop, path: yield* pathWithT3 })).toBe(
96 `sudo '${shim}' browser setup`,
97 );
98​
99 // The link the app installs onto PATH leads to its own launcher, so plain `t3` is right.
100 const linked = yield* fs.makeTempDirectoryScoped();
101 yield* fs.symlink(shim, path.join(linked, "t3"));
102 expect(yield* rootCommand({ ...desktop, path: linked })).toBe(
103 'sudo env "PATH=$PATH" t3 browser setup',
104 );
105​
106 expect(
107 yield* rootCommand({
108 node: "/opt/t3/t3",
109 entry: "/opt/t3/t3",
110 executable: true,
111 }),
112 ).toBe("sudo /opt/t3/t3 browser setup");
113​
114 // A standalone executable started directly (systemd, no launcher) is still this install.
115 expect(
116 yield* rootCommand({
117 node: "/opt/t3/t3",
118 entry: "/opt/t3/t3",
119 executable: true,
120 path: yield* pathWithT3,
121 }),
122 ).toBe("sudo /opt/t3/t3 browser setup");
123​
124 // A global npm launcher runs the downloaded executable; PATH's `t3` is that launcher.
125 const prefix = path.join(home, "npm");
126 const npmLauncher = path.join(prefix, "lib/node_modules/t3/bin/t3.mjs");
127 yield* fs.makeDirectory(path.dirname(npmLauncher), { recursive: true });
128 yield* fs.makeDirectory(path.join(prefix, "bin"));
129 yield* fs.writeFileString(npmLauncher, "", { mode: 0o755 });
130 yield* fs.writeFileString(
131 path.join(prefix, "lib/node_modules/t3/package.json"),
132 `{"name":"t3","version":"${packageJson.version}"}`,
133 );
134 yield* fs.symlink(npmLauncher, path.join(prefix, "bin/t3"));
135 expect(
136 yield* rootCommand({
137 node: "/home/theo/.t3/runtime/versions/1.0.0/t3",
138 entry: "/home/theo/.t3/runtime/versions/1.0.0/t3",
139 executable: true,
140 path: path.join(prefix, "bin"),
141 env: { T3CODE_NPM_LAUNCHER_PATH: npmLauncher },
142 }),
143 ).toBe('sudo env "PATH=$PATH" t3 browser setup');
144​
145 expect(
146 yield* rootCommand({ ...desktop, env: { T3CODE_CLI_PATH: path.join(home, "gone") } }),
147 ).toBe('sudo env "PATH=$PATH" t3 browser setup');
148 }),
149 );
150});
151​
152it.layer(NodeServices.layer)("host CLI commands", (it) => {
153 it.effect("renders a PowerShell command line on Windows", () =>
154 Effect.gen(function* () {
155 const fs = yield* FileSystem.FileSystem;
156 const path = yield* Path.Path;
157 const home = yield* fs.makeTempDirectoryScoped();
158 const shim = path.join(home, "t3.cmd");
159 yield* fs.writeFileString(shim, "@echo off\r\n");
160 const hostCommand = (platform: NodeJS.Platform, env: Record<string, string>) =>
161 resolveHostCliCommand("doctor", ["--base-dir", "C:\\Users\\Jane Doe\\.t3"]).pipe(
162 Effect.provideService(HostProcess.ExecutablePath, "C:\\Program Files\\T3 Code\\t3.exe"),
163 Effect.provideService(HostProcess.Arguments, [
164 "C:\\Program Files\\T3 Code\\t3.exe",
165 "C:\\Program Files\\T3 Code\\resources\\app.asar\\apps\\server\\dist\\bin.mjs",
166 ]),
167 Effect.provideService(HostProcess.IsExecutable, false),
168 Effect.provideService(HostProcess.Platform, platform),
169 Effect.provideService(HostProcess.Environment, { PATH: "", ...env }),
170 );
171 expect((yield* hostCommand("win32", { T3CODE_CLI_PATH: shim })).command).toBe(
172 `& "${shim}" doctor "--base-dir" "C:\\Users\\Jane Doe\\.t3"`,
173 );
174 expect((yield* hostCommand("win32", {})).command).toBe(
175 't3 doctor "--base-dir" "C:\\Users\\Jane Doe\\.t3"',
176 );
177 expect(
178 (yield* resolveHostCliCommand("doctor", ["--base-dir", "C:\\T3\\\u201CArchive\u201D"]).pipe(
179 Effect.provideService(HostProcess.Platform, "win32"),
180 Effect.provideService(HostProcess.Environment, { PATH: "" }),
181 Effect.provideService(HostProcess.ExecutablePath, "C:\\t3.exe"),
182 Effect.provideService(HostProcess.Arguments, ["C:\\t3.exe", "C:\\bin.mjs"]),
183 Effect.provideService(HostProcess.IsExecutable, false),
184 )).command,
185 ).toBe('t3 doctor "--base-dir" "C:\\T3\\\u201C\u201CArchive\u201D\u201D"');
186 expect((yield* hostCommand("linux", { T3CODE_CLI_PATH: shim })).command).toMatch(
187 / doctor --base-dir 'C:\\Users\\Jane Doe\\\.t3'$/,
188 );
189 }),
190 );
191​
192 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
193 "names the absolute path of a t3 found through a relative PATH entry",
194 () =>
195 Effect.gen(function* () {
196 const fs = yield* FileSystem.FileSystem;
197 const path = yield* Path.Path;
198 const bin = yield* fs.makeTempDirectoryScoped();
199 yield* fs.writeFileString(path.join(bin, "t3"), "#!/bin/sh\n", { mode: 0o755 });
200 const hostCommand = (searchPath: string) =>
201 resolveHostCliCommand("doctor").pipe(
202 Effect.provideService(HostProcess.ExecutablePath, "/usr/bin/node"),
203 Effect.provideService(HostProcess.Arguments, [
204 "/usr/bin/node",
205 "/opt/t3/lib/node_modules/t3/dist/bin.mjs",
206 ]),
207 Effect.provideService(HostProcess.IsExecutable, false),
208 Effect.provideService(HostProcess.Platform, "linux"),
209 Effect.provideService(HostProcess.Environment, { PATH: searchPath }),
210 );
211​
212 // A relative entry was searched from this process's directory, unlike the user's shell.
213 expect(yield* hostCommand(path.relative(process.cwd(), bin))).toEqual({
214 command: `${hostShellWord("linux", path.join(bin, "t3"))} doctor`,
215 launcher: false,
216 });
217 // An empty entry makes the shell search its own directory first, wherever that is.
218 expect(yield* hostCommand(`:${bin}`)).toEqual({
219 command: `${hostShellWord("linux", path.join(bin, "t3"))} doctor`,
220 launcher: false,
221 });
222 expect(yield* hostCommand(bin)).toEqual({ command: "t3 doctor", launcher: false });
223 }),
224 );
225});
226​
227it.layer(NodeServices.layer)("manual server installation ownership", (it) => {
228 it.effect("recognizes runner caches for script packages and the npm launcher", () =>
229 Effect.gen(function* () {
230 const fs = yield* FileSystem.FileSystem;
231 const path = yield* Path.Path;
232 const root = yield* fs.makeTempDirectoryScoped();
233 const executable = path.join(root, ".t3/runtime/versions/1.0.0/t3");
234 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
235 yield* fs.writeFileString(executable, "");
236 for (const [relative, kind] of [
237 ["npm/_npx/hash/node_modules/t3/dist/bin.mjs", "npx"],
238 ["npm/_npx/hash/node_modules/t3/bin/t3.mjs", "npx"],
239 ["pnpm/dlx/hash/node_modules/t3/dist/bin.mjs", "pnpm-dlx"],
240 [".bun/install/cache/t3/dist/bin.mjs", "bunx"],
241 ] as const) {
242 const entry = path.join(root, relative);
243 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
244 yield* fs.writeFileString(entry, "");
245 yield* fs.writeFileString(
246 path.join(path.dirname(entry), "../package.json"),
247 `{"name":"t3","version":"${packageJson.version}","bin":{"t3":"./bin/t3.mjs"}}`,
248 );
249 const launchedBy = entry.endsWith("t3.mjs");
250 const installation = yield* resolveServerInstallation.pipe(
251 Effect.provideService(HostProcess.Arguments, [launchedBy ? executable : "node", entry]),
252 Effect.provideService(HostProcess.ExecutablePath, executable),
253 Effect.provideService(HostProcess.IsExecutable, launchedBy),
254 Effect.provideService(
255 HostProcess.Environment,
256 launchedBy ? { T3CODE_NPM_LAUNCHER_PATH: entry } : {},
257 ),
258 );
259 expect(installation).toEqual({ kind });
260 }
261 }),
262 );
263​
264 it.effect("requires the npm prefix's bin to point to the running package", () =>
265 Effect.gen(function* () {
266 const fs = yield* FileSystem.FileSystem;
267 const path = yield* Path.Path;
268 const root = yield* fs.makeTempDirectoryScoped();
269 const prefix = path.join(root, "bunx-tools");
270 const packageRoot = path.join(prefix, "lib/node_modules/t3");
271 const entry = path.join(packageRoot, "dist/bin.mjs");
272 const globalBin = path.join(prefix, "bin/t3");
273 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
274 yield* fs.makeDirectory(path.dirname(globalBin), { recursive: true });
275 yield* fs.writeFileString(entry, "");
276 yield* fs.writeFileString(
277 path.join(packageRoot, "package.json"),
278 '{"name":"t3","version":"0.0.45","bin":{"t3":"./dist/bin.mjs"}}',
279 );
280 const resolve = resolveServerInstallation.pipe(
281 Effect.provideService(HostProcess.Arguments, ["node", entry]),
282 Effect.provideService(HostProcess.IsExecutable, false),
283 Effect.provideService(HostProcess.Platform, "linux"),
284 );
285 expect(yield* resolve).toBeNull();
286 yield* fs.symlink(entry, globalBin);
287 expect(yield* resolve).toEqual({ kind: "npm-global", prefix });
288 yield* fs.remove(globalBin);
289 yield* fs.writeFileString(globalBin, "an unrelated t3 command");
290 expect(yield* resolve).toBeNull();
291 expect(yield* resolve.pipe(Effect.provideService(HostProcess.Platform, "win32"))).toBeNull();
292 }),
293 );
294​
295 it.effect("proves the downloaded executable was started by a global npm launcher", () =>
296 Effect.gen(function* () {
297 const fs = yield* FileSystem.FileSystem;
298 const path = yield* Path.Path;
299 const root = yield* fs.makeTempDirectoryScoped();
300 const prefix = path.join(root, "bunx-tools");
301 const packageRoot = path.join(prefix, "lib/node_modules/t3");
302 const launcher = path.join(packageRoot, "bin/t3.mjs");
303 const executable = path.join(root, ".t3/runtime/versions/1.0.0/t3");
304 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
305 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
306 yield* fs.makeDirectory(path.join(prefix, "bin"));
307 yield* fs.writeFileString(launcher, "");
308 yield* fs.writeFileString(executable, "");
309 yield* fs.symlink(launcher, path.join(prefix, "bin/t3"));
310 const resolve = resolveServerInstallation.pipe(
311 Effect.provideService(HostProcess.ExecutablePath, executable),
312 Effect.provideService(HostProcess.Arguments, [executable, executable]),
313 Effect.provideService(HostProcess.IsExecutable, true),
314 Effect.provideService(HostProcess.Platform, "linux"),
315 Effect.provideService(HostProcess.Environment, { T3CODE_NPM_LAUNCHER_PATH: launcher }),
316 );
317 for (const [version, expected] of [
318 ["0.0.1", null],
319 [packageJson.version, { kind: "npm-global", prefix }],
320 ] as const) {
321 yield* fs.writeFileString(
322 path.join(packageRoot, "package.json"),
323 `{"name":"t3","version":"${version}","bin":{"t3":"./bin/t3.mjs"}}`,
324 );
325 expect(yield* resolve).toEqual(expected);
326 }
327 }),
328 );
329​
330 it.effect("names the launcher that started a downloaded runtime", () =>
331 Effect.gen(function* () {
332 const fs = yield* FileSystem.FileSystem;
333 const path = yield* Path.Path;
334 const root = yield* fs.realPath(yield* fs.makeTempDirectoryScoped());
335 // The install script's launcher names ~/.t3, which here links to a home elsewhere.
336 const executable = path.join(root, "data/t3/runtime/versions/1.0.0/t3");
337 const launcher = path.join(root, "bin/t3");
338 const copy = path.join(root, "bin/t3-copy");
339 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
340 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
341 yield* fs.writeFileString(executable, "", { mode: 0o755 });
342 yield* fs.writeFileString(copy, "", { mode: 0o755 });
343 yield* fs.symlink(path.join(root, "data/t3"), path.join(root, ".t3"));
344 yield* fs.symlink(path.join(root, ".t3/runtime/versions/1.0.0/t3"), launcher);
345 // Earlier PATH entries hold a directory and a non-executable file named t3.
346 yield* fs.makeDirectory(path.join(root, "shadow-dir/t3"), { recursive: true });
347 yield* fs.makeDirectory(path.join(root, "shadow-file"));
348 yield* fs.writeFileString(path.join(root, "shadow-file/t3"), "");
349 const resolve = (invokedAs: string, PATH = "shadow-dir:shadow-file:bin") =>
350 resolveServerInstallation.pipe(
351 Effect.provideService(HostProcess.ExecutablePath, executable),
352 Effect.provideService(HostProcess.IsExecutable, true),
353 Effect.provideService(HostProcess.InvokedAs, invokedAs),
354 Effect.provideService(HostProcess.WorkingDirectory, root),
355 Effect.provideService(HostProcess.Environment, { PATH }),
356 Effect.provideService(HostProcess.HomeDirectory, root),
357 Effect.provideService(HostProcess.Platform, "linux"),
358 );
359 const baseDir = path.join(root, ".t3");
360​
361 expect(yield* resolve("t3")).toEqual({ kind: "t3-launcher", launcher, baseDir });
362 expect(yield* resolve(launcher)).toEqual({ kind: "t3-launcher", launcher, baseDir });
363 expect(yield* resolve(executable)).toBeNull();
364 expect(yield* resolve(copy)).toBeNull();
365​
366 // The script launcher execs the runtime by absolute path, so argv0 names the runtime.
367 yield* fs.remove(launcher);
368 yield* fs.writeFileString(
369 launcher,
370 posixLauncherScript(path.join(root, ".t3/runtime/versions/1.0.0/t3")),
371 { mode: 0o755 },
372 );
373 expect(yield* resolve(executable, path.join(root, "bin"))).toEqual({
374 kind: "t3-launcher",
375 launcher,
376 baseDir,
377 });
378 }),
379 );
380​
381 it.effect("leaves local, standalone, missing and unreadable installs unknown", () =>
382 Effect.gen(function* () {
383 const fs = yield* FileSystem.FileSystem;
384 const path = yield* Path.Path;
385 const root = yield* fs.makeTempDirectoryScoped();
386 for (const relative of [
387 "project/node_modules/t3/dist/bin.mjs",
388 "project/apps/server/dist/bin.mjs",
389 ".t3/runtime/0.0.45/t3",
390 "missing/dist/bin.mjs",
391 ]) {
392 const entry = path.join(root, relative);
393 if (!relative.startsWith("missing")) {
394 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
395 yield* fs.writeFileString(entry, "");
396 }
397 expect(
398 yield* resolveServerInstallation.pipe(
399 Effect.provideService(HostProcess.Arguments, ["node", entry]),
400 Effect.provideService(HostProcess.IsExecutable, false),
401 ),
402 ).toBeNull();
403 }
404 }),
405 );
406});