|
| 1 | +""" |
| 2 | +Tests for ChatController message content normalization functionality. |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from src.core.app.controllers.chat_controller import ChatController |
| 9 | + |
| 10 | + |
| 11 | +class TestCoerceMessageContentToText: |
| 12 | + """Test cases for _coerce_message_content_to_text method.""" |
| 13 | + |
| 14 | + def test_coerce_message_content_to_text_handles_string(self) -> None: |
| 15 | + """String content should be returned as-is.""" |
| 16 | + content = "Hello, world!" |
| 17 | + result = ChatController._coerce_message_content_to_text(content) |
| 18 | + assert result == "Hello, world!" |
| 19 | + |
| 20 | + def test_coerce_message_content_to_text_handles_bytes(self) -> None: |
| 21 | + """Bytes content should be decoded as UTF-8.""" |
| 22 | + content = b"Hello, world!" |
| 23 | + result = ChatController._coerce_message_content_to_text(content) |
| 24 | + assert result == "Hello, world!" |
| 25 | + |
| 26 | + def test_coerce_message_content_to_text_handles_none(self) -> None: |
| 27 | + """None input should return empty string.""" |
| 28 | + result = ChatController._coerce_message_content_to_text(None) |
| 29 | + assert result == "" |
| 30 | + |
| 31 | + def test_coerce_message_content_to_text_handles_empty_sequence(self) -> None: |
| 32 | + """Empty sequences should return empty string.""" |
| 33 | + result = ChatController._coerce_message_content_to_text([]) |
| 34 | + assert result == "" |
| 35 | + |
| 36 | + def test_coerce_message_content_to_text_handles_dict_with_text(self) -> None: |
| 37 | + """Dict with text field should extract text value.""" |
| 38 | + content = {"text": "Hello from dict"} |
| 39 | + result = ChatController._coerce_message_content_to_text(content) |
| 40 | + assert result == "Hello from dict" |
| 41 | + |
| 42 | + def test_coerce_message_content_to_text_handles_dict_with_bytes_text(self) -> None: |
| 43 | + """Dict with bytes text field should decode bytes.""" |
| 44 | + content = {"text": b"Hello from bytes"} |
| 45 | + result = ChatController._coerce_message_content_to_text(content) |
| 46 | + assert result == "Hello from bytes" |
| 47 | + |
| 48 | + def test_coerce_message_content_to_text_extracts_image_url(self) -> None: |
| 49 | + """Image URL content should extract the URL string.""" |
| 50 | + content = { |
| 51 | + "type": "image_url", |
| 52 | + "image_url": {"url": "https://example.com/image.png"}, |
| 53 | + } |
| 54 | + result = ChatController._coerce_message_content_to_text(content) |
| 55 | + assert result == "https://example.com/image.png" |
| 56 | + |
| 57 | + def test_coerce_message_content_to_text_handles_dict_without_text(self) -> None: |
| 58 | + """Dict without text should JSON serialize.""" |
| 59 | + content = {"key": "value", "number": 42} |
| 60 | + result = ChatController._coerce_message_content_to_text(content) |
| 61 | + assert result == json.dumps(content, ensure_ascii=False) |
| 62 | + |
| 63 | + def test_coerce_message_content_to_text_handles_sequence(self) -> None: |
| 64 | + """Sequence should flatten parts with double newlines.""" |
| 65 | + content = ["Part 1", "Part 2", "Part 3"] |
| 66 | + result = ChatController._coerce_message_content_to_text(content) |
| 67 | + assert result == "Part 1\n\nPart 2\n\nPart 3" |
| 68 | + |
| 69 | + def test_coerce_message_content_to_text_handles_nested_sequence(self) -> None: |
| 70 | + """Nested sequences should be flattened recursively.""" |
| 71 | + content = ["Outer 1", ["Inner 1", "Inner 2"], "Outer 2"] |
| 72 | + result = ChatController._coerce_message_content_to_text(content) |
| 73 | + assert result == "Outer 1\n\nInner 1\n\nInner 2\n\nOuter 2" |
| 74 | + |
| 75 | + def test_coerce_message_content_to_text_handles_mixed_sequence(self) -> None: |
| 76 | + """Mixed sequence should handle different types.""" |
| 77 | + content = ["Text part", {"text": "Dict part"}, b"Bytes part"] |
| 78 | + result = ChatController._coerce_message_content_to_text(content) |
| 79 | + assert result == "Text part\n\nDict part\n\nBytes part" |
| 80 | + |
| 81 | + def test_coerce_message_content_to_text_handles_object_with_model_dump( |
| 82 | + self, |
| 83 | + ) -> None: |
| 84 | + """Objects with model_dump should use dumped content.""" |
| 85 | + |
| 86 | + class TestModel: |
| 87 | + def model_dump(self) -> dict[str, Any]: |
| 88 | + return {"text": "From model_dump"} |
| 89 | + |
| 90 | + content = TestModel() |
| 91 | + result = ChatController._coerce_message_content_to_text(content) |
| 92 | + assert result == "From model_dump" |
| 93 | + |
| 94 | + def test_coerce_message_content_to_text_handles_object_with_text_attr(self) -> None: |
| 95 | + """Objects with text attribute should return the text value.""" |
| 96 | + |
| 97 | + class CustomObject: |
| 98 | + text = "custom content" |
| 99 | + |
| 100 | + result = ChatController._coerce_message_content_to_text(CustomObject()) |
| 101 | + assert result == "custom content" |
| 102 | + |
| 103 | + def test_coerce_message_content_to_text_handles_object_with_bytes_text_attr( |
| 104 | + self, |
| 105 | + ) -> None: |
| 106 | + """Objects with bytes text attribute should decode bytes.""" |
| 107 | + |
| 108 | + class CustomObject: |
| 109 | + text = b"custom content" |
| 110 | + |
| 111 | + result = ChatController._coerce_message_content_to_text(CustomObject()) |
| 112 | + assert result == "custom content" |
| 113 | + |
| 114 | + def test_coerce_message_content_to_text_fallback_to_str(self) -> None: |
| 115 | + """Unknown objects should fallback to str().""" |
| 116 | + |
| 117 | + class CustomObject: |
| 118 | + def __str__(self) -> str: |
| 119 | + return "string representation" |
| 120 | + |
| 121 | + result = ChatController._coerce_message_content_to_text(CustomObject()) |
| 122 | + assert result == "string representation" |
| 123 | + |
| 124 | + def test_coerce_message_content_to_text_handles_model_dump_exception(self) -> None: |
| 125 | + """Objects with failing model_dump should continue processing.""" |
| 126 | + |
| 127 | + class TestModel: |
| 128 | + def model_dump(self) -> dict[str, Any]: |
| 129 | + raise RuntimeError("Dump failed") |
| 130 | + |
| 131 | + content = TestModel() |
| 132 | + result = ChatController._coerce_message_content_to_text(content) |
| 133 | + assert result == str(content) |
| 134 | + |
| 135 | + def test_coerce_message_content_to_text_prevents_stack_overflow(self) -> None: |
| 136 | + """Circular references should not cause stack overflow.""" |
| 137 | + # Create a circular reference |
| 138 | + content: dict[str, Any] = {} |
| 139 | + content["self"] = content |
| 140 | + |
| 141 | + # This should not raise RecursionError but should handle circular reference gracefully |
| 142 | + result = ChatController._coerce_message_content_to_text(content) |
| 143 | + # Should return some string representation without infinite recursion |
| 144 | + assert isinstance(result, str) |
| 145 | + assert len(result) > 0 |
| 146 | + # The result should contain some indication of the circular reference |
| 147 | + assert "Circular reference detected" in result |
0 commit comments