|
| 1 | +"""Tests for message parser error handling.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from claude_code_sdk._errors import MessageParseError |
| 6 | +from claude_code_sdk._internal.message_parser import parse_message |
| 7 | +from claude_code_sdk.types import ( |
| 8 | + AssistantMessage, |
| 9 | + ResultMessage, |
| 10 | + SystemMessage, |
| 11 | + TextBlock, |
| 12 | + ToolUseBlock, |
| 13 | + UserMessage, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class TestMessageParser: |
| 18 | + """Test message parsing with the new exception behavior.""" |
| 19 | + |
| 20 | + def test_parse_valid_user_message(self): |
| 21 | + """Test parsing a valid user message.""" |
| 22 | + data = {"type": "user", "message": {"content": [{"type": "text", "text": "Hello"}]}} |
| 23 | + message = parse_message(data) |
| 24 | + assert isinstance(message, UserMessage) |
| 25 | + |
| 26 | + def test_parse_valid_assistant_message(self): |
| 27 | + """Test parsing a valid assistant message.""" |
| 28 | + data = { |
| 29 | + "type": "assistant", |
| 30 | + "message": { |
| 31 | + "content": [ |
| 32 | + {"type": "text", "text": "Hello"}, |
| 33 | + { |
| 34 | + "type": "tool_use", |
| 35 | + "id": "tool_123", |
| 36 | + "name": "Read", |
| 37 | + "input": {"file_path": "/test.txt"}, |
| 38 | + }, |
| 39 | + ] |
| 40 | + }, |
| 41 | + } |
| 42 | + message = parse_message(data) |
| 43 | + assert isinstance(message, AssistantMessage) |
| 44 | + assert len(message.content) == 2 |
| 45 | + assert isinstance(message.content[0], TextBlock) |
| 46 | + assert isinstance(message.content[1], ToolUseBlock) |
| 47 | + |
| 48 | + def test_parse_valid_system_message(self): |
| 49 | + """Test parsing a valid system message.""" |
| 50 | + data = {"type": "system", "subtype": "start"} |
| 51 | + message = parse_message(data) |
| 52 | + assert isinstance(message, SystemMessage) |
| 53 | + assert message.subtype == "start" |
| 54 | + |
| 55 | + def test_parse_valid_result_message(self): |
| 56 | + """Test parsing a valid result message.""" |
| 57 | + data = { |
| 58 | + "type": "result", |
| 59 | + "subtype": "success", |
| 60 | + "duration_ms": 1000, |
| 61 | + "duration_api_ms": 500, |
| 62 | + "is_error": False, |
| 63 | + "num_turns": 2, |
| 64 | + "session_id": "session_123", |
| 65 | + } |
| 66 | + message = parse_message(data) |
| 67 | + assert isinstance(message, ResultMessage) |
| 68 | + assert message.subtype == "success" |
| 69 | + |
| 70 | + def test_parse_invalid_data_type(self): |
| 71 | + """Test that non-dict data raises MessageParseError.""" |
| 72 | + with pytest.raises(MessageParseError) as exc_info: |
| 73 | + parse_message("not a dict") # type: ignore |
| 74 | + assert "Invalid message data type" in str(exc_info.value) |
| 75 | + assert "expected dict, got str" in str(exc_info.value) |
| 76 | + |
| 77 | + def test_parse_missing_type_field(self): |
| 78 | + """Test that missing 'type' field raises MessageParseError.""" |
| 79 | + with pytest.raises(MessageParseError) as exc_info: |
| 80 | + parse_message({"message": {"content": []}}) |
| 81 | + assert "Message missing 'type' field" in str(exc_info.value) |
| 82 | + |
| 83 | + def test_parse_unknown_message_type(self): |
| 84 | + """Test that unknown message type raises MessageParseError.""" |
| 85 | + with pytest.raises(MessageParseError) as exc_info: |
| 86 | + parse_message({"type": "unknown_type"}) |
| 87 | + assert "Unknown message type: unknown_type" in str(exc_info.value) |
| 88 | + |
| 89 | + def test_parse_user_message_missing_fields(self): |
| 90 | + """Test that user message with missing fields raises MessageParseError.""" |
| 91 | + with pytest.raises(MessageParseError) as exc_info: |
| 92 | + parse_message({"type": "user"}) |
| 93 | + assert "Missing required field in user message" in str(exc_info.value) |
| 94 | + |
| 95 | + def test_parse_assistant_message_missing_fields(self): |
| 96 | + """Test that assistant message with missing fields raises MessageParseError.""" |
| 97 | + with pytest.raises(MessageParseError) as exc_info: |
| 98 | + parse_message({"type": "assistant"}) |
| 99 | + assert "Missing required field in assistant message" in str(exc_info.value) |
| 100 | + |
| 101 | + def test_parse_system_message_missing_fields(self): |
| 102 | + """Test that system message with missing fields raises MessageParseError.""" |
| 103 | + with pytest.raises(MessageParseError) as exc_info: |
| 104 | + parse_message({"type": "system"}) |
| 105 | + assert "Missing required field in system message" in str(exc_info.value) |
| 106 | + |
| 107 | + def test_parse_result_message_missing_fields(self): |
| 108 | + """Test that result message with missing fields raises MessageParseError.""" |
| 109 | + with pytest.raises(MessageParseError) as exc_info: |
| 110 | + parse_message({"type": "result", "subtype": "success"}) |
| 111 | + assert "Missing required field in result message" in str(exc_info.value) |
| 112 | + |
| 113 | + def test_message_parse_error_contains_data(self): |
| 114 | + """Test that MessageParseError contains the original data.""" |
| 115 | + data = {"type": "unknown", "some": "data"} |
| 116 | + with pytest.raises(MessageParseError) as exc_info: |
| 117 | + parse_message(data) |
| 118 | + assert exc_info.value.data == data |
0 commit comments