forked from NVIDIA-AI-Blueprints/aiq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatThinking.spec.tsx
More file actions
294 lines (218 loc) · 10.5 KB
/
ChatThinking.spec.tsx
File metadata and controls
294 lines (218 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
import { render, screen } from '@/test-utils'
import userEvent from '@testing-library/user-event'
import { vi, describe, test, expect, beforeEach } from 'vitest'
import { ChatThinking } from './ChatThinking'
import type { ThinkingStep } from '../types'
// Helper to create a thinking step
const createStep = (overrides: Partial<ThinkingStep> = {}): ThinkingStep => ({
id: 'step-1',
userMessageId: 'msg-1',
category: 'tasks',
functionName: 'test_function',
displayName: 'Test Function',
content: 'Step content here',
isComplete: false,
timestamp: new Date('2024-01-15T14:30:00'),
...overrides,
})
describe('ChatThinking', () => {
beforeEach(() => {
vi.clearAllMocks()
})
describe('empty state', () => {
test('renders nothing when no steps provided', () => {
render(<ChatThinking steps={[]} />)
// No status text, no toggle - component renders null
expect(screen.queryByText('Working on a response...')).not.toBeInTheDocument()
expect(screen.queryByText('Done')).not.toBeInTheDocument()
expect(screen.queryByText(/Show thinking/)).not.toBeInTheDocument()
})
})
describe('status header', () => {
test('shows spinner and working text when isThinking is true', () => {
const steps = [createStep()]
render(<ChatThinking steps={steps} isThinking={true} />)
expect(screen.getByLabelText('Thinking in progress')).toBeInTheDocument()
expect(screen.getByText('Working on a response...')).toBeInTheDocument()
})
test('shows check icon and done text when isThinking is false', () => {
const steps = [createStep()]
render(<ChatThinking steps={steps} isThinking={false} />)
expect(screen.queryByLabelText('Thinking in progress')).not.toBeInTheDocument()
expect(screen.getByText('Done')).toBeInTheDocument()
})
test('defaults to isThinking true', () => {
const steps = [createStep()]
render(<ChatThinking steps={steps} />)
expect(screen.getByLabelText('Thinking in progress')).toBeInTheDocument()
expect(screen.getByText('Working on a response...')).toBeInTheDocument()
})
test('shows warning icon and interrupted text when isInterrupted is true', () => {
const steps = [createStep()]
render(<ChatThinking steps={steps} isThinking={false} isInterrupted={true} />)
expect(screen.getByText('Interrupted')).toBeInTheDocument()
// Should NOT show "Done" or spinner
expect(screen.queryByText('Done')).not.toBeInTheDocument()
expect(screen.queryByLabelText('Thinking in progress')).not.toBeInTheDocument()
})
test('isThinking takes priority over isInterrupted', () => {
const steps = [createStep()]
// When both isThinking and isInterrupted are set, spinner should show (active thinking wins)
render(<ChatThinking steps={steps} isThinking={true} isInterrupted={true} />)
expect(screen.getByText('Working on a response...')).toBeInTheDocument()
expect(screen.queryByText('Interrupted')).not.toBeInTheDocument()
})
test('shows clock icon and waiting text when isWaiting is true', () => {
const steps = [createStep()]
render(<ChatThinking steps={steps} isThinking={false} isWaiting={true} />)
expect(screen.getByText('Waiting for response')).toBeInTheDocument()
expect(screen.queryByText('Done')).not.toBeInTheDocument()
expect(screen.queryByText('Interrupted')).not.toBeInTheDocument()
expect(screen.queryByLabelText('Thinking in progress')).not.toBeInTheDocument()
})
test('isWaiting takes priority over isInterrupted', () => {
const steps = [createStep()]
render(<ChatThinking steps={steps} isThinking={false} isWaiting={true} isInterrupted={true} />)
expect(screen.getByText('Waiting for response')).toBeInTheDocument()
expect(screen.queryByText('Interrupted')).not.toBeInTheDocument()
})
test('isThinking takes priority over isWaiting', () => {
const steps = [createStep()]
render(<ChatThinking steps={steps} isThinking={true} isWaiting={true} />)
expect(screen.getByText('Working on a response...')).toBeInTheDocument()
expect(screen.queryByText('Waiting for response')).not.toBeInTheDocument()
})
})
describe('collapse/expand toggle', () => {
test('shows step count in trigger', () => {
const steps = [createStep()]
render(<ChatThinking steps={steps} />)
expect(screen.getByText('Show thinking (1)')).toBeInTheDocument()
})
test('step list is collapsed by default', () => {
const steps = [createStep({ displayName: 'Intent Classifier' })]
render(<ChatThinking steps={steps} />)
// The content should be in the DOM but hidden by KUI Collapsible
expect(screen.getByText('Show thinking (1)')).toBeInTheDocument()
})
test('expands step list on trigger click', async () => {
const user = userEvent.setup()
const steps = [createStep({ displayName: 'Intent Classifier' })]
render(<ChatThinking steps={steps} />)
// Click the trigger area to expand
await user.click(screen.getByText('Show thinking (1)'))
expect(screen.getByText('Intent Classifier')).toBeVisible()
})
})
describe('step list rendering', () => {
test('renders all steps as flat list with displayName', async () => {
const user = userEvent.setup()
const steps = [
createStep({ id: '1', displayName: 'Intent Classifier', category: 'agents' }),
createStep({ id: '2', displayName: 'Depth Router', category: 'agents' }),
createStep({ id: '3', displayName: 'Web Search Tool', category: 'tools' }),
createStep({ id: '4', displayName: 'Tavily Search', category: 'tools' }),
]
render(<ChatThinking steps={steps} />)
// Expand via trigger
await user.click(screen.getByText(`Show thinking (${steps.length})`))
expect(screen.getByText('Intent Classifier')).toBeVisible()
expect(screen.getByText('Depth Router')).toBeVisible()
expect(screen.getByText('Web Search Tool')).toBeVisible()
expect(screen.getByText('Tavily Search')).toBeVisible()
})
test('shows timestamps for each step', async () => {
const user = userEvent.setup()
const steps = [createStep({ timestamp: new Date('2024-01-15T14:30:00') })]
render(<ChatThinking steps={steps} />)
await user.click(screen.getByText(/Show thinking/))
// Timestamp should be formatted and visible
expect(screen.getByText(/\d{1,2}:\d{2}/)).toBeInTheDocument()
})
test('renders steps from all categories in a flat list (no tabs)', async () => {
const user = userEvent.setup()
const steps = [
createStep({ id: '1', category: 'tasks', displayName: 'Workflow Task' }),
createStep({ id: '2', category: 'agents', displayName: 'Agent Step' }),
createStep({ id: '3', category: 'tools', displayName: 'Tool Step' }),
]
render(<ChatThinking steps={steps} />)
await user.click(screen.getByText(/Show thinking/))
// All three categories appear in a single flat list
expect(screen.getByText('Workflow Task')).toBeVisible()
expect(screen.getByText('Agent Step')).toBeVisible()
expect(screen.getByText('Tool Step')).toBeVisible()
})
test('step list has correct ARIA role', async () => {
const user = userEvent.setup()
const steps = [createStep()]
render(<ChatThinking steps={steps} />)
await user.click(screen.getByText(/Show thinking/))
expect(screen.getByRole('list', { name: 'Thinking steps' })).toBeInTheDocument()
})
})
describe('styling', () => {
test('outer container has base border class', () => {
const steps = [createStep()]
render(<ChatThinking steps={steps} />)
// The trigger lives inside the outer container with the base border
const triggerText = screen.getByText(/Show thinking/)
const outerDiv = triggerText.closest('.border-base')
expect(outerDiv).toBeInTheDocument()
})
})
describe('data sources summary', () => {
test('data sources are visible without expanding the collapsible', () => {
const steps = [createStep()]
const enabledDataSources = ['web_search', 'knowledge_base']
render(<ChatThinking steps={steps} enabledDataSources={enabledDataSources} />)
expect(screen.getByText('Selected Data Sources:')).toBeVisible()
expect(screen.getByText('Web Search, Knowledge Base')).toBeVisible()
})
test('displays files when provided', () => {
const steps = [createStep()]
const messageFiles = [
{ id: 'file-1', fileName: 'document.pdf' },
{ id: 'file-2', fileName: 'report.docx' },
]
render(<ChatThinking steps={steps} messageFiles={messageFiles} />)
expect(screen.getByText('Selected Data Sources:')).toBeVisible()
expect(screen.getByText('document.pdf, report.docx')).toBeVisible()
})
test('displays both data sources and files', () => {
const steps = [createStep()]
const enabledDataSources = ['web_search']
const messageFiles = [{ id: 'file-1', fileName: 'document.pdf' }]
render(
<ChatThinking
steps={steps}
enabledDataSources={enabledDataSources}
messageFiles={messageFiles}
/>
)
expect(screen.getByText('Selected Data Sources:')).toBeVisible()
expect(screen.getByText('Web Search')).toBeVisible()
expect(screen.getByText('document.pdf')).toBeVisible()
})
test('excludes knowledge_layer from data sources display', () => {
const steps = [createStep()]
const enabledDataSources = ['web_search', 'knowledge_layer']
render(<ChatThinking steps={steps} enabledDataSources={enabledDataSources} />)
expect(screen.getByText('Web Search')).toBeVisible()
expect(screen.queryByText(/Knowledge Layer/i)).not.toBeInTheDocument()
})
test('does not show data sources section when no sources or files', () => {
const steps = [createStep()]
render(<ChatThinking steps={steps} />)
expect(screen.queryByText('Selected Data Sources')).not.toBeInTheDocument()
})
test('formats data source names correctly', () => {
const steps = [createStep()]
const enabledDataSources = ['web_search', 'onedrive', 'google_drive']
render(<ChatThinking steps={steps} enabledDataSources={enabledDataSources} />)
expect(screen.getByText('Web Search, Onedrive, Google Drive')).toBeVisible()
})
})
})