Skip to content

Commit f7bfcb0

Browse files
committed
cp dines
Made-with: Cursor
1 parent eca07b2 commit f7bfcb0

1 file changed

Lines changed: 44 additions & 36 deletions

File tree

tests/test_command_id.py

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
from __future__ import annotations
88

99
import json
10+
from typing import cast
1011

1112
import httpx
1213
import pytest
13-
import respx
14+
from respx import Route, MockRouter
1415

15-
from runloop_api_client import AsyncRunloop, Runloop
16+
from runloop_api_client import Runloop, AsyncRunloop
1617

17-
BASE = "http://localhost"
18-
EXECUTE_PATTERN = f"{BASE}/v1/devboxes/dbx_test/execute"
18+
base_url = "http://127.0.0.1:4010"
19+
EXECUTE_PATH = "/v1/devboxes/dbx_test/execute"
1920

2021
STUB_RESPONSE = {
2122
"execution_id": "exec_1",
@@ -28,59 +29,66 @@
2829
}
2930

3031

31-
class TestCommandIdUniqueness:
32-
"""Every call without an explicit command_id must produce a distinct UUID."""
32+
def _get_command_ids(route: Route) -> list[str]:
33+
return [
34+
json.loads(cast(bytes, call.request.content))["command_id"] # type: ignore[union-attr]
35+
for call in route.calls # type: ignore[union-attr]
36+
]
3337

34-
@respx.mock
35-
def test_sync_execute_generates_unique_ids(self) -> None:
36-
route = respx.post(EXECUTE_PATTERN).mock(
37-
return_value=httpx.Response(200, json=STUB_RESPONSE)
38-
)
39-
client = Runloop(base_url=BASE, bearer_token="test")
38+
39+
def _get_request_body(route: Route, index: int = 0) -> dict[str, object]:
40+
return json.loads(cast(bytes, route.calls[index].request.content)) # type: ignore[union-attr]
41+
42+
43+
class TestCommandIdGeneration:
44+
"""command_id must be a fresh UUIDv7 per call when not explicitly provided."""
45+
46+
@pytest.mark.respx(base_url=base_url)
47+
def test_execute_generates_unique_command_ids(self, respx_mock: MockRouter) -> None:
48+
route = respx_mock.post(EXECUTE_PATH).mock(return_value=httpx.Response(200, json=STUB_RESPONSE))
49+
client = Runloop(base_url=base_url, bearer_token="test")
4050

4151
for _ in range(5):
4252
client.devboxes.execute(id="dbx_test", command="echo hi")
4353

4454
assert route.call_count == 5
45-
ids = [json.loads(call.request.content)["command_id"] for call in route.calls]
46-
assert len(set(ids)) == 5, f"All command_ids should be unique, got: {ids}"
55+
ids = _get_command_ids(route)
56+
assert len(set(ids)) == 5, f"command_ids should all be unique, got: {ids}"
4757

48-
@respx.mock
49-
def test_sync_execute_respects_explicit_id(self) -> None:
50-
route = respx.post(EXECUTE_PATTERN).mock(
51-
return_value=httpx.Response(200, json=STUB_RESPONSE)
52-
)
53-
client = Runloop(base_url=BASE, bearer_token="test")
58+
@pytest.mark.respx(base_url=base_url)
59+
def test_execute_preserves_explicit_command_id(self, respx_mock: MockRouter) -> None:
60+
route = respx_mock.post(EXECUTE_PATH).mock(return_value=httpx.Response(200, json=STUB_RESPONSE))
61+
client = Runloop(base_url=base_url, bearer_token="test")
5462

5563
client.devboxes.execute(id="dbx_test", command="echo hi", command_id="my-custom-id")
5664

57-
body = json.loads(route.calls[0].request.content)
65+
body = _get_request_body(route)
5866
assert body["command_id"] == "my-custom-id"
5967

60-
@respx.mock
68+
69+
class TestAsyncCommandIdGeneration:
70+
"""Async variant: command_id must be a fresh UUIDv7 per call when not explicitly provided."""
71+
72+
@pytest.mark.respx(base_url=base_url)
6173
@pytest.mark.asyncio
62-
async def test_async_execute_generates_unique_ids(self) -> None:
63-
route = respx.post(EXECUTE_PATTERN).mock(
64-
return_value=httpx.Response(200, json=STUB_RESPONSE)
65-
)
66-
client = AsyncRunloop(base_url=BASE, bearer_token="test")
74+
async def test_execute_generates_unique_command_ids(self, respx_mock: MockRouter) -> None:
75+
route = respx_mock.post(EXECUTE_PATH).mock(return_value=httpx.Response(200, json=STUB_RESPONSE))
76+
client = AsyncRunloop(base_url=base_url, bearer_token="test")
6777

6878
for _ in range(5):
6979
await client.devboxes.execute(id="dbx_test", command="echo hi")
7080

7181
assert route.call_count == 5
72-
ids = [json.loads(call.request.content)["command_id"] for call in route.calls]
73-
assert len(set(ids)) == 5, f"All command_ids should be unique, got: {ids}"
82+
ids = _get_command_ids(route)
83+
assert len(set(ids)) == 5, f"command_ids should all be unique, got: {ids}"
7484

75-
@respx.mock
85+
@pytest.mark.respx(base_url=base_url)
7686
@pytest.mark.asyncio
77-
async def test_async_execute_respects_explicit_id(self) -> None:
78-
route = respx.post(EXECUTE_PATTERN).mock(
79-
return_value=httpx.Response(200, json=STUB_RESPONSE)
80-
)
81-
client = AsyncRunloop(base_url=BASE, bearer_token="test")
87+
async def test_execute_preserves_explicit_command_id(self, respx_mock: MockRouter) -> None:
88+
route = respx_mock.post(EXECUTE_PATH).mock(return_value=httpx.Response(200, json=STUB_RESPONSE))
89+
client = AsyncRunloop(base_url=base_url, bearer_token="test")
8290

8391
await client.devboxes.execute(id="dbx_test", command="echo hi", command_id="my-custom-id")
8492

85-
body = json.loads(route.calls[0].request.content)
93+
body = _get_request_body(route)
8694
assert body["command_id"] == "my-custom-id"

0 commit comments

Comments
 (0)