import asyncio import json import unittest from typing import Any from plba.health import ( HealthAggregator, HealthComponent, HealthExpectation, HealthInput, HealthInputInvalid, HealthContributorRegistry, RuntimeHealthInputProvider, ) from plba.http import ControlApi, HttpRoute from plba.runtime import ApplicationLifecycle, ApplicationRuntime, RuntimeContext from plba.runtime.worker import ( ComponentHealth, HealthStatus, WorkerConfig, WorkerDefinition, WorkerRegistry, WorkerSupervisor, ) class StaticHealthProvider: def __init__(self, health_input: HealthInput) -> None: self.health_input = health_input async def read(self) -> HealthInput: return self.health_input class EmptyWorkerConfigReader: def get_worker_config(self, worker_name: str) -> WorkerConfig: return WorkerConfig() class HealthAggregationTests(unittest.IsolatedAsyncioTestCase): async def test_missing_critical_and_noncritical_components_apply_priority(self) -> None: health_input = HealthInput( "service-a", "running", ( HealthExpectation("required", True), HealthExpectation("optional", False), ), ( HealthComponent("degraded", HealthStatus.DEGRADED, True), HealthComponent("optional-bad", HealthStatus.UNHEALTHY, False), ), ) report = await HealthAggregator(StaticHealthProvider(health_input)).aggregate() self.assertEqual(report.status, "unhealthy") self.assertEqual( {item.name: item.status for item in report.components}["required"], HealthStatus.UNHEALTHY, ) self.assertEqual( {item.name: item.status for item in report.components}["optional"], HealthStatus.DEGRADED, ) async def test_noncritical_unhealthy_is_capped_at_degraded(self) -> None: health_input = HealthInput( "service-a", "running", (), (HealthComponent("cache", HealthStatus.UNHEALTHY, False),), ) report = await HealthAggregator(StaticHealthProvider(health_input)).aggregate() self.assertEqual(report.status, "degraded") self.assertEqual(report.components[0].status, HealthStatus.UNHEALTHY) async def test_lifecycle_controls_base_health_and_invalid_inputs_fail(self) -> None: for lifecycle, expected in ( ("new", "unhealthy"), ("starting", "degraded"), ("stopping", "degraded"), ("failed", "unhealthy"), ("stopped", "stopped"), ): report = await HealthAggregator( StaticHealthProvider(HealthInput("service-a", lifecycle, (), ())) ).aggregate() self.assertEqual(report.status, expected) invalid = HealthInput( "service-a", "running", (), (HealthComponent("bad", "unknown", True),) # type: ignore[arg-type] ) with self.assertRaises(HealthInputInvalid): await HealthAggregator(StaticHealthProvider(invalid)).aggregate() async def test_disabled_worker_is_not_expected_before_application_start(self) -> None: registry = WorkerRegistry() async def process() -> None: return None registry.register(WorkerDefinition("disabled", process, critical=True)) class ConfigReader: def get_worker_config(self, worker_name: str) -> WorkerConfig: return WorkerConfig(disabled=True) supervisor = WorkerSupervisor(registry, ConfigReader()) runtime = ApplicationRuntime(RuntimeContext("service-a"), supervisor) provider = RuntimeHealthInputProvider(runtime, supervisor, HealthContributorRegistry()) report = await HealthAggregator(provider).aggregate() self.assertEqual(report.components, ()) self.assertEqual(report.status, "unhealthy") class ControlApiTests(unittest.IsolatedAsyncioTestCase): async def asyncSetUp(self) -> None: self.registry = WorkerRegistry() self.supervisor = WorkerSupervisor(self.registry, EmptyWorkerConfigReader()) self.runtime = ApplicationRuntime(RuntimeContext("service-http"), self.supervisor) provider = RuntimeHealthInputProvider( self.runtime, self.supervisor, HealthContributorRegistry() ) self.health_aggregator = HealthAggregator(provider) self.api = ControlApi(self.runtime, self.health_aggregator) async def test_status_start_health_and_stop_routes(self) -> None: status = await self.request("GET", "/api/v1/runtime/status") self.assertEqual(status[0], 200) self.assertEqual(status[1], {"status": "new"}) started = await self.request("POST", "/api/v1/runtime/start") self.assertEqual(started[0], 200) self.assertEqual(started[1], {"status": "running"}) health = await self.request("GET", "/api/v1/health") self.assertEqual(health[0], 200) self.assertEqual(health[1]["application_id"], "service-http") self.assertEqual(health[1]["status"], "ok") stopped = await self.request("POST", "/api/v1/runtime/stop", b"{}") self.assertEqual(stopped, (200, {"status": "stopped"})) async def test_application_route_uses_control_api_and_receives_path_params(self) -> None: async def get_item(scope, receive, send) -> None: body = json.dumps({"item_id": scope["path_params"]["item_id"]}).encode() await send( { "type": "http.response.start", "status": 200, "headers": [(b"content-type", b"application/json")], } ) await send({"type": "http.response.body", "body": body}) self.api = ControlApi( self.runtime, self.health_aggregator, (HttpRoute("GET", "/api/v1/items/{item_id}", get_item),), ) response = await self.request("GET", "/api/v1/items/sku-42") self.assertEqual(response, (200, {"item_id": "sku-42"})) async def test_router_errors_use_versioned_api_codes(self) -> None: missing = await self.request("GET", "/api/v1/missing") method_not_allowed = await self.request("POST", "/api/v1/health") self.assertEqual(missing[0], 404) self.assertEqual(missing[1]["error"], "HTTP_ROUTE_NOT_FOUND") self.assertEqual(method_not_allowed[0], 405) self.assertEqual(method_not_allowed[1]["error"], "HTTP_METHOD_NOT_ALLOWED") async def test_invalid_stop_request_is_http_400(self) -> None: missing = await self.request("POST", "/api/v1/runtime/stop") malformed = await self.request("POST", "/api/v1/runtime/stop", b"not-json") invalid_timeout = await self.request( "POST", "/api/v1/runtime/stop", b'{"timeout_seconds": 0}' ) null_timeout = await self.request( "POST", "/api/v1/runtime/stop", b'{"timeout_seconds": null}' ) self.assertEqual(missing[0], 400) self.assertEqual(malformed[0], 400) self.assertEqual(invalid_timeout[0], 400) self.assertEqual(null_timeout[0], 400) async def test_timeout_returns_202_while_worker_stop_continues(self) -> None: active = asyncio.Event() release = asyncio.Event() async def process() -> None: active.set() await release.wait() self.registry.register(WorkerDefinition("blocking", process)) # A fresh supervisor/runtime picks up the worker registered above. self.supervisor = WorkerSupervisor( self.registry, type("Reader", (), {"get_worker_config": lambda _self, _name: WorkerConfig(pause_seconds=1)})(), ) self.runtime = ApplicationRuntime(RuntimeContext("service-http"), self.supervisor) provider = RuntimeHealthInputProvider( self.runtime, self.supervisor, HealthContributorRegistry() ) self.health_aggregator = HealthAggregator(provider) self.api = ControlApi(self.runtime, self.health_aggregator) await self.runtime.start() await asyncio.wait_for(active.wait(), timeout=1) result = await self.request( "POST", "/api/v1/runtime/stop", b'{"timeout_seconds": 0.01}' ) self.assertEqual(result, (202, {"status": "stopping"})) release.set() await asyncio.wait_for(self._wait_until_stopped(), timeout=1) async def _wait_until_stopped(self) -> None: while self.runtime.lifecycle is not ApplicationLifecycle.STOPPED: await asyncio.sleep(0) async def request( self, method: str, path: str, body: bytes = b"", query: bytes = b"" ) -> tuple[int, dict[str, Any]]: incoming = [{"type": "http.request", "body": body, "more_body": False}] outgoing: list[dict[str, Any]] = [] async def receive() -> dict[str, Any]: return incoming.pop(0) if incoming else {"type": "http.disconnect"} async def send(message: dict[str, Any]) -> None: outgoing.append(message) await self.api( {"type": "http", "method": method, "path": path, "query_string": query}, receive, send, ) response = json.loads(outgoing[-1]["body"]) return outgoing[0]["status"], response if __name__ == "__main__": unittest.main()