| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
from typing import Any, cast |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from anthropic import Anthropic, AsyncAnthropic |
| 9 |
from tests.utils import assert_matches_type |
| 10 |
from anthropic.pagination import SyncPageCursor, AsyncPageCursor |
| 11 |
from anthropic.types.beta.sessions import BetaManagedAgentsSessionEvent |
| 12 |
|
| 13 |
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 14 |
|
| 15 |
|
| 16 |
class TestEvents: |
| 17 |
parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 18 |
|
| 19 |
@pytest.mark.skip(reason="buildURL drops path-level query params") |
| 20 |
@parametrize |
| 21 |
def test_method_list(self, client: Anthropic) -> None: |
| 22 |
event = client.beta.sessions.threads.events.list( |
| 23 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 24 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 25 |
) |
| 26 |
assert_matches_type(SyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"]) |
| 27 |
|
| 28 |
@pytest.mark.skip(reason="buildURL drops path-level query params") |
| 29 |
@parametrize |
| 30 |
def test_method_list_with_all_params(self, client: Anthropic) -> None: |
| 31 |
event = client.beta.sessions.threads.events.list( |
| 32 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 33 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 34 |
limit=0, |
| 35 |
page="page", |
| 36 |
betas=["message-batches-2024-09-24"], |
| 37 |
workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy", |
| 38 |
) |
| 39 |
assert_matches_type(SyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"]) |
| 40 |
|
| 41 |
@pytest.mark.skip(reason="buildURL drops path-level query params") |
| 42 |
@parametrize |
| 43 |
def test_raw_response_list(self, client: Anthropic) -> None: |
| 44 |
response = client.beta.sessions.threads.events.with_raw_response.list( |
| 45 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 46 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 47 |
) |
| 48 |
|
| 49 |
assert response.is_closed is True |
| 50 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 51 |
event = response.parse() |
| 52 |
assert_matches_type(SyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"]) |
| 53 |
|
| 54 |
@pytest.mark.skip(reason="buildURL drops path-level query params") |
| 55 |
@parametrize |
| 56 |
def test_streaming_response_list(self, client: Anthropic) -> None: |
| 57 |
with client.beta.sessions.threads.events.with_streaming_response.list( |
| 58 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 59 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 60 |
) as response: |
| 61 |
assert not response.is_closed |
| 62 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 63 |
|
| 64 |
event = response.parse() |
| 65 |
assert_matches_type(SyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"]) |
| 66 |
|
| 67 |
assert cast(Any, response.is_closed) is True |
| 68 |
|
| 69 |
@pytest.mark.skip(reason="buildURL drops path-level query params") |
| 70 |
@parametrize |
| 71 |
def test_path_params_list(self, client: Anthropic) -> None: |
| 72 |
with pytest.raises(ValueError, match=r"Expected a non-empty value for `session_id` but received ''"): |
| 73 |
client.beta.sessions.threads.events.with_raw_response.list( |
| 74 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 75 |
session_id="", |
| 76 |
) |
| 77 |
|
| 78 |
with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
| 79 |
client.beta.sessions.threads.events.with_raw_response.list( |
| 80 |
thread_id="", |
| 81 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 82 |
) |
| 83 |
|
| 84 |
@parametrize |
| 85 |
def test_method_stream(self, client: Anthropic) -> None: |
| 86 |
event_stream = client.beta.sessions.threads.events.stream( |
| 87 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 88 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 89 |
) |
| 90 |
event_stream.response.close() |
| 91 |
|
| 92 |
@parametrize |
| 93 |
def test_method_stream_with_all_params(self, client: Anthropic) -> None: |
| 94 |
event_stream = client.beta.sessions.threads.events.stream( |
| 95 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 96 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 97 |
event_deltas=["agent.message"], |
| 98 |
betas=["message-batches-2024-09-24"], |
| 99 |
workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy", |
| 100 |
) |
| 101 |
event_stream.response.close() |
| 102 |
|
| 103 |
@parametrize |
| 104 |
def test_raw_response_stream(self, client: Anthropic) -> None: |
| 105 |
response = client.beta.sessions.threads.events.with_raw_response.stream( |
| 106 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 107 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 108 |
) |
| 109 |
|
| 110 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 111 |
stream = response.parse() |
| 112 |
stream.close() |
| 113 |
|
| 114 |
@parametrize |
| 115 |
def test_streaming_response_stream(self, client: Anthropic) -> None: |
| 116 |
with client.beta.sessions.threads.events.with_streaming_response.stream( |
| 117 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 118 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 119 |
) as response: |
| 120 |
assert not response.is_closed |
| 121 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 122 |
|
| 123 |
stream = response.parse() |
| 124 |
stream.close() |
| 125 |
|
| 126 |
assert cast(Any, response.is_closed) is True |
| 127 |
|
| 128 |
@parametrize |
| 129 |
def test_path_params_stream(self, client: Anthropic) -> None: |
| 130 |
with pytest.raises(ValueError, match=r"Expected a non-empty value for `session_id` but received ''"): |
| 131 |
client.beta.sessions.threads.events.with_raw_response.stream( |
| 132 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 133 |
session_id="", |
| 134 |
) |
| 135 |
|
| 136 |
with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
| 137 |
client.beta.sessions.threads.events.with_raw_response.stream( |
| 138 |
thread_id="", |
| 139 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 140 |
) |
| 141 |
|
| 142 |
|
| 143 |
class TestAsyncEvents: |
| 144 |
parametrize = pytest.mark.parametrize( |
| 145 |
"async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"] |
| 146 |
) |
| 147 |
|
| 148 |
@pytest.mark.skip(reason="buildURL drops path-level query params") |
| 149 |
@parametrize |
| 150 |
async def test_method_list(self, async_client: AsyncAnthropic) -> None: |
| 151 |
event = await async_client.beta.sessions.threads.events.list( |
| 152 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 153 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 154 |
) |
| 155 |
assert_matches_type(AsyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"]) |
| 156 |
|
| 157 |
@pytest.mark.skip(reason="buildURL drops path-level query params") |
| 158 |
@parametrize |
| 159 |
async def test_method_list_with_all_params(self, async_client: AsyncAnthropic) -> None: |
| 160 |
event = await async_client.beta.sessions.threads.events.list( |
| 161 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 162 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 163 |
limit=0, |
| 164 |
page="page", |
| 165 |
betas=["message-batches-2024-09-24"], |
| 166 |
workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy", |
| 167 |
) |
| 168 |
assert_matches_type(AsyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"]) |
| 169 |
|
| 170 |
@pytest.mark.skip(reason="buildURL drops path-level query params") |
| 171 |
@parametrize |
| 172 |
async def test_raw_response_list(self, async_client: AsyncAnthropic) -> None: |
| 173 |
response = await async_client.beta.sessions.threads.events.with_raw_response.list( |
| 174 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 175 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 176 |
) |
| 177 |
|
| 178 |
assert response.is_closed is True |
| 179 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 180 |
event = await response.parse() |
| 181 |
assert_matches_type(AsyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"]) |
| 182 |
|
| 183 |
@pytest.mark.skip(reason="buildURL drops path-level query params") |
| 184 |
@parametrize |
| 185 |
async def test_streaming_response_list(self, async_client: AsyncAnthropic) -> None: |
| 186 |
async with async_client.beta.sessions.threads.events.with_streaming_response.list( |
| 187 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 188 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 189 |
) as response: |
| 190 |
assert not response.is_closed |
| 191 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 192 |
|
| 193 |
event = await response.parse() |
| 194 |
assert_matches_type(AsyncPageCursor[BetaManagedAgentsSessionEvent], event, path=["response"]) |
| 195 |
|
| 196 |
assert cast(Any, response.is_closed) is True |
| 197 |
|
| 198 |
@pytest.mark.skip(reason="buildURL drops path-level query params") |
| 199 |
@parametrize |
| 200 |
async def test_path_params_list(self, async_client: AsyncAnthropic) -> None: |
| 201 |
with pytest.raises(ValueError, match=r"Expected a non-empty value for `session_id` but received ''"): |
| 202 |
await async_client.beta.sessions.threads.events.with_raw_response.list( |
| 203 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 204 |
session_id="", |
| 205 |
) |
| 206 |
|
| 207 |
with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
| 208 |
await async_client.beta.sessions.threads.events.with_raw_response.list( |
| 209 |
thread_id="", |
| 210 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 211 |
) |
| 212 |
|
| 213 |
@parametrize |
| 214 |
async def test_method_stream(self, async_client: AsyncAnthropic) -> None: |
| 215 |
event_stream = await async_client.beta.sessions.threads.events.stream( |
| 216 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 217 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 218 |
) |
| 219 |
await event_stream.response.aclose() |
| 220 |
|
| 221 |
@parametrize |
| 222 |
async def test_method_stream_with_all_params(self, async_client: AsyncAnthropic) -> None: |
| 223 |
event_stream = await async_client.beta.sessions.threads.events.stream( |
| 224 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 225 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 226 |
event_deltas=["agent.message"], |
| 227 |
betas=["message-batches-2024-09-24"], |
| 228 |
workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy", |
| 229 |
) |
| 230 |
await event_stream.response.aclose() |
| 231 |
|
| 232 |
@parametrize |
| 233 |
async def test_raw_response_stream(self, async_client: AsyncAnthropic) -> None: |
| 234 |
response = await async_client.beta.sessions.threads.events.with_raw_response.stream( |
| 235 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 236 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 237 |
) |
| 238 |
|
| 239 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 240 |
stream = await response.parse() |
| 241 |
await stream.close() |
| 242 |
|
| 243 |
@parametrize |
| 244 |
async def test_streaming_response_stream(self, async_client: AsyncAnthropic) -> None: |
| 245 |
async with async_client.beta.sessions.threads.events.with_streaming_response.stream( |
| 246 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 247 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 248 |
) as response: |
| 249 |
assert not response.is_closed |
| 250 |
assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 251 |
|
| 252 |
stream = await response.parse() |
| 253 |
await stream.close() |
| 254 |
|
| 255 |
assert cast(Any, response.is_closed) is True |
| 256 |
|
| 257 |
@parametrize |
| 258 |
async def test_path_params_stream(self, async_client: AsyncAnthropic) -> None: |
| 259 |
with pytest.raises(ValueError, match=r"Expected a non-empty value for `session_id` but received ''"): |
| 260 |
await async_client.beta.sessions.threads.events.with_raw_response.stream( |
| 261 |
thread_id="sthr_011CZkZVWa6oIjw0rgXZpnBt", |
| 262 |
session_id="", |
| 263 |
) |
| 264 |
|
| 265 |
with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"): |
| 266 |
await async_client.beta.sessions.threads.events.with_raw_response.stream( |
| 267 |
thread_id="", |
| 268 |
session_id="sesn_011CZkZAtmR3yMPDzynEDxu7", |
| 269 |
) |