Files
orca/mobile/src/components/codex-reset-credit-capability.test.ts
T
Jinwoo Hong c37413271e perf(mobile): open a session with parallel startup RPCs and a pre-warmed terminal engine (#19260)
Startup RPCs now fan out in parallel and the xterm engine pre-warms inside the
real terminal frame while they are in flight, so the first pane inherits a warm
WebView and an already-measured viewport instead of paying a round trip for it.

The pre-warm opens its engine before measuring: web-ready only reports that the
bundle loaded, and the WebView answers a measure with null until a terminal
exists. It also pre-warms at the user's saved text size, because cell size is
what the frame height gets divided by.

Host writes such as worktree.activate wait for an evaluated status.get reply.
Navigation still fails open when a host cannot answer one, but that fallback no
longer reads as a passing compatibility verdict.
2026-09-07 13:16:44 -04:00

81 lines
2.8 KiB
TypeScript

import { createElement } from 'react'
import { act, create, type ReactTestRenderer } from 'react-test-renderer'
import { afterEach, describe, expect, it, vi } from 'vitest'
import type { RpcClient } from '../transport/rpc-client'
const probe = vi.hoisted(() => ({
start: vi.fn()
}))
vi.mock('../transport/runtime-status-probe', () => ({
startRuntimeCapabilityProbe: probe.start
}))
import {
MOBILE_CODEX_RESET_CREDIT_CAPABILITY,
readCodexResetCreditCapability,
useCodexResetCreditCapability
} from './codex-reset-credit-capability'
afterEach(() => {
vi.restoreAllMocks()
probe.start.mockReset()
})
describe('readCodexResetCreditCapability', () => {
it('enables reset only when the host explicitly advertises the contract', async () => {
const sendRequest = vi.fn().mockResolvedValue({
ok: true,
result: { capabilities: ['mobile.tasks.v1', MOBILE_CODEX_RESET_CREDIT_CAPABILITY] }
})
await expect(readCodexResetCreditCapability({ sendRequest })).resolves.toBe(true)
expect(sendRequest).toHaveBeenCalledWith('status.get')
})
it.each([
{ ok: true, result: { capabilities: ['mobile.tasks.v1'] } },
{ ok: true, result: { capabilities: 'accounts.codex-reset-credit.v1' } },
{ ok: false, error: { code: 'old-host', message: 'unsupported' } }
])('fails closed for an unsupported or malformed host response', async (response) => {
const sendRequest = vi.fn().mockResolvedValue(response)
await expect(readCodexResetCreditCapability({ sendRequest })).resolves.toBe(false)
})
it('fails closed when the capability probe cannot complete', async () => {
const sendRequest = vi.fn().mockRejectedValue(new Error('connection lost'))
await expect(readCodexResetCreditCapability({ sendRequest })).resolves.toBe(false)
})
})
describe('useCodexResetCreditCapability', () => {
it('uses the reconnect-safe probe and cancels it on unmount', () => {
const cancel = vi.fn()
let publish: ((capabilities: readonly string[]) => void) | null = null
probe.start.mockImplementation(
(_client: RpcClient, onCapabilities: (capabilities: readonly string[]) => void) => {
publish = onCapabilities
return cancel
}
)
const client = { sendRequest: vi.fn() } as unknown as RpcClient
let renderer: ReactTestRenderer | null = null
function Harness() {
const supported = useCodexResetCreditCapability(client, true)
return createElement('CapabilityResult', { supported })
}
act(() => {
renderer = create(createElement(Harness))
})
expect(renderer!.root.findByType('CapabilityResult').props.supported).toBe(false)
act(() => publish?.([MOBILE_CODEX_RESET_CREDIT_CAPABILITY]))
expect(renderer!.root.findByType('CapabilityResult').props.supported).toBe(true)
act(() => renderer!.unmount())
expect(cancel).toHaveBeenCalledOnce()
})
})