#!/usr/bin/env python3
"""Contract tests for evidence-qualified quality confidence."""

from __future__ import annotations

import sys
from pathlib import Path


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

from carbon_quality_confidence import assess_quality_confidence  # noqa: E402


def test(test_id: str, status: str, priority: str = "P2", evidence: bool = True) -> dict:
    row = {"id": test_id, "status": status, "priority": priority, "adapter": "static-source"}
    if status in {"passed", "failed"}:
        row["actual"] = f"Observed {status} behavior"
        row["evidence"] = [f"evidence/{test_id}.json"] if evidence else []
    return row


planned = assess_quality_confidence({"tests": [test("CAR-001", "planned"), test("CAR-002", "queued")]})
assert planned["confidenceInQuality"]["band"] == "not-assessed"
assert planned["remainingRisk"]["level"] == "unknown"
assert planned["coverage"]["conclusivePercent"] == 0
assert planned["coverageMissing"]

complete = assess_quality_confidence({"tests": [test(f"CAR-{index:03d}", "passed") for index in range(1, 6)]})
assert complete["confidenceInQuality"]["band"] == "high"
assert complete["remainingRisk"]["level"] == "low"
assert complete["coverage"]["conclusivePercent"] == 100
assert complete["coverage"]["evidenceBacked"] == 5
assert complete["coverageMissing"] == []

failed = assess_quality_confidence({"tests": [test("CAR-P0", "failed", "P0"), test("CAR-OK", "passed")]})
assert failed["confidenceInQuality"]["band"] == "low"
assert failed["remainingRisk"]["level"] == "critical"
assert any("P0/P1" in reason for reason in failed["remainingRisk"]["reasons"])

incomplete = assess_quality_confidence({
    "tests": [test("CAR-OK", "passed"), test("CAR-BLOCKED", "blocked"), test("CAR-CANCELED", "canceled")],
    "exclusions": ["administrator recovery flow"],
})
assert incomplete["confidenceInQuality"]["band"] == "guarded"
assert incomplete["remainingRisk"]["level"] == "moderate"
assert any("canceled" in gap for gap in incomplete["coverageMissing"])
assert any("administrator recovery" in gap for gap in incomplete["coverageMissing"])

unsupported_pass = assess_quality_confidence({"tests": [test("CAR-NO-EVIDENCE", "passed", evidence=False)]})
assert unsupported_pass["confidenceInQuality"]["band"] == "guarded"
assert unsupported_pass["coverage"]["evidenceBacked"] == 0
assert unsupported_pass["confidenceInQuality"]["isProbabilityOfCorrectness"] is False
assert "not a probability" in unsupported_pass["interpretation"]

multidimensional = assess_quality_confidence({
    "tests": [
        {**test("CAR-AUTH", "failed", "P1"), "type": "api-authorization", "title": "Cross-tenant authorization"},
        {**test("CAR-A11Y", "passed"), "type": "accessibility", "title": "Keyboard and ARIA labels"},
        {**test("CAR-LATENCY", "passed"), "type": "performance", "title": "API latency boundary"},
    ],
    "findings": [{"severity": "high", "category": "security", "title": "Tenant data leak", "detail": "Wrong-owner access"}],
})
assert multidimensional["dimensionOrder"] == [
    "functional", "security", "accessibility", "performance",
    "reliability", "usability", "data", "operational",
]
assert len(multidimensional["dimensions"]) == 8
assert multidimensional["dimensions"]["security"]["band"] == "low"
assert multidimensional["dimensions"]["accessibility"]["band"] in {"moderate", "high"}
assert multidimensional["dimensions"]["reliability"]["band"] == "not-assessed"
assert all(item["isProbabilityOfCorrectness"] is False for item in multidimensional["dimensions"].values())

structured_artifacts = assess_quality_confidence(
    {
        "tests": [{
            **test("CAR-SURFACE", "failed", "P1"),
            "type": "browser-flow",
            "title": "Task board surface",
            "observations": [
                {
                    "id": "CAR-SURFACE-OBS-001", "title": "Keyboard opens task",
                    "status": "failed", "actual": "Enter did not open the card",
                    "evidence": ["evidence/keyboard.json"], "dimensions": ["accessibility"],
                },
                {
                    "id": "CAR-SURFACE-OBS-002", "title": "Pointer opens task",
                    "status": "passed", "actual": "Click opened the task",
                    "evidence": ["evidence/pointer.json"], "dimensions": ["functional"],
                },
            ],
        }],
        "findings": [{
            "severity": "high", "category": "security", "dimensions": ["security"],
            "title": "Stored XSS through task tag", "detail": "Untrusted text reaches innerHTML",
        }],
    },
    {
        "issues": [
            {
                "severity": "low", "category": "console",
                "title": "Browser resources were not found",
                "detail": "Failed to load resource: 404 /favicon.ico",
            },
            {
                "severity": "high", "category": "correlation",
                "title": "Artifact signals coincide with a failed test",
                "detail": "Derived correlation only",
            },
        ]
    },
)
assert structured_artifacts["dimensions"]["security"]["band"] == "low"
assert structured_artifacts["dimensions"]["accessibility"]["failed"] == 1
assert structured_artifacts["dimensions"]["functional"]["passed"] == 1
assert structured_artifacts["dimensions"]["performance"]["band"] == "not-assessed"
assert structured_artifacts["dimensions"]["operational"]["severeFindings"] == 0
assert not any("2 high-severity" in reason for reason in structured_artifacts["remainingRisk"]["reasons"])

print("PASS: confidence in quality, eight quality dimensions, remaining risk, missing coverage, decision-changing evidence, and non-probabilistic interpretation verified.")
