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.
TypeScript adeagent-ideai-agentsautomationbotclaude-codecodexghosttygrok-bothermes-agentmobile-appmuseopenclawopencodeorchestrationpiruntimeterminalworktreesyc-backed
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { connect, type RpcClient } from './rpc-client'
vi.mock('./direct-e2ee-framing-policy', () => import('./legacy-direct-framing-test-policy'))
vi.mock('./e2ee', () => ({
generateKeyPair: () => ({
publicKey: new Uint8Array(32),
secretKey: new Uint8Array(32)
}),
deriveSharedKey: () => new Uint8Array(32),
publicKeyFromBase64: () => new Uint8Array(32),
publicKeyToBase64: () => 'client-public-key',
encrypt: (plaintext: string) => `encrypted:${plaintext}`,
decrypt: (raw: string) => raw.replace(/^encrypted:/, ''),
decryptBytes: (bytes: Uint8Array) => bytes
}))
vi.mock('./mobile-runtime-capability-negotiation', () => ({
negotiateMobileRuntimeCapabilities: (args: { onReady: () => void }) => args.onReady()
}))
class RuntimeEventTestSocket {
static CONNECTING = 0
static OPEN = 1
static CLOSING = 2
static CLOSED = 3
readonly CONNECTING = RuntimeEventTestSocket.CONNECTING
readonly OPEN = RuntimeEventTestSocket.OPEN
readonly CLOSING = RuntimeEventTestSocket.CLOSING
readonly CLOSED = RuntimeEventTestSocket.CLOSED
readyState = RuntimeEventTestSocket.CONNECTING
onopen: (() => void) | null = null
onclose: (() => void) | null = null
onmessage: ((event: { data: unknown }) => void) | null = null
onerror: (() => void) | null = null
sent: string[] = []
constructor(readonly endpoint: string) {
sockets.push(this)
}
send(payload: string): void {
this.sent.push(payload)
}
close(): void {
this.readyState = RuntimeEventTestSocket.CLOSED
this.onclose?.()
}
open(): void {
this.readyState = RuntimeEventTestSocket.OPEN
this.onopen?.()
}
receive(payload: unknown): void {
this.onmessage?.({ data: payload })
}
}
type SentRequest = { id: string; method: string; params?: unknown }
const sockets: RuntimeEventTestSocket[] = []
const originalWebSocket = globalThis.WebSocket
function sentRequests(socket: RuntimeEventTestSocket, method: string): SentRequest[] {
return socket.sent
.flatMap((payload) => {
const value: unknown = JSON.parse(payload.replace(/^encrypted:/, ''))
return value &&
typeof value === 'object' &&
'id' in value &&
typeof value.id === 'string' &&
'method' in value &&
typeof value.method === 'string'
? [
{
id: value.id,
method: value.method,
params: 'params' in value ? value.params : undefined
}
]
: []
})
.filter((request) => request.method === method)
}
function connectReadyClient(): { client: RpcClient; socket: RuntimeEventTestSocket } {
const client = connect('ws://desktop.invalid', 'token', 'server-key')
const socket = sockets[0]!
socket.open()
socket.receive(JSON.stringify({ type: 'e2ee_ready' }))
socket.receive('encrypted:{"type":"e2ee_authenticated"}')
return { client, socket }
}
function emitReady(
socket: RuntimeEventTestSocket,
requestId: string,
subscriptionId: string
): void {
socket.receive(
`encrypted:${JSON.stringify({
id: requestId,
ok: true,
streaming: true,
result: { type: 'ready', subscriptionId },
_meta: { runtimeId: 'r1' }
})}`
)
}
describe('runtime client-event stream disposal', () => {
beforeEach(() => {
vi.useFakeTimers()
sockets.length = 0
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: This fixture supplies the socket constructor, states, handlers, send and close consumed by the client.
globalThis.WebSocket = RuntimeEventTestSocket as unknown as typeof WebSocket
})
afterEach(() => {
globalThis.WebSocket = originalWebSocket
vi.useRealTimers()
})
it('unsubscribes a stream disposed after ready', () => {
const { client, socket } = connectReadyClient()
const unsubscribe = client.subscribe('runtime.clientEvents.subscribe', null, () => {})
const request = sentRequests(socket, 'runtime.clientEvents.subscribe')[0]!
emitReady(socket, request.id, 'runtime-events:test')
unsubscribe()
expect(sentRequests(socket, 'runtime.clientEvents.unsubscribe')).toEqual([
expect.objectContaining({ params: { subscriptionId: 'runtime-events:test' } })
])
client.close()
})
it('keeps a tombstone and unsubscribes a stream disposed before ready', () => {
const { client, socket } = connectReadyClient()
const listener = vi.fn()
const unsubscribe = client.subscribe('runtime.clientEvents.subscribe', null, listener)
const request = sentRequests(socket, 'runtime.clientEvents.subscribe')[0]!
unsubscribe()
emitReady(socket, request.id, 'runtime-events:late')
expect(listener).not.toHaveBeenCalled()
expect(sentRequests(socket, 'runtime.clientEvents.unsubscribe')).toEqual([
expect.objectContaining({ params: { subscriptionId: 'runtime-events:late' } })
])
client.close()
})
it('releases the replayed registration on the new socket when disposed before its ready', async () => {
const { client, socket: first } = connectReadyClient()
const unsubscribe = client.subscribe('runtime.clientEvents.subscribe', null, () => {})
const request = sentRequests(first, 'runtime.clientEvents.subscribe')[0]!
emitReady(first, request.id, 'runtime-events:first-socket')
first.close()
await vi.advanceTimersByTimeAsync(500)
const second = sockets.at(-1)!
expect(second).not.toBe(first)
second.open()
second.receive(JSON.stringify({ type: 'e2ee_ready' }))
second.receive('encrypted:{"type":"e2ee_authenticated"}')
expect(sentRequests(second, 'runtime.clientEvents.subscribe')).toEqual([
expect.objectContaining({ id: request.id })
])
unsubscribe()
emitReady(second, request.id, 'runtime-events:second-socket')
expect(sentRequests(second, 'runtime.clientEvents.unsubscribe')).toEqual([
expect.objectContaining({ params: { subscriptionId: 'runtime-events:second-socket' } })
])
client.close()
})
})