|
1 | | -"""Tests for the shared claude-agent-sdk helpers (hackbot_runtime.claude).""" |
| 1 | +"""Tests for the shared claude-agent-sdk Reporter (hackbot_runtime.claude).""" |
2 | 2 |
|
3 | | -import asyncio |
4 | | - |
5 | | -import pytest |
6 | | -from claude_agent_sdk import ( |
7 | | - ResultMessage, |
8 | | - SystemMessage, |
9 | | - TaskNotificationMessage, |
10 | | - TaskStartedMessage, |
11 | | - TaskUpdatedMessage, |
12 | | -) |
13 | | -from hackbot_runtime.claude import ( |
14 | | - Reporter, |
15 | | - UnsettledResponseError, |
16 | | - _truncate, |
17 | | - receive_settled_response, |
18 | | -) |
| 3 | +from hackbot_runtime.claude import Reporter, _truncate |
19 | 4 |
|
20 | 5 |
|
21 | 6 | def test_truncate_short_string_unchanged(): |
@@ -49,136 +34,3 @@ def test_no_log_file_when_path_is_none(tmp_path): |
49 | 34 | with Reporter(verbose=True, log_path=None) as reporter: |
50 | 35 | reporter.header("section") |
51 | 36 | assert not list(tmp_path.iterdir()) |
52 | | - |
53 | | - |
54 | | -class _FakeClient: |
55 | | - """Replays a fixed message list from ``receive_messages()``. |
56 | | -
|
57 | | - If ``hang_after`` is set, blocks forever once the list is exhausted, |
58 | | - simulating a connection left open with no further messages — the |
59 | | - situation ``timeout_s`` in ``receive_settled_response`` is meant to |
60 | | - bound. |
61 | | - """ |
62 | | - |
63 | | - def __init__(self, messages, hang_after: bool = False): |
64 | | - self._messages = messages |
65 | | - self._hang_after = hang_after |
66 | | - |
67 | | - async def receive_messages(self): |
68 | | - for msg in self._messages: |
69 | | - yield msg |
70 | | - if self._hang_after: |
71 | | - await asyncio.Event().wait() |
72 | | - |
73 | | - |
74 | | -def _result(is_error: bool = False, num_turns: int = 1) -> ResultMessage: |
75 | | - return ResultMessage( |
76 | | - subtype="success", |
77 | | - duration_ms=1, |
78 | | - duration_api_ms=1, |
79 | | - is_error=is_error, |
80 | | - num_turns=num_turns, |
81 | | - session_id="s1", |
82 | | - ) |
83 | | - |
84 | | - |
85 | | -def _task_started(task_id: str, task_type: str = "local_agent") -> TaskStartedMessage: |
86 | | - return TaskStartedMessage( |
87 | | - subtype="task_started", |
88 | | - data={}, |
89 | | - task_id=task_id, |
90 | | - description="do a thing", |
91 | | - uuid="u1", |
92 | | - session_id="s1", |
93 | | - task_type=task_type, |
94 | | - ) |
95 | | - |
96 | | - |
97 | | -def _task_notification( |
98 | | - task_id: str, status: str = "completed" |
99 | | -) -> TaskNotificationMessage: |
100 | | - return TaskNotificationMessage( |
101 | | - subtype="task_notification", |
102 | | - data={}, |
103 | | - task_id=task_id, |
104 | | - status=status, |
105 | | - output_file="", |
106 | | - summary="done", |
107 | | - uuid="u2", |
108 | | - session_id="s1", |
109 | | - ) |
110 | | - |
111 | | - |
112 | | -def _task_updated(task_id: str, status: str = "completed") -> TaskUpdatedMessage: |
113 | | - return TaskUpdatedMessage( |
114 | | - subtype="task_updated", |
115 | | - data={}, |
116 | | - task_id=task_id, |
117 | | - patch={"status": status}, |
118 | | - status=status, |
119 | | - ) |
120 | | - |
121 | | - |
122 | | -async def test_receive_settled_response_returns_immediately_when_nothing_pending(): |
123 | | - result = _result() |
124 | | - client = _FakeClient([result]) |
125 | | - seen = [] |
126 | | - |
127 | | - got = await receive_settled_response(client, on_message=seen.append) |
128 | | - |
129 | | - assert got is result |
130 | | - assert seen == [result] |
131 | | - |
132 | | - |
133 | | -async def test_receive_settled_response_keeps_draining_past_early_result(): |
134 | | - started = _task_started("t1") |
135 | | - early_result = _result(num_turns=1) |
136 | | - notification = _task_notification("t1") |
137 | | - final_result = _result(num_turns=2) |
138 | | - client = _FakeClient([started, early_result, notification, final_result]) |
139 | | - |
140 | | - got = await receive_settled_response(client) |
141 | | - |
142 | | - # The first ResultMessage arrived while "t1" was still open — it must be |
143 | | - # ignored in favor of the one that follows the task's terminal message. |
144 | | - assert got is final_result |
145 | | - |
146 | | - |
147 | | -async def test_receive_settled_response_task_updated_also_clears_pending(): |
148 | | - started = _task_started("t1") |
149 | | - early_result = _result(num_turns=1) |
150 | | - updated = _task_updated("t1") |
151 | | - final_result = _result(num_turns=2) |
152 | | - client = _FakeClient([started, early_result, updated, final_result]) |
153 | | - |
154 | | - got = await receive_settled_response(client) |
155 | | - |
156 | | - assert got is final_result |
157 | | - |
158 | | - |
159 | | -async def test_receive_settled_response_ignores_non_deferring_task_types(): |
160 | | - # A backgrounded shell (task_type="local_bash") can run indefinitely by |
161 | | - # design — the CLI itself never holds the result frame back for one, so |
162 | | - # neither should we. Settling immediately (rather than waiting on "t1") |
163 | | - # is the correct behavior here, not a race we need to rescue. |
164 | | - started = _task_started("t1", task_type="local_bash") |
165 | | - result = _result() |
166 | | - client = _FakeClient([started, result]) |
167 | | - |
168 | | - got = await receive_settled_response(client) |
169 | | - |
170 | | - assert got is result |
171 | | - |
172 | | - |
173 | | -async def test_receive_settled_response_raises_on_timeout_when_task_never_settles(): |
174 | | - client = _FakeClient([_task_started("t1"), _result()], hang_after=True) |
175 | | - |
176 | | - with pytest.raises(UnsettledResponseError): |
177 | | - await receive_settled_response(client, timeout_s=0.05) |
178 | | - |
179 | | - |
180 | | -async def test_receive_settled_response_raises_when_stream_ends_without_result(): |
181 | | - client = _FakeClient([SystemMessage(subtype="init", data={})]) |
182 | | - |
183 | | - with pytest.raises(UnsettledResponseError): |
184 | | - await receive_settled_response(client) |
0 commit comments