Skip to content

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

3.8 KB
Raw Blame
/*
 * Copyright 2026 BoxLite AI
 * SPDX-License-Identifier: AGPL-3.0
 */

import { BoxDestroyAction } from './box-destroy.action'
import { BoxState } from '../../enums/box-state.enum'
import { BoxDesiredState } from '../../enums/box-desired-state.enum'
import { RunnerState } from '../../enums/runner-state.enum'
import { DONT_SYNC_AGAIN, SYNC_AGAIN } from './box.action'

function makeAction() {
  const runner = { id: 'runner-1', state: RunnerState.READY, apiVersion: '2' }
  const adapter = {
    boxInfo: jest.fn().mockResolvedValue({ state: BoxState.STOPPED }),
    destroyBox: jest.fn().mockResolvedValue(undefined),
  }
  const runnerService = { findOneOrFail: jest.fn().mockResolvedValue(runner) }
  const runnerAdapterFactory = { create: jest.fn().mockResolvedValue(adapter) }
  const action = new BoxDestroyAction(runnerService as never, runnerAdapterFactory as never, {} as never, {} as never)
  const updateBoxState = jest.spyOn(action as any, 'updateBoxState').mockResolvedValue(undefined)
  return { action, runner, adapter, runnerService, runnerAdapterFactory, updateBoxState }
}

function destroyableBox(state = BoxState.STOPPED) {
  return {
    id: 'box-1',
    runnerId: 'runner-1',
    state,
    desiredState: BoxDesiredState.DESTROYED,
    pending: true,
  } as never
}

describe('BoxDestroyAction', () => {
  it('returns the apiVersion of the Runner it already read', async () => {
    const { action } = makeAction()

    await expect(action.run(destroyableBox(), {} as never)).resolves.toEqual({
      syncState: SYNC_AGAIN,
      runnerApiVersion: '2',
    })
  })

  it('does not read a Runner for an already destroyed Box', async () => {
    const { action, runnerService, runnerAdapterFactory } = makeAction()

    await expect(action.run(destroyableBox(BoxState.DESTROYED), {} as never)).resolves.toEqual({
      syncState: DONT_SYNC_AGAIN,
    })
    expect(runnerService.findOneOrFail).not.toHaveBeenCalled()
    expect(runnerAdapterFactory.create).not.toHaveBeenCalled()
  })

  it('finishes an archived Box without reading a Runner', async () => {
    const { action, runnerService, updateBoxState } = makeAction()
    const box = destroyableBox(BoxState.ARCHIVED)
    const lockCode = {} as never

    await expect(action.run(box, lockCode)).resolves.toEqual({ syncState: DONT_SYNC_AGAIN })
    expect(updateBoxState).toHaveBeenCalledWith(box, BoxState.DESTROYED, lockCode)
    expect(runnerService.findOneOrFail).not.toHaveBeenCalled()
  })

  it('stops synchronization when the Runner is not ready', async () => {
    const { action, runner, runnerAdapterFactory } = makeAction()
    runner.state = RunnerState.DISABLED

    await expect(action.run(destroyableBox(), {} as never)).resolves.toEqual({
      syncState: DONT_SYNC_AGAIN,
      runnerApiVersion: '2',
    })
    expect(runnerAdapterFactory.create).not.toHaveBeenCalled()
  })

  it('finishes when the Runner reports the Box as destroyed', async () => {
    const { action, adapter, updateBoxState } = makeAction()
    adapter.boxInfo.mockResolvedValue({ state: BoxState.DESTROYED })
    const box = destroyableBox()
    const lockCode = {} as never

    await expect(action.run(box, lockCode)).resolves.toEqual({
      syncState: DONT_SYNC_AGAIN,
      runnerApiVersion: '2',
    })
    expect(updateBoxState).toHaveBeenCalledWith(box, BoxState.DESTROYED, lockCode)
  })

  it('finishes when the Runner no longer has the Box', async () => {
    const { action, adapter, updateBoxState } = makeAction()
    adapter.boxInfo.mockRejectedValue({ response: { status: 404 } })
    const box = destroyableBox()
    const lockCode = {} as never

    await expect(action.run(box, lockCode)).resolves.toEqual({
      syncState: DONT_SYNC_AGAIN,
      runnerApiVersion: '2',
    })
    expect(updateBoxState).toHaveBeenCalledWith(box, BoxState.DESTROYED, lockCode)
  })
})