Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import { beforeEach, vi } from "vite-plus/test";
4
5const { openExternalMock, writeTextMock } = vi.hoisted(() => ({
6 openExternalMock: vi.fn(),
7 writeTextMock: vi.fn(),
8}));
9
10vi.mock("electron", () => ({
11 shell: {
12 openExternal: openExternalMock,
13 },
14 clipboard: {
15 writeText: writeTextMock,
16 },
17}));
18
19import * as ElectronShell from "./ElectronShell.ts";
20
21describe("ElectronShell", () => {
22 beforeEach(() => {
23 openExternalMock.mockReset();
24 writeTextMock.mockReset();
25 });
26
27 it.effect("opens safe external URLs", () =>
28 Effect.gen(function* () {
29 openExternalMock.mockResolvedValue(undefined);
30
31 const electronShell = yield* ElectronShell.ElectronShell;
32 const result = yield* electronShell.openExternal("https://example.com/path");
33
34 assert.equal(result, true);
35 assert.deepEqual(openExternalMock.mock.calls, [["https://example.com/path"]]);
36 }).pipe(Effect.provide(ElectronShell.layer)),
37 );
38
39 it.effect("copies text to the system clipboard", () =>
40 Effect.gen(function* () {
41 writeTextMock.mockResolvedValue(undefined);
42
43 const electronShell = yield* ElectronShell.ElectronShell;
44 yield* electronShell.copyText("https://example.com/path");
45
46 assert.deepEqual(writeTextMock.mock.calls, [["https://example.com/path"]]);
47 }).pipe(Effect.provide(ElectronShell.layer)),
48 );
49
50 it.effect("does not fail when the clipboard write rejects", () =>
51 Effect.gen(function* () {
52 writeTextMock.mockRejectedValue(new Error("write failed"));
53
54 const electronShell = yield* ElectronShell.ElectronShell;
55 yield* electronShell.copyText("https://example.com/path");
56
57 assert.deepEqual(writeTextMock.mock.calls, [["https://example.com/path"]]);
58 }).pipe(Effect.provide(ElectronShell.layer)),
59 );
60
61 it.effect("opens the Full Disk Access settings anchor", () =>
62 Effect.gen(function* () {
63 openExternalMock.mockResolvedValue(undefined);
64
65 const electronShell = yield* ElectronShell.ElectronShell;
66 const result = yield* electronShell.openSystemSettings("full-disk-access");
67
68 assert.equal(result, true);
69 assert.deepEqual(openExternalMock.mock.calls, [
70 ["x-apple.systempreferences:com.apple.settings.PrivacySecurity.extension?Privacy_AllFiles"],
71 ]);
72 }).pipe(Effect.provide(ElectronShell.layer)),
73 );
74
75 it.effect("opens remote SSH editor URLs", () =>
76 Effect.gen(function* () {
77 openExternalMock.mockResolvedValue(undefined);
78
79 const electronShell = yield* ElectronShell.ElectronShell;
80 const result = yield* electronShell.openExternal(
81 "vscode://vscode-remote/ssh-remote+example.com/home/user/project",
82 );
83
84 assert.equal(result, true);
85 assert.deepEqual(openExternalMock.mock.calls, [
86 ["vscode://vscode-remote/ssh-remote+example.com/home/user/project"],
87 ]);
88 }).pipe(Effect.provide(ElectronShell.layer)),
89 );
90
91 it.effect("opens Zed's ssh deep link", () =>
92 Effect.gen(function* () {
93 openExternalMock.mockResolvedValue(undefined);
94
95 const electronShell = yield* ElectronShell.ElectronShell;
96 const result = yield* electronShell.openExternal("zed://ssh/example.com/home/user/project");
97
98 assert.equal(result, true);
99 assert.deepEqual(openExternalMock.mock.calls, [["zed://ssh/example.com/home/user/project"]]);
100 }).pipe(Effect.provide(ElectronShell.layer)),
101 );
102
103 it.effect("does not open editor URLs that mix up link shapes", () =>
104 Effect.gen(function* () {
105 openExternalMock.mockResolvedValue(undefined);
106
107 const electronShell = yield* ElectronShell.ElectronShell;
108 const results = yield* Effect.all([
109 electronShell.openExternal("zed://extension/attacker"),
110 electronShell.openExternal("vscode://ssh/example.com/home/user/project"),
111 ]);
112
113 assert.deepEqual(results, [false, false]);
114 assert.equal(openExternalMock.mock.calls.length, 0);
115 }).pipe(Effect.provide(ElectronShell.layer)),
116 );
117
118 it.effect("does not open remote editor URLs with userinfo", () =>
119 Effect.gen(function* () {
120 openExternalMock.mockResolvedValue(undefined);
121
122 const electronShell = yield* ElectronShell.ElectronShell;
123 const results = yield* Effect.all([
124 electronShell.openExternal(
125 "vscode://user@vscode-remote/ssh-remote+example.com/home/user/project",
126 ),
127 electronShell.openExternal(
128 "vscode://:secret@vscode-remote/ssh-remote+example.com/home/user/project",
129 ),
130 electronShell.openExternal("zed://ssh/user@example.com/home/user/project"),
131 ]);
132
133 assert.deepEqual(results, [false, false, false]);
134 assert.equal(openExternalMock.mock.calls.length, 0);
135 }).pipe(Effect.provide(ElectronShell.layer)),
136 );
137
138 it.effect("does not open unsafe external URLs", () =>
139 Effect.gen(function* () {
140 const electronShell = yield* ElectronShell.ElectronShell;
141 const result = yield* electronShell.openExternal("file:///etc/passwd");
142
143 assert.equal(result, false);
144 assert.equal(openExternalMock.mock.calls.length, 0);
145 }).pipe(Effect.provide(ElectronShell.layer)),
146 );
147
148 it.effect("does not open non-remote editor URLs", () =>
149 Effect.gen(function* () {
150 openExternalMock.mockResolvedValue(undefined);
151
152 const electronShell = yield* ElectronShell.ElectronShell;
153 const result = yield* electronShell.openExternal(
154 "vscode://ms-python.python/some-command?argument=attacker",
155 );
156
157 assert.equal(result, false);
158 assert.equal(openExternalMock.mock.calls.length, 0);
159 }).pipe(Effect.provide(ElectronShell.layer)),
160 );
161
162 it.effect("returns false when Electron rejects openExternal", () =>
163 Effect.gen(function* () {
164 openExternalMock.mockRejectedValue(new Error("open failed"));
165
166 const electronShell = yield* ElectronShell.ElectronShell;
167 const result = yield* electronShell.openExternal("https://example.com/path");
168
169 assert.equal(result, false);
170 }).pipe(Effect.provide(ElectronShell.layer)),
171 );
172});