|
| 1 | +"""Tests for command_id default generation in execute / execute_and_await_completion. |
| 2 | +
|
| 3 | +Verifies that each call generates a fresh UUIDv7 rather than reusing a frozen |
| 4 | +default (the bug fixed in this change). |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import json |
| 10 | + |
| 11 | +import httpx |
| 12 | +import pytest |
| 13 | +import respx |
| 14 | + |
| 15 | +from runloop_api_client import AsyncRunloop, Runloop |
| 16 | + |
| 17 | +BASE = "http://localhost" |
| 18 | +EXECUTE_PATTERN = f"{BASE}/v1/devboxes/dbx_test/execute" |
| 19 | + |
| 20 | +STUB_RESPONSE = { |
| 21 | + "execution_id": "exec_1", |
| 22 | + "command_id": "ignored", |
| 23 | + "devbox_id": "dbx_test", |
| 24 | + "status": "completed", |
| 25 | + "exit_status": 0, |
| 26 | + "stdout": "", |
| 27 | + "stderr": "", |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +class TestCommandIdUniqueness: |
| 32 | + """Every call without an explicit command_id must produce a distinct UUID.""" |
| 33 | + |
| 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") |
| 40 | + |
| 41 | + for _ in range(5): |
| 42 | + client.devboxes.execute(id="dbx_test", command="echo hi") |
| 43 | + |
| 44 | + 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}" |
| 47 | + |
| 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") |
| 54 | + |
| 55 | + client.devboxes.execute(id="dbx_test", command="echo hi", command_id="my-custom-id") |
| 56 | + |
| 57 | + body = json.loads(route.calls[0].request.content) |
| 58 | + assert body["command_id"] == "my-custom-id" |
| 59 | + |
| 60 | + @respx.mock |
| 61 | + @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") |
| 67 | + |
| 68 | + for _ in range(5): |
| 69 | + await client.devboxes.execute(id="dbx_test", command="echo hi") |
| 70 | + |
| 71 | + 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}" |
| 74 | + |
| 75 | + @respx.mock |
| 76 | + @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") |
| 82 | + |
| 83 | + await client.devboxes.execute(id="dbx_test", command="echo hi", command_id="my-custom-id") |
| 84 | + |
| 85 | + body = json.loads(route.calls[0].request.content) |
| 86 | + assert body["command_id"] == "my-custom-id" |
0 commit comments