|
| 1 | +# Copyright (c) The OGX Contributors. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the terms described in the LICENSE file in |
| 5 | +# the root directory of this source tree. |
| 6 | + |
| 7 | +import json |
| 8 | +from unittest.mock import AsyncMock |
| 9 | + |
| 10 | +from fastapi import FastAPI |
| 11 | +from starlette.testclient import TestClient |
| 12 | + |
| 13 | +from ogx.core.server.fastapi_router_registry import build_fastapi_router |
| 14 | +from ogx_api import Api, Responses |
| 15 | +from ogx_api.openai_responses import ( |
| 16 | + OpenAIResponseObject, |
| 17 | + OpenAIResponseObjectStreamResponseCompleted, |
| 18 | + OpenAIResponseObjectStreamResponseCreated, |
| 19 | +) |
| 20 | + |
| 21 | +# WebSocket transport tests |
| 22 | + |
| 23 | + |
| 24 | +def _ws_app(impl: Responses) -> FastAPI: |
| 25 | + app = FastAPI() |
| 26 | + router = build_fastapi_router(Api.responses, impl) |
| 27 | + assert router is not None |
| 28 | + app.include_router(router) |
| 29 | + return app |
| 30 | + |
| 31 | + |
| 32 | +def _ws_response(response_id: str, status: str = "completed") -> OpenAIResponseObject: |
| 33 | + return OpenAIResponseObject( |
| 34 | + id=response_id, |
| 35 | + created_at=1234567890, |
| 36 | + model="test-model", |
| 37 | + object="response", |
| 38 | + output=[], |
| 39 | + status=status, |
| 40 | + store=False, |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def test_websocket_invalid_json_returns_error_envelope(): |
| 45 | + impl = AsyncMock(spec=Responses) |
| 46 | + client = TestClient(_ws_app(impl)) |
| 47 | + |
| 48 | + with client.websocket_connect("/v1/responses") as ws: |
| 49 | + ws.send_text("this is not json") |
| 50 | + event = ws.receive_json() |
| 51 | + |
| 52 | + assert event["type"] == "error" |
| 53 | + assert event["error"]["code"] == "invalid_json" |
| 54 | + impl.create_openai_response.assert_not_called() |
| 55 | + |
| 56 | + |
| 57 | +def test_websocket_validation_error_returns_invalid_request(): |
| 58 | + impl = AsyncMock(spec=Responses) |
| 59 | + client = TestClient(_ws_app(impl)) |
| 60 | + |
| 61 | + with client.websocket_connect("/v1/responses") as ws: |
| 62 | + # Missing required model/input fields. |
| 63 | + ws.send_text(json.dumps({"type": "response.create"})) |
| 64 | + event = ws.receive_json() |
| 65 | + |
| 66 | + assert event["type"] == "error" |
| 67 | + assert event["error"]["code"] == "invalid_request" |
| 68 | + impl.create_openai_response.assert_not_called() |
| 69 | + |
| 70 | + |
| 71 | +def test_websocket_unknown_previous_response_not_found(): |
| 72 | + impl = AsyncMock(spec=Responses) |
| 73 | + client = TestClient(_ws_app(impl)) |
| 74 | + |
| 75 | + with client.websocket_connect("/v1/responses") as ws: |
| 76 | + ws.send_text( |
| 77 | + json.dumps( |
| 78 | + { |
| 79 | + "type": "response.create", |
| 80 | + "model": "test", |
| 81 | + "store": False, |
| 82 | + "previous_response_id": "resp_does_not_exist", |
| 83 | + "input": "continue please", |
| 84 | + } |
| 85 | + ) |
| 86 | + ) |
| 87 | + event = ws.receive_json() |
| 88 | + |
| 89 | + assert event["type"] == "error" |
| 90 | + assert event["status"] == 404 |
| 91 | + assert event["error"]["code"] == "previous_response_not_found" |
| 92 | + # No inference is attempted for a connection-local cache miss. |
| 93 | + impl.create_openai_response.assert_not_called() |
| 94 | + |
| 95 | + |
| 96 | +def test_websocket_impl_exception_returns_server_error(): |
| 97 | + impl = AsyncMock(spec=Responses) |
| 98 | + impl.create_openai_response.side_effect = RuntimeError("boom") |
| 99 | + client = TestClient(_ws_app(impl)) |
| 100 | + |
| 101 | + with client.websocket_connect("/v1/responses") as ws: |
| 102 | + ws.send_text(json.dumps({"type": "response.create", "model": "test", "input": "hi"})) |
| 103 | + event = ws.receive_json() |
| 104 | + |
| 105 | + assert event["type"] == "error" |
| 106 | + assert event["error"]["code"] == "server_error" |
| 107 | + |
| 108 | + |
| 109 | +def test_websocket_streams_events_until_terminal(): |
| 110 | + impl = AsyncMock(spec=Responses) |
| 111 | + |
| 112 | + async def _stream(): |
| 113 | + yield OpenAIResponseObjectStreamResponseCreated(response=_ws_response("resp_ws_1"), sequence_number=0) |
| 114 | + yield OpenAIResponseObjectStreamResponseCompleted(response=_ws_response("resp_ws_1"), sequence_number=1) |
| 115 | + |
| 116 | + impl.create_openai_response.return_value = _stream() |
| 117 | + client = TestClient(_ws_app(impl)) |
| 118 | + |
| 119 | + with client.websocket_connect("/v1/responses") as ws: |
| 120 | + ws.send_text(json.dumps({"type": "response.create", "model": "test", "input": "hi"})) |
| 121 | + first = ws.receive_json() |
| 122 | + second = ws.receive_json() |
| 123 | + |
| 124 | + assert first["type"] == "response.created" |
| 125 | + assert second["type"] == "response.completed" |
| 126 | + # The HTTP-only streaming discriminator must not leak into the request. |
| 127 | + sent_request = impl.create_openai_response.call_args.args[0] |
| 128 | + assert sent_request.stream is True |
| 129 | + assert not hasattr(sent_request, "type") or "type" not in sent_request.model_dump() |
0 commit comments