The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
import { act, cleanup, render } from '@testing-library/react'
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
import { $desktopBoot } from '@/store/boot'
import { closeSecondaryGateways } from '@/store/gateway'
import { $activeGatewayProfile } from '@/store/profile'
import { $connection, $gatewayState } from '@/store/session'
import { FakeWebSocket } from '../../../test/fake-gateway-socket'
import { takeGatewaySurvivor } from './gateway-hmr-survivor'
import { useGatewayBoot } from './use-gateway-boot'
// The blocked update-hold screen's renderer leg (R8 D3/M6): a hold the main
// process publishes on boot progress lands in $desktopBoot even after boot.
// Shares the fake socket with use-gateway-boot.test.tsx; the desktop bridge
// here carries only what the boot hook touches.
vi.mock(import('@/store/notifications'), async importOriginal => ({
...(await importOriginal()),
notifyError: vi.fn()
}))
vi.mock(import('@/store/terminal-backend-warning'), () => ({
warnIfTerminalBackendUnavailable: vi.fn(async () => false)
}))
const primaryConn = {
authMode: 'token' as const,
baseUrl: 'https://vps.example.com',
connectionId: 'primary-vps',
profile: 'default',
token: 't',
wsUrl: 'wss://vps.example.com/api/ws?token=t'
}
function fakeDesktop() {
let bootProgressHandler: ((payload: Record<string, unknown>) => void) | null = null
const unsubscribe = () => () => undefined
return {
getConnection: vi.fn(async () => primaryConn),
getGatewayWsUrl: vi.fn(async (conn?: { wsUrl?: string }) => conn?.wsUrl ?? primaryConn.wsUrl),
getBootProgress: vi.fn(async () => ({
error: null,
fakeMode: false,
message: '',
phase: 'init',
progress: 0,
retryable: false,
running: true,
timestamp: Date.now()
})),
onBootProgress: vi.fn(callback => {
bootProgressHandler = callback
return () => {
bootProgressHandler = null
}
}),
emitBootProgress(payload: Record<string, unknown>) {
bootProgressHandler?.(payload)
},
onBackendExit: vi.fn(unsubscribe),
onConnectionApplied: vi.fn(unsubscribe),
onPowerResume: vi.fn(unsubscribe),
revalidateConnection: vi.fn(async () => ({ ok: true, rebuilt: false })),
onWindowStateChanged: vi.fn(unsubscribe),
profile: { get: vi.fn(async () => ({ profile: 'default' })) }
}
}
function Harness() {
useGatewayBoot({
beforeConnectionSwitch: () => undefined,
handleGatewayEvent: () => undefined,
handleServerRequest: () => false,
onConnectionReady: () => undefined,
onGatewayReady: () => undefined,
refreshHermesConfig: async () => undefined,
refreshSessions: async () => undefined
})
return null
}
function closeSurvivor() {
try {
takeGatewaySurvivor()?.gateway.close()
} catch {
// ignore
}
}
const originalWebSocket = globalThis.WebSocket
beforeEach(() => {
closeSurvivor()
closeSecondaryGateways()
$activeGatewayProfile.set('default')
$connection.set(null)
vi.useFakeTimers()
FakeWebSocket.mode = 'open'
FakeWebSocket.instances = []
FakeWebSocket.pingMode = 'pong'
;(globalThis as { WebSocket: unknown }).WebSocket = FakeWebSocket
$gatewayState.set('idle')
$desktopBoot.set({
error: null,
fakeMode: false,
message: '',
phase: 'init',
progress: 0,
running: true,
timestamp: Date.now(),
visible: true
})
})
afterEach(() => {
cleanup()
closeSurvivor()
closeSecondaryGateways()
$connection.set(null)
vi.useRealTimers()
;(globalThis as { WebSocket: unknown }).WebSocket = originalWebSocket
delete (window as { hermesDesktop?: unknown }).hermesDesktop
})
async function flushAsync() {
await act(async () => {
await vi.advanceTimersByTimeAsync(0)
})
}
it('a hold published after boot (a pool/profile backend) still reaches the blocked screen (R8 M6)', async () => {
const desktop = fakeDesktop()
;(window as { hermesDesktop?: unknown }).hermesDesktop = desktop
render(<Harness />)
await flushAsync()
expect($gatewayState.get()).toBe('open')
const hold = {
holdId: 'a1b2c3d4e5f60718',
verdict: 'held' as const,
ownerPid: 4242,
since: Date.now() - 6_000,
checkedAt: Date.now(),
logPath: '/x/logs/update.log'
}
const progress = {
error: null,
fakeMode: false,
message: 'Hermes is ready',
phase: 'backend.ready',
progress: 100,
retryable: false,
running: false,
timestamp: Date.now()
}
act(() => desktop.emitBootProgress({ ...progress, updateHold: hold }))
expect($desktopBoot.get().updateHold).toEqual(hold)
expect($desktopBoot.get().phase).not.toBe('backend.ready')
act(() => desktop.emitBootProgress({ ...progress, updateHold: null }))
expect($desktopBoot.get().updateHold).toBeNull()
})