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

DesktopWebLinks.test.ts

89 lines3.8 KB

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("treats only http and https as web links", () => {
40 assert.isTrue(DesktopWebLinks.isWebLink("https://example.com/"));
41 assert.isTrue(DesktopWebLinks.isWebLink("http://localhost:3000/a?b=1"));
42 assert.isFalse(DesktopWebLinks.isWebLink("t3code://app/welcome"));
43 assert.isFalse(DesktopWebLinks.isWebLink("mailto:hello@example.com"));
44 assert.isFalse(DesktopWebLinks.isWebLink("not a url"));
45 });
46​
47 it("opens HTML files the way a browser does, and nothing else by path", () => {
48 assert.isTrue(DesktopWebLinks.isWebPageFile("/Users/me/report.html"));
49 assert.isTrue(DesktopWebLinks.isWebPageFile("/tmp/page.HTM"));
50 assert.isTrue(DesktopWebLinks.isWebPageFile("/tmp/page.xhtml"));
51 assert.isFalse(DesktopWebLinks.isWebPageFile("/tmp/notes.md"));
52 assert.isFalse(DesktopWebLinks.isWebPageFile("/tmp/html"));
53 });
54​
55 it.effect("holds links until the renderer listens, then delivers them in order", () => {
56 const { sent, revealed, layer } = makeWindow();
57 return Effect.gen(function* () {
58 const webLinks = yield* DesktopWebLinks.DesktopWebLinks;
59 yield* webLinks.receive("https://example.com/first");
60 yield* webLinks.receive("https://example.com/second");
61 assert.deepStrictEqual(sent, []);
62​
63 yield* webLinks.setRendererReady(true);
64 assert.deepStrictEqual(sent, ["https://example.com/first", "https://example.com/second"]);
65 assert.strictEqual(revealed.length, 1);
66​
67 yield* webLinks.receive("https://example.com/third");
68 assert.deepStrictEqual(sent.at(-1), "https://example.com/third");
69​
70 yield* webLinks.setRendererReady(false);
71 yield* webLinks.receive("https://example.com/fourth");
72 assert.strictEqual(sent.length, 3);
73 }).pipe(Effect.provide(layer));
74 });
75​
76 it.effect("holds links that arrive while the web app reloads until it listens again", () => {
77 const { sent, layer, reload } = makeWindow();
78 return Effect.gen(function* () {
79 const webLinks = yield* DesktopWebLinks.DesktopWebLinks;
80 yield* webLinks.setRendererReady(true);
81 reload();
82 yield* webLinks.receive("https://example.com/during-reload");
83 assert.deepStrictEqual(sent, []);
84​
85 yield* webLinks.setRendererReady(true);
86 assert.deepStrictEqual(sent, ["https://example.com/during-reload"]);
87 }).pipe(Effect.provide(layer));
88 });
89});