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
104 lines (88 loc) · 3.16 KB
/
Copy pathtest_widgets.py
File metadata and controls
104 lines (88 loc) · 3.16 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
import json
from datetime import datetime
from typing import Literal
import pytest
from chatkit.server import diff_widget
from chatkit.types import WidgetItem
from chatkit.widgets import Card, Text, WidgetRoot
@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"],
),
],
)
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