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

DesktopBrowserHost.test.ts

149 lines5.3 KB

1// @effect-diagnostics nodeBuiltinImport:off - Stands in for an Electron debugger.
2import { describe, expect, it } from "@effect/vitest";
3import { DesktopBrowserEvent } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6import * as Schema from "effect/Schema";
7import * as Stream from "effect/Stream";
8import * as NodeEvents from "node:events";
9​
10import * as DesktopBrowserHost from "./DesktopBrowserHost.ts";
11​
12const decodeEvent = Schema.decodeUnknownSync(Schema.fromJsonString(DesktopBrowserEvent));
13const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
14const decodeCdpReply = Schema.decodeUnknownSync(
15 Schema.fromJsonString(Schema.Struct({ id: Schema.Number })),
16);
17const key = { threadId: "thread-1", tabId: "tab-1" };
18​
19const makeDebuggee = () => {
20 const emitter = new NodeEvents.EventEmitter();
21 const pending: Array<() => void> = [];
22 const debuggee = Object.assign(emitter, {
23 sendCommand: (method: string) =>
24 new Promise((resolve) => {
25 if (method === "Target.getTargetInfo") {
26 resolve({ targetInfo: { targetId: "GUEST" } });
27 return;
28 }
29 pending.push(() => resolve({ method }));
30 }),
31 });
32 const webContents = {
33 getURL: () => "http://localhost/",
34 getTitle: () => "Page",
35 getUserAgent: () => "Electron",
36 };
37 return {
38 tab: {
39 webContents: webContents as unknown as Electron.WebContents,
40 debugger: debuggee as unknown as Electron.Debugger,
41 },
42 emit: (method: string, params: unknown) => emitter.emit("message", {}, method, params, ""),
43 release: () => pending.splice(0).forEach((resolve) => resolve()),
44 };
45};
46​
47const takeEvents = (host: DesktopBrowserHost.DesktopBrowserHost["Service"], count: number) =>
48 host.events.pipe(
49 Stream.take(count),
50 Stream.runCollect,
51 Effect.map((lines) => lines.map((line) => decodeEvent(new TextDecoder().decode(line)))),
52 );
53​
54describe("DesktopBrowserHost", () => {
55 it.effect("announces tabs already attached to a backend that starts later", () =>
56 Effect.gen(function* () {
57 const host = yield* DesktopBrowserHost.make;
58 host.attach(key, makeDebuggee().tab);
59​
60 expect(yield* takeEvents(host, 1)).toEqual([{ type: "attached", ...key }]);
61 expect(yield* takeEvents(host, 1)).toEqual([{ type: "attached", ...key }]);
62 }),
63 );
64​
65 it.effect("drops replies from a relay the server released", () =>
66 Effect.gen(function* () {
67 const host = yield* DesktopBrowserHost.make;
68 const debuggee = makeDebuggee();
69 host.attach(key, debuggee.tab);
70 const reader = yield* takeEvents(host, 2).pipe(Effect.forkScoped);
71​
72 yield* Effect.yieldNow;
73 const command = (id: number, method: string) =>
74 host.handleCommandLine(
75 encodeJson({
76 type: "cdp",
77 ...key,
78 message: encodeJson({ id, method, sessionId: "t3-preview-page" }),
79 }),
80 );
81 yield* command(1, "Page.captureScreenshot");
82 yield* host.handleCommandLine(encodeJson({ type: "release", ...key }));
83 yield* command(2, "DOM.enable");
84 debuggee.release();
85 yield* Effect.promise(() => new Promise((resolve) => setImmediate(resolve)));
86 const [, reply] = yield* Fiber.join(reader);
87​
88 expect(reply).toMatchObject({ type: "cdp" });
89 expect(decodeCdpReply((reply as { message: string }).message).id).toBe(2);
90 }).pipe(Effect.scoped),
91 );
92​
93 it.effect("saves a server tab's download under its CDP guid where the server asked", () =>
94 Effect.gen(function* () {
95 const host = yield* DesktopBrowserHost.make;
96 const debuggee = makeDebuggee();
97 host.attach(key, debuggee.tab);
98 const paths: Array<string> = [];
99 const item = {
100 setSavePath: (path: string) => void paths.push(path),
101 } as unknown as Electron.DownloadItem;
102​
103 expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(false);
104 yield* host.handleCommandLine(
105 encodeJson({
106 type: "cdp",
107 ...key,
108 message: encodeJson({
109 id: 1,
110 method: "Browser.setDownloadBehavior",
111 params: { behavior: "allowAndName", downloadPath: "/srv/downloads" },
112 }),
113 }),
114 );
115 debuggee.emit("Browser.downloadWillBegin", { guid: "guid-1", suggestedFilename: "r.csv" });
116 expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(true);
117 expect(paths).toEqual(["/srv/downloads/guid-1"]);
118 }),
119 );
120​
121 it.effect("tells a download the person clicked from one the agent's input started", () =>
122 Effect.gen(function* () {
123 const host = yield* DesktopBrowserHost.make;
124 const debuggee = makeDebuggee();
125 host.attach(key, debuggee.tab);
126​
127 yield* host.handleCommandLine(
128 encodeJson({
129 type: "cdp",
130 ...key,
131 message: encodeJson({ id: 1, method: "DOM.getDocument", params: {} }),
132 }),
133 );
134 expect(host.humanStartedDownload(debuggee.tab.webContents)).toBe(true);
135 yield* host.handleCommandLine(
136 encodeJson({
137 type: "cdp",
138 ...key,
139 message: encodeJson({
140 id: 2,
141 method: "Input.dispatchMouseEvent",
142 params: { type: "mousePressed", x: 1, y: 1 },
143 }),
144 }),
145 );
146 expect(host.humanStartedDownload(debuggee.tab.webContents)).toBe(false);
147 }),
148 );
149});