Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

7.8 KB
Raw Blame
/*
 * Copyright 2025 BoxLite AI
 * SPDX-License-Identifier: AGPL-3.0
 */

import { EventEmitter } from 'events'
import { HttpStatus, RequestTimeoutException } from '@nestjs/common'
import { BoxProgressPhase } from '../enums/box-progress-phase.enum'
import { BoxState } from '../enums/box-state.enum'
import { BoxPreparingImageTimeoutError } from '../errors/box-preparing-image-timeout.error'
import { BoxReadinessError } from '../errors/box-readiness.error'
import { BoxStateWaiterService } from './box-state-waiter.service'
import { BOX_EVENT_CHANNEL } from '../../common/constants/constants'
import { NotificationService } from '../../notification/services/notification.service'
import { BoxDesiredState } from '../enums/box-desired-state.enum'

function makeWaiter(initialState = BoxState.STARTING, toBoxDto?: (box: any) => Promise<any>) {
  const subscriber = new EventEmitter() as any
  subscriber.subscribe = jest.fn().mockResolvedValue(1)
  subscriber.quit = jest.fn().mockResolvedValue(undefined)
  const redis = { duplicate: jest.fn(() => subscriber) } as any
  const boxService = {
    findOneByIdOrName: jest.fn().mockResolvedValue({ id: 'box-1', state: initialState }),
    toBoxDto: jest.fn(toBoxDto ?? (async (box: any) => box)),
  } as any
  return { waiter: new BoxStateWaiterService(boxService, redis), subscriber, boxService }
}

