"""Tests for fan curve logic.""" import pytest from backend.fan_control import FanCurveManager, FanCurvePoint def test_parse_curve(): """Test parsing fan curve from JSON.""" json_data = '[{"temp": 30, "speed": 20}, {"temp": 50, "speed": 50}, {"temp": 70, "speed": 100}]' curve = FanCurveManager.parse_curve(json_data) assert len(curve) == 3 assert curve[0].temp == 30 assert curve[0].speed == 20 def test_parse_invalid_curve(): """Test parsing invalid curve returns default.""" curve = FanCurveManager.parse_curve("invalid json") assert len(curve) == 6 # Default curve has 6 points assert curve[0].temp == 30 assert curve[0].speed == 10 def test_calculate_speed_below_min(): """Test speed calculation below minimum temperature.""" curve = [ FanCurvePoint(30, 10), FanCurvePoint(50, 50), FanCurvePoint(70, 100), ] speed = FanCurveManager.calculate_speed(curve, 20) assert speed == 10 def test_calculate_speed_above_max(): """Test speed calculation above maximum temperature.""" curve = [ FanCurvePoint(30, 10), FanCurvePoint(50, 50), FanCurvePoint(70, 100), ] speed = FanCurveManager.calculate_speed(curve, 80) assert speed == 100 def test_calculate_speed_interpolation(): """Test speed calculation with interpolation.""" curve = [ FanCurvePoint(30, 10), FanCurvePoint(50, 50), FanCurvePoint(70, 100), ] # At 40°C, should be halfway between 10% and 50% speed = FanCurveManager.calculate_speed(curve, 40) assert speed == 30 def test_calculate_speed_exact_point(): """Test speed calculation at exact curve point.""" curve = [ FanCurvePoint(30, 10), FanCurvePoint(50, 50), ] speed = FanCurveManager.calculate_speed(curve, 50) assert speed == 50 def test_serialize_curve(): """Test serializing curve to JSON.""" points = [ FanCurvePoint(30, 10), FanCurvePoint(50, 50), ] json_data = FanCurveManager.serialize_curve(points) assert "30" in json_data assert "10" in json_data assert "50" in json_data