Jack1808/Claude_Code
0
1from unittest.mock import MagicMock2 3import pytest4 5from messaging.rendering.telegram_markdown import (6 escape_md_v2,7 escape_md_v2_code,8 mdv2_bold,9 mdv2_code_inline,10 render_markdown_to_mdv2,11)12from messaging.transcript import RenderCtx, TranscriptBuffer13 14 15@pytest.fixture16def handler():17 platform = MagicMock()18 cli = MagicMock()19 store = MagicMock()20 # Kept for backwards test structure; transcript rendering is now separate.21 return (platform, cli, store)22 23 24def _ctx() -> RenderCtx:25 return RenderCtx(26 bold=mdv2_bold,27 code_inline=mdv2_code_inline,28 escape_code=escape_md_v2_code,29 escape_text=escape_md_v2,30 render_markdown=render_markdown_to_mdv2,31 )32 33 34def test_transcript_structure_and_order(handler):35 """Verify ordered transcript rendering (thinking/tool/subagent/text/error/status)."""36 status = "✅ *Complete*"37 t = TranscriptBuffer()38 39 # Apply in a deliberate sequence.40 t.apply({"type": "thinking_chunk", "text": "Thinking process..."})41 t.apply(42 {"type": "tool_use", "id": "t1", "name": "list_files", "input": {"path": "."}}43 )44 45 # Subagent marker (Task tool).46 t.apply(47 {48 "type": "tool_use",49 "id": "task1",50 "name": "Task",51 "input": {"description": "Searching codebase..."},52 }53 )54 t.apply(55 {"type": "tool_use", "id": "t2", "name": "read_file", "input": {"path": "x.py"}}56 )57 t.apply({"type": "tool_result", "tool_use_id": "task1", "content": "done"})58 59 t.apply({"type": "text_chunk", "text": "Here is the file content."})60 t.apply({"type": "error", "message": "Some error happened"})61 62 msg = t.render(_ctx(), limit_chars=3900, status=status)63 64 print(f"Generated Message:\n{msg}")65 66 # Check existence67 assert "Thinking process..." in msg68 assert "list_files" in msg69 assert "read_file" in msg70 assert "Searching codebase..." in msg71 assert escape_md_v2("Here is the file content.") in msg72 assert "Some error happened" in msg73 assert "✅ *Complete*" in msg74 75 # Check headers/markers used in the transcript.76 assert "💭 *Thinking*" in msg77 assert "🛠 *Tool call:*" in msg78 assert "🤖 *Subagent:*" in msg79 assert "⚠️ *Error:*" in msg80 81 # Check Order: Thinking -> Tool call -> Subagent -> Content -> Errors -> Status82 p_thinking = msg.find("Thinking process...")83 p_tool_call = msg.find("🛠 *Tool call:*")84 p_subagent = msg.find("🤖 *Subagent:*")85 p_content = msg.find(escape_md_v2("Here is the file content."))86 p_errors = msg.find("⚠️ *Error:*")87 p_status = msg.find("✅ *Complete*")88 89 assert p_thinking < p_tool_call, "Thinking should come before tool calls"90 assert p_tool_call < p_subagent, "Tool calls should come before subagent marker"91 assert p_subagent < p_content, "Subagent should come before Content"92 assert p_content < p_errors, "Content should come before Errors"93 assert p_errors < p_status, "Errors should come before Status"94 95 96def test_transcript_simple(handler):97 """Verify simple transcript with just text + status."""98 t = TranscriptBuffer()99 t.apply({"type": "text_chunk", "text": "Simple message."})100 msg = t.render(_ctx(), limit_chars=3900, status="Ready")101 102 assert escape_md_v2("Simple message.") in msg103 assert "Ready" in msg104 assert "💭" not in msg105 assert "🛠" not in msg106 107 108def test_subagents_formatting(handler):109 """Verify subagent formatting (Task tool)."""110 t = TranscriptBuffer()111 t.apply(112 {113 "type": "tool_use",114 "id": "task_1",115 "name": "Task",116 "input": {"description": "Task 1"},117 }118 )119 t.apply({"type": "tool_result", "tool_use_id": "task_1", "content": "done"})120 t.apply(121 {122 "type": "tool_use",123 "id": "task_2",124 "name": "Task",125 "input": {"description": "Task 2"},126 }127 )128 129 msg = t.render(_ctx(), limit_chars=3900, status=None)130 131 assert "🤖 *Subagent:* `Task 1`" in msg132 assert "🤖 *Subagent:* `Task 2`" in msg133 