from __future__ import annotations

from tests.harness.test_runner import TestCase
from .common import assert_success


SUITE = "test_health"


def _status_returns_200(ctx):
    resp = ctx.client.status()
    assert_success(resp)
    return True, "status success", {"keys": list(resp.keys())}


def _status_has_fields(ctx):
    resp = ctx.client.status()
    for key in ["name", "version", "pubkey", "relay_count"]:
        assert key in resp, f"missing field: {key}"
    return True, "required fields present", {}


def _context_current(ctx):
    resp = ctx.client.context_current()
    assert_success(resp)
    msgs = resp.get("messages", [])
    assert isinstance(msgs, list), "messages must be a list"
    assert resp.get("total_chars", 0) >= 0, "missing/invalid total_chars"
    return True, "context current ok", {"message_count": len(msgs)}


def _context_parts(ctx):
    resp = ctx.client.context_parts()
    assert_success(resp)
    parts = resp.get("parts", [])
    names = [p.get("name") for p in parts if isinstance(p, dict)]
    assert "system_prompt" in names, "system_prompt part missing"
    return True, "context parts ok", {"parts": names}


def get_tests():
    return [
        TestCase(SUITE, "status_returns_200", "GET /api/status success", _status_returns_200),
        TestCase(SUITE, "status_has_fields", "GET /api/status has required fields", _status_has_fields),
        TestCase(SUITE, "context_current_returns_messages", "GET /api/context/current has messages", _context_current),
        TestCase(SUITE, "context_parts_has_system_prompt", "GET /api/context/parts has system_prompt", _context_parts),
    ]
