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