Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DesktopCliCommand.test.ts

251 lines10.3 KB

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