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