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 { createElement } from 'react'
import { act, create, type ReactTestRenderer } from 'react-test-renderer'
import { beforeEach, afterEach, expect, it, vi } from 'vitest'
import type { RpcClient } from '../transport/rpc-client'
import { resetWorkerTerminalTakeoverReportsForTest } from '../terminal/worker-terminal-takeover-report'
import { useMobileSessionTerminalWebview } from './use-mobile-session-terminal-webview'
import { useMobileSessionTerminalSendActions } from './use-mobile-session-terminal-send-actions'
import { useMobileSessionTerminalInput } from './use-mobile-session-terminal-input'
import { useMobileTerminalPaste } from './use-mobile-terminal-paste'
import { useTerminalLiveInputCommit } from '../terminal/use-terminal-live-input-commit'
import { routeDictationTranscript } from '../terminal/terminal-live-dictation-routing'
import {
sendMobileNativeChatMessageWithOutcome,
clearMobileNativeChatInput
} from './native-chat/mobile-native-chat-send'
import { sendMobileTerminalQueryReply } from '../terminal/mobile-terminal-query-reply'
import { useMobileDiffReviewSendActions } from './use-mobile-diff-review-send-actions'
import { pasteMobileNativeChatImagePaths } from './native-chat/composer/mobile-native-chat-image-send'
vi.mock('react-native', () => ({ Keyboard: { dismiss: vi.fn() } }))
vi.mock('../platform/haptics', () => ({ triggerError: vi.fn(), triggerSuccess: vi.fn() }))
const clipboard = vi.hoisted(() => ({ text: 'pasted text' }))
vi.mock('expo-clipboard', () => ({ getStringAsync: async () => clipboard.text }))
vi.mock('./mobile-image-source-picker', () => ({
pickMobileImage: vi.fn(),
pickMobileImages: vi.fn()
}))
vi.mock('expo-file-system', () => ({ File: class {}, Paths: { cache: '/tmp' } }))
const REPORT = 'orchestration.workerTerminalUserInput'
const ref = <T>(current: T) => ({ current })
const renderers: ReactTestRenderer[] = []
function clientFixture() {
return {
sendRequest: vi.fn(async (method: string, _params?: unknown) => ({
id: 'rpc',
ok: true as const,
result:
method === 'session.tabs.createTerminal'
? { tab: { type: 'terminal', id: 'tab', terminal: 'term-1', title: 'test' } }
: method === REPORT
? { changed: 1 }
: { send: { accepted: true } }
}))
}
}
function mountSendSites(client: ReturnType<typeof clientFixture>, handle = 'term-1') {
const activeHandleRef = ref<string | null>(handle)
const activeSessionTabTypeRef = ref<string | null>('terminal')
const sendLiveTerminalInputRef = ref(async (_handle: string, _text: string) => false)
const scrollToBottom = vi.fn()
const scope = {
terminalRefs: ref(new Map([[handle, { scrollToBottom }]])),
client,
clientRef: ref(client),
activeHandle: handle,
activeHandleRef,
activeSessionTabTypeRef,
connState: 'connected',
connStateRef: ref('connected'),
activeSessionTab: { type: 'terminal', terminal: handle },
sendingRef: ref(false),
canSend: true,
deviceTokenRef: ref('phone'),
liveInputRef: ref(null),
commandInputRef: ref(null),
liveInputFocusTimerRef: ref(null),
sendLiveTerminalInputRef,
getSendCompletionGeneration: () => 0,
showToast: vi.fn(),
ptyModesRef: ref(new Map([[handle, { altScreen: true }]])),
terminalGestureInputBucketsRef: ref(new Map()),
terminalGestureInputQueuesRef: ref(new Map()),
terminalGestureInputInFlightRef: ref(new Set()),
bufferedTerminalDraftState: {
input: 'command',
beginBufferedTerminalDraftSend: vi.fn(),
restoreRejectedDraft: vi.fn(),
settleBufferedTerminalDraftSend: () => true
}
}
let actions!: ReturnType<typeof useMobileSessionTerminalSendActions>
let live!: ReturnType<typeof useTerminalLiveInputCommit>
let gestures!: ReturnType<typeof useMobileSessionTerminalInput>
let paste!: ReturnType<typeof useMobileTerminalPaste>
let diff!: ReturnType<typeof useMobileDiffReviewSendActions>
function Harness() {
live = useTerminalLiveInputCommit({
activeHandle: handle,
activeHandleRef,
activeSessionTabType: 'terminal',
activeSessionTabTypeRef,
connected: true,
liveInputRef: ref(null),
liveInputTerminalHandles: new Set([handle]),
liveInputTerminalHandlesRef: ref(new Set([handle])),
sendLiveTerminalInputRef,
setLiveInputCapture: vi.fn()
})
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: only supplied view refs are read; unrelated subscription callbacks never run.
const webview = useMobileSessionTerminalWebview(scope as never)
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the fixture supplies the send routes; unrelated session model fields are unused.
actions = useMobileSessionTerminalSendActions({
...scope,
...webview,
handleLiveInputAccessoryBytes: live.handleLiveInputAccessoryBytes
} as never)
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the fixture supplies gesture refs; unrelated file-action callbacks are unused.
gestures = useMobileSessionTerminalInput({
...scope,
liveInputTerminalHandlesRef: ref(new Set([handle])),
sendLiveInputClick: live.sendLiveInputClick
} as never)
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the fixture supplies paste dependencies; unrelated session callbacks are unused.
paste = useMobileTerminalPaste({
...scope,
sendTerminalUserInput: webview.sendTerminalUserInput,
flushPendingLiveInputBeforeExternalSend: live.flushPendingLiveInputBeforeExternalSend,
getActiveWorktreeConnectionId: async () => null,
onError: vi.fn(),
onSuccess: vi.fn(),
refreshCanPaste: vi.fn()
} as never)
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: untyped vi.fn() stubs stand in for callbacks whose returns the send path never reads.
diff = useMobileDiffReviewSendActions({
client: client as unknown as RpcClient,
connState: 'connected',
worktreeId: 'workspace',
screenState: { kind: 'loading' },
setActionError: vi.fn(),
sheets: { openSheet: vi.fn(), closeSheet: vi.fn(), updateSendSheet: vi.fn() },
saveCommentsAndReviewState: vi.fn()
} as never)
return null
}
act(() => {
renderers.push(create(createElement(Harness)))
})
let text = ''
return {
scrollToBottom,
'live field': async () => {
text += 'x'
live.handleLiveInputChange({ nativeEvent: { text, isComposing: false } })
await live.flushPendingLiveInputBeforeExternalSend(handle)
},
'mouse-shaped live field': async () => {
live.handleLiveInputChange({
nativeEvent: { text: '\x1b[<0;2;2M', isComposing: false }
})
await live.flushPendingLiveInputBeforeExternalSend(handle)
},
'live submit': () => live.handleLiveInputSubmit(),
'live accessory': async () => {
live.handleLiveInputChange({ nativeEvent: { text: 'composing', isComposing: true } })
await live.handleLiveInputAccessoryBytes({ bytes: '\x1b[A' })
},
'raw accessory': () => actions.handleAccessoryKey({ bytes: '\x1b[A' } as never),
'buffered submit': () => actions.handleSend(),
'mouse-shaped composer': () => {
scope.bufferedTerminalDraftState.input = '\x1b[<0;2;2M'
return actions.handleSend()
},
'mouse-shaped paste': () => {
clipboard.text = '\x1b[<0;2;2M'
return paste()
},
'accessory tab': () => actions.handleAccessoryKey({ bytes: '\t' }),
'gesture arrows': async () => {
await gestures.handleTerminalInput(handle, '\x1b[A')
await gestures.flushTerminalGestureInput(handle)
},
'gesture wheel': async () => {
await gestures.handleTerminalInput(handle, '\x1b[<64;2;2M')
await gestures.flushTerminalGestureInput(handle)
},
'gesture click': () => gestures.handleTerminalInput(handle, '\x1b[<0;2;2M', { click: true }),
paste: () => paste(),
dictation: async () => {
const route = routeDictationTranscript('dictated text', true)
expect(route.kind).toBe('live-insert')
await actions.sendLiveTerminalInput(handle, route.text)
},
'native chat': () =>
sendMobileNativeChatMessageWithOutcome({
client: client as unknown as RpcClient,
terminal: handle,
text: 'hello'
}),
'query reply': () =>
sendMobileTerminalQueryReply({
bytes: '\x1b[0n',
client,
clientId: 'phone',
connected: true,
handle,
hostSupportsQueryReplyInput: true,
hostSupportsProgramStatus: true,
subscribedTerminals: new Set([handle])
}),
'image heal': () =>
clearMobileNativeChatInput({
client: client as unknown as RpcClient,
terminal: handle,
clearInput: '\x15'
}),
'image attachment': () =>
pasteMobileNativeChatImagePaths({
client,
terminal: handle,
deviceToken: 'phone',
imagePaths: ['/tmp/picture.png'],
followedByText: true
}),
'diff review': () => diff.sendPromptToTerminal(handle, []),
programmatic: () => client.sendRequest('terminal.send')
}
}
beforeEach(() => {
vi.useFakeTimers()
vi.setSystemTime(1_000)
resetWorkerTerminalTakeoverReportsForTest()
clipboard.text = 'pasted text'
})
afterEach(() => {
act(() => {
for (const renderer of renderers.splice(0)) {
renderer.unmount()
}
})
vi.useRealTimers()
})
const realSites = [
'live field',
'live submit',
'live accessory',
'raw accessory',
'buffered submit',
'gesture arrows',
'paste',
'dictation',
'native chat'
] as const
it.each(realSites)('%s reports on its send target once per handle per 30 seconds', async (site) => {
const client = clientFixture()
const sites = mountSendSites(client)
const invoke = async () => {
await act(async () => {
await sites[site]()
})
}
const reports = () => client.sendRequest.mock.calls.filter(([method]) => method === REPORT)
await invoke()
await invoke()
expect(
client.sendRequest.mock.calls.filter(([method]) => method === 'terminal.send').length
).toBeGreaterThanOrEqual(2)
expect(reports()).toHaveLength(1)
expect(reports()[0]).toEqual([REPORT, { terminal: 'term-1' }, expect.any(Object)])
await vi.advanceTimersByTimeAsync(29_999)
await invoke()
expect(reports()).toHaveLength(1)
await vi.advanceTimersByTimeAsync(1)
await invoke()
expect(reports()).toHaveLength(2)
const other = mountSendSites(client, 'term-2')
await act(async () => {
await other[site]()
})
expect(reports()).toHaveLength(3)
expect(reports()[2][1]).toEqual({ terminal: 'term-2' })
})
it.each(['query reply', 'image heal', 'image attachment', 'diff review', 'programmatic'] as const)(
'%s never reports takeover',
async (site) => {
const client = clientFixture()
const sites = mountSendSites(client)
await act(async () => {
await sites[site]()
await sites[site]()
})
expect(client.sendRequest.mock.calls.some(([method]) => method === 'terminal.send')).toBe(true)
expect(client.sendRequest.mock.calls.filter(([method]) => method === REPORT)).toHaveLength(0)
}
)
it.each(realSites)('%s does not report a rejected send', async (site) => {
const client = clientFixture()
client.sendRequest.mockResolvedValue({
id: 'rpc',
ok: true,
result: { send: { accepted: false } }
})
const sites = mountSendSites(client)
await act(async () => {
await sites[site]()
})
expect(client.sendRequest.mock.calls.filter(([method]) => method === REPORT)).toHaveLength(0)
})
it.each([
'live field',
'live submit',
'live accessory',
'raw accessory',
'accessory tab',
'buffered submit',
'mouse-shaped live field',
'mouse-shaped composer',
'mouse-shaped paste',
'paste',
'dictation'
] as const)('%s returns to the prompt once per typed send', async (site) => {
const client = clientFixture()
const sites = mountSendSites(client)
await act(async () => {
await sites[site]()
})
const sends = client.sendRequest.mock.calls.filter(([method]) => method === 'terminal.send')
expect(sends.length).toBeGreaterThan(0)
expect(sites.scrollToBottom).toHaveBeenCalledTimes(sends.length)
})
it.each(['query reply', 'gesture arrows', 'gesture wheel', 'gesture click'] as const)(
'%s preserves the reading position',
async (site) => {
const client = clientFixture()
const sites = mountSendSites(client)
await act(async () => {
await sites[site]()
})
expect(client.sendRequest.mock.calls.some(([method]) => method === 'terminal.send')).toBe(true)
expect(sites.scrollToBottom).not.toHaveBeenCalled()
}
)