The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
import { deleteApp, purgeApp, restoreApp, startPurger } from './lifecycle'
import type { Ctx } from './ctx'
import type { AppRow } from './db'
it('destroys every App Box before deleting a volume shared between its environments', async () => {
const app = { id: 'app', name: 'demo' } as AppRow
const consumers = new Set(['box-main', 'box-worker', 'box-staging'])
const removeVolume = jest.fn(async () => {
if (consumers.size) throw new Error('Volume is in use')
})
const ctx = {
store: {
envs: async () => [
{ id: 'main', active_box_id: 'box-main' },
{ id: 'staging', active_box_id: 'box-staging' },
],
deploymentsWhere: async () => [],
tx: async (work) => work(),
run: async () => undefined,
get: async () => app,
appById: async () => app,
},
withPurgeLock: async (_id, work) => work(new AbortController().signal),
boxlite: {
deleteBox: async (id) => {
consumers.delete(id)
},
},
boxes: { list: async () => ['box-main', 'box-worker', 'box-staging'] },
resources: { deleteVolumes: removeVolume },
domains: { releaseEnvironment: async () => undefined },
blobs: { deleteApp: async () => undefined },
} as unknown as Ctx
await purgeApp(ctx, app)
expect(removeVolume).toHaveBeenCalledTimes(2)
expect(consumers.size).toBe(0)
})
describe('App retention', () => {
afterEach(() => {
jest.restoreAllMocks()
jest.useRealTimers()
})
test('keeps the original deadline on repeated deletion and restores only during retention', async () => {
jest.useFakeTimers({ now: new Date('2026-03-01T00:00:00Z') })
const app = { id: 'app', name: 'demo', org_id: 'org', deleted_at: null, purge_after: null } as AppRow
const recordActivity = jest.fn()
const diagnostic = jest.spyOn(console, 'error').mockImplementation(() => undefined)
const sleep = jest.fn(async () => undefined).mockRejectedValueOnce(new Error('unreachable'))
const ctx = {
store: {
tx: async (work) => work(),
get: async () => app,
envs: async () => [],
recordActivity,
run: async (sql: string, deletedAt: string, purgeAfter: string) => {
if (sql.includes('SET deleted_at = NULL')) {
if (Date.parse(app.purge_after!) <= Date.now()) return { changes: 0 }
Object.assign(app, { deleted_at: null, purge_after: null })
} else Object.assign(app, { deleted_at: deletedAt, purge_after: purgeAfter })
return { changes: 1 }
},
},
boxes: { list: async () => ['a', 'b'] },
resources: { sleep },
} as unknown as Ctx
const first = await deleteApp(ctx, app, 'owner')
expect(first.purge_after).toBe('2026-03-08T00:00:00.000Z')
jest.advanceTimersByTime(24 * 3600_000)
expect(await deleteApp(ctx, app, 'owner')).toEqual(first)
expect(sleep.mock.calls).toEqual([['a'], ['b']])
expect(diagnostic).toHaveBeenCalledWith('[lifecycle] stop a: unreachable')
await restoreApp(ctx, app, 'owner')
expect(app.deleted_at).toBeNull()
await restoreApp(ctx, app, 'owner')
expect(recordActivity.mock.calls.map((call) => call[3])).toEqual(['app.delete', 'app.restore'])
await deleteApp(ctx, app, 'owner')
jest.advanceTimersByTime(7 * 24 * 3600_000)
await expect(restoreApp(ctx, app, 'owner')).rejects.toThrow('retention has ended')
expect(app.deleted_at).not.toBeNull()
})
test('scheduled purges skip restored candidates and retry failed cleanup on the next interval', async () => {
jest.useFakeTimers()
jest.spyOn(console, 'error').mockImplementation(() => undefined)
const app = {
id: 'app',
name: 'demo',
deleted_at: '2025-01-01',
purge_after: '2025-01-08',
neon_project_id: 'project',
} as AppRow
const restored = { ...app, id: 'restored', name: 'restored', deleted_at: null, purge_after: null }
const deleteProject = jest
.fn()
.mockRejectedValueOnce(new Error('provider unavailable'))
.mockResolvedValue(undefined)
const removeSource = jest.fn()
const run = jest.fn()
const scan = jest.fn(async () => [app, restored])
const ctx = {
store: {
all: scan,
tx: async (work) => work(),
get: async (_sql, id) => (id === 'app' ? app : restored),
envs: async () => [],
run,
},
boxes: { list: async () => [] },
neon: { deleteProject },
blobs: { deleteApp: removeSource },
withPurgeLock: (_id, work) => work(new AbortController().signal),
} as unknown as Ctx
const stop = startPurger(ctx)
await jest.advanceTimersByTimeAsync(10 * 60_000)
expect(removeSource).not.toHaveBeenCalled()
expect(deleteProject).toHaveBeenCalledTimes(1)
await jest.advanceTimersByTimeAsync(10 * 60_000)
expect(deleteProject).toHaveBeenCalledTimes(2)
expect(removeSource.mock.calls).toEqual([['app']])
expect(run).toHaveBeenCalledWith('DELETE FROM db_readers WHERE project_id = ?', 'project')
stop()
await jest.advanceTimersByTimeAsync(10 * 60_000)
expect(scan).toHaveBeenCalledTimes(2)
})
test('a resource lost between listing and locking is skipped by the purger and reported to management', async () => {
const app = { id: 'gone', name: 'removed' } as AppRow
const ctx = {
store: { tx: (work) => work(), get: async () => undefined },
withPurgeLock: (_id, work) => work(new AbortController().signal),
} as unknown as Ctx
await expect(purgeApp(ctx, app, { dueAt: new Date().toISOString() })).resolves.toBe(false)
await expect(purgeApp(ctx, app, { deletedOnly: true })).rejects.toMatchObject({ code: 'not_found_app' })
await expect(deleteApp(ctx, app, 'owner')).rejects.toMatchObject({ code: 'not_found_app' })
await expect(restoreApp(ctx, app, 'owner')).rejects.toMatchObject({ code: 'not_found_app' })
})
test('an active deployment prevents deletion before the first resource is touched', async () => {
const app = { id: 'app', name: 'demo' } as AppRow
const run = jest.fn()
const ctx = {
store: {
tx: async (work) => work(),
get: async () => app,
envs: async () => [{ id: 'env', name: 'main' }],
deploymentsWhere: async () => [{ number: 1 }],
run,
},
} as unknown as Ctx
await expect(deleteApp(ctx, app, 'owner')).rejects.toThrow('deployment in progress')
expect(run).not.toHaveBeenCalled()
})
})