Open History to see the last commit
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("types into an unfocused guest's own page, not the app's focused field", async () => {
40 const guest = makeGuest({ focused: false });
41 await guest.send("Input.insertText", { text: "pairing-code" });
42 expect(guest.webContents.insertText).toHaveBeenCalledWith("pairing-code");
43 expect(guest.sendCommand).not.toHaveBeenCalledWith("Input.insertText", expect.anything());
44 });
45
46 it("refuses input when a cross-site frame holds the unfocused page's focus", async () => {
47 const guest = makeGuest({ focused: false, focusedDocument: false });
48 await expect(guest.send("Input.insertText", { text: "secret" })).rejects.toThrow(
49 /cross-site frame/,
50 );
51 expect(guest.webContents.insertText).not.toHaveBeenCalled();
52 });
53});
54
55describe("CDP key events as Electron input", () => {
56 it("sends a key's text as char events after its key down", () => {
57 expect(toKeyboardInputEvents({ type: "keyDown", key: "Enter", text: "\r" })).toEqual([
58 { type: "rawKeyDown", keyCode: "Enter", modifiers: [] },
59 { type: "char", keyCode: "\r", modifiers: [] },
60 ]);
61 expect(toKeyboardInputEvents({ type: "keyDown", key: "é", text: "é", modifiers: 8 })).toEqual([
62 { type: "rawKeyDown", keyCode: "é", modifiers: ["shift"] },
63 { type: "char", keyCode: "é", modifiers: ["shift"] },
64 ]);
65 });
66
67 // regression: #18556 agent typing in a preview tab landed in the app instead of the guest
68 it("names arrows and function keys as Electron accelerators", () => {
69 expect(toKeyboardInputEvents({ type: "keyUp", key: "ArrowLeft", modifiers: 3 })).toEqual([
70 { type: "keyUp", keyCode: "Left", modifiers: ["alt", "control"] },
71 ]);
72 expect(toKeyboardInputEvents({ type: "rawKeyDown", key: "F12" })).toEqual([
73 { type: "rawKeyDown", keyCode: "F12", modifiers: [] },
74 ]);
75 expect(toKeyboardInputEvents({ type: "rawKeyDown", key: "AudioVolumeUp" })).toEqual([]);
76 });
77});