Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ElectronShell.test.ts

59 lines1.8 KB

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("does not open unsafe external URLs", () =>
40 Effect.gen(function* () {
41 const electronShell = yield* ElectronShell.ElectronShell;
42 const result = yield* electronShell.openExternal("file:///etc/passwd");
43​
44 assert.equal(result, false);
45 assert.equal(openExternalMock.mock.calls.length, 0);
46 }).pipe(Effect.provide(ElectronShell.layer)),
47 );
48​
49 it.effect("returns false when Electron rejects openExternal", () =>
50 Effect.gen(function* () {
51 openExternalMock.mockRejectedValue(new Error("open failed"));
52​
53 const electronShell = yield* ElectronShell.ElectronShell;
54 const result = yield* electronShell.openExternal("https://example.com/path");
55​
56 assert.equal(result, false);
57 }).pipe(Effect.provide(ElectronShell.layer)),
58 );
59});