forked from openai/chatkit-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_widgets.py
More file actions
261 lines (235 loc) · 7.63 KB
/
Copy pathtest_widgets.py
File metadata and controls
261 lines (235 loc) · 7.63 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
import json
from datetime import datetime
from typing import Any, Literal
import pytest
from chatkit.server import diff_widget
from chatkit.types import WidgetItem
from chatkit.widgets import (
BasicRoot,
Card,
DynamicWidgetComponent,
DynamicWidgetRoot,
Text,
WidgetRoot,
WidgetTemplate,
)
@pytest.mark.parametrize(
"before, after, expected",
[
(Card(children=[]), Card(children=[]), []),
(
Card(children=[Text(id="text", value="Hello", streaming=True)]),
Card(children=[Text(id="text", value="Hello, world!", streaming=True)]),
["widget.streaming_text.value_delta"],
),
(
Card(children=[Text(id="text", value="Hello", streaming=True)]),
Card(children=[Text(id="text", value="Hello, world!", streaming=False)]),
["widget.root.updated"],
),
(
Card(children=[Text(value="Hello")]),
Card(children=[Text(value="world!")]),
["widget.root.updated"],
),
# DynamicWidgetRoot tests
(
DynamicWidgetRoot(type="Card", children=[]),
DynamicWidgetRoot(type="Card", children=[]),
[],
),
(
DynamicWidgetRoot(
type="Card",
children=[
DynamicWidgetComponent.model_validate({
"type": "Text",
"id": "text",
"value": "Hello",
"streaming": True,
})
],
),
DynamicWidgetRoot(
type="Card",
children=[
DynamicWidgetComponent.model_validate({
"type": "Text",
"id": "text",
"value": "Hello, world!",
"streaming": True,
})
],
),
["widget.streaming_text.value_delta"],
),
(
DynamicWidgetRoot(
type="Card",
children=[
DynamicWidgetComponent.model_validate({
"type": "Text",
"id": "text",
"value": "Hello",
"streaming": True,
})
],
),
DynamicWidgetRoot(
type="Card",
children=[
DynamicWidgetComponent.model_validate({
"type": "Text",
"id": "text",
"value": "Hello, world!",
"streaming": False,
})
],
),
["widget.root.updated"],
),
(
DynamicWidgetRoot(
type="Card",
children=[
DynamicWidgetComponent.model_validate({
"type": "Text",
"value": "Hello",
})
],
),
DynamicWidgetRoot(
type="Card",
children=[
DynamicWidgetComponent.model_validate({
"type": "Text",
"value": "world!",
})
],
),
["widget.root.updated"],
),
],
)
def test_diff(
before: WidgetRoot,
after: WidgetRoot,
expected: list[
Literal[
"widget.streaming_text.value_delta",
"widget.root.updated",
]
],
):
diff = diff_widget(before, after)
assert len(diff) == len(expected)
for i in range(len(diff)):
assert diff[i].type == expected[i]
def test_json_dump_excludes_none_fields():
widget = Card(children=[Text(value="Hello")])
json_str = widget.model_dump_json()
assert isinstance(json_str, str)
data = json.loads(json_str)
# Top-level widget should include type and exclude None-valued fields.
assert data["type"] == "Card"
assert "key" not in data
assert "padding" not in data
assert "status" not in data
assert "collapsed" not in data
# Children should be serialized with None fields omitted as well.
assert isinstance(data["children"], list)
assert len(data["children"]) == 1
text_dump = data["children"][0]
assert text_dump["type"] == "Text"
assert text_dump["value"] == "Hello"
assert "italic" not in text_dump
assert "streaming" not in text_dump
assert "color" not in text_dump
assert "key" not in text_dump
def test_json_dump_excludes_none_fields_nested():
widget = Card(children=[Text(value="Hello")])
widget_item = WidgetItem(
thread_id="1", widget=widget, id="1", created_at=datetime.now()
)
json_str = widget_item.model_dump_json()
assert isinstance(json_str, str)
data = json.loads(json_str)
# Top-level widget should include type and exclude None-valued fields.
widget_dump = data["widget"]
assert widget_dump["type"] == "Card"
assert "key" not in widget_dump
assert "padding" not in widget_dump
assert "status" not in widget_dump
assert "collapsed" not in widget_dump
# Children should be serialized with None fields omitted as well.
assert isinstance(widget_dump["children"], list)
assert len(widget_dump["children"]) == 1
text_dump = widget_dump["children"][0]
assert text_dump["type"] == "Text"
assert text_dump["value"] == "Hello"
assert "italic" not in text_dump
assert "streaming" not in text_dump
assert "color" not in text_dump
assert "key" not in text_dump
@pytest.mark.parametrize(
"widget_name, data",
[
("list_view_no_data", None),
("card_no_data", None),
(
"list_view_with_data",
{
"items": [
{
"id": "blue",
"label": "Blue line",
"color": "blue-500",
},
{
"id": "orange",
"label": "Orange line",
"color": "orange-500",
},
{
"id": "purple",
"label": "Purple line",
"color": "purple-500",
},
],
},
),
(
"card_with_data",
{
"channel": "#proj-chatkit",
"time": "4:48 PM",
"user": {
"image": "/pam.png",
"name": "Pam Beesly",
},
},
),
],
)
def test_widget_template_from_file(
widget_name: str,
data: dict[str, Any] | None,
):
template = WidgetTemplate.from_file(f"assets/widgets/{widget_name}.widget")
with open(f"tests/assets/widgets/{widget_name}.json", "r") as file:
expected_widget_dict = json.load(file)
widget = template.build(data)
assert isinstance(widget, DynamicWidgetRoot)
assert widget.model_dump(exclude_none=True) == expected_widget_dict
def test_widget_template_with_basic_root():
template = WidgetTemplate.from_file("assets/widgets/basic_root.widget")
with open("tests/assets/widgets/basic_root.json", "r") as file:
expected_widget_dict = json.load(file)
widget = template.build_basic(
{
"name": "Harry Potter",
"bio": "The boy who lived",
},
)
assert isinstance(widget, BasicRoot)
assert widget.model_dump(exclude_none=True) == expected_widget_dict