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 DesktopWindow from "../window/DesktopWindow.ts";
9import * as ElectronWindow from "../electron/ElectronWindow.ts";
10import { WEB_LINK_OPEN_CHANNEL } from "../ipc/channels.ts";
11import * as DesktopWebLinks from "./DesktopWebLinks.ts";
12
13const makeWindow = () => {
14 const sent: Array<string> = [];
15 const revealed: Array<unknown> = [];
16 const open = () => {
17 const webContents = Object.assign(new NodeEvents.EventEmitter(), {
18 isDestroyed: () => false,
19 send: (channel: string, url: string) => {
20 if (channel === WEB_LINK_OPEN_CHANNEL) sent.push(url);
21 },
22 });
23 return { webContents } as unknown as Electron.BrowserWindow;
24 };
25 let window = Option.some(open());
26 const reload = () => {
27 if (Option.isSome(window)) {
28 window.value.webContents.emit("did-start-navigation", {
29 isMainFrame: true,
30 isSameDocument: false,
31 });
32 }
33 };
34 const close = () => {
35 const closed = window;
36 window = Option.none();
37 if (Option.isSome(closed)) closed.value.webContents.emit("destroyed");
38 };
39 const reopen = () => {
40 window = Option.some(open());
41 };
42 const layer = DesktopWebLinks.layer.pipe(
43 Layer.provide(
44 Layer.succeed(DesktopWindow.DesktopWindow, {
45 activate: Effect.sync(() => void revealed.push(window)),
46 } as unknown as DesktopWindow.DesktopWindow["Service"]),
47 ),
48 Layer.provide(
49 Layer.succeed(
50 ElectronWindow.ElectronWindow,
51 ElectronWindow.ElectronWindow.of({
52 main: Effect.sync(() => window),
53 currentMainOrFirst: Effect.sync(() => window),
54 reveal: (target: Electron.BrowserWindow) => Effect.sync(() => void revealed.push(target)),
55 } as unknown as ElectronWindow.ElectronWindow["Service"]),
56 ),
57 ),
58 );
59 return { sent, revealed, layer, reload, close, reopen };
60};
61
62describe("DesktopWebLinks", () => {
63 it.effect("holds links until the renderer listens, then delivers them in order", () => {
64 const { sent, revealed, layer } = makeWindow();
65 return Effect.gen(function* () {
66 const webLinks = yield* DesktopWebLinks.DesktopWebLinks;
67 yield* webLinks.receive("https://example.com/first");
68 yield* webLinks.receive("https://example.com/second");
69 assert.deepStrictEqual(sent, []);
70
71 yield* webLinks.setRendererReady(true);
72 assert.deepStrictEqual(sent, ["https://example.com/first", "https://example.com/second"]);
73 assert.strictEqual(revealed.length, 1);
74
75 yield* webLinks.receive("https://example.com/third");
76 assert.deepStrictEqual(sent.at(-1), "https://example.com/third");
77
78 yield* webLinks.setRendererReady(false);
79 yield* webLinks.receive("https://example.com/fourth");
80 assert.strictEqual(sent.length, 3);
81 assert.strictEqual(revealed.length, 2);
82 }).pipe(Effect.provide(layer));
83 });
84
85 it.effect("holds links that arrive while the web app reloads until it listens again", () => {
86 const { sent, layer, reload } = makeWindow();
87 return Effect.gen(function* () {
88 const webLinks = yield* DesktopWebLinks.DesktopWebLinks;
89 yield* webLinks.setRendererReady(true);
90 reload();
91 yield* webLinks.receive("https://example.com/during-reload");
92 assert.deepStrictEqual(sent, []);
93
94 yield* webLinks.setRendererReady(true);
95 assert.deepStrictEqual(sent, ["https://example.com/during-reload"]);
96 }).pipe(Effect.provide(layer));
97 });
98
99 it.effect("asks for a window when a link arrives after the main window closed", () => {
100 const { sent, revealed, layer, close, reopen } = makeWindow();
101 return Effect.gen(function* () {
102 const webLinks = yield* DesktopWebLinks.DesktopWebLinks;
103 close();
104 yield* webLinks.receive("https://example.com/at-launch");
105 assert.strictEqual(revealed.length, 0);
106
107 reopen();
108 yield* webLinks.setRendererReady(true);
109 assert.deepStrictEqual(sent, ["https://example.com/at-launch"]);
110 assert.strictEqual(revealed.length, 1);
111
112 close();
113 yield* webLinks.receive("https://example.com/after-close");
114 assert.deepStrictEqual(sent, ["https://example.com/at-launch"]);
115 assert.strictEqual(revealed.length, 2);
116
117 reopen();
118 yield* webLinks.setRendererReady(true);
119 assert.deepStrictEqual(sent, [
120 "https://example.com/at-launch",
121 "https://example.com/after-close",
122 ]);
123 }).pipe(Effect.provide(layer));
124 });
125});