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

teardown.test.ts

58 lines1.9 KB

1/**
2 * A teardown step that never settles must not hold back the end of the run
3 * (issue #1638). The server's `dispose()` and the bridge's `close()` both
4 * hang here; the stream must still end.
5 */
6import { describe, expect, it, vi } from 'vitest'
7import {
8 provideSandbox,
9 provideToolBridgeProvisioner,
10} from '@tanstack/ai-sandbox'
11import { resolveDebugOption } from '@tanstack/ai/adapter-internals'
12import { opencodeText } from '../src/index'
13import type { CapabilityContext } from '@tanstack/ai'
14import type { SandboxHandle } from '@tanstack/ai-sandbox'
15​
16const never = () => new Promise<void>(() => {})
17​
18vi.mock('../src/process/sandbox-server', () => ({
19 startOpencodeServerInSandbox: () =>
20 Promise.resolve({ baseUrl: 'http://127.0.0.1:1', dispose: never }),
21}))
22vi.mock('../src/process/server', () => ({
23 startOpencodeSession: () => Promise.reject(new Error('no session')),
24}))
25​
26const MODEL = 'anthropic/claude-sonnet-4-5'
27​
28describe('opencode teardown', () => {
29 it('ends the stream when dispose() and close() never settle', async () => {
30 const capabilities = {
31 capabilities: { markProvided: () => {}, has: () => true },
32 } as unknown as CapabilityContext
33 provideSandbox(capabilities, {
34 id: 'sbx',
35 provider: 'mock',
36 } as unknown as SandboxHandle)
37 provideToolBridgeProvisioner(capabilities, {
38 provision: () =>
39 Promise.resolve({
40 name: 'tanstack',
41 url: 'http://127.0.0.1:1/mcp',
42 token: 'token',
43 close: never,
44 }),
45 })
46 const types: Array<string> = []
47 for await (const chunk of opencodeText(MODEL).chatStream({
48 model: MODEL,
49 messages: [{ role: 'user', content: 'hi' }],
50 logger: resolveDebugOption(false),
51 capabilities,
52 tools: [{ name: 'getWeather', description: 'weather' }],
53 })) {
54 types.push(chunk.type)
55 }
56 expect(types).toContain('RUN_ERROR')
57 })
58})