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.

1.4 KB
Raw Blame
import type { Page } from '@stablyai/playwright-test'
import { expect } from './orca-app'

type Outcome<Result> =
  | { state: 'pending' }
  | { state: 'done'; value: Result }
  | { state: 'failed'; error: string }

let nextKey = 0

// Poll preload outcomes rather than holding a CDP promise that may be garbage-collected; args cross as JSON.
export async function evaluatePolled<Args, Result>(
  page: Page,
  run: (args: Args) => Promise<Result>,
  args: Args,
  options: { timeout?: number } = {}
): Promise<Result> {
  nextKey += 1
  const key = JSON.stringify(`__e2eEvaluatePolled${nextKey}`)
  await page.evaluate(`(() => {
    const run = ${run.toString()};
    globalThis[${key}] = { state: 'pending' };
    Promise.resolve()
      .then(() => run(${JSON.stringify(args)}))
      .then(
        (value) => { globalThis[${key}] = { state: 'done', value }; },
        (error) => { globalThis[${key}] = { state: 'failed', error: String(error) }; }
      );
  })()`)
  const read = () => page.evaluate<Outcome<Result>>(`globalThis[${key}]`)
  await expect
    .poll(async () => (await read()).state, {
      message: 'page work never settled',
      timeout: options.timeout
    })
    .not.toBe('pending')
  const outcome = await read()
  if (outcome.state !== 'done') {
    throw new Error(outcome.state === 'failed' ? outcome.error : 'page work never settled')
  }
  return outcome.value
}