356 lines
16 KiB
Python
356 lines
16 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from cold_display_guard.config import load_config_document, merge_calibration, save_config_document
|
|
from cold_display_guard.manage_api import ManageContext, build_summary
|
|
|
|
|
|
class ManageApiTests(unittest.TestCase):
|
|
def test_merge_calibration_updates_zones_and_trash(self) -> None:
|
|
data = {
|
|
"camera_id": "cam",
|
|
"layout": {"rows": 2, "cols": 4, "zone_ids": ["r1c1", "r1c2"]},
|
|
"zones": [{"id": "r1c2", "polygon": [[0.5, 0], [1, 0], [1, 0.5]]}],
|
|
}
|
|
|
|
merged = merge_calibration(
|
|
data,
|
|
[{"id": "r1c1", "polygon": [[0, 0], [0.5, 0], [0.5, 0.5], [0, 0.5]]}],
|
|
[[0.8, 0.8], [1, 0.8], [1, 1], [0.8, 1]],
|
|
)
|
|
|
|
self.assertEqual(merged["layout"]["zone_ids"], ["r1c1", "r1c2"])
|
|
self.assertEqual(merged["zones"][0]["id"], "r1c1")
|
|
self.assertEqual(merged["zones"][1]["id"], "r1c2")
|
|
self.assertEqual(merged["trash"]["roi"][0], [0.8, 0.8])
|
|
|
|
def test_merge_calibration_replaces_numeric_food_zones_and_keeps_trash_separate(self) -> None:
|
|
data = {
|
|
"layout": {"zone_count": 2, "zone_ids": ["1", "2"]},
|
|
"zones": [
|
|
{"id": "1", "polygon": [[0, 0], [0.3, 0], [0.3, 0.3]]},
|
|
{"id": "2", "polygon": [[0.3, 0], [0.6, 0], [0.6, 0.3]]},
|
|
],
|
|
}
|
|
|
|
merged = merge_calibration(
|
|
data,
|
|
[
|
|
{"id": "1", "label": "区域 1", "polygon": [[0, 0], [0.2, 0], [0.2, 0.2]]},
|
|
{"id": "2", "label": "区域 2", "polygon": [[0.2, 0], [0.4, 0], [0.4, 0.2]]},
|
|
{"id": "3", "label": "区域 3", "polygon": [[0.4, 0], [0.6, 0], [0.6, 0.2]]},
|
|
],
|
|
[[0.8, 0.8], [1, 0.8], [1, 1], [0.8, 1]],
|
|
)
|
|
|
|
self.assertEqual(merged["layout"]["zone_count"], 3)
|
|
self.assertEqual(merged["layout"]["zone_ids"], ["1", "2", "3"])
|
|
self.assertEqual([zone["label"] for zone in merged["zones"]], ["区域 1", "区域 2", "区域 3"])
|
|
self.assertEqual(merged["trash"]["roi"][0], [0.8, 0.8])
|
|
self.assertNotIn("trash", merged["layout"]["zone_ids"])
|
|
|
|
def test_merge_calibration_preserves_numeric_zone_count_when_some_zones_are_unmarked(self) -> None:
|
|
data = {
|
|
"layout": {"zone_count": 3, "zone_ids": ["1", "2", "3"]},
|
|
"zones": [
|
|
{"id": "1", "label": "区域 1", "polygon": [[0, 0], [0.2, 0], [0.2, 0.2]]},
|
|
{"id": "2", "label": "区域 2", "polygon": [[0.2, 0], [0.4, 0], [0.4, 0.2]]},
|
|
],
|
|
}
|
|
|
|
merged = merge_calibration(
|
|
data,
|
|
[{"id": "1", "label": "区域 1", "polygon": [[0, 0], [0.3, 0], [0.3, 0.3]]}],
|
|
[[0.8, 0.8], [1, 0.8], [1, 1]],
|
|
{"zone_count": 3, "zone_ids": ["1", "2", "3"]},
|
|
)
|
|
|
|
self.assertEqual(merged["layout"]["zone_count"], 3)
|
|
self.assertEqual(merged["layout"]["zone_ids"], ["1", "2", "3"])
|
|
self.assertEqual([zone["id"] for zone in merged["zones"]], ["1", "2"])
|
|
self.assertEqual(merged["zones"][0]["polygon"], [[0.0, 0.0], [0.3, 0.0], [0.3, 0.3]])
|
|
self.assertEqual(merged["zones"][1]["polygon"], [[0.2, 0.0], [0.4, 0.0], [0.4, 0.2]])
|
|
|
|
def test_merge_calibration_rejects_more_than_ten_numeric_food_zones(self) -> None:
|
|
zones = [
|
|
{"id": str(index), "polygon": [[0, 0], [0.1, 0], [0.1, 0.1]]}
|
|
for index in range(1, 12)
|
|
]
|
|
|
|
with self.assertRaisesRegex(ValueError, "1 to 10"):
|
|
merge_calibration({"layout": {}}, zones, None)
|
|
|
|
def test_save_config_document_round_trips_manage_fields(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
path = Path(tmpdir) / "config.toml"
|
|
save_config_document(
|
|
path,
|
|
{
|
|
"camera_id": "cam",
|
|
"timezone": "Asia/Shanghai",
|
|
"stream": {"rtsp_url": "rtsp://example"},
|
|
"thresholds": {"max_dwell_seconds": 30, "trash_confirmation_seconds": 5},
|
|
"layout": {"rows": 1, "cols": 1, "zone_ids": ["r1c1"]},
|
|
"zones": [{"id": "r1c1", "polygon": [[0, 0], [1, 0], [1, 1]]}],
|
|
"trash": {"roi": [[0, 0], [1, 0], [1, 1]]},
|
|
"event_sink": {"path": "logs/events.jsonl"},
|
|
},
|
|
)
|
|
loaded = load_config_document(path)
|
|
|
|
self.assertEqual(loaded["stream"]["rtsp_url"], "rtsp://example")
|
|
self.assertEqual(loaded["zones"][0]["polygon"][1], [1.0, 0.0])
|
|
|
|
def test_summary_reads_event_jsonl(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
root = Path(tmpdir)
|
|
config_path = root / "config" / "local.toml"
|
|
save_config_document(
|
|
config_path,
|
|
{
|
|
"event_sink": {"path": "logs/events.jsonl"},
|
|
"layout": {"rows": 1, "cols": 1, "zone_ids": ["r1c1"]},
|
|
},
|
|
)
|
|
events_path = root / "logs" / "events.jsonl"
|
|
events_path.parent.mkdir()
|
|
events_path.write_text(
|
|
"\n".join(
|
|
[
|
|
json.dumps({"event": "batch_started", "severity": "info", "ts": "2026-04-27T10:00:00+08:00"}),
|
|
json.dumps({"event": "time_alarm", "severity": "alarm", "ts": "2026-04-27T12:00:00+08:00"}),
|
|
json.dumps({"event": "warning_escalated", "severity": "warning", "ts": "2026-04-27T13:02:00+08:00"}),
|
|
]
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
summary = build_summary(ManageContext(config_path=config_path, project_root=root))
|
|
|
|
self.assertEqual(summary["metrics"]["event_count"], 3)
|
|
self.assertEqual(summary["metrics"]["alert_count"], 1)
|
|
self.assertEqual(summary["metrics"]["warning_count"], 1)
|
|
self.assertEqual(summary["metrics"]["violation_count"], 1)
|
|
self.assertEqual(summary["metrics"]["latest_alert_time"], "2026-04-27T13:02:00+08:00")
|
|
|
|
def test_summary_counts_escalated_and_legacy_warnings_without_pending_disposal(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
root = Path(tmpdir)
|
|
config_path = root / "config" / "local.toml"
|
|
save_config_document(
|
|
config_path,
|
|
{
|
|
"event_sink": {"path": "logs/events.jsonl"},
|
|
"layout": {"rows": 1, "cols": 1, "zone_ids": ["r1c1"]},
|
|
},
|
|
)
|
|
events_path = root / "logs" / "events.jsonl"
|
|
events_path.parent.mkdir()
|
|
events_path.write_text(
|
|
"\n".join(
|
|
[
|
|
json.dumps({"event": "batch_pending_disposal", "severity": "warning", "ts": "2026-04-27T12:01:00+08:00"}),
|
|
json.dumps({"event": "mixed_batch_violation", "ts": "2026-04-27T12:02:00+08:00"}),
|
|
json.dumps({"event": "warning_escalated", "severity": "warning", "ts": "2026-04-27T12:03:00+08:00"}),
|
|
]
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
summary = build_summary(ManageContext(config_path=config_path, project_root=root))
|
|
|
|
self.assertEqual(summary["metrics"]["event_count"], 3)
|
|
self.assertEqual(summary["metrics"]["alert_count"], 0)
|
|
self.assertEqual(summary["metrics"]["warning_count"], 2)
|
|
self.assertEqual(summary["metrics"]["violation_count"], 2)
|
|
self.assertEqual(summary["metrics"]["latest_alert_time"], "2026-04-27T12:03:00+08:00")
|
|
|
|
def test_summary_reads_runtime_diagnostics(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
root = Path(tmpdir)
|
|
config_path = root / "config" / "local.toml"
|
|
save_config_document(
|
|
config_path,
|
|
{
|
|
"runtime": {"diagnostics_path": "logs/runtime_diagnostics.jsonl"},
|
|
"event_sink": {"path": "logs/events.jsonl"},
|
|
"layout": {"rows": 1, "cols": 1, "zone_ids": ["r1c1"]},
|
|
},
|
|
)
|
|
diagnostics_path = root / "logs" / "runtime_diagnostics.jsonl"
|
|
diagnostics_path.parent.mkdir()
|
|
diagnostics_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"ts": "2026-04-28T10:00:00+08:00",
|
|
"zone_counts": {"r1c1": 1},
|
|
"diagnostics": {"baseline_ready": True},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
summary = build_summary(ManageContext(config_path=config_path, project_root=root))
|
|
|
|
self.assertEqual(summary["metrics"]["diagnostics_count"], 1)
|
|
self.assertEqual(summary["metrics"]["latest_zone_counts"], {"r1c1": 1})
|
|
self.assertTrue(summary["metrics"]["baseline_ready"])
|
|
|
|
def test_summary_uses_stable_runtime_occupancy_when_raw_metrics_flicker(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
root = Path(tmpdir)
|
|
config_path = root / "config" / "local.toml"
|
|
save_config_document(
|
|
config_path,
|
|
{
|
|
"runtime": {
|
|
"diagnostics_path": "logs/runtime_diagnostics.jsonl",
|
|
"occupancy_mean_delta": 55.0,
|
|
"occupancy_texture_delta": 18.0,
|
|
"occupancy_dark_fraction": 0.06,
|
|
"occupancy_texture_dark_fraction": 0.04,
|
|
},
|
|
"event_sink": {"path": "logs/events.jsonl"},
|
|
"layout": {"zone_count": 2, "zone_ids": ["1", "2"]},
|
|
},
|
|
)
|
|
diagnostics_path = root / "logs" / "runtime_diagnostics.jsonl"
|
|
diagnostics_path.parent.mkdir()
|
|
diagnostics_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"ts": "2026-05-29T10:05:26+08:00",
|
|
"zone_counts": {"1": 0, "2": 1},
|
|
"diagnostics": {
|
|
"baseline_ready": True,
|
|
"zones": {
|
|
"1": {
|
|
"mean_delta": 0.0,
|
|
"texture_delta": 0.0,
|
|
"dark_fraction": 0.0,
|
|
"baseline_dark_fraction": 0.0,
|
|
"bright_fraction": 0.0,
|
|
"occupied": False,
|
|
},
|
|
"2": {
|
|
"mean_delta": 17.077,
|
|
"texture_delta": 8.819,
|
|
"dark_fraction": 0.0357,
|
|
"baseline_dark_fraction": 0.0,
|
|
"bright_fraction": 0.0,
|
|
"raw_occupied": False,
|
|
"occupied": True,
|
|
"empty_streak": 1,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
summary = build_summary(ManageContext(config_path=config_path, project_root=root))
|
|
|
|
self.assertEqual(summary["metrics"]["latest_zone_counts"], {"1": 0, "2": 1})
|
|
|
|
def test_summary_recomputes_latest_zone_counts_from_runtime_thresholds_when_stable_state_is_absent(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
root = Path(tmpdir)
|
|
config_path = root / "config" / "local.toml"
|
|
save_config_document(
|
|
config_path,
|
|
{
|
|
"runtime": {
|
|
"diagnostics_path": "logs/runtime_diagnostics.jsonl",
|
|
"occupancy_mean_delta": 45.0,
|
|
"occupancy_texture_delta": 18.0,
|
|
},
|
|
"event_sink": {"path": "logs/events.jsonl"},
|
|
"layout": {"zone_count": 3, "zone_ids": ["1", "2", "3"]},
|
|
},
|
|
)
|
|
diagnostics_path = root / "logs" / "runtime_diagnostics.jsonl"
|
|
diagnostics_path.parent.mkdir()
|
|
diagnostics_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"ts": "2026-05-27T11:02:23+08:00",
|
|
"zone_counts": {"1": 1, "3": 1},
|
|
"diagnostics": {
|
|
"baseline_ready": True,
|
|
"zones": {
|
|
"1": {"mean_delta": 70.0, "texture_delta": 27.0},
|
|
"3": {"mean_delta": 36.0, "texture_delta": -9.0},
|
|
},
|
|
},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
summary = build_summary(ManageContext(config_path=config_path, project_root=root))
|
|
|
|
self.assertEqual(summary["metrics"]["latest_zone_counts"], {"1": 1, "3": 0})
|
|
|
|
def test_summary_recomputes_latest_zone_counts_with_dark_fraction_rule(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
root = Path(tmpdir)
|
|
config_path = root / "config" / "local.toml"
|
|
save_config_document(
|
|
config_path,
|
|
{
|
|
"runtime": {
|
|
"diagnostics_path": "logs/runtime_diagnostics.jsonl",
|
|
"occupancy_mean_delta": 55.0,
|
|
"occupancy_texture_delta": 18.0,
|
|
"occupancy_dark_fraction": 0.06,
|
|
"occupancy_texture_dark_fraction": 0.04,
|
|
"occupancy_bright_reflection_fraction": 0.18,
|
|
},
|
|
"event_sink": {"path": "logs/events.jsonl"},
|
|
"layout": {"zone_count": 2, "zone_ids": ["1", "2"]},
|
|
},
|
|
)
|
|
diagnostics_path = root / "logs" / "runtime_diagnostics.jsonl"
|
|
diagnostics_path.parent.mkdir()
|
|
diagnostics_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"ts": "2026-05-28T09:41:13+08:00",
|
|
"zone_counts": {"1": 1, "2": 1},
|
|
"diagnostics": {
|
|
"baseline_ready": True,
|
|
"zones": {
|
|
"1": {
|
|
"mean_delta": 45.0,
|
|
"texture_delta": 20.0,
|
|
"dark_fraction": 0.20,
|
|
"baseline_dark_fraction": 0.0,
|
|
"bright_fraction": 0.0,
|
|
},
|
|
"2": {
|
|
"mean_delta": 16.0,
|
|
"texture_delta": 40.0,
|
|
"dark_fraction": 0.0769,
|
|
"baseline_dark_fraction": 0.0,
|
|
"bright_fraction": 0.3077,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
summary = build_summary(ManageContext(config_path=config_path, project_root=root))
|
|
|
|
self.assertEqual(summary["metrics"]["latest_zone_counts"], {"1": 1, "2": 0})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|