"""Tests for scripts/collect_leo_restart_survival_proof.py."""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO_ROOT / "scripts"))

import collect_leo_restart_survival_proof as restart_proof  # noqa: E402


COUNTS = {
    "public.claims": 1837,
    "public.sources": 4145,
    "public.claim_evidence": 4670,
    "public.claim_edges": 4916,
    "kb_stage.kb_proposals": 26,
}


def args(tmp_path: Path, *, execute_restart: bool = True, skip_handler: bool = False) -> argparse.Namespace:
    return argparse.Namespace(
        execute_restart=execute_restart,
        skip_handler=skip_handler,
        out=tmp_path / "restart.json",
        markdown_out=tmp_path / "restart.md",
        ssh_target="root@example.test",
        ssh_key=tmp_path / "key",
        connect_timeout=1,
        timeout=5,
        active_timeout=5,
        poll_interval=0,
        handler_timeout=5,
        handler_turn_timeout=300,
        remote_handler_report="/tmp/leo-restart-survival-direct-claim-current.json",
    )


def readback(label: str, pid: str) -> dict:
    return {
        "label": label,
        "remote_repo": "/opt/teleo-eval/workspaces/deploy-infra",
        "deploy_head": "abc123",
        "last_deploy_sha": "abc123",
        "git_status_short": "",
        "service": {
            "returncode": 0,
            "stderr": "",
            "state": {
                "ActiveState": "active",
                "SubState": "running",
                "MainPID": pid,
                "NRestarts": "0",
                "ExecMainStartTimestamp": "Fri 2026-07-10 01:04:22 UTC",
            },
        },
        "db_counts": {"returncode": 0, "stderr": "", "counts": COUNTS, "raw": json.dumps(COUNTS)},
    }


def fake_runner(command: list[str], input_text: str, timeout: int) -> restart_proof.CommandResult:
    remote = command[-1]
    if "before_restart" in remote:
        return restart_proof.CommandResult(0, json.dumps(readback("before_restart", "100")) + "\n", "")
    if remote == "systemctl restart leoclean-gateway.service":
        return restart_proof.CommandResult(0, "", "")
    if "after_restart_poll" in remote:
        return restart_proof.CommandResult(0, json.dumps(readback("after_restart_poll", "200")) + "\n", "")
    if "run_leo_direct_claim_handler_remote.py" in remote:
        return restart_proof.CommandResult(
            0,
            json.dumps(
                {
                    "pass_runtime": True,
                    "posted_to_telegram": False,
                    "mutates_kb_by_harness": False,
                    "changed_live_profile": False,
                    "temp_profile_removed": True,
                    "results": [{"prompt_id": "DC-01", "ok": True, "reply": "ok"}],
                }
            )
            + "\n",
            "",
        )
    raise AssertionError(remote)


def test_collect_restart_survival_passes_with_unchanged_counts_and_handler(tmp_path: Path):
    report = restart_proof.collect(args(tmp_path), runner=fake_runner)

    assert report["status"] == "pass"
    assert report["survives_restart"] is True
    assert report["db_counts_changed"] is False
    assert report["posted_to_telegram"] is False
    assert report["production_db_apply_ran"] is False
    assert report["handler"]["pass_runtime"] is True
    assert report["before"]["service"]["state"]["MainPID"] == "100"
    assert report["after"]["service"]["state"]["MainPID"] == "200"


def test_collect_refuses_to_claim_restart_without_execute_flag(tmp_path: Path):
    report = restart_proof.collect(args(tmp_path, execute_restart=False), runner=fake_runner)

    assert report["status"] == "not_executed_missing_execute_restart"
    assert report["survives_restart"] is False
    assert "before" in report


def test_markdown_report_names_safety_boundaries(tmp_path: Path):
    report = restart_proof.collect(args(tmp_path), runner=fake_runner)

    text = restart_proof.markdown_report(report)

    assert "Survives restart: `True`" in text
    assert "Posted to Telegram: `False`" in text
    assert "Production DB apply ran: `False`" in text
