Skip to content

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

7.1 KB
Raw Blame
import { buildSpec, classifyMigrations, diffSpecs, onlineChecks, specToManifest } from './spec'
import { MAX_SNAPSHOT_BYTES } from './platform'

type Manifest = ReturnType<typeof specToManifest>

const manifest = (fields: Partial<Manifest> = {}): Manifest => ({
  app: 'reports',
  environment: 'main',
  runtime: { image: 'node', size: 'small', build: [], output: null },
  services: [{ name: 'web', command: 'node index.js', port: 3000 }],
  schedules: [],
  database: null,
  volumes: [],
  auth: null,
  domains: { builtin: 'reports', custom: [] },
  access: 'private',
  public_paths: [],
  variables: {},
  secrets: {},
  ...fields,
})
const applied = [{ file: '0001_reports.sql', sha256: 'abc', deployment: 1 }]
const assigned = {
  box_id: 'box',
  box_url: 'https://box.example.test',
  db_branch: 'main',
  db_host: 'db.example.test',
  app_url: 'https://reports.example.test',
}
function snapshot(m: Manifest, accessOverride: string | null = null) {
  return buildSpec({ manifest: m, version: 2, deployment: 3, baseVersion: 1, applied, assigned, accessOverride })
}

describe('hosted App deployment specs', () => {
  it('restores the manifest fields from a live spec, including the effective online access setting', () => {
    const m = manifest({ database: { migrations: 'migrations/' }, access: 'private' })
    const live = snapshot(m, 'org')
    expect(live).toMatchObject({
      version: 2,
      base_version: 1,
      deployment: 3,
      database: { applied },
      assigned,
      access: 'org',
    })
    expect(specToManifest(live)).toEqual({ ...m, access: 'org' })
    const old = { ...live }
    Reflect.deleteProperty(old, 'public_paths')
    expect(specToManifest(old).public_paths).toEqual([])
    expect(diffSpecs(live, m, { accessOverride: 'org' })).toEqual([
      { kind: 'online_only', path: 'access', from: 'private', to: 'org' },
    ])
    expect(diffSpecs(live, { ...m, access: 'org' }, { accessOverride: 'org' })).toEqual([])
  })

  it('reports named additions, removals and field edits and separates first deploy from an update', () => {
    const previous = manifest({
      services: [
        { name: 'web', command: 'node old.js', port: 3000 },
        { name: 'queue', command: 'node jobs.js' },
      ],
      database: { migrations: 'old/' },
      variables: { KEEP: 'unchanged', OLD: 'remove', LOG_LEVEL: 'debug' },
      secrets: { CACHE: 'app/cache' },
    })
    const next = manifest({
      services: [{ name: 'web', static: 'dist/' }],
      schedules: [{ name: 'refresh', cron: '0 * * * *', target: 'web:/refresh' }],
      database: { migrations: 'sql/' },
      auth: { providers: ['github'], protect: ['/account'] },
      domains: { ...previous.domains, custom: ['reports.example.test'] },
      access: 'public',
      public_paths: ['/webhooks/*'],
      variables: { KEEP: 'unchanged', NEW: 'add', LOG_LEVEL: 'info' },
    })
    const changes = diffSpecs(snapshot(previous), next)
    expect(changes).toEqual(
      expect.arrayContaining([
        { kind: '-', path: 'service.queue', from: previous.services[1] },
        { kind: '~', path: 'service.web.command', from: 'node old.js', to: null },
        { kind: '~', path: 'service.web.static', from: null, to: 'dist/' },
        { kind: '+', path: 'schedule.refresh', to: next.schedules[0] },
        { kind: '~', path: 'database.migrations', from: 'old/', to: 'sql/' },
        { kind: '+', path: 'auth', from: null, to: next.auth },
        { kind: '~', path: 'domains.custom', from: [], to: ['reports.example.test'] },
        { kind: '~', path: 'access', from: 'private', to: 'public' },
        { kind: '~', path: 'public_paths', from: [], to: ['/webhooks/*'] },
        { kind: '-', path: 'variables.OLD', from: 'remove' },
        { kind: '+', path: 'variables.NEW', to: 'add' },
        { kind: '~', path: 'variables.LOG_LEVEL', from: 'debug', to: 'info' },
        { kind: '-', path: 'secrets.CACHE', from: 'app/cache' },
      ]),
    )
    expect(changes.map((entry) => entry.path)).not.toContain('variables.KEEP')
    expect(diffSpecs(undefined, next)).toContainEqual({ kind: '+', path: 'database', to: 'database for main' })
    expect(diffSpecs(snapshot(next), manifest(), { envName: 'preview' })).toEqual(
      expect.arrayContaining([
        { kind: '-', path: 'database', from: 'database for preview' },
        { kind: '-', path: 'auth', from: next.auth, to: null },
      ]),
    )
    const auth = { providers: ['email' as const], protect: ['/account'] }
    expect(diffSpecs(snapshot(next), { ...next, auth })).toEqual([
      { kind: '~', path: 'auth', from: next.auth, to: auth },
    ])
  })

  it('classifies pending, unchanged, and edited migrations in execution order', () => {
    const files = [
      { file: '0003_add.sql', sha256: 'new' },
      { file: '0002_edit.sql', sha256: 'edited' },
      { file: '0000_first.sql', sha256: 'first' },
      ...applied,
    ]
    expect(classifyMigrations(files, [...applied, { file: '0002_edit.sql', sha256: 'original' }])).toEqual({
      pending: ['0000_first.sql', '0003_add.sql'],
      changed: ['0002_edit.sql'],
      applied: ['0001_reports.sql'],
    })
  })

  it('validates platform-dependent limits, secret ownership and sign-in configuration', async () => {
    const m = manifest({
      secrets: { APP_KEY: 'app/api', ORG_KEY: 'org/team', READY: 'app/configured' },
      auth: { providers: ['github', 'email', 'google'], protect: ['/account'] },
      volumes: [
        { name: 'shared', mount: '/data', from: 'main', mode: 'ro', path: null },
        { name: 'reports', mount: '/reports', from: null, mode: 'ro', path: null },
      ],
      domains: { builtin: 'reports', custom: ['reports.example.test'] },
    })
    const platform = {
      runtimeImages: { base: 'base:1' },
      appName: 'other',
      envName: 'main',
      secretExists: async (ref: string) => ref === 'app/configured',
      uploadBytes: MAX_SNAPSHOT_BYTES + 1,
      configuredProviders: ['google'],
    }
    const result = await onlineChecks(m, platform)
    expect(result.errors.map((e) => e.rule)).toEqual([
      'app_mismatch',
      'image_not_allowed',
      'not_supported_yet',
      'not_supported_yet',
      'not_supported_yet',
      'secret_not_found',
      'secret_not_found',
      'upload_too_large',
    ])
    expect(result.errors.map((e) => e.message)).toEqual(
      expect.arrayContaining([
        expect.stringContaining('no such App secret'),
        expect.stringContaining('no such org secret'),
        expect.stringContaining('bringing your own domain is not available'),
      ]),
    )
    expect(result.warnings.map((w) => w.message)).toEqual([
      expect.stringContaining('github: not configured'),
      expect.stringContaining('no mail sender'),
    ])
    const onlineDomains = await onlineChecks(m, { ...platform, customDomains: true })
    expect(onlineDomains.errors.map((e) => e.message)).toContainEqual(
      expect.stringContaining('boxlite domain bind HOST'),
    )
    expect(
      await onlineChecks(manifest(), {
        ...platform,
        appName: m.app,
        runtimeImages: { node: 'node:1' },
        uploadBytes: MAX_SNAPSHOT_BYTES,
      }),
    ).toEqual({ errors: [], warnings: [] })
  })
})