Skip to content

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

3.2 KB
Raw Blame
import type { TestContext } from 'node:test'
import type { RunnerRequest } from '../../stack/runners.ts'
import { gcpRunnerProvider } from '../../stack/providers/gcp/runners.ts'
import { resolveRunnerBinary } from '../../stack/runner-binary.ts'

export function mockRunnerStack(t: TestContext) {
  const output = (value: any): any => ({ value, apply: (fn: (value: any) => any) => output(fn(value)) })
  const secretInputs = new WeakSet<object>()
  const resources = new Map<string, { args: any; options: any; resource: any }>()
  class Resource {
    [key: string]: any
    constructor(name: string, args: any, options: any = {}) {
      resources.set(name, { args, options, resource: this })
      Object.assign(this, args, { id: output(name), name: output(name) })
      if (args.metadataStartupScript) this.instanceId = output(name === 'Runner' ? '12345' : '67890')
      if (name === 'SecretServerRunnerRegistry') {
        const slots: { name: string }[] = JSON.parse(args.stdin.value)
        const entries = slots.map(({ name }, index) => [index === 0 ? '12345' : String(67890 + index - 1), name])
        this.stdout = output(JSON.stringify({ gcp_iit: Object.fromEntries(entries) }))
      }
    }
  }
  const constructors = new Proxy({}, { get: () => Resource })
  const globals = {
    gcp: new Proxy({}, { get: () => constructors }),
    command: { local: constructors },
    $jsonStringify: (input: unknown) => output(JSON.stringify(input, (_, v) => v?.value ?? v)),
    $app: { name: 'boxlite-app', stage: 'dev' },
    $util: {
      output,
      secret: (value: any) => {
        const input = value?.apply ? value : output(value)
        secretInputs.add(input)
        return input
      },
    },
    $resolve: (values: any[]) => output(values.map((value) => value?.value ?? value)),
  }
  for (const [key, value] of Object.entries(globals)) {
    const previous = Object.getOwnPropertyDescriptor(globalThis, key)
    Object.defineProperty(globalThis, key, { configurable: true, value })
    t.after(() =>
      previous ? Object.defineProperty(globalThis, key, previous) : Reflect.deleteProperty(globalThis, key),
    )
  }
  const provider = gcpRunnerProvider({
    project: 'project',
    zone: 'us-east5-a',
    volumePrefix: 'boxlite-volume',
    artifactsBucket: 'artifacts',
    adminApiKey: 'admin',
    regionId: 'region',
    dependsOn: [],
    placement: { cloud: 'gcp', subnetwork: output('subnet'), serviceAccount: output('[email protected]') } as any,
  })
  const requestFor = (count: number): RunnerRequest => ({
    size: 'small',
    rootDiskGb: 100,
    nestedVirtualization: true,
    fleet: Array.from({ length: count }, (_, index) => ({
      slot: {
        resourceName: index ? `Runner-runner-${index + 1}` : 'Runner',
        nameTag: `boxlite-app-dev-runner${index ? `-${index + 1}` : ''}`,
        controlPlaneRunnerName: index ? `runner-${index + 1}` : 'default',
      },
      token: 'test-token',
    })),
    binary: resolveRunnerBinary({ environment: {}, configRoot: '.', readVersion: () => '0.10.8' }),
    apiUrl: 'https://api.dev.example.com',
    otlpUrl: 'http://otel:4317',
    registryProxyHost: null,
    checkpointBucket: null,
    environment: {},
    secrets: {},
  })
  return { output, globals, resources, provider, requestFor, secretInputs }
}