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