"""task-2566 — runner health marker schema + queued/stuck simulation tests.

Validates that runner_health_check.sh produces a marker conforming to the
expected schema, and that the queued-stuck detection branch fires when
synthetic queued workflows are present.
"""
from __future__ import annotations

import json
import os
from pathlib import Path

import pytest

REPO_ROOT = Path(__file__).resolve().parents[2]
HEALTH_MARKER = REPO_ROOT / "memory" / "events" / "task-2566.runner.health.json"
QUEUED_MARKER = REPO_ROOT / "memory" / "events" / "task-2566.runner.queued"


REQUIRED_TOP_KEYS = {
    "task_id",
    "checked_at",
    "runner_name",
    "repo",
    "systemd",
    "github",
    "queued_workflows",
}
REQUIRED_SYSTEMD_KEYS = {"service", "state", "scope"}
REQUIRED_GITHUB_KEYS = {"status", "busy", "labels"}
REQUIRED_QUEUED_KEYS = {"count", "items"}


def _load_marker():
    if not HEALTH_MARKER.exists():
        pytest.skip(f"health marker not yet produced: {HEALTH_MARKER}")
    return json.loads(HEALTH_MARKER.read_text())


def test_health_marker_top_level_schema():
    marker = _load_marker()
    missing = REQUIRED_TOP_KEYS - set(marker)
    assert not missing, f"missing top keys: {missing}"
    assert marker["task_id"] == "task-2566"
    assert marker["runner_name"], "runner_name empty"
    assert marker["repo"], "repo empty"


def test_health_marker_systemd_block():
    marker = _load_marker()
    sysd = marker["systemd"]
    missing = REQUIRED_SYSTEMD_KEYS - set(sysd)
    assert not missing, f"missing systemd keys: {missing}"
    assert sysd["scope"] in ("user", "system")


def test_health_marker_github_block():
    marker = _load_marker()
    gh = marker["github"]
    missing = REQUIRED_GITHUB_KEYS - set(gh)
    assert not missing, f"missing github keys: {missing}"
    assert isinstance(gh["labels"], list)
    # Required labels from task-2566 §5
    required_labels_lower = {"self-hosted", "linux", "anu-ci"}
    actual_lower = {str(label).lower() for label in gh["labels"]}
    assert required_labels_lower.issubset(actual_lower), (
        f"missing required labels: required={required_labels_lower}, actual={actual_lower}"
    )


def test_health_marker_queued_block():
    marker = _load_marker()
    q = marker["queued_workflows"]
    missing = REQUIRED_QUEUED_KEYS - set(q)
    assert not missing, f"missing queued keys: {missing}"
    assert isinstance(q["count"], int)
    assert isinstance(q["items"], list)
    assert q["count"] == len(q["items"])


def test_queued_marker_consistency_with_health():
    """If health marker reports queued > 0, the .runner.queued marker must exist
    (and vice versa). This wires the stuck-detection branch to disk evidence."""
    marker = _load_marker()
    count = marker["queued_workflows"]["count"]
    if count > 0:
        assert QUEUED_MARKER.exists(), (
            "health reports queued>0 but no .runner.queued marker on disk"
        )
        payload = json.loads(QUEUED_MARKER.read_text())
        assert payload["task_id"] == "task-2566"
        assert payload["stuck_count"] == count
    else:
        # When no queued runs detected, the queued marker should be absent OR
        # contain a stale entry from a previous run — we only require absence
        # when freshly generated, so skip if exists.
        if QUEUED_MARKER.exists():
            payload = json.loads(QUEUED_MARKER.read_text())
            # Stale markers are allowed but must match schema
            assert payload["task_id"] == "task-2566"


def test_required_label_set_matches_workflow_runs_on():
    """task-2566 §5 mandates runs-on: [self-hosted, linux, anu-ci]. The runner
    must have these labels (case-insensitive)."""
    marker = _load_marker()
    actual_lower = {str(label).lower() for label in marker["github"]["labels"]}
    for required in ("self-hosted", "linux", "anu-ci"):
        assert required in actual_lower, f"label {required!r} missing from runner"


def test_runner_health_workflow_file_present():
    wf = REPO_ROOT / ".github" / "workflows" / "runner-health.yml"
    assert wf.exists(), "runner-health workflow missing"
    body = wf.read_text()
    assert "self-hosted" in body and "anu-ci" in body
    # Verify no ubuntu-latest *runner* declaration. Comments may mention the
    # token (e.g. "DO NOT add ubuntu-latest fallback") so we only forbid it
    # on lines that actually declare a runner.
    code_lines = [
        ln.split("#", 1)[0]
        for ln in body.splitlines()
        if "runs-on" in ln or "ubuntu-latest" in ln
    ]
    for ln in code_lines:
        without_comment = ln.split("#", 1)[0]
        assert "ubuntu-latest" not in without_comment, (
            f"ubuntu-latest forbidden in non-comment workflow code: {ln!r}"
        )


def test_bootstrap_script_present_and_executable():
    bs = REPO_ROOT / "scripts" / "runner_bootstrap.sh"
    assert bs.exists()
    assert os.access(bs, os.X_OK), "runner_bootstrap.sh not executable"
