Skip to content

Commit 5559bdb

Browse files
authored
test(api): cover generated WebSocket resources in redirect harness (openai#3992)
## Summary Connect the WebSocket redirect/reconnect test harness to the generated resource registry so newly generated resource families inherit the existing coverage. The harness supplies synthetic required connection arguments and retains SDK-owned beta Realtime coverage. API definitions and runtime behavior are unchanged. ## Validation - All 99 WebSocket redirect/reconnect tests pass locally. - Ruff formatting and lint pass for the changed test file. - Full SDK, compatibility, and Castiron checks run in CI. Co-authored-by: apcha-oai <228803254+apcha-oai@users.noreply.github.com>
1 parent 51692ed commit 5559bdb

3 files changed

Lines changed: 33 additions & 13 deletions

File tree

‎.castiron.stats.yml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
schema_version: 1
2-
generation_id: 531ae883-4e92-4d68-806a-afa871a99ebf
2+
generation_id: ce06443e-d28b-4283-849b-5a25fd1ad9a9
33
openapi_spec_hash: 982e032cd9bc8921825601fbb36a8a92
44
openapi_transformed_spec_hash: 7554bcd8a14262861882411fc4426386
55
config_hash: ac46b7ace0a09008785717fbff2f1d19
6-
codegen_sha: f3f4c861a6667d43481d59bc13ec819aea842424
7-
codegen_hash: 9a593cdd8f956c8e66684b95d43e866785aa2ddfa5050b37032ddcd86564cd61
8-
public_codegen_sha: 6c05ca76072a68a16d7a5fccd5b78f648f90c3ba
6+
codegen_sha: 0d833f5593b49e59cbc139ebecdabb03dd2e2137
7+
codegen_hash: 9db992d1159f9180396901efa950985fd0eee02fccb090be17c15d53b9a829c7
8+
public_codegen_sha: 401db4818440978f47a92ae7535a243934ccea29
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# File generated from our OpenAPI spec by Castiron. See CONTRIBUTING.md for details.
2+
3+
# Consumed by the SDK-owned tests/lib/test_websocket_redirects.py harness.
4+
WEBSOCKET_RESOURCES = [
5+
"beta.responses",
6+
"responses",
7+
"live",
8+
"live.sideband",
9+
"live.forks",
10+
"realtime",
11+
]

‎tests/lib/test_websocket_redirects.py‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import inspect
45
from typing import Any, Callable
56
from unittest.mock import Mock, AsyncMock
67

@@ -15,8 +16,10 @@
1516
from openai import OpenAI, AsyncOpenAI, AsyncAzureOpenAI
1617
from openai.lib._websocket import _WebSocketConnect
1718
from openai.types.websocket_reconnection import ReconnectingEvent
19+
from tests.api_resources._websocket_resources import WEBSOCKET_RESOURCES
1820

19-
RESOURCES = ["realtime", "beta.realtime", "responses", "beta.responses", "live", "live.sideband", "live.forks"]
21+
# Beta Realtime is SDK-owned; all generated resources come from the compiler.
22+
RESOURCES = sorted({*WEBSOCKET_RESOURCES, "beta.realtime"})
2023
RECONNECTING_RESOURCES = [name for name in RESOURCES if name != "beta.realtime"]
2124
EXTRA_HEADERS = {"api-key": "fake-key", "Cookie": "fake-cookie", "X-Custom": "fake-private-header"}
2225
FOLLOWS_REDIRECTS = hasattr(connect, "process_redirect")
@@ -36,11 +39,17 @@ def resource(client: Any, name: str) -> Any:
3639
return client
3740

3841

39-
def options(name: str) -> dict[str, Any]:
42+
def options(client: Any, name: str) -> dict[str, Any]:
43+
# The socket-free harness needs only synthetic values for required URL arguments.
44+
parameters = inspect.signature(resource(client, name).connect).parameters
4045
return {
4146
"extra_headers": EXTRA_HEADERS,
42-
**({"model": "fake-model"} if name.endswith("realtime") else {}),
43-
**({"session_id": "live_fake"} if name in {"live.sideband", "live.forks"} else {}),
47+
**{
48+
key: "fake-value"
49+
for key, parameter in parameters.items()
50+
if parameter.default is inspect.Parameter.empty
51+
and parameter.kind in (inspect.Parameter.POSITIONAL_OR_KEYWORD, inspect.Parameter.KEYWORD_ONLY)
52+
},
4453
}
4554

4655

@@ -112,7 +121,7 @@ async def test_async_websocket_redirects(
112121
websocket_base_url=base,
113122
http_client=async_http_client(),
114123
) as client:
115-
manager = resource(client, name).connect(**options(name))
124+
manager = resource(client, name).connect(**options(client, name))
116125
if name in RECONNECTING_RESOURCES:
117126
manager.send({"type": "response.create"})
118127
if succeeds:
@@ -146,7 +155,7 @@ async def test_later_cross_origin_redirect_is_rejected(monkeypatch: pytest.Monke
146155
api_key="fake-key", websocket_base_url="wss://origin.test", http_client=async_http_client()
147156
) as client:
148157
with pytest.raises(SecurityError):
149-
async with resource(client, name).connect(**options(name)):
158+
async with resource(client, name).connect(**options(client, name)):
150159
pytest.fail("Unexpected connection")
151160
assert len(handshakes.attempts) == 2
152161
assert all(uri.host == "origin.test" for uri, _ in handshakes.attempts)
@@ -164,7 +173,7 @@ async def test_async_websocket_reconnect_redirects(monkeypatch: pytest.MonkeyPat
164173
api_key="fake-key", websocket_base_url="wss://origin.test", http_client=async_http_client()
165174
) as client:
166175
manager = resource(client, name).connect(
167-
**options(name), on_reconnecting=reconnect, initial_delay=0, max_retries=1
176+
**options(client, name), on_reconnecting=reconnect, initial_delay=0, max_retries=1
168177
)
169178
async with manager as connection:
170179
connection._send_queue.enqueue("fake-queued-message")
@@ -212,12 +221,12 @@ def test_sync_websocket_connector_is_unchanged(monkeypatch: pytest.MonkeyPatch,
212221
websocket_base_url="wss://origin.test",
213222
http_client=httpx2.Client(transport=httpx2.MockTransport(unexpected_http)),
214223
) as client:
215-
with resource(client, name).connect(**options(name)):
224+
with resource(client, name).connect(**options(client, name)):
216225
pass
217226
error = redirect_error("wss://other.test/final")
218227
connect_mock.side_effect = error
219228
with pytest.raises(InvalidStatus) as caught:
220-
with resource(client, name).connect(**options(name)):
229+
with resource(client, name).connect(**options(client, name)):
221230
pytest.fail("Unexpected connection")
222231
assert caught.value is error
223232
assert connect_mock.call_count == 2

0 commit comments

Comments
 (0)