"""Unit tests for compact vitals tool payloads.""" from tools import ( RANGE_SNAPSHOT_CAP, _agent_snapshot, _cap_snapshots, _compact_live_vitals, _compact_snapshot, _resolve_stored_vital_label, ) def test_compact_snapshot_keeps_key_labels_only(): snapshot = { "id": 1, "ts": "2026-06-10T14:00:00+00:00", "dtcs": ["P0171"], "readings": { "RPM": "850", "Coolant temp": "92", "Short fuel trim B1": "12.5", "Cat temp B1S1": "400", "Speed": "0", }, } compact = _compact_snapshot(snapshot) assert compact["ts"] == snapshot["ts"] assert compact["dtcs"] == ["P0171"] assert compact["readings"] == { "RPM": "850", "Coolant temp": "92", "Short fuel trim B1": "12.5", "Speed": "0", } assert "Cat temp B1S1" not in compact["readings"] def test_compact_snapshot_from_readings_list(): snapshot = { "ts": "2026-06-10T14:01:00+00:00", "dtcs": [], "readings": [ {"label": "RPM", "value": "900"}, {"label": "MAF", "value": "3.2"}, {"label": "Throttle position", "value": "14"}, ], } compact = _compact_snapshot(snapshot) assert compact["readings"] == { "RPM": "900", "MAF": "3.2", "Throttle position": "14", } def test_agent_snapshot_includes_id(): snapshot = { "id": 42, "ts": "2026-06-10T14:00:00+00:00", "dtcs": [], "readings": {"RPM": "800"}, } out = _agent_snapshot(snapshot) assert out["id"] == 42 assert out["readings"] == {"RPM": "800"} def test_cap_snapshots_under_limit(): rows = [{"id": i} for i in range(10)] capped, truncated = _cap_snapshots(rows) assert len(capped) == 10 assert truncated is False def test_cap_snapshots_over_limit(): rows = [{"id": i} for i in range(RANGE_SNAPSHOT_CAP + 5)] capped, truncated = _cap_snapshots(rows) assert len(capped) == RANGE_SNAPSHOT_CAP assert truncated is True def test_compact_live_vitals_strips_descriptions(): vitals = [ {"name": "RPM", "description": "Engine RPM", "value": "850"}, {"name": "MAF", "description": "Mass Air Flow", "value": "4.1"}, ] assert _compact_live_vitals(vitals) == [ {"name": "RPM", "value": "850"}, {"name": "MAF", "value": "4.1"}, ] def test_resolve_stored_vital_label(): assert _resolve_stored_vital_label("RPM") == "RPM" assert _resolve_stored_vital_label("coolant temp") == "Coolant temp" assert _resolve_stored_vital_label("COOLANT_TEMP") == "Coolant temp" assert _resolve_stored_vital_label("Not A Sensor") is None def test_get_vital_history_chronological(tmp_path, monkeypatch): import vitals_store db = tmp_path / "vitals.db" monkeypatch.setenv("VITALS_DB_PATH", str(db)) vitals_store._initialized = False vitals_store.DB_PATH = db vitals_store.append_snapshot( { "readings": [{"label": "RPM", "value": "800"}], "dtcs": [], } ) vitals_store.append_snapshot( { "readings": [{"label": "RPM", "value": "850"}], "dtcs": [], } ) history = vitals_store.get_vital_history("RPM", count=10) assert len(history) == 2 assert history[0]["value"] == "800" assert history[1]["value"] == "850" assert "ts" in history[0] def test_probe_obd_vitals_shape(): from obd_connection import probe_obd_vitals result = probe_obd_vitals() assert "can_receive_vitals" in result assert "port" in result assert "probe_command" in result assert isinstance(result["can_receive_vitals"], bool) if __name__ == "__main__": test_compact_snapshot_keeps_key_labels_only() test_compact_snapshot_from_readings_list() test_agent_snapshot_includes_id() test_cap_snapshots_under_limit() test_cap_snapshots_over_limit() test_compact_live_vitals_strips_descriptions() test_resolve_stored_vital_label() test_probe_obd_vitals_shape() print("OK")