Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, 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 {
9 formatCliCommand,
10 resolveHostCliCommand,
11 resolveRootCliCommand,
12 resolveServerInstallation,
13} from "./invocation.ts";
14
15it("formats package runner commands from their cache entry paths", () => {
16 for (const [entryPath, expected] of [
17 ["/home/theo/.npm/_npx/abc123/node_modules/t3/dist/bin.mjs", "npx t3 serve"],
18 [
19 "C:\\Users\\theo\\AppData\\Local\\npm-cache\\_npx\\abc\\node_modules\\t3\\dist\\bin.mjs",
20 "npx t3 serve",
21 ],
22 ["/home/theo/.cache/pnpm/dlx/abc/node_modules/t3/dist/bin.mjs", "pnpm dlx t3 serve"],
23 [
24 "/home/theo/.local/share/pnpm/.pnpm/dlx/abc/node_modules/t3/dist/bin.mjs",
25 "pnpm dlx t3 serve",
26 ],
27 [
28 "C:\\Users\\theo\\AppData\\Local\\pnpm-cache\\dlx\\abc\\node_modules\\t3\\dist\\bin.mjs",
29 "pnpm dlx t3 serve",
30 ],
31 ["/home/theo/.bun/install/cache/t3@0.0.31/dist/bin.mjs", "bunx t3 serve"],
32 ["/tmp/bunx-1000-t3@latest/node_modules/t3/dist/bin.mjs", "bunx t3 serve"],
33 [
34 "C:\\Users\\theo\\AppData\\Local\\Temp\\bunx-0-t3@latest\\node_modules\\t3\\dist\\bin.mjs",
35 "bunx t3 serve",
36 ],
37 ] as const) {
38 assert.equal(formatCliCommand({ subcommand: "serve", entryPath, version: "0.0.31" }), expected);
39 }
40});
41
42it("treats stable installs as direct invocations", () => {
43 for (const entryPath of [
44 "/usr/local/lib/node_modules/t3/dist/bin.mjs",
45 "/home/theo/Code/work/t3code/apps/server/dist/bin.mjs",
46 "/home/theo/.t3/runtime/0.0.31/node_modules/t3/dist/bin.mjs",
47 "",
48 ]) {
49 assert.equal(
50 formatCliCommand({ subcommand: "serve", entryPath, version: "0.0.31" }),
51 "t3 serve",
52 );
53 }
54});
55
56it("re-suggests the prerelease channel only for prerelease builds", () => {
57 for (const [version, expected] of [
58 ["0.0.31-nightly.20260729", "npx t3@nightly serve"],
59 ["0.0.31-preview.20260729.1", "npx t3@preview serve"],
60 ["0.0.31-foo-preview.20260729.1", "npx t3 serve"],
61 ["0.0.31", "npx t3 serve"],
62 ] as const) {
63 assert.equal(
64 formatCliCommand({
65 subcommand: "serve",
66 entryPath: "/home/theo/.npm/_npx/abc123/node_modules/t3/dist/bin.mjs",
67 version,
68 }),
69 expected,
70 );
71 }
72});
73
74it("formats serve suggestions to match the launching command", () => {
75 assert.equal(
76 formatCliCommand({
77 subcommand: "serve",
78 entryPath: "/home/theo/.npm/_npx/abc/node_modules/t3/dist/bin.mjs",
79 version: "0.0.31-nightly.20260729",
80 }),
81 "npx t3@nightly serve",
82 );
83 assert.equal(
84 formatCliCommand({
85 subcommand: "serve",
86 entryPath: "/tmp/bunx-1000-t3@latest/node_modules/t3/dist/bin.mjs",
87 version: "0.0.31",
88 }),
89 "bunx t3 serve",
90 );
91 assert.equal(
92 formatCliCommand({
93 subcommand: "serve",
94 entryPath: "/usr/local/lib/node_modules/t3/dist/bin.mjs",
95 version: "0.0.31-nightly.20260729",
96 }),
97 "t3 serve",
98 );
99});
100
101it.layer(NodeServices.layer)("root CLI commands", (it) => {
102 /** `sudo t3 browser setup` as this process would render it, with `t3` on PATH or not. */
103 const rootCommand = (input: {
104 readonly node: string;
105 readonly entry: string;
106 readonly path?: string;
107 readonly env?: Record<string, string>;
108 readonly executable?: boolean;
109 }) =>
110 resolveRootCliCommand("browser setup").pipe(
111 Effect.provideService(HostProcess.ExecutablePath, input.node),
112 Effect.provideService(HostProcess.Arguments, [input.node, input.entry]),
113 Effect.provideService(HostProcess.IsExecutable, input.executable ?? false),
114 Effect.provideService(HostProcess.Platform, "linux"),
115 Effect.provideService(HostProcess.Environment, { PATH: input.path ?? "", ...input.env }),
116 );
117
118 /** A directory holding an executable `t3`, to stand in for one on PATH. */
119 const pathWithT3 = Effect.gen(function* () {
120 const fs = yield* FileSystem.FileSystem;
121 const path = yield* Path.Path;
122 const bin = yield* fs.makeTempDirectoryScoped();
123 yield* fs.writeFileString(path.join(bin, "t3"), "#!/bin/sh\n", { mode: 0o755 });
124 return bin;
125 });
126
127 it.effect("keeps a user-installed Node reachable when the command runs under sudo", () =>
128 Effect.gen(function* () {
129 const npx = "/home/theo/.npm/_npx/abc/node_modules/t3/dist/bin.mjs";
130 // sudo's secure_path already has a system Node.
131 expect(yield* rootCommand({ node: "/usr/bin/node", entry: npx })).toBe(
132 "sudo npx t3 browser setup",
133 );
134 // nvm, fnm, and tarball installs are dropped by sudo's PATH reset.
135 expect(
136 yield* rootCommand({ node: "/home/theo/.nvm/versions/node/v24/bin/node", entry: npx }),
137 ).toBe('sudo env "PATH=$PATH" npx t3 browser setup');
138 expect(
139 yield* rootCommand({
140 node: "/home/theo/.local/node/bin/node",
141 entry: "/home/theo/.local/lib/node_modules/t3/dist/bin.mjs",
142 path: yield* pathWithT3,
143 }),
144 ).toBe('sudo env "PATH=$PATH" t3 browser setup');
145 }).pipe(Effect.scoped),
146 );
147
148 it.effect("names this install's launcher when t3 is not on PATH", () =>
149 Effect.gen(function* () {
150 const fs = yield* FileSystem.FileSystem;
151 const path = yield* Path.Path;
152 const home = yield* fs.makeTempDirectoryScoped();
153 const shim = path.join(home, ".local bin", "t3");
154 yield* fs.makeDirectory(path.dirname(shim), { recursive: true });
155 yield* fs.writeFileString(shim, "#!/bin/sh\n", { mode: 0o755 });
156 const desktop = {
157 node: "/tmp/.mount_T3abc/t3code",
158 entry: "/tmp/.mount_T3abc/resources/app.asar/apps/server/dist/bin.mjs",
159 env: { T3CODE_CLI_PATH: shim },
160 };
161 // The desktop app's shim, quoted for the shell and run as root as is.
162 expect(yield* rootCommand(desktop)).toBe(`sudo '${shim}' browser setup`);
163 // A `t3` the person put on PATH still wins.
164 expect(yield* rootCommand({ ...desktop, path: yield* pathWithT3 })).toBe(
165 'sudo env "PATH=$PATH" t3 browser setup',
166 );
167 // A standalone binary names itself.
168 expect(
169 yield* rootCommand({
170 node: "/opt/t3/t3",
171 entry: "/opt/t3/t3",
172 executable: true,
173 }),
174 ).toBe("sudo /opt/t3/t3 browser setup");
175 // A stale shim path, then no launcher at all, fall back to plain `t3`.
176 expect(
177 yield* rootCommand({ ...desktop, env: { T3CODE_CLI_PATH: path.join(home, "gone") } }),
178 ).toBe('sudo env "PATH=$PATH" t3 browser setup');
179 }).pipe(Effect.scoped),
180 );
181});
182
183it.layer(NodeServices.layer)("host CLI commands", (it) => {
184 it.effect("renders a PowerShell command line on Windows", () =>
185 Effect.gen(function* () {
186 const fs = yield* FileSystem.FileSystem;
187 const path = yield* Path.Path;
188 const home = yield* fs.makeTempDirectoryScoped();
189 const shim = path.join(home, "t3.cmd");
190 yield* fs.writeFileString(shim, "@echo off\r\n");
191 const hostCommand = (platform: NodeJS.Platform, env: Record<string, string>) =>
192 resolveHostCliCommand("doctor", ["--base-dir", "C:\\Users\\Jane Doe\\.t3"]).pipe(
193 Effect.provideService(HostProcess.ExecutablePath, "C:\\Program Files\\T3 Code\\t3.exe"),
194 Effect.provideService(HostProcess.Arguments, [
195 "C:\\Program Files\\T3 Code\\t3.exe",
196 "C:\\Program Files\\T3 Code\\resources\\app.asar\\apps\\server\\dist\\bin.mjs",
197 ]),
198 Effect.provideService(HostProcess.IsExecutable, false),
199 Effect.provideService(HostProcess.Platform, platform),
200 Effect.provideService(HostProcess.Environment, { PATH: "", ...env }),
201 );
202 // The desktop shim is called through `&`, and each argument is quoted
203 // the PowerShell way: no POSIX single quotes anywhere.
204 expect((yield* hostCommand("win32", { T3CODE_CLI_PATH: shim })).command).toBe(
205 `& "${shim}" doctor "--base-dir" "C:\\Users\\Jane Doe\\.t3"`,
206 );
207 // Without a launcher, plain `t3` with PowerShell-quoted arguments.
208 expect((yield* hostCommand("win32", {})).command).toBe(
209 't3 doctor "--base-dir" "C:\\Users\\Jane Doe\\.t3"',
210 );
211 // Typographic quotes also delimit PowerShell strings; doubling keeps them.
212 expect(
213 (yield* resolveHostCliCommand("doctor", ["--base-dir", "C:\\T3\\\u201CArchive\u201D"]).pipe(
214 Effect.provideService(HostProcess.Platform, "win32"),
215 Effect.provideService(HostProcess.Environment, { PATH: "" }),
216 Effect.provideService(HostProcess.ExecutablePath, "C:\\t3.exe"),
217 Effect.provideService(HostProcess.Arguments, ["C:\\t3.exe", "C:\\bin.mjs"]),
218 Effect.provideService(HostProcess.IsExecutable, false),
219 )).command,
220 ).toBe('t3 doctor "--base-dir" "C:\\T3\\\u201C\u201CArchive\u201D\u201D"');
221 // POSIX hosts keep single quotes for the same input. The shim prefix is
222 // a host path, so only the arguments are asserted.
223 expect((yield* hostCommand("linux", { T3CODE_CLI_PATH: shim })).command).toMatch(
224 / doctor --base-dir 'C:\\Users\\Jane Doe\\\.t3'$/,
225 );
226 }).pipe(Effect.scoped),
227 );
228});
229
230it.layer(NodeServices.layer)("manual server installation ownership", (it) => {
231 it.effect("recognizes runner caches for both script and executable packages", () =>
232 Effect.gen(function* () {
233 const fs = yield* FileSystem.FileSystem;
234 const path = yield* Path.Path;
235 const root = yield* fs.makeTempDirectoryScoped();
236 for (const [relative, kind] of [
237 ["npm/_npx/hash/node_modules/t3/dist/bin.mjs", "npx"],
238 ["npm/_npx/hash/node_modules/@t3code/t3-linux-x64/t3", "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 const installation = yield* resolveServerInstallation.pipe(
246 Effect.provideService(HostProcess.Arguments, ["node", entry]),
247 Effect.provideService(HostProcess.ExecutablePath, entry),
248 Effect.provideService(HostProcess.IsExecutable, entry.endsWith("/t3")),
249 );
250 expect(installation).toEqual({ kind });
251 }
252 }),
253 );
254
255 it.effect("requires the npm prefix's bin to point to the running package", () =>
256 Effect.gen(function* () {
257 const fs = yield* FileSystem.FileSystem;
258 const path = yield* Path.Path;
259 const root = yield* fs.makeTempDirectoryScoped();
260 const prefix = path.join(root, "bunx-tools");
261 const packageRoot = path.join(prefix, "lib/node_modules/t3");
262 const entry = path.join(packageRoot, "dist/bin.mjs");
263 const globalBin = path.join(prefix, "bin/t3");
264 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
265 yield* fs.makeDirectory(path.dirname(globalBin), { recursive: true });
266 yield* fs.writeFileString(entry, "");
267 yield* fs.writeFileString(
268 path.join(packageRoot, "package.json"),
269 '{"name":"t3","version":"0.0.45","bin":{"t3":"./dist/bin.mjs"}}',
270 );
271 const resolve = resolveServerInstallation.pipe(
272 Effect.provideService(HostProcess.Arguments, ["node", entry]),
273 Effect.provideService(HostProcess.IsExecutable, false),
274 Effect.provideService(HostProcess.Platform, "linux"),
275 );
276 expect(yield* resolve).toBeNull();
277 yield* fs.symlink(entry, globalBin);
278 expect(yield* resolve).toEqual({ kind: "npm-global", prefix });
279 yield* fs.remove(globalBin);
280 yield* fs.writeFileString(globalBin, "an unrelated t3 command");
281 expect(yield* resolve).toBeNull();
282 expect(yield* resolve.pipe(Effect.provideService(HostProcess.Platform, "win32"))).toBeNull();
283 }),
284 );
285
286 it.effect("proves the native executable belongs to the npm launcher", () =>
287 Effect.gen(function* () {
288 const fs = yield* FileSystem.FileSystem;
289 const path = yield* Path.Path;
290 const root = yield* fs.makeTempDirectoryScoped();
291 const prefix = path.join(root, "bunx-tools");
292 const packageRoot = path.join(prefix, "lib/node_modules/t3");
293 const launcher = path.join(packageRoot, "bin/t3.js");
294 const entry = path.join(packageRoot, "node_modules/@t3code/t3-linux-x64/t3");
295 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
296 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
297 yield* fs.makeDirectory(path.join(prefix, "bin"));
298 yield* fs.writeFileString(launcher, "");
299 yield* fs.writeFileString(entry, "");
300 yield* fs.writeFileString(
301 path.join(packageRoot, "package.json"),
302 '{"name":"t3","version":"0.0.45","bin":{"t3":"./bin/t3.js"},"optionalDependencies":{"@t3code/t3-linux-x64":"0.0.45"}}',
303 );
304 yield* fs.symlink(launcher, path.join(prefix, "bin/t3"));
305 const resolve = resolveServerInstallation.pipe(
306 Effect.provideService(HostProcess.ExecutablePath, entry),
307 Effect.provideService(HostProcess.IsExecutable, true),
308 Effect.provideService(HostProcess.Platform, "linux"),
309 );
310 for (const [version, expected] of [
311 ["0.0.44", null],
312 ["0.0.45", { kind: "npm-global", prefix }],
313 ]) {
314 yield* fs.writeFileString(
315 path.join(path.dirname(entry), "package.json"),
316 `{"name":"@t3code/t3-linux-x64","version":"${version}"}`,
317 );
318 expect(yield* resolve).toEqual(expected);
319 }
320 }),
321 );
322
323 it.effect("leaves local, standalone, missing and unreadable installs unknown", () =>
324 Effect.gen(function* () {
325 const fs = yield* FileSystem.FileSystem;
326 const path = yield* Path.Path;
327 const root = yield* fs.makeTempDirectoryScoped();
328 for (const relative of [
329 "project/node_modules/t3/dist/bin.mjs",
330 "project/apps/server/dist/bin.mjs",
331 ".t3/runtime/0.0.45/t3",
332 "missing/dist/bin.mjs",
333 ]) {
334 const entry = path.join(root, relative);
335 if (!relative.startsWith("missing")) {
336 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
337 yield* fs.writeFileString(entry, "");
338 }
339 expect(
340 yield* resolveServerInstallation.pipe(
341 Effect.provideService(HostProcess.Arguments, ["node", entry]),
342 Effect.provideService(HostProcess.IsExecutable, false),
343 ),
344 ).toBeNull();
345 }
346 }),
347 );
348});