hermes-agent/apps/desktop/electron/backend-serve-support.test.ts
kshitijk4poor 844e5f2610 fix(desktop): do not cache a timed-out serve-support probe
The serve-support resolver caches the probe outcome per resolved runtime
for the process lifetime. That is right for a genuine "unknown
subcommand" exit, but a probe that died by timeout says nothing about the
runtime — only that this machine was slow right then (cold AV scan on
Windows, first Python import after boot). Caching that as `false` routed
a modern runtime through the legacy `dashboard` form until the app was
relaunched.

Export isTimeoutError from backend-probes and evict the cache entry on a
timeout so the next check re-probes; non-timeout failures stay cached.
One vitest case covers timeout → re-probe; the existing case still pins
non-timeout failure → cached.

Also replace the last synchronous read on the discovery path
(dashboard.py fast path) with fs.promises.readFile, matching the stack's
goal of keeping runtime discovery off the main event loop.
2026-09-15 10:50:53 +05:30

49 lines
1.7 KiB
TypeScript

import assert from 'node:assert/strict'
import { test, vi } from 'vitest'
import * as probes from './backend-probes'
import { createBackendServeSupportResolver } from './backend-serve-support'
test('concurrent serve checks share one pending probe and retain its negative result', async () => {
let fail!: (error: Error) => void
const pending = new Promise<void>((_resolve, reject) => {
fail = reject
})
const probe = vi.spyOn(probes, 'execProbe').mockReturnValue(pending)
const supportsServe = createBackendServeSupportResolver('/unused', () => {})
const backend = { command: '/unused/hermes', args: ['serve'] }
try {
const first = supportsServe(backend)
const second = supportsServe(backend)
const callsWhilePending = probe.mock.calls.length
fail(new Error('unsupported'))
assert.deepEqual(await Promise.all([first, second]), [false, false])
assert.equal(callsWhilePending, 1)
assert.equal(await supportsServe(backend), false)
assert.equal(probe.mock.calls.length, 1)
} finally {
probe.mockRestore()
}
})
test('a probe that fails by timeout is not cached, so the next check re-probes', async () => {
const probe = vi
.spyOn(probes, 'execProbe')
.mockRejectedValueOnce(Object.assign(new Error('timed out'), { killed: true }))
.mockResolvedValueOnce(undefined)
const supportsServe = createBackendServeSupportResolver('/unused', () => {})
const backend = { command: '/unused/hermes', args: ['serve'] }
try {
assert.equal(await supportsServe(backend), false)
assert.equal(await supportsServe(backend), true)
assert.equal(probe.mock.calls.length, 2)
} finally {
probe.mockRestore()
}
})