Skip to content

The agent that grows with you

2.8 KB
Raw Blame
import { JsonRpcGatewayError } from '@hermes/shared'
import { describe, expect, it } from 'vitest'

import { isMissingPendingPromptRequest, isMissingRpcMethod, isOfflineMaintenance } from './gateway-rpc'

describe('isMissingRpcMethod', () => {
  it('trusts the JSON-RPC code over the message when the frame survived', () => {
    expect(isMissingRpcMethod(new JsonRpcGatewayError('unknown method: projects.create', { code: -32601 }))).toBe(true)
    // A tool result that merely mentions the phrase must not read as a capability verdict.
    expect(isMissingRpcMethod(new JsonRpcGatewayError('unknown method in user script', { code: -32000 }))).toBe(false)
  })

  it('falls back to the message for codeless (IPC-flattened) errors', () => {
    expect(isMissingRpcMethod(new Error('unknown method: projects.create'))).toBe(true)
    expect(isMissingRpcMethod(new Error('Method not found'))).toBe(true)
    expect(isMissingRpcMethod(new Error('RPC failed: -32601'))).toBe(true)
  })

  it('ignores unrelated failures', () => {
    expect(isMissingRpcMethod(new Error('Hermes gateway is not connected'))).toBe(false)
    expect(isMissingRpcMethod(new Error('no such project'))).toBe(false)
  })
})

describe('isOfflineMaintenance', () => {
  it('matches only the anchored HTTP 409 status marker, bare or through the IPC bridge', () => {
    // Real server form (api-transport.ts::httpStatusError) and its IPC-wrapped twin.
    const detail = '{"detail":"Exclusive maintenance refused: Gateway runtime already owns profile /x. Drain and stop the gateway, then retry."}'
    expect(isOfflineMaintenance(new Error(`409: ${detail}`))).toBe(true)
    expect(isOfflineMaintenance(new Error(`Error invoking remote method 'hermes:api': Error: 409: ${detail}`))).toBe(true)
    // Decoys: a 409 token in a body, a longer number, another status, bare conflict text.
    expect(isOfflineMaintenance(new Error('500: Query returned 409 rows'))).toBe(false)
    expect(isOfflineMaintenance(new Error('4096: not a status'))).toBe(false)
    expect(isOfflineMaintenance(new Error('404: {"detail":"Not Found"}'))).toBe(false)
    expect(isOfflineMaintenance(new Error('conflict: gateway owns the profile'))).toBe(false)
  })
})

describe('isMissingPendingPromptRequest', () => {
  it('detects stale prompt response errors from the gateway', () => {
    expect(isMissingPendingPromptRequest(new Error('no pending password request'), 'password')).toBe(true)
    expect(isMissingPendingPromptRequest(new Error('RPC failed: no pending value request'), 'value')).toBe(true)
  })

  it('ignores unrelated gateway failures', () => {
    expect(isMissingPendingPromptRequest(new Error('gateway not connected'), 'password')).toBe(false)
    expect(isMissingPendingPromptRequest(new Error('no pending value request'), 'password')).toBe(false)
  })
})