Skip to content

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

2.5 KB
Raw Blame
import { BadRequestException, ForbiddenException, NotFoundException } from '@nestjs/common'
import { managementHttp } from '../../../test/cloud-app/management'
import type { Ctx } from './ctx'
import { UpstreamError } from './errors'

describe('hosted App HTTP error responses', () => {
  afterEach(() => jest.restoreAllMocks())
  const listing = (error: Error) =>
    managementHttp({
      config: {},
      store: {
        apps: async () => {
          throw error
        },
      },
    } as unknown as Ctx)('/v1/apps')

  test.each([
    [new NotFoundException('organization gone'), 404, 'not_found', 'organization gone'],
    [new ForbiddenException('App hosting disabled'), 403, 'forbidden', 'App hosting disabled'],
    [
      new BadRequestException({ message: ['invalid name', 'size missing'] }),
      400,
      'invalid_request',
      'invalid name; size missing',
    ],
    [
      new BadRequestException({ code: 'quota_reached', message: 'quota reached', next: 'remove an unused Box' }),
      400,
      'quota_reached',
      'quota reached',
    ],
  ] as const)(
    'passes through the platform status and public error details for %p',
    async (exception, status, code, message) => {
      const result = await listing(exception)
      expect(result.status).toBe(status)
      expect(await result.json()).toEqual({
        error: {
          code,
          message,
          next: code === 'quota_reached' ? 'remove an unused Box' : 'check the request and organization permissions',
        },
      })
      expect(result.headers.has('x-request-id')).toBe(false)
    },
  )

  test.each([
    [
      new UpstreamError('box service', 403, 'provider credential refused', 'upstream diagnostic details'),
      503,
      'unavailable',
    ],
    [new Error('private storage diagnostic'), 500, 'internal'],
  ] as const)(
    'exposes an incident ID and keeps the diagnostic in the operator log',
    async (exception, status, code) => {
      const log = jest.spyOn(console, 'error').mockImplementation(() => undefined)
      const result = await listing(exception)
      expect(result.status).toBe(status)
      const id = result.headers.get('x-request-id')
      expect(id).toMatch(/^req_[0-9a-f]{16}$/)
      const body = await result.json()
      expect(body).toMatchObject({ error: { request_id: id, code, next: expect.stringContaining(id!) } })
      expect(JSON.stringify(body)).not.toContain(exception.message)
      expect(log.mock.calls[0][0]).toContain(id)
      expect(log.mock.calls[0][0]).toContain('GET /v1/apps')
    },
  )
})