"""Tests for scripts/record_leo_direct_claim_handler_suite.py."""

from __future__ import annotations

import json
import sys
from pathlib import Path

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

import record_leo_direct_claim_handler_suite as recorder  # noqa: E402
import run_leo_direct_claim_handler_remote as remote_runner  # noqa: E402


def sample_report() -> dict:
    return {
        "mode": "live_vps_gatewayrunner_temp_profile_direct_claim_suite",
        "posted_to_telegram": False,
        "changed_live_profile": False,
        "mutates_kb_by_harness": False,
        "pass_runtime": True,
        "db_counts_changed": False,
        "temp_profile_removed": True,
        "results": [{"prompt_id": "DC-01", "reply": "ok", "ok": True}],
    }


def test_remote_runner_preserves_direct_claim_prompt_catalog():
    prompts = remote_runner.direct_claim_prompts()

    assert [prompt["id"] for prompt in prompts] == ["DC-01", "DC-02", "DC-03", "DC-04", "DC-05", "DC-06"]
    assert prompts[0]["message"] == "Did we actually update the knowledge base, or is it still just proposals?"


def test_recorder_parses_last_json_line_and_redacts_token():
    raw = "noise\n" + json.dumps(sample_report() | {"secret": "token=123456:ABCdefghijklmnopqrstuvwxyz"}) + "\n"

    parsed = recorder.parse_report(raw)

    assert parsed["pass_runtime"] is True
    assert parsed["secret"] == "token=[REDACTED_TOKEN]"


def test_recorder_writes_report_with_source_path(tmp_path: Path):
    out = tmp_path / "suite.json"

    report = recorder.record_report(sample_report(), out=out)

    assert report["source_report_path"] == str(out)
    written = json.loads(out.read_text(encoding="utf-8"))
    assert written["posted_to_telegram"] is False
    assert written["temp_profile_removed"] is True
