Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

2.6 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest'
import { RelayDispatcher } from './dispatcher'
import type { RelayClient } from './dispatcher-contract'
import type { JsonRpcNotification } from './protocol'

type DispatcherInternals = {
  primaryClient: RelayClient
  estimateFrameBytes: (msg: JsonRpcNotification) => number
  enqueueFrame: (client: RelayClient, msg: JsonRpcNotification, lane: string) => boolean
}

describe('RelayDispatcher frame guards', () => {
  it('returns zero for invalid active-client limits without encoding', () => {
    const dispatcher = new RelayDispatcher(() => true, {
      writableHighWaterMark: () => 1024 * 1024,
      writableLength: () => 0
    })
    try {
      const spy = vi.spyOn(dispatcher as unknown as DispatcherInternals, 'estimateFrameBytes')
      for (const limit of [0, -1, Number.NaN]) {
        expect(dispatcher.maxLegacyPtyDataChars({ id: 'pty-1' }, 'hello', limit)).toBe(0)
      }
      expect(spy).not.toHaveBeenCalled()
    } finally {
      dispatcher.dispose()
    }
  })

  it('does not estimate frames after disposal', () => {
    const dispatcher = new RelayDispatcher(() => true)
    const internals = dispatcher as unknown as DispatcherInternals
    const spy = vi.spyOn(internals, 'estimateFrameBytes')
    const msg: JsonRpcNotification = {
      jsonrpc: '2.0',
      method: 'pty.data',
      params: {
        data: {
          toJSON: () => {
            throw new Error('must not serialize')
          }
        }
      }
    }
    dispatcher.dispose()
    expect(internals.enqueueFrame(internals.primaryClient, msg, 'ordinary')).toBe(false)
    expect(spy).not.toHaveBeenCalled()
  })

  it('does not serialize notifications without an active client', () => {
    const dispatcher = new RelayDispatcher(() => true)
    try {
      dispatcher.invalidateClient()

      expect(() =>
        dispatcher.notify('test.event', {
          data: {
            toJSON: () => {
              throw new Error('must not serialize')
            }
          }
        })
      ).not.toThrow()
    } finally {
      dispatcher.dispose()
    }
  })

  it('does not serialize pty.data rejected by every active client', () => {
    const dispatcher = new RelayDispatcher(() => true)
    const unregister = dispatcher.registerPtyDataPublicationAdmission(() => false)
    try {
      expect(() =>
        dispatcher.notify('pty.data', {
          data: {
            toJSON: () => {
              throw new Error('must not serialize')
            }
          }
        })
      ).not.toThrow()
    } finally {
      unregister()
      dispatcher.dispose()
    }
  })
})