|
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | 15 |
|
16 | | -from contextlib import asynccontextmanager |
17 | | - |
18 | 16 | import pytest |
19 | | -from asgi_lifespan import LifespanManager |
20 | | -from httpx import ASGITransport |
21 | | -from httpx import AsyncClient |
22 | 17 | from httpx_sse import aconnect_sse |
23 | 18 |
|
24 | 19 | from nat.data_models.api_server import ChatRequest |
|
31 | 26 | from nat.data_models.config import Config |
32 | 27 | from nat.data_models.config import GeneralConfig |
33 | 28 | from nat.front_ends.fastapi.fastapi_front_end_config import FastApiFrontEndConfig |
34 | | -from nat.front_ends.fastapi.fastapi_front_end_plugin_worker import FastApiFrontEndPluginWorker |
35 | 29 | from nat.test.functions import EchoFunctionConfig |
36 | 30 | from nat.test.functions import StreamingEchoFunctionConfig |
37 | | - |
38 | | - |
39 | | -@asynccontextmanager |
40 | | -async def _build_client(config: Config, worker_class: type[FastApiFrontEndPluginWorker] = FastApiFrontEndPluginWorker): |
41 | | - """Helper to build test client with proper lifecycle management""" |
42 | | - worker = worker_class(config) |
43 | | - app = worker.build_app() |
44 | | - |
45 | | - async with LifespanManager(app): |
46 | | - async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: |
47 | | - yield client |
| 31 | +from nat.test.utils import build_nat_client |
48 | 32 |
|
49 | 33 |
|
50 | 34 | def test_fastapi_config_openai_api_v1_path_field(): |
@@ -198,7 +182,7 @@ async def test_legacy_vs_openai_v1_mode_endpoints(openai_api_v1_path: str | None |
198 | 182 | workflow=EchoFunctionConfig(use_openai_api=True), |
199 | 183 | ) |
200 | 184 |
|
201 | | - async with _build_client(config) as client: |
| 185 | + async with build_nat_client(config) as client: |
202 | 186 | base_path = "/v1/chat/completions" |
203 | 187 |
|
204 | 188 | if openai_api_v1_path: |
@@ -273,7 +257,7 @@ async def test_openai_compatible_mode_stream_parameter(): |
273 | 257 | workflow=StreamingEchoFunctionConfig(use_openai_api=True), |
274 | 258 | ) |
275 | 259 |
|
276 | | - async with _build_client(config) as client: |
| 260 | + async with build_nat_client(config) as client: |
277 | 261 | base_path = "/v1/chat/completions" |
278 | 262 |
|
279 | 263 | # Test stream=true (should return streaming response) |
@@ -311,7 +295,7 @@ async def test_legacy_non_streaming_response_format(): |
311 | 295 | workflow=EchoFunctionConfig(use_openai_api=True), |
312 | 296 | ) |
313 | 297 |
|
314 | | - async with _build_client(config) as client: |
| 298 | + async with build_nat_client(config) as client: |
315 | 299 | # Send request to legacy OpenAI endpoint |
316 | 300 | response = await client.post("/chat", |
317 | 301 | json={ |
@@ -386,7 +370,7 @@ async def test_legacy_streaming_response_format(): |
386 | 370 | workflow=StreamingEchoFunctionConfig(use_openai_api=True), |
387 | 371 | ) |
388 | 372 |
|
389 | | - async with _build_client(config) as client: |
| 373 | + async with build_nat_client(config) as client: |
390 | 374 | async with aconnect_sse(client, |
391 | 375 | "POST", |
392 | 376 | "/chat/stream", |
@@ -454,7 +438,7 @@ async def test_openai_compatible_non_streaming_response_format(): |
454 | 438 | workflow=EchoFunctionConfig(use_openai_api=True), |
455 | 439 | ) |
456 | 440 |
|
457 | | - async with _build_client(config) as client: |
| 441 | + async with build_nat_client(config) as client: |
458 | 442 | # Send request to actual OpenAI endpoint - this will trigger generate_single_response |
459 | 443 | response = await client.post("/v1/chat/completions", |
460 | 444 | json={ |
@@ -530,7 +514,7 @@ async def test_openai_compatible_streaming_response_format(): |
530 | 514 | workflow=StreamingEchoFunctionConfig(use_openai_api=True), |
531 | 515 | ) |
532 | 516 |
|
533 | | - async with _build_client(config) as client: |
| 517 | + async with build_nat_client(config) as client: |
534 | 518 | async with aconnect_sse(client, |
535 | 519 | "POST", |
536 | 520 | "/v1/chat/completions", |
|
0 commit comments