Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop): r1-b1 prove host shortcut listener lifecycle

Josué3da21474ec05
diff --git a/desktop-app/src/__tests__/mainShortcutLifecycle.test.ts b/desktop-app/src/__tests__/mainShortcutLifecycle.test.ts
index 78191b1d3..fc3964259 100644
--- a/desktop-app/src/__tests__/mainShortcutLifecycle.test.ts
+++ b/desktop-app/src/__tests__/mainShortcutLifecycle.test.ts
@@ -1,11 +1,86 @@
-import { describe, expect, it } from 'vitest'
-import { readFile } from 'node:fs/promises'
-import path from 'node:path'
+import { describe, expect, it, vi } from 'vitest'
+import { EventEmitter } from 'node:events'
+import type { DesktopShortcutInput } from '../desktopCommands.js'
+import { type DesktopShortcutHost, wireHostDesktopShortcutRouting } from '../shortcutRouter.js'
+
+class FakeWebContents extends EventEmitter {
+ readonly focus = vi.fn()
+ readonly send = vi.fn()
+
+ constructor(readonly id: number) {
+ super()
+ }
+
+ isDestroyed(): boolean {
+ return false
+ }
+
+ isFocused(): boolean {
+ return true
+ }
+}
+
+class FakeBrowserWindow extends EventEmitter {
+ constructor(readonly webContents: FakeWebContents) {
+ super()
+ }
+}
+
+function shortcutInput(): DesktopShortcutInput {
+ return {
+ type: 'keyDown',
+ key: 'f',
+ control: process.platform !== 'darwin',
+ meta: process.platform === 'darwin',
+ alt: false,
+ shift: false,
+ isAutoRepeat: false,
+ isComposing: false,
+ }
+}
+
+function createHost(id: number): FakeBrowserWindow {
+ return new FakeBrowserWindow(new FakeWebContents(id))
+}
describe('main window shortcut lifecycle', () => {
- it('owns host shortcut routing through BrowserWindow teardown', async () => {
- const source = await readFile(path.join(process.cwd(), 'src', 'main.ts'), 'utf8')
+ it('owns one routable listener until close and gives a recreated window a fresh listener', () => {
+ const firstWindow = createHost(1)
+ const firstWebContents = firstWindow.webContents
+ const firstRemoveListener = vi.spyOn(firstWebContents, 'removeListener')
+
+ wireHostDesktopShortcutRouting(firstWindow as unknown as DesktopShortcutHost)
+
+ expect(firstWebContents.listenerCount('before-input-event')).toBe(1)
+ const firstListener = firstWebContents.listeners('before-input-event')[0]
+ const firstEvent = { preventDefault: vi.fn() }
+ firstWebContents.emit('before-input-event', firstEvent, shortcutInput())
+
+ expect(firstEvent.preventDefault).toHaveBeenCalledOnce()
+ expect(firstWebContents.send).toHaveBeenCalledExactlyOnceWith('shortcuts:command', {
+ commandId: 'search.open',
+ source: 'host',
+ })
+
+ firstWindow.emit('closed')
+ firstWindow.emit('closed')
+
+ expect(firstRemoveListener).toHaveBeenCalledExactlyOnceWith('before-input-event', firstListener)
+ expect(firstWebContents.listenerCount('before-input-event')).toBe(0)
+ firstWebContents.emit('before-input-event', { preventDefault: vi.fn() }, shortcutInput())
+ expect(firstWebContents.send).toHaveBeenCalledOnce()
+
+ const secondWindow = createHost(2)
+ const secondWebContents = secondWindow.webContents
+ wireHostDesktopShortcutRouting(secondWindow as unknown as DesktopShortcutHost)
+
+ expect(secondWebContents.listenerCount('before-input-event')).toBe(1)
+ expect(secondWebContents.listeners('before-input-event')[0]).not.toBe(firstListener)
+ secondWebContents.emit('before-input-event', { preventDefault: vi.fn() }, shortcutInput())
- expect(source).toContain('wireHostDesktopShortcutRouting(window)')
+ expect(secondWebContents.send).toHaveBeenCalledExactlyOnceWith('shortcuts:command', {
+ commandId: 'search.open',
+ source: 'host',
+ })
})
})
diff --git a/desktop-app/src/__tests__/shortcutRouter.test.ts b/desktop-app/src/__tests__/shortcutRouter.test.ts
index 2e389aef9..16da1db8f 100644
--- a/desktop-app/src/__tests__/shortcutRouter.test.ts
+++ b/desktop-app/src/__tests__/shortcutRouter.test.ts
@@ -4,7 +4,6 @@ import {
type DesktopShortcutRoute,
isStandardEditingShortcut,
routeDesktopShortcut,
- wireHostDesktopShortcutRouting,
} from '../shortcutRouter.js'
function input(overrides: Partial<DesktopShortcutInput> = {}): DesktopShortcutInput {
@@ -83,35 +82,6 @@ describe('main-process Desktop shortcut routing', () => {
})
})
- it('removes a host shortcut listener when its owning window closes', () => {
- const webContents = {
- id: 1,
- isDestroyed: () => false,
- isFocused: () => true,
- focus: vi.fn(),
- on: vi.fn(),
- removeListener: vi.fn(),
- send: vi.fn(),
- }
- let closeListener: (() => void) | undefined
- const host = {
- webContents,
- once: vi.fn((_event: 'closed', listener: () => void) => {
- closeListener = listener
- }),
- }
-
- wireHostDesktopShortcutRouting(host)
-
- expect(webContents.on).toHaveBeenCalledOnce()
- expect(host.once).toHaveBeenCalledWith('closed', expect.any(Function))
- closeListener?.()
- expect(webContents.removeListener).toHaveBeenCalledWith(
- 'before-input-event',
- expect.any(Function)
- )
- })
-
it('does not intercept reserved, ordinary editing, IME, AltGr, or key-up input', () => {
const current = route('host')
const preventDefault = vi.fn()