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("opens the Full Disk Access settings anchor", () =>
40 Effect.gen(function* () {
41 openExternalMock.mockResolvedValue(undefined);
42
43 const electronShell = yield* ElectronShell.ElectronShell;
44 const result = yield* electronShell.openSystemSettings("full-disk-access");
45
46 assert.equal(result, true);
47 assert.deepEqual(openExternalMock.mock.calls, [
48 ["x-apple.systempreferences:com.apple.settings.PrivacySecurity.extension?Privacy_AllFiles"],
49 ]);
50 }).pipe(Effect.provide(ElectronShell.layer)),
51 );
52
53 it.effect("opens remote SSH editor URLs", () =>
54 Effect.gen(function* () {
55 openExternalMock.mockResolvedValue(undefined);
56
57 const electronShell = yield* ElectronShell.ElectronShell;
58 const result = yield* electronShell.openExternal(
59 "vscode://vscode-remote/ssh-remote+example.com/home/user/project",
60 );
61
62 assert.equal(result, true);
63 assert.deepEqual(openExternalMock.mock.calls, [
64 ["vscode://vscode-remote/ssh-remote+example.com/home/user/project"],
65 ]);
66 }).pipe(Effect.provide(ElectronShell.layer)),
67 );
68
69 it.effect("does not open remote editor URLs with userinfo", () =>
70 Effect.gen(function* () {
71 openExternalMock.mockResolvedValue(undefined);
72
73 const electronShell = yield* ElectronShell.ElectronShell;
74 const results = yield* Effect.all([
75 electronShell.openExternal(
76 "vscode://user@vscode-remote/ssh-remote+example.com/home/user/project",
77 ),
78 electronShell.openExternal(
79 "vscode://:secret@vscode-remote/ssh-remote+example.com/home/user/project",
80 ),
81 ]);
82
83 assert.deepEqual(results, [false, false]);
84 assert.equal(openExternalMock.mock.calls.length, 0);
85 }).pipe(Effect.provide(ElectronShell.layer)),
86 );
87
88 it.effect("does not open unsafe external URLs", () =>
89 Effect.gen(function* () {
90 const electronShell = yield* ElectronShell.ElectronShell;
91 const result = yield* electronShell.openExternal("file:///etc/passwd");
92
93 assert.equal(result, false);
94 assert.equal(openExternalMock.mock.calls.length, 0);
95 }).pipe(Effect.provide(ElectronShell.layer)),
96 );
97
98 it.effect("does not open non-remote editor URLs", () =>
99 Effect.gen(function* () {
100 openExternalMock.mockResolvedValue(undefined);
101
102 const electronShell = yield* ElectronShell.ElectronShell;
103 const result = yield* electronShell.openExternal(
104 "vscode://ms-python.python/some-command?argument=attacker",
105 );
106
107 assert.equal(result, false);
108 assert.equal(openExternalMock.mock.calls.length, 0);
109 }).pipe(Effect.provide(ElectronShell.layer)),
110 );
111
112 it.effect("returns false when Electron rejects openExternal", () =>
113 Effect.gen(function* () {
114 openExternalMock.mockRejectedValue(new Error("open failed"));
115
116 const electronShell = yield* ElectronShell.ElectronShell;
117 const result = yield* electronShell.openExternal("https://example.com/path");
118
119 assert.equal(result, false);
120 }).pipe(Effect.provide(ElectronShell.layer)),
121 );
122});