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

keyCodes.test.ts

64 lines2.6 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 expect(ghosttyConsumedMods({ ...shifted, key: " " })).toBe(1);
22 });
23});
24​
25describe("ghosttyUnshiftedCodepoint", () => {
26 it("provides the logical base character for Kitty keyboard encoding", () => {
27 expect(ghosttyUnshiftedCodepoint({ code: "KeyC", key: "c", shiftKey: false })).toBe(
28 "c".codePointAt(0),
29 );
30 expect(ghosttyUnshiftedCodepoint({ code: "KeyC", key: "C", shiftKey: true })).toBe(
31 "c".codePointAt(0),
32 );
33 expect(ghosttyUnshiftedCodepoint({ code: "Digit1", key: "!", shiftKey: true })).toBe(
34 "1".codePointAt(0),
35 );
36 expect(ghosttyUnshiftedCodepoint({ code: "Slash", key: "?", shiftKey: true })).toBe(
37 "/".codePointAt(0),
38 );
39 expect(ghosttyUnshiftedCodepoint({ code: "Digit1", key: "&", shiftKey: false })).toBe(
40 "&".codePointAt(0),
41 );
42 expect(ghosttyUnshiftedCodepoint({ code: "Enter", key: "Enter", shiftKey: false })).toBe(0);
43 });
44​
45 it("reports unknown instead of the shifted character without layout data", () => {
46 expect(ghosttyUnshiftedCodepoint({ code: "Digit7", key: "/", shiftKey: true })).toBe(0);
47 expect(ghosttyUnshiftedCodepoint({ code: "KeyD", key: "Д", shiftKey: true })).toBe(
48 "д".codePointAt(0),
49 );
50 });
51​
52 it("prefers the active browser layout over US physical key positions", () => {
53 const layoutMap = new Map([
54 ["Digit1", "&"],
55 ["KeyC", "j"],
56 ]);
57 expect(ghosttyUnshiftedCodepoint({ code: "Digit1", key: "1", shiftKey: true }, layoutMap)).toBe(
58 "&".codePointAt(0),
59 );
60 expect(ghosttyUnshiftedCodepoint({ code: "KeyC", key: "J", shiftKey: true }, layoutMap)).toBe(
61 "j".codePointAt(0),
62 );
63 });
64});