Jack1808/Claude_Code
0
1from messaging.event_parser import parse_cli_event2 3 4def test_parse_cli_event_assistant_content():5 event = {6 "type": "assistant",7 "message": {8 "content": [9 {"type": "thinking", "thinking": "Internal thought"},10 {"type": "text", "text": "Hello user"},11 ]12 },13 }14 results = parse_cli_event(event)15 assert len(results) == 216 assert results[0] == {"type": "thinking_chunk", "text": "Internal thought"}17 assert results[1] == {"type": "text_chunk", "text": "Hello user"}18 19 20def test_parse_cli_event_assistant_tools():21 event = {22 "type": "assistant",23 "message": {24 "content": [{"type": "tool_use", "name": "ls", "input": {"path": "."}}]25 },26 }27 results = parse_cli_event(event)28 assert len(results) == 129 assert results[0]["type"] == "tool_use"30 assert results[0]["name"] == "ls"31 assert results[0]["input"] == {"path": "."}32 33 34def test_parse_cli_event_assistant_subagent():35 event = {36 "type": "assistant",37 "message": {38 "content": [39 {40 "type": "tool_use",41 "name": "Task",42 "input": {"description": "Fix bug"},43 }44 ]45 },46 }47 results = parse_cli_event(event)48 assert len(results) == 149 assert results[0]["type"] == "tool_use"50 assert results[0]["name"] == "Task"51 assert results[0]["input"] == {"description": "Fix bug"}52 53 54def test_parse_cli_event_content_block_delta():55 # Text delta56 event_text = {57 "type": "content_block_delta",58 "index": 0,59 "delta": {"type": "text_delta", "text": " more"},60 }61 results_text = parse_cli_event(event_text)62 assert results_text == [{"type": "text_delta", "index": 0, "text": " more"}]63 64 # Thinking delta65 event_think = {66 "type": "content_block_delta",67 "index": 1,68 "delta": {"type": "thinking_delta", "thinking": " more thought"},69 }70 results_think = parse_cli_event(event_think)71 assert results_think == [72 {"type": "thinking_delta", "index": 1, "text": " more thought"}73 ]74 75 76def test_parse_cli_event_content_block_start():77 event = {78 "type": "content_block_start",79 "index": 2,80 "content_block": {81 "type": "tool_use",82 "name": "Task",83 "input": {"description": "deploy"},84 },85 }86 results = parse_cli_event(event)87 assert results == [88 {89 "type": "tool_use_start",90 "index": 2,91 "id": "",92 "name": "Task",93 "input": {"description": "deploy"},94 }95 ]96 97 98def test_parse_cli_event_error():99 event = {"type": "error", "error": {"message": "something failed"}}100 results = parse_cli_event(event)101 assert results == [{"type": "error", "message": "something failed"}]102 103 104def test_parse_cli_event_user_tool_result():105 event = {106 "type": "user",107 "message": {108 "content": [109 {110 "type": "tool_result",111 "tool_use_id": "tool_1",112 "content": "ok",113 "is_error": False,114 }115 ]116 },117 }118 results = parse_cli_event(event)119 assert results == [120 {121 "type": "tool_result",122 "tool_use_id": "tool_1",123 "content": "ok",124 "is_error": False,125 }126 ]127 128 129def test_parse_cli_event_exit_success():130 event = {"type": "exit", "code": 0}131 results = parse_cli_event(event)132 assert results == [{"type": "complete", "status": "success"}]133 134 135def test_parse_cli_event_exit_failure():136 event = {"type": "exit", "code": 1, "stderr": "fatal error"}137 results = parse_cli_event(event)138 assert len(results) == 2139 assert results[0] == {"type": "error", "message": "fatal error"}140 assert results[1] == {"type": "complete", "status": "failed"}141 142 143def test_parse_cli_event_invalid_input():144 assert parse_cli_event(None) == []145 assert parse_cli_event("not a dict") == []146 assert parse_cli_event({"type": "unknown"}) == []147 148 149def test_parse_cli_event_system_ignored():150 assert parse_cli_event({"type": "system", "foo": "bar"}) == []151 152 153def test_parse_cli_event_result_with_content_directly():154 event = {"type": "result", "content": [{"type": "text", "text": "hi"}]}155 assert parse_cli_event(event) == [{"type": "text_chunk", "text": "hi"}]156 157 158def test_parse_cli_event_result_with_result_content_directly():159 event = {"type": "result", "result": {"content": [{"type": "text", "text": "hi"}]}}160 assert parse_cli_event(event) == [{"type": "text_chunk", "text": "hi"}]161 162 163def test_parse_cli_event_content_block_unknown_type_skipped():164 """Content block with unknown type is skipped; known blocks still parsed."""165 event = {166 "type": "assistant",167 "message": {168 "content": [169 {"type": "text", "text": "visible"},170 {"type": "unknown", "data": "ignored"},171 {"type": "thinking", "thinking": "thought"},172 ]173 },174 }175 results = parse_cli_event(event)176 assert len(results) == 2177 assert results[0] == {"type": "text_chunk", "text": "visible"}178 assert results[1] == {"type": "thinking_chunk", "text": "thought"}179 180 181def test_parse_cli_event_error_non_dict():182 """Error event with error as string (not dict) is handled."""183 event = {"type": "error", "error": "plain string error"}184 results = parse_cli_event(event)185 assert results == [{"type": "error", "message": "plain string error"}]186 187 188def test_parse_cli_event_exit_code_none():189 """Exit event with no code defaults to success."""190 event = {"type": "exit"}191 results = parse_cli_event(event)192 assert results == [{"type": "complete", "status": "success"}]193 