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

DesktopBrowserHost.test.ts

148 lines5.2 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 const [, reply] = yield* Fiber.join(reader);
86​
87 expect(reply).toMatchObject({ type: "cdp" });
88 expect(decodeCdpReply((reply as { message: string }).message).id).toBe(2);
89 }).pipe(Effect.scoped),
90 );
91​
92 it.effect("saves a server tab's download under its CDP guid where the server asked", () =>
93 Effect.gen(function* () {
94 const host = yield* DesktopBrowserHost.make;
95 const debuggee = makeDebuggee();
96 host.attach(key, debuggee.tab);
97 const paths: Array<string> = [];
98 const item = {
99 setSavePath: (path: string) => void paths.push(path),
100 } as unknown as Electron.DownloadItem;
101​
102 expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(false);
103 yield* host.handleCommandLine(
104 encodeJson({
105 type: "cdp",
106 ...key,
107 message: encodeJson({
108 id: 1,
109 method: "Browser.setDownloadBehavior",
110 params: { behavior: "allowAndName", downloadPath: "/srv/downloads" },
111 }),
112 }),
113 );
114 debuggee.emit("Browser.downloadWillBegin", { guid: "guid-1", suggestedFilename: "r.csv" });
115 expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(true);
116 expect(paths).toEqual(["/srv/downloads/guid-1"]);
117 }),
118 );
119​
120 it.effect("tells a download the person clicked from one the agent's input started", () =>
121 Effect.gen(function* () {
122 const host = yield* DesktopBrowserHost.make;
123 const debuggee = makeDebuggee();
124 host.attach(key, debuggee.tab);
125​
126 yield* host.handleCommandLine(
127 encodeJson({
128 type: "cdp",
129 ...key,
130 message: encodeJson({ id: 1, method: "DOM.getDocument", params: {} }),
131 }),
132 );
133 expect(host.humanStartedDownload(debuggee.tab.webContents)).toBe(true);
134 yield* host.handleCommandLine(
135 encodeJson({
136 type: "cdp",
137 ...key,
138 message: encodeJson({
139 id: 2,
140 method: "Input.dispatchMouseEvent",
141 params: { type: "mousePressed", x: 1, y: 1 },
142 }),
143 }),
144 );
145 expect(host.humanStartedDownload(debuggee.tab.webContents)).toBe(false);
146 }),
147 );
148});