Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - Stands in for an Electron webContents.
2import { assert, describe, it } from "@effect/vitest";
3import * as NodeEvents from "node:events";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7
8import * as ElectronWindow from "../electron/ElectronWindow.ts";
9import { WEB_LINK_OPEN_CHANNEL } from "../ipc/channels.ts";
10import * as DesktopWebLinks from "./DesktopWebLinks.ts";
11
12const makeWindow = () => {
13 const sent: Array<string> = [];
14 const revealed: Array<unknown> = [];
15 const webContents = Object.assign(new NodeEvents.EventEmitter(), {
16 isDestroyed: () => false,
17 send: (channel: string, url: string) => {
18 if (channel === WEB_LINK_OPEN_CHANNEL) sent.push(url);
19 },
20 });
21 const window = { webContents } as unknown as Electron.BrowserWindow;
22 const reload = () =>
23 webContents.emit("did-start-navigation", { isMainFrame: true, isSameDocument: false });
24 const layer = DesktopWebLinks.layer.pipe(
25 Layer.provide(
26 Layer.succeed(
27 ElectronWindow.ElectronWindow,
28 ElectronWindow.ElectronWindow.of({
29 currentMainOrFirst: Effect.succeed(Option.some(window)),
30 reveal: (target: Electron.BrowserWindow) => Effect.sync(() => void revealed.push(target)),
31 } as unknown as ElectronWindow.ElectronWindow["Service"]),
32 ),
33 ),
34 );
35 return { sent, revealed, layer, reload };
36};
37
38describe("DesktopWebLinks", () => {
39 it.effect("holds links until the renderer listens, then delivers them in order", () => {
40 const { sent, revealed, layer } = makeWindow();
41 return Effect.gen(function* () {
42 const webLinks = yield* DesktopWebLinks.DesktopWebLinks;
43 yield* webLinks.receive("https://example.com/first");
44 yield* webLinks.receive("https://example.com/second");
45 assert.deepStrictEqual(sent, []);
46
47 yield* webLinks.setRendererReady(true);
48 assert.deepStrictEqual(sent, ["https://example.com/first", "https://example.com/second"]);
49 assert.strictEqual(revealed.length, 1);
50
51 yield* webLinks.receive("https://example.com/third");
52 assert.deepStrictEqual(sent.at(-1), "https://example.com/third");
53
54 yield* webLinks.setRendererReady(false);
55 yield* webLinks.receive("https://example.com/fourth");
56 assert.strictEqual(sent.length, 3);
57 }).pipe(Effect.provide(layer));
58 });
59
60 it.effect("holds links that arrive while the web app reloads until it listens again", () => {
61 const { sent, layer, reload } = makeWindow();
62 return Effect.gen(function* () {
63 const webLinks = yield* DesktopWebLinks.DesktopWebLinks;
64 yield* webLinks.setRendererReady(true);
65 reload();
66 yield* webLinks.receive("https://example.com/during-reload");
67 assert.deepStrictEqual(sent, []);
68
69 yield* webLinks.setRendererReady(true);
70 assert.deepStrictEqual(sent, ["https://example.com/during-reload"]);
71 }).pipe(Effect.provide(layer));
72 });
73});