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

ui-selectors.test.ts

248 lines7.1 KB

1import { describe, expect, it } from 'vitest'
2import {
3 collectInlineToolNames,
4 partTypeToKey,
5 resolveInterruptComponent,
6 selectChatUI,
7} from '../src/ui'
8import {
9 approvalInterrupt,
10 genericInterrupt,
11 messageWithToolResults,
12 orphanResultMessage,
13 purchaseApprovalInterrupt,
14 purchaseApprovalMessage,
15 unboundInterrupt,
16} from './ui-fixtures'
17​
18describe('selectChatUI', () => {
19 it('matches results and partitions interrupts', () => {
20 const selected = selectChatUI({
21 messages: [messageWithToolResults],
22 interrupts: [approvalInterrupt, genericInterrupt],
23 inlineToolNames: ['purchaseItem'],
24 })
25​
26 const parts = selected.messages[0]?.parts
27 expect(parts?.map((part) => part.key)).toEqual([
28 'toolCall',
29 'toolResult',
30 'structuredOutput',
31 ])
32​
33 const call = parts?.[0]
34 expect(call?.key).toBe('toolCall')
35 if (call?.key === 'toolCall') {
36 expect(call.result?.toolCallId).toBe(call.part.id)
37 expect(call.interrupt).toBe(approvalInterrupt)
38 }
39​
40 expect(selected.interrupts).toEqual([genericInterrupt])
41 })
42​
43 it('uses camel-case component keys', () => {
44 expect(partTypeToKey('tool-call')).toBe('toolCall')
45 expect(partTypeToKey('tool-result')).toBe('toolResult')
46 expect(partTypeToKey('structured-output')).toBe('structuredOutput')
47 expect(partTypeToKey('ui-resource')).toBe('uiResource')
48 expect(partTypeToKey('activity')).toBe('activity')
49 expect(partTypeToKey('subagent')).toBe('subagent')
50 })
51​
52 it('exposes activity parts on activity messages', () => {
53 const selected = selectChatUI({
54 messages: [
55 {
56 id: 'act-1',
57 role: 'activity',
58 parts: [
59 {
60 type: 'activity',
61 activityType: 'SEARCH',
62 content: { query: 'tanstack' },
63 },
64 ],
65 },
66 ],
67 })
68 expect(selected.messages[0]?.message.role).toBe('activity')
69 expect(selected.messages[0]?.parts).toEqual([
70 {
71 key: 'activity',
72 part: {
73 type: 'activity',
74 activityType: 'SEARCH',
75 content: { query: 'tanstack' },
76 },
77 },
78 ])
79 })
80​
81 it('keeps unmatched tool-result parts', () => {
82 const selected = selectChatUI({
83 messages: [orphanResultMessage],
84 interrupts: [],
85 })
86 const resultPart = selected.messages[0]?.parts[1]
87 expect(resultPart?.key).toBe('toolResult')
88 if (resultPart?.key === 'toolResult') {
89 expect(resultPart.matched).toBe(false)
90 expect(resultPart.part.toolCallId).toBe('missing-call')
91 }
92 })
93​
94 it('keeps orphan approvals in the interrupt list', () => {
95 const selected = selectChatUI({
96 messages: [messageWithToolResults],
97 interrupts: [purchaseApprovalInterrupt],
98 inlineToolNames: ['purchaseItem'],
99 })
100 expect(selected.interrupts).toEqual([purchaseApprovalInterrupt])
101 })
102​
103 it('attaches list-placement approvals to the call and keeps them in the list', () => {
104 const selected = selectChatUI({
105 messages: [purchaseApprovalMessage],
106 interrupts: [purchaseApprovalInterrupt],
107 inlineToolNames: [],
108 })
109 const call = selected.messages[0]?.parts[0]
110 expect(call?.key).toBe('toolCall')
111 if (call?.key === 'toolCall') {
112 expect(call.interrupt).toBe(purchaseApprovalInterrupt)
113 }
114 expect(selected.interrupts).toEqual([purchaseApprovalInterrupt])
115 })
116​
117 it('hides inline approvals from the interrupt list when a call matches', () => {
118 const selected = selectChatUI({
119 messages: [purchaseApprovalMessage],
120 interrupts: [purchaseApprovalInterrupt, genericInterrupt],
121 inlineToolNames: ['purchaseItem'],
122 })
123 expect(selected.interrupts).toEqual([genericInterrupt])
124 })
125​
126 it('treats mapped tools as inline unless the interrupt map puts them in the list', () => {
127 expect(
128 collectInlineToolNames(undefined, ['purchaseItem', 'getWeather']),
129 ).toEqual(['purchaseItem', 'getWeather'])
130 expect(collectInlineToolNames({}, ['purchaseItem'])).toEqual([
131 'purchaseItem',
132 ])
133 expect(
134 collectInlineToolNames({ purchaseItem: {} }, ['purchaseItem']),
135 ).toEqual([])
136 })
137​
138 it('preserves empty arrays', () => {
139 const selected = selectChatUI({ messages: [], interrupts: [] })
140 expect(selected.messages).toEqual([])
141 expect(selected.interrupts).toEqual([])
142 })
143​
144 it('preserves message and part order across every part key', () => {
145 const selected = selectChatUI({
146 messages: [
147 {
148 id: 'ordered',
149 role: 'assistant',
150 parts: [
151 { type: 'thinking', content: 'hmm' },
152 { type: 'text', content: 'hi' },
153 {
154 type: 'image',
155 source: { type: 'url', value: 'https://example.com/a.png' },
156 },
157 {
158 type: 'audio',
159 source: { type: 'url', value: 'https://example.com/a.wav' },
160 },
161 {
162 type: 'video',
163 source: { type: 'url', value: 'https://example.com/a.mp4' },
164 },
165 {
166 type: 'document',
167 source: { type: 'url', value: 'https://example.com/a.pdf' },
168 },
169 {
170 type: 'tool-call',
171 id: 'call-a',
172 name: 'getWeather',
173 arguments: '{}',
174 state: 'awaiting-input',
175 },
176 {
177 type: 'tool-result',
178 toolCallId: 'other',
179 content: 'orphan',
180 state: 'complete',
181 },
182 {
183 type: 'structured-output',
184 status: 'streaming',
185 raw: '{',
186 },
187 {
188 type: 'ui-resource',
189 resource: {
190 uri: 'ui://widget',
191 mimeType: 'text/html',
192 text: '<p></p>',
193 },
194 toolCallId: 'call-a',
195 toolName: 'getWeather',
196 },
197 ],
198 },
199 ],
200 })
201​
202 expect(selected.messages[0]?.parts.map((part) => part.key)).toEqual([
203 'thinking',
204 'text',
205 'image',
206 'audio',
207 'video',
208 'document',
209 'toolCall',
210 'toolResult',
211 'structuredOutput',
212 'uiResource',
213 ])
214 })
215​
216 it('keeps unbound and registered generic interrupts in the list', () => {
217 const selected = selectChatUI({
218 messages: [],
219 interrupts: [genericInterrupt, unboundInterrupt],
220 })
221 expect(selected.interrupts).toEqual([genericInterrupt, unboundInterrupt])
222 })
223})
224​
225describe('resolveInterruptComponent', () => {
226 const Plan = { id: 'plan' }
227 const Fallback = { id: 'fallback' }
228 const Purchase = { id: 'purchase' }
229 const map = {
230 tools: { purchaseItem: Purchase },
231 generic: {
232 choosePlan: Plan,
233 fallback: Fallback,
234 },
235 }
236​
237 it('resolves a registered generic interrupt from generic[id]', () => {
238 expect(resolveInterruptComponent(genericInterrupt, map)).toBe(Plan)
239 })
240​
241 it('resolves unbound through generic.fallback', () => {
242 expect(resolveInterruptComponent(unboundInterrupt, map)).toBe(Fallback)
243 })
244​
245 it('resolves a tool approval from tools[name]', () => {
246 expect(resolveInterruptComponent(approvalInterrupt, map)).toBe(Purchase)
247 })
248})