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.6 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest'
import { startRegionalRehomeWorker } from './regional-rehome-worker.js'
import { jitteredSweepIntervalMs } from './relay-sweep-schedule.js'

describe('sweep schedule jitter', () => {
  it('spreads instances across a bounded window above the base period', () => {
    for (const random of [0, 0.25, 0.5, 0.75, 0.999]) {
      expect(jitteredSweepIntervalMs(1_000, () => random)).toBeGreaterThanOrEqual(1_000)
    }
    expect(jitteredSweepIntervalMs(30_000, () => 0)).toBe(30_000)
    expect(jitteredSweepIntervalMs(30_000, () => 0.5)).toBe(33_000)
    // Math.random() never returns 1, so the open bound is the real ceiling.
    expect(jitteredSweepIntervalMs(30_000, () => 0.999)).toBeLessThan(36_000)
  })

  it('jitters the six-second regional rehome dispatch tick across directors', () => {
    const timers: number[] = []
    const setIntervalSpy = vi
      .spyOn(globalThis, 'setInterval')
      .mockImplementation(((_handler: unknown, delayMs?: number) => {
        timers.push(delayMs ?? 0)
        return { unref: () => undefined, [Symbol.dispose]: () => undefined } as never
      }) as never)

    try {
      startRegionalRehomeWorker(
        {
          role: 'director',
          rehomeAudience: 'https://rehome.example.test',
          rehomeDirectorServiceAccount: '[email protected]'
        } as never,
        { selectIdleRegionalRehomeCandidates: async () => [] } as never,
        { random: () => 0.5, safetySnapshot: () => ({}) as never }
      )
    } finally {
      setIntervalSpy.mockRestore()
    }

    expect(timers).toEqual([6_600])
  })
})