Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { afterEach, beforeEach, 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 Option from "effect/Option";
7import * as Path from "effect/Path";
8import * as Sink from "effect/Sink";
9import * as Stream from "effect/Stream";
10import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11
12import * as DesktopCliCommand from "./DesktopCliCommand.ts";
13import * as DesktopCliShim from "./DesktopCliShim.ts";
14import * as DesktopEnvironment from "./DesktopEnvironment.ts";
15
16const environmentFor = (
17 path: Path.Path,
18 input: { home: string; baseDir: string; platform?: NodeJS.Platform; isPackaged?: boolean },
19) =>
20 DesktopEnvironment.DesktopEnvironment.of({
21 path,
22 platform: input.platform ?? "linux",
23 isPackaged: input.isPackaged ?? true,
24 homeDirectory: input.home,
25 baseDir: input.baseDir,
26 stateDir: path.join(input.baseDir, "userdata"),
27 serverRoot: "/opt/T3 Code/resources/app.asar",
28 appImagePath: Option.none(),
29 } as unknown as DesktopEnvironment.DesktopEnvironment["Service"]);
30
31const commandIn = (
32 input: { home: string; baseDir?: string; platform?: NodeJS.Platform; isPackaged?: boolean },
33 spawner?: ChildProcessSpawner.ChildProcessSpawner["Service"],
34) =>
35 Effect.gen(function* () {
36 const path = yield* Path.Path;
37 const fs = yield* FileSystem.FileSystem;
38 const baseDir = input.baseDir ?? path.join(input.home, ".t3");
39 yield* fs.makeDirectory(path.join(baseDir, "userdata"), { recursive: true });
40 const make = DesktopCliCommand.make.pipe(
41 Effect.provideService(
42 DesktopEnvironment.DesktopEnvironment,
43 environmentFor(path, { ...input, baseDir }),
44 ),
45 );
46 return yield* spawner
47 ? make.pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner))
48 : make;
49 });
50
51const fakePowerShell = (initial: string) => {
52 const registry = { path: initial, failReads: false, writes: 0 };
53 const spawner = ChildProcessSpawner.make((command) =>
54 Effect.sync(() => {
55 const { options } = command as unknown as {
56 readonly options: { readonly env?: Record<string, string> };
57 };
58 const env = options.env ?? {};
59 let exitCode = 0;
60 let stdout = "";
61 if (env.T3_SET === "1") {
62 registry.path = env.T3_PATH ?? "";
63 registry.writes += 1;
64 } else if (registry.failReads) {
65 exitCode = 1;
66 } else {
67 stdout = Buffer.from(registry.path, "utf8").toString("base64");
68 }
69 return ChildProcessSpawner.makeHandle({
70 pid: ChildProcessSpawner.ProcessId(1),
71 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
72 isRunning: Effect.succeed(false),
73 kill: () => Effect.void,
74 unref: Effect.succeed(Effect.void),
75 stdin: Sink.drain,
76 stdout: Stream.make(new TextEncoder().encode(stdout)),
77 stderr: Stream.empty,
78 all: Stream.empty,
79 getInputFd: () => Sink.drain,
80 getOutputFd: () => Stream.empty,
81 });
82 }),
83 );
84 return { registry, spawner };
85};
86
87const machinePath = process.env.PATH;
88beforeEach(() => {
89 process.env.PATH = "";
90});
91afterEach(() => {
92 process.env.PATH = machinePath;
93});
94
95it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
96 it.effect("links the launcher onto PATH and removes only that link", () =>
97 Effect.gen(function* () {
98 const fs = yield* FileSystem.FileSystem;
99 const path = yield* Path.Path;
100 const home = yield* fs.makeTempDirectoryScoped();
101 const command = yield* commandIn({ home });
102 const link = path.join(home, ".local", "bin", "t3");
103
104 expect(yield* command.state).toEqual({
105 supported: true,
106 installedPath: null,
107 onPath: false,
108 launcherPath: null,
109 });
110 const installed = yield* command.install;
111 expect(installed.installedPath).toBe(link);
112 expect(installed.launcherPath).toBe(path.join(home, ".t3", "bin", "t3"));
113 expect(yield* fs.readLink(link)).toBe(path.join(home, ".t3", "bin", "t3"));
114 expect((yield* command.install).installedPath).toBe(link);
115
116 expect((yield* command.uninstall).installedPath).toBeNull();
117 expect(yield* fs.exists(link)).toBe(false);
118 expect(yield* fs.exists(path.join(home, ".t3", "bin", "t3"))).toBe(true);
119 }).pipe(Effect.scoped),
120 );
121
122 it.effect("never replaces or removes a t3 it did not create", () =>
123 Effect.gen(function* () {
124 const fs = yield* FileSystem.FileSystem;
125 const path = yield* Path.Path;
126 const home = yield* fs.makeTempDirectoryScoped();
127 const command = yield* commandIn({ home });
128 const theirs = path.join(home, ".local", "bin", "t3");
129 yield* fs.makeDirectory(path.dirname(theirs), { recursive: true });
130 yield* fs.writeFileString(theirs, "npm's t3\n");
131 yield* fs.makeDirectory(path.join(home, "bin"), { recursive: true });
132 yield* fs.symlink(path.join(home, "gone"), path.join(home, "bin", "t3"));
133
134 const error = yield* Effect.flip(command.install);
135 expect(error.message).toContain("Another t3 command is already installed");
136 yield* command.uninstall;
137 expect(yield* fs.readFileString(theirs)).toBe("npm's t3\n");
138 expect(yield* fs.readLink(path.join(home, "bin", "t3"))).toBe(path.join(home, "gone"));
139
140 const launcher = path.join(home, ".t3", "bin", "t3");
141 yield* fs.writeFileString(launcher, "npm's t3\n");
142 process.env.PATH = path.dirname(launcher);
143 expect(yield* command.state).toMatchObject({ onPath: false, shadowedBy: launcher });
144 expect((yield* Effect.flip(command.install)).message).toContain("Could not set up");
145 yield* command.uninstall;
146 expect(yield* fs.readFileString(launcher)).toBe("npm's t3\n");
147 }).pipe(Effect.scoped),
148 );
149
150 it.effect("finds and removes a link left by a previous T3 home", () =>
151 Effect.gen(function* () {
152 const fs = yield* FileSystem.FileSystem;
153 const path = yield* Path.Path;
154 const home = yield* fs.makeTempDirectoryScoped();
155 const before = yield* commandIn({ home, baseDir: path.join(home, "old-t3") });
156 const link = (yield* before.install).installedPath;
157
158 const after = yield* commandIn({ home, baseDir: path.join(home, "new-t3") });
159 expect((yield* after.state).installedPath).toBe(link);
160 expect((yield* after.install).installedPath).toBe(link);
161 expect(yield* fs.readLink(link!)).toBe(path.join(home, "new-t3", "bin", "t3"));
162 yield* after.uninstall;
163 expect(yield* fs.exists(path.join(home, ".local", "bin", "t3"))).toBe(false);
164 expect(yield* fs.exists(path.join(home, "bin", "t3"))).toBe(false);
165 }).pipe(Effect.scoped),
166 );
167
168 it.effect("refuses to install behind another t3 that runs first", () =>
169 Effect.gen(function* () {
170 const fs = yield* FileSystem.FileSystem;
171 const path = yield* Path.Path;
172 const home = yield* fs.makeTempDirectoryScoped();
173 const shadow = path.join(home, "shadow");
174 yield* fs.makeDirectory(shadow);
175 yield* fs.writeFileString(path.join(shadow, "t3"), "#!/bin/sh\n", { mode: 0o755 });
176 process.env.PATH = [shadow, path.join(home, ".local", "bin")].join(":");
177
178 const command = yield* commandIn({ home });
179 const theirs = path.join(shadow, "t3");
180 expect((yield* command.state).shadowedBy).toBe(theirs);
181 const error = yield* Effect.flip(command.install);
182 expect(error.message).toContain(theirs);
183 expect(yield* fs.exists(path.join(home, ".local", "bin", "t3"))).toBe(false);
184
185 yield* fs.remove(theirs);
186 const installed = yield* command.install;
187 expect(installed).toMatchObject({
188 onPath: true,
189 installedPath: path.join(home, ".local", "bin", "t3"),
190 });
191 expect(installed.shadowedBy).toBeUndefined();
192 }).pipe(Effect.scoped),
193 );
194
195 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
196 "skips a t3 directory or a file this user cannot execute",
197 () =>
198 Effect.gen(function* () {
199 const fs = yield* FileSystem.FileSystem;
200 const path = yield* Path.Path;
201 const home = yield* fs.makeTempDirectoryScoped();
202 const folder = path.join(home, "folder");
203 const plain = path.join(home, "plain");
204 const shadow = path.join(home, "shadow");
205 yield* fs.makeDirectory(path.join(folder, "t3"), { recursive: true });
206 yield* fs.makeDirectory(plain);
207 yield* fs.writeFileString(path.join(plain, "t3"), "notes\n");
208 // Root can execute mode 0o601; other users must honor the owner permissions.
209 yield* fs.chmod(path.join(plain, "t3"), process.getuid?.() === 0 ? 0o600 : 0o601);
210 yield* fs.makeDirectory(shadow);
211 yield* fs.writeFileString(path.join(shadow, "t3"), "#!/bin/sh\n", { mode: 0o755 });
212 process.env.PATH = [folder, plain, shadow, path.join(home, ".local", "bin")].join(":");
213
214 const command = yield* commandIn({ home });
215 expect((yield* command.state).shadowedBy).toBe(path.join(shadow, "t3"));
216
217 const error = yield* Effect.flip(command.install);
218 expect(error.message).toContain(path.join(shadow, "t3"));
219
220 yield* fs.remove(shadow, { recursive: true });
221 expect(yield* command.install).toMatchObject({
222 onPath: true,
223 installedPath: path.join(home, ".local", "bin", "t3"),
224 });
225 }).pipe(Effect.scoped),
226 );
227
228 it.effect("does not treat its own launcher as another t3 when its folder is first on PATH", () =>
229 Effect.gen(function* () {
230 const fs = yield* FileSystem.FileSystem;
231 const path = yield* Path.Path;
232 const home = yield* fs.makeTempDirectoryScoped();
233 const launcher = path.join(home, ".t3", "bin", "t3");
234 process.env.PATH = [path.dirname(launcher), path.join(home, ".local", "bin")].join(":");
235
236 const command = yield* commandIn({ home });
237 const installed = yield* command.install;
238
239 expect(yield* fs.exists(launcher)).toBe(true);
240 expect(installed).toMatchObject({
241 onPath: true,
242 installedPath: path.join(home, ".local", "bin", "t3"),
243 });
244 expect(installed.shadowedBy).toBeUndefined();
245 expect(yield* command.state).toEqual(installed);
246
247 const removed = yield* command.uninstall;
248 expect(removed.installedPath).toBeNull();
249 expect(removed.onPath).toBe(true);
250 expect(yield* command.state).toEqual(removed);
251 expect(yield* fs.exists(installed.installedPath!)).toBe(false);
252 expect(yield* fs.exists(launcher)).toBe(true);
253 }).pipe(Effect.scoped),
254 );
255
256 it.effect("leaves the Windows PATH alone when it cannot be read", () =>
257 Effect.gen(function* () {
258 const fs = yield* FileSystem.FileSystem;
259 const home = yield* fs.makeTempDirectoryScoped();
260 const userPath = "C:\\Users\\Me\\npm;%USERPROFILE%\\tools";
261 const { registry, spawner } = fakePowerShell(userPath);
262 const command = yield* commandIn({ home, platform: "win32" }, spawner);
263
264 registry.failReads = true;
265 const error = yield* Effect.flip(command.install);
266 expect(error.message).toContain("left unchanged");
267 expect(registry).toMatchObject({ path: userPath, writes: 0 });
268
269 registry.failReads = false;
270 const launcherDir = DesktopCliShim.launcherPath(
271 environmentFor(yield* Path.Path, { home, baseDir: `${home}/.t3`, platform: "win32" }),
272 ).replace(/[\\/]t3\.cmd$/, "");
273 yield* command.install;
274 expect(registry.path).toBe(`${userPath};${launcherDir}`);
275 yield* command.uninstall;
276 expect(registry.path).toBe(userPath);
277 }).pipe(Effect.scoped),
278 );
279
280 it.effect("does not claim or remove a Windows PATH entry the user added", () =>
281 Effect.gen(function* () {
282 const fs = yield* FileSystem.FileSystem;
283 const path = yield* Path.Path;
284 const home = yield* fs.makeTempDirectoryScoped();
285 const launcherDir = path.dirname(
286 DesktopCliShim.launcherPath(
287 environmentFor(path, { home, baseDir: path.join(home, ".t3"), platform: "win32" }),
288 ),
289 );
290 const userPath = `C:\\Tools;${launcherDir}`;
291 const { registry, spawner } = fakePowerShell(userPath);
292 const command = yield* commandIn({ home, platform: "win32" }, spawner);
293
294 expect((yield* command.state).installedPath).toBeNull();
295 yield* command.uninstall;
296 expect(registry.path).toBe(userPath);
297 }).pipe(Effect.scoped),
298 );
299});