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

GuestInput.test.ts

76 lines2.9 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import { sendPageCommand, toKeyboardInputEvents } from "./GuestInput.ts";
4​
5const makeGuest = ({
6 focused,
7 focusedDocument = true,
8}: {
9 readonly focused: boolean;
10 readonly focusedDocument?: boolean;
11}) => {
12 const sendCommand = vi.fn(async (method: string, params?: { expression?: string }) =>
13 method === "Runtime.evaluate" && params?.expression?.endsWith("!== null")
14 ? { result: { value: focusedDocument } }
15 : { result: {} },
16 );
17 const webContents = {
18 focusedFrame: focused ? {} : null,
19 insertText: vi.fn(async () => {}),
20 sendInputEvent: vi.fn(),
21 reload: vi.fn(),
22 reloadIgnoringCache: vi.fn(),
23 };
24 return {
25 webContents,
26 sendCommand,
27 send: (method: string, params: Record<string, unknown>) =>
28 sendPageCommand(
29 webContents as unknown as Electron.WebContents,
30 { sendCommand } as unknown as Electron.Debugger,
31 method,
32 params,
33 ),
34 };
35};
36​
37describe("preview guest input", () => {
38 it("types into an unfocused guest's own page, not the app's focused field", async () => {
39 const guest = makeGuest({ focused: false });
40 await guest.send("Input.insertText", { text: "pairing-code" });
41 expect(guest.webContents.insertText).toHaveBeenCalledWith("pairing-code");
42 expect(guest.sendCommand).not.toHaveBeenCalledWith("Input.insertText", expect.anything());
43 });
44​
45 it("refuses input when a cross-site frame holds the unfocused page's focus", async () => {
46 const guest = makeGuest({ focused: false, focusedDocument: false });
47 await expect(guest.send("Input.insertText", { text: "secret" })).rejects.toThrow(
48 /cross-site frame/,
49 );
50 expect(guest.webContents.insertText).not.toHaveBeenCalled();
51 });
52});
53​
54describe("CDP key events as Electron input", () => {
55 it("sends a key's text as char events after its key down", () => {
56 expect(toKeyboardInputEvents({ type: "keyDown", key: "Enter", text: "\r" })).toEqual([
57 { type: "rawKeyDown", keyCode: "Enter", modifiers: [] },
58 { type: "char", keyCode: "\r", modifiers: [] },
59 ]);
60 expect(toKeyboardInputEvents({ type: "keyDown", key: "é", text: "é", modifiers: 8 })).toEqual([
61 { type: "rawKeyDown", keyCode: "é", modifiers: ["shift"] },
62 { type: "char", keyCode: "é", modifiers: ["shift"] },
63 ]);
64 });
65​
66 // regression: #18556 agent typing in a preview tab landed in the app instead of the guest
67 it("names arrows and function keys as Electron accelerators", () => {
68 expect(toKeyboardInputEvents({ type: "keyUp", key: "ArrowLeft", modifiers: 3 })).toEqual([
69 { type: "keyUp", keyCode: "Left", modifiers: ["alt", "control"] },
70 ]);
71 expect(toKeyboardInputEvents({ type: "rawKeyDown", key: "F12" })).toEqual([
72 { type: "rawKeyDown", keyCode: "F12", modifiers: [] },
73 ]);
74 expect(toKeyboardInputEvents({ type: "rawKeyDown", key: "AudioVolumeUp" })).toEqual([]);
75 });
76});