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

GuestInput.test.ts

129 lines5.2 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import { sendPageCommand, toKeyboardInputEvents } from "./GuestInput.ts";
4​
5/** A guest whose page reports `focusedDocument` for the focus probe. */
6const makeGuest = ({
7 focused,
8 focusedDocument = true,
9}: {
10 readonly focused: boolean;
11 readonly focusedDocument?: boolean;
12}) => {
13 const sendCommand = vi.fn(async (method: string, params?: { expression?: string }) =>
14 method === "Runtime.evaluate" && params?.expression?.endsWith("!== null")
15 ? { result: { value: focusedDocument } }
16 : { result: {} },
17 );
18 const webContents = {
19 focusedFrame: focused ? {} : null,
20 insertText: vi.fn(async () => {}),
21 sendInputEvent: vi.fn(),
22 reload: vi.fn(),
23 reloadIgnoringCache: vi.fn(),
24 };
25 return {
26 webContents,
27 sendCommand,
28 send: (method: string, params: Record<string, unknown>) =>
29 sendPageCommand(
30 webContents as unknown as Electron.WebContents,
31 { sendCommand } as unknown as Electron.Debugger,
32 method,
33 params,
34 ),
35 };
36};
37​
38describe("preview guest input", () => {
39 it.each([undefined, false, true])(
40 "reloads only the guest with ignoreCache=%s",
41 async (ignoreCache) => {
42 const guest = makeGuest({ focused: false });
43 await expect(
44 guest.send("Page.reload", ignoreCache === undefined ? {} : { ignoreCache }),
45 ).resolves.toEqual({});
46 expect(guest.webContents.reload).toHaveBeenCalledTimes(ignoreCache ? 0 : 1);
47 expect(guest.webContents.reloadIgnoringCache).toHaveBeenCalledTimes(ignoreCache ? 1 : 0);
48 expect(guest.sendCommand).not.toHaveBeenCalled();
49 },
50 );
51​
52 it("types into an unfocused guest's own page, not the app's focused field", async () => {
53 const guest = makeGuest({ focused: false });
54 await guest.send("Input.insertText", { text: "pairing-code" });
55 expect(guest.webContents.insertText).toHaveBeenCalledWith("pairing-code");
56 expect(guest.sendCommand).not.toHaveBeenCalledWith("Input.insertText", expect.anything());
57 });
58​
59 it("uses CDP input while the guest holds focus, where it already reaches the page", async () => {
60 const guest = makeGuest({ focused: true });
61 await guest.send("Input.insertText", { text: "x" });
62 await guest.send("Input.dispatchKeyEvent", { type: "keyUp", key: "a" });
63 expect(guest.sendCommand.mock.calls.map(([method]) => method)).toEqual([
64 "Input.insertText",
65 "Input.dispatchKeyEvent",
66 ]);
67 expect(guest.webContents.insertText).not.toHaveBeenCalled();
68 });
69​
70 it("refuses input when a cross-site frame holds the unfocused page's focus", async () => {
71 const guest = makeGuest({ focused: false, focusedDocument: false });
72 await expect(guest.send("Input.insertText", { text: "secret" })).rejects.toThrow(
73 /cross-site frame/,
74 );
75 expect(guest.webContents.insertText).not.toHaveBeenCalled();
76 });
77​
78 it("clears a field with Cmd+A and Delete while unfocused, never touching the clipboard", async () => {
79 const guest = makeGuest({ focused: false });
80 await guest.send("Input.dispatchKeyEvent", {
81 type: "rawKeyDown",
82 key: "a",
83 modifiers: 4,
84 commands: ["selectAll", "paste"],
85 });
86 await guest.send("Input.dispatchKeyEvent", { type: "rawKeyDown", key: "Delete" });
87 expect(guest.webContents.sendInputEvent.mock.calls.map(([event]) => event)).toEqual([
88 { type: "rawKeyDown", keyCode: "a", modifiers: ["meta"] },
89 { type: "rawKeyDown", keyCode: "Delete", modifiers: [] },
90 ]);
91 const scripts = guest.sendCommand.mock.calls.map(([, params]) => params?.expression ?? "");
92 expect(scripts.some((script) => script.includes('["selectAll"]'))).toBe(true);
93 expect(scripts.some((script) => script.includes("paste"))).toBe(false);
94 });
95​
96 it("does not run other page commands through the guest's direct input", async () => {
97 const guest = makeGuest({ focused: false });
98 await guest.send("Input.dispatchMouseEvent", { type: "mousePressed", x: 1, y: 1 });
99 expect(guest.sendCommand).toHaveBeenCalledWith("Input.dispatchMouseEvent", {
100 type: "mousePressed",
101 x: 1,
102 y: 1,
103 });
104 expect(guest.webContents.sendInputEvent).not.toHaveBeenCalled();
105 });
106});
107​
108describe("CDP key events as Electron input", () => {
109 it("sends a key's text as char events after its key down", () => {
110 expect(toKeyboardInputEvents({ type: "keyDown", key: "Enter", text: "\r" })).toEqual([
111 { type: "rawKeyDown", keyCode: "Enter", modifiers: [] },
112 { type: "char", keyCode: "\r", modifiers: [] },
113 ]);
114 expect(toKeyboardInputEvents({ type: "keyDown", key: "é", text: "é", modifiers: 8 })).toEqual([
115 { type: "rawKeyDown", keyCode: "é", modifiers: ["shift"] },
116 { type: "char", keyCode: "é", modifiers: ["shift"] },
117 ]);
118 });
119​
120 it("names arrows and function keys as Electron accelerators", () => {
121 expect(toKeyboardInputEvents({ type: "keyUp", key: "ArrowLeft", modifiers: 3 })).toEqual([
122 { type: "keyUp", keyCode: "Left", modifiers: ["alt", "control"] },
123 ]);
124 expect(toKeyboardInputEvents({ type: "rawKeyDown", key: "F12" })).toEqual([
125 { type: "rawKeyDown", keyCode: "F12", modifiers: [] },
126 ]);
127 expect(toKeyboardInputEvents({ type: "rawKeyDown", key: "AudioVolumeUp" })).toEqual([]);
128 });
129});