The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
/*
* Copyright 2025 Daytona Platforms Inc.
* Modified by BoxLite AI, 2025-2026
* SPDX-License-Identifier: AGPL-3.0
*/
import { Runner } from '../entities/runner.entity'
import { RunnerState } from '../enums/runner-state.enum'
import { RunnerService } from './runner.service'
describe('RunnerService decommission verification', () => {
const createService = (boxCount: number, checkCount: string | null = null) => {
const runner = { id: 'runner-1', draining: true, state: RunnerState.READY }
const runnerRepository = {
find: jest.fn().mockResolvedValue([runner]),
findOne: jest.fn().mockResolvedValue(runner),
findOneOrFail: jest.fn().mockResolvedValue(runner),
save: jest.fn().mockResolvedValue(runner),
update: jest.fn().mockResolvedValue({ affected: 1 }),
}
const boxRepository = { count: jest.fn().mockResolvedValue(boxCount) }
// Only the singleton cron guards still take a Redis lock; the two runner
// state writers serialise against each other with a FOR UPDATE row lock
// inside `dataSource.transaction`.
const redisLockProvider = {
lock: jest.fn().mockResolvedValue(true),
unlock: jest.fn().mockResolvedValue(undefined),
}
const configService = { getOrThrow: jest.fn().mockReturnValue(1) }
const redis = {
get: jest.fn().mockResolvedValue(checkCount),
set: jest.fn().mockResolvedValue('OK'),
del: jest.fn().mockResolvedValue(1),
}
// Stands in for the locked read + write the two writers do; `count` here is
// the transactional re-count, distinct from boxRepository.count outside it.
const entityManager = {
findOne: jest.fn().mockResolvedValue(runner),
count: jest.fn().mockResolvedValue(boxCount),
update: jest.fn().mockResolvedValue({ affected: 1 }),
save: jest.fn(async (_entity: unknown, value: unknown) => value),
query: jest.fn().mockResolvedValue(undefined),
}
const dataSource = {
queryResultCache: undefined,
transaction: jest.fn(async (cb: (em: typeof entityManager) => Promise<unknown>) => cb(entityManager)),
}
const eventEmitter = { emit: jest.fn() }
const service = new RunnerService(
runnerRepository as any,
{} as any,
boxRepository as any,
redisLockProvider as any,
configService as any,
{} as any,
eventEmitter as any,
dataSource as any,
redis as any,
)
return {
service,
runnerRepository,
boxRepository,
redisLockProvider,
redis,
eventEmitter,
entityManager,
dataSource,
}
}
it('resets verification while any box is still assigned to the draining runner', async () => {
const { service, runnerRepository, boxRepository, redis } = createService(1, '2')
await (service as any).handleCheckDecommissionRunners()
expect(boxRepository.count).toHaveBeenCalledWith({ where: { runnerId: 'runner-1' } })
expect(redis.set).toHaveBeenCalledWith('runner:draining-check:runner-1', '0', 'EX', 600)
expect(runnerRepository.update).not.toHaveBeenCalled()
})
it('decommissions only after three checks with no remaining runner assignments', async () => {
const { service, redis, entityManager } = createService(0, '2')
await (service as any).handleCheckDecommissionRunners()
expect(entityManager.update).toHaveBeenCalledWith(Runner, 'runner-1', { state: RunnerState.DECOMMISSIONED })
expect(redis.del).toHaveBeenCalledWith('runner:draining-check:runner-1')
})
it('does not inspect runners when another worker owns the verification lock', async () => {
const { service, runnerRepository, redisLockProvider } = createService(0)
redisLockProvider.lock.mockResolvedValue(false)
await (service as any).handleCheckDecommissionRunners()
expect(runnerRepository.find).not.toHaveBeenCalled()
expect(redisLockProvider.unlock).not.toHaveBeenCalled()
})
it('clears prior verification progress whenever draining status changes', async () => {
const { service, redis } = createService(0, '2')
await service.updateDrainingStatus('runner-1', false)
expect(redis.del).toHaveBeenCalledWith('runner:draining-check:runner-1')
})
it('does not publish decommission when the transactional re-count still finds a box', async () => {
const { service, redis, entityManager } = createService(0, '2')
entityManager.count.mockResolvedValue(1)
await (service as any).handleCheckDecommissionRunners()
expect(entityManager.update).not.toHaveBeenCalled()
expect(redis.set).toHaveBeenCalledWith('runner:draining-check:runner-1', '0', 'EX', 600)
// The re-count has to happen under the same exclusive lock as the write.
expect(entityManager.findOne).toHaveBeenCalledWith(
Runner,
expect.objectContaining({ lock: { mode: 'pessimistic_write' } }),
)
})
it('does not decommission when draining is cleared before final verification', async () => {
const { service, entityManager } = createService(0, '2')
entityManager.findOne.mockResolvedValue({ id: 'runner-1', draining: false, state: RunnerState.READY })
await (service as any).handleCheckDecommissionRunners()
expect(entityManager.count).not.toHaveBeenCalled()
expect(entityManager.update).not.toHaveBeenCalled()
})
it('updates draining under an exclusive lock on the runner row', async () => {
const { service, entityManager } = createService(0)
await service.updateDrainingStatus('runner-1', false)
expect(entityManager.findOne).toHaveBeenCalledWith(
Runner,
expect.objectContaining({ where: { id: 'runner-1' }, lock: { mode: 'pessimistic_write' } }),
)
expect(entityManager.save).toHaveBeenCalledWith(Runner, expect.objectContaining({ draining: false }))
})
it('keeps a runner decommissioned when an in-flight health write completes later', async () => {
const { service, runnerRepository } = createService(0)
runnerRepository.findOne.mockResolvedValue({ id: 'runner-1', state: RunnerState.READY })
await service.updateRunnerHealth('runner-1')
expect(runnerRepository.update).toHaveBeenCalledWith(
expect.objectContaining({
id: 'runner-1',
state: expect.any(Object),
}),
expect.objectContaining({ state: RunnerState.READY }),
)
})
it('does not emit a stale state event when the guarded health update loses the race', async () => {
const { service, runnerRepository, eventEmitter } = createService(0)
runnerRepository.update.mockResolvedValue({ affected: 0 })
await service.updateRunnerHealth('runner-1')
expect(eventEmitter.emit).not.toHaveBeenCalled()
})
it('waits out the row lock rather than capping it', async () => {
const { service, entityManager } = createService(0, '2')
await (service as any).handleCheckDecommissionRunners()
await service.updateDrainingStatus('runner-1', true)
// No `SET LOCAL lock_timeout`: the only holder either can wait on is the
// other one, for a single read-modify-write. Assignments take no lock on
// the runner row, so no create is queued behind this either way.
expect(entityManager.query).not.toHaveBeenCalled()
})
it('translates a missing uncached runner into NotFoundException', async () => {
const { service, runnerRepository } = createService(0)
runnerRepository.findOne.mockResolvedValue(null)
await expect(service.findOneUncachedOrFail('missing')).rejects.toMatchObject({ status: 404 })
})
})