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 results = yield* Effect.all([
97 electronShell.openExternal("zed://ssh/example.com/home/user/project"),
98 electronShell.openExternal("zed://ssh/example.com/"),
99 ]);
100
101 assert.deepEqual(results, [true, true]);
102 assert.deepEqual(openExternalMock.mock.calls, [
103 ["zed://ssh/example.com/home/user/project"],
104 ["zed://ssh/example.com/"],
105 ]);
106 }).pipe(Effect.provide(ElectronShell.layer)),
107 );
108
109 it.effect("does not open editor URLs that mix up link shapes", () =>
110 Effect.gen(function* () {
111 openExternalMock.mockResolvedValue(undefined);
112
113 const electronShell = yield* ElectronShell.ElectronShell;
114 const results = yield* Effect.all([
115 electronShell.openExternal("zed://extension/attacker"),
116 electronShell.openExternal("vscode://ssh/example.com/home/user/project"),
117 ]);
118
119 assert.deepEqual(results, [false, false]);
120 assert.equal(openExternalMock.mock.calls.length, 0);
121 }).pipe(Effect.provide(ElectronShell.layer)),
122 );
123
124 it.effect("does not open remote editor URLs with userinfo", () =>
125 Effect.gen(function* () {
126 openExternalMock.mockResolvedValue(undefined);
127
128 const electronShell = yield* ElectronShell.ElectronShell;
129 const results = yield* Effect.all([
130 electronShell.openExternal(
131 "vscode://user@vscode-remote/ssh-remote+example.com/home/user/project",
132 ),
133 electronShell.openExternal(
134 "vscode://:secret@vscode-remote/ssh-remote+example.com/home/user/project",
135 ),
136 electronShell.openExternal("zed://ssh/user@example.com/home/user/project"),
137 ]);
138
139 assert.deepEqual(results, [false, false, false]);
140 assert.equal(openExternalMock.mock.calls.length, 0);
141 }).pipe(Effect.provide(ElectronShell.layer)),
142 );
143
144 it.effect("does not open unsafe external URLs", () =>
145 Effect.gen(function* () {
146 const electronShell = yield* ElectronShell.ElectronShell;
147 const result = yield* electronShell.openExternal("file:///etc/passwd");
148
149 assert.equal(result, false);
150 assert.equal(openExternalMock.mock.calls.length, 0);
151 }).pipe(Effect.provide(ElectronShell.layer)),
152 );
153
154 it.effect("does not open non-remote editor URLs", () =>
155 Effect.gen(function* () {
156 openExternalMock.mockResolvedValue(undefined);
157
158 const electronShell = yield* ElectronShell.ElectronShell;
159 const result = yield* electronShell.openExternal(
160 "vscode://ms-python.python/some-command?argument=attacker",
161 );
162
163 assert.equal(result, false);
164 assert.equal(openExternalMock.mock.calls.length, 0);
165 }).pipe(Effect.provide(ElectronShell.layer)),
166 );
167
168 it.effect("returns false when Electron rejects openExternal", () =>
169 Effect.gen(function* () {
170 openExternalMock.mockRejectedValue(new Error("open failed"));
171
172 const electronShell = yield* ElectronShell.ElectronShell;
173 const result = yield* electronShell.openExternal("https://example.com/path");
174
175 assert.equal(result, false);
176 }).pipe(Effect.provide(ElectronShell.layer)),
177 );
178});