hermes-agent/apps/desktop/electron/preload-external-open.test.ts
Brooklyn Nicholson ba5e3bfa31 fix(desktop): say "File not found" when opening a missing file
Opening a deleted/renamed artifact reported "No application found to open
URL": macOS LaunchServices answers a non-existent path with
kLSApplicationNotFoundErr (-10814), and the open route handed resolved
paths to the OS without ever stat-ing. On current main the file route is
reveal-in-folder only, which is worse on a miss — showItemInFolder of a
non-existent path is a silent no-op on macOS, so the click does nothing
at all.

- hardening.ts: assertExistingPathForOpen — pre-open stat wired into
  openExternalFile before the reveal-dedupe window is touched (a miss
  must not poison dedupe against a legitimate retry). Only
  ENOENT/ENOTDIR become an honest missing-file failure.
- external-open.ts: reportPreOpenStatFailure — the guard's
  classification lives in the electron-free module with the rest of the
  open logic, so it unit-tests without electron. A miss is reported and
  the route stops; every other stat failure (EACCES on a locked volume,
  ELOOP, Windows EPERM, ENAMETOOLONG) is logged and the OS is still
  asked, so an existing-but-locked file keeps its real error — a stat
  failure never fabricates a miss and never swallows the click.
- preload.ts: the failure dialog merged in #122023 had no preload
  forwarder for hermes:external-open-failed, so every open failure was
  silently swallowed in every window (the optional-chained bridge is
  invisible to typecheck). Adds the onExternalOpenFailed listener plus a
  test tripwire so the dead wiring cannot come back.
- The failure payload gains code: 'missing-file'; the dialog shows
  localized "File not found" copy for it instead of the no-browser
  text (i18n types + en).

Fixes https://github.com/NousResearch/hermes-agent/issues/122027

Co-authored-by: Andrew Ho <[email protected]>
Co-authored-by: Enough1122 <[email protected]>
2026-09-27 07:10:01 -05:00

53 lines
2.1 KiB
TypeScript

import { expect, it, vi } from 'vitest'
// Tripwire for the dead-wiring class: main broadcasts `hermes:external-open-
// failed` and the dialog subscribes via `onExternalOpenFailed` — with no
// preload forwarder in between the dialog never fires and the failure is
// silent in every window. The dialog guards with an optional-chain, so
// typecheck can never catch the missing listener; this does.
const host = vi.hoisted(() => ({
exposeInMainWorld: vi.fn(),
send: vi.fn<(channel: string, ...args: unknown[]) => void>(),
sendSync: vi.fn((channel: string): unknown => (channel === 'hermes:feature-flags' ? {} : {})),
on: vi.fn((channel: string, listener: (...args: unknown[]) => void) => {
handlers.set(channel, listener)
}),
removeListener: vi.fn((channel: string) => {
handlers.delete(channel)
})
}))
const handlers = new Map<string, (...args: unknown[]) => void>()
vi.mock('electron', () => ({
contextBridge: { exposeInMainWorld: host.exposeInMainWorld },
ipcRenderer: { send: host.send, sendSync: host.sendSync, on: host.on, removeListener: host.removeListener },
webFrame: {},
webUtils: {}
}))
it('exposes onExternalOpenFailed and forwards the failure payload', async (): Promise<void> => {
await import('./preload')
const registration = host.exposeInMainWorld.mock.calls.find(([name]): boolean => name === 'hermesDesktop')
expect(registration).toBeDefined()
const bridge = registration![1] as Record<string, (callback: (payload: unknown) => void) => () => void>
expect(typeof bridge.onExternalOpenFailed).toBe('function')
const received: unknown[] = []
const unsubscribe = bridge.onExternalOpenFailed(payload => received.push(payload))
const listener = handlers.get('hermes:external-open-failed')
expect(listener).toBeDefined()
listener?.({}, { url: 'file:///tmp/gone.html', message: 'Open external file failed: missing.', code: 'missing-file' })
expect(received).toEqual([
{ url: 'file:///tmp/gone.html', message: 'Open external file failed: missing.', code: 'missing-file' }
])
unsubscribe()
expect(handlers.has('hermes:external-open-failed')).toBe(false)
})