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

chat-client-noop-devtools.test.ts

82 lines2.8 KB

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 threadId: 'thread-1',
25 connection: createMockConnectionAdapter({
26 chunks: createTextChunks('Hi there'),
27 }),
28 devtoolsBridgeFactory: createChatDevtoolsBridge,
29 })
30 await client.sendMessage('hello')
31 return emit
32 }
33​
34 it('emits no devtools events in production', async () => {
35 vi.stubEnv('NODE_ENV', 'production')
36 expect(await streamWithRealFactory()).not.toHaveBeenCalled()
37 })
38​
39 it('emits devtools events in development', async () => {
40 vi.stubEnv('NODE_ENV', 'development')
41 expect(await streamWithRealFactory()).toHaveBeenCalled()
42 })
43​
44 it('does not throw when there is no process global', () => {
45 vi.stubGlobal('process', undefined)
46 try {
47 expect(
48 () =>
49 new ChatClient({
50 threadId: 'thread-1',
51 connection: createMockConnectionAdapter(),
52 devtoolsBridgeFactory: createChatDevtoolsBridge,
53 }),
54 ).not.toThrow()
55 } finally {
56 vi.unstubAllGlobals()
57 }
58 })
59})
60​
61describe('ChatClient with default no-op devtools bridge', () => {
62 it('sends the first message and appends it', async () => {
63 const adapter = createMockConnectionAdapter({
64 chunks: createTextChunks('Hi there'),
65 })
66 const client = new ChatClient({ threadId: 'thread-1', connection: adapter })
67​
68 await client.sendMessage('hello')
69​
70 const messages = client.getMessages()
71 expect(messages.at(0)?.role).toBe('user')
72 expect(messages.at(0)?.parts).toEqual([{ type: 'text', content: 'hello' }])
73 expect(messages.at(1)?.role).toBe('assistant')
74 })
75​
76 it('updates tools without throwing', () => {
77 const adapter = createMockConnectionAdapter()
78 const client = new ChatClient({ threadId: 'thread-1', connection: adapter })
79​
80 expect(() => client.updateOptions({ tools: [] })).not.toThrow()
81 })
82})