|
| 1 | +import json |
| 2 | +import unittest |
| 3 | + |
| 4 | +from pydantic import TypeAdapter |
| 5 | + |
| 6 | +from ag_ui.core.events import Event, EventType, ToolCallResultEvent |
| 7 | + |
| 8 | + |
| 9 | +class TestToolCallResultError(unittest.TestCase): |
| 10 | + """The optional `error` on TOOL_CALL_RESULT — the event-side twin of ToolMessage.error.""" |
| 11 | + |
| 12 | + def _base(self, **overrides): |
| 13 | + kwargs = dict( |
| 14 | + type=EventType.TOOL_CALL_RESULT, |
| 15 | + message_id="msg_1", |
| 16 | + tool_call_id="tc_1", |
| 17 | + content='{"hits":2}', |
| 18 | + ) |
| 19 | + kwargs.update(overrides) |
| 20 | + return ToolCallResultEvent(**kwargs) |
| 21 | + |
| 22 | + def test_error_is_a_declared_field_not_an_extra(self): |
| 23 | + # ConfiguredBaseModel uses extra="allow", so simply passing error= and |
| 24 | + # reading it back would pass even if the field were never declared. |
| 25 | + # model_fields is the assertion that actually proves the declaration. |
| 26 | + self.assertIn("error", ToolCallResultEvent.model_fields) |
| 27 | + |
| 28 | + def test_defaults_to_none_and_is_omitted_from_the_wire(self): |
| 29 | + event = self._base() |
| 30 | + self.assertIsNone(event.error) |
| 31 | + self.assertNotIn("error", json.loads(event.model_dump_json(by_alias=True))) |
| 32 | + |
| 33 | + def test_carries_a_real_error_string(self): |
| 34 | + event = self._base(content="", error="SearchTimeout: upstream did not respond within 30s") |
| 35 | + self.assertEqual(event.error, "SearchTimeout: upstream did not respond within 30s") |
| 36 | + self.assertEqual( |
| 37 | + json.loads(event.model_dump_json(by_alias=True))["error"], |
| 38 | + "SearchTimeout: upstream did not respond within 30s", |
| 39 | + ) |
| 40 | + |
| 41 | + def test_empty_string_error_survives_rather_than_being_dropped(self): |
| 42 | + # Omission applies to None, not to a falsy value the producer chose to |
| 43 | + # send. An empty string must not silently become "the call succeeded". |
| 44 | + event = self._base(error="") |
| 45 | + self.assertEqual(event.error, "") |
| 46 | + self.assertEqual(json.loads(event.model_dump_json(by_alias=True))["error"], "") |
| 47 | + |
| 48 | + def test_round_trips_through_json(self): |
| 49 | + event = self._base(content="", error="boom") |
| 50 | + restored = ToolCallResultEvent.model_validate_json(event.model_dump_json(by_alias=True)) |
| 51 | + self.assertEqual(restored.error, "boom") |
| 52 | + self.assertEqual(restored.tool_call_id, "tc_1") |
| 53 | + |
| 54 | + def test_discriminated_union_still_resolves_tool_call_result(self): |
| 55 | + adapter = TypeAdapter(Event) |
| 56 | + with_error = adapter.validate_python( |
| 57 | + { |
| 58 | + "type": "TOOL_CALL_RESULT", |
| 59 | + "messageId": "msg_1", |
| 60 | + "toolCallId": "tc_1", |
| 61 | + "content": "", |
| 62 | + "error": "boom", |
| 63 | + } |
| 64 | + ) |
| 65 | + self.assertIsInstance(with_error, ToolCallResultEvent) |
| 66 | + self.assertEqual(with_error.error, "boom") |
| 67 | + |
| 68 | + without_error = adapter.validate_python( |
| 69 | + { |
| 70 | + "type": "TOOL_CALL_RESULT", |
| 71 | + "messageId": "msg_1", |
| 72 | + "toolCallId": "tc_1", |
| 73 | + "content": "ok", |
| 74 | + } |
| 75 | + ) |
| 76 | + self.assertIsInstance(without_error, ToolCallResultEvent) |
| 77 | + self.assertIsNone(without_error.error) |
| 78 | + |
| 79 | + def test_a_pre_existing_event_serializes_exactly_as_before(self): |
| 80 | + # The additive guarantee: an event from before this field existed keeps |
| 81 | + # the same keys and values, so no consumer sees a change. |
| 82 | + legacy_wire = { |
| 83 | + "type": "TOOL_CALL_RESULT", |
| 84 | + "messageId": "msg_1", |
| 85 | + "toolCallId": "tc_1", |
| 86 | + "content": '{"hits":2}', |
| 87 | + "role": "tool", |
| 88 | + } |
| 89 | + event = ToolCallResultEvent.model_validate(legacy_wire) |
| 90 | + self.assertEqual(json.loads(event.model_dump_json(by_alias=True)), legacy_wire) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + unittest.main() |
0 commit comments