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