|
| 1 | +"""Stdin wrappers use generated routing and preserve request configuration.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | + |
| 7 | +import httpx |
| 8 | +import pytest |
| 9 | + |
| 10 | +from runloop_api_client import RunloopError |
| 11 | +from runloop_api_client.sdk import Execution, RunloopSDK, AsyncExecution, AsyncRunloopSDK |
| 12 | +from runloop_api_client.types import DevboxAsyncExecutionDetailView |
| 13 | + |
| 14 | + |
| 15 | +class StdinTransport: |
| 16 | + def __init__(self, success: bool) -> None: |
| 17 | + self.success = success |
| 18 | + self.bodies: list[object] = [] |
| 19 | + |
| 20 | + def __call__(self, request: httpx.Request) -> httpx.Response: |
| 21 | + assert request.method == "POST" |
| 22 | + assert request.url.path == "/v1/devboxes/dbx_test/executions/execution_test/send_std_in" |
| 23 | + assert request.headers["x-request-id"] == "synthetic-idempotency-key" |
| 24 | + assert request.headers["x-probe"] == "probe" |
| 25 | + assert request.extensions["timeout"]["read"] == 12 |
| 26 | + self.bodies.append(json.loads(request.content)) |
| 27 | + return httpx.Response( |
| 28 | + 200, json={"devbox_id": "dbx_test", "execution_id": "execution_test", "success": self.success} |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize("success", [True, False]) |
| 33 | +def test_stdin_generated_routing_and_unsuccessful_delivery(success: bool) -> None: |
| 34 | + transport = StdinTransport(success) |
| 35 | + with RunloopSDK( |
| 36 | + bearer_token="synthetic", |
| 37 | + base_url="https://api.invalid", |
| 38 | + max_retries=0, |
| 39 | + http_client=httpx.Client(transport=httpx.MockTransport(transport)), |
| 40 | + ) as sdk: |
| 41 | + execution = Execution( |
| 42 | + sdk.api, "dbx_test", DevboxAsyncExecutionDetailView.model_construct(execution_id="execution_test") |
| 43 | + ) |
| 44 | + for operation in [ |
| 45 | + lambda: execution.send_std_in( |
| 46 | + "hello\n", extra_headers={"x-probe": "probe"}, idempotency_key="synthetic-idempotency-key", timeout=12 |
| 47 | + ), |
| 48 | + lambda: execution.close_std_in( |
| 49 | + extra_headers={"x-probe": "probe"}, idempotency_key="synthetic-idempotency-key", timeout=12 |
| 50 | + ), |
| 51 | + ]: |
| 52 | + if success: |
| 53 | + assert operation() is None |
| 54 | + else: |
| 55 | + with pytest.raises(RunloopError, match="Failed to send stdin"): |
| 56 | + operation() |
| 57 | + assert transport.bodies == [{"text": "hello\n"}, {"signal": "EOF"}] |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize("success", [True, False]) |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_async_stdin_generated_routing_and_unsuccessful_delivery(success: bool) -> None: |
| 63 | + transport = StdinTransport(success) |
| 64 | + async with AsyncRunloopSDK( |
| 65 | + bearer_token="synthetic", |
| 66 | + base_url="https://api.invalid", |
| 67 | + max_retries=0, |
| 68 | + http_client=httpx.AsyncClient(transport=httpx.MockTransport(transport)), |
| 69 | + ) as sdk: |
| 70 | + execution = AsyncExecution( |
| 71 | + sdk.api, "dbx_test", DevboxAsyncExecutionDetailView.model_construct(execution_id="execution_test") |
| 72 | + ) |
| 73 | + for operation in [ |
| 74 | + lambda: execution.send_std_in( |
| 75 | + "hello\n", extra_headers={"x-probe": "probe"}, idempotency_key="synthetic-idempotency-key", timeout=12 |
| 76 | + ), |
| 77 | + lambda: execution.close_std_in( |
| 78 | + extra_headers={"x-probe": "probe"}, idempotency_key="synthetic-idempotency-key", timeout=12 |
| 79 | + ), |
| 80 | + ]: |
| 81 | + if success: |
| 82 | + assert await operation() is None |
| 83 | + else: |
| 84 | + with pytest.raises(RunloopError, match="Failed to send stdin"): |
| 85 | + await operation() |
| 86 | + assert transport.bodies == [{"text": "hello\n"}, {"signal": "EOF"}] |
0 commit comments