Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import ghosttyKeyHeader from "../../../../../native/libghostty-vt/include/ghostty/vt/key/event.h?raw";
3
4import {
5 ghosttyConsumedMods,
6 ghosttyKeyForCode,
7 ghosttyKeyForEvent,
8 ghosttyUnshiftedCodepoint,
9} from "./keyCodes";
10
11describe("ghosttyKeyForCode", () => {
12 it("matches the pinned native key enum after the function keys", () => {
13 const enumBody = ghosttyKeyHeader.slice(
14 ghosttyKeyHeader.indexOf("GHOSTTY_KEY_UNIDENTIFIED"),
15 ghosttyKeyHeader.indexOf("GHOSTTY_KEY_MAX_VALUE"),
16 );
17 const keys = [...enumBody.matchAll(/GHOSTTY_KEY_([A-Z0-9_]+)/g)].map((match) => match[1]);
18 for (const [code, native] of [
19 ["F24", "F24"],
20 ["F25", "F25"],
21 ["FnLock", "FN_LOCK"],
22 ["PrintScreen", "PRINT_SCREEN"],
23 ["ScrollLock", "SCROLL_LOCK"],
24 ["Pause", "PAUSE"],
25 ["Paste", "PASTE"],
26 ]) {
27 expect(keys).toContain(native);
28 expect(ghosttyKeyForCode(code!)).toBe(keys.indexOf(native));
29 }
30 });
31});
32
33describe("ghosttyKeyForEvent", () => {
34 it.each(["", "Unidentified", "Unknown"])(
35 "falls back to the logical key when code '%s' is not mapped",
36 (code) => {
37 expect(ghosttyKeyForEvent({ code, key: "Backspace" })).toBe(ghosttyKeyForCode("Backspace"));
38 },
39 );
40
41 it("prefers recognized physical codes over logical keys", () => {
42 expect(ghosttyKeyForEvent({ code: "Delete", key: "Backspace" })).toBe(
43 ghosttyKeyForCode("Delete"),
44 );
45 expect(ghosttyKeyForEvent({ code: "NumpadEnter", key: "Enter" })).toBe(
46 ghosttyKeyForCode("NumpadEnter"),
47 );
48 });
49
50 it("leaves printable characters and unknown keys to the text encoding path", () => {
51 expect(ghosttyKeyForEvent({ code: "", key: "a" })).toBe(0);
52 expect(ghosttyKeyForEvent({ code: "", key: "Unidentified" })).toBe(0);
53 });
54});
55
56describe("ghosttyConsumedMods", () => {
57 const shifted = { altKey: false, ctrlKey: false, key: "@", metaKey: false, shiftKey: true };
58
59 it("only consumes a lone Shift producing a character", () => {
60 expect(ghosttyConsumedMods(shifted)).toBe(1);
61 expect(ghosttyConsumedMods({ ...shifted, ctrlKey: true })).toBe(0);
62 expect(ghosttyConsumedMods({ ...shifted, key: "Tab" })).toBe(0);
63 expect(ghosttyConsumedMods({ ...shifted, key: " " })).toBe(1);
64 });
65});
66
67describe("ghosttyUnshiftedCodepoint", () => {
68 it("provides the logical base character for Kitty keyboard encoding", () => {
69 expect(ghosttyUnshiftedCodepoint({ code: "KeyC", key: "c", shiftKey: false })).toBe(
70 "c".codePointAt(0),
71 );
72 expect(ghosttyUnshiftedCodepoint({ code: "KeyC", key: "C", shiftKey: true })).toBe(
73 "c".codePointAt(0),
74 );
75 expect(ghosttyUnshiftedCodepoint({ code: "Digit1", key: "!", shiftKey: true })).toBe(
76 "1".codePointAt(0),
77 );
78 expect(ghosttyUnshiftedCodepoint({ code: "Slash", key: "?", shiftKey: true })).toBe(
79 "/".codePointAt(0),
80 );
81 expect(ghosttyUnshiftedCodepoint({ code: "Digit1", key: "&", shiftKey: false })).toBe(
82 "&".codePointAt(0),
83 );
84 expect(ghosttyUnshiftedCodepoint({ code: "Enter", key: "Enter", shiftKey: false })).toBe(0);
85 });
86
87 it("reports unknown instead of the shifted character without layout data", () => {
88 expect(ghosttyUnshiftedCodepoint({ code: "Digit7", key: "/", shiftKey: true })).toBe(0);
89 expect(ghosttyUnshiftedCodepoint({ code: "KeyD", key: "Д", shiftKey: true })).toBe(
90 "д".codePointAt(0),
91 );
92 });
93
94 it("prefers the active browser layout over US physical key positions", () => {
95 const layoutMap = new Map([
96 ["Digit1", "&"],
97 ["KeyC", "j"],
98 ]);
99 expect(ghosttyUnshiftedCodepoint({ code: "Digit1", key: "1", shiftKey: true }, layoutMap)).toBe(
100 "&".codePointAt(0),
101 );
102 expect(ghosttyUnshiftedCodepoint({ code: "KeyC", key: "J", shiftKey: true }, layoutMap)).toBe(
103 "j".codePointAt(0),
104 );
105 });
106});