describe('BoxStateWaiterService', () => {
  it('resolves concurrent waiters for the same box from one state event', async () => {
    const { waiter, subscriber } = makeWaiter()
    const first = waiter.waitForStarted('box-1', 'org-1', 5)
    const second = waiter.waitForStarted('box-1', 'org-1', 5)
    await new Promise((resolve) => setImmediate(resolve))

    subscriber.emit('message', BOX_EVENT_CHANNEL, JSON.stringify({ box: { id: 'box-1', state: BoxState.STARTED } }))

    await expect(Promise.all([first, second])).resolves.toEqual([
      { id: 'box-1', state: BoxState.STARTED },
      { id: 'box-1', state: BoxState.STARTED },
    ])
  })

  it('returns the failed Box identity and a stable error code', async () => {
    const { waiter, boxService } = makeWaiter(BoxState.ERROR)
    boxService.findOneByIdOrName.mockResolvedValue({
      id: 'box-1',
      state: BoxState.ERROR,
      errorReason: 'runner has not declared filestore',
    })

    const error = await waiter.waitForStarted('box-1', 'org-1', 1).catch((failure) => failure)

    expect(error).toBeInstanceOf(BoxReadinessError)
    expect(error.getStatus()).toBe(400)
    expect(error.getResponse()).toMatchObject({
      message: 'Box state transition failed: runner has not declared filestore',
      code: 'BOX_STATE_TRANSITION_FAILED',
      boxId: 'box-1',
    })
  })

  it.each(['state', 'desired state'])('observes the %s notification before dashboard enrichment', async (kind) => {
    const { waiter, subscriber } = makeWaiter()
    const onStarted = jest.fn()
    const pending = waiter.waitForStarted('box-1', 'org-1', 5, onStarted)
    await new Promise((resolve) => setImmediate(resolve))

    let release!: () => void
    const metadata = new Promise<void>((resolve) => {
      release = resolve
    })
    const box = { id: 'box-1', state: BoxState.STARTED } as never
    const emitter = { emitBoxStateUpdated: jest.fn(), emitBoxDesiredStateUpdated: jest.fn() }
    const notifications = new NotificationService(
      emitter as never,
      {} as never,
      {
        toBoxDto: async () => {
          await metadata
          return { lastActivityAt: 'enriched' }
        },
      } as never,
      { publish: async (channel: string, message: string) => subscriber.emit('message', channel, message) } as never,
    )
    const dispatch =
      kind === 'state'
        ? notifications.handleBoxStateUpdated({ box, oldState: BoxState.STARTING, newState: BoxState.STARTED })
        : notifications.handleBoxDesiredStateUpdated({
            box,
            oldDesiredState: BoxDesiredState.STOPPED,
            newDesiredState: BoxDesiredState.STARTED,
          })
    const notify = kind === 'state' ? emitter.emitBoxStateUpdated : emitter.emitBoxDesiredStateUpdated
    try {
      expect(onStarted).toHaveBeenCalledTimes(1)
      expect(notify).not.toHaveBeenCalled()
    } finally {
      release()
      await dispatch
      await pending
    }
    expect(notify.mock.calls[0][0]).toEqual({ lastActivityAt: 'enriched' })
  })

  it('waits through checkpoint import and the stopped state until the Box starts', async () => {
    const { waiter, subscriber, boxService } = makeWaiter(BoxState.RESTORING)
    const started = jest.fn()
    const pending = waiter.waitForStarted('box-1', 'org-1', 5).then(started)
    await new Promise((resolve) => setImmediate(resolve))

    const emit = (state: BoxState) =>
      subscriber.emit('message', BOX_EVENT_CHANNEL, JSON.stringify({ box: { id: 'box-1', state } }))
    emit(BoxState.STOPPED)
    await new Promise((resolve) => setImmediate(resolve))
    expect(boxService.toBoxDto).not.toHaveBeenCalled()
    emit(BoxState.STARTED)
    await pending
    expect(started).toHaveBeenCalledWith({ id: 'box-1', state: BoxState.STARTED })
  })

  it('rejects instead of returning a non-target state on timeout', async () => {
    jest.useFakeTimers()
    const { waiter } = makeWaiter()
    const pending = expect(waiter.waitForStarted('box-1', 'org-1', 1)).rejects.toThrow(
      'Timed out waiting for box box-1 to reach started',
    )
    await Promise.resolve()
    await jest.advanceTimersByTimeAsync(1000)

    await pending
    jest.useRealTimers()
  })

  /**
   * The wait is bounded here, not by the box: the pull carries on after the
   * request is answered. A caller told only "timed out" would read that as a
   * stuck box and retry straight into the same wait.
   */
  it('says what the box was waiting on when the wait ends mid-pull', async () => {
    jest.useFakeTimers()
    const progress = { phase: BoxProgressPhase.PREPARING_IMAGE, retryAfterMs: 5000 }
    const { waiter } = makeWaiter(BoxState.CREATING, async (box: any) => ({ ...box, progress }))
    const pending = waiter.waitForStarted('box-1', 'org-1', 1).catch((error) => error)
    await Promise.resolve()
    await jest.advanceTimersByTimeAsync(1000)

    const error = await pending
    expect(error).toBeInstanceOf(BoxPreparingImageTimeoutError)
    expect(error.getStatus()).toBe(HttpStatus.REQUEST_TIMEOUT)
    expect(error.getResponse()).toEqual({
      message: 'Timed out waiting for box box-1 to reach started',
      progress,
      boxId: 'box-1',
    })
    expect(error.retryAfterSeconds).toBe(5)
    jest.useRealTimers()
  })

  it('keeps the plain timeout when the box was not waiting on an image', async () => {
    jest.useFakeTimers()
    const { waiter } = makeWaiter(BoxState.STARTING)
    const pending = waiter.waitForStarted('box-1', 'org-1', 1).catch((error) => error)
    await Promise.resolve()
    await jest.advanceTimersByTimeAsync(1000)

    const error = await pending
    expect(error).toBeInstanceOf(RequestTimeoutException)
    expect(error).not.toBeInstanceOf(BoxPreparingImageTimeoutError)
    expect(error.getResponse()).toMatchObject({ boxId: 'box-1' })
    jest.useRealTimers()
  })

  /**
   * The read that enriches the failure is best-effort. Losing it must not
   * replace a timeout the caller can act on with a database error it cannot.
   */
  it('still times out when it cannot read what the box was waiting on', async () => {
    jest.useFakeTimers()
    const { waiter } = makeWaiter(BoxState.CREATING, async () => {
      throw new Error('catalog unavailable')
    })
    const pending = waiter.waitForStarted('box-1', 'org-1', 1).catch((error) => error)
    await Promise.resolve()
    await jest.advanceTimersByTimeAsync(1000)

    const error = await pending
    expect(error).toBeInstanceOf(RequestTimeoutException)
    expect(error.message).toBe('Timed out waiting for box box-1 to reach started')
    expect(error.getResponse()).toMatchObject({ boxId: 'box-1' })
    jest.useRealTimers()
  })
})