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.

3.7 KB
Raw Blame
import { afterEach, describe, expect, it } from 'vitest'
import { createFakeBridgePortPair as createPair } from './bridge-port-pair-test-harness'
import { readBridgeHostMessage } from './bridge-envelope'
import { createBridgeInitFrame } from './bridge-init-frame'
import { readShellSession } from './bridge-client-session'

const resources = new Set<ReturnType<typeof createPair>>()
function createFakeBridgePortPair(
  ...args: Parameters<typeof createPair>
): ReturnType<typeof createPair> {
  const resource = createPair(...args)
  resources.add(resource)
  return resource
}
function closeResources(): void {
  for (const resource of resources) {
    resource.client.close()
    resource.host.dispose()
  }
  resources.clear()
}

function initFrame(keyboardInset?: number) {
  return createBridgeInitFrame({
    sessionId: 'session-a',
    buildId: 'build-a',
    connection: {
      state: 'connected',
      reconnectAttempt: 0,
      lastConnectedAt: null,
      lastInboundAt: null,
      generation: null
    },
    route: { pathname: '/h/host-a' },
    pageRoutes: ['/h/[hostId]'],
    granted: [],
    host: { id: 'host-a', name: 'Host A', endpoint: 'ws://host-a', lastConnected: 0 },
    storage: {},
    ...(keyboardInset === undefined ? {} : { keyboardInset })
  })
}

function readInit(json: string) {
  const read = readBridgeHostMessage(json)
  if (!read.ok || read.message.type !== 'init') {
    throw new Error(
      `not an init the page would read: ${read.ok ? read.message.type : read.refusal}`
    )
  }
  return read.message
}

describe('keyboardInset on init', () => {
  it('reads an init without the field as 0, which is every shell before it', () => {
    for (const value of [undefined, 0]) {
      const init = readInit(JSON.stringify(initFrame(value)))
      expect('keyboardInset' in init).toBe(false)
      expect(readShellSession(init).keyboardInset).toBe(0)
    }
  })

  it('passes the keyboard height through to the session the page holds', () => {
    expect(readShellSession(readInit(JSON.stringify(initFrame(312)))).keyboardInset).toBe(312)
  })

  it('refuses negative, oversized, null and string keyboard heights', () => {
    for (const bad of [-1, 1e9, null, '312']) {
      const frame = { ...initFrame(), keyboardInset: bad }
      expect(readBridgeHostMessage(JSON.stringify(frame)).ok, String(bad)).toBe(false)
    }
  })
})

describe('a keyboard that moves under a live page', () => {
  it('re-sends init on show, height change and hide, and the page reads each in place', async () => {
    const pair = createFakeBridgePortPair()
    await pair.flush()
    const session = pair.client.getShellSession()
    expect(session?.keyboardInset).toBe(0)
    const seen: number[] = []
    const unsubscribe = pair.client.onKeyboardInsetUpdate((height) => seen.push(height))

    // Pixel_API_37: the IME opens at 312, the suggestion strip grows it by 34, then it closes.
    for (const height of [312, 346, 0]) {
      pair.host.publishKeyboardInset(height)
      await pair.flush()
      expect(pair.client.getShellSession()?.keyboardInset).toBe(height)
    }
    expect(seen).toEqual([312, 346, 0])
    unsubscribe()
    expect(pair.client.getShellSession()?.sessionId).toBe(session?.sessionId)
    expect(pair.client.getShellSession()?.route).toEqual(session?.route)
  })

  it('sends nothing for a height that did not move', async () => {
    const pair = createFakeBridgePortPair({ keyboardInset: 312 })
    await pair.flush()
    const inits = () => pair.readToPage().filter((frame) => frame.type === 'init').length
    const before = inits()
    pair.host.publishKeyboardInset(312)
    await pair.flush()
    expect(inits()).toBe(before)
    expect(pair.client.getShellSession()?.keyboardInset).toBe(312)
  })
})

afterEach(closeResources)