-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_output_parser_test.go
More file actions
386 lines (298 loc) · 9.41 KB
/
ui_output_parser_test.go
File metadata and controls
386 lines (298 loc) · 9.41 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package sdk
import (
"testing"
)
func TestUIOutputParser_ParseFencedBlocks(t *testing.T) {
parser := NewUIOutputParser()
content := `Here is your data:
` + "```ui:table" + `
{
"title": "Users",
"headers": [{"label": "Name", "key": "name"}, {"label": "Email", "key": "email"}],
"rows": [["Alice", "alice@example.com"], ["Bob", "bob@example.com"]]
}
` + "```" + `
The data shows all active users.`
result := parser.Parse(content)
if len(result.UIBlocks) != 1 {
t.Fatalf("expected 1 UI block, got %d", len(result.UIBlocks))
}
block := result.UIBlocks[0]
if block.Type != PartTypeTable {
t.Errorf("expected type 'table', got '%s'", block.Type)
}
if block.Part == nil {
t.Error("expected part to be parsed")
}
// Check clean content doesn't have UI block
if parser.HasUIBlocks(result.CleanContent) {
t.Error("clean content should not have UI blocks")
}
}
func TestUIOutputParser_ParseInlineTags(t *testing.T) {
parser := NewUIOutputParser()
content := `Here is a chart: <ui:chart>{"type": "bar", "title": "Sales", "labels": ["Q1", "Q2"], "datasets": [{"label": "Revenue", "data": [100, 200]}]}</ui:chart> and some text after.`
result := parser.Parse(content)
if len(result.UIBlocks) != 1 {
t.Fatalf("expected 1 UI block, got %d", len(result.UIBlocks))
}
block := result.UIBlocks[0]
if block.Type != PartTypeChart {
t.Errorf("expected type 'chart', got '%s'", block.Type)
}
}
func TestUIOutputParser_MultipleBlocks(t *testing.T) {
parser := NewUIOutputParser()
content := `First table:
` + "```ui:table" + `
{"title": "Table 1", "headers": [{"label": "A", "key": "a"}], "rows": [["1"]]}
` + "```" + `
And now a chart:
` + "```ui:chart" + `
{"type": "pie", "title": "Distribution", "labels": ["A", "B"], "datasets": [{"data": [50, 50]}]}
` + "```" + `
And some metrics: <ui:metrics>{"metrics": [{"label": "Total", "value": 100}]}</ui:metrics>
Done.`
result := parser.Parse(content)
if len(result.UIBlocks) != 3 {
t.Fatalf("expected 3 UI blocks, got %d", len(result.UIBlocks))
}
expectedTypes := []ContentPartType{PartTypeTable, PartTypeChart, PartTypeMetric}
for i, block := range result.UIBlocks {
if block.Type != expectedTypes[i] {
t.Errorf("block %d: expected type '%s', got '%s'", i, expectedTypes[i], block.Type)
}
}
// Verify text parts exist
if len(result.TextParts) == 0 {
t.Error("expected text parts to be extracted")
}
}
func TestUIOutputParser_NoBlocks(t *testing.T) {
parser := NewUIOutputParser()
content := "This is just plain text with no UI blocks."
result := parser.Parse(content)
if len(result.UIBlocks) != 0 {
t.Errorf("expected 0 UI blocks, got %d", len(result.UIBlocks))
}
if result.CleanContent != content {
t.Error("clean content should be unchanged")
}
}
func TestUIOutputParser_HasUIBlocks(t *testing.T) {
parser := NewUIOutputParser()
tests := []struct {
name string
content string
expected bool
}{
{
name: "with fenced block",
content: "```ui:table\n{}\n```",
expected: true,
},
{
name: "with inline tag",
content: "<ui:chart>{}</ui:chart>",
expected: true,
},
{
name: "plain text",
content: "No UI blocks here",
expected: false,
},
{
name: "regular code block",
content: "```python\nprint('hello')\n```",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := parser.HasUIBlocks(tt.content)
if result != tt.expected {
t.Errorf("HasUIBlocks() = %v, expected %v", result, tt.expected)
}
})
}
}
func TestUIOutputParser_TypeNormalization(t *testing.T) {
parser := NewUIOutputParser()
tests := []struct {
input string
expected ContentPartType
}{
{"<ui:table>{}</ui:table>", PartTypeTable},
{"<ui:chart>{}</ui:chart>", PartTypeChart},
{"<ui:graph>{}</ui:graph>", PartTypeChart}, // graph normalizes to chart
{"<ui:metric>{}</ui:metric>", PartTypeMetric},
{"<ui:metrics>{}</ui:metrics>", PartTypeMetric},
{"<ui:kpi>{}</ui:kpi>", PartTypeMetric}, // kpi normalizes to metric
{"<ui:buttons>{}</ui:buttons>", PartTypeButtonGroup},
{"<ui:button>{}</ui:button>", PartTypeButtonGroup},
{"<ui:buttongroup>{}</ui:buttongroup>", PartTypeButtonGroup},
{"<ui:kanban>{}</ui:kanban>", PartTypeKanban},
{"<ui:board>{}</ui:board>", PartTypeKanban}, // board normalizes to kanban
{"<ui:alert>{}</ui:alert>", PartTypeAlert},
{"<ui:notification>{}</ui:notification>", PartTypeAlert},
}
for _, tt := range tests {
result := parser.Parse(tt.input)
if len(result.UIBlocks) == 0 {
t.Errorf("expected UI block for %s", tt.input)
continue
}
if result.UIBlocks[0].Type != tt.expected {
t.Errorf("input %s: expected type %s, got %s", tt.input, tt.expected, result.UIBlocks[0].Type)
}
}
}
func TestUIOutputParser_DisableType(t *testing.T) {
parser := NewUIOutputParser()
parser.DisableType(PartTypeTable)
content := `<ui:table>{"headers": [], "rows": []}</ui:table>
<ui:chart>{"type": "bar", "labels": [], "datasets": []}</ui:chart>`
result := parser.Parse(content)
// Should only have chart, not table
if len(result.UIBlocks) != 1 {
t.Fatalf("expected 1 UI block (chart), got %d", len(result.UIBlocks))
}
if result.UIBlocks[0].Type != PartTypeChart {
t.Errorf("expected chart block, got %s", result.UIBlocks[0].Type)
}
}
func TestUIOutputParser_ParseTableBlock(t *testing.T) {
parser := NewUIOutputParser()
content := "```ui:table\n" + `{
"title": "Sales Data",
"headers": [
{"label": "Month", "key": "month", "sortable": true},
{"label": "Revenue", "key": "revenue"}
],
"rows": [
["January", "$10,000"],
["February", "$12,000"],
["March", "$15,000"]
]
}` + "\n```"
result := parser.Parse(content)
if len(result.UIBlocks) != 1 {
t.Fatalf("expected 1 UI block, got %d", len(result.UIBlocks))
}
tablePart, ok := result.UIBlocks[0].Part.(*TablePart)
if !ok {
t.Fatal("expected TablePart")
}
if tablePart.Title != "Sales Data" {
t.Errorf("expected title 'Sales Data', got '%s'", tablePart.Title)
}
if len(tablePart.Headers) != 2 {
t.Errorf("expected 2 headers, got %d", len(tablePart.Headers))
}
if len(tablePart.Rows) != 3 {
t.Errorf("expected 3 rows, got %d", len(tablePart.Rows))
}
}
func TestUIOutputParser_ParseChartBlock(t *testing.T) {
parser := NewUIOutputParser()
content := "```ui:chart\n" + `{
"title": "Revenue Trend",
"type": "line",
"labels": ["Jan", "Feb", "Mar", "Apr"],
"datasets": [
{
"label": "2024",
"data": [100, 150, 200, 250],
"borderColor": "#4CAF50"
}
]
}` + "\n```"
result := parser.Parse(content)
if len(result.UIBlocks) != 1 {
t.Fatalf("expected 1 UI block, got %d", len(result.UIBlocks))
}
chartPart, ok := result.UIBlocks[0].Part.(*ChartPart)
if !ok {
t.Fatal("expected ChartPart")
}
if chartPart.Title != "Revenue Trend" {
t.Errorf("expected title 'Revenue Trend', got '%s'", chartPart.Title)
}
if chartPart.ChartType != ChartLine {
t.Errorf("expected type 'line', got '%s'", chartPart.ChartType)
}
if len(chartPart.Data.Labels) != 4 {
t.Errorf("expected 4 labels, got %d", len(chartPart.Data.Labels))
}
}
func TestUIOutputParser_ParseAlertBlock(t *testing.T) {
parser := NewUIOutputParser()
content := `<ui:alert>{"title": "Warning", "message": "Please check your input", "severity": "warning", "dismissible": true}</ui:alert>`
result := parser.Parse(content)
if len(result.UIBlocks) != 1 {
t.Fatalf("expected 1 UI block, got %d", len(result.UIBlocks))
}
alertPart, ok := result.UIBlocks[0].Part.(*AlertPart)
if !ok {
t.Fatal("expected AlertPart")
}
if alertPart.Title != "Warning" {
t.Errorf("expected title 'Warning', got '%s'", alertPart.Title)
}
if alertPart.Severity != AlertWarning {
t.Errorf("expected severity 'warning', got '%s'", alertPart.Severity)
}
if !alertPart.Dismissible {
t.Error("expected dismissible to be true")
}
}
func TestUIOutputParser_InvalidJSON(t *testing.T) {
parser := NewUIOutputParser()
// Invalid JSON should result in an error but not panic
content := "```ui:table\n{invalid json}\n```"
result := parser.Parse(content)
// Should have one block with error
if len(result.Errors) == 0 {
t.Error("expected parsing error for invalid JSON")
}
}
func TestUIOutputParser_ExtractUIBlocks(t *testing.T) {
parser := NewUIOutputParser()
content := `Text before <ui:metric>{"metrics": [{"label": "Score", "value": 95}]}</ui:metric> text after`
blocks := parser.ExtractUIBlocks(content)
if len(blocks) != 1 {
t.Fatalf("expected 1 block, got %d", len(blocks))
}
if blocks[0].Type != PartTypeMetric {
t.Errorf("expected metric type, got %s", blocks[0].Type)
}
}
func TestUIOutputParser_CleanContent(t *testing.T) {
parser := NewUIOutputParser()
content := `Before
<ui:table>{"headers": [], "rows": []}</ui:table>
After`
result := parser.Parse(content)
// Clean content should have Before and After but not the UI block
if parser.HasUIBlocks(result.CleanContent) {
t.Error("clean content should not contain UI blocks")
}
if result.CleanContent == "" {
t.Error("clean content should not be empty")
}
}
func TestConvertToStructuredResponse(t *testing.T) {
parser := NewUIOutputParser()
content := `<ui:table>{"title": "Test", "headers": [], "rows": []}</ui:table>`
result := parser.Parse(content)
response := ConvertToStructuredResponse(result)
if response == nil {
t.Fatal("expected structured response")
}
if response.ID == "" {
t.Error("expected response ID")
}
if len(response.Parts) == 0 {
t.Error("expected parts in response")
}
}