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

invocation.test.ts

359 lines14.8 KB

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