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.2 KB
Raw Blame
import { vi } from 'vitest'

// Mirror WebIDL timer receivers: omitted means global; explicit nonglobal receivers throw.
function assertGlobalReceiver(receiver: unknown): void {
  if (receiver !== undefined && receiver !== globalThis) {
    throw new TypeError('Illegal invocation')
  }
}

export type GuardedTimerHandles = {
  scheduled: ReturnType<typeof setTimeout>[]
  cleared: ReturnType<typeof setTimeout>[]
}

// Wrap current real/fake timers so virtual time still works; restore with vi.unstubAllGlobals().
export function installIllegalInvocationTimerGuards(): GuardedTimerHandles {
  const scheduleTimer = globalThis.setTimeout
  const cancelTimer = globalThis.clearTimeout
  const handles: GuardedTimerHandles = { scheduled: [], cleared: [] }
  vi.stubGlobal('setTimeout', function (this: unknown, handler: () => void, ms?: number) {
    assertGlobalReceiver(this)
    const handle = scheduleTimer(handler, ms)
    handles.scheduled.push(handle)
    return handle
  })
  vi.stubGlobal('clearTimeout', function (this: unknown, handle: ReturnType<typeof setTimeout>) {
    assertGlobalReceiver(this)
    handles.cleared.push(handle)
    cancelTimer(handle)
  })
  return handles
}