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 {
10 resolveHostCliCommand,
11 resolveRootCliCommand,
12 resolveServerInstallation,
13} from "./invocation.ts";
14
15it.layer(NodeServices.layer)("root CLI commands", (it) => {
16 const rootCommand = (input: {
17 readonly node: string;
18 readonly entry: string;
19 readonly path?: string;
20 readonly env?: Record<string, string>;
21 readonly executable?: boolean;
22 }) =>
23 resolveRootCliCommand("browser setup").pipe(
24 Effect.provideService(HostProcess.ExecutablePath, input.node),
25 Effect.provideService(HostProcess.Arguments, [input.node, input.entry]),
26 Effect.provideService(HostProcess.IsExecutable, input.executable ?? false),
27 Effect.provideService(HostProcess.Platform, "linux"),
28 Effect.provideService(HostProcess.Environment, { PATH: input.path ?? "", ...input.env }),
29 );
30
31 const pathWithT3 = Effect.gen(function* () {
32 const fs = yield* FileSystem.FileSystem;
33 const path = yield* Path.Path;
34 const bin = yield* fs.makeTempDirectoryScoped();
35 yield* fs.writeFileString(path.join(bin, "t3"), "#!/bin/sh\n", { mode: 0o755 });
36 return bin;
37 });
38
39 it.effect("keeps a user-installed Node reachable when the command runs under sudo", () =>
40 Effect.gen(function* () {
41 const npx = "/home/theo/.npm/_npx/abc/node_modules/t3/dist/bin.mjs";
42 expect(yield* rootCommand({ node: "/usr/bin/node", entry: npx })).toBe(
43 "sudo npx t3 browser setup",
44 );
45 expect(
46 yield* rootCommand({ node: "/home/theo/.nvm/versions/node/v24/bin/node", entry: npx }),
47 ).toBe('sudo env "PATH=$PATH" npx t3 browser setup');
48 expect(
49 yield* rootCommand({
50 node: "/home/theo/.local/node/bin/node",
51 entry: "/home/theo/.local/lib/node_modules/t3/dist/bin.mjs",
52 path: yield* pathWithT3,
53 }),
54 ).toBe('sudo env "PATH=$PATH" t3 browser setup');
55 }).pipe(Effect.scoped),
56 );
57
58 it.effect("names this install's launcher when t3 is not on PATH", () =>
59 Effect.gen(function* () {
60 const fs = yield* FileSystem.FileSystem;
61 const path = yield* Path.Path;
62 const home = yield* fs.makeTempDirectoryScoped();
63 const shim = path.join(home, ".local bin", "t3");
64 yield* fs.makeDirectory(path.dirname(shim), { recursive: true });
65 yield* fs.writeFileString(shim, "#!/bin/sh\n", { mode: 0o755 });
66 const desktop = {
67 node: "/tmp/.mount_T3abc/t3code",
68 entry: "/tmp/.mount_T3abc/resources/app.asar/apps/server/dist/bin.mjs",
69 env: { T3CODE_CLI_PATH: shim },
70 };
71
72 expect(yield* rootCommand(desktop)).toBe(`sudo '${shim}' browser setup`);
73
74 expect(yield* rootCommand({ ...desktop, path: yield* pathWithT3 })).toBe(
75 'sudo env "PATH=$PATH" t3 browser setup',
76 );
77
78 expect(
79 yield* rootCommand({
80 node: "/opt/t3/t3",
81 entry: "/opt/t3/t3",
82 executable: true,
83 }),
84 ).toBe("sudo /opt/t3/t3 browser setup");
85
86 expect(
87 yield* rootCommand({ ...desktop, env: { T3CODE_CLI_PATH: path.join(home, "gone") } }),
88 ).toBe('sudo env "PATH=$PATH" t3 browser setup');
89 }).pipe(Effect.scoped),
90 );
91});
92
93it.layer(NodeServices.layer)("host CLI commands", (it) => {
94 it.effect("renders a PowerShell command line on Windows", () =>
95 Effect.gen(function* () {
96 const fs = yield* FileSystem.FileSystem;
97 const path = yield* Path.Path;
98 const home = yield* fs.makeTempDirectoryScoped();
99 const shim = path.join(home, "t3.cmd");
100 yield* fs.writeFileString(shim, "@echo off\r\n");
101 const hostCommand = (platform: NodeJS.Platform, env: Record<string, string>) =>
102 resolveHostCliCommand("doctor", ["--base-dir", "C:\\Users\\Jane Doe\\.t3"]).pipe(
103 Effect.provideService(HostProcess.ExecutablePath, "C:\\Program Files\\T3 Code\\t3.exe"),
104 Effect.provideService(HostProcess.Arguments, [
105 "C:\\Program Files\\T3 Code\\t3.exe",
106 "C:\\Program Files\\T3 Code\\resources\\app.asar\\apps\\server\\dist\\bin.mjs",
107 ]),
108 Effect.provideService(HostProcess.IsExecutable, false),
109 Effect.provideService(HostProcess.Platform, platform),
110 Effect.provideService(HostProcess.Environment, { PATH: "", ...env }),
111 );
112 expect((yield* hostCommand("win32", { T3CODE_CLI_PATH: shim })).command).toBe(
113 `& "${shim}" doctor "--base-dir" "C:\\Users\\Jane Doe\\.t3"`,
114 );
115 expect((yield* hostCommand("win32", {})).command).toBe(
116 't3 doctor "--base-dir" "C:\\Users\\Jane Doe\\.t3"',
117 );
118 expect(
119 (yield* resolveHostCliCommand("doctor", ["--base-dir", "C:\\T3\\\u201CArchive\u201D"]).pipe(
120 Effect.provideService(HostProcess.Platform, "win32"),
121 Effect.provideService(HostProcess.Environment, { PATH: "" }),
122 Effect.provideService(HostProcess.ExecutablePath, "C:\\t3.exe"),
123 Effect.provideService(HostProcess.Arguments, ["C:\\t3.exe", "C:\\bin.mjs"]),
124 Effect.provideService(HostProcess.IsExecutable, false),
125 )).command,
126 ).toBe('t3 doctor "--base-dir" "C:\\T3\\\u201C\u201CArchive\u201D\u201D"');
127 expect((yield* hostCommand("linux", { T3CODE_CLI_PATH: shim })).command).toMatch(
128 / doctor --base-dir 'C:\\Users\\Jane Doe\\\.t3'$/,
129 );
130 }).pipe(Effect.scoped),
131 );
132});
133
134it.layer(NodeServices.layer)("manual server installation ownership", (it) => {
135 it.effect("recognizes runner caches for script packages and the npm launcher", () =>
136 Effect.gen(function* () {
137 const fs = yield* FileSystem.FileSystem;
138 const path = yield* Path.Path;
139 const root = yield* fs.makeTempDirectoryScoped();
140 const executable = path.join(root, ".t3/runtime/versions/1.0.0/t3");
141 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
142 yield* fs.writeFileString(executable, "");
143 for (const [relative, kind] of [
144 ["npm/_npx/hash/node_modules/t3/dist/bin.mjs", "npx"],
145 ["npm/_npx/hash/node_modules/t3/bin/t3.mjs", "npx"],
146 ["pnpm/dlx/hash/node_modules/t3/dist/bin.mjs", "pnpm-dlx"],
147 [".bun/install/cache/t3/dist/bin.mjs", "bunx"],
148 ] as const) {
149 const entry = path.join(root, relative);
150 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
151 yield* fs.writeFileString(entry, "");
152 yield* fs.writeFileString(
153 path.join(path.dirname(entry), "../package.json"),
154 `{"name":"t3","version":"${packageJson.version}","bin":{"t3":"./bin/t3.mjs"}}`,
155 );
156 const launchedBy = entry.endsWith("t3.mjs");
157 const installation = yield* resolveServerInstallation.pipe(
158 Effect.provideService(HostProcess.Arguments, [launchedBy ? executable : "node", entry]),
159 Effect.provideService(HostProcess.ExecutablePath, executable),
160 Effect.provideService(HostProcess.IsExecutable, launchedBy),
161 Effect.provideService(
162 HostProcess.Environment,
163 launchedBy ? { T3CODE_NPM_LAUNCHER_PATH: entry } : {},
164 ),
165 );
166 expect(installation).toEqual({ kind });
167 }
168 }),
169 );
170
171 it.effect("requires the npm prefix's bin to point to the running package", () =>
172 Effect.gen(function* () {
173 const fs = yield* FileSystem.FileSystem;
174 const path = yield* Path.Path;
175 const root = yield* fs.makeTempDirectoryScoped();
176 const prefix = path.join(root, "bunx-tools");
177 const packageRoot = path.join(prefix, "lib/node_modules/t3");
178 const entry = path.join(packageRoot, "dist/bin.mjs");
179 const globalBin = path.join(prefix, "bin/t3");
180 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
181 yield* fs.makeDirectory(path.dirname(globalBin), { recursive: true });
182 yield* fs.writeFileString(entry, "");
183 yield* fs.writeFileString(
184 path.join(packageRoot, "package.json"),
185 '{"name":"t3","version":"0.0.45","bin":{"t3":"./dist/bin.mjs"}}',
186 );
187 const resolve = resolveServerInstallation.pipe(
188 Effect.provideService(HostProcess.Arguments, ["node", entry]),
189 Effect.provideService(HostProcess.IsExecutable, false),
190 Effect.provideService(HostProcess.Platform, "linux"),
191 );
192 expect(yield* resolve).toBeNull();
193 yield* fs.symlink(entry, globalBin);
194 expect(yield* resolve).toEqual({ kind: "npm-global", prefix });
195 yield* fs.remove(globalBin);
196 yield* fs.writeFileString(globalBin, "an unrelated t3 command");
197 expect(yield* resolve).toBeNull();
198 expect(yield* resolve.pipe(Effect.provideService(HostProcess.Platform, "win32"))).toBeNull();
199 }),
200 );
201
202 it.effect("proves the downloaded executable was started by a global npm launcher", () =>
203 Effect.gen(function* () {
204 const fs = yield* FileSystem.FileSystem;
205 const path = yield* Path.Path;
206 const root = yield* fs.makeTempDirectoryScoped();
207 const prefix = path.join(root, "bunx-tools");
208 const packageRoot = path.join(prefix, "lib/node_modules/t3");
209 const launcher = path.join(packageRoot, "bin/t3.mjs");
210 const executable = path.join(root, ".t3/runtime/versions/1.0.0/t3");
211 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
212 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
213 yield* fs.makeDirectory(path.join(prefix, "bin"));
214 yield* fs.writeFileString(launcher, "");
215 yield* fs.writeFileString(executable, "");
216 yield* fs.symlink(launcher, path.join(prefix, "bin/t3"));
217 const resolve = resolveServerInstallation.pipe(
218 Effect.provideService(HostProcess.ExecutablePath, executable),
219 Effect.provideService(HostProcess.Arguments, [executable, executable]),
220 Effect.provideService(HostProcess.IsExecutable, true),
221 Effect.provideService(HostProcess.Platform, "linux"),
222 Effect.provideService(HostProcess.Environment, { T3CODE_NPM_LAUNCHER_PATH: launcher }),
223 );
224 for (const [version, expected] of [
225 ["0.0.1", null],
226 [packageJson.version, { kind: "npm-global", prefix }],
227 ] as const) {
228 yield* fs.writeFileString(
229 path.join(packageRoot, "package.json"),
230 `{"name":"t3","version":"${version}","bin":{"t3":"./bin/t3.mjs"}}`,
231 );
232 expect(yield* resolve).toEqual(expected);
233 }
234 }),
235 );
236
237 it.effect("leaves local, standalone, missing and unreadable installs unknown", () =>
238 Effect.gen(function* () {
239 const fs = yield* FileSystem.FileSystem;
240 const path = yield* Path.Path;
241 const root = yield* fs.makeTempDirectoryScoped();
242 for (const relative of [
243 "project/node_modules/t3/dist/bin.mjs",
244 "project/apps/server/dist/bin.mjs",
245 ".t3/runtime/0.0.45/t3",
246 "missing/dist/bin.mjs",
247 ]) {
248 const entry = path.join(root, relative);
249 if (!relative.startsWith("missing")) {
250 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
251 yield* fs.writeFileString(entry, "");
252 }
253 expect(
254 yield* resolveServerInstallation.pipe(
255 Effect.provideService(HostProcess.Arguments, ["node", entry]),
256 Effect.provideService(HostProcess.IsExecutable, false),
257 ),
258 ).toBeNull();
259 }
260 }),
261 );
262});