|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +from hotdata.exceptions import ApiException |
| 6 | + |
| 7 | +from hotdata_runtime.client import HotdataClient |
| 8 | +from hotdata_runtime.health import workspace_health_lines |
| 9 | + |
| 10 | + |
| 11 | +def test_workspace_health_ok(): |
| 12 | + client = HotdataClient("k", "ws", host="https://api.hotdata.dev") |
| 13 | + listing = type("L", (), {"connections": [object()]})() |
| 14 | + |
| 15 | + class FakeConnectionsApi: |
| 16 | + def list_connections(self): |
| 17 | + return listing |
| 18 | + |
| 19 | + with patch.object(client, "connections", return_value=FakeConnectionsApi()): |
| 20 | + ok, parts = workspace_health_lines(client) |
| 21 | + assert ok is True |
| 22 | + assert any("reachable" in p for p in parts) |
| 23 | + |
| 24 | + |
| 25 | +def test_workspace_health_ok_includes_sandbox_when_session_set(): |
| 26 | + client = HotdataClient( |
| 27 | + "k", "ws", host="https://api.hotdata.dev", session_id="sb_test" |
| 28 | + ) |
| 29 | + listing = type("L", (), {"connections": [object()]})() |
| 30 | + |
| 31 | + class FakeConnectionsApi: |
| 32 | + def list_connections(self): |
| 33 | + return listing |
| 34 | + |
| 35 | + with patch.object(client, "connections", return_value=FakeConnectionsApi()): |
| 36 | + ok, parts = workspace_health_lines(client) |
| 37 | + assert ok is True |
| 38 | + assert any("sandbox" in p and "sb_test" in p for p in parts) |
| 39 | + |
| 40 | + |
| 41 | +def test_workspace_health_api_error(): |
| 42 | + client = HotdataClient("k", "ws", host="https://api.hotdata.dev") |
| 43 | + |
| 44 | + class Boom: |
| 45 | + def list_connections(self): |
| 46 | + raise ApiException(status=500, reason="nope") |
| 47 | + |
| 48 | + with patch.object(client, "connections", return_value=Boom()): |
| 49 | + ok, parts = workspace_health_lines(client) |
| 50 | + assert ok is False |
| 51 | + assert parts == ["nope"] |
| 52 | + |
| 53 | + |
| 54 | +def test_workspace_health_non_api_error(): |
| 55 | + client = HotdataClient("k", "ws", host="https://api.hotdata.dev") |
| 56 | + |
| 57 | + class Boom: |
| 58 | + def list_connections(self): |
| 59 | + raise OSError("connection refused") |
| 60 | + |
| 61 | + with patch.object(client, "connections", return_value=Boom()): |
| 62 | + ok, parts = workspace_health_lines(client) |
| 63 | + assert ok is False |
| 64 | + assert parts == ["connection refused"] |
0 commit comments