Open History to see the last commit
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('subagent')).toBe('subagent')
49 })
50
51 it('keeps unmatched tool-result parts', () => {
52 const selected = selectChatUI({
53 messages: [orphanResultMessage],
54 interrupts: [],
55 })
56 const resultPart = selected.messages[0]?.parts[1]
57 expect(resultPart?.key).toBe('toolResult')
58 if (resultPart?.key === 'toolResult') {
59 expect(resultPart.matched).toBe(false)
60 expect(resultPart.part.toolCallId).toBe('missing-call')
61 }
62 })
63
64 it('keeps orphan approvals in the interrupt list', () => {
65 const selected = selectChatUI({
66 messages: [messageWithToolResults],
67 interrupts: [purchaseApprovalInterrupt],
68 inlineToolNames: ['purchaseItem'],
69 })
70 expect(selected.interrupts).toEqual([purchaseApprovalInterrupt])
71 })
72
73 it('attaches list-placement approvals to the call and keeps them in the list', () => {
74 const selected = selectChatUI({
75 messages: [purchaseApprovalMessage],
76 interrupts: [purchaseApprovalInterrupt],
77 inlineToolNames: [],
78 })
79 const call = selected.messages[0]?.parts[0]
80 expect(call?.key).toBe('toolCall')
81 if (call?.key === 'toolCall') {
82 expect(call.interrupt).toBe(purchaseApprovalInterrupt)
83 }
84 expect(selected.interrupts).toEqual([purchaseApprovalInterrupt])
85 })
86
87 it('hides inline approvals from the interrupt list when a call matches', () => {
88 const selected = selectChatUI({
89 messages: [purchaseApprovalMessage],
90 interrupts: [purchaseApprovalInterrupt, genericInterrupt],
91 inlineToolNames: ['purchaseItem'],
92 })
93 expect(selected.interrupts).toEqual([genericInterrupt])
94 })
95
96 it('treats mapped tools as inline unless the interrupt map puts them in the list', () => {
97 expect(
98 collectInlineToolNames(undefined, ['purchaseItem', 'getWeather']),
99 ).toEqual(['purchaseItem', 'getWeather'])
100 expect(collectInlineToolNames({}, ['purchaseItem'])).toEqual([
101 'purchaseItem',
102 ])
103 expect(
104 collectInlineToolNames({ purchaseItem: {} }, ['purchaseItem']),
105 ).toEqual([])
106 })
107
108 it('preserves empty arrays', () => {
109 const selected = selectChatUI({ messages: [], interrupts: [] })
110 expect(selected.messages).toEqual([])
111 expect(selected.interrupts).toEqual([])
112 })
113
114 it('preserves message and part order across every part key', () => {
115 const selected = selectChatUI({
116 messages: [
117 {
118 id: 'ordered',
119 role: 'assistant',
120 parts: [
121 { type: 'thinking', content: 'hmm' },
122 { type: 'text', content: 'hi' },
123 {
124 type: 'image',
125 source: { type: 'url', value: 'https://example.com/a.png' },
126 },
127 {
128 type: 'audio',
129 source: { type: 'url', value: 'https://example.com/a.wav' },
130 },
131 {
132 type: 'video',
133 source: { type: 'url', value: 'https://example.com/a.mp4' },
134 },
135 {
136 type: 'document',
137 source: { type: 'url', value: 'https://example.com/a.pdf' },
138 },
139 {
140 type: 'tool-call',
141 id: 'call-a',
142 name: 'getWeather',
143 arguments: '{}',
144 state: 'awaiting-input',
145 },
146 {
147 type: 'tool-result',
148 toolCallId: 'other',
149 content: 'orphan',
150 state: 'complete',
151 },
152 {
153 type: 'structured-output',
154 status: 'streaming',
155 raw: '{',
156 },
157 {
158 type: 'ui-resource',
159 resource: {
160 uri: 'ui://widget',
161 mimeType: 'text/html',
162 text: '<p></p>',
163 },
164 toolCallId: 'call-a',
165 toolName: 'getWeather',
166 },
167 ],
168 },
169 ],
170 })
171
172 expect(selected.messages[0]?.parts.map((part) => part.key)).toEqual([
173 'thinking',
174 'text',
175 'image',
176 'audio',
177 'video',
178 'document',
179 'toolCall',
180 'toolResult',
181 'structuredOutput',
182 'uiResource',
183 ])
184 })
185
186 it('keeps unbound and registered generic interrupts in the list', () => {
187 const selected = selectChatUI({
188 messages: [],
189 interrupts: [genericInterrupt, unboundInterrupt],
190 })
191 expect(selected.interrupts).toEqual([genericInterrupt, unboundInterrupt])
192 })
193})
194
195describe('resolveInterruptComponent', () => {
196 const Plan = { id: 'plan' }
197 const Fallback = { id: 'fallback' }
198 const Purchase = { id: 'purchase' }
199 const map = {
200 tools: { purchaseItem: Purchase },
201 generic: {
202 choosePlan: Plan,
203 fallback: Fallback,
204 },
205 }
206
207 it('resolves a registered generic interrupt from generic[id]', () => {
208 expect(resolveInterruptComponent(genericInterrupt, map)).toBe(Plan)
209 })
210
211 it('resolves unbound through generic.fallback', () => {
212 expect(resolveInterruptComponent(unboundInterrupt, map)).toBe(Fallback)
213 })
214
215 it('resolves a tool approval from tools[name]', () => {
216 expect(resolveInterruptComponent(approvalInterrupt, map)).toBe(Purchase)
217 })
218})