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

keyCodes.test.ts

65 lines2.7 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { ghosttyConsumedMods, ghosttyKeyForCode, ghosttyUnshiftedCodepoint } from "./keyCodes";
4​
5describe("ghosttyKeyForCode", () => {
6 it("keeps the tail of the pinned Ghostty key enum in order", () => {
7 expect(ghosttyKeyForCode("F25")).toBe(ghosttyKeyForCode("F24") + 1);
8 expect(ghosttyKeyForCode("PrintScreen")).toBe(ghosttyKeyForCode("FnLock") + 1);
9 expect(ghosttyKeyForCode("Pause")).toBe(ghosttyKeyForCode("ScrollLock") + 1);
10 expect(ghosttyKeyForCode("Paste")).toBe(ghosttyKeyForCode("Cut") + 1);
11 });
12});
13​
14describe("ghosttyConsumedMods", () => {
15 const shifted = { altKey: false, ctrlKey: false, key: "@", metaKey: false, shiftKey: true };
16​
17 it("only consumes a lone Shift producing a character", () => {
18 expect(ghosttyConsumedMods(shifted)).toBe(1);
19 expect(ghosttyConsumedMods({ ...shifted, ctrlKey: true })).toBe(0);
20 expect(ghosttyConsumedMods({ ...shifted, key: "Tab" })).toBe(0);
21 // Deliberate: Shift+Space collapses to Space so it still types one.
22 expect(ghosttyConsumedMods({ ...shifted, key: " " })).toBe(1);
23 });
24});
25​
26describe("ghosttyUnshiftedCodepoint", () => {
27 it("provides the logical base character for Kitty keyboard encoding", () => {
28 expect(ghosttyUnshiftedCodepoint({ code: "KeyC", key: "c", shiftKey: false })).toBe(
29 "c".codePointAt(0),
30 );
31 expect(ghosttyUnshiftedCodepoint({ code: "KeyC", key: "C", shiftKey: true })).toBe(
32 "c".codePointAt(0),
33 );
34 expect(ghosttyUnshiftedCodepoint({ code: "Digit1", key: "!", shiftKey: true })).toBe(
35 "1".codePointAt(0),
36 );
37 expect(ghosttyUnshiftedCodepoint({ code: "Slash", key: "?", shiftKey: true })).toBe(
38 "/".codePointAt(0),
39 );
40 expect(ghosttyUnshiftedCodepoint({ code: "Digit1", key: "&", shiftKey: false })).toBe(
41 "&".codePointAt(0),
42 );
43 expect(ghosttyUnshiftedCodepoint({ code: "Enter", key: "Enter", shiftKey: false })).toBe(0);
44 });
45​
46 it("reports unknown instead of the shifted character without layout data", () => {
47 expect(ghosttyUnshiftedCodepoint({ code: "Digit7", key: "/", shiftKey: true })).toBe(0);
48 expect(ghosttyUnshiftedCodepoint({ code: "KeyD", key: "Д", shiftKey: true })).toBe(
49 "д".codePointAt(0),
50 );
51 });
52​
53 it("prefers the active browser layout over US physical key positions", () => {
54 const layoutMap = new Map([
55 ["Digit1", "&"],
56 ["KeyC", "j"],
57 ]);
58 expect(ghosttyUnshiftedCodepoint({ code: "Digit1", key: "1", shiftKey: true }, layoutMap)).toBe(
59 "&".codePointAt(0),
60 );
61 expect(ghosttyUnshiftedCodepoint({ code: "KeyC", key: "J", shiftKey: true }, layoutMap)).toBe(
62 "j".codePointAt(0),
63 );
64 });
65});