hermes-agent/apps/desktop/electron/preview-guest-escape.test.ts
Brooklyn Nicholson d2b9a49e63 fix(desktop): guarantee an exit path for a fullscreened preview pane
A preview guest that HTML5-fullscreens itself owns all input: the host
renderer can't see into the out-of-process webview, the macOS-only Close
menu and HUD chords aren't reachable, and Wayland has no xdotool/wmctrl to
break out from a terminal — so a page that swallows Esc locks the display
(#97213).

Three layers, all routed before the guest sees the key or fully outside it:

- before-input-event on every webview guest: Esc exits the host window's
  fullscreen (gated on the fullscreen state so normal-mode panes keep Esc),
  Ctrl/Cmd+Shift+W closes the pane via the existing close-preview channel
- hermes://close-preview deep link handled in the main process: restores
  and focuses the window, exits fullscreen, closes the pane
- `hermes desktop --close-preview` CLI flag forwarded to the packaged exe,
  riding the single-instance argv so a second invocation unlocks the running
  app

The permission handlers keep auto-allowing 'fullscreen' — the exit paths
are the fix, and scoping the grant would just break video fullscreen.

Refs #97213
2026-09-29 18:48:37 -05:00

52 lines
2.2 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { hasClosePreviewFlag, previewGuestInputAction } from './preview-guest-escape'
describe('previewGuestInputAction', () => {
it('exits fullscreen on Escape only while the host window is fullscreen', () => {
expect(previewGuestInputAction({ key: 'Escape', type: 'keyDown' }, true)).toBe('exit-fullscreen')
// A pane in normal mode keeps passing Esc through to the page.
expect(previewGuestInputAction({ key: 'Escape', type: 'keyDown' }, false)).toBeNull()
})
it('closes the preview on Ctrl+Shift+W regardless of fullscreen state', () => {
expect(previewGuestInputAction({ control: true, shift: true, key: 'w', type: 'keyDown' }, true)).toBe(
'close-preview'
)
expect(previewGuestInputAction({ control: true, shift: true, key: 'w', type: 'keyDown' }, false)).toBe(
'close-preview'
)
})
it('also accepts the Cmd+Shift+W (macOS) form', () => {
expect(previewGuestInputAction({ meta: true, shift: true, key: 'W', type: 'keyDown' }, true)).toBe(
'close-preview'
)
})
it('leaves every other key for the page', () => {
expect(previewGuestInputAction({ key: 'a', type: 'keyDown' }, true)).toBeNull()
// Bare W / Ctrl+W without Shift is not the close chord.
expect(previewGuestInputAction({ control: true, key: 'w', type: 'keyDown' }, true)).toBeNull()
// Shift+W without the modifier is typing, not closing.
expect(previewGuestInputAction({ shift: true, key: 'W', type: 'keyDown' }, true)).toBeNull()
})
it('ignores key releases and auto-repeats', () => {
expect(previewGuestInputAction({ key: 'Escape', type: 'keyUp' }, true)).toBeNull()
expect(previewGuestInputAction({ isAutoRepeat: true, key: 'Escape', type: 'keyDown' }, true)).toBeNull()
})
})
describe('hasClosePreviewFlag', () => {
it('detects the flag in both forms', () => {
expect(hasClosePreviewFlag(['Hermes', '--close-preview'])).toBe(true)
expect(hasClosePreviewFlag(['Hermes', '--close-preview=1'])).toBe(true)
})
it('ignores other argv and empty argv', () => {
expect(hasClosePreviewFlag(['Hermes'])).toBe(false)
expect(hasClosePreviewFlag(['--close'])).toBe(false)
expect(hasClosePreviewFlag([])).toBe(false)
})
})