from __future__ import annotations

from fastapi.testclient import TestClient

from jarvis_finance.api.main import create_app
from jarvis_finance.services import system_ops


def test_system_status_endpoint_has_safe_shape() -> None:
    client = TestClient(create_app())
    response = client.get("/api/system/status")
    assert response.status_code == 200
    data = response.json()
    assert data["purpose"] == "system_ops_status_v1"
    assert "backend" in data and "frontend" in data
    assert "secret" not in str(data).lower()


def test_restart_endpoint_uses_allowed_service_action(monkeypatch) -> None:
    called: list[str] = []
    def fake_restart(action: str):
        called.append(action)
        return {"status": "ok", "action": f"restart_{action}", "started_at": "now", "message": "Restart angestoßen."}
    monkeypatch.setattr(system_ops, "restart_system_component", fake_restart)
    import jarvis_finance.api.routers.system as system_router
    monkeypatch.setattr(system_router, "restart_system_component", fake_restart)

    client = TestClient(create_app())
    response = client.post("/api/system/restart-frontend")

    assert response.status_code == 200
    assert response.json()["action"] == "restart_frontend"
    assert called == ["frontend"]
