Skip to content

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

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

import 'reflect-metadata'
import { once } from 'node:events'
import { createServer, type Server as HttpServer } from 'node:http'
import type { AddressInfo } from 'node:net'
import { IoAdapter } from '@nestjs/platform-socket.io'
// The package root does not re-export the decorator's metadata key.
import { GATEWAY_OPTIONS } from '@nestjs/websockets/constants'
import type { Server as SocketIOServer } from 'socket.io'
import { WebSocket, WebSocketServer } from 'ws'
import { NotificationGateway } from './notification.gateway'

// `main.ts` routes exec attach upgrades on the API's HTTP server to the ws
// proxy, which writes nothing until the Box is ready and the runner answers.
describe('NotificationGateway on the shared HTTP server', () => {
  let http: HttpServer
  let io: SocketIOServer | undefined
  let attach: WebSocketServer | undefined
  let client: WebSocket | undefined

  afterEach(async () => {
    const closed = once(http, 'close')
    client?.terminate()
    attach?.close()
    io?.close()
    await closed
  })

  it('leaves a slow upgrade on another path to its handler', async () => {
    http = createServer()
    io = new IoAdapter(http).create(0, Reflect.getMetadata(GATEWAY_OPTIONS, NotificationGateway))
    attach = new WebSocketServer({ noServer: true })
    http.on('upgrade', (req, socket, head) => {
      setTimeout(() => {
        if (!socket.destroyed) attach?.handleUpgrade(req, socket, head, () => undefined)
      }, 1500)
    })
    http.listen(0, '127.0.0.1')
    await once(http, 'listening')

    client = new WebSocket(
      `ws://127.0.0.1:${(http.address() as AddressInfo).port}/api/v1/org-1/boxes/box-1/executions/exec-1/attach`,
      { handshakeTimeout: 4000 },
    )
    await expect(once(client, 'open')).resolves.toEqual([])
  })
})