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

open.test.ts

413 lines13.3 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import { assertSuccess } from "@effect/vitest/utils";
4import { FileSystem, Path, Effect } from "effect";
5​
6import {
7 isCommandAvailable,
8 launchDetached,
9 resolveAvailableEditors,
10 resolveEditorLaunch,
11} from "./open.ts";
12​
13it.layer(NodeServices.layer)("resolveEditorLaunch", (it) => {
14 it.effect("returns commands for command-based editors", () =>
15 Effect.gen(function* () {
16 const antigravityLaunch = yield* resolveEditorLaunch(
17 { cwd: "/tmp/workspace", editor: "antigravity" },
18 "darwin",
19 { PATH: "" },
20 );
21 assert.deepEqual(antigravityLaunch, {
22 command: "agy",
23 args: ["/tmp/workspace"],
24 });
25​
26 const cursorLaunch = yield* resolveEditorLaunch(
27 { cwd: "/tmp/workspace", editor: "cursor" },
28 "darwin",
29 { PATH: "" },
30 );
31 assert.deepEqual(cursorLaunch, {
32 command: "cursor",
33 args: ["/tmp/workspace"],
34 });
35​
36 const traeLaunch = yield* resolveEditorLaunch(
37 { cwd: "/tmp/workspace", editor: "trae" },
38 "darwin",
39 );
40 assert.deepEqual(traeLaunch, {
41 command: "trae",
42 args: ["/tmp/workspace"],
43 });
44​
45 const kiroLaunch = yield* resolveEditorLaunch(
46 { cwd: "/tmp/workspace", editor: "kiro" },
47 "darwin",
48 { PATH: "" },
49 );
50 assert.deepEqual(kiroLaunch, {
51 command: "kiro",
52 args: ["ide", "/tmp/workspace"],
53 });
54​
55 const vscodeLaunch = yield* resolveEditorLaunch(
56 { cwd: "/tmp/workspace", editor: "vscode" },
57 "darwin",
58 { PATH: "" },
59 );
60 assert.deepEqual(vscodeLaunch, {
61 command: "code",
62 args: ["/tmp/workspace"],
63 });
64​
65 const vscodeInsidersLaunch = yield* resolveEditorLaunch(
66 { cwd: "/tmp/workspace", editor: "vscode-insiders" },
67 "darwin",
68 );
69 assert.deepEqual(vscodeInsidersLaunch, {
70 command: "code-insiders",
71 args: ["/tmp/workspace"],
72 });
73​
74 const vscodiumLaunch = yield* resolveEditorLaunch(
75 { cwd: "/tmp/workspace", editor: "vscodium" },
76 "darwin",
77 );
78 assert.deepEqual(vscodiumLaunch, {
79 command: "codium",
80 args: ["/tmp/workspace"],
81 });
82​
83 const zedLaunch = yield* resolveEditorLaunch(
84 { cwd: "/tmp/workspace", editor: "zed" },
85 "darwin",
86 { PATH: "" },
87 );
88 assert.deepEqual(zedLaunch, {
89 command: "zed",
90 args: ["/tmp/workspace"],
91 });
92​
93 const ideaLaunch = yield* resolveEditorLaunch(
94 { cwd: "/tmp/workspace", editor: "idea" },
95 "darwin",
96 );
97 assert.deepEqual(ideaLaunch, {
98 command: "idea",
99 args: ["/tmp/workspace"],
100 });
101 }),
102 );
103​
104 it.effect("applies launch-style-specific navigation arguments", () =>
105 Effect.gen(function* () {
106 const lineOnly = yield* resolveEditorLaunch(
107 { cwd: "/tmp/workspace/AGENTS.md:48", editor: "cursor" },
108 "darwin",
109 { PATH: "" },
110 );
111 assert.deepEqual(lineOnly, {
112 command: "cursor",
113 args: ["--goto", "/tmp/workspace/AGENTS.md:48"],
114 });
115​
116 const lineAndColumn = yield* resolveEditorLaunch(
117 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "cursor" },
118 "darwin",
119 { PATH: "" },
120 );
121 assert.deepEqual(lineAndColumn, {
122 command: "cursor",
123 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
124 });
125​
126 const traeLineAndColumn = yield* resolveEditorLaunch(
127 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "trae" },
128 "darwin",
129 );
130 assert.deepEqual(traeLineAndColumn, {
131 command: "trae",
132 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
133 });
134​
135 const kiroLineAndColumn = yield* resolveEditorLaunch(
136 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "kiro" },
137 "darwin",
138 { PATH: "" },
139 );
140 assert.deepEqual(kiroLineAndColumn, {
141 command: "kiro",
142 args: ["ide", "--goto", "/tmp/workspace/src/open.ts:71:5"],
143 });
144​
145 const vscodeLineAndColumn = yield* resolveEditorLaunch(
146 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "vscode" },
147 "darwin",
148 { PATH: "" },
149 );
150 assert.deepEqual(vscodeLineAndColumn, {
151 command: "code",
152 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
153 });
154​
155 const vscodeInsidersLineAndColumn = yield* resolveEditorLaunch(
156 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "vscode-insiders" },
157 "darwin",
158 );
159 assert.deepEqual(vscodeInsidersLineAndColumn, {
160 command: "code-insiders",
161 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
162 });
163​
164 const vscodiumLineAndColumn = yield* resolveEditorLaunch(
165 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "vscodium" },
166 "darwin",
167 );
168 assert.deepEqual(vscodiumLineAndColumn, {
169 command: "codium",
170 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
171 });
172​
173 const zedLineAndColumn = yield* resolveEditorLaunch(
174 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "zed" },
175 "darwin",
176 { PATH: "" },
177 );
178 assert.deepEqual(zedLineAndColumn, {
179 command: "zed",
180 args: ["/tmp/workspace/src/open.ts:71:5"],
181 });
182​
183 const zedLineOnly = yield* resolveEditorLaunch(
184 { cwd: "/tmp/workspace/AGENTS.md:48", editor: "zed" },
185 "darwin",
186 { PATH: "" },
187 );
188 assert.deepEqual(zedLineOnly, {
189 command: "zed",
190 args: ["/tmp/workspace/AGENTS.md:48"],
191 });
192​
193 const ideaLineOnly = yield* resolveEditorLaunch(
194 { cwd: "/tmp/workspace/AGENTS.md:48", editor: "idea" },
195 "darwin",
196 );
197 assert.deepEqual(ideaLineOnly, {
198 command: "idea",
199 args: ["--line", "48", "/tmp/workspace/AGENTS.md"],
200 });
201​
202 const ideaLineAndColumn = yield* resolveEditorLaunch(
203 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "idea" },
204 "darwin",
205 );
206 assert.deepEqual(ideaLineAndColumn, {
207 command: "idea",
208 args: ["--line", "71", "--column", "5", "/tmp/workspace/src/open.ts"],
209 });
210 }),
211 );
212​
213 it.effect("falls back to zeditor when zed is not installed", () =>
214 Effect.gen(function* () {
215 const fs = yield* FileSystem.FileSystem;
216 const path = yield* Path.Path;
217 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
218 yield* fs.writeFileString(path.join(dir, "zeditor"), "#!/bin/sh\nexit 0\n");
219 yield* fs.chmod(path.join(dir, "zeditor"), 0o755);
220​
221 const result = yield* resolveEditorLaunch({ cwd: "/tmp/workspace", editor: "zed" }, "linux", {
222 PATH: dir,
223 });
224​
225 assert.deepEqual(result, {
226 command: "zeditor",
227 args: ["/tmp/workspace"],
228 });
229 }),
230 );
231​
232 it.effect("falls back to the primary command when no alias is installed", () =>
233 Effect.gen(function* () {
234 const result = yield* resolveEditorLaunch({ cwd: "/tmp/workspace", editor: "zed" }, "linux", {
235 PATH: "",
236 });
237 assert.deepEqual(result, {
238 command: "zed",
239 args: ["/tmp/workspace"],
240 });
241 }),
242 );
243​
244 it.effect("maps file-manager editor to OS open commands", () =>
245 Effect.gen(function* () {
246 const launch1 = yield* resolveEditorLaunch(
247 { cwd: "/tmp/workspace", editor: "file-manager" },
248 "darwin",
249 { PATH: "" },
250 );
251 assert.deepEqual(launch1, {
252 command: "open",
253 args: ["/tmp/workspace"],
254 });
255​
256 const launch2 = yield* resolveEditorLaunch(
257 { cwd: "C:\\workspace", editor: "file-manager" },
258 "win32",
259 { PATH: "" },
260 );
261 assert.deepEqual(launch2, {
262 command: "explorer",
263 args: ["C:\\workspace"],
264 });
265​
266 const launch3 = yield* resolveEditorLaunch(
267 { cwd: "/tmp/workspace", editor: "file-manager" },
268 "linux",
269 { PATH: "" },
270 );
271 assert.deepEqual(launch3, {
272 command: "xdg-open",
273 args: ["/tmp/workspace"],
274 });
275 }),
276 );
277});
278​
279it.layer(NodeServices.layer)("launchDetached", (it) => {
280 it.effect("resolves when command can be spawned", () =>
281 Effect.gen(function* () {
282 const result = yield* launchDetached({
283 command: process.execPath,
284 args: ["-e", "process.exit(0)"],
285 }).pipe(Effect.result);
286 assertSuccess(result, undefined);
287 }),
288 );
289​
290 it.effect("rejects when command does not exist", () =>
291 Effect.gen(function* () {
292 const result = yield* launchDetached({
293 command: `t3code-no-such-command-${Date.now()}`,
294 args: [],
295 }).pipe(Effect.result);
296 assert.equal(result._tag, "Failure");
297 }),
298 );
299});
300​
301it.layer(NodeServices.layer)("isCommandAvailable", (it) => {
302 it.effect("resolves win32 commands with PATHEXT", () =>
303 Effect.gen(function* () {
304 const fs = yield* FileSystem.FileSystem;
305 const path = yield* Path.Path;
306 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
307 yield* fs.writeFileString(path.join(dir, "code.CMD"), "@echo off\r\n");
308 const env = {
309 PATH: dir,
310 PATHEXT: ".COM;.EXE;.BAT;.CMD",
311 } satisfies NodeJS.ProcessEnv;
312 assert.equal(isCommandAvailable("code", { platform: "win32", env }), true);
313 }),
314 );
315​
316 it("returns false when a command is not on PATH", () => {
317 const env = {
318 PATH: "",
319 PATHEXT: ".COM;.EXE;.BAT;.CMD",
320 } satisfies NodeJS.ProcessEnv;
321 assert.equal(isCommandAvailable("definitely-not-installed", { platform: "win32", env }), false);
322 });
323​
324 it.effect("does not treat bare files without executable extension as available on win32", () =>
325 Effect.gen(function* () {
326 const fs = yield* FileSystem.FileSystem;
327 const path = yield* Path.Path;
328 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
329 yield* fs.writeFileString(path.join(dir, "npm"), "echo nope\r\n");
330 const env = {
331 PATH: dir,
332 PATHEXT: ".COM;.EXE;.BAT;.CMD",
333 } satisfies NodeJS.ProcessEnv;
334 assert.equal(isCommandAvailable("npm", { platform: "win32", env }), false);
335 }),
336 );
337​
338 it.effect("appends PATHEXT for commands with non-executable extensions on win32", () =>
339 Effect.gen(function* () {
340 const fs = yield* FileSystem.FileSystem;
341 const path = yield* Path.Path;
342 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
343 yield* fs.writeFileString(path.join(dir, "my.tool.CMD"), "@echo off\r\n");
344 const env = {
345 PATH: dir,
346 PATHEXT: ".COM;.EXE;.BAT;.CMD",
347 } satisfies NodeJS.ProcessEnv;
348 assert.equal(isCommandAvailable("my.tool", { platform: "win32", env }), true);
349 }),
350 );
351​
352 it.effect("uses platform-specific PATH delimiter for platform overrides", () =>
353 Effect.gen(function* () {
354 const fs = yield* FileSystem.FileSystem;
355 const path = yield* Path.Path;
356 const firstDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
357 const secondDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
358 yield* fs.writeFileString(path.join(firstDir, "code.CMD"), "@echo off\r\n");
359 yield* fs.writeFileString(path.join(secondDir, "code.CMD"), "MZ");
360 const env = {
361 PATH: `${firstDir};${secondDir}`,
362 PATHEXT: ".COM;.EXE;.BAT;.CMD",
363 } satisfies NodeJS.ProcessEnv;
364 assert.equal(isCommandAvailable("code", { platform: "win32", env }), true);
365 }),
366 );
367});
368​
369it.layer(NodeServices.layer)("resolveAvailableEditors", (it) => {
370 it.effect("returns installed editors for command launches", () =>
371 Effect.gen(function* () {
372 const fs = yield* FileSystem.FileSystem;
373 const path = yield* Path.Path;
374 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editors-" });
375​
376 yield* fs.writeFileString(path.join(dir, "trae.CMD"), "@echo off\r\n");
377 yield* fs.writeFileString(path.join(dir, "kiro.CMD"), "@echo off\r\n");
378 yield* fs.writeFileString(path.join(dir, "code-insiders.CMD"), "@echo off\r\n");
379 yield* fs.writeFileString(path.join(dir, "codium.CMD"), "@echo off\r\n");
380 yield* fs.writeFileString(path.join(dir, "explorer.CMD"), "MZ");
381 const editors = resolveAvailableEditors("win32", {
382 PATH: dir,
383 PATHEXT: ".COM;.EXE;.BAT;.CMD",
384 });
385 assert.deepEqual(editors, ["trae", "kiro", "vscode-insiders", "vscodium", "file-manager"]);
386 }),
387 );
388​
389 it.effect("includes zed when only the zeditor command is installed", () =>
390 Effect.gen(function* () {
391 const fs = yield* FileSystem.FileSystem;
392 const path = yield* Path.Path;
393 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editors-" });
394​
395 yield* fs.writeFileString(path.join(dir, "zeditor"), "#!/bin/sh\nexit 0\n");
396 yield* fs.writeFileString(path.join(dir, "xdg-open"), "#!/bin/sh\nexit 0\n");
397 yield* fs.chmod(path.join(dir, "zeditor"), 0o755);
398 yield* fs.chmod(path.join(dir, "xdg-open"), 0o755);
399​
400 const editors = resolveAvailableEditors("linux", {
401 PATH: dir,
402 });
403 assert.deepEqual(editors, ["zed", "file-manager"]);
404 }),
405 );
406​
407 it("omits file-manager when the platform opener is unavailable", () => {
408 const editors = resolveAvailableEditors("linux", {
409 PATH: "",
410 });
411 assert.deepEqual(editors, []);
412 });
413});