hermes-agent/apps/desktop/electron/window-focus-policy.test.ts
Brooklyn Nicholson 8d30c4eaab fix(desktop): stop focusWindow() and ambient reveals from stealing the Windows foreground during streaming (#83998)
On Windows, BrowserWindow.show() and focus() both seize the OS foreground.
focusWindow() did show() + unconditional focus(), and the
sendOpenUpdatesRequested / deep-link handlers did the same — so any ambient
IPC reaching those paths mid-stream dismissed other apps' native save and
confirm dialogs (the reported Notepad repro).

focusWindow() now reveals with showInactive() and guards focus() with
isFocused() so a redundant call never pumps SetForegroundWindow; the
updates-toast and deep-link call sites get the same guarded pattern. The
policy lives in electron/window-focus-policy.ts (pure, unit-tested).
User-intentional raises (pet overlay toggle/open clicks, HUD/Quick Entry
reveals) keep show()+focus() — a click is consent.

Supersedes #84026 (frizikk), which guarded only the renderer side and
explicitly left the main-process focus paths unchanged.

Fixes #83998

Co-authored-by: Jakub Wolniewicz <[email protected]>
2026-09-29 21:10:10 -05:00

34 lines
1.3 KiB
TypeScript

/**
* Invariants for the Windows foreground-steal fix (#83998).
*
* `focusWindow()` runs for every ambient window raise (tray restore,
* notification click, second-instance, dock/taskbar activate). On Windows,
* `show()` + unconditional `focus()` seizes the OS foreground and dismisses
* other apps' native dialogs while Hermes streams in the background. The
* main-process call sites now follow the policy in window-focus-policy.ts;
* these tests pin it.
*/
import assert from 'node:assert/strict'
import { test } from 'vitest'
import { revealAction, shouldFocusToTakeKeyboard } from './window-focus-policy'
test('an invisible window is revealed without stealing foreground (showInactive)', () => {
assert.equal(revealAction(false), 'showInactive')
})
test('an already-visible window is left alone by the reveal path', () => {
assert.equal(revealAction(true), 'none')
})
test('focus is only taken when the window does not already have it', () => {
assert.equal(shouldFocusToTakeKeyboard({ isFocused: () => false }), true)
})
test('an already-focused window never pumps the OS foreground path', () => {
// The #83998 regression: a redundant .focus() on a focused window still
// runs SetForegroundWindow on Windows, dismissing another app's dialog.
assert.equal(shouldFocusToTakeKeyboard({ isFocused: () => true }), false)
})