|
| 1 | +import httpx |
| 2 | +import pytest |
| 3 | + |
| 4 | +from kernel import Kernel, AsyncKernel, NotFoundError |
| 5 | + |
| 6 | + |
| 7 | +def test_acquire_returns_browser() -> None: |
| 8 | + browser = { |
| 9 | + "cdp_ws_url": "wss://example.test/cdp", |
| 10 | + "created_at": "2026-10-09T00:00:00Z", |
| 11 | + "headless": True, |
| 12 | + "memory": "1GiB", |
| 13 | + "region": "us-east", |
| 14 | + "session_id": "session-1", |
| 15 | + "stealth": False, |
| 16 | + "webdriver_ws_url": "wss://example.test/webdriver", |
| 17 | + } |
| 18 | + with Kernel( |
| 19 | + api_key="test", |
| 20 | + http_client=httpx.Client(transport=httpx.MockTransport(lambda _request: httpx.Response(200, json=browser))), |
| 21 | + ) as client: |
| 22 | + result = client.browser_pools.acquire("pool") |
| 23 | + assert result is not None |
| 24 | + assert result.session_id == "session-1" |
| 25 | + |
| 26 | + |
| 27 | +def test_acquire_returns_none_on_poll_timeout() -> None: |
| 28 | + calls = 0 |
| 29 | + |
| 30 | + def handle(_request: httpx.Request) -> httpx.Response: |
| 31 | + nonlocal calls |
| 32 | + calls += 1 |
| 33 | + return httpx.Response(204) |
| 34 | + |
| 35 | + with Kernel(api_key="test", http_client=httpx.Client(transport=httpx.MockTransport(handle))) as client: |
| 36 | + assert client.browser_pools.acquire("pool") is None |
| 37 | + response = client.browser_pools.with_raw_response.acquire("pool") |
| 38 | + assert response.status_code == 204 |
| 39 | + assert response.parse() is None |
| 40 | + assert calls == 2 |
| 41 | + |
| 42 | + |
| 43 | +async def test_async_acquire_returns_none_on_poll_timeout() -> None: |
| 44 | + async with AsyncKernel( |
| 45 | + api_key="test", |
| 46 | + http_client=httpx.AsyncClient(transport=httpx.MockTransport(lambda _request: httpx.Response(204))), |
| 47 | + ) as client: |
| 48 | + assert await client.browser_pools.acquire("pool") is None |
| 49 | + |
| 50 | + |
| 51 | +def test_acquire_still_raises_not_found() -> None: |
| 52 | + with Kernel( |
| 53 | + api_key="test", |
| 54 | + http_client=httpx.Client(transport=httpx.MockTransport(lambda _request: httpx.Response(404, json={}))), |
| 55 | + ) as client: |
| 56 | + with pytest.raises(NotFoundError): |
| 57 | + client.browser_pools.acquire("missing") |
0 commit comments