"""Tests for the STRUST claim-evidence survival model."""

from __future__ import annotations

import json
import unittest
from pathlib import Path

from evidence_graph import build_claim_evidence


HERE = Path(__file__).resolve().parents[1]


class ClaimEvidenceGraphTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.config = json.loads(
            (HERE / "study_config.json").read_text(encoding="utf-8")
        )
        cls.bundle = build_claim_evidence(cls.config)

    def test_declared_graph_counts(self) -> None:
        summary = self.bundle["summary"]
        self.assertEqual(summary["claim_classes"], 5)
        self.assertEqual(summary["artifact_classes"], 10)
        self.assertEqual(summary["required_claim_artifact_edges"], 19)
        self.assertEqual(summary["surviving_required_edges"], 2)
        self.assertEqual(summary["missing_required_edges"], 17)

    def test_only_table_arithmetic_is_auditable(self) -> None:
        survival = self.bundle["survival"].set_index("claim_id")
        auditable = survival.index[
            survival["auditable_from_surviving_package"]
        ].tolist()
        self.assertEqual(auditable, ["table_arithmetic"])
        self.assertEqual(float(survival.loc["accuracy", "artifact_coverage"]), 0.0)
        self.assertEqual(
            int(survival.loc["accuracy", "declared_missing_bundle_size"]), 6
        )

    def test_no_single_recovery_completes_a_blocked_claim(self) -> None:
        counterfactual = self.bundle["counterfactual"]
        self.assertTrue(
            counterfactual["claims_completed_if_recovered_alone"].eq(0).all()
        )
        environment = counterfactual.loc[
            counterfactual["artifact_id"].eq("environment_manifest")
        ].iloc[0]
        self.assertEqual(int(environment["requirements_satisfied_if_recovered"]), 4)

    def test_edge_status_matches_artifact_status(self) -> None:
        edges = self.bundle["edges"]
        self.assertTrue(
            (
                edges["survives"]
                == edges["artifact_status"].eq("present").astype(int)
            ).all()
        )


if __name__ == "__main__":
    unittest.main()
