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({
104 supported: true,
105 installedPath: null,
106 onPath: false,
107 launcherPath: null,
108 });
109 const installed = yield* command.install;
110 expect(installed.installedPath).toBe(link);
111 expect(installed.launcherPath).toBe(path.join(home, ".t3", "bin", "t3"));
112 expect(yield* fs.readLink(link)).toBe(path.join(home, ".t3", "bin", "t3"));
113 expect((yield* command.install).installedPath).toBe(link);
114
115 expect((yield* command.uninstall).installedPath).toBeNull();
116 expect(yield* fs.exists(link)).toBe(false);
117 expect(yield* fs.exists(path.join(home, ".t3", "bin", "t3"))).toBe(true);
118 }).pipe(Effect.scoped),
119 );
120
121 it.effect("never replaces or removes a t3 it did not create", () =>
122 Effect.gen(function* () {
123 const fs = yield* FileSystem.FileSystem;
124 const path = yield* Path.Path;
125 const home = yield* fs.makeTempDirectoryScoped();
126 const command = yield* commandIn({ home });
127 const theirs = path.join(home, ".local", "bin", "t3");
128 yield* fs.makeDirectory(path.dirname(theirs), { recursive: true });
129 yield* fs.writeFileString(theirs, "npm's t3\n");
130 yield* fs.makeDirectory(path.join(home, "bin"), { recursive: true });
131 yield* fs.symlink(path.join(home, "gone"), path.join(home, "bin", "t3"));
132
133 const error = yield* Effect.flip(command.install);
134 expect(error.message).toContain("Another t3 command is already installed");
135 yield* command.uninstall;
136 expect(yield* fs.readFileString(theirs)).toBe("npm's t3\n");
137 expect(yield* fs.readLink(path.join(home, "bin", "t3"))).toBe(path.join(home, "gone"));
138 }).pipe(Effect.scoped),
139 );
140
141 it.effect("finds and removes a link left by a previous T3 home", () =>
142 Effect.gen(function* () {
143 const fs = yield* FileSystem.FileSystem;
144 const path = yield* Path.Path;
145 const home = yield* fs.makeTempDirectoryScoped();
146 const before = yield* commandIn({ home, baseDir: path.join(home, "old-t3") });
147 const link = (yield* before.install).installedPath;
148
149 const after = yield* commandIn({ home, baseDir: path.join(home, "new-t3") });
150 expect((yield* after.state).installedPath).toBe(link);
151 expect((yield* after.install).installedPath).toBe(link);
152 expect(yield* fs.readLink(link!)).toBe(path.join(home, "new-t3", "bin", "t3"));
153 yield* after.uninstall;
154 expect(yield* fs.exists(path.join(home, ".local", "bin", "t3"))).toBe(false);
155 expect(yield* fs.exists(path.join(home, "bin", "t3"))).toBe(false);
156 }).pipe(Effect.scoped),
157 );
158
159 it.effect("refuses to install behind another t3 that runs first", () =>
160 Effect.gen(function* () {
161 const fs = yield* FileSystem.FileSystem;
162 const path = yield* Path.Path;
163 const home = yield* fs.makeTempDirectoryScoped();
164 const shadow = path.join(home, "shadow");
165 yield* fs.makeDirectory(shadow);
166 yield* fs.writeFileString(path.join(shadow, "t3"), "#!/bin/sh\n", { mode: 0o755 });
167 process.env.PATH = [shadow, path.join(home, ".local", "bin")].join(":");
168
169 const command = yield* commandIn({ home });
170 const theirs = path.join(shadow, "t3");
171 expect((yield* command.state).shadowedBy).toBe(theirs);
172 const error = yield* Effect.flip(command.install);
173 expect(error.message).toContain(theirs);
174 expect(yield* fs.exists(path.join(home, ".local", "bin", "t3"))).toBe(false);
175
176 yield* fs.remove(theirs);
177 const installed = yield* command.install;
178 expect(installed).toMatchObject({
179 onPath: true,
180 installedPath: path.join(home, ".local", "bin", "t3"),
181 });
182 expect(installed.shadowedBy).toBeUndefined();
183 }).pipe(Effect.scoped),
184 );
185
186 it.effect("leaves the Windows PATH alone when it cannot be read", () =>
187 Effect.gen(function* () {
188 const fs = yield* FileSystem.FileSystem;
189 const home = yield* fs.makeTempDirectoryScoped();
190 const userPath = "C:\\Users\\Me\\npm;%USERPROFILE%\\tools";
191 const { registry, spawner } = fakePowerShell(userPath);
192 const command = yield* commandIn({ home, platform: "win32" }, spawner);
193
194 registry.failReads = true;
195 const error = yield* Effect.flip(command.install);
196 expect(error.message).toContain("left unchanged");
197 expect(registry).toMatchObject({ path: userPath, writes: 0 });
198
199 registry.failReads = false;
200 const launcherDir = DesktopCliShim.launcherPath(
201 environmentFor(yield* Path.Path, { home, baseDir: `${home}/.t3`, platform: "win32" }),
202 ).replace(/[\\/]t3\.cmd$/, "");
203 yield* command.install;
204 expect(registry.path).toBe(`${userPath};${launcherDir}`);
205 yield* command.uninstall;
206 expect(registry.path).toBe(userPath);
207 }).pipe(Effect.scoped),
208 );
209
210 it.effect("does not claim or remove a Windows PATH entry the user added", () =>
211 Effect.gen(function* () {
212 const fs = yield* FileSystem.FileSystem;
213 const path = yield* Path.Path;
214 const home = yield* fs.makeTempDirectoryScoped();
215 const launcherDir = path.dirname(
216 DesktopCliShim.launcherPath(
217 environmentFor(path, { home, baseDir: path.join(home, ".t3"), platform: "win32" }),
218 ),
219 );
220 const userPath = `C:\\Tools;${launcherDir}`;
221 const { registry, spawner } = fakePowerShell(userPath);
222 const command = yield* commandIn({ home, platform: "win32" }, spawner);
223
224 expect((yield* command.state).installedPath).toBeNull();
225 yield* command.uninstall;
226 expect(registry.path).toBe(userPath);
227 }).pipe(Effect.scoped),
228 );
229});