Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
3import {
4 makeQuitHoldHandler,
5 QUIT_DOUBLE_TAP_MS,
6 QUIT_HOLD_DURATION_MS,
7 QUIT_HOLD_RELEASE_GRACE_MS,
8} from "./QuitHold.ts";
9import type { QuitHoldKeyInput, QuitHoldState } from "./QuitHold.ts";
10
11function makeInput(overrides: Partial<QuitHoldKeyInput>): QuitHoldKeyInput {
12 return {
13 type: "keyDown",
14 key: "q",
15 meta: true,
16 control: false,
17 alt: false,
18 shift: false,
19 isAutoRepeat: false,
20 ...overrides,
21 };
22}
23
24function makeHarness(options?: {
25 enabled?: boolean;
26 platform?: NodeJS.Platform;
27 isEnabled?: () => Promise<boolean>;
28}) {
29 const notifications: Array<QuitHoldState> = [];
30 const quit = vi.fn();
31 const handler = makeQuitHoldHandler({
32 platform: options?.platform ?? "darwin",
33 isEnabled: options?.isEnabled ?? (() => Promise.resolve(options?.enabled ?? true)),
34 notify: (state) => notifications.push(state),
35 quit,
36 });
37 const preventDefault = vi.fn();
38 const send = async (input: QuitHoldKeyInput) => {
39 handler({ preventDefault }, input);
40 // Let the isEnabled promise settle.
41 await Promise.resolve();
42 await Promise.resolve();
43 };
44 // Simulates the OS auto-repeating the held shortcut every `intervalMs`.
45 const holdFor = async (
46 durationMs: number,
47 repeatOverrides: Partial<QuitHoldKeyInput> = {},
48 intervalMs = 100,
49 ) => {
50 for (let elapsed = 0; elapsed < durationMs; elapsed += intervalMs) {
51 vi.advanceTimersByTime(intervalMs);
52 await send(makeInput({ isAutoRepeat: true, ...repeatOverrides }));
53 }
54 };
55 return { notifications, quit, preventDefault, send, holdFor };
56}
57
58describe("makeQuitHoldHandler", () => {
59 beforeEach(() => {
60 vi.useFakeTimers();
61 });
62 afterEach(() => {
63 vi.useRealTimers();
64 });
65
66 it("shows the hint on a tap without quitting, even when the release is never seen", async () => {
67 // macOS suppresses the letter's keyUp while Cmd is held, so a tap may
68 // produce no keyUp at all. Quit must still not fire.
69 const harness = makeHarness();
70 await harness.send(makeInput({}));
71 expect(harness.preventDefault).toHaveBeenCalledTimes(1);
72 expect(harness.notifications).toEqual(["down"]);
73
74 vi.advanceTimersByTime(QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS);
75 expect(harness.quit).not.toHaveBeenCalled();
76 // The watchdog dismisses the hint once the press is clearly over.
77 expect(harness.notifications).toEqual(["down", "up"]);
78 });
79
80 it("quits once the shortcut auto-repeats past the hold duration", async () => {
81 const harness = makeHarness();
82 await harness.send(makeInput({}));
83 await harness.holdFor(QUIT_HOLD_DURATION_MS - 200);
84 expect(harness.quit).not.toHaveBeenCalled();
85 await harness.holdFor(400);
86 expect(harness.quit).toHaveBeenCalledTimes(1);
87 // Exactly one hint cycle for the whole hold.
88 expect(harness.notifications).toEqual(["down", "up"]);
89 });
90
91 it("does not quit when the hold stops before the duration", async () => {
92 const harness = makeHarness();
93 await harness.send(makeInput({}));
94 await harness.holdFor(500);
95 await harness.send(makeInput({ type: "keyUp" }));
96 expect(harness.notifications).toEqual(["down", "up"]);
97 vi.advanceTimersByTime((QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS) * 2);
98 expect(harness.quit).not.toHaveBeenCalled();
99 });
100
101 it("cancels the hold when the modifier is released first", async () => {
102 const harness = makeHarness();
103 await harness.send(makeInput({}));
104 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
105 expect(harness.notifications).toEqual(["down", "up"]);
106 vi.advanceTimersByTime((QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS) * 2);
107 expect(harness.quit).not.toHaveBeenCalled();
108 });
109
110 it("quits immediately on a single press when disabled", async () => {
111 const harness = makeHarness({ enabled: false });
112 await harness.send(makeInput({}));
113 expect(harness.quit).toHaveBeenCalledTimes(1);
114 // The hint is dismissed in case the quit gets cancelled downstream.
115 expect(harness.notifications).toEqual(["down", "up"]);
116 });
117
118 it("discards a stale isEnabled resolution from a superseded press", async () => {
119 // Press #1's isEnabled is still pending when the user releases and
120 // presses again; its late resolution must not act for press #2.
121 const resolvers: Array<(enabled: boolean) => void> = [];
122 const harness = makeHarness({
123 isEnabled: () => new Promise((resolve) => resolvers.push(resolve)),
124 });
125 await harness.send(makeInput({}));
126 await harness.send(makeInput({ type: "keyUp" }));
127 // Outside the double-tap window, so the second press starts a new hold.
128 vi.advanceTimersByTime(QUIT_DOUBLE_TAP_MS + 100);
129 await harness.send(makeInput({}));
130 expect(resolvers).toHaveLength(2);
131
132 // Press #1 resolves late with "disabled" — it must not quit press #2.
133 resolvers[0]?.(false);
134 await Promise.resolve();
135 await Promise.resolve();
136 expect(harness.quit).not.toHaveBeenCalled();
137
138 // Press #2 resolves enabled and completes a full hold.
139 resolvers[1]?.(true);
140 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
141 expect(harness.quit).toHaveBeenCalledTimes(1);
142 });
143
144 it("quits on a quick double tap, even when the first release was never seen", async () => {
145 const harness = makeHarness();
146 await harness.send(makeInput({}));
147 vi.advanceTimersByTime(QUIT_DOUBLE_TAP_MS - 100);
148 await harness.send(makeInput({}));
149 expect(harness.quit).toHaveBeenCalledTimes(1);
150 });
151
152 it("treats two slow taps as separate presses", async () => {
153 const harness = makeHarness();
154 await harness.send(makeInput({}));
155 await harness.send(makeInput({ type: "keyUp" }));
156 vi.advanceTimersByTime(QUIT_DOUBLE_TAP_MS + 100);
157 await harness.send(makeInput({}));
158 expect(harness.quit).not.toHaveBeenCalled();
159 expect(harness.notifications).toEqual(["down", "up", "down"]);
160 });
161
162 it("cancels the hold when another key interrupts it", async () => {
163 const harness = makeHarness();
164 await harness.send(makeInput({}));
165 await harness.holdFor(500);
166 // Shift pressed mid-hold breaks the gesture...
167 await harness.send(makeInput({ shift: true }));
168 expect(harness.notifications).toEqual(["down", "up"]);
169 // ...so later repeats past the threshold must not quit.
170 await harness.holdFor(QUIT_HOLD_DURATION_MS);
171 expect(harness.quit).not.toHaveBeenCalled();
172 });
173
174 it("does not count an interrupted press toward a double tap", async () => {
175 const harness = makeHarness();
176 await harness.send(makeInput({}));
177 await harness.send(makeInput({ shift: true }));
178 // A fresh press right after the interruption starts a new hold, not a
179 // double-tap quit.
180 await harness.send(makeInput({}));
181 expect(harness.quit).not.toHaveBeenCalled();
182 expect(harness.notifications).toEqual(["down", "up", "down"]);
183 });
184
185 it("ignores other shortcuts", async () => {
186 const harness = makeHarness();
187 await harness.send(makeInput({ key: "w" }));
188 await harness.send(makeInput({ shift: true }));
189 await harness.send(makeInput({ meta: false }));
190 expect(harness.preventDefault).not.toHaveBeenCalled();
191 expect(harness.notifications).toEqual([]);
192 });
193
194 it("uses control on non-mac platforms", async () => {
195 const harness = makeHarness({ platform: "linux" });
196 await harness.send(makeInput({ meta: false, control: true }));
197 expect(harness.preventDefault).toHaveBeenCalledTimes(1);
198 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200, { meta: false, control: true });
199 expect(harness.quit).toHaveBeenCalledTimes(1);
200 });
201});