Open History to see the last commit
1import fs from "node:fs";
2import os from "node:os";
3import path from "node:path";
4
5import { assert, describe, it } from "@effect/vitest";
6
7import {
8 isCommandAvailable,
9 launchDetached,
10 resolveAvailableEditors,
11 resolveEditorLaunch,
12} from "./open";
13import { Effect } from "effect";
14import { assertSuccess } from "@effect/vitest/utils";
15
16describe("resolveEditorLaunch", () => {
17 it.effect("returns commands for command-based editors", () =>
18 Effect.gen(function* () {
19 const cursorLaunch = yield* resolveEditorLaunch(
20 { cwd: "/tmp/workspace", editor: "cursor" },
21 "darwin",
22 );
23 assert.deepEqual(cursorLaunch, {
24 command: "cursor",
25 args: ["/tmp/workspace"],
26 });
27
28 const vscodeLaunch = yield* resolveEditorLaunch(
29 { cwd: "/tmp/workspace", editor: "vscode" },
30 "darwin",
31 );
32 assert.deepEqual(vscodeLaunch, {
33 command: "code",
34 args: ["/tmp/workspace"],
35 });
36
37 const zedLaunch = yield* resolveEditorLaunch(
38 { cwd: "/tmp/workspace", editor: "zed" },
39 "darwin",
40 );
41 assert.deepEqual(zedLaunch, {
42 command: "zed",
43 args: ["/tmp/workspace"],
44 });
45 }),
46 );
47
48 it.effect("uses --goto when editor supports line/column suffixes", () =>
49 Effect.gen(function* () {
50 const lineOnly = yield* resolveEditorLaunch(
51 { cwd: "/tmp/workspace/AGENTS.md:48", editor: "cursor" },
52 "darwin",
53 );
54 assert.deepEqual(lineOnly, {
55 command: "cursor",
56 args: ["--goto", "/tmp/workspace/AGENTS.md:48"],
57 });
58
59 const lineAndColumn = yield* resolveEditorLaunch(
60 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "cursor" },
61 "darwin",
62 );
63 assert.deepEqual(lineAndColumn, {
64 command: "cursor",
65 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
66 });
67
68 const vscodeLineAndColumn = yield* resolveEditorLaunch(
69 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "vscode" },
70 "darwin",
71 );
72 assert.deepEqual(vscodeLineAndColumn, {
73 command: "code",
74 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
75 });
76
77 const zedLineAndColumn = yield* resolveEditorLaunch(
78 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "zed" },
79 "darwin",
80 );
81 assert.deepEqual(zedLineAndColumn, {
82 command: "zed",
83 args: ["/tmp/workspace/src/open.ts:71:5"],
84 });
85 }),
86 );
87
88 it.effect("maps file-manager editor to OS open commands", () =>
89 Effect.gen(function* () {
90 const launch1 = yield* resolveEditorLaunch(
91 { cwd: "/tmp/workspace", editor: "file-manager" },
92 "darwin",
93 );
94 assert.deepEqual(launch1, {
95 command: "open",
96 args: ["/tmp/workspace"],
97 });
98
99 const launch2 = yield* resolveEditorLaunch(
100 { cwd: "C:\\workspace", editor: "file-manager" },
101 "win32",
102 );
103 assert.deepEqual(launch2, {
104 command: "explorer",
105 args: ["C:\\workspace"],
106 });
107
108 const launch3 = yield* resolveEditorLaunch(
109 { cwd: "/tmp/workspace", editor: "file-manager" },
110 "linux",
111 );
112 assert.deepEqual(launch3, {
113 command: "xdg-open",
114 args: ["/tmp/workspace"],
115 });
116 }),
117 );
118});
119
120describe("launchDetached", () => {
121 it.effect("resolves when command can be spawned", () =>
122 Effect.gen(function* () {
123 const result = yield* launchDetached({
124 command: process.execPath,
125 args: ["-e", "process.exit(0)"],
126 }).pipe(Effect.result);
127 assertSuccess(result, undefined);
128 }),
129 );
130
131 it.effect("rejects when command does not exist", () =>
132 Effect.gen(function* () {
133 const result = yield* launchDetached({
134 command: `t3code-no-such-command-${Date.now()}`,
135 args: [],
136 }).pipe(Effect.result);
137 assert.equal(result._tag, "Failure");
138 }),
139 );
140});
141
142describe("isCommandAvailable", () => {
143 function withTempDir(run: (dir: string) => void): void {
144 const dir = fs.mkdtempSync(path.join(os.tmpdir(), "t3-open-"));
145 try {
146 run(dir);
147 } finally {
148 fs.rmSync(dir, { recursive: true, force: true });
149 }
150 }
151
152 it("resolves win32 commands with PATHEXT", () => {
153 withTempDir((dir) => {
154 fs.writeFileSync(path.join(dir, "code.CMD"), "@echo off\r\n", "utf8");
155 const env = {
156 PATH: dir,
157 PATHEXT: ".COM;.EXE;.BAT;.CMD",
158 } satisfies NodeJS.ProcessEnv;
159 assert.equal(isCommandAvailable("code", { platform: "win32", env }), true);
160 });
161 });
162
163 it("returns false when a command is not on PATH", () => {
164 const env = {
165 PATH: "",
166 PATHEXT: ".COM;.EXE;.BAT;.CMD",
167 } satisfies NodeJS.ProcessEnv;
168 assert.equal(isCommandAvailable("definitely-not-installed", { platform: "win32", env }), false);
169 });
170
171 it("does not treat bare files without executable extension as available on win32", () => {
172 withTempDir((dir) => {
173 fs.writeFileSync(path.join(dir, "npm"), "echo nope\r\n", "utf8");
174 const env = {
175 PATH: dir,
176 PATHEXT: ".COM;.EXE;.BAT;.CMD",
177 } satisfies NodeJS.ProcessEnv;
178 assert.equal(isCommandAvailable("npm", { platform: "win32", env }), false);
179 });
180 });
181
182 it("appends PATHEXT for commands with non-executable extensions on win32", () => {
183 withTempDir((dir) => {
184 fs.writeFileSync(path.join(dir, "my.tool.CMD"), "@echo off\r\n", "utf8");
185 const env = {
186 PATH: dir,
187 PATHEXT: ".COM;.EXE;.BAT;.CMD",
188 } satisfies NodeJS.ProcessEnv;
189 assert.equal(isCommandAvailable("my.tool", { platform: "win32", env }), true);
190 });
191 });
192
193 it("uses platform-specific PATH delimiter for platform overrides", () => {
194 withTempDir((firstDir) => {
195 withTempDir((secondDir) => {
196 fs.writeFileSync(path.join(secondDir, "code.CMD"), "@echo off\r\n", "utf8");
197 const env = {
198 PATH: `${firstDir};${secondDir}`,
199 PATHEXT: ".COM;.EXE;.BAT;.CMD",
200 } satisfies NodeJS.ProcessEnv;
201 assert.equal(isCommandAvailable("code", { platform: "win32", env }), true);
202 });
203 });
204 });
205});
206
207describe("resolveAvailableEditors", () => {
208 it("returns only editors whose launch commands are available", () => {
209 const dir = fs.mkdtempSync(path.join(os.tmpdir(), "t3-editors-"));
210 try {
211 fs.writeFileSync(path.join(dir, "cursor.CMD"), "@echo off\r\n", "utf8");
212 fs.writeFileSync(path.join(dir, "explorer.EXE"), "MZ", "utf8");
213 const editors = resolveAvailableEditors("win32", {
214 PATH: dir,
215 PATHEXT: ".COM;.EXE;.BAT;.CMD",
216 });
217 assert.deepEqual(editors, ["cursor", "file-manager"]);
218 } finally {
219 fs.rmSync(dir, { recursive: true, force: true });
220 }
221 });
222});