@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 371 lines 16.5 KB
Raw
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { expect, it } from "@effect/vitest";
3 import * as HostProcess from "@t3tools/shared/HostProcess";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Path from "effect/Path";
7
8 import packageJson from "../../package.json" with { type: "json" };
9 import { posixLauncherScript } from "../cloud/pinnedRuntime.ts";
10 import {
11 resolveHostCliCommand,
12 resolveRootCliCommand,
13 resolveServerInstallation,
14 } from "./invocation.ts";
15
16 it.layer(NodeServices.layer)("root CLI commands", (it) => {
17 const rootCommand = (input: {
18 readonly node: string;
19 readonly entry: string;
20 readonly path?: string;
21 readonly env?: Record<string, string>;
22 readonly executable?: boolean;
23 readonly args?: ReadonlyArray<string>;
24 }) =>
25 resolveRootCliCommand("browser setup", input.args).pipe(
26 Effect.provideService(HostProcess.ExecutablePath, input.node),
27 Effect.provideService(HostProcess.Arguments, [input.node, input.entry]),
28 Effect.provideService(HostProcess.IsExecutable, input.executable ?? false),
29 Effect.provideService(HostProcess.Platform, "linux"),
30 Effect.provideService(HostProcess.Environment, { PATH: input.path ?? "", ...input.env }),
31 );
32
33 const pathWithT3 = Effect.gen(function* () {
34 const fs = yield* FileSystem.FileSystem;
35 const path = yield* Path.Path;
36 const bin = yield* fs.makeTempDirectoryScoped();
37 yield* fs.writeFileString(path.join(bin, "t3"), "#!/bin/sh\n", { mode: 0o755 });
38 return bin;
39 });
40
41 it.effect("keeps a user-installed Node reachable when the command runs under sudo", () =>
42 Effect.gen(function* () {
43 const npx = "/home/theo/.npm/_npx/abc/node_modules/t3/dist/bin.mjs";
44 expect(yield* rootCommand({ node: "/usr/bin/node", entry: npx })).toBe(
45 "sudo npx t3 browser setup",
46 );
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 }),
58 );
59
60 it.effect("keeps the arguments, so root runs setup on the same T3 home", () =>
61 Effect.gen(function* () {
62 const args = ["--base-dir", "/srv/t3 home"];
63 expect(
64 yield* rootCommand({
65 node: "/home/theo/.local/node/bin/node",
66 entry: "/home/theo/.local/lib/node_modules/t3/dist/bin.mjs",
67 path: yield* pathWithT3,
68 args,
69 }),
70 ).toBe(`sudo env "PATH=$PATH" t3 browser setup --base-dir '/srv/t3 home'`);
71 expect(
72 yield* rootCommand({ node: "/opt/t3/t3", entry: "/opt/t3/t3", executable: true, args }),
73 ).toBe("sudo /opt/t3/t3 browser setup --base-dir '/srv/t3 home'");
74 }),
75 );
76
77 it.effect("names this install's launcher when t3 is not on PATH", () =>
78 Effect.gen(function* () {
79 const fs = yield* FileSystem.FileSystem;
80 const path = yield* Path.Path;
81 const home = yield* fs.makeTempDirectoryScoped();
82 const shim = path.join(home, ".local bin", "t3");
83 yield* fs.makeDirectory(path.dirname(shim), { recursive: true });
84 yield* fs.writeFileString(shim, "#!/bin/sh\n", { mode: 0o755 });
85 const desktop = {
86 node: "/tmp/.mount_T3abc/t3code",
87 entry: "/tmp/.mount_T3abc/resources/app.asar/apps/server/dist/bin.mjs",
88 env: { T3CODE_CLI_PATH: shim },
89 };
90
91 expect(yield* rootCommand(desktop)).toBe(`sudo '${shim}' browser setup`);
92
93 // An unrelated `t3` on PATH, such as an older npm global, is another install's CLI.
94 expect(yield* rootCommand({ ...desktop, path: yield* pathWithT3 })).toBe(
95 `sudo '${shim}' browser setup`,
96 );
97
98 // The link the app installs onto PATH leads to its own launcher, so plain `t3` is right.
99 const linked = yield* fs.makeTempDirectoryScoped();
100 yield* fs.symlink(shim, path.join(linked, "t3"));
101 expect(yield* rootCommand({ ...desktop, path: linked })).toBe(
102 'sudo env "PATH=$PATH" t3 browser setup',
103 );
104
105 expect(
106 yield* rootCommand({
107 node: "/opt/t3/t3",
108 entry: "/opt/t3/t3",
109 executable: true,
110 }),
111 ).toBe("sudo /opt/t3/t3 browser setup");
112
113 // A standalone executable started directly (systemd, no launcher) is still this install.
114 expect(
115 yield* rootCommand({
116 node: "/opt/t3/t3",
117 entry: "/opt/t3/t3",
118 executable: true,
119 path: yield* pathWithT3,
120 }),
121 ).toBe("sudo /opt/t3/t3 browser setup");
122
123 // A global npm launcher runs the downloaded executable; PATH's `t3` is that launcher.
124 const prefix = path.join(home, "npm");
125 const npmLauncher = path.join(prefix, "lib/node_modules/t3/bin/t3.mjs");
126 yield* fs.makeDirectory(path.dirname(npmLauncher), { recursive: true });
127 yield* fs.makeDirectory(path.join(prefix, "bin"));
128 yield* fs.writeFileString(npmLauncher, "", { mode: 0o755 });
129 yield* fs.writeFileString(
130 path.join(prefix, "lib/node_modules/t3/package.json"),
131 `{"name":"t3","version":"${packageJson.version}"}`,
132 );
133 yield* fs.symlink(npmLauncher, path.join(prefix, "bin/t3"));
134 expect(
135 yield* rootCommand({
136 node: "/home/theo/.t3/runtime/versions/1.0.0/t3",
137 entry: "/home/theo/.t3/runtime/versions/1.0.0/t3",
138 executable: true,
139 path: path.join(prefix, "bin"),
140 env: { T3CODE_NPM_LAUNCHER_PATH: npmLauncher },
141 }),
142 ).toBe('sudo env "PATH=$PATH" t3 browser setup');
143
144 expect(
145 yield* rootCommand({ ...desktop, env: { T3CODE_CLI_PATH: path.join(home, "gone") } }),
146 ).toBe('sudo env "PATH=$PATH" t3 browser setup');
147 }),
148 );
149 });
150
151 it.layer(NodeServices.layer)("host CLI commands", (it) => {
152 it.effect("renders a PowerShell command line on Windows", () =>
153 Effect.gen(function* () {
154 const fs = yield* FileSystem.FileSystem;
155 const path = yield* Path.Path;
156 const home = yield* fs.makeTempDirectoryScoped();
157 const shim = path.join(home, "t3.cmd");
158 yield* fs.writeFileString(shim, "@echo off\r\n");
159 const hostCommand = (platform: NodeJS.Platform, env: Record<string, string>) =>
160 resolveHostCliCommand("doctor", ["--base-dir", "C:\\Users\\Jane Doe\\.t3"]).pipe(
161 Effect.provideService(HostProcess.ExecutablePath, "C:\\Program Files\\T3 Code\\t3.exe"),
162 Effect.provideService(HostProcess.Arguments, [
163 "C:\\Program Files\\T3 Code\\t3.exe",
164 "C:\\Program Files\\T3 Code\\resources\\app.asar\\apps\\server\\dist\\bin.mjs",
165 ]),
166 Effect.provideService(HostProcess.IsExecutable, false),
167 Effect.provideService(HostProcess.Platform, platform),
168 Effect.provideService(HostProcess.Environment, { PATH: "", ...env }),
169 );
170 expect((yield* hostCommand("win32", { T3CODE_CLI_PATH: shim })).command).toBe(
171 `& "${shim}" doctor "--base-dir" "C:\\Users\\Jane Doe\\.t3"`,
172 );
173 expect((yield* hostCommand("win32", {})).command).toBe(
174 't3 doctor "--base-dir" "C:\\Users\\Jane Doe\\.t3"',
175 );
176 expect(
177 (yield* resolveHostCliCommand("doctor", ["--base-dir", "C:\\T3\\\u201CArchive\u201D"]).pipe(
178 Effect.provideService(HostProcess.Platform, "win32"),
179 Effect.provideService(HostProcess.Environment, { PATH: "" }),
180 Effect.provideService(HostProcess.ExecutablePath, "C:\\t3.exe"),
181 Effect.provideService(HostProcess.Arguments, ["C:\\t3.exe", "C:\\bin.mjs"]),
182 Effect.provideService(HostProcess.IsExecutable, false),
183 )).command,
184 ).toBe('t3 doctor "--base-dir" "C:\\T3\\\u201C\u201CArchive\u201D\u201D"');
185 expect((yield* hostCommand("linux", { T3CODE_CLI_PATH: shim })).command).toMatch(
186 / doctor --base-dir 'C:\\Users\\Jane Doe\\\.t3'$/,
187 );
188 }),
189 );
190 });
191
192 it.layer(NodeServices.layer)("manual server installation ownership", (it) => {
193 it.effect("recognizes runner caches for script packages and the npm launcher", () =>
194 Effect.gen(function* () {
195 const fs = yield* FileSystem.FileSystem;
196 const path = yield* Path.Path;
197 const root = yield* fs.makeTempDirectoryScoped();
198 const executable = path.join(root, ".t3/runtime/versions/1.0.0/t3");
199 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
200 yield* fs.writeFileString(executable, "");
201 for (const [relative, kind] of [
202 ["npm/_npx/hash/node_modules/t3/dist/bin.mjs", "npx"],
203 ["npm/_npx/hash/node_modules/t3/bin/t3.mjs", "npx"],
204 ["pnpm/dlx/hash/node_modules/t3/dist/bin.mjs", "pnpm-dlx"],
205 [".bun/install/cache/t3/dist/bin.mjs", "bunx"],
206 ] as const) {
207 const entry = path.join(root, relative);
208 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
209 yield* fs.writeFileString(entry, "");
210 yield* fs.writeFileString(
211 path.join(path.dirname(entry), "../package.json"),
212 `{"name":"t3","version":"${packageJson.version}","bin":{"t3":"./bin/t3.mjs"}}`,
213 );
214 const launchedBy = entry.endsWith("t3.mjs");
215 const installation = yield* resolveServerInstallation.pipe(
216 Effect.provideService(HostProcess.Arguments, [launchedBy ? executable : "node", entry]),
217 Effect.provideService(HostProcess.ExecutablePath, executable),
218 Effect.provideService(HostProcess.IsExecutable, launchedBy),
219 Effect.provideService(
220 HostProcess.Environment,
221 launchedBy ? { T3CODE_NPM_LAUNCHER_PATH: entry } : {},
222 ),
223 );
224 expect(installation).toEqual({ kind });
225 }
226 }),
227 );
228
229 it.effect("requires the npm prefix's bin to point to the running package", () =>
230 Effect.gen(function* () {
231 const fs = yield* FileSystem.FileSystem;
232 const path = yield* Path.Path;
233 const root = yield* fs.makeTempDirectoryScoped();
234 const prefix = path.join(root, "bunx-tools");
235 const packageRoot = path.join(prefix, "lib/node_modules/t3");
236 const entry = path.join(packageRoot, "dist/bin.mjs");
237 const globalBin = path.join(prefix, "bin/t3");
238 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
239 yield* fs.makeDirectory(path.dirname(globalBin), { recursive: true });
240 yield* fs.writeFileString(entry, "");
241 yield* fs.writeFileString(
242 path.join(packageRoot, "package.json"),
243 '{"name":"t3","version":"0.0.45","bin":{"t3":"./dist/bin.mjs"}}',
244 );
245 const resolve = resolveServerInstallation.pipe(
246 Effect.provideService(HostProcess.Arguments, ["node", entry]),
247 Effect.provideService(HostProcess.IsExecutable, false),
248 Effect.provideService(HostProcess.Platform, "linux"),
249 );
250 expect(yield* resolve).toBeNull();
251 yield* fs.symlink(entry, globalBin);
252 expect(yield* resolve).toEqual({ kind: "npm-global", prefix });
253 yield* fs.remove(globalBin);
254 yield* fs.writeFileString(globalBin, "an unrelated t3 command");
255 expect(yield* resolve).toBeNull();
256 expect(yield* resolve.pipe(Effect.provideService(HostProcess.Platform, "win32"))).toBeNull();
257 }),
258 );
259
260 it.effect("proves the downloaded executable was started by a global npm launcher", () =>
261 Effect.gen(function* () {
262 const fs = yield* FileSystem.FileSystem;
263 const path = yield* Path.Path;
264 const root = yield* fs.makeTempDirectoryScoped();
265 const prefix = path.join(root, "bunx-tools");
266 const packageRoot = path.join(prefix, "lib/node_modules/t3");
267 const launcher = path.join(packageRoot, "bin/t3.mjs");
268 const executable = path.join(root, ".t3/runtime/versions/1.0.0/t3");
269 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
270 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
271 yield* fs.makeDirectory(path.join(prefix, "bin"));
272 yield* fs.writeFileString(launcher, "");
273 yield* fs.writeFileString(executable, "");
274 yield* fs.symlink(launcher, path.join(prefix, "bin/t3"));
275 const resolve = resolveServerInstallation.pipe(
276 Effect.provideService(HostProcess.ExecutablePath, executable),
277 Effect.provideService(HostProcess.Arguments, [executable, executable]),
278 Effect.provideService(HostProcess.IsExecutable, true),
279 Effect.provideService(HostProcess.Platform, "linux"),
280 Effect.provideService(HostProcess.Environment, { T3CODE_NPM_LAUNCHER_PATH: launcher }),
281 );
282 for (const [version, expected] of [
283 ["0.0.1", null],
284 [packageJson.version, { kind: "npm-global", prefix }],
285 ] as const) {
286 yield* fs.writeFileString(
287 path.join(packageRoot, "package.json"),
288 `{"name":"t3","version":"${version}","bin":{"t3":"./bin/t3.mjs"}}`,
289 );
290 expect(yield* resolve).toEqual(expected);
291 }
292 }),
293 );
294
295 it.effect("names the launcher that started a downloaded runtime", () =>
296 Effect.gen(function* () {
297 const fs = yield* FileSystem.FileSystem;
298 const path = yield* Path.Path;
299 const root = yield* fs.realPath(yield* fs.makeTempDirectoryScoped());
300 // The install script's launcher names ~/.t3, which here links to a home elsewhere.
301 const executable = path.join(root, "data/t3/runtime/versions/1.0.0/t3");
302 const launcher = path.join(root, "bin/t3");
303 const copy = path.join(root, "bin/t3-copy");
304 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
305 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
306 yield* fs.writeFileString(executable, "", { mode: 0o755 });
307 yield* fs.writeFileString(copy, "", { mode: 0o755 });
308 yield* fs.symlink(path.join(root, "data/t3"), path.join(root, ".t3"));
309 yield* fs.symlink(path.join(root, ".t3/runtime/versions/1.0.0/t3"), launcher);
310 // Earlier PATH entries hold a directory and a non-executable file named t3.
311 yield* fs.makeDirectory(path.join(root, "shadow-dir/t3"), { recursive: true });
312 yield* fs.makeDirectory(path.join(root, "shadow-file"));
313 yield* fs.writeFileString(path.join(root, "shadow-file/t3"), "");
314 const resolve = (invokedAs: string, PATH = "shadow-dir:shadow-file:bin") =>
315 resolveServerInstallation.pipe(
316 Effect.provideService(HostProcess.ExecutablePath, executable),
317 Effect.provideService(HostProcess.IsExecutable, true),
318 Effect.provideService(HostProcess.InvokedAs, invokedAs),
319 Effect.provideService(HostProcess.WorkingDirectory, root),
320 Effect.provideService(HostProcess.Environment, { PATH }),
321 Effect.provideService(HostProcess.HomeDirectory, root),
322 Effect.provideService(HostProcess.Platform, "linux"),
323 );
324 const baseDir = path.join(root, ".t3");
325
326 expect(yield* resolve("t3")).toEqual({ kind: "t3-launcher", launcher, baseDir });
327 expect(yield* resolve(launcher)).toEqual({ kind: "t3-launcher", launcher, baseDir });
328 expect(yield* resolve(executable)).toBeNull();
329 expect(yield* resolve(copy)).toBeNull();
330
331 // The script launcher execs the runtime by absolute path, so argv0 names the runtime.
332 yield* fs.remove(launcher);
333 yield* fs.writeFileString(
334 launcher,
335 posixLauncherScript(path.join(root, ".t3/runtime/versions/1.0.0/t3")),
336 { mode: 0o755 },
337 );
338 expect(yield* resolve(executable, path.join(root, "bin"))).toEqual({
339 kind: "t3-launcher",
340 launcher,
341 baseDir,
342 });
343 }),
344 );
345
346 it.effect("leaves local, standalone, missing and unreadable installs unknown", () =>
347 Effect.gen(function* () {
348 const fs = yield* FileSystem.FileSystem;
349 const path = yield* Path.Path;
350 const root = yield* fs.makeTempDirectoryScoped();
351 for (const relative of [
352 "project/node_modules/t3/dist/bin.mjs",
353 "project/apps/server/dist/bin.mjs",
354 ".t3/runtime/0.0.45/t3",
355 "missing/dist/bin.mjs",
356 ]) {
357 const entry = path.join(root, relative);
358 if (!relative.startsWith("missing")) {
359 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
360 yield* fs.writeFileString(entry, "");
361 }
362 expect(
363 yield* resolveServerInstallation.pipe(
364 Effect.provideService(HostProcess.Arguments, ["node", entry]),
365 Effect.provideService(HostProcess.IsExecutable, false),
366 ),
367 ).toBeNull();
368 }
369 }),
370 );
371 });