"""Regression coverage for the public README quick-start flow."""

from __future__ import annotations

import json
from pathlib import Path

from icsd.core import Invariant, InvariantSet, Transition, validate_evidence_payload


def test_public_quickstart_generates_valid_evidence(tmp_path: Path) -> None:
    def positive_balance(state: dict[str, int]) -> bool:
        return state["balance"] >= 0

    inv = Invariant(name="balance_non_negative", check=positive_balance)
    invset = InvariantSet([inv])
    transition = Transition.simple(
        name="debit_demo",
        mutate=lambda state: state.update({"balance": state["balance"] - 1}),
        invariants=invset,
    )

    result = transition.apply(
        state={"balance": 10},
        entity_type="account",
        entity_id="acct-001",
        actor="user:quickstart",
        authority="policy/account-demo",
    )
    assert result.after_state == {"balance": 9}

    evidence_path = tmp_path / "quickstart.evidence.json"
    evidence_path.write_text(result.evidence_json, encoding="utf-8")
    payload = json.loads(evidence_path.read_text(encoding="utf-8"))

    assert payload == result.evidence.as_dict()
    assert validate_evidence_payload(payload) == ()
