1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 formatSnapShotShortcutLabel,
5 parseDesktopSnapShotShortcut,
6 sameSnapShotShortcut,
7 snapShotKeybindingConflict,
8 snapShotShortcutKeyLabels,
9} from "./snapShotShortcut";
10
11describe("desktop-approved window capture shortcut labels", () => {
12 it.each([
13 ["Press <Shift><Control>2", ["Ctrl", "⇧", "2"]],
14 ["<Control><Shift>2", ["Ctrl", "⇧", "2"]],
15 ["Ctrl+Shift+8", ["Ctrl", "⇧", "8"]],
16 ["CTRL+SHIFT+8", ["Ctrl", "⇧", "8"]],
17 ["Meta+F12", ["Super", "F12"]],
18 ["Super+F8", ["Super", "F8"]],
19 ["<Primary><Alt>Return", ["Ctrl", "Alt", "Enter"]],
20 ["<Super>Left", ["Super", "Left"]],
21 ["<Control>space", ["Ctrl", "Space"]],
22 ["<Control>plus", ["Ctrl", "+"]],
23 ["<Control>+", ["Ctrl", "+"]],
24 ["Ctrl++", ["Ctrl", "+"]],
25 [" Ctrl + Shift + 2 ", ["Ctrl", "⇧", "2"]],
26 ])("formats %s using the shared keycap labels", (label, expected) => {
27 const shortcut = parseDesktopSnapShotShortcut(label);
28 expect(shortcut).not.toBeNull();
29 expect(snapShotShortcutKeyLabels(shortcut!, "Linux")).toEqual(expected);
30 });
31
32 it.each([
33 "",
34 " ",
35 "Press ",
36 "Ctrl+",
37 "Press the shortcut configured in your desktop",
38 "Ctrl+Shift+2 or Ctrl+Shift+3",
39 "Press <Control>2 or <Control>3",
40 "<Unknown>2",
41 "<Control>",
42 "<Control>UnknownKey",
43 ])("does not guess keys from an unrecognized description: %s", (label) => {
44 expect(parseDesktopSnapShotShortcut(label)).toBeNull();
45 });
46});
47
48describe("window capture shortcut labels", () => {
49 it("labels the default physical Shift pair", () => {
50 expect(formatSnapShotShortcutLabel({ kind: "both-shift-keys" }, "MacIntel")).toBe(
51 "Shift + Shift",
52 );
53 });
54
55 it("labels other modifier pairs per platform", () => {
56 expect(
57 formatSnapShotShortcutLabel({ kind: "modifier-pair", modifier: "meta" }, "MacIntel"),
58 ).toBe("Command + Command");
59 expect(formatSnapShotShortcutLabel({ kind: "modifier-pair", modifier: "meta" }, "Linux")).toBe(
60 "Super + Super",
61 );
62 expect(
63 formatSnapShotShortcutLabel({ kind: "modifier-pair", modifier: "alt" }, "MacIntel"),
64 ).toBe("Option + Option");
65 });
66
67 it.each([
68 ["MacIntel", { kind: "modifier-pair", modifier: "meta" } as const, ["⌘", "⌘"]],
69 ["MacIntel", { kind: "both-shift-keys" } as const, ["⇧", "⇧"]],
70 ["MacIntel", { kind: "modifier-pair", modifier: "alt" } as const, ["⌥", "⌥"]],
71 ["MacIntel", { kind: "modifier-pair", modifier: "control" } as const, ["⌃", "⌃"]],
72 ["Win32", { kind: "modifier-pair", modifier: "meta" } as const, ["⊞", "⊞"]],
73 ["Win32", { kind: "both-shift-keys" } as const, ["⇧", "⇧"]],
74 ["Win32", { kind: "modifier-pair", modifier: "alt" } as const, ["Alt", "Alt"]],
75 ["Win32", { kind: "modifier-pair", modifier: "control" } as const, ["Ctrl", "Ctrl"]],
76 ["Linux", { kind: "modifier-pair", modifier: "meta" } as const, ["Super", "Super"]],
77 ["Linux", { kind: "both-shift-keys" } as const, ["⇧", "⇧"]],
78 ["Linux", { kind: "modifier-pair", modifier: "alt" } as const, ["Alt", "Alt"]],
79 ["Linux", { kind: "modifier-pair", modifier: "control" } as const, ["Ctrl", "Ctrl"]],
80 ])("renders modifier-pair key caps on %s", (platform, shortcut, expected) => {
81 expect(snapShotShortcutKeyLabels(shortcut, platform)).toEqual(expected);
82 });
83
84 it.each([
85 ["MacIntel", ["⇧", "⌘", "2"]],
86 ["Win32", ["Ctrl", "⇧", "2"]],
87 ["Linux", ["Ctrl", "⇧", "2"]],
88 ])("renders chord key caps on %s", (platform, expected) => {
89 expect(
90 snapShotShortcutKeyLabels(
91 {
92 key: "2",
93 metaKey: false,
94 ctrlKey: false,
95 shiftKey: true,
96 altKey: false,
97 modKey: true,
98 },
99 platform,
100 ),
101 ).toEqual(expected);
102 });
103});
104
105describe("window capture keybinding conflicts", () => {
106 it("finds an effective T3 Code keybinding on the current platform", () => {
107 expect(
108 snapShotKeybindingConflict(
109 {
110 key: "n",
111 metaKey: false,
112 ctrlKey: false,
113 shiftKey: false,
114 altKey: false,
115 modKey: true,
116 },
117 [
118 {
119 command: "chat.new",
120 shortcut: {
121 key: "n",
122 metaKey: true,
123 ctrlKey: false,
124 shiftKey: false,
125 altKey: false,
126 modKey: false,
127 },
128 },
129 ],
130 "MacIntel",
131 ),
132 ).toBe("chat.new");
133 });
134
135 it("does not conflict with regular keybindings for both Shift keys", () => {
136 expect(snapShotKeybindingConflict({ kind: "both-shift-keys" }, [], "Linux")).toBeNull();
137 });
138
139 it("keeps symbols on different physical keys distinct", () => {
140 const modifiers = {
141 metaKey: false,
142 ctrlKey: false,
143 shiftKey: true,
144 altKey: false,
145 modKey: true,
146 } as const;
147
148 expect(
149 snapShotKeybindingConflict(
150 { key: '"', ...modifiers },
151 [
152 {
153 command: "capture.other",
154 shortcut: { key: "'", ...modifiers },
155 },
156 ],
157 "MacIntel",
158 ),
159 ).toBeNull();
160 });
161});
162
163describe("sameSnapShotShortcut", () => {
164 it("compares the physical Shift pair and platform-equivalent chords", () => {
165 expect(
166 sameSnapShotShortcut({ kind: "both-shift-keys" }, { kind: "both-shift-keys" }, "MacIntel"),
167 ).toBe(true);
168 expect(
169 sameSnapShotShortcut(
170 { kind: "both-shift-keys" },
171 { kind: "modifier-pair", modifier: "shift" },
172 "MacIntel",
173 ),
174 ).toBe(true);
175 expect(
176 sameSnapShotShortcut(
177 { kind: "modifier-pair", modifier: "meta" },
178 { kind: "both-shift-keys" },
179 "MacIntel",
180 ),
181 ).toBe(false);
182 expect(
183 sameSnapShotShortcut(
184 { key: "n", metaKey: false, ctrlKey: false, shiftKey: false, altKey: false, modKey: true },
185 { key: "n", metaKey: true, ctrlKey: false, shiftKey: false, altKey: false, modKey: false },
186 "MacIntel",
187 ),
188 ).toBe(true);
189 });
190});