Skip to content

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

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

import { BoxUsageService } from './box-usage.service'

const boxId = 'aB3cD4eF5gH6'
const period = (startAt: string, endAt: string, resources: { cpu?: number; mem?: number; disk?: number } = {}) => ({
  organizationId: 'org-1',
  boxId,
  region: 'us',
  startAt: new Date(startAt),
  endAt: new Date(endAt),
  cpu: 0,
  gpu: 0,
  mem: 0,
  disk: 0,
  ...resources,
})
const read = (periods: unknown[]) =>
  new BoxUsageService({ query: jest.fn().mockResolvedValue(periods) } as never).getUsage(boxId, 'org-1')

describe('BoxUsageService', () => {
  it('answers one line per archived period with the allocation the export bills', async () => {
    expect(
      await read([
        period('2026-11-29T10:00:00.347Z', '2026-11-29T10:20:01.912Z', { cpu: 0.125, mem: 0.125, disk: 10 }),
        period('2026-11-29T10:20:01.912Z', '2026-11-29T15:45:30.004Z', { disk: 10 }),
        period('2026-11-29T15:45:30.004Z', '2026-11-30T00:00:00.000Z', { cpu: 1.375, mem: 1000 / 1024, disk: 10 }),
      ]),
    ).toEqual({
      periods: [
        {
          start_at: '2026-11-29T10:00:00.347Z',
          end_at: '2026-11-29T10:20:01.912Z',
          cpu: 0.125,
          memory_gib: 0.125,
          disk_gib: 10,
        },
        {
          start_at: '2026-11-29T10:20:01.912Z',
          end_at: '2026-11-29T15:45:30.004Z',
          cpu: 0,
          memory_gib: 0,
          disk_gib: 10,
        },
        {
          start_at: '2026-11-29T15:45:30.004Z',
          end_at: '2026-11-30T00:00:00.000Z',
          cpu: 1.375,
          memory_gib: 0.976563,
          disk_gib: 10,
        },
      ],
    })
  })

  it('leaves out a period that lasted no time or that the export would refuse', async () => {
    const kept = period('2026-11-29T10:00:30.000Z', '2026-11-29T10:00:45.000Z', { cpu: 1 })

    expect(
      await read([
        period('2026-11-29T10:00:00.000Z', '2026-11-29T10:00:00.000Z', { cpu: 1 }),
        period('2026-11-29T10:00:00.000Z', '2026-11-29T10:00:15.000Z', { cpu: Number.NaN }),
        { ...period('2026-11-29T10:00:15.000Z', '2026-11-29T10:00:30.000Z', { cpu: 1 }), gpu: -1 },
        kept,
      ]),
    ).toEqual({
      periods: [
        {
          start_at: '2026-11-29T10:00:30.000Z',
          end_at: '2026-11-29T10:00:45.000Z',
          cpu: 1,
          memory_gib: 0,
          disk_gib: 0,
        },
      ],
    })
  })
})