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 Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Option from "effect/Option";
6import * as Path from "effect/Path";
7import * as Sink from "effect/Sink";
8import * as Stream from "effect/Stream";
9import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
10
11import * as DesktopCliCommand from "./DesktopCliCommand.ts";
12import * as DesktopCliShim from "./DesktopCliShim.ts";
13import * as DesktopEnvironment from "./DesktopEnvironment.ts";
14
15const environmentFor = (
16 path: Path.Path,
17 input: { home: string; baseDir: string; platform?: NodeJS.Platform; isPackaged?: boolean },
18) =>
19 DesktopEnvironment.DesktopEnvironment.of({
20 path,
21 platform: input.platform ?? "linux",
22 isPackaged: input.isPackaged ?? true,
23 homeDirectory: input.home,
24 baseDir: input.baseDir,
25 stateDir: path.join(input.baseDir, "userdata"),
26 serverRoot: "/opt/T3 Code/resources/app.asar",
27 appImagePath: Option.none(),
28 } as unknown as DesktopEnvironment.DesktopEnvironment["Service"]);
29
30const commandIn = (
31 input: { home: string; baseDir?: string; platform?: NodeJS.Platform; isPackaged?: boolean },
32 spawner?: ChildProcessSpawner.ChildProcessSpawner["Service"],
33) =>
34 Effect.gen(function* () {
35 const path = yield* Path.Path;
36 const fs = yield* FileSystem.FileSystem;
37 const baseDir = input.baseDir ?? path.join(input.home, ".t3");
38 yield* fs.makeDirectory(path.join(baseDir, "userdata"), { recursive: true });
39 const make = DesktopCliCommand.make.pipe(
40 Effect.provideService(
41 DesktopEnvironment.DesktopEnvironment,
42 environmentFor(path, { ...input, baseDir }),
43 ),
44 );
45 return yield* spawner
46 ? make.pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner))
47 : make;
48 });
49
50const fakePowerShell = (initial: string) => {
51 const registry = { path: initial, failReads: false, writes: 0 };
52 const spawner = ChildProcessSpawner.make((command) =>
53 Effect.sync(() => {
54 const { options } = command as unknown as {
55 readonly options: { readonly env?: Record<string, string> };
56 };
57 const env = options.env ?? {};
58 let exitCode = 0;
59 let stdout = "";
60 if (env.T3_SET === "1") {
61 registry.path = env.T3_PATH ?? "";
62 registry.writes += 1;
63 } else if (registry.failReads) {
64 exitCode = 1;
65 } else {
66 stdout = Buffer.from(registry.path, "utf8").toString("base64");
67 }
68 return ChildProcessSpawner.makeHandle({
69 pid: ChildProcessSpawner.ProcessId(1),
70 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
71 isRunning: Effect.succeed(false),
72 kill: () => Effect.void,
73 unref: Effect.succeed(Effect.void),
74 stdin: Sink.drain,
75 stdout: Stream.make(new TextEncoder().encode(stdout)),
76 stderr: Stream.empty,
77 all: Stream.empty,
78 getInputFd: () => Sink.drain,
79 getOutputFd: () => Stream.empty,
80 });
81 }),
82 );
83 return { registry, spawner };
84};
85
86const machinePath = process.env.PATH;
87beforeEach(() => {
88 process.env.PATH = "";
89});
90afterEach(() => {
91 process.env.PATH = machinePath;
92});
93
94it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
95 it.effect("links the launcher onto PATH and removes only that link", () =>
96 Effect.gen(function* () {
97 const fs = yield* FileSystem.FileSystem;
98 const path = yield* Path.Path;
99 const home = yield* fs.makeTempDirectoryScoped();
100 const command = yield* commandIn({ home });
101 const link = path.join(home, ".local", "bin", "t3");
102
103 expect(yield* command.state).toEqual({ supported: true, installedPath: null, onPath: false });
104 const installed = yield* command.install;
105 expect(installed.installedPath).toBe(link);
106 expect(yield* fs.readLink(link)).toBe(path.join(home, ".t3", "bin", "t3"));
107 expect((yield* command.install).installedPath).toBe(link);
108
109 expect((yield* command.uninstall).installedPath).toBeNull();
110 expect(yield* fs.exists(link)).toBe(false);
111 expect(yield* fs.exists(path.join(home, ".t3", "bin", "t3"))).toBe(true);
112 }).pipe(Effect.scoped),
113 );
114
115 it.effect("never replaces or removes a t3 it did not create", () =>
116 Effect.gen(function* () {
117 const fs = yield* FileSystem.FileSystem;
118 const path = yield* Path.Path;
119 const home = yield* fs.makeTempDirectoryScoped();
120 const command = yield* commandIn({ home });
121 const theirs = path.join(home, ".local", "bin", "t3");
122 yield* fs.makeDirectory(path.dirname(theirs), { recursive: true });
123 yield* fs.writeFileString(theirs, "npm's t3\n");
124 yield* fs.makeDirectory(path.join(home, "bin"), { recursive: true });
125 yield* fs.symlink(path.join(home, "gone"), path.join(home, "bin", "t3"));
126
127 const error = yield* Effect.flip(command.install);
128 expect(error.message).toContain("Another t3 command is already installed");
129 yield* command.uninstall;
130 expect(yield* fs.readFileString(theirs)).toBe("npm's t3\n");
131 expect(yield* fs.readLink(path.join(home, "bin", "t3"))).toBe(path.join(home, "gone"));
132 }).pipe(Effect.scoped),
133 );
134
135 it.effect("finds and removes a link left by a previous T3 home", () =>
136 Effect.gen(function* () {
137 const fs = yield* FileSystem.FileSystem;
138 const path = yield* Path.Path;
139 const home = yield* fs.makeTempDirectoryScoped();
140 const before = yield* commandIn({ home, baseDir: path.join(home, "old-t3") });
141 const link = (yield* before.install).installedPath;
142
143 const after = yield* commandIn({ home, baseDir: path.join(home, "new-t3") });
144 expect((yield* after.state).installedPath).toBe(link);
145 expect((yield* after.install).installedPath).toBe(link);
146 expect(yield* fs.readLink(link!)).toBe(path.join(home, "new-t3", "bin", "t3"));
147 yield* after.uninstall;
148 expect(yield* fs.exists(path.join(home, ".local", "bin", "t3"))).toBe(false);
149 expect(yield* fs.exists(path.join(home, "bin", "t3"))).toBe(false);
150 }).pipe(Effect.scoped),
151 );
152
153 it.effect("refuses to install behind another t3 that runs first", () =>
154 Effect.gen(function* () {
155 const fs = yield* FileSystem.FileSystem;
156 const path = yield* Path.Path;
157 const home = yield* fs.makeTempDirectoryScoped();
158 const shadow = path.join(home, "shadow");
159 yield* fs.makeDirectory(shadow);
160 yield* fs.writeFileString(path.join(shadow, "t3"), "#!/bin/sh\n", { mode: 0o755 });
161 process.env.PATH = [shadow, path.join(home, ".local", "bin")].join(":");
162
163 const command = yield* commandIn({ home });
164 const theirs = path.join(shadow, "t3");
165 expect((yield* command.state).shadowedBy).toBe(theirs);
166 const error = yield* Effect.flip(command.install);
167 expect(error.message).toContain(theirs);
168 expect(yield* fs.exists(path.join(home, ".local", "bin", "t3"))).toBe(false);
169
170 yield* fs.remove(theirs);
171 const installed = yield* command.install;
172 expect(installed).toMatchObject({
173 onPath: true,
174 installedPath: path.join(home, ".local", "bin", "t3"),
175 });
176 expect(installed.shadowedBy).toBeUndefined();
177 }).pipe(Effect.scoped),
178 );
179
180 it.effect("leaves the Windows PATH alone when it cannot be read", () =>
181 Effect.gen(function* () {
182 const fs = yield* FileSystem.FileSystem;
183 const home = yield* fs.makeTempDirectoryScoped();
184 const userPath = "C:\\Users\\Me\\npm;%USERPROFILE%\\tools";
185 const { registry, spawner } = fakePowerShell(userPath);
186 const command = yield* commandIn({ home, platform: "win32" }, spawner);
187
188 registry.failReads = true;
189 const error = yield* Effect.flip(command.install);
190 expect(error.message).toContain("left unchanged");
191 expect(registry).toMatchObject({ path: userPath, writes: 0 });
192
193 registry.failReads = false;
194 const launcherDir = DesktopCliShim.launcherPath(
195 environmentFor(yield* Path.Path, { home, baseDir: `${home}/.t3`, platform: "win32" }),
196 ).replace(/[\\/]t3\.cmd$/, "");
197 yield* command.install;
198 expect(registry.path).toBe(`${userPath};${launcherDir}`);
199 yield* command.uninstall;
200 expect(registry.path).toBe(userPath);
201 }).pipe(Effect.scoped),
202 );
203
204 it.effect("does not claim or remove a Windows PATH entry the user added", () =>
205 Effect.gen(function* () {
206 const fs = yield* FileSystem.FileSystem;
207 const path = yield* Path.Path;
208 const home = yield* fs.makeTempDirectoryScoped();
209 const launcherDir = path.dirname(
210 DesktopCliShim.launcherPath(
211 environmentFor(path, { home, baseDir: path.join(home, ".t3"), platform: "win32" }),
212 ),
213 );
214 const userPath = `C:\\Tools;${launcherDir}`;
215 const { registry, spawner } = fakePowerShell(userPath);
216 const command = yield* commandIn({ home, platform: "win32" }, spawner);
217
218 expect((yield* command.state).installedPath).toBeNull();
219 yield* command.uninstall;
220 expect(registry.path).toBe(userPath);
221 }).pipe(Effect.scoped),
222 );
223});