Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, expect, it } from "@effect/vitest";
3import {
4 HostProcessArguments,
5 HostProcessExecutablePath,
6 HostProcessIsExecutable,
7 HostProcessPlatform,
8} from "@t3tools/shared/hostProcess";
9import * as Effect from "effect/Effect";
10import * as FileSystem from "effect/FileSystem";
11import * as Path from "effect/Path";
12
13import {
14 formatCliCommand,
15 resolveRootCliCommand,
16 resolveServerInstallation,
17} from "./invocation.ts";
18
19it("formats package runner commands from their cache entry paths", () => {
20 for (const [entryPath, expected] of [
21 ["/home/theo/.npm/_npx/abc123/node_modules/t3/dist/bin.mjs", "npx t3 serve"],
22 [
23 "C:\\Users\\theo\\AppData\\Local\\npm-cache\\_npx\\abc\\node_modules\\t3\\dist\\bin.mjs",
24 "npx t3 serve",
25 ],
26 ["/home/theo/.cache/pnpm/dlx/abc/node_modules/t3/dist/bin.mjs", "pnpm dlx t3 serve"],
27 [
28 "/home/theo/.local/share/pnpm/.pnpm/dlx/abc/node_modules/t3/dist/bin.mjs",
29 "pnpm dlx t3 serve",
30 ],
31 [
32 "C:\\Users\\theo\\AppData\\Local\\pnpm-cache\\dlx\\abc\\node_modules\\t3\\dist\\bin.mjs",
33 "pnpm dlx t3 serve",
34 ],
35 ["/home/theo/.bun/install/cache/t3@0.0.31/dist/bin.mjs", "bunx t3 serve"],
36 ["/tmp/bunx-1000-t3@latest/node_modules/t3/dist/bin.mjs", "bunx t3 serve"],
37 [
38 "C:\\Users\\theo\\AppData\\Local\\Temp\\bunx-0-t3@latest\\node_modules\\t3\\dist\\bin.mjs",
39 "bunx t3 serve",
40 ],
41 ] as const) {
42 assert.equal(formatCliCommand({ subcommand: "serve", entryPath, version: "0.0.31" }), expected);
43 }
44});
45
46it("treats stable installs as direct invocations", () => {
47 for (const entryPath of [
48 "/usr/local/lib/node_modules/t3/dist/bin.mjs",
49 "/home/theo/Code/work/t3code/apps/server/dist/bin.mjs",
50 "/home/theo/.t3/runtime/0.0.31/node_modules/t3/dist/bin.mjs",
51 "",
52 ]) {
53 assert.equal(
54 formatCliCommand({ subcommand: "serve", entryPath, version: "0.0.31" }),
55 "t3 serve",
56 );
57 }
58});
59
60it("re-suggests the prerelease channel only for prerelease builds", () => {
61 for (const [version, expected] of [
62 ["0.0.31-nightly.20260729", "npx t3@nightly serve"],
63 ["0.0.31-preview.20260729.1", "npx t3@preview serve"],
64 ["0.0.31-foo-preview.20260729.1", "npx t3 serve"],
65 ["0.0.31", "npx t3 serve"],
66 ] as const) {
67 assert.equal(
68 formatCliCommand({
69 subcommand: "serve",
70 entryPath: "/home/theo/.npm/_npx/abc123/node_modules/t3/dist/bin.mjs",
71 version,
72 }),
73 expected,
74 );
75 }
76});
77
78it("formats serve suggestions to match the launching command", () => {
79 assert.equal(
80 formatCliCommand({
81 subcommand: "serve",
82 entryPath: "/home/theo/.npm/_npx/abc/node_modules/t3/dist/bin.mjs",
83 version: "0.0.31-nightly.20260729",
84 }),
85 "npx t3@nightly serve",
86 );
87 assert.equal(
88 formatCliCommand({
89 subcommand: "serve",
90 entryPath: "/tmp/bunx-1000-t3@latest/node_modules/t3/dist/bin.mjs",
91 version: "0.0.31",
92 }),
93 "bunx t3 serve",
94 );
95 assert.equal(
96 formatCliCommand({
97 subcommand: "serve",
98 entryPath: "/usr/local/lib/node_modules/t3/dist/bin.mjs",
99 version: "0.0.31-nightly.20260729",
100 }),
101 "t3 serve",
102 );
103});
104
105it.effect("keeps a user-installed Node reachable when the command runs under sudo", () =>
106 Effect.gen(function* () {
107 const command = (node: string, entry: string) =>
108 resolveRootCliCommand("browser setup").pipe(
109 Effect.provideService(HostProcessExecutablePath, node),
110 Effect.provideService(HostProcessArguments, [node, entry]),
111 );
112 const npx = "/home/theo/.npm/_npx/abc/node_modules/t3/dist/bin.mjs";
113 // sudo's secure_path already has a system Node.
114 expect(yield* command("/usr/bin/node", npx)).toBe("sudo npx t3 browser setup");
115 // nvm, fnm, and tarball installs are dropped by sudo's PATH reset.
116 expect(yield* command("/home/theo/.nvm/versions/node/v24/bin/node", npx)).toBe(
117 'sudo env "PATH=$PATH" npx t3 browser setup',
118 );
119 expect(
120 yield* command(
121 "/home/theo/.local/node/bin/node",
122 "/home/theo/.local/lib/node_modules/t3/dist/bin.mjs",
123 ),
124 ).toBe('sudo env "PATH=$PATH" t3 browser setup');
125 }),
126);
127
128it.layer(NodeServices.layer)("manual server installation ownership", (it) => {
129 it.effect("recognizes runner caches for both script and executable packages", () =>
130 Effect.gen(function* () {
131 const fs = yield* FileSystem.FileSystem;
132 const path = yield* Path.Path;
133 const root = yield* fs.makeTempDirectoryScoped();
134 for (const [relative, kind] of [
135 ["npm/_npx/hash/node_modules/t3/dist/bin.mjs", "npx"],
136 ["npm/_npx/hash/node_modules/@t3code/t3-linux-x64/t3", "npx"],
137 ["pnpm/dlx/hash/node_modules/t3/dist/bin.mjs", "pnpm-dlx"],
138 [".bun/install/cache/t3/dist/bin.mjs", "bunx"],
139 ] as const) {
140 const entry = path.join(root, relative);
141 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
142 yield* fs.writeFileString(entry, "");
143 const installation = yield* resolveServerInstallation.pipe(
144 Effect.provideService(HostProcessArguments, ["node", entry]),
145 Effect.provideService(HostProcessExecutablePath, entry),
146 Effect.provideService(HostProcessIsExecutable, entry.endsWith("/t3")),
147 );
148 expect(installation).toEqual({ kind });
149 }
150 }),
151 );
152
153 it.effect("requires the npm prefix's bin to point to the running package", () =>
154 Effect.gen(function* () {
155 const fs = yield* FileSystem.FileSystem;
156 const path = yield* Path.Path;
157 const root = yield* fs.makeTempDirectoryScoped();
158 const prefix = path.join(root, "bunx-tools");
159 const packageRoot = path.join(prefix, "lib/node_modules/t3");
160 const entry = path.join(packageRoot, "dist/bin.mjs");
161 const globalBin = path.join(prefix, "bin/t3");
162 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
163 yield* fs.makeDirectory(path.dirname(globalBin), { recursive: true });
164 yield* fs.writeFileString(entry, "");
165 yield* fs.writeFileString(
166 path.join(packageRoot, "package.json"),
167 '{"name":"t3","version":"0.0.45","bin":{"t3":"./dist/bin.mjs"}}',
168 );
169 const resolve = resolveServerInstallation.pipe(
170 Effect.provideService(HostProcessArguments, ["node", entry]),
171 Effect.provideService(HostProcessIsExecutable, false),
172 Effect.provideService(HostProcessPlatform, "linux"),
173 );
174 expect(yield* resolve).toBeNull();
175 yield* fs.symlink(entry, globalBin);
176 expect(yield* resolve).toEqual({ kind: "npm-global", prefix });
177 yield* fs.remove(globalBin);
178 yield* fs.writeFileString(globalBin, "an unrelated t3 command");
179 expect(yield* resolve).toBeNull();
180 expect(yield* resolve.pipe(Effect.provideService(HostProcessPlatform, "win32"))).toBeNull();
181 }),
182 );
183
184 it.effect("proves the native executable belongs to the npm launcher", () =>
185 Effect.gen(function* () {
186 const fs = yield* FileSystem.FileSystem;
187 const path = yield* Path.Path;
188 const root = yield* fs.makeTempDirectoryScoped();
189 const prefix = path.join(root, "bunx-tools");
190 const packageRoot = path.join(prefix, "lib/node_modules/t3");
191 const launcher = path.join(packageRoot, "bin/t3.js");
192 const entry = path.join(packageRoot, "node_modules/@t3code/t3-linux-x64/t3");
193 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
194 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
195 yield* fs.makeDirectory(path.join(prefix, "bin"));
196 yield* fs.writeFileString(launcher, "");
197 yield* fs.writeFileString(entry, "");
198 yield* fs.writeFileString(
199 path.join(packageRoot, "package.json"),
200 '{"name":"t3","version":"0.0.45","bin":{"t3":"./bin/t3.js"},"optionalDependencies":{"@t3code/t3-linux-x64":"0.0.45"}}',
201 );
202 yield* fs.symlink(launcher, path.join(prefix, "bin/t3"));
203 const resolve = resolveServerInstallation.pipe(
204 Effect.provideService(HostProcessExecutablePath, entry),
205 Effect.provideService(HostProcessIsExecutable, true),
206 Effect.provideService(HostProcessPlatform, "linux"),
207 );
208 for (const [version, expected] of [
209 ["0.0.44", null],
210 ["0.0.45", { kind: "npm-global", prefix }],
211 ]) {
212 yield* fs.writeFileString(
213 path.join(path.dirname(entry), "package.json"),
214 `{"name":"@t3code/t3-linux-x64","version":"${version}"}`,
215 );
216 expect(yield* resolve).toEqual(expected);
217 }
218 }),
219 );
220
221 it.effect("leaves local, standalone, missing and unreadable installs unknown", () =>
222 Effect.gen(function* () {
223 const fs = yield* FileSystem.FileSystem;
224 const path = yield* Path.Path;
225 const root = yield* fs.makeTempDirectoryScoped();
226 for (const relative of [
227 "project/node_modules/t3/dist/bin.mjs",
228 "project/apps/server/dist/bin.mjs",
229 ".t3/runtime/0.0.45/t3",
230 "missing/dist/bin.mjs",
231 ]) {
232 const entry = path.join(root, relative);
233 if (!relative.startsWith("missing")) {
234 yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
235 yield* fs.writeFileString(entry, "");
236 }
237 expect(
238 yield* resolveServerInstallation.pipe(
239 Effect.provideService(HostProcessArguments, ["node", entry]),
240 Effect.provideService(HostProcessIsExecutable, false),
241 ),
242 ).toBeNull();
243 }
244 }),
245 );
246});