Skip to content

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

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

import { http, HttpResponse } from 'msw'
import { BOXLITE_APPLET_AGENT_GUIDE_URL } from '@/constants/ExternalLinks'
import { nameProblem } from '@/lib/apps/format'
import type { AppDetail, AppRelease } from '@/lib/apps/types'
import { MOCK_ORGANIZATION_ID } from './fixtures'

function draftApp(name: string): AppDetail {
  const url = `https://app-${crypto.randomUUID().replaceAll('-', '')}.apps.example`
  const server = 'https://api.example.test/api'
  return {
    id: name,
    appId: `app_mock_${name}`,
    name,
    slug: name,
    url,
    visibility: 'private',
    status: 'draft',
    lastDeploy: null,
    resources: { database: false, storage: 0, users: null },
    createdAt: new Date().toISOString(),
    environment: 'main',
    createdFrom: null,
    runtime: null,
    box: null,
    services: [],
    activeRelease: null,
    releases: [],
    activity: [],
    database: null,
    storages: [],
    secrets: [],
    variables: null,
    platformVariables: [],
    problems: [],
    statusDetail: null,
    deploying: null,
    schedules: [],
    liveVariables: null,
    metrics: null,
    preview: null,
    previewAttempt: null,
    handoffPrompt: `Read the Applet agent guide at ${BOXLITE_APPLET_AGENT_GUIDE_URL} before building and deploying ${name} with the BoxLite CLI.
Connect using an organization API key with Apps read and write permissions.
Guide the user through boxlite auth login --url ${server} --method api-key and have them enter the key at the secure prompt.
In the project directory, run BOXLITE_API_URL=${server}/v1/${MOCK_ORGANIZATION_ID} boxlite app init --app ${name} --env main.`,
    accessSource: 'manifest',
    specVersion: null,
    auth: {
      declared: false,
      providers: [],
      protect: [],
      enforced: false,
      methods: [],
      loginUrl: `${url}/__boxlite/auth/login`,
      users: 0,
      note: null,
    },
    domains: { supported: true, target: null, list: [] },
  }
}

