openai/chatkit-python
Publicmirrored fromhttps://github.com/openai/chatkit-pythonAvailable
tests/test_widgets.py
104lines · modecode
| 1 | import json |
| 2 | from datetime import datetime |
| 3 | from typing import Literal |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from chatkit.server import diff_widget |
| 8 | from chatkit.types import WidgetItem |
| 9 | from chatkit.widgets import Card, Text, WidgetRoot |
| 10 | |
| 11 | |
| 12 | @pytest.mark.parametrize( |
| 13 | "before, after, expected", |
| 14 | [ |
| 15 | (Card(children=[]), Card(children=[]), []), |
| 16 | ( |
| 17 | Card(children=[Text(id="text", value="Hello", streaming=True)]), |
| 18 | Card(children=[Text(id="text", value="Hello, world!", streaming=True)]), |
| 19 | ["widget.streaming_text.value_delta"], |
| 20 | ), |
| 21 | ( |
| 22 | Card(children=[Text(id="text", value="Hello", streaming=True)]), |
| 23 | Card(children=[Text(id="text", value="Hello, world!", streaming=False)]), |
| 24 | ["widget.root.updated"], |
| 25 | ), |
| 26 | ( |
| 27 | Card(children=[Text(value="Hello")]), |
| 28 | Card(children=[Text(value="world!")]), |
| 29 | ["widget.root.updated"], |
| 30 | ), |
| 31 | ], |
| 32 | ) |
| 33 | def test_diff( |
| 34 | before: WidgetRoot, |
| 35 | after: WidgetRoot, |
| 36 | expected: list[ |
| 37 | Literal[ |
| 38 | "widget.streaming_text.value_delta", |
| 39 | "widget.root.updated", |
| 40 | ] |
| 41 | ], |
| 42 | ): |
| 43 | diff = diff_widget(before, after) |
| 44 | assert len(diff) == len(expected) |
| 45 | for i in range(len(diff)): |
| 46 | assert diff[i].type == expected[i] |
| 47 | |
| 48 | |
| 49 | def test_json_dump_excludes_none_fields(): |
| 50 | widget = Card(children=[Text(value="Hello")]) |
| 51 | |
| 52 | json_str = widget.model_dump_json() |
| 53 | assert isinstance(json_str, str) |
| 54 | data = json.loads(json_str) |
| 55 | |
| 56 | # Top-level widget should include type and exclude None-valued fields. |
| 57 | assert data["type"] == "Card" |
| 58 | assert "key" not in data |
| 59 | assert "padding" not in data |
| 60 | assert "status" not in data |
| 61 | assert "collapsed" not in data |
| 62 | |
| 63 | # Children should be serialized with None fields omitted as well. |
| 64 | assert isinstance(data["children"], list) |
| 65 | assert len(data["children"]) == 1 |
| 66 | |
| 67 | text_dump = data["children"][0] |
| 68 | assert text_dump["type"] == "Text" |
| 69 | assert text_dump["value"] == "Hello" |
| 70 | assert "italic" not in text_dump |
| 71 | assert "streaming" not in text_dump |
| 72 | assert "color" not in text_dump |
| 73 | assert "key" not in text_dump |
| 74 | |
| 75 | |
| 76 | def test_json_dump_excludes_none_fields_nested(): |
| 77 | widget = Card(children=[Text(value="Hello")]) |
| 78 | widget_item = WidgetItem( |
| 79 | thread_id="1", widget=widget, id="1", created_at=datetime.now() |
| 80 | ) |
| 81 | |
| 82 | json_str = widget_item.model_dump_json() |
| 83 | assert isinstance(json_str, str) |
| 84 | data = json.loads(json_str) |
| 85 | |
| 86 | # Top-level widget should include type and exclude None-valued fields. |
| 87 | widget_dump = data["widget"] |
| 88 | assert widget_dump["type"] == "Card" |
| 89 | assert "key" not in widget_dump |
| 90 | assert "padding" not in widget_dump |
| 91 | assert "status" not in widget_dump |
| 92 | assert "collapsed" not in widget_dump |
| 93 | |
| 94 | # Children should be serialized with None fields omitted as well. |
| 95 | assert isinstance(widget_dump["children"], list) |
| 96 | assert len(widget_dump["children"]) == 1 |
| 97 | |
| 98 | text_dump = widget_dump["children"][0] |
| 99 | assert text_dump["type"] == "Text" |
| 100 | assert text_dump["value"] == "Hello" |
| 101 | assert "italic" not in text_dump |
| 102 | assert "streaming" not in text_dump |
| 103 | assert "color" not in text_dump |
| 104 | assert "key" not in text_dump |
| 105 | |