"""Tests for scripts/build_working_leo_production_receipt_template.py."""

from __future__ import annotations

import sys
from pathlib import Path

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

import build_working_leo_authorized_apply_runbook as runbook_builder  # noqa: E402
import build_working_leo_production_receipt_template as template  # noqa: E402


def test_template_contains_required_receipt_fields_without_claiming_execution():
    runbook = runbook_builder.build_runbook()
    result = template.build_template(git_sha="57bd705")
    receipt = result["receipt_template"]

    assert result["mode"] == "receipt_template_not_executed"
    assert result["production_apply_executed"] is False
    assert result["valid_production_receipt"] is False
    assert set(runbook["production_apply_receipt_required_fields"]).issubset(receipt.keys())
    assert receipt["explicit_authorization_record"]["authorized"] is False
    assert receipt["git_commit_sha"] == "57bd705"
    assert receipt["expected_after_apply_counts"] == runbook["expected_after_apply_counts"]
    assert receipt["actual_after_apply_counts"] == {}
    assert receipt["apply_output_paths"] == []
    assert receipt["postflight_output_paths"] == []


def test_template_marks_after_apply_fields_as_not_ready():
    result = template.build_template(git_sha="57bd705")

    assert "apply_output_paths" in result["must_fill_after_authorized_apply"]
    assert "postflight_output_paths" in result["must_fill_after_authorized_apply"]
    assert "actual_after_apply_counts" in result["must_fill_after_authorized_apply"]
    assert "service_readback" in result["must_fill_after_authorized_apply"]
    assert "cleanup_readback" in result["must_fill_after_authorized_apply"]
    assert "expected_after_apply_counts" in result["ready_or_baseline_fields"]
    assert "rollback_packet_paths" in result["ready_or_baseline_fields"]


def test_template_uses_live_preflight_baseline_as_baseline_not_apply_proof():
    result = template.build_template(git_sha="57bd705")

    assert result["field_status"]["preflight_output_paths"] == "baseline_available_rerun_immediately_before_apply"
    assert result["receipt_template"]["preflight_output_paths"] == [
        "docs/reports/leo-working-state-20260709/working-leo-production-preflight-current.json",
        "docs/reports/leo-working-state-20260709/working-leo-production-preflight-current.md",
    ]
    assert "does not authorize production apply" in result["claim_ceiling"]
    assert "does not prove canonical rows were changed" in result["claim_ceiling"]


def test_markdown_preserves_non_executed_boundary(tmp_path: Path):
    result = template.build_template(git_sha="57bd705")
    out = tmp_path / "template.md"

    template.write_markdown(out, result)

    text = out.read_text()
    assert "Production apply executed: `False`" in text
    assert "Valid production receipt: `False`" in text
    assert "`apply_output_paths`: `must_fill_after_apply_execution`" in text
    assert "Claim Ceiling" in text
