-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_streaming.py
More file actions
211 lines (169 loc) · 5.4 KB
/
Copy pathtest_streaming.py
File metadata and controls
211 lines (169 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""Tests for StreamReader and AsyncStreamReader."""
from __future__ import annotations
import json
from unittest.mock import AsyncMock, MagicMock
import httpx
import pytest
from hawk.streaming import AsyncStreamReader, StreamReader
class TestStreamReader:
"""Tests for synchronous StreamReader."""
def _make_stream(self, lines: list[str]) -> StreamReader:
"""Create a StreamReader from a list of raw lines."""
response = MagicMock(spec=httpx.Response)
response.iter_lines.return_value = iter(lines)
response.close = MagicMock()
return StreamReader(response)
def test_basic_events(self) -> None:
lines = [
"data: Hello",
"",
"data: World",
"",
"event: done",
"data: {}",
"",
]
reader = self._make_stream(lines)
events = list(reader.events())
assert len(events) == 3
assert events[0].data == "Hello"
assert events[0].event is None
assert events[1].data == "World"
assert events[2].event == "done"
assert events[2].data == "{}"
def test_collect_text(self) -> None:
lines = [
"data: Hello ",
"",
"data: World",
"",
"event: done",
"data: {}",
"",
]
reader = self._make_stream(lines)
text = reader.collect_text()
assert text == "Hello World"
def test_collect_tool_calls(self) -> None:
tool_data = json.dumps(
{
"id": "tc-1",
"name": "get_weather",
"arguments": {"location": "NYC"},
}
)
lines = [
"event: tool_call",
f"data: {tool_data}",
"",
"event: done",
"data: {}",
"",
]
reader = self._make_stream(lines)
calls = reader.collect_tool_calls()
assert len(calls) == 1
assert calls[0].name == "get_weather"
assert calls[0].arguments == {"location": "NYC"}
def test_multi_line_data(self) -> None:
"""SSE spec: multiple data: lines in one event are joined with newlines."""
lines = [
"data: line1",
"data: line2",
"",
]
reader = self._make_stream(lines)
events = list(reader.events())
assert len(events) == 1
assert events[0].data == "line1\nline2"
def test_empty_data_field(self) -> None:
lines = [
"data:",
"",
]
reader = self._make_stream(lines)
events = list(reader.events())
assert len(events) == 1
assert events[0].data == ""
def test_context_manager(self) -> None:
response = MagicMock(spec=httpx.Response)
response.iter_lines.return_value = iter(["data: hi", ""])
response.close = MagicMock()
with StreamReader(response) as reader:
events = list(reader.events())
assert len(events) == 1
response.close.assert_called_once()
def test_multiple_empty_lines_ignored(self) -> None:
lines = [
"",
"",
"data: content",
"",
"",
"",
]
reader = self._make_stream(lines)
events = list(reader.events())
assert len(events) == 1
assert events[0].data == "content"
@pytest.mark.asyncio
class TestAsyncStreamReader:
"""Tests for async stream reader."""
async def test_basic_events(self) -> None:
lines = [
"data: Hello",
"",
"data: World",
"",
]
response = MagicMock(spec=httpx.Response)
async def _aiter_lines():
for line in lines:
yield line
response.aiter_lines.return_value = _aiter_lines()
response.aclose = AsyncMock()
reader = AsyncStreamReader(response)
events = []
async for event in reader:
events.append(event)
assert len(events) == 2
assert events[0].data == "Hello"
assert events[1].data == "World"
async def test_multi_line_data(self) -> None:
"""SSE spec: multiple data: lines in one event are joined with newlines."""
lines = [
"data: line1",
"data: line2",
"",
]
response = MagicMock(spec=httpx.Response)
async def _aiter_lines():
for line in lines:
yield line
response.aiter_lines.return_value = _aiter_lines()
response.aclose = AsyncMock()
reader = AsyncStreamReader(response)
events = []
async for event in reader:
events.append(event)
assert len(events) == 1
assert events[0].data == "line1\nline2"
async def test_collect_text(self) -> None:
lines = [
"data: Async ",
"",
"data: text",
"",
"event: done",
"data: {}",
"",
]
response = MagicMock(spec=httpx.Response)
async def _aiter_lines():
for line in lines:
yield line
response.aiter_lines.return_value = _aiter_lines()
response.aclose = AsyncMock()
reader = AsyncStreamReader(response)
text = await reader.collect_text()
assert text == "Async text"