Skip to content

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

3.3 KB
Raw Blame
import { readFileSync } from 'node:fs'
import { join, resolve } from 'node:path'

// Webpack resolves these emitted .js names to their TypeScript sources.
jest.mock('./app.module.js', () => jest.requireActual('./app.module'), { virtual: true })
jest.mock('./app.service.js', () => jest.requireActual('./app.service'), { virtual: true })
jest.mock('./config/typed-config.service.js', () => jest.requireActual('./config/typed-config.service'), {
  virtual: true,
})
jest.mock(
  './organization/services/organization.service.js',
  () => jest.requireActual('./organization/services/organization.service'),
  { virtual: true },
)
jest.mock(
  './notification/gateways/notification.gateway.js',
  () => jest.requireActual('./notification/gateways/notification.gateway'),
  { virtual: true },
)
jest.mock('./cloud-app/engine/sqlguard.js', () => jest.requireActual('./cloud-app/engine/sqlguard'), { virtual: true })
jest.mock('./generate-openapi.js', () => jest.requireActual('./generate-openapi'), { virtual: true })
// The ESM proxy transport is idle during the disconnected bootstrap.
jest.mock('http-proxy-middleware', () => ({
  createProxyMiddleware: () => () => undefined,
  fixRequestBody: () => undefined,
}))

describe('disconnected production bootstrap', () => {
  test.each([false, true])(
    'checks the API with OpenAPI generation=%s',
    async (openapi) => {
      const cwd = process.cwd()
      const env = process.env
      const argv = process.argv
      const onExit: Array<() => void> = []
      const originalOn = process.on.bind(process)
      process.chdir(resolve(__dirname, '../..'))
      process.argv = [
        'node',
        'bootstrap-check',
        ...(openapi ? [resolve(__dirname, '../.env.example'), '--openapi'] : []),
      ]
      const exitCode = new Promise<number>((done) => {
        jest.spyOn(process, 'exit').mockImplementation(((code: number) => {
          done(code)
        }) as never)
      })
      jest.spyOn(process, 'on').mockImplementation((name, listener) => {
        if (name === 'exit') {
          onExit.push(listener as () => void)
          return process
        }
        return originalOn(name, listener)
      })
      try {
        await jest.isolateModulesAsync(async () => {
          // BoxController subscribes in its constructor, and Nest quits lazy clients on shutdown.
          const { Redis } = await import('ioredis')
          jest.spyOn(Redis.prototype, 'subscribe').mockResolvedValue(1)
          jest.spyOn(Redis.prototype, 'quit').mockResolvedValue('OK')
          require('./bootstrap-check')
          expect(await exitCode).toBe(0)
        })
        if (openapi) {
          const spec = JSON.parse(readFileSync(join(process.cwd(), 'dist/apps/api/openapi.3.1.0.json'), 'utf8'))
          expect(spec.openapi).toBe('3.1.0')
          expect(spec.paths['/box/paginated'].get).toBeDefined()
          expect(spec.webhooks['box.created'].post.requestBody.content['application/json'].schema.$ref).toBe(
            '#/components/schemas/BoxCreatedWebhook',
          )
          expect(spec.components.schemas.BoxCreatedWebhook).toBeDefined()
        }
      } finally {
        process.chdir(cwd)
        onExit.forEach((close) => close())
        process.env = env
        process.argv = argv
        jest.restoreAllMocks()
      }
    },
    35_000,
  )
})