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 } 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 MockWebSocket {
static CONNECTING = 0
static OPEN = 1
static CLOSED = 3
readonly CONNECTING = MockWebSocket.CONNECTING
readonly OPEN = MockWebSocket.OPEN
readonly CLOSED = MockWebSocket.CLOSED
readyState = MockWebSocket.CONNECTING
onopen: (() => void) | null = null
onclose: (() => void) | null = null
onmessage: ((event: { data: unknown }) => void) | null = null
sent: string[] = []
close = vi.fn(() => {
if (this.readyState === MockWebSocket.CLOSED) {
return
}
this.readyState = MockWebSocket.CLOSED
this.onclose?.()
})
constructor(readonly endpoint: string) {
mockSockets.push(this)
}
send(payload: string): void {
this.sent.push(payload)
}
open(): void {
this.readyState = MockWebSocket.OPEN
this.onopen?.()
this.receive(JSON.stringify({ type: 'e2ee_ready' }))
this.receive('encrypted:{"type":"e2ee_authenticated"}')
}
receive(payload: unknown): void {
this.onmessage?.({ data: payload })
}
}
const mockSockets: MockWebSocket[] = []
const originalWebSocket = globalThis.WebSocket
function track(request: Promise<unknown>): { read: () => string } {
let outcome = 'pending'
request.then(
() => {
outcome = 'resolved'
},
(error: Error) => {
outcome = error.message
}
)
return { read: () => outcome }
}
function terminalSendFrames(socket: MockWebSocket): string[] {
return socket.sent.filter((frame) => frame.includes('terminal.send'))
}
describe('mobile rpc-client connect-wait replay', () => {
beforeEach(() => {
vi.useFakeTimers()
mockSockets.length = 0
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: This socket fixture supplies the constructor, states, handlers, send and close consumed by the real client.
globalThis.WebSocket = MockWebSocket as unknown as typeof WebSocket
})
afterEach(() => {
vi.useRealTimers()
globalThis.WebSocket = originalWebSocket
})
it('Given a cut connection When a send opts into failWhenDisconnected Then it rejects now and nothing replays after reconnect', async () => {
const client = connect('ws://desktop.invalid', 'token', 'server-key')
const socket = mockSockets[0]!
socket.open()
socket.close()
const request = client.sendRequest(
'terminal.send',
{ terminal: 't1', text: 'YZZY' },
{ failWhenDisconnected: true }
)
const outcome = track(request)
try {
await vi.advanceTimersByTimeAsync(0)
expect(outcome.read()).toBe('Not connected: terminal.send')
// Reconnect; the rejected keystroke must not ride the new socket.
await vi.advanceTimersByTimeAsync(500)
mockSockets[1]!.open()
await vi.advanceTimersByTimeAsync(0)
expect(client.getState()).toBe('connected')
expect(terminalSendFrames(mockSockets[1]!)).toEqual([])
} finally {
client.close()
await request.catch(() => undefined)
}
})
it('Given a cut connection When a caller does not opt in Then the send parks and is delivered on the next socket', async () => {
const client = connect('ws://desktop.invalid', 'token', 'server-key')
const socket = mockSockets[0]!
socket.open()
socket.close()
// Pins the default behavior that motivates the opt-out: parked requests
// replay after reconnect, which is what corrupted post-recovery input.
const request = client.sendRequest('terminal.send', { terminal: 't1', text: 'YZZY' })
const outcome = track(request)
try {
await vi.advanceTimersByTimeAsync(0)
expect(outcome.read()).toBe('pending')
await vi.advanceTimersByTimeAsync(500)
mockSockets[1]!.open()
await vi.advanceTimersByTimeAsync(0)
expect(terminalSendFrames(mockSockets[1]!)).toHaveLength(1)
const frame: unknown = JSON.parse(
terminalSendFrames(mockSockets[1]!)[0]!.replace(/^encrypted:/, '')
)
expect(frame).toEqual({
id: expect.any(String),
deviceToken: 'token',
method: 'terminal.send',
params: { terminal: 't1', text: 'YZZY' }
})
if (!frame || typeof frame !== 'object' || !('id' in frame) || typeof frame.id !== 'string') {
throw new Error('Expected the written request ID')
}
mockSockets[1]!.receive(
`encrypted:${JSON.stringify({ id: frame.id, ok: true, result: { sent: true } })}`
)
await expect(request).resolves.toMatchObject({ ok: true, result: { sent: true } })
} finally {
client.close()
await request.catch(() => undefined)
}
})
})