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

invocation.test.ts

306 lines11.9 KB

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