"""Tests for src/pygui/backend/wafer_layouts.py."""
import pytest

from pygui.backend.wafer.wafer_layouts import (
    available_families,
    ec_pairs_for_wafer_id,
    load_layout,
)
from pygui.backend.wafer.zwafer_models import Sensor


def test_lists_bundled_families():
    fams = available_families()
    assert "aep" in fams and "x" in fams


def test_loads_sensors_in_mm():
    sensors = load_layout("aep")
    assert sensors and all(isinstance(s, Sensor) for s in sensors)
    assert any(s.x < 0 for s in sensors)
    assert max(abs(s.x) for s in sensors) < 200.0


def test_unknown_family_raises():
    with pytest.raises(KeyError):
        load_layout("nope")


# ---- edge-center pair tables (ADR-0002) ----

def test_ec_pairs_aep_match_csharp_table():
    pairs = ec_pairs_for_wafer_id("A00123")
    assert pairs[0] == (0, 40)
    assert pairs[-1] == (15, 47)
    assert len(pairs) == 16
    assert ec_pairs_for_wafer_id("E1") == pairs
    assert ec_pairs_for_wafer_id("P1") == pairs


def test_ec_pairs_per_family_classes():
    assert len(ec_pairs_for_wafer_id("B00108")) == 12
    assert ec_pairs_for_wafer_id("C1") == ec_pairs_for_wafer_id("D1")
    assert ec_pairs_for_wafer_id("F1")[0] == (0, 18)
    assert ec_pairs_for_wafer_id("X1")[0] == (5, 76)
    assert ec_pairs_for_wafer_id("Z1")[0] == (49, 0)


def test_ec_pairs_unknown_family_empty():
    assert ec_pairs_for_wafer_id("Q999") == ()
    assert ec_pairs_for_wafer_id("") == ()
