Open History to see the last commit
1// Regression coverage for the shipping default. The suite-wide setup file
2// (`use-real-devtools-bridges.ts`) re-routes the no-op devtools factories to
3// the real bridges, so no other test exercises the bridge production
4// consumers actually get when they don't opt into devtools. Unmock here so
5// `ChatClient` runs against the actual no-op bridge that ships as the
6// default, instead of the real-bridge substitute the setup file installs.
7import { afterEach, describe, expect, it, vi } from 'vitest'
8import { aiEventClient } from '@tanstack/ai-event-client'
9import { ChatClient } from '../src/chat-client'
10import { createChatDevtoolsBridge } from '../src/devtools'
11import { createMockConnectionAdapter, createTextChunks } from './test-utils'
12
13vi.unmock('../src/devtools-noop')
14
15describe('createChatDevtoolsBridge by NODE_ENV', () => {
16 afterEach(() => {
17 vi.unstubAllEnvs()
18 vi.restoreAllMocks()
19 })
20
21 async function streamWithRealFactory() {
22 const emit = vi.spyOn(aiEventClient, 'emit')
23 const client = new ChatClient({
24 connection: createMockConnectionAdapter({
25 chunks: createTextChunks('Hi there'),
26 }),
27 devtoolsBridgeFactory: createChatDevtoolsBridge,
28 })
29 await client.sendMessage('hello')
30 return emit
31 }
32
33 it('emits no devtools events in production', async () => {
34 vi.stubEnv('NODE_ENV', 'production')
35 expect(await streamWithRealFactory()).not.toHaveBeenCalled()
36 })
37
38 it('emits devtools events in development', async () => {
39 vi.stubEnv('NODE_ENV', 'development')
40 expect(await streamWithRealFactory()).toHaveBeenCalled()
41 })
42
43 it('does not throw when there is no process global', () => {
44 vi.stubGlobal('process', undefined)
45 try {
46 expect(
47 () =>
48 new ChatClient({
49 connection: createMockConnectionAdapter(),
50 devtoolsBridgeFactory: createChatDevtoolsBridge,
51 }),
52 ).not.toThrow()
53 } finally {
54 vi.unstubAllGlobals()
55 }
56 })
57})
58
59describe('ChatClient with default no-op devtools bridge', () => {
60 it('sends the first message and appends it', async () => {
61 const adapter = createMockConnectionAdapter({
62 chunks: createTextChunks('Hi there'),
63 })
64 const client = new ChatClient({ connection: adapter })
65
66 await client.sendMessage('hello')
67
68 const messages = client.getMessages()
69 expect(messages.at(0)?.role).toBe('user')
70 expect(messages.at(0)?.parts).toEqual([{ type: 'text', content: 'hello' }])
71 expect(messages.at(1)?.role).toBe('assistant')
72 })
73
74 it('updates tools without throwing', () => {
75 const adapter = createMockConnectionAdapter()
76 const client = new ChatClient({ connection: adapter })
77
78 expect(() => client.updateOptions({ tools: [] })).not.toThrow()
79 })
80})