from jarvis_gateway.adapters.base import BaseAdapter
from jarvis_gateway.contracts import ModuleStatus
from jarvis_gateway.services.overview_service import OverviewService


class BrokenAdapter(BaseAdapter):
    module_id = "finance"
    title = "Broken Finance"

    def get_snapshot(self):  # type: ignore[no-untyped-def]
        raise RuntimeError("boom /home/agent/jarvis_runtime amount_chf")


def test_overview_limits_and_status_aggregation() -> None:
    overview = OverviewService.mock().build_overview()
    assert len(overview.modules) <= 5
    assert len(overview.attention_items) <= 5
    assert all(len(module.kpis) <= 3 for module in overview.modules)
    assert overview.status == ModuleStatus.ATTENTION


def test_adapter_error_becomes_degraded_snapshot_not_crash() -> None:
    overview = OverviewService([BrokenAdapter()]).build_overview()
    assert len(overview.modules) == 1
    module = overview.modules[0]
    assert module.status == ModuleStatus.DEGRADED
    assert module.degraded_reason == "adapter_error"
    assert "boom" in module.source_health.last_error_redacted
    assert "/home/agent" not in module.source_health.last_error_redacted
    assert "amount_chf" not in module.source_health.last_error_redacted
