|
| 1 | +"""Test Core Version evaluation.""" |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | +from unittest.mock import PropertyMock, patch |
| 5 | + |
| 6 | +from awesomeversion import AwesomeVersion |
| 7 | +import pytest |
| 8 | + |
| 9 | +from supervisor.const import CoreState |
| 10 | +from supervisor.coresys import CoreSys |
| 11 | +from supervisor.homeassistant.module import HomeAssistant |
| 12 | +from supervisor.resolution.evaluations.core_version import EvaluateCoreVersion |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + "current,expected", |
| 17 | + [ |
| 18 | + ("2022.1.0", True), # More than 2 years old, should be unsupported |
| 19 | + ("2023.12.0", False), # Less than 2 years old, should be supported |
| 20 | + (f"{datetime.now().year}.1", False), # Current year, supported |
| 21 | + (f"{datetime.now().year - 1}.12", False), # 1 year old, supported |
| 22 | + (f"{datetime.now().year - 2}.1", True), # 2 years old, unsupported |
| 23 | + (f"{datetime.now().year - 3}.1", True), # 3 years old, unsupported |
| 24 | + ("2021.6.0", True), # Very old version, unsupported |
| 25 | + (None, False), # No current version info, check skipped |
| 26 | + ], |
| 27 | +) |
| 28 | +async def test_core_version_evaluation( |
| 29 | + coresys: CoreSys, current: str | None, expected: bool |
| 30 | +): |
| 31 | + """Test evaluation logic on Core versions.""" |
| 32 | + evaluation = EvaluateCoreVersion(coresys) |
| 33 | + await coresys.core.set_state(CoreState.RUNNING) |
| 34 | + |
| 35 | + with ( |
| 36 | + patch.object( |
| 37 | + HomeAssistant, |
| 38 | + "version", |
| 39 | + new=PropertyMock(return_value=current and AwesomeVersion(current)), |
| 40 | + ), |
| 41 | + patch.object( |
| 42 | + HomeAssistant, |
| 43 | + "latest_version", |
| 44 | + new=PropertyMock( |
| 45 | + return_value=AwesomeVersion("2024.12.0") |
| 46 | + ), # Mock latest version |
| 47 | + ), |
| 48 | + ): |
| 49 | + assert evaluation.reason not in coresys.resolution.unsupported |
| 50 | + await evaluation() |
| 51 | + assert (evaluation.reason in coresys.resolution.unsupported) is expected |
| 52 | + |
| 53 | + |
| 54 | +async def test_core_version_evaluation_no_latest(coresys: CoreSys): |
| 55 | + """Test evaluation when no latest version is available.""" |
| 56 | + evaluation = EvaluateCoreVersion(coresys) |
| 57 | + await coresys.core.set_state(CoreState.RUNNING) |
| 58 | + |
| 59 | + with ( |
| 60 | + patch.object( |
| 61 | + HomeAssistant, |
| 62 | + "version", |
| 63 | + new=PropertyMock(return_value=AwesomeVersion("2022.1.0")), |
| 64 | + ), |
| 65 | + patch.object( |
| 66 | + HomeAssistant, |
| 67 | + "latest_version", |
| 68 | + new=PropertyMock(return_value=None), |
| 69 | + ), |
| 70 | + ): |
| 71 | + assert evaluation.reason not in coresys.resolution.unsupported |
| 72 | + await evaluation() |
| 73 | + assert evaluation.reason not in coresys.resolution.unsupported |
| 74 | + |
| 75 | + |
| 76 | +async def test_core_version_invalid_format(coresys: CoreSys): |
| 77 | + """Test evaluation with invalid version format.""" |
| 78 | + evaluation = EvaluateCoreVersion(coresys) |
| 79 | + await coresys.core.set_state(CoreState.RUNNING) |
| 80 | + |
| 81 | + with ( |
| 82 | + patch.object( |
| 83 | + HomeAssistant, |
| 84 | + "version", |
| 85 | + new=PropertyMock(return_value=AwesomeVersion("invalid.version")), |
| 86 | + ), |
| 87 | + patch.object( |
| 88 | + HomeAssistant, |
| 89 | + "latest_version", |
| 90 | + new=PropertyMock(return_value=AwesomeVersion("2024.12.0")), |
| 91 | + ), |
| 92 | + ): |
| 93 | + assert evaluation.reason not in coresys.resolution.unsupported |
| 94 | + await evaluation() |
| 95 | + # Should handle gracefully and not mark as unsupported |
| 96 | + assert evaluation.reason not in coresys.resolution.unsupported |
| 97 | + |
| 98 | + |
| 99 | +async def test_did_run(coresys: CoreSys): |
| 100 | + """Test that the evaluation ran as expected.""" |
| 101 | + evaluation = EvaluateCoreVersion(coresys) |
| 102 | + should_run = evaluation.states |
| 103 | + should_not_run = [state for state in CoreState if state not in should_run] |
| 104 | + assert len(should_run) != 0 |
| 105 | + assert len(should_not_run) != 0 |
| 106 | + |
| 107 | + with patch( |
| 108 | + "supervisor.resolution.evaluations.core_version.EvaluateCoreVersion.evaluate", |
| 109 | + return_value=None, |
| 110 | + ) as evaluate: |
| 111 | + for state in should_run: |
| 112 | + await coresys.core.set_state(state) |
| 113 | + await evaluation() |
| 114 | + evaluate.assert_called_once() |
| 115 | + evaluate.reset_mock() |
| 116 | + |
| 117 | + for state in should_not_run: |
| 118 | + await coresys.core.set_state(state) |
| 119 | + await evaluation() |
| 120 | + evaluate.assert_not_called() |
| 121 | + evaluate.reset_mock() |
0 commit comments