Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Option from "effect/Option";
5import type * as Electron from "electron";
6import { beforeEach, vi } from "vitest";
7
8const { registerFileProtocolMock, registerSchemesAsPrivilegedMock, unregisterProtocolMock } =
9 vi.hoisted(() => ({
10 registerFileProtocolMock: vi.fn(),
11 registerSchemesAsPrivilegedMock: vi.fn(),
12 unregisterProtocolMock: vi.fn(),
13 }));
14
15vi.mock("electron", () => ({
16 protocol: {
17 registerFileProtocol: registerFileProtocolMock,
18 registerSchemesAsPrivileged: registerSchemesAsPrivilegedMock,
19 unregisterProtocol: unregisterProtocolMock,
20 },
21}));
22
23import * as ElectronProtocol from "./ElectronProtocol.ts";
24
25describe("ElectronProtocol", () => {
26 beforeEach(() => {
27 registerFileProtocolMock.mockReset();
28 registerSchemesAsPrivilegedMock.mockReset();
29 unregisterProtocolMock.mockReset();
30 });
31
32 it("normalizes safe desktop protocol pathnames", () => {
33 assert.equal(
34 Option.getOrNull(ElectronProtocol.normalizeDesktopProtocolPathname("/settings/./general")),
35 "settings/general",
36 );
37 assert.isTrue(Option.isNone(ElectronProtocol.normalizeDesktopProtocolPathname("/../secret")));
38 });
39
40 it.effect("registers desktop scheme privileges through a layer", () =>
41 Effect.scoped(
42 Layer.build(ElectronProtocol.layerSchemePrivileges).pipe(
43 Effect.andThen(
44 Effect.sync(() => {
45 assert.deepEqual(registerSchemesAsPrivilegedMock.mock.calls, [
46 [
47 [
48 {
49 scheme: "t3",
50 privileges: {
51 standard: true,
52 secure: true,
53 supportFetchAPI: true,
54 corsEnabled: true,
55 },
56 },
57 ],
58 ],
59 ]);
60 }),
61 ),
62 ),
63 ),
64 );
65
66 it.effect("scopes registered file protocols", () =>
67 Effect.gen(function* () {
68 let capturedHandler:
69 | ((
70 request: Electron.ProtocolRequest,
71 callback: (response: Electron.ProtocolResponse) => void,
72 ) => void)
73 | undefined;
74
75 registerFileProtocolMock.mockImplementation((_scheme, handler) => {
76 capturedHandler = handler;
77 return true;
78 });
79
80 const response = yield* Effect.scoped(
81 Effect.gen(function* () {
82 const electronProtocol = yield* ElectronProtocol.ElectronProtocol;
83 yield* electronProtocol.registerFileProtocol({
84 scheme: "t3",
85 handler: () => Effect.succeed({ path: "/app/index.html" }),
86 });
87
88 assert.isDefined(capturedHandler);
89 return yield* Effect.callback<Electron.ProtocolResponse>((resume) => {
90 capturedHandler?.({ url: "t3://app/" } as Electron.ProtocolRequest, (response) =>
91 resume(Effect.succeed(response)),
92 );
93 });
94 }),
95 );
96
97 assert.deepEqual(response, { path: "/app/index.html" });
98 assert.deepEqual(
99 registerFileProtocolMock.mock.calls.map((call) => call[0]),
100 ["t3"],
101 );
102 assert.deepEqual(unregisterProtocolMock.mock.calls, [["t3"]]);
103 }).pipe(Effect.provide(ElectronProtocol.layer)),
104 );
105});