Skip to content

Commit 0524e21

Browse files
committed
fix(bay): map gull health from payload status
1 parent e413f24 commit 0524e21

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

pkgs/bay/app/adapters/gull.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,16 @@ async def health(self) -> bool:
155155
f"{self._base_url}/health",
156156
timeout=5.0,
157157
)
158-
return response.status_code == 200
158+
if response.status_code != 200:
159+
return False
160+
161+
try:
162+
payload = response.json()
163+
except Exception:
164+
return False
165+
166+
status = payload.get("status") if isinstance(payload, dict) else None
167+
return status in {"healthy", "degraded"}
159168
except Exception:
160169
return False
161170

pkgs/bay/tests/unit/adapters/test_gull_adapter.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,45 @@ async def request(self, *args, **kwargs):
127127
adapter = GullAdapter("http://gull")
128128
with pytest.raises(RequestTimeoutError):
129129
await adapter.get_meta()
130+
131+
132+
@pytest.mark.asyncio
133+
@pytest.mark.parametrize(
134+
("status_value", "expected"),
135+
[
136+
("healthy", True),
137+
("degraded", True),
138+
("unhealthy", False),
139+
("unknown", False),
140+
],
141+
)
142+
async def test_health_maps_payload_status(
143+
monkeypatch: pytest.MonkeyPatch, status_value: str, expected: bool
144+
):
145+
async def handler(_request: httpx.Request) -> httpx.Response:
146+
return httpx.Response(
147+
200,
148+
json={"status": status_value, "browser_active": False, "session": "s"},
149+
)
150+
151+
client = httpx.AsyncClient(transport=httpx.MockTransport(handler), base_url="http://gull")
152+
monkeypatch.setattr(gull_mod, "_get_shared_client", lambda: client)
153+
154+
adapter = GullAdapter("http://gull")
155+
assert await adapter.health() is expected
156+
157+
await client.aclose()
158+
159+
160+
@pytest.mark.asyncio
161+
async def test_health_returns_false_on_malformed_payload(monkeypatch: pytest.MonkeyPatch):
162+
async def handler(_request: httpx.Request) -> httpx.Response:
163+
return httpx.Response(200, content=b"not-json")
164+
165+
client = httpx.AsyncClient(transport=httpx.MockTransport(handler), base_url="http://gull")
166+
monkeypatch.setattr(gull_mod, "_get_shared_client", lambda: client)
167+
168+
adapter = GullAdapter("http://gull")
169+
assert await adapter.health() is False
170+
171+
await client.aclose()

0 commit comments

Comments
 (0)