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

invocation.test.ts

272 lines12.1 KB

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