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

QuitHold.test.ts

464 lines17.5 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 makeQuitShortcutHandler,
5 QUIT_DOUBLE_PRESS_MS,
6 QUIT_HOLD_DURATION_MS,
7 QUIT_HOLD_RELEASE_GRACE_MS,
8} from "./QuitHold.ts";
9import type { QuitHoldKeyInput } from "./QuitHold.ts";
10import type { QuitConfirmationMode, QuitShortcutHintEvent } from "@t3tools/contracts";
11​
12const HOLD_DOWN = { state: "down", mode: "hold" } as const;
13const DOUBLE_CLICK_DOWN = { state: "down", mode: "double-click" } as const;
14const UP = { state: "up" } as const;
15​
16function makeInput(overrides: Partial<QuitHoldKeyInput>): QuitHoldKeyInput {
17 return {
18 type: "keyDown",
19 key: "q",
20 meta: true,
21 control: false,
22 alt: false,
23 shift: false,
24 isAutoRepeat: false,
25 ...overrides,
26 };
28​
29function makeHarness(options?: {
30 mode?: QuitConfirmationMode;
31 platform?: NodeJS.Platform;
32 getMode?: () => Promise<QuitConfirmationMode>;
33}) {
34 const notifications: Array<QuitShortcutHintEvent> = [];
35 const concealWindow = vi.fn();
36 const quit = vi.fn();
37 const handler = makeQuitShortcutHandler({
38 platform: options?.platform ?? "darwin",
39 getMode: options?.getMode ?? (() => Promise.resolve(options?.mode ?? "hold")),
40 notify: (event) => notifications.push(event),
41 concealWindow,
42 quit,
43 });
44 const preventDefault = vi.fn();
45 const send = async (input: QuitHoldKeyInput) => {
46 handler({ preventDefault }, input);
47 await Promise.resolve();
48 await Promise.resolve();
49 };
50 const holdFor = async (
51 durationMs: number,
52 repeatOverrides: Partial<QuitHoldKeyInput> = {},
53 intervalMs = 100,
54 ) => {
55 for (let elapsed = 0; elapsed < durationMs; elapsed += intervalMs) {
56 vi.advanceTimersByTime(intervalMs);
57 await send(makeInput({ isAutoRepeat: true, ...repeatOverrides }));
58 }
59 };
60 return { notifications, concealWindow, quit, preventDefault, send, holdFor };
62​
63describe("makeQuitShortcutHandler", () => {
64 beforeEach(() => {
65 vi.useFakeTimers();
66 });
67 afterEach(() => {
68 vi.useRealTimers();
69 });
70​
71 it("shows the hint on a tap without quitting, even when the release is never seen", async () => {
72 // macOS suppresses the letter's keyUp while Cmd is held, so a tap may
73 // produce no keyUp at all. Quit must still not fire.
74 const harness = makeHarness();
75 await harness.send(makeInput({}));
76 expect(harness.preventDefault).toHaveBeenCalledTimes(1);
77 expect(harness.notifications).toEqual([HOLD_DOWN]);
78​
79 vi.advanceTimersByTime(QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS);
80 expect(harness.quit).not.toHaveBeenCalled();
81 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
82 });
83​
84 it("conceals a completed hold, then quits after release", async () => {
85 const harness = makeHarness();
86 await harness.send(makeInput({}));
87 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
88 expect(harness.concealWindow).toHaveBeenCalledTimes(1);
89 expect(harness.quit).not.toHaveBeenCalled();
90 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
91 expect(harness.quit).not.toHaveBeenCalled();
92 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
93 expect(harness.quit).toHaveBeenCalledTimes(1);
94 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
95 });
96​
97 it("keeps a concealed hold committed when another key is pressed", async () => {
98 const harness = makeHarness();
99 await harness.send(makeInput({}));
100 await harness.holdFor(QUIT_HOLD_DURATION_MS);
101​
102 await harness.send(makeInput({ key: "Shift", shift: true }));
103 expect(harness.concealWindow).toHaveBeenCalledTimes(1);
104 expect(harness.quit).not.toHaveBeenCalled();
105​
106 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
107 expect(harness.quit).toHaveBeenCalledTimes(1);
108 });
109​
110 it("keeps a concealed hold committed through a fresh Cmd+Q press", async () => {
111 const harness = makeHarness();
112 await harness.send(makeInput({}));
113 await harness.holdFor(QUIT_HOLD_DURATION_MS);
114​
115 await harness.send(makeInput({}));
116 expect(harness.concealWindow).toHaveBeenCalledTimes(1);
117 expect(harness.quit).not.toHaveBeenCalled();
118​
119 await harness.send(makeInput({ type: "keyUp" }));
120 expect(harness.quit).toHaveBeenCalledTimes(1);
121 });
122​
123 it("quits when a completed hold goes quiet without release events", async () => {
124 const harness = makeHarness();
125 await harness.send(makeInput({}));
126 await harness.holdFor(QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS * 2);
127​
128 expect(harness.quit).not.toHaveBeenCalled();
129 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
130​
131 expect(harness.quit).toHaveBeenCalledTimes(1);
132 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
133 });
134​
135 it("waits for slow repeats to stop before quitting", async () => {
136 const harness = makeHarness();
137 await harness.send(makeInput({}));
138​
139 vi.advanceTimersByTime(300);
140 await harness.send(makeInput({ isAutoRepeat: true }));
141 vi.advanceTimersByTime(900);
142 await harness.send(makeInput({ isAutoRepeat: true }));
143​
144 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
145 expect(harness.quit).not.toHaveBeenCalled();
146​
147 vi.advanceTimersByTime(300);
148 await harness.send(makeInput({ isAutoRepeat: true }));
149 vi.advanceTimersByTime(1_799);
150 expect(harness.quit).not.toHaveBeenCalled();
151 vi.advanceTimersByTime(1);
152 expect(harness.quit).toHaveBeenCalledTimes(1);
153 });
154​
155 it("uses the initial repeat delay when the first repeat completes the hold", async () => {
156 const harness = makeHarness();
157 await harness.send(makeInput({}));
158​
159 vi.advanceTimersByTime(QUIT_HOLD_DURATION_MS + 100);
160 await harness.send(makeInput({ isAutoRepeat: true }));
161​
162 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
163 expect(harness.quit).not.toHaveBeenCalled();
164 vi.advanceTimersByTime(1_999);
165 expect(harness.quit).not.toHaveBeenCalled();
166 vi.advanceTimersByTime(1);
167 expect(harness.quit).toHaveBeenCalledTimes(1);
168 });
169​
170 it("waits for Q release when Cmd is released first", async () => {
171 const harness = makeHarness();
172 await harness.send(makeInput({}));
173 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
174 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
175 harness.preventDefault.mockClear();
176 await harness.holdFor(QUIT_HOLD_RELEASE_GRACE_MS * 2, { meta: false });
177 expect(harness.preventDefault).toHaveBeenCalled();
178 expect(harness.quit).not.toHaveBeenCalled();
179 await harness.send(makeInput({ type: "keyUp", meta: false }));
180 expect(harness.quit).toHaveBeenCalledTimes(1);
181 });
182​
183 it("commits a concealed hold when the last Q repeat is never released", async () => {
184 // macOS can drop the final Q keyUp. The quit must land on its own once
185 // repeats stop, rather than sitting armed until an unrelated key arrives.
186 const harness = makeHarness();
187 await harness.send(makeInput({}));
188 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
189 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
190 await harness.send(makeInput({ meta: false, isAutoRepeat: true }));
191​
192 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
193 expect(harness.quit).toHaveBeenCalledTimes(1);
194​
195 harness.quit.mockClear();
196 await harness.send(makeInput({ key: "Meta" }));
197 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
198 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS * 4);
199 expect(harness.quit).not.toHaveBeenCalled();
200 });
201​
202 it("does not quit when the hold stops before the duration", async () => {
203 const harness = makeHarness();
204 await harness.send(makeInput({}));
205 await harness.holdFor(500);
206 await harness.send(makeInput({ type: "keyUp" }));
207 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
208 vi.advanceTimersByTime((QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS) * 2);
209 expect(harness.concealWindow).not.toHaveBeenCalled();
210 expect(harness.quit).not.toHaveBeenCalled();
211 });
212​
213 it("cancels the hold when the modifier is released first", async () => {
214 const harness = makeHarness();
215 await harness.send(makeInput({}));
216 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
217 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
218 vi.advanceTimersByTime((QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS) * 2);
219 expect(harness.quit).not.toHaveBeenCalled();
220 });
221​
222 it("quits without showing a hint in direct mode", async () => {
223 const harness = makeHarness({ mode: "direct" });
224 await harness.send(makeInput({}));
225 expect(harness.concealWindow).not.toHaveBeenCalled();
226 expect(harness.quit).toHaveBeenCalledTimes(1);
227 expect(harness.notifications).toEqual([]);
228 });
229​
230 it("honors direct mode when the key is released before its mode read settles", async () => {
231 let resolveMode: ((mode: QuitConfirmationMode) => void) | undefined;
232 const harness = makeHarness({
233 getMode: () =>
234 new Promise((resolve) => {
235 resolveMode = resolve;
236 }),
237 });
238 await harness.send(makeInput({}));
239 await harness.send(makeInput({ type: "keyUp" }));
240​
241 resolveMode?.("direct");
242 await Promise.resolve();
243 await Promise.resolve();
244​
245 expect(harness.quit).toHaveBeenCalledTimes(1);
246 expect(harness.notifications).toEqual([]);
247 });
248​
249 it("does not arm hold mode after a released key's mode read settles", async () => {
250 let resolveMode: ((mode: QuitConfirmationMode) => void) | undefined;
251 const harness = makeHarness({
252 getMode: () =>
253 new Promise((resolve) => {
254 resolveMode = resolve;
255 }),
256 });
257 await harness.send(makeInput({}));
258 await harness.send(makeInput({ type: "keyUp" }));
259​
260 resolveMode?.("hold");
261 await Promise.resolve();
262 await Promise.resolve();
263​
264 expect(harness.quit).not.toHaveBeenCalled();
265 expect(harness.notifications).toEqual([]);
266 });
267​
268 it.each(["direct", "hold", "double-click"] as const)(
269 "quits on a quick second press without waiting for a pending %s mode read",
270 async (mode) => {
271 const resolvers: Array<(mode: QuitConfirmationMode) => void> = [];
272 const harness = makeHarness({
273 getMode: () => new Promise((resolve) => resolvers.push(resolve)),
274 });
275 await harness.send(makeInput({}));
276 await harness.send(makeInput({ type: "keyUp" }));
277 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 100);
278 await harness.send(makeInput({}));
279​
280 expect(harness.quit).toHaveBeenCalledTimes(1);
281 expect(harness.notifications).toEqual([]);
282​
283 await harness.send(makeInput({ type: "keyUp" }));
284​
285 resolvers[0]?.(mode);
286 await Promise.resolve();
287 await Promise.resolve();
288​
289 expect(harness.quit).toHaveBeenCalledTimes(1);
290 expect(harness.notifications).toEqual([]);
291 },
292 );
293​
294 it("discards a stale mode resolution from a superseded press", async () => {
295 const resolvers: Array<(mode: QuitConfirmationMode) => void> = [];
296 const harness = makeHarness({
297 getMode: () => new Promise((resolve) => resolvers.push(resolve)),
298 });
299 await harness.send(makeInput({}));
300 await harness.send(makeInput({ type: "keyUp" }));
301 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS + 100);
302 await harness.send(makeInput({}));
303 expect(resolvers).toHaveLength(2);
304​
305 resolvers[0]?.("direct");
306 await Promise.resolve();
307 await Promise.resolve();
308 expect(harness.quit).not.toHaveBeenCalled();
309​
310 resolvers[1]?.("hold");
311 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
312 await harness.send(makeInput({ type: "keyUp" }));
313 expect(harness.quit).toHaveBeenCalledTimes(1);
314 });
315​
316 it("quits on a quick double press in double-click mode when the first release is unseen", async () => {
317 const harness = makeHarness({ mode: "double-click" });
318 await harness.send(makeInput({}));
319 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 100);
320 await harness.send(makeInput({}));
321 expect(harness.concealWindow).not.toHaveBeenCalled();
322 expect(harness.quit).toHaveBeenCalledTimes(1);
323 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP]);
324 });
325​
326 it("keeps the double-press hint visible after key release until the window ends", async () => {
327 const harness = makeHarness({ mode: "double-click" });
328 await harness.send(makeInput({}));
329 vi.advanceTimersByTime(100);
330 await harness.send(makeInput({ type: "keyUp" }));
331 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN]);
332​
333 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 101);
334 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN]);
335 vi.advanceTimersByTime(1);
336 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP]);
337 });
338​
339 it("accepts a second full shortcut after the modifier is released and pressed again", async () => {
340 const harness = makeHarness({ mode: "double-click" });
341 await harness.send(makeInput({}));
342 await harness.send(makeInput({ type: "keyUp" }));
343 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
344 vi.advanceTimersByTime(100);
345​
346 await harness.send(makeInput({ key: "Meta" }));
347 await harness.send(makeInput({}));
348​
349 expect(harness.quit).toHaveBeenCalledTimes(1);
350 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP]);
351 });
352​
353 it("expires a delayed double-press hint from keydown rather than mode resolution", async () => {
354 let resolveMode: ((mode: QuitConfirmationMode) => void) | undefined;
355 const harness = makeHarness({
356 getMode: () =>
357 new Promise((resolve) => {
358 resolveMode = resolve;
359 }),
360 });
361 await harness.send(makeInput({}));
362 vi.advanceTimersByTime(100);
363 await harness.send(makeInput({ type: "keyUp" }));
364 vi.advanceTimersByTime(100);
365 resolveMode?.("double-click");
366 await Promise.resolve();
367 await Promise.resolve();
368 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN]);
369​
370 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 201);
371 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN]);
372 vi.advanceTimersByTime(1);
373 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP]);
374 });
375​
376 it("treats two slow presses as separate attempts in double-click mode", async () => {
377 const harness = makeHarness({ mode: "double-click" });
378 await harness.send(makeInput({}));
379 await harness.send(makeInput({ type: "keyUp" }));
380 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS + 100);
381 await harness.send(makeInput({}));
382 expect(harness.quit).not.toHaveBeenCalled();
383 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP, DOUBLE_CLICK_DOWN]);
384 });
385​
386 it("quits on a quick second press in hold mode", async () => {
387 const harness = makeHarness();
388 await harness.send(makeInput({}));
389 await harness.send(makeInput({ type: "keyUp" }));
390 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 100);
391 await harness.send(makeInput({}));
392 expect(harness.quit).toHaveBeenCalledTimes(1);
393 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
394 });
395​
396 it("quits on a quick second press in hold mode when the first release is unseen", async () => {
397 const harness = makeHarness();
398 await harness.send(makeInput({}));
399 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 100);
400 await harness.send(makeInput({}));
401​
402 expect(harness.concealWindow).not.toHaveBeenCalled();
403 expect(harness.quit).toHaveBeenCalledTimes(1);
404 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
405 });
406​
407 it("does not count auto-repeat as a second press", async () => {
408 const harness = makeHarness();
409 await harness.send(makeInput({}));
410 await harness.holdFor(QUIT_DOUBLE_PRESS_MS - 100);
411​
412 expect(harness.quit).not.toHaveBeenCalled();
413 expect(harness.notifications).toEqual([HOLD_DOWN]);
414 });
415​
416 it("does not count a released tap after another shortcut interrupts it", async () => {
417 const harness = makeHarness();
418 await harness.send(makeInput({}));
419 await harness.send(makeInput({ type: "keyUp" }));
420 await harness.send(makeInput({ key: "c" }));
421 vi.advanceTimersByTime(100);
422 await harness.send(makeInput({}));
423​
424 expect(harness.quit).not.toHaveBeenCalled();
425 expect(harness.notifications).toEqual([HOLD_DOWN, UP, HOLD_DOWN]);
426 });
427​
428 it("cancels the hold when another key interrupts it", async () => {
429 const harness = makeHarness();
430 await harness.send(makeInput({}));
431 await harness.holdFor(500);
432 await harness.send(makeInput({ shift: true }));
433 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
434 await harness.holdFor(QUIT_HOLD_DURATION_MS);
435 expect(harness.quit).not.toHaveBeenCalled();
436 });
437​
438 it("does not count an interrupted press toward a double press", async () => {
439 const harness = makeHarness({ mode: "double-click" });
440 await harness.send(makeInput({}));
441 await harness.send(makeInput({ shift: true }));
442 await harness.send(makeInput({}));
443 expect(harness.quit).not.toHaveBeenCalled();
444 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP, DOUBLE_CLICK_DOWN]);
445 });
446​
447 it("ignores other shortcuts", async () => {
448 const harness = makeHarness();
449 await harness.send(makeInput({ key: "w" }));
450 await harness.send(makeInput({ shift: true }));
451 await harness.send(makeInput({ meta: false }));
452 expect(harness.preventDefault).not.toHaveBeenCalled();
453 expect(harness.notifications).toEqual([]);
454 });
455​
456 it("uses control on non-mac platforms", async () => {
457 const harness = makeHarness({ platform: "linux" });
458 await harness.send(makeInput({ meta: false, control: true }));
459 expect(harness.preventDefault).toHaveBeenCalledTimes(1);
460 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200, { meta: false, control: true });
461 await harness.send(makeInput({ type: "keyUp", meta: false, control: true }));
462 expect(harness.quit).toHaveBeenCalledTimes(1);
463 });
464});