export function cloudAppHandlers() {
  const live = draftApp('demo-shop')
  const deployedAt = new Date(Date.now() - 3_600_000).toISOString()
  const release: AppRelease = {
    id: '#1',
    number: 1,
    summary: 'Deploy the storefront',
    createdAt: deployedAt,
    startedAt: deployedAt,
    finishedAt: deployedAt,
    by: 'console',
    status: 'live',
    current: true,
    steps: ['Provision', 'Build', 'Start', 'Health check'].map((name) => ({ name, status: 'done', ms: 1200 })),
    changes: ['+ service.web', '+ database'],
    files: 12,
    bytes: 24576,
    buildSeconds: 8,
    hasSource: false,
    rollback: { possible: false, reason: 'This is the first deployment.' },
    restorePoint: null,
    running: null,
    cause: null,
  }
  Object.assign(live, {
    status: 'live',
    lastDeploy: { releaseId: release.id, at: deployedAt, by: release.by },
    resources: { database: true, storage: 0, users: 0 },
    runtime: { image: 'node:24-slim', size: 'small', cpus: 1, memoryMib: 1024 },
    box: { id: 'mock-box-running', state: 'started' },
    activeRelease: release.id,
    releases: [release],
    services: [
      {
        name: 'web',
        command: ['node', 'server.js'],
        exposed: true,
        port: 3000,
        healthPath: '/health',
        state: 'running',
        health: 'healthy',
        restarts: 0,
        memoryBytes: 52_428_800,
        recentErrors: [],
      },
    ],
    database: {
      status: 'ready',
      engine: 'PostgreSQL',
      host: 'demo.neon.example',
      name: 'neondb',
      user: 'app',
      branch: 'main',
      injectedAs: 'DATABASE_URL',
      injectedInto: ['web'],
    },
    variables: { NODE_ENV: 'production', LOG_LEVEL: 'info' },
    liveVariables: { NODE_ENV: 'production', LOG_LEVEL: 'info' },
    specVersion: 1,
  } satisfies Partial<AppDetail>)
  live.auth.methods = ['github', 'google'].map((provider) => ({
    provider,
    label: provider === 'github' ? 'GitHub' : 'Google',
    status: 'ready',
    declared: false,
    redirectUri: `https://api.example.test/internal/apps/auth/callback/${provider}`,
    keys: 'shared',
    clientId: null,
    managedAvailable: true,
  }))
  const empty = typeof window !== 'undefined' && new URLSearchParams(window.location.search).get('appsPreview') === 'empty'
  const apps = new Map((empty ? [] : [live, draftApp('starter')]).map((app) => [app.id, app]))
  const deleted = new Map<string, { app: AppDetail; deletedAt: string; purgeAfter: string }>()
  const base = `*/v1/${MOCK_ORGANIZATION_ID}/apps`
  const missing = () => HttpResponse.json({ error: { code: 'not_found', message: 'App not found.' } }, { status: 404 })

  return [
    http.get(base, () => HttpResponse.json([...apps.values()].map((app) => ({ ...app, environments: 1 })))),
    http.get(`${base}/deleted`, () =>
      HttpResponse.json(
        [...deleted.values()].map(({ app, deletedAt, purgeAfter }) => ({
          name: app.name,
          deletedAt,
          purgeAfter,
          environments: 1,
        })),
      ),
    ),
    http.delete(`${base}/deleted/:name`, ({ params }) => {
      if (!deleted.delete(String(params.name))) return missing()
      return new HttpResponse(null, { status: 204 })
    }),
    http.post(base, async ({ request }) => {
      const { name } = (await request.json()) as { name: string }
      const problem = typeof name === 'string' ? nameProblem(name) : 'An App name is required.'
      if (problem) return HttpResponse.json({ error: { message: problem } }, { status: 400 })
      if (apps.has(name) || deleted.has(name))
        return HttpResponse.json({ error: { message: 'That App name is taken.' } }, { status: 409 })
      const app = draftApp(name)
      apps.set(app.id, app)
      return HttpResponse.json({ app, prompt: app.handoffPrompt }, { status: 201 })
    }),
    http.delete(`${base}/:app`, ({ params }) => {
      const app = apps.get(String(params.app))
      if (!app) return missing()
      const now = Date.now()
      deleted.set(app.name, {
        app,
        deletedAt: new Date(now).toISOString(),
        purgeAfter: new Date(now + 7 * 86_400_000).toISOString(),
      })
      apps.delete(app.id)
      return new HttpResponse(null, { status: 204 })
    }),
    http.post(`${base}/deleted/:name/restore`, ({ params }) => {
      const retained = deleted.get(String(params.name))
      if (!retained) return missing()
      apps.set(retained.app.id, retained.app)
      deleted.delete(retained.app.name)
      return HttpResponse.json({ ...retained.app, environments: 1 })
    }),
    http.get(`${base}/:app/envs/:env/db/view`, ({ params }) => {
      const app = apps.get(String(params.app))
      if (!app) return missing()
      return HttpResponse.json(
        app.database
          ? {
              ...app.database,
              sizeBytes: 65_536,
              tables: [],
              migrations: [],
              backups: [],
              restoreWindowHours: 24,
              errors: [],
            }
          : { status: 'none' },
      )
    }),
    http.get(`${base}/:app/:section`, ({ params }) => {
      const app = apps.get(String(params.app))
      if (!app) return missing()
      switch (params.section) {
        case 'view':
          return HttpResponse.json(app)
        case 'envs':
          return HttpResponse.json([
            {
              name: 'main',
              status: app.status,
              url: app.url,
              box: app.box?.id ?? null,
              lastDeploy: app.lastDeploy,
              from: null,
            },
          ])
        case 'auth':
          return HttpResponse.json({ status: app.auth, users: [], events: [] })
        case 'logs':
          return HttpResponse.json(
            app.status === 'live'
              ? [
                  {
                    id: 'mock-log-1',
                    ts: deployedAt,
                    source: 'web',
                    level: 'info',
                    line: 'Storefront ready on port 3000',
                    deployment: 1,
                  },
                ]
              : [],
          )
        case 'secrets':
          return HttpResponse.json([])
        case 'handoff':
          return HttpResponse.json({ prompt: app.handoffPrompt })
        default:
          return new HttpResponse(null, { status: 404 })
      }
    }),
    http.get(`${base}/:app/deployments/:number`, ({ params }) => {
      const app = apps.get(String(params.app))
      const release = app?.releases.find((release) => release.number === Number(params.number))
      if (!release) return missing()
      return HttpResponse.json({
        ...release,
        message: release.summary,
        specVersion: 1,
        baseVersion: 0,
        snapshotId: null,
        rollbackTo: null,
        timeline: release.steps.map((step) => ({
          ...step,
          ms: step.ms ?? null,
          detail: null,
          startedAt: release.startedAt,
          finishedAt: release.finishedAt,
        })),
        log: [],
        logTruncated: false,
        changeSet: { infrastructure: release.changes, configuration: [], code: null },
        failure: null,
      })
    }),
    http.all(`${base}/*`, () =>
      HttpResponse.json(
        {
          error: {
            code: 'preview_read_only',
            message: 'This action requires a live Cloud Apps API.',
          },
        },
        { status: 501 },
      ),
    ),
  ]
}