"""
Mathematical tests for MAX31865 PT100 conversion.
"""

from sentinel.sensors.max31865 import (
    MAX31865TemperatureSensor,
)


class FakeSPI:
    """Minimal SPI fake for mathematical tests."""

    max_speed_hz = 0
    mode = 0

    def xfer2(
        self,
        data: list[int],
    ) -> list[int]:
        return [0] * len(data)

    def close(self) -> None:
        pass


def build_sensor() -> MAX31865TemperatureSensor:
    return MAX31865TemperatureSensor(
        wires=3,
        rtd_nominal_ohms=100.0,
        reference_resistor_ohms=430.0,
        spi=FakeSPI(),
    )


def test_pt100_zero_celsius() -> None:
    sensor = build_sensor()

    value = sensor._temperature_from_resistance(
        100.0
    )

    assert abs(value) < 0.01


def test_pt100_approximately_100_celsius() -> None:
    sensor = build_sensor()

    value = sensor._temperature_from_resistance(
        138.5055
    )

    assert abs(value - 100.0) < 0.05
