hermes-agent/apps/desktop/electron/window-state.test.ts
Siddharth Balyan 666f313d1d
Desktop opens new installs at a screen-sized window and 110% zoom (#127448)
* feat(desktop): open new installs at a display-sized window and 110% zoom

A fresh install opened at a fixed 1220x800 and 90% zoom, so body text sat
below the Apple and Windows minimums and users pressed Cmd+ repeatedly.

The first window now takes 75% x 80% of the primary display's work area,
floored at 1220x800, capped at 1600x1000, and never larger than the work
area. A saved window-state.json still wins. The default zoom is the 110%
UI Scale preset; Cmd+0 / Actual Size resets to it. Existing installs keep
their saved zoom (first launch has always written the default to
zoom-state.json).

* chore: retrigger CI (zero-job dispatch failure, auto-heal)

* test(desktop-e2e): let a full-window overlay cover the viewport within 1 px

waitForInteractive treats a fixed element as blocking only when its rect
reaches innerWidth/innerHeight. innerWidth is rounded: at 110% zoom a
1280x860 window is 1163.6 CSS px wide and innerWidth reads 1164, so the boot
overlay (inset-0, 1163.6 px) no longer counted as covering the window and the
update E2E declared the app interactive while it was still booting.
2026-09-29 06:55:56 +00:00

198 lines
7.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Unit tests for the pure window-state geometry helpers. These cover the logic
* that protects the user: garbage rejection, off-screen fallback, oversized
* clamping, and the debounce that collapses mid-drag write storms.
*/
import assert from 'node:assert/strict'
import { EventEmitter } from 'node:events'
import { test, vi } from 'vitest'
import {
bindGeometryPersistence,
computeWindowOptions,
debounce,
matchingWorkArea,
MIN_HEIGHT,
MIN_WIDTH,
sanitizeWindowState
} from './window-state'
// A single 1920×1080 monitor (work area trimmed for the taskbar).
const PRIMARY = [{ workArea: { x: 0, y: 0, width: 1920, height: 1040 } }]
// A laptop panel left behind after a bigger external monitor is unplugged.
const LAPTOP = [{ workArea: { x: 0, y: 0, width: 1366, height: 728 } }]
// ─── sanitizeWindowState ───────────────────────────────────────────────────
test('sanitizeWindowState rejects missing/garbage input', () => {
for (const bad of [
null,
undefined,
'nope',
42,
{},
{ width: 'x', height: 800 },
{ width: NaN, height: 800 },
{ width: 1000 }
]) {
assert.equal(sanitizeWindowState(bad), null)
}
})
test('sanitizeWindowState keeps a valid full state and rounds HiDPI fractions', () => {
assert.deepEqual(sanitizeWindowState({ x: 100.6, y: 50.2, width: 1400.4, height: 900.7, isMaximized: true }), {
x: 101,
y: 50,
width: 1400,
height: 901,
isMaximized: true
})
})
test('sanitizeWindowState floors size to the minimums', () => {
const state = sanitizeWindowState({ width: 10, height: 10 })
assert.equal(state.width, MIN_WIDTH)
assert.equal(state.height, MIN_HEIGHT)
})
test('sanitizeWindowState drops a partial position but keeps the size', () => {
assert.deepEqual(sanitizeWindowState({ x: 100, width: 1400, height: 900 }), {
width: 1400,
height: 900,
isMaximized: false
})
})
test('sanitizeWindowState treats isMaximized strictly', () => {
assert.equal(sanitizeWindowState({ width: 1400, height: 900, isMaximized: 'yes' }).isMaximized, false)
})
// ─── matchingWorkArea ──────────────────────────────────────────────────────────────
test('matchingWorkArea accepts a window on the primary or a secondary display', () => {
const dual = [...PRIMARY, { workArea: { x: 1920, y: 0, width: 2560, height: 1400 } }]
assert.notEqual(matchingWorkArea({ x: 100, y: 100, width: 1220, height: 800 }, PRIMARY), null)
assert.notEqual(matchingWorkArea({ x: 2200, y: 200, width: 1220, height: 800 }, dual), null)
})
test('matchingWorkArea rejects off-screen, slivers, and bad input', () => {
assert.equal(matchingWorkArea({ x: 3000, y: 100, width: 1220, height: 800 }, PRIMARY), null) // past right edge
assert.equal(matchingWorkArea({ x: 100, y: -900, width: 1220, height: 800 }, PRIMARY), null) // above top
assert.equal(matchingWorkArea({ x: 1910, y: 100, width: 1220, height: 800 }, PRIMARY), null) // ~10px sliver
assert.equal(matchingWorkArea({ x: 0, y: 0, width: 1220, height: 800 }, []), null)
assert.equal(matchingWorkArea({ x: 0, y: 0, width: 1220, height: 800 }, null), null)
})
// ─── computeWindowOptions ──────────────────────────────────────────────────
test('computeWindowOptions restores an on-screen position', () => {
const saved = sanitizeWindowState({ x: 200, y: 100, width: 1400, height: 900 })
assert.deepEqual(computeWindowOptions(saved, PRIMARY), { width: 1400, height: 900, x: 200, y: 100 })
})
test('computeWindowOptions clamps a trusted saved position fully inside its display work area', () => {
const saved = sanitizeWindowState({ x: -102, y: 175, width: 960, height: 1032 })
assert.deepEqual(computeWindowOptions(saved, PRIMARY), { width: 960, height: 1032, x: 0, y: 8 })
})
test('computeWindowOptions caps a positioned window to the display it overlaps', () => {
const dual = [
{ workArea: { x: 0, y: 0, width: 2560, height: 1400 } },
{ workArea: { x: 2560, y: 0, width: 1366, height: 728 } }
]
const saved = sanitizeWindowState({ x: 2700, y: 100, width: 1400, height: 900 })
assert.deepEqual(computeWindowOptions(saved, dual), { width: 1366, height: 728, x: 2560, y: 0 })
})
test('computeWindowOptions keeps the size but drops an off-screen position', () => {
const saved = sanitizeWindowState({ x: 5000, y: 150, width: 1400, height: 900 })
assert.deepEqual(computeWindowOptions(saved, PRIMARY), { width: 1400, height: 900 })
})
test('computeWindowOptions clamps a size larger than the only display', () => {
const saved = sanitizeWindowState({ width: 2560, height: 1440 })
assert.deepEqual(computeWindowOptions(saved, LAPTOP), { width: 1366, height: 728 })
})
test('computeWindowOptions keeps the MIN floor on a sub-minimum display', () => {
const tiny = [{ workArea: { x: 0, y: 0, width: 360, height: 480 } }]
const saved = sanitizeWindowState({ width: 2000, height: 1500 })
assert.deepEqual(computeWindowOptions(saved, tiny), { width: MIN_WIDTH, height: MIN_HEIGHT })
})
test('computeWindowOptions does not clamp when displays are unknown', () => {
const saved = sanitizeWindowState({ width: 2560, height: 1440 })
assert.deepEqual(computeWindowOptions(saved, []), { width: 2560, height: 1440 })
})
// ─── debounce ──────────────────────────────────────────────────────────────
test('debounce coalesces a burst into one trailing run', () => {
vi.useFakeTimers()
let calls = 0
const d = debounce(() => {
calls += 1
}, 250)
d()
d()
d()
assert.equal(calls, 0)
vi.advanceTimersByTime(249)
assert.equal(calls, 0)
vi.advanceTimersByTime(1)
assert.equal(calls, 1)
vi.useRealTimers()
})
test('debounce.flush runs now and cancels the pending timer', () => {
vi.useFakeTimers()
let calls = 0
const d = debounce(() => {
calls += 1
}, 250)
d()
d.flush()
assert.equal(calls, 1)
vi.advanceTimersByTime(1000)
assert.equal(calls, 1)
vi.useRealTimers()
})
// ─── bindGeometryPersistence ───────────────────────────────────────────────
test('bindGeometryPersistence saves on drag and on resize', () => {
const win = new EventEmitter()
let saves = 0
bindGeometryPersistence(win, () => {
saves += 1
})
win.emit('move')
win.emit('resize')
assert.equal(saves, 2)
})
// The regression this exists for: `moved`/`resized` never fire on Linux, so a
// window bound only to those pretends to persist and silently forgets its place
// every launch. A window that emits nothing else must still save.
test('a window that never emits moved/resized still saves its geometry', () => {
const win = new EventEmitter()
let saves = 0
bindGeometryPersistence(win, () => {
saves += 1
})
win.emit('move')
assert.ok(saves > 0)
})