Repository navigation
Expand file tree
/
Copy pathinvocation.test.ts
More file actions
288 lines (272 loc) · 12.6 KB
/
Copy pathinvocation.test.ts
File metadata and controls
288 lines (272 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import * as NodeServices from "@effect/platform-node/NodeServices";
import { expect, it } from "@effect/vitest";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import packageJson from "../../package.json" with { type: "json" };
import {
resolveHostCliCommand,
resolveRootCliCommand,
resolveServerInstallation,
} from "./invocation.ts";
it.layer(NodeServices.layer)("root CLI commands", (it) => {
const rootCommand = (input: {
readonly node: string;
readonly entry: string;
readonly path?: string;
readonly env?: Record<string, string>;
readonly executable?: boolean;
readonly args?: ReadonlyArray<string>;
}) =>
resolveRootCliCommand("browser setup", input.args).pipe(
Effect.provideService(HostProcess.ExecutablePath, input.node),
Effect.provideService(HostProcess.Arguments, [input.node, input.entry]),
Effect.provideService(HostProcess.IsExecutable, input.executable ?? false),
Effect.provideService(HostProcess.Platform, "linux"),
Effect.provideService(HostProcess.Environment, { PATH: input.path ?? "", ...input.env }),
);
const pathWithT3 = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const bin = yield* fs.makeTempDirectoryScoped();
yield* fs.writeFileString(path.join(bin, "t3"), "#!/bin/sh\n", { mode: 0o755 });
return bin;
});
it.effect("keeps a user-installed Node reachable when the command runs under sudo", () =>
Effect.gen(function* () {
const npx = "/home/theo/.npm/_npx/abc/node_modules/t3/dist/bin.mjs";
expect(yield* rootCommand({ node: "/usr/bin/node", entry: npx })).toBe(
"sudo npx t3 browser setup",
);
expect(
yield* rootCommand({ node: "/home/theo/.nvm/versions/node/v24/bin/node", entry: npx }),
).toBe('sudo env "PATH=$PATH" npx t3 browser setup');
expect(
yield* rootCommand({
node: "/home/theo/.local/node/bin/node",
entry: "/home/theo/.local/lib/node_modules/t3/dist/bin.mjs",
path: yield* pathWithT3,
}),
).toBe('sudo env "PATH=$PATH" t3 browser setup');
}).pipe(Effect.scoped),
);
it.effect("keeps the arguments, so root runs setup on the same T3 home", () =>
Effect.gen(function* () {
const args = ["--base-dir", "/srv/t3 home"];
expect(
yield* rootCommand({
node: "/home/theo/.local/node/bin/node",
entry: "/home/theo/.local/lib/node_modules/t3/dist/bin.mjs",
path: yield* pathWithT3,
args,
}),
).toBe(`sudo env "PATH=$PATH" t3 browser setup --base-dir '/srv/t3 home'`);
expect(
yield* rootCommand({ node: "/opt/t3/t3", entry: "/opt/t3/t3", executable: true, args }),
).toBe("sudo /opt/t3/t3 browser setup --base-dir '/srv/t3 home'");
}).pipe(Effect.scoped),
);
it.effect("names this install's launcher when t3 is not on PATH", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const home = yield* fs.makeTempDirectoryScoped();
const shim = path.join(home, ".local bin", "t3");
yield* fs.makeDirectory(path.dirname(shim), { recursive: true });
yield* fs.writeFileString(shim, "#!/bin/sh\n", { mode: 0o755 });
const desktop = {
node: "/tmp/.mount_T3abc/t3code",
entry: "/tmp/.mount_T3abc/resources/app.asar/apps/server/dist/bin.mjs",
env: { T3CODE_CLI_PATH: shim },
};
expect(yield* rootCommand(desktop)).toBe(`sudo '${shim}' browser setup`);
// An unrelated `t3` on PATH, such as an older npm global, is another install's CLI.
expect(yield* rootCommand({ ...desktop, path: yield* pathWithT3 })).toBe(
`sudo '${shim}' browser setup`,
);
// The link the app installs onto PATH leads to its own launcher, so plain `t3` is right.
const linked = yield* fs.makeTempDirectoryScoped();
yield* fs.symlink(shim, path.join(linked, "t3"));
expect(yield* rootCommand({ ...desktop, path: linked })).toBe(
'sudo env "PATH=$PATH" t3 browser setup',
);
expect(
yield* rootCommand({
node: "/opt/t3/t3",
entry: "/opt/t3/t3",
executable: true,
}),
).toBe("sudo /opt/t3/t3 browser setup");
expect(
yield* rootCommand({ ...desktop, env: { T3CODE_CLI_PATH: path.join(home, "gone") } }),
).toBe('sudo env "PATH=$PATH" t3 browser setup');
}).pipe(Effect.scoped),
);
});
it.layer(NodeServices.layer)("host CLI commands", (it) => {
it.effect("renders a PowerShell command line on Windows", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const home = yield* fs.makeTempDirectoryScoped();
const shim = path.join(home, "t3.cmd");
yield* fs.writeFileString(shim, "@echo off\r\n");
const hostCommand = (platform: NodeJS.Platform, env: Record<string, string>) =>
resolveHostCliCommand("doctor", ["--base-dir", "C:\\Users\\Jane Doe\\.t3"]).pipe(
Effect.provideService(HostProcess.ExecutablePath, "C:\\Program Files\\T3 Code\\t3.exe"),
Effect.provideService(HostProcess.Arguments, [
"C:\\Program Files\\T3 Code\\t3.exe",
"C:\\Program Files\\T3 Code\\resources\\app.asar\\apps\\server\\dist\\bin.mjs",
]),
Effect.provideService(HostProcess.IsExecutable, false),
Effect.provideService(HostProcess.Platform, platform),
Effect.provideService(HostProcess.Environment, { PATH: "", ...env }),
);
expect((yield* hostCommand("win32", { T3CODE_CLI_PATH: shim })).command).toBe(
`& "${shim}" doctor "--base-dir" "C:\\Users\\Jane Doe\\.t3"`,
);
expect((yield* hostCommand("win32", {})).command).toBe(
't3 doctor "--base-dir" "C:\\Users\\Jane Doe\\.t3"',
);
expect(
(yield* resolveHostCliCommand("doctor", ["--base-dir", "C:\\T3\\\u201CArchive\u201D"]).pipe(
Effect.provideService(HostProcess.Platform, "win32"),
Effect.provideService(HostProcess.Environment, { PATH: "" }),
Effect.provideService(HostProcess.ExecutablePath, "C:\\t3.exe"),
Effect.provideService(HostProcess.Arguments, ["C:\\t3.exe", "C:\\bin.mjs"]),
Effect.provideService(HostProcess.IsExecutable, false),
)).command,
).toBe('t3 doctor "--base-dir" "C:\\T3\\\u201C\u201CArchive\u201D\u201D"');
expect((yield* hostCommand("linux", { T3CODE_CLI_PATH: shim })).command).toMatch(
/ doctor --base-dir 'C:\\Users\\Jane Doe\\\.t3'$/,
);
}).pipe(Effect.scoped),
);
});
it.layer(NodeServices.layer)("manual server installation ownership", (it) => {
it.effect("recognizes runner caches for script packages and the npm launcher", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped();
const executable = path.join(root, ".t3/runtime/versions/1.0.0/t3");
yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
yield* fs.writeFileString(executable, "");
for (const [relative, kind] of [
["npm/_npx/hash/node_modules/t3/dist/bin.mjs", "npx"],
["npm/_npx/hash/node_modules/t3/bin/t3.mjs", "npx"],
["pnpm/dlx/hash/node_modules/t3/dist/bin.mjs", "pnpm-dlx"],
[".bun/install/cache/t3/dist/bin.mjs", "bunx"],
] as const) {
const entry = path.join(root, relative);
yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
yield* fs.writeFileString(entry, "");
yield* fs.writeFileString(
path.join(path.dirname(entry), "../package.json"),
`{"name":"t3","version":"${packageJson.version}","bin":{"t3":"./bin/t3.mjs"}}`,
);
const launchedBy = entry.endsWith("t3.mjs");
const installation = yield* resolveServerInstallation.pipe(
Effect.provideService(HostProcess.Arguments, [launchedBy ? executable : "node", entry]),
Effect.provideService(HostProcess.ExecutablePath, executable),
Effect.provideService(HostProcess.IsExecutable, launchedBy),
Effect.provideService(
HostProcess.Environment,
launchedBy ? { T3CODE_NPM_LAUNCHER_PATH: entry } : {},
),
);
expect(installation).toEqual({ kind });
}
}),
);
it.effect("requires the npm prefix's bin to point to the running package", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped();
const prefix = path.join(root, "bunx-tools");
const packageRoot = path.join(prefix, "lib/node_modules/t3");
const entry = path.join(packageRoot, "dist/bin.mjs");
const globalBin = path.join(prefix, "bin/t3");
yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
yield* fs.makeDirectory(path.dirname(globalBin), { recursive: true });
yield* fs.writeFileString(entry, "");
yield* fs.writeFileString(
path.join(packageRoot, "package.json"),
'{"name":"t3","version":"0.0.45","bin":{"t3":"./dist/bin.mjs"}}',
);
const resolve = resolveServerInstallation.pipe(
Effect.provideService(HostProcess.Arguments, ["node", entry]),
Effect.provideService(HostProcess.IsExecutable, false),
Effect.provideService(HostProcess.Platform, "linux"),
);
expect(yield* resolve).toBeNull();
yield* fs.symlink(entry, globalBin);
expect(yield* resolve).toEqual({ kind: "npm-global", prefix });
yield* fs.remove(globalBin);
yield* fs.writeFileString(globalBin, "an unrelated t3 command");
expect(yield* resolve).toBeNull();
expect(yield* resolve.pipe(Effect.provideService(HostProcess.Platform, "win32"))).toBeNull();
}),
);
it.effect("proves the downloaded executable was started by a global npm launcher", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped();
const prefix = path.join(root, "bunx-tools");
const packageRoot = path.join(prefix, "lib/node_modules/t3");
const launcher = path.join(packageRoot, "bin/t3.mjs");
const executable = path.join(root, ".t3/runtime/versions/1.0.0/t3");
yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
yield* fs.makeDirectory(path.join(prefix, "bin"));
yield* fs.writeFileString(launcher, "");
yield* fs.writeFileString(executable, "");
yield* fs.symlink(launcher, path.join(prefix, "bin/t3"));
const resolve = resolveServerInstallation.pipe(
Effect.provideService(HostProcess.ExecutablePath, executable),
Effect.provideService(HostProcess.Arguments, [executable, executable]),
Effect.provideService(HostProcess.IsExecutable, true),
Effect.provideService(HostProcess.Platform, "linux"),
Effect.provideService(HostProcess.Environment, { T3CODE_NPM_LAUNCHER_PATH: launcher }),
);
for (const [version, expected] of [
["0.0.1", null],
[packageJson.version, { kind: "npm-global", prefix }],
] as const) {
yield* fs.writeFileString(
path.join(packageRoot, "package.json"),
`{"name":"t3","version":"${version}","bin":{"t3":"./bin/t3.mjs"}}`,
);
expect(yield* resolve).toEqual(expected);
}
}),
);
it.effect("leaves local, standalone, missing and unreadable installs unknown", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped();
for (const relative of [
"project/node_modules/t3/dist/bin.mjs",
"project/apps/server/dist/bin.mjs",
".t3/runtime/0.0.45/t3",
"missing/dist/bin.mjs",
]) {
const entry = path.join(root, relative);
if (!relative.startsWith("missing")) {
yield* fs.makeDirectory(path.dirname(entry), { recursive: true });
yield* fs.writeFileString(entry, "");
}
expect(
yield* resolveServerInstallation.pipe(
Effect.provideService(HostProcess.Arguments, ["node", entry]),
Effect.provideService(HostProcess.IsExecutable, false),
),
).toBeNull();
}
}),
);
});