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 };
23 return {
24 webContents,
25 sendCommand,
26 send: (method: string, params: Record<string, unknown>) =>
27 sendPageCommand(
28 webContents as unknown as Electron.WebContents,
29 { sendCommand } as unknown as Electron.Debugger,
30 method,
31 params,
32 ),
33 };
34};
35
36describe("preview guest input", () => {
37 it("types into an unfocused guest's own page, not the app's focused field", async () => {
38 const guest = makeGuest({ focused: false });
39 await guest.send("Input.insertText", { text: "pairing-code" });
40 expect(guest.webContents.insertText).toHaveBeenCalledWith("pairing-code");
41 expect(guest.sendCommand).not.toHaveBeenCalledWith("Input.insertText", expect.anything());
42 });
43
44 it("uses CDP input while the guest holds focus, where it already reaches the page", async () => {
45 const guest = makeGuest({ focused: true });
46 await guest.send("Input.insertText", { text: "x" });
47 await guest.send("Input.dispatchKeyEvent", { type: "keyUp", key: "a" });
48 expect(guest.sendCommand.mock.calls.map(([method]) => method)).toEqual([
49 "Input.insertText",
50 "Input.dispatchKeyEvent",
51 ]);
52 expect(guest.webContents.insertText).not.toHaveBeenCalled();
53 });
54
55 it("refuses input when a cross-site frame holds the unfocused page's focus", async () => {
56 const guest = makeGuest({ focused: false, focusedDocument: false });
57 await expect(guest.send("Input.insertText", { text: "secret" })).rejects.toThrow(
58 /cross-site frame/,
59 );
60 expect(guest.webContents.insertText).not.toHaveBeenCalled();
61 });
62
63 it("clears a field with Cmd+A and Delete while unfocused, never touching the clipboard", async () => {
64 const guest = makeGuest({ focused: false });
65 await guest.send("Input.dispatchKeyEvent", {
66 type: "rawKeyDown",
67 key: "a",
68 modifiers: 4,
69 commands: ["selectAll", "paste"],
70 });
71 await guest.send("Input.dispatchKeyEvent", { type: "rawKeyDown", key: "Delete" });
72 expect(guest.webContents.sendInputEvent.mock.calls.map(([event]) => event)).toEqual([
73 { type: "rawKeyDown", keyCode: "a", modifiers: ["meta"] },
74 { type: "rawKeyDown", keyCode: "Delete", modifiers: [] },
75 ]);
76 const scripts = guest.sendCommand.mock.calls.map(([, params]) => params?.expression ?? "");
77 expect(scripts.some((script) => script.includes('["selectAll"]'))).toBe(true);
78 expect(scripts.some((script) => script.includes("paste"))).toBe(false);
79 });
80
81 it("does not run other page commands through the guest's direct input", async () => {
82 const guest = makeGuest({ focused: false });
83 await guest.send("Input.dispatchMouseEvent", { type: "mousePressed", x: 1, y: 1 });
84 expect(guest.sendCommand).toHaveBeenCalledWith("Input.dispatchMouseEvent", {
85 type: "mousePressed",
86 x: 1,
87 y: 1,
88 });
89 expect(guest.webContents.sendInputEvent).not.toHaveBeenCalled();
90 });
91});
92
93describe("CDP key events as Electron input", () => {
94 it("sends a key's text as char events after its key down", () => {
95 expect(toKeyboardInputEvents({ type: "keyDown", key: "Enter", text: "\r" })).toEqual([
96 { type: "rawKeyDown", keyCode: "Enter", modifiers: [] },
97 { type: "char", keyCode: "\r", modifiers: [] },
98 ]);
99 expect(toKeyboardInputEvents({ type: "keyDown", key: "é", text: "é", modifiers: 8 })).toEqual([
100 { type: "rawKeyDown", keyCode: "é", modifiers: ["shift"] },
101 { type: "char", keyCode: "é", modifiers: ["shift"] },
102 ]);
103 });
104
105 it("names arrows and function keys as Electron accelerators", () => {
106 expect(toKeyboardInputEvents({ type: "keyUp", key: "ArrowLeft", modifiers: 3 })).toEqual([
107 { type: "keyUp", keyCode: "Left", modifiers: ["alt", "control"] },
108 ]);
109 expect(toKeyboardInputEvents({ type: "rawKeyDown", key: "F12" })).toEqual([
110 { type: "rawKeyDown", keyCode: "F12", modifiers: [] },
111 ]);
112 expect(toKeyboardInputEvents({ type: "rawKeyDown", key: "AudioVolumeUp" })).toEqual([]);
113 });
114});