| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import type { ResolvedKeybindingsConfig } from "@t3tools/contracts"; |
| 4 | |
| 5 | import { |
| 6 | preventRepeatedTerminalCloseShortcut, |
| 7 | preventTerminalCloseShortcut, |
| 8 | type TerminalCloseShortcutEvent, |
| 9 | } from "./terminalCloseShortcut"; |
| 10 | |
| 11 | const keybindings = [ |
| 12 | { |
| 13 | command: "terminal.close", |
| 14 | shortcut: { |
| 15 | key: "w", |
| 16 | metaKey: false, |
| 17 | ctrlKey: false, |
| 18 | shiftKey: false, |
| 19 | altKey: false, |
| 20 | modKey: true, |
| 21 | }, |
| 22 | whenAst: { type: "identifier", name: "terminalFocus" }, |
| 23 | }, |
| 24 | ] satisfies ResolvedKeybindingsConfig; |
| 25 | |
| 26 | type KeyboardEventOverrides = Partial< |
| 27 | Pick< |
| 28 | TerminalCloseShortcutEvent, |
| 29 | "key" | "code" | "metaKey" | "ctrlKey" | "shiftKey" | "altKey" | "repeat" |
| 30 | > |
| 31 | >; |
| 32 | |
| 33 | function keyboardEvent( |
| 34 | overrides: KeyboardEventOverrides = {}, |
| 35 | ): TerminalCloseShortcutEvent & { readonly defaultPrevented: boolean } { |
| 36 | let defaultPrevented = false; |
| 37 | return { |
| 38 | key: "w", |
| 39 | code: "KeyW", |
| 40 | metaKey: false, |
| 41 | ctrlKey: true, |
| 42 | shiftKey: false, |
| 43 | altKey: false, |
| 44 | repeat: false, |
| 45 | ...overrides, |
| 46 | preventDefault: () => { |
| 47 | defaultPrevented = true; |
| 48 | }, |
| 49 | get defaultPrevented() { |
| 50 | return defaultPrevented; |
| 51 | }, |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | describe("terminal close shortcut guards", () => { |
| 56 | it("prevents the browser default for a deliberate terminal close", () => { |
| 57 | const event = keyboardEvent(); |
| 58 | |
| 59 | expect(preventTerminalCloseShortcut(event, keybindings, "Linux x86_64")).toBe(true); |
| 60 | expect(event.defaultPrevented).toBe(true); |
| 61 | }); |
| 62 | |
| 63 | it("keeps held close repeats from closing the browser after the last terminal unmounts", () => { |
| 64 | const firstPress = keyboardEvent(); |
| 65 | expect(preventTerminalCloseShortcut(firstPress, keybindings, "Linux x86_64")).toBe(true); |
| 66 | |
| 67 | let browserCloseCount = 0; |
| 68 | for (const repeat of [true, true, true]) { |
| 69 | const event = keyboardEvent({ repeat }); |
| 70 | preventRepeatedTerminalCloseShortcut(event, keybindings, "Linux x86_64"); |
| 71 | if (!event.defaultPrevented) browserCloseCount += 1; |
| 72 | } |
| 73 | |
| 74 | expect(browserCloseCount).toBe(0); |
| 75 | expect(preventRepeatedTerminalCloseShortcut(keyboardEvent(), keybindings, "Linux x86_64")).toBe( |
| 76 | false, |
| 77 | ); |
| 78 | }); |
| 79 | |
| 80 | it("leaves a non-repeated window close and unrelated repeats alone", () => { |
| 81 | const deliberateWindowClose = keyboardEvent({ repeat: false }); |
| 82 | const unrelatedRepeat = keyboardEvent({ key: "q", code: "KeyQ", repeat: true }); |
| 83 | |
| 84 | expect( |
| 85 | preventRepeatedTerminalCloseShortcut(deliberateWindowClose, keybindings, "Linux x86_64"), |
| 86 | ).toBe(false); |
| 87 | expect(deliberateWindowClose.defaultPrevented).toBe(false); |
| 88 | expect(preventRepeatedTerminalCloseShortcut(unrelatedRepeat, keybindings, "Linux x86_64")).toBe( |
| 89 | false, |
| 90 | ); |
| 91 | expect(unrelatedRepeat.defaultPrevented).toBe(false); |
| 92 | }); |
| 93 | }); |