Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

live.test.ts

127 lines4.4 KB

1/**
2 * Live suite against real E2B sandboxes. Runs only with `E2B_API_KEY`; every
3 * sandbox it creates is killed in `afterAll`.
4 */
5import { afterAll, beforeAll, describe, expect, it } from 'vitest'
6import { Sandbox } from 'e2b'
7import { toolDefinition } from '@tanstack/ai'
8import { createCodeMode } from '@tanstack/ai-code-mode'
9import { z } from 'zod'
10import { createE2BIsolateDriver } from '../src/index'
11​
12const apiKey = process.env.E2B_API_KEY
13​
14/** Processes of this driver carry the runner path in their command line. */
15async function runnerProcesses(sandbox: Sandbox): Promise<string> {
16 // The bracket stops pgrep from matching its own command line.
17 const ps = await sandbox.commands.run(
18 'pgrep -af "[t]anstack-code-mode" || true',
19 )
20 return ps.stdout.trim()
22​
23describe.skipIf(!apiKey)('e2b isolate driver (live)', () => {
24 let sandbox: Sandbox
25​
26 beforeAll(async () => {
27 sandbox = await Sandbox.create({ timeoutMs: 5 * 60_000 })
28 }, 60_000)
29​
30 afterAll(async () => {
31 await sandbox?.kill()
32 })
33​
34 it('runs Code Mode end to end with a host tool', async () => {
35 let calls = 0
36 const add = toolDefinition({
37 name: 'add',
38 description: 'Add two numbers.',
39 inputSchema: z.object({ x: z.number(), y: z.number() }),
40 }).server(async ({ x, y }) => {
41 calls++
42 return x + y
43 })
44 const { tool } = createCodeMode({
45 driver: createE2BIsolateDriver({ sandbox }),
46 tools: [add],
47 })
48 const result = await tool.execute!({
49 typescriptCode: `const a: number = await external_add({ x: 1, y: 2 })
50 console.log('a is', a)
51 const b: number = await external_add({ x: a, y: 10 })
52 return b * 10`,
53 })
54 expect(result).toMatchObject({
55 success: true,
56 result: 130,
57 logs: ['a is 3'],
58 })
59 expect(calls).toBe(2)
60 }, 60_000)
61​
62 it('kills the process in the sandbox on timeout and leaves no file behind', async () => {
63 const context = await createE2BIsolateDriver({ sandbox }).createContext({
64 bindings: {},
65 timeout: 3000,
66 })
67 const result = await context.execute('while (true) {}')
68 expect(result.error?.name).toBe('TimeoutError')
69 expect(await runnerProcesses(sandbox)).toBe('')
70 const leftovers = await sandbox.commands.run(
71 'ls /tmp | grep tanstack-code-mode || true',
72 )
73 expect(leftovers.stdout.trim()).toBe('')
74 }, 60_000)
75​
76 it('kills processes the code spawned once the execution returns', async () => {
77 const context = await createE2BIsolateDriver({ sandbox }).createContext({
78 bindings: {},
79 })
80 const result = await context.execute(
81 `process.mainModule.require('node:child_process')
82 .spawn('sleep', ['600'], { stdio: 'ignore' })
83 return 'spawned'`,
84 )
85 expect(result.value).toBe('spawned')
86 await context.dispose()
87 const ps = await sandbox.commands.run('pgrep -af "[s]leep 600" || true')
88 expect(ps.stdout.trim()).toBe('')
89 }, 60_000)
90​
91 it('enforces the memory limit', async () => {
92 const context = await createE2BIsolateDriver({ sandbox }).createContext({
93 bindings: {},
94 memoryLimit: 32,
95 timeout: 20_000,
96 })
97 const result = await context.execute(
98 'const a = []; for (;;) a.push(new Array(1e6).fill(1))',
99 )
100 expect(result.error?.name).toBe('MemoryLimitError')
101 }, 60_000)
102​
103 it('disposing one context leaves another execution in the same sandbox running', async () => {
104 const driver = createE2BIsolateDriver({ sandbox })
105 const first = await driver.createContext({ bindings: {} })
106 const second = await driver.createContext({ bindings: {} })
107 const hung = first.execute('await new Promise(() => {})')
108 const slow = second.execute(
109 "await new Promise((r) => setTimeout(r, 3000)); return 'second'",
110 )
111 await new Promise((r) => setTimeout(r, 1500))
112 await first.dispose()
113 expect((await hung).error?.name).toBe('DisposedError')
114 expect(await slow).toMatchObject({ success: true, value: 'second' })
115 expect(await runnerProcesses(sandbox)).toBe('')
116 }, 60_000)
117​
118 it('reports a killed sandbox as unavailable', async () => {
119 const gone = await Sandbox.create({ timeoutMs: 60_000 })
120 await gone.kill()
121 const context = await createE2BIsolateDriver({
122 sandbox: gone,
123 }).createContext({ bindings: {} })
124 const result = await context.execute('return 1')
125 expect(result.error?.name).toBe('E2BSandboxUnavailableError')
126 }, 60_000)
127})