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

PreviewKeyboard.test.ts

184 lines6.1 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 makePreviewAutomationKeySequence,
5 makePreviewAutomationNativeKeySequence,
6} from "./PreviewKeyboard.ts";
7​
8describe("preview keyboard packets", () => {
9 it("includes the Chromium virtual key code and Enter text", () => {
10 expect(makePreviewAutomationKeySequence({ key: "Enter" })).toEqual({
11 keyDown: {
12 type: "keyDown",
13 key: "Enter",
14 code: "Enter",
15 modifiers: 0,
16 windowsVirtualKeyCode: 13,
17 location: 0,
18 isKeypad: false,
19 text: "\r",
20 unmodifiedText: "\r",
21 },
22 keyUp: {
23 type: "keyUp",
24 key: "Enter",
25 code: "Enter",
26 modifiers: 0,
27 windowsVirtualKeyCode: 13,
28 location: 0,
29 isKeypad: false,
30 },
31 signal: { kind: "key", key: "Enter", code: "Enter" },
32 });
33 });
34​
35 it("dispatches printable keys as text key-down events", () => {
36 const sequence = makePreviewAutomationKeySequence({ key: "z" });
37 expect(sequence.keyDown).toMatchObject({
38 type: "keyDown",
39 key: "z",
40 code: "KeyZ",
41 windowsVirtualKeyCode: 90,
42 text: "z",
43 });
44 expect(sequence.keyUp).not.toHaveProperty("text");
45 });
46​
47 it("suppresses text and uses raw key-down for shortcuts", () => {
48 expect(
49 makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }, { isMac: true }).keyDown,
50 ).toEqual({
51 type: "rawKeyDown",
52 key: "a",
53 code: "KeyA",
54 modifiers: 4,
55 windowsVirtualKeyCode: 65,
56 location: 0,
57 isKeypad: false,
58 commands: ["selectAll"],
59 });
60 });
61​
62 it("maps common macOS editing shortcuts without changing other platforms", () => {
63 expect(
64 makePreviewAutomationKeySequence({ key: "z", modifiers: ["Shift", "Meta"] }, { isMac: true })
65 .keyDown.commands,
66 ).toEqual(["redo"]);
67 expect(
68 makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }).keyDown,
69 ).not.toHaveProperty("commands");
70 });
71​
72 it("resolves shifted printable keys to their browser values", () => {
73 const sequence = makePreviewAutomationKeySequence({ key: "1", modifiers: ["Shift"] });
74 expect(sequence.keyDown).toMatchObject({
75 key: "!",
76 code: "Digit1",
77 modifiers: 8,
78 windowsVirtualKeyCode: 49,
79 text: "!",
80 });
81 expect(sequence.signal).toEqual({ kind: "key", key: "!", code: "Digit1" });
82 });
83​
84 it("keeps shifted key values while suppressing text for modified chords", () => {
85 const sequence = makePreviewAutomationKeySequence({
86 key: "1",
87 modifiers: ["Control", "Shift"],
88 });
89 expect(sequence.keyDown).toEqual({
90 type: "rawKeyDown",
91 key: "!",
92 code: "Digit1",
93 modifiers: 10,
94 windowsVirtualKeyCode: 49,
95 location: 0,
96 isKeypad: false,
97 });
98 expect(sequence.signal).toEqual({ kind: "key", key: "!", code: "Digit1" });
99 });
100​
101 it.each([
102 ["Enter", "\r"],
103 ["z", "z"],
104 ])("converts %s into native down, char, and up packets", (key, text) => {
105 const sequence = makePreviewAutomationNativeKeySequence({ key });
106 const shared = { keyCode: key, modifiers: [], skipIfUnhandled: true };
107 expect(sequence.keyDown).toEqual({ type: "keyDown", ...shared });
108 expect(sequence.char).toEqual({ type: "char", ...shared, keyCode: text });
109 expect(sequence.keyUp).toEqual({ type: "keyUp", ...shared });
110 });
111​
112 it("suppresses text for shortcuts and retains macOS editing commands", () => {
113 const sequence = makePreviewAutomationNativeKeySequence(
114 { key: "a", modifiers: ["Meta"] },
115 { isMac: true },
116 );
117 expect(sequence.keyDown).toEqual({
118 type: "keyDown",
119 keyCode: "a",
120 modifiers: ["meta"],
121 skipIfUnhandled: true,
122 });
123 expect(sequence.char).toBeUndefined();
124 expect(sequence.commands).toEqual(["selectAll"]);
125 });
126​
127 it.each([
128 ["ArrowLeft", "Left"],
129 ["ArrowRight", "Right"],
130 ["ArrowUp", "Up"],
131 ["ArrowDown", "Down"],
132 ])("maps %s to Electron's %s accelerator", (key, keyCode) => {
133 const sequence = makePreviewAutomationNativeKeySequence({ key });
134 expect(sequence.keyDown.keyCode).toBe(keyCode);
135 expect(sequence.keyUp.keyCode).toBe(keyCode);
136 expect(sequence.signal.key).toBe(key);
137 expect(sequence.char).toBeUndefined();
138 });
139​
140 it("matches native uppercase key signals without inventing shortcut modifiers", () => {
141 const plain = makePreviewAutomationNativeKeySequence({ key: "X" });
142 expect(plain.signal).toEqual({ kind: "key", key: "x", code: "KeyX" });
143 expect(plain.char?.keyCode).toBe("X");
144 const shortcut = makePreviewAutomationNativeKeySequence({ key: "A", modifiers: ["Control"] });
145 expect(shortcut.signal).toEqual({ kind: "key", key: "a", code: "KeyA" });
146 expect(shortcut.keyDown.modifiers).toEqual(["control"]);
147 expect(shortcut.char).toBeUndefined();
148 expect(
149 makePreviewAutomationNativeKeySequence({ key: "X", modifiers: ["Shift"] }).signal,
150 ).toEqual({
151 kind: "key",
152 key: "X",
153 code: "KeyX",
154 });
155 });
156​
157 it("matches native signals for Unicode text and literal spaces", () => {
158 const unicode = makePreviewAutomationNativeKeySequence({ key: "é" });
159 expect(unicode.signal).toEqual({ kind: "key", key: "", code: "" });
160 expect(unicode.char?.keyCode).toBe("é");
161 expect(makePreviewAutomationNativeKeySequence({ key: " " }).signal).toEqual({
162 kind: "key",
163 key: " ",
164 code: "Space",
165 });
166 });
167​
168 it("preserves text and editing commands for isolated child renderer targets", () => {
169 const text = makePreviewAutomationKeySequence({ key: "é" });
170 expect(text.keyDown).toMatchObject({ type: "keyDown", text: "é", key: "é" });
171 expect(text.keyUp).toMatchObject({ type: "keyUp", key: "é" });
172 const shortcut = makePreviewAutomationKeySequence(
173 { key: "a", modifiers: ["Meta"] },
174 { isMac: true },
175 );
176 expect(shortcut.keyDown).toMatchObject({
177 type: "rawKeyDown",
178 modifiers: 4,
179 commands: ["selectAll"],
180 });
181 expect(shortcut.keyDown).not.toHaveProperty("text");
182 expect(shortcut.keyDown).not.toHaveProperty("nativeVirtualKeyCode");
183 });
184});