import json
from pathlib import Path

from klt51_core import Klt51Engine, Reper4, d_star, d_star_quadratic, lambda_truth


def test_lambda_truth():
    result = lambda_truth(0.9, 0.6, 0.4, 0.7)
    assert result.status == "ok"
    assert result.lambda_value is not None
    assert result.delta is not None
    assert result.auth is not None
    assert result.lambda_quadratic is not None
    assert result.delta_quadratic is not None
    assert result.interval_status in {
        "full_quadratic_harmonic",
        "quadratic_obstruction",
        "metric_closed_amplitude_gap",
        "near_harmonic_complex_interval",
        "open_interval",
    }


def test_d_star():
    assert d_star(0.9, 0.6, 0.4) is not None
    q = d_star_quadratic(0.9, 0.6, 0.4)
    assert q["status"] in {"ok", "complex_branch_required", "singular"}


def test_engine_demo():
    demo = Path(__file__).resolve().parents[2] / "examples" / "demo_project.json"
    data = json.loads(demo.read_text(encoding="utf-8"))
    result = Klt51Engine().analyze(data)
    assert result["pix_count"] >= 10
    assert result["defect_count"] >= 1
    assert 0 <= result["cri"] <= 1
