Open History to see the last commit
1// @vitest-environment jsdom
2import { render } from '@testing-library/react'
3import { expect, it } from 'vitest'
4import type { ComponentType } from 'react'
5import { createChatUI } from '../../src/chat-ui/create-ui'
6import type {
7 ChatUIHost,
8 MessageProps,
9 SubagentProps,
10} from '../../src/chat-ui/create-ui'
11import {
12 chatOptions,
13 createChatResult,
14 messageWithToolResults,
15} from '../../../ai-client/tests/ui-fixtures'
16
17function host(
18 init?: Parameters<typeof createChatResult>[0],
19): ChatUIHost<typeof chatOptions> {
20 return createChatResult(init ?? {}) as unknown as ChatUIHost<
21 typeof chatOptions
22 >
23}
24
25// `Parts` closes over the message, so the naive implementation builds a new
26// component on every render and React remounts the whole part subtree — which
27// during streaming means once per token. Identity must be constant instead.
28it('hands `message` the same `Parts` component across re-renders', () => {
29 const seen: Array<MessageProps<typeof chatOptions>['Parts']> = []
30
31 const UI = createChatUI(chatOptions, {
32 components: {
33 layout: ({ Messages }) => <Messages />,
34 message: ({ Parts }) => {
35 seen.push(Parts)
36 return (
37 <article>
38 <Parts />
39 </article>
40 )
41 },
42 },
43 partsComponents: { fallback: ({ part }) => <span>{part.type}</span> },
44 toolsComponents: {
45 getWeather: ({ part }) => <strong>{part.input?.city}</strong>,
46 purchaseItem: () => null,
47 },
48 interruptsComponents: {
49 generic: { choosePlan: () => null, fallback: () => null },
50 },
51 })
52
53 const { rerender } = render(
54 <UI.Chat chat={host({ messages: [messageWithToolResults] })} />,
55 )
56
57 // A new message object with the same id is what a stream chunk looks like.
58 rerender(
59 <UI.Chat chat={host({ messages: [{ ...messageWithToolResults }] })} />,
60 )
61
62 expect(seen.length).toBeGreaterThan(1)
63 expect(new Set(seen).size).toBe(1)
64})
65
66it('keeps SubagentMessages identity across re-renders', () => {
67 const seen: Array<ComponentType> = []
68 const handle = {
69 id: 'sub-1',
70 name: 'researcher',
71 status: 'running' as const,
72 messages: [
73 {
74 id: 'child-1',
75 role: 'assistant' as const,
76 parts: [{ type: 'text' as const, content: 'Notes' }],
77 },
78 ],
79 }
80 const message = {
81 id: 'parent-1',
82 role: 'assistant' as const,
83 parts: [{ type: 'subagent' as const, subagent: handle }],
84 }
85
86 const UI = createChatUI(chatOptions, {
87 components: {
88 layout: ({ Messages }) => <Messages />,
89 message: ({ Parts }) => (
90 <article>
91 <Parts />
92 </article>
93 ),
94 },
95 partsComponents: {
96 fallback: ({ part }) => <span>{part.type}</span>,
97 },
98 subagentsComponents: {
99 researcher: ({ Parts }: SubagentProps<typeof chatOptions>) => {
100 seen.push(Parts)
101 return <Parts />
102 },
103 },
104 toolsComponents: {
105 getWeather: () => null,
106 purchaseItem: () => null,
107 },
108 interruptsComponents: {
109 generic: { choosePlan: () => null, fallback: () => null },
110 },
111 })
112
113 const { rerender } = render(<UI.Chat chat={host({ messages: [message] })} />)
114 rerender(
115 <UI.Chat
116 chat={host({
117 messages: [
118 {
119 ...message,
120 parts: [{ type: 'subagent', subagent: handle }],
121 },
122 ],
123 })}
124 />,
125 )
126
127 expect(seen.length).toBeGreaterThan(1)
128 expect(new Set(seen).size).toBe(1)
129})
130
131it('does not re-render Subagents rows when only nested messages change', () => {
132 let listRenders = 0
133 const handle = {
134 id: 'sub-1',
135 name: 'writer',
136 status: 'running' as const,
137 messages: [
138 {
139 id: 'child-1',
140 role: 'assistant' as const,
141 parts: [{ type: 'text' as const, content: 'Draft' }],
142 },
143 ],
144 }
145
146 const UI = createChatUI(chatOptions, {
147 components: {
148 layout: ({ Subagents }) => <Subagents />,
149 message: ({ Parts }) => (
150 <article>
151 <Parts />
152 </article>
153 ),
154 },
155 subagentsComponents: {
156 writer: ({ Parts }: SubagentProps<typeof chatOptions>) => {
157 listRenders += 1
158 return (
159 <section>
160 <Parts />
161 </section>
162 )
163 },
164 },
165 partsComponents: {
166 text: ({ part }) => <span>{part.content}</span>,
167 fallback: ({ part }) => <span>{part.type}</span>,
168 },
169 toolsComponents: {
170 getWeather: () => null,
171 purchaseItem: () => null,
172 },
173 interruptsComponents: {
174 generic: { choosePlan: () => null, fallback: () => null },
175 },
176 })
177
178 const { rerender, getByText } = render(
179 <UI.Chat chat={host({ subagents: [handle] })} />,
180 )
181 getByText('Draft')
182 const first = listRenders
183 rerender(
184 <UI.Chat
185 chat={host({
186 subagents: [
187 {
188 ...handle,
189 messages: [
190 {
191 id: 'child-1',
192 role: 'assistant',
193 parts: [{ type: 'text', content: 'Draft v2' }],
194 },
195 ],
196 },
197 ],
198 })}
199 />,
200 )
201 expect(listRenders).toBe(first)
202 // The row did not render again, but the child text still updated.
203 getByText('Draft v2')
204})