Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

3.3 KB
Raw Blame
import { describe, expect, test } from 'vitest'
import { SandboxInfo } from 'e2b'

import {
  buildTableRows,
  sortSandboxes,
} from '../../../src/commands/sandbox/list'

function sandbox(sandboxId: string, startedAt: Date): SandboxInfo {
  return {
    sandboxId,
    templateId: 'template-id',
    metadata: {},
    startedAt,
    endAt: new Date(startedAt.getTime() + 60_000),
    state: 'running',
    cpuCount: 2,
    memoryMB: 512,
    envdVersion: '0.2.0',
  }
}

describe('sandbox list table rows', () => {
  test('sorts by timestamp, not by locale-formatted date string', () => {
    // In en-US, "9/1/2026, ..." > "10/1/2026, ..." lexicographically,
    // so string sorting would put September after October.
    const september = sandbox('sbx-sep', new Date('2026-09-01T10:00:00Z'))
    const october = sandbox('sbx-oct', new Date('2026-10-01T09:00:00Z'))

    const rows = buildTableRows([october, september])

    expect(rows.map((r) => r.sandboxId)).toEqual(['sbx-sep', 'sbx-oct'])
  })

  test('breaks ties on identical timestamps by sandbox ID', () => {
    const startedAt = new Date('2026-09-01T10:00:00Z')
    const rows = buildTableRows([
      sandbox('sbx-b', startedAt),
      sandbox('sbx-a', startedAt),
    ])

    expect(rows.map((r) => r.sandboxId)).toEqual(['sbx-a', 'sbx-b'])
  })

  test('sorts descending when order is desc', () => {
    const september = sandbox('sbx-sep', new Date('2026-09-01T10:00:00Z'))
    const october = sandbox('sbx-oct', new Date('2026-10-01T09:00:00Z'))

    const rows = buildTableRows([september, october], 'desc')

    expect(rows.map((r) => r.sandboxId)).toEqual(['sbx-oct', 'sbx-sep'])
  })

  test('formats dates and state for display', () => {
    const startedAt = new Date('2026-09-01T10:00:00Z')
    const [row] = buildTableRows([sandbox('sbx-1', startedAt)])

    expect(row.startedAt).toBe(startedAt.toLocaleString())
    expect(row.state).toBe('Running')
    expect(row.metadata).toBe('{}')
  })

  test('does not mutate the input array', () => {
    const september = sandbox('sbx-sep', new Date('2026-09-01T10:00:00Z'))
    const october = sandbox('sbx-oct', new Date('2026-10-01T09:00:00Z'))
    const input = [october, september]

    buildTableRows(input)

    expect(input.map((s) => s.sandboxId)).toEqual(['sbx-oct', 'sbx-sep'])
  })
})

describe('sandbox list json sorting', () => {
  test('sorts ascending by default', () => {
    const september = sandbox('sbx-sep', new Date('2026-09-01T10:00:00Z'))
    const october = sandbox('sbx-oct', new Date('2026-10-01T09:00:00Z'))

    const sorted = sortSandboxes([october, september])

    expect(sorted.map((s) => s.sandboxId)).toEqual(['sbx-sep', 'sbx-oct'])
  })

  test('sorts descending when order is desc', () => {
    const september = sandbox('sbx-sep', new Date('2026-09-01T10:00:00Z'))
    const october = sandbox('sbx-oct', new Date('2026-10-01T09:00:00Z'))

    const sorted = sortSandboxes([september, october], 'desc')

    expect(sorted.map((s) => s.sandboxId)).toEqual(['sbx-oct', 'sbx-sep'])
  })

  test('keeps original SandboxInfo values (no display formatting)', () => {
    const startedAt = new Date('2026-09-01T10:00:00Z')
    const [item] = sortSandboxes([sandbox('sbx-1', startedAt)])

    expect(item.startedAt).toEqual(startedAt)
    expect(item.state).toBe('running')
  })
})