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.

27.4 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest'
import type { RelayConfig } from './config.js'

vi.mock('./admin-token-verifier.js', () => ({
  createAdminTokenVerifier: () => async (token: string, route?: string) =>
    token === 'deploy-token' ||
    (token === 'monitor-token' &&
      (!route ||
        route === '/v1/admin/regional-rehome-control' ||
        route === '/v1/admin/regional-rehome-preview')),
  createReadOnlyAdminTokenVerifier: () => async () => false,
  createRegionalRehomeControlApplyTokenVerifier: () => async (token: string) =>
    token === 'deploy-token',
  createRegionalRehomeRuntimeTokenVerifier: () => async (token: string) =>
    token === 'runtime-token',
  createRegionalRehomeTokenVerifier: () => async (token: string) => token === 'rehome-token',
  createRuntimeTokenVerifier: () => async (token: string) => token === 'runtime-token'
}))

vi.mock('./relay-token-verifier.js', () => ({
  createRelayTokenVerifier: () => async () => null,
  readBearer: (value: string | undefined) => value?.replace(/^Bearer /, '') ?? null
}))

import { createRelayApp } from './app.js'
import { RelayObservability } from './relay-observability.js'
import { emptyPostgresPoolPressureCounts } from './postgres-pool-pressure.js'
import {
  REGIONAL_REHOME_TRUST_PROBE_ATTEMPT_ID,
  REGIONAL_REHOME_TRUST_PROBE_HOST_ID,
  REGIONAL_REHOME_TRUST_PROBE_USER_ID
} from './regional-rehome-trust-probe.js'

const cellIncarnation = '11111111-1111-4111-8111-111111111111'
const request = {
  v: 1,
  attemptId: '22222222-2222-4222-8222-222222222222',
  userId: 'user-1',
  relayHostId: 'abcdefghijklmnop',
  sourceCellId: 'production-gce-c7',
  sourceCellIncarnation: cellIncarnation,
  sourceAssignmentEpoch: 7,
  graceMs: 60_000
}

describe('idle regional cutover endpoint', () => {
  it('authenticates and fences the source before invoking a cutover', async () => {
    const idleRehome = vi.fn(async () => ({ outcome: 'busy' }))
    const app = createRelayApp(config(), {
      store: {} as never,
      assignments: {} as never,
      drain: vi.fn(),
      idleRehome,
      cellIncarnation,
      ready: vi.fn(async () => true)
    } as Parameters<typeof createRelayApp>[1])
    const input = {
      v: 1,
      attemptId: request.attemptId,
      userId: request.userId,
      relayHostId: request.relayHostId,
      sourceCellId: request.sourceCellId,
      sourceCellIncarnation: cellIncarnation,
      sourceAssignmentEpoch: 7,
      sourceGeneration: 1,
      targetCellId: 'target-cell',
      cohortPercent: 100,
      directorSafety: {
        observedAt: 100, sqlFailures: 0, reconnects: 0, controlActivityRecoveryFailures: 0,
        databasePoolWaiting: 0, databasePoolWaitersMax: 0, databasePoolWaitMsMax: 0
      }
    }
    const path = '/v1/admin/host-idle-rehome'
    expect((await postPath(app, path, 'runtime-token', input)).status).toBe(401)
    expect(
      (
        await postPath(app, path, 'rehome-token', {
          ...input,
          sourceCellIncarnation: '33333333-3333-4333-8333-333333333333'
        })
      ).status
    ).toBe(409)
    expect(idleRehome).not.toHaveBeenCalled()
    const response = await postPath(app, path, 'rehome-token', input)
    expect(response.status).toBe(200)
    expect(await response.json()).toEqual({ v: 1, outcome: 'busy' })
    expect(idleRehome).toHaveBeenCalledExactlyOnceWith(input)
  })
})

