Implement S23 runtime health slice
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import io
|
||||
import json
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock
|
||||
|
||||
from routes import cortex_os_runtime_health as route
|
||||
|
||||
|
||||
class FakeHandler:
|
||||
def __init__(self, command="GET"):
|
||||
self.command = command
|
||||
self.status_code = None
|
||||
self.headers = []
|
||||
self.wfile = io.BytesIO()
|
||||
|
||||
def send_response(self, status_code):
|
||||
self.status_code = status_code
|
||||
|
||||
def send_header(self, name, value):
|
||||
self.headers.append((name, value))
|
||||
|
||||
def end_headers(self):
|
||||
return None
|
||||
|
||||
def payload(self):
|
||||
return json.loads(self.wfile.getvalue().decode("utf-8"))
|
||||
|
||||
|
||||
def test_register_wires_get_runtime_health_route():
|
||||
api = Mock()
|
||||
api.logger.return_value = Mock()
|
||||
|
||||
route.register(api)
|
||||
|
||||
api.register_route.assert_called_once_with(
|
||||
"/api/cortex-os/runtime-health",
|
||||
"GET",
|
||||
route._handle_runtime_health,
|
||||
)
|
||||
|
||||
|
||||
def test_get_returns_runtime_health_envelope():
|
||||
handler = FakeHandler()
|
||||
|
||||
assert route._handle_runtime_health(handler, SimpleNamespace(query="")) is True
|
||||
|
||||
payload = handler.payload()
|
||||
assert handler.status_code == 200
|
||||
assert payload["ok"] is True
|
||||
assert payload["result"]["contract_id"] == "runtime-health/v0.1"
|
||||
assert payload["result"]["status"] == "unknown"
|
||||
assert payload["result"]["source_trace"]["live_probe"] is False
|
||||
assert payload["result"]["authority"]["runtime_state_mutation"] is False
|
||||
|
||||
|
||||
def test_host_signal_mapping_is_closed_and_redacted():
|
||||
payload = route.runtime_health_payload(
|
||||
{
|
||||
"health": "healthy",
|
||||
"agent_health": "degraded",
|
||||
"dashboard_status": "unavailable",
|
||||
"health_detail": "http://localhost:8000/raw",
|
||||
"agent_health_detail": "/home/svrnty/private",
|
||||
"dashboard_status_detail": "token=abc123",
|
||||
}
|
||||
)
|
||||
|
||||
assert payload["status"] == "unavailable"
|
||||
assert {signal["detail"] for signal in payload["signals"]} == {"redacted"}
|
||||
assert payload["source_trace"]["host_surfaces"] == [
|
||||
"health",
|
||||
"agent_health",
|
||||
"dashboard_status",
|
||||
]
|
||||
|
||||
|
||||
def test_non_get_and_query_targets_are_rejected():
|
||||
post_handler = FakeHandler(command="POST")
|
||||
query_handler = FakeHandler()
|
||||
|
||||
route._handle_runtime_health(post_handler, SimpleNamespace(query=""))
|
||||
route._handle_runtime_health(query_handler, SimpleNamespace(query="target=raw"))
|
||||
|
||||
assert post_handler.status_code == 405
|
||||
assert post_handler.payload()["ok"] is False
|
||||
assert query_handler.status_code == 400
|
||||
assert query_handler.payload()["error"]["code"] == "query_not_allowed"
|
||||
@@ -0,0 +1,45 @@
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
JS = ROOT / "static" / "cortex-os" / "runtime-health" / "runtime_health.js"
|
||||
CSS = ROOT / "static" / "cortex-os" / "runtime-health" / "runtime_health.css"
|
||||
|
||||
|
||||
def test_runtime_health_assets_exist_and_render_all_states():
|
||||
js = JS.read_text(encoding="utf-8")
|
||||
css = CSS.read_text(encoding="utf-8")
|
||||
|
||||
assert "/api/cortex-os/runtime-health" in js
|
||||
for state in ["healthy", "degraded", "unavailable", "unknown", "loading", "error", "redacted"]:
|
||||
assert state in js
|
||||
assert state in css
|
||||
|
||||
|
||||
def test_runtime_health_display_has_no_hidden_write_surface():
|
||||
js = JS.read_text(encoding="utf-8")
|
||||
forbidden = [
|
||||
"localStorage",
|
||||
"sessionStorage",
|
||||
"document.cookie",
|
||||
"<form",
|
||||
".submit(",
|
||||
"POST",
|
||||
"PUT",
|
||||
"PATCH",
|
||||
"DELETE",
|
||||
"/api/gateway/status",
|
||||
"/api/tools",
|
||||
"/api/mcp",
|
||||
]
|
||||
|
||||
for snippet in forbidden:
|
||||
assert snippet not in js
|
||||
|
||||
|
||||
def test_runtime_health_css_is_scoped_to_panel():
|
||||
css = CSS.read_text(encoding="utf-8")
|
||||
|
||||
assert ".cortex-os-runtime-health" in css
|
||||
assert "body {" not in css
|
||||
assert "#app" not in css
|
||||
Reference in New Issue
Block a user