114 lines
4.5 KiB
Python
114 lines
4.5 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from cold_display_guard.main import restore_runtime_state
|
|
|
|
|
|
class RuntimeRestoreTests(unittest.TestCase):
|
|
def test_restore_runtime_state_uses_stable_occupancy_when_raw_metrics_flicker(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
diagnostics_path = Path(tmpdir) / "runtime_diagnostics.jsonl"
|
|
diagnostics_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"ts": "2026-05-29T10:05:26+08:00",
|
|
"zone_counts": {"2": 1},
|
|
"diagnostics": {
|
|
"baseline_ready": True,
|
|
"zones": {
|
|
"2": {
|
|
"baseline_mean_luma": 165.0,
|
|
"baseline_texture": 16.0,
|
|
"baseline_dark_fraction": 0.0,
|
|
"baseline_bright_fraction": 0.0,
|
|
"mean_delta": 17.077,
|
|
"texture_delta": 8.819,
|
|
"dark_fraction": 0.0357,
|
|
"bright_fraction": 0.0,
|
|
"raw_occupied": False,
|
|
"occupied": True,
|
|
"empty_streak": 1,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
_, zone_counts = restore_runtime_state(
|
|
diagnostics_path,
|
|
{
|
|
"runtime": {
|
|
"occupancy_mean_delta": 55.0,
|
|
"occupancy_texture_delta": 18.0,
|
|
"occupancy_dark_fraction": 0.06,
|
|
"occupancy_texture_dark_fraction": 0.04,
|
|
}
|
|
},
|
|
)
|
|
|
|
self.assertEqual(zone_counts, {"2": 1})
|
|
|
|
def test_restore_runtime_state_uses_dark_fraction_rules(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
diagnostics_path = Path(tmpdir) / "runtime_diagnostics.jsonl"
|
|
diagnostics_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"ts": "2026-05-29T10:00:00+08:00",
|
|
"zone_counts": {"1": 1, "4": 1},
|
|
"diagnostics": {
|
|
"baseline_ready": True,
|
|
"zones": {
|
|
"1": {
|
|
"baseline_mean_luma": 165.0,
|
|
"baseline_texture": 16.0,
|
|
"baseline_dark_fraction": 0.0,
|
|
"baseline_bright_fraction": 0.0,
|
|
"mean_delta": 40.0,
|
|
"texture_delta": 18.0,
|
|
"dark_fraction": 0.10,
|
|
"bright_fraction": 0.0,
|
|
},
|
|
"4": {
|
|
"baseline_mean_luma": 177.0,
|
|
"baseline_texture": 9.0,
|
|
"baseline_dark_fraction": 0.0,
|
|
"baseline_bright_fraction": 0.0,
|
|
"mean_delta": 16.0,
|
|
"texture_delta": 40.0,
|
|
"dark_fraction": 0.0769,
|
|
"bright_fraction": 0.3077,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
baselines, zone_counts = restore_runtime_state(
|
|
diagnostics_path,
|
|
{
|
|
"runtime": {
|
|
"occupancy_mean_delta": 55.0,
|
|
"occupancy_texture_delta": 18.0,
|
|
"occupancy_dark_fraction": 0.06,
|
|
"occupancy_texture_dark_fraction": 0.04,
|
|
}
|
|
},
|
|
)
|
|
|
|
self.assertEqual(zone_counts, {"1": 1, "4": 0})
|
|
self.assertEqual(baselines["1"].dark_fraction, 0.0)
|
|
self.assertEqual(baselines["4"].bright_fraction, 0.0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|