describe('regional host drain endpoint', () => {
  it('exposes aggregate preview to monitors without a mutation path', async () => {
    const preview = { counts: { 'eligible:asia-east2-to-us-central1': 2 } }
    const safety = { observedAt: 100 }
    const previewRegionalRehomeEligibility = vi.fn(async () => preview)
    const app = createRelayApp(config({ role: 'director', cellId: 'director' }), {
      store: {} as never,
      assignments: {
        previewRegionalRehomeEligibility,
        regionCorrectionOutcomes: async () => []
      } as never,
      regionalRehomeSafetySnapshot: () => safety as never,
      drain: vi.fn(),
      ready: vi.fn(async () => true)
    })
    const path = '/v1/admin/regional-rehome-preview'
    expect((await app.request(path)).status).toBe(401)
    expect(previewRegionalRehomeEligibility).not.toHaveBeenCalled()
    const response = await app.request(path, { headers: { authorization: 'Bearer monitor-token' } })
    expect(response.status).toBe(200)
    expect(await response.json()).toEqual({ v: 1, preview, outcomes: [] })
    expect(previewRegionalRehomeEligibility).toHaveBeenCalledExactlyOnceWith(safety)
    expect(
      (
        await app.request(path, {
          method: 'POST',
          headers: { authorization: 'Bearer deploy-token' }
        })
      ).status
    ).toBe(404)
  })

  it('accepts only the dedicated identity and exact cell generation', async () => {
    const drainHost = vi.fn(() => 'accepted' as const)
    const app = createRelayApp(config(), {
      store: {} as never,
      assignments: {} as never,
      drain: vi.fn(),
      drainHost,
      cellIncarnation,
      ready: vi.fn(async () => true)
    })

    const accepted = await post(app, 'rehome-token', request)
    expect(accepted.status).toBe(200)
    expect(await accepted.json()).toEqual({ v: 1, outcome: 'accepted' })
    expect(drainHost).toHaveBeenCalledWith(request)

    expect((await post(app, 'deploy-token', request)).status).toBe(401)
    expect(
      (
        await post(app, 'rehome-token', {
          ...request,
          sourceCellIncarnation: '33333333-3333-4333-8333-333333333333'
        })
      ).status
    ).toBe(409)
    expect(drainHost).toHaveBeenCalledOnce()
  })

  it('waits for an asynchronous drain operation before acknowledging', async () => {
    let grant!: (value: 'accepted') => void
    let entered!: () => void
    const started = new Promise<void>((resolve) => {
      entered = resolve
    })
    const drainHost = vi.fn(() => {
      entered()
      return new Promise<'accepted'>((resolve) => {
        grant = resolve
      })
    })
    const app = createRelayApp(config(), {
      store: {} as never,
      assignments: {} as never,
      drain: vi.fn(),
      drainHost,
      cellIncarnation,
      ready: vi.fn(async () => true)
    })
    const pending = post(app, 'rehome-token', request)
    let acknowledged = false
    void pending.then(() => {
      acknowledged = true
    })
    await started
    await Promise.resolve()
    expect(acknowledged).toBe(false)
    grant('accepted')
    const response = await pending
    expect(response.status).toBe(200)
    expect(await response.json()).toEqual({ v: 1, outcome: 'accepted' })
  })

  it('rejects a failed asynchronous drain instead of acknowledging it', async () => {
    const app = createRelayApp(config(), {
      store: {} as never,
      assignments: {} as never,
      drain: vi.fn(),
      drainHost: async () => {
        throw new Error('activity_cell_not_authoritative')
      },
      cellIncarnation,
      ready: vi.fn(async () => true)
    })
    const response = await post(app, 'rehome-token', request)
    expect(response.status).toBe(409)
    expect(await response.json()).toEqual({ error: 'activity_cell_not_authoritative' })
  })

  it('rejects malformed identities before touching the session registry', async () => {
    const drainHost = vi.fn(() => 'accepted' as const)
    const app = createRelayApp(config(), {
      store: {} as never,
      assignments: {} as never,
      drain: vi.fn(),
      drainHost,
      cellIncarnation,
      ready: vi.fn(async () => true)
    })

    expect(
      (await post(app, 'rehome-token', { ...request, relayHostId: 'raw-host-id' })).status
    ).toBe(400)
    expect(drainHost).not.toHaveBeenCalled()
  })

  it('is unavailable on the director', async () => {
    const app = createRelayApp(config({ role: 'director', cellId: 'director' }), {
      store: {} as never,
      assignments: {} as never,
      drain: vi.fn(),
      drainHost: vi.fn(() => 'accepted' as const),
      cellIncarnation,
      ready: vi.fn(async () => true)
    })
    expect((await post(app, 'rehome-token', request)).status).toBe(404)
  })

  it('proves the shared runtime identity is rejected without touching a session', async () => {
    const drainHost = vi.fn(() => 'host-not-connected' as const)
    const app = createRelayApp(config(), {
      store: {} as never,
      assignments: {} as never,
      drain: vi.fn(),
      drainHost,
      regionalRehomeTrustProbeHostExists: vi.fn(() => false),
      regionalRehomeIdentityToken: vi.fn(async () => 'runtime-token'),
      cellIncarnation,
      ready: vi.fn(async () => true)
    })
    const probe = {
      ...request,
      attemptId: REGIONAL_REHOME_TRUST_PROBE_ATTEMPT_ID,
      userId: REGIONAL_REHOME_TRUST_PROBE_USER_ID,
      relayHostId: REGIONAL_REHOME_TRUST_PROBE_HOST_ID,
      sourceAssignmentEpoch: 1,
      graceMs: 0
    }

    for (let call = 0; call < 2; call++) {
      const response = await post(app, 'rehome-token', probe)
      expect(response.status).toBe(200)
      expect(await response.json()).toEqual({
        v: 1,
        outcome: 'host-not-connected',
        sharedRuntimeIdentityRejected: true
      })
    }
    expect(drainHost).toHaveBeenCalledTimes(2)
  })

  it('fails closed if the synthetic host is not provably absent', async () => {
    const drainHost = vi.fn(() => 'host-not-connected' as const)
    const app = createRelayApp(config(), {
      store: {} as never,
      assignments: {} as never,
      drain: vi.fn(),
      drainHost,
      regionalRehomeTrustProbeHostExists: vi.fn(() => true),
      regionalRehomeIdentityToken: vi.fn(async () => 'runtime-token'),
      cellIncarnation,
      ready: vi.fn(async () => true)
    })
    const response = await post(app, 'rehome-token', {
      ...request,
      attemptId: REGIONAL_REHOME_TRUST_PROBE_ATTEMPT_ID,
      userId: REGIONAL_REHOME_TRUST_PROBE_USER_ID,
      relayHostId: REGIONAL_REHOME_TRUST_PROBE_HOST_ID,
      sourceAssignmentEpoch: 1,
      graceMs: 0
    })

    expect(response.status).toBe(409)
    expect(drainHost).not.toHaveBeenCalled()
  })

  it('rechecks synthetic host absence after asynchronous identity proof', async () => {
    let hostExists = false
    const drainHost = vi.fn(() => 'host-not-connected' as const)
    const app = createRelayApp(config(), {
      store: {} as never,
      assignments: {} as never,
      drain: vi.fn(),
      drainHost,
      regionalRehomeTrustProbeHostExists: vi.fn(() => hostExists),
      regionalRehomeIdentityToken: vi.fn(async () => {
        hostExists = true
        return 'runtime-token'
      }),
      cellIncarnation,
      ready: vi.fn(async () => true)
    })
    const response = await post(app, 'rehome-token', {
      ...request,
      attemptId: REGIONAL_REHOME_TRUST_PROBE_ATTEMPT_ID,
      userId: REGIONAL_REHOME_TRUST_PROBE_USER_ID,
      relayHostId: REGIONAL_REHOME_TRUST_PROBE_HOST_ID,
      sourceAssignmentEpoch: 1,
      graceMs: 0
    })

    expect(response.status).toBe(409)
    expect(drainHost).not.toHaveBeenCalled()
  })

  it('fails closed when the cell cannot prove its runtime identity rejection', async () => {
    const drainHost = vi.fn(() => 'host-not-connected' as const)
    const app = createRelayApp(config(), {
      store: {} as never,
      assignments: {} as never,
      drain: vi.fn(),
      drainHost,
      regionalRehomeTrustProbeHostExists: vi.fn(() => false),
      regionalRehomeIdentityToken: vi.fn(async () => 'rehome-token'),
      cellIncarnation,
      ready: vi.fn(async () => true)
    })
    const response = await post(app, 'rehome-token', {
      ...request,
      attemptId: REGIONAL_REHOME_TRUST_PROBE_ATTEMPT_ID,
      userId: REGIONAL_REHOME_TRUST_PROBE_USER_ID,
      relayHostId: REGIONAL_REHOME_TRUST_PROBE_HOST_ID,
      sourceAssignmentEpoch: 1,
      graceMs: 0
    })

    expect(response.status).toBe(409)
    expect(drainHost).not.toHaveBeenCalled()
  })
})

describe('regional rehome director controls', () => {
  it('records cell capability separately from the legacy heartbeat contract', async () => {
    const recordCellRegionalRehomeStatus = vi.fn().mockResolvedValue(undefined)
    const app = createRelayApp(config({ role: 'director', cellId: 'director' }), {
      store: {} as never,
      assignments: { recordCellRegionalRehomeStatus } as never,
      drain: vi.fn(),
      ready: vi.fn(async () => true)
    })
    const body = {
      v: 1,
      cellId: 'production-gce-c7',
      cellIncarnation,
      regionalRehomeProtocol: 2,
      safety: {
        observedAt: 100,
        sqlFailures: 0,
        reconnects: 0,
        controlActivityRecoveryFailures: 0,
        databasePoolWaiting: 0,
        databasePoolWaitersMax: 0,
        databasePoolWaitMsMax: 0
      }
    }
    const response = await postPath(app, '/v1/admin/cell-rehome-status', 'runtime-token', body)

    expect(response.status).toBe(200)
    expect(recordCellRegionalRehomeStatus).toHaveBeenCalledWith(body)
  })

  it('accepts the exact safety payload the cell heartbeat composes', async () => {
    // index.ts sends full pool-pressure safety counts; missing strict-schema fields once rejected every heartbeat and left rehome protocol at 0 (2026-08-15..26).
    const recordCellRegionalRehomeStatus = vi.fn().mockResolvedValue(undefined)
    const app = createRelayApp(config({ role: 'director', cellId: 'director' }), {
      store: {} as never,
      assignments: { recordCellRegionalRehomeStatus } as never,
      drain: vi.fn(),
      ready: vi.fn(async () => true)
    })
    const observability = new RelayObservability(
      { role: 'cell', cellId: 'production-gce-c7', region: 'us-central1' },
      () => {}
    )
    observability.stop()
    const body = {
      v: 1,
      cellId: 'production-gce-c7',
      cellIncarnation,
      regionalRehomeProtocol: 2,
      safety: {
        ...observability.regionalRehomeRuntimeSafety(),
        ...emptyPostgresPoolPressureCounts()
      }
    }
    const response = await postPath(app, '/v1/admin/cell-rehome-status', 'runtime-token', body)

    expect(response.status).toBe(200)
    expect(recordCellRegionalRehomeStatus).toHaveBeenCalledWith(body)
  })

  it('applies the durable switch only with matching confirmation', async () => {
    const inspectRegionalRehomeControl = vi.fn().mockResolvedValue({
      generation: 0,
      enabled: false
    })
    const applyRegionalRehomeControl = vi.fn(async (input) => ({
      ...input,
      generation: input.expectedGeneration + 1
    }))
    const app = createRelayApp(config({ role: 'director', cellId: 'director' }), {
      store: {} as never,
      assignments: {
        inspectRegionalRehomeControl,
        applyRegionalRehomeControl
      } as never,
      drain: vi.fn(),
      ready: vi.fn(async () => true)
    })
    expect(
      (
        await postPath(app, '/v1/admin/regional-rehome-control', 'deploy-token', {
          v: 1,
          action: 'inspect'
        })
      ).status
    ).toBe(200)
    const apply = {
      v: 1,
      action: 'apply',
      expectedGeneration: 0,
      enabled: true,
      notBefore: 100,
      ratePerMinute: 10,
      preferenceMaxAgeMs: 24 * 60 * 60_000,
      hostCooldownMs: 7 * 24 * 60 * 60_000,
      drainGraceMs: 60_000,
      confirmation: 'ENABLE_REGIONAL_REHOMING'
    }
    expect(
      (await postPath(app, '/v1/admin/regional-rehome-control', 'deploy-token', apply)).status
    ).toBe(200)
    expect(applyRegionalRehomeControl).toHaveBeenCalledOnce()
    expect(
      (
        await postPath(app, '/v1/admin/regional-rehome-control', 'monitor-token', {
          v: 1,
          action: 'inspect'
        })
      ).status
    ).toBe(200)
    expect(
      (await postPath(app, '/v1/admin/regional-rehome-control', 'monitor-token', apply)).status
    ).toBe(403)
    expect(
      (
        await postPath(app, '/v1/admin/regional-rehome-control', 'deploy-token', {
          ...apply,
          confirmation: 'DISABLE_REGIONAL_REHOMING'
        })
      ).status
    ).toBe(400)
    // The per-host cooldown is part of the durable shape an operator must state.
    const { hostCooldownMs: _omitted, ...withoutCooldown } = apply
    expect(
      (await postPath(app, '/v1/admin/regional-rehome-control', 'deploy-token', withoutCooldown))
        .status
    ).toBe(400)
    expect(applyRegionalRehomeControl).toHaveBeenCalledExactlyOnceWith(apply)
  })

  it('probes dedicated trust twice and returns only aggregate proof', async () => {
    const requests: Array<{ url: string; init?: RequestInit }> = []
    const reserveAwareCellDeploymentStatus = vi.fn().mockResolvedValue({
      admitMode: 'db',
      status: {
        cellId: 'production-gce-c7',
        cellUrl: 'https://c7.relay.example.test',
        region: 'us-central1',
        runtime: {
          cellIncarnation,
          ready: true,
          heartbeatFresh: true,
          regionalRehomeProtocol: 1
        }
      }
    })
    const app = createRelayApp(config({ role: 'director', cellId: 'director' }), {
      store: {} as never,
      assignments: { reserveAwareCellDeploymentStatus } as never,
      drain: vi.fn(),
      regionalRehomeIdentityToken: vi.fn(async () => 'rehome-token'),
      regionalRehomeFetch: (async (url, init) => {
        requests.push({ url: String(url), init })
        return Response.json({
          v: 1,
          outcome: 'host-not-connected',
          sharedRuntimeIdentityRejected: true
        })
      }) as typeof fetch,
      ready: vi.fn(async () => true)
    })
    const response = await postPath(app, '/v1/admin/regional-rehome-trust-probe', 'deploy-token', {
      v: 1,
      sourceCellId: 'production-gce-c7',
      sourceCellIncarnation: cellIncarnation
    })

    expect(response.status).toBe(200)
    const responseBody = await response.json()
    expect(responseBody).toEqual({
      v: 1,
      dedicatedIdentity: {
        accepted: true,
        firstOutcome: 'host-not-connected',
        secondOutcome: 'host-not-connected',
        idempotent: true
      },
      sharedRuntimeIdentityRejected: true,
      proven: true
    })
    expect(requests).toHaveLength(2)
    expect(requests.map(({ url }) => url)).toEqual([
      'https://c7.relay.example.test/v1/admin/host-drain',
      'https://c7.relay.example.test/v1/admin/host-drain'
    ])
    const bodies = requests.map(({ init }) => JSON.parse(String(init?.body)))
    expect(bodies[0]).toEqual(bodies[1])
    expect(bodies[0]).toMatchObject({
      attemptId: REGIONAL_REHOME_TRUST_PROBE_ATTEMPT_ID,
      userId: REGIONAL_REHOME_TRUST_PROBE_USER_ID,
      relayHostId: REGIONAL_REHOME_TRUST_PROBE_HOST_ID,
      sourceAssignmentEpoch: 1,
      graceMs: 0
    })
    expect(requests.every(({ init }) => init?.signal instanceof AbortSignal)).toBe(true)
    expect(JSON.stringify(responseBody)).not.toContain('rehome-token')
  })

  it('probes a source cell in any region, not only the default one', async () => {
    // Rehoming moves hosts in both directions, so an asia-east2 cell is a
    // source too and its trust has to be provable the same way.
    const reserveAwareCellDeploymentStatus = vi.fn().mockResolvedValue({
      admitMode: 'db',
      status: {
        cellId: 'production-gce-c27',
        cellUrl: 'https://c27.relay.example.test',
        region: 'asia-east2',
        runtime: {
          cellIncarnation,
          ready: true,
          heartbeatFresh: true,
          regionalRehomeProtocol: 1
        }
      }
    })
    const app = createRelayApp(config({ role: 'director', cellId: 'director' }), {
      store: {} as never,
      assignments: { reserveAwareCellDeploymentStatus } as never,
      drain: vi.fn(),
      regionalRehomeIdentityToken: vi.fn(async () => 'rehome-token'),
      regionalRehomeFetch: (async () =>
        Response.json({
          v: 1,
          outcome: 'host-not-connected',
          sharedRuntimeIdentityRejected: true
        })) as typeof fetch,
      ready: vi.fn(async () => true)
    })

    const response = await postPath(app, '/v1/admin/regional-rehome-trust-probe', 'deploy-token', {
      v: 1,
      sourceCellId: 'production-gce-c27',
      sourceCellIncarnation: cellIncarnation
    })

    expect(response.status).toBe(200)
    expect(await response.json()).toMatchObject({ proven: true })
  })

  it('still refuses a trust probe against a cell without the drain protocol', async () => {
    const reserveAwareCellDeploymentStatus = vi.fn().mockResolvedValue({
      admitMode: 'db',
      status: {
        cellId: 'production-gce-c27',
        cellUrl: 'https://c27.relay.example.test',
        region: 'asia-east2',
        runtime: {
          cellIncarnation,
          ready: true,
          heartbeatFresh: true,
          regionalRehomeProtocol: 0
        }
      }
    })
    const sourceFetch = vi.fn<typeof fetch>()
    const app = createRelayApp(config({ role: 'director', cellId: 'director' }), {
      store: {} as never,
      assignments: { reserveAwareCellDeploymentStatus } as never,
      drain: vi.fn(),
      regionalRehomeIdentityToken: vi.fn(async () => 'rehome-token'),
      regionalRehomeFetch: sourceFetch,
      ready: vi.fn(async () => true)
    })

    const response = await postPath(app, '/v1/admin/regional-rehome-trust-probe', 'deploy-token', {
      v: 1,
      sourceCellId: 'production-gce-c27',
      sourceCellIncarnation: cellIncarnation
    })

    expect(response.status).toBe(409)
    expect(await response.json()).toEqual({ error: 'regional_rehome_trust_probe_source_unavailable' })
    expect(reserveAwareCellDeploymentStatus).toHaveBeenCalledExactlyOnceWith('production-gce-c27')
    expect(sourceFetch).not.toHaveBeenCalled()
  })

  it('restricts trust probes to deploy authorization and strict input', async () => {
    const app = createRelayApp(config({ role: 'director', cellId: 'director' }), {
      store: {} as never,
      assignments: {} as never,
      drain: vi.fn(),
      ready: vi.fn(async () => true)
    })
    const body = {
      v: 1,
      sourceCellId: 'production-gce-c7',
      sourceCellIncarnation: cellIncarnation
    }
    expect(
      (await postPath(app, '/v1/admin/regional-rehome-trust-probe', 'monitor-token', body)).status
    ).toBe(401)
    expect(
      (
        await postPath(app, '/v1/admin/regional-rehome-trust-probe', 'deploy-token', {
          ...body,
          unexpected: true
        })
      ).status
    ).toBe(400)
  })

  it('fails closed when the source rejects the dedicated identity', async () => {
    const reserveAwareCellDeploymentStatus = vi.fn().mockResolvedValue({
      admitMode: 'db',
      status: {
        cellUrl: 'https://c7.relay.example.test',
        region: 'us-central1',
        runtime: {
          cellIncarnation,
          ready: true,
          heartbeatFresh: true,
          regionalRehomeProtocol: 1
        }
      }
    })
    const sourceFetch = vi
      .fn<typeof fetch>()
      .mockResolvedValue(Response.json({ error: 'invalid_token' }, { status: 401 }))
    const app = createRelayApp(config({ role: 'director', cellId: 'director' }), {
      store: {} as never,
      assignments: { reserveAwareCellDeploymentStatus } as never,
      drain: vi.fn(),
      regionalRehomeIdentityToken: vi.fn(async () => 'rehome-token'),
      regionalRehomeFetch: sourceFetch,
      ready: vi.fn(async () => true)
    })
    const response = await postPath(app, '/v1/admin/regional-rehome-trust-probe', 'deploy-token', {
      v: 1,
      sourceCellId: 'production-gce-c7',
      sourceCellIncarnation: cellIncarnation
    })

    expect(response.status).toBe(409)
    expect(reserveAwareCellDeploymentStatus).toHaveBeenCalledExactlyOnceWith('production-gce-c7')
    expect(sourceFetch).toHaveBeenCalledOnce()
    expect(await response.json()).toEqual({ error: 'regional_rehome_trust_probe_source_401' })
  })
})

async function post(
  app: ReturnType<typeof createRelayApp>,
  token: string,
  body: unknown
): Promise<Response> {
  return await app.request('/v1/admin/host-drain', {
    method: 'POST',
    headers: {
      authorization: `Bearer ${token}`,
      'content-type': 'application/json'
    },
    body: JSON.stringify(body)
  })
}

async function postPath(
  app: ReturnType<typeof createRelayApp>,
  path: string,
  token: string,
  body: unknown
): Promise<Response> {
  return await app.request(path, {
    method: 'POST',
    headers: {
      authorization: `Bearer ${token}`,
      'content-type': 'application/json'
    },
    body: JSON.stringify(body)
  })
}

describe('cell drain endpoint pacing', () => {
  function appWithDrain(): {
    app: ReturnType<typeof createRelayApp>
    drain: ReturnType<typeof vi.fn>
  } {
    const drain = vi.fn()
    const app = createRelayApp(config(), {
      store: {} as never,
      assignments: {} as never,
      drain,
      cellIncarnation,
      ready: vi.fn(async () => true)
    } as Parameters<typeof createRelayApp>[1])
    return { app, drain }
  }

  it('drains everything at once when the caller asks for no pacing', async () => {
    const { app, drain } = appWithDrain()
    const response = await postPath(app, '/v1/admin/drain', 'deploy-token', { v: 1, graceMs: 0 })
    expect(response.status).toBe(200)
    expect(await response.json()).toEqual({ ok: true, paceWindowMs: 0 })
    expect(drain).toHaveBeenCalledWith(0, { paceWindowMs: 0 })
  })

  it('passes the requested window through and echoes what it accepted', async () => {
    const { app, drain } = appWithDrain()
    const response = await postPath(app, '/v1/admin/drain', 'deploy-token', {
      v: 1,
      graceMs: 0,
      paceWindowMs: 120_000
    })
    expect(response.status).toBe(200)
    expect(await response.json()).toEqual({ ok: true, paceWindowMs: 120_000 })
    expect(drain).toHaveBeenCalledWith(0, { paceWindowMs: 120_000 })
  })

  it('refuses a window that is negative, fractional, or past the cap', async () => {
    for (const paceWindowMs of [-1, 1.5, 1_200_001]) {
      const { app, drain } = appWithDrain()
      const response = await postPath(app, '/v1/admin/drain', 'deploy-token', {
        v: 1,
        graceMs: 0,
        paceWindowMs
      })
      expect(response.status).toBe(400)
      expect(drain).not.toHaveBeenCalled()
    }
  })

  it('accepts the cap itself', async () => {
    const { app, drain } = appWithDrain()
    const response = await postPath(app, '/v1/admin/drain', 'deploy-token', {
      v: 1,
      graceMs: 0,
      paceWindowMs: 1_200_000
    })
    expect(response.status).toBe(200)
    expect(drain).toHaveBeenCalledWith(0, { paceWindowMs: 1_200_000 })
  })

  it('still rejects an unauthenticated pacing request', async () => {
    const { app, drain } = appWithDrain()
    const response = await postPath(app, '/v1/admin/drain', 'wrong-token', {
      v: 1,
      graceMs: 0,
      paceWindowMs: 120_000
    })
    expect(response.status).toBe(401)
    expect(drain).not.toHaveBeenCalled()
  })
})

function config(overrides: Partial<RelayConfig> = {}): RelayConfig {
  return {
    port: 8080,
    publicUrl: 'https://c7.relay.example.test',
    cellUrl: 'https://c7.relay.example.test',
    region: 'us-central1',
    authIssuer: 'https://auth.example.test',
    authAudience: 'orca-relay',
    jwksUrl: 'https://auth.example.test/jwks',
    assignmentSigningKey: new Uint8Array(32),
    role: 'cell',
    cellId: 'production-gce-c7',
    cells: [],
    adminAudience: 'https://relay.example.test/v1/admin/drain',
    deployServiceAccount: '[email protected]',
    rehomeDirectorServiceAccount: '[email protected]',
    rehomeAudience: 'https://relay.example.test/v1/admin/host-drain',
    runtimeServiceAccount: '[email protected]',
    adminJwksUrl: 'https://auth.example.test/jwks',
    databasePoolMax: 10,
    publicAssignmentsEnabled: true,
    publicAssignmentConcurrency: 2,
    publicAssignmentQueueMax: 128,
    publicAssignmentWaitMs: 4_000,
    publicResolveConcurrency: 1,
    publicResolveWaitMs: 5_000,
    publicAssignmentRetryAfterSeconds: 5,
    dataDir: './data',
    ...overrides
  }
}