Open History to see the last commit
1import { afterEach, describe, expect, it } from 'vitest'
2import { createElement } from 'remix/ui'
3import { createChatHook } from '../src/chat-ui/create-chat-hook.ts'
4import { createChatUI } from '../src/chat-ui/create-ui.tsx'
5import type {
6 ChatUIFactoryConfig,
7 ChatUIHost,
8 LayoutProps,
9 MessageProps,
10} from '../src/chat-ui/create-ui.tsx'
11import type { Handle, RemixNode } from 'remix/ui'
12import type { UIMessage } from '../src/types.ts'
13
14const weatherMessage: UIMessage = {
15 id: 'message-1',
16 role: 'assistant',
17 parts: [
18 {
19 type: 'tool-call',
20 id: 'call-weather',
21 name: 'getWeather',
22 arguments: '{"city":"London"}',
23 input: { city: 'London' },
24 state: 'complete',
25 },
26 ],
27}
28
29const chatOptions = {
30 tools: [{ name: 'getWeather' as const }],
31}
32
33function getWeather(handle: Handle<{ part: { input?: { city?: string } } }>) {
34 return () => createElement('strong', {}, handle.props.part.input?.city)
35}
36
37const kit = {
38 components: {
39 layout(handle: Handle<LayoutProps<typeof chatOptions>>) {
40 return () => {
41 const { Messages } = handle.props
42 return createElement(Messages)
43 }
44 },
45 message(handle: Handle<MessageProps<typeof chatOptions>>) {
46 return () => {
47 const { Parts } = handle.props
48 return createElement(Parts)
49 }
50 },
51 },
52 partsComponents: {
53 fallback() {
54 return () => null
55 },
56 },
57 toolsComponents: {
58 getWeather,
59 },
60} as ChatUIFactoryConfig<typeof chatOptions>
61
62function host(messages: Array<UIMessage>): ChatUIHost<typeof chatOptions> {
63 return {
64 messages,
65 interrupts: [],
66 queue: [],
67 cancelQueued() {},
68 } as unknown as ChatUIHost<typeof chatOptions>
69}
70
71describe('createChatHook', () => {
72 it('returns createAppChat, ui, and useChatContext', () => {
73 const { createAppChat, ui, useChatContext } = createChatHook({
74 options: chatOptions,
75 ...kit,
76 })
77 expect(createAppChat).toBeTypeOf('function')
78 expect(ui.Chat).toBeTypeOf('function')
79 expect(useChatContext).toBeTypeOf('function')
80 })
81})
82
83describe('createChatUI', () => {
84 let cleanup: (() => void) | undefined
85
86 afterEach(() => {
87 cleanup?.()
88 cleanup = undefined
89 })
90
91 it('renders a getWeather tool component with the city text', async () => {
92 const UI = createChatUI(chatOptions, kit)
93
94 let render:
95 | ((node: RemixNode) => { container: HTMLElement; cleanup: () => void })
96 | undefined
97 try {
98 ;({ render } = await import('remix/ui/test'))
99 } catch {
100 render = undefined
101 }
102
103 const result = render
104 ? render(
105 createElement(UI.Chat, {
106 chat: host([weatherMessage]),
107 }),
108 )
109 : undefined
110
111 if (result) {
112 cleanup = result.cleanup
113 expect(result.container.textContent).toContain('London')
114 return
115 }
116
117 expect(getWeather).toBeTypeOf('function')
118 const node = getWeather({
119 props: {
120 part: weatherMessage.parts[0],
121 },
122 } as unknown as Handle<{ part: { input?: { city?: string } } }>)()
123 expect(readText(node)).toContain('London')
124 })
125})
126
127function readText(node: RemixNode): string {
128 if (node == null || typeof node === 'boolean') return ''
129 if (typeof node === 'string' || typeof node === 'number') return String(node)
130 if (Array.isArray(node)) return node.map(readText).join('')
131 if (typeof node === 'object' && 'props' in node) {
132 return readText(node.props.children as RemixNode)
133 }
134 return ''
135}