"""Tests for the Stage F card layout: four info cards, no status header. The dashboard used to open with six status-header tiles *and* four compact cards that restated them (runner state lived in three places at once). Stage F keeps the cards and deletes the tiles: each card is now the single owner of its subject, enriched with what only the tiles used to show (CPU%/RAM/disk with their gauges) plus what neither had (hostname, kernel, CPU model, and the ``vmware-host.service`` row). Health is still never color-only: every gauge is paired with a text level (``ok``/``elevated``/``critical``/``unknown``) from :func:`_resource_levels`. """ from __future__ import annotations import shutil from collections import namedtuple from pathlib import Path from typing import Any import pytest from fastapi.testclient import TestClient import ci_orchestrator.web.app as app_module from ci_orchestrator.config import BackendConfig, CargoCacheConfig, Config, Paths from ci_orchestrator.operations.cache import CacheStatus from ci_orchestrator.operations.host import DiskStatus, HostResources, VmwareModuleHealth from ci_orchestrator.web.app import create_app, get_backend _DiskUsage = namedtuple("_DiskUsage", ["total", "used", "free"]) _RESOURCES = HostResources( cpu_pct=42.0, ncpu=16, mem_total_gb=62.5, mem_used_gb=20.0, uptime_sec=93784, load1=0.5, load5=0.75, load15=1.0, cpu_model="Intel(R) Core(TM) i9-13900K CPU @ 3.00GHz", kernel="7.0.0-28-generic", hostname="ci-host", ) _VMWARE_OK = VmwareModuleHealth( vmmon_loaded=True, vmnet_loaded=True, host_service_active=True, host_service_sub_state="running", ) _CACHE = CacheStatus( root=Path("/var/lib/ci/cache/cargo"), total_bytes=1024, entry_count=7, oldest_age_days=4, retention_days=30, ) def _make_config(root: Path) -> Config: return Config( paths=Paths( root=root, templates=root / "templates", build_vms=root / "build-vms", artifacts=root / "artifacts", keys=root / "keys", ), backend=BackendConfig(), cargo_cache=CargoCacheConfig( enabled=True, root=root / "cache" / "cargo", retention_days=30 ), ) class _FakeBackend: def list_running(self) -> list[Any]: return [] def get_ip(self, handle: Any, timeout: float = 0.0) -> str | None: return None def _client(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> TestClient: gb = 1024**3 monkeypatch.setattr( shutil, "disk_usage", lambda _p: _DiskUsage(total=100 * gb, used=40 * gb, free=60 * gb) ) monkeypatch.setattr(shutil, "which", lambda _name: None) monkeypatch.setattr(app_module, "host_resources", lambda: _RESOURCES) monkeypatch.setattr(app_module, "vmware_module_health", lambda: _VMWARE_OK) app = create_app(_make_config(tmp_path)) app.dependency_overrides[get_backend] = lambda: _FakeBackend() return TestClient(app) # ── _resource_levels: thresholds behind the card gauges ────────────────── _DISK_OK = DiskStatus(path=Path("/"), total_gb=100.0, free_gb=60.0, used_pct=40.0) _DISK_ELEVATED = DiskStatus(path=Path("/"), total_gb=100.0, free_gb=25.0, used_pct=75.0) _DISK_CRITICAL = DiskStatus(path=Path("/"), total_gb=100.0, free_gb=5.0, used_pct=90.0) def test_resource_levels_without_a_snapshot_is_unknown_not_false_green() -> None: levels = app_module._resource_levels(resources=None, disk=None) assert levels.cpu_class == "tile-unknown" assert levels.cpu_label == "unknown" assert levels.mem_class == "tile-unknown" assert levels.disk_class == "tile-unknown" assert levels.mem_pct is None def test_resource_levels_healthy_snapshot_is_ok() -> None: levels = app_module._resource_levels(resources=_RESOURCES, disk=_DISK_OK) assert (levels.cpu_class, levels.cpu_label) == ("tile-ok", "ok") assert (levels.mem_class, levels.mem_label) == ("tile-ok", "ok") assert (levels.disk_class, levels.disk_label) == ("tile-ok", "ok") assert levels.mem_pct == pytest.approx(32.0) def test_resource_levels_disk_above_elevated_is_warn() -> None: levels = app_module._resource_levels(resources=_RESOURCES, disk=_DISK_ELEVATED) assert (levels.disk_class, levels.disk_label) == ("tile-warn", "elevated") def test_resource_levels_disk_above_critical_is_bad() -> None: levels = app_module._resource_levels(resources=_RESOURCES, disk=_DISK_CRITICAL) assert (levels.disk_class, levels.disk_label) == ("tile-bad", "critical") def test_resource_levels_busy_cpu_and_full_memory_are_bad() -> None: busy = HostResources( cpu_pct=95.0, ncpu=16, mem_total_gb=62.5, mem_used_gb=61.0, uptime_sec=1, load1=0.0, load5=0.0, load15=0.0, ) levels = app_module._resource_levels(resources=busy, disk=_DISK_OK) assert (levels.cpu_class, levels.cpu_label) == ("tile-bad", "critical") assert (levels.mem_class, levels.mem_label) == ("tile-bad", "critical") # ── the status header is gone ──────────────────────────────────────────── def test_dashboard_has_no_status_header(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: resp = _client(tmp_path, monkeypatch).get("/") assert resp.status_code == 200 assert 'id="status-header"' not in resp.text assert "stat-tile" not in resp.text @pytest.mark.parametrize("route", ["/status-header", "/platform"]) def test_retired_routes_are_gone( route: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: assert _client(tmp_path, monkeypatch).get(route).status_code == 404 def test_all_four_info_cards_share_the_same_dl_grammar( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: """Host and Cargo cache were always a