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