Files
NODEDC_MISSION_CORE/tests/fleet/test_bms_transient.py

69 lines
2.5 KiB
Python

import copy
import json
from k1link.fleet.sensor_transient import SensorTransient
def test_bms_inventory_never_persists_measurements_and_repeated_saves_do_not_extend_expiry():
now = [0]
cache = SensorTransient(clock=lambda: now[0])
row = {
"id": "vehicle",
"sensor_state": {
"items": [
{
"id": "battery",
"kind": "jk.bms",
"online": True,
"bms_status": {
"identity": {"model": "JK"},
"sample": {"voltage_v": 53.3},
"sample_at": "synthetic-time",
"fresh": True,
"rssi_dbm": -65,
},
}
]
},
}
saved = cache.persist(row)
assert "voltage_v" not in json.dumps(saved)
assert not saved["sensor_state"]["items"][0]["online"]
assert cache.hydrate(copy.deepcopy(saved)) == row
assert SensorTransient().hydrate(copy.deepcopy(saved)) == saved
now[0] = 86399
cache.persist(row)
now[0] = 86401
assert cache.hydrate(copy.deepcopy(saved)) == saved
def test_bms_disconnect_retains_latest_until_daily_reset_without_disk_measurements():
now, day = [0], [1]
cache = SensorTransient(clock=lambda: now[0], day=lambda: day[0])
def row(sample, at, vehicle="v"):
return {"id": vehicle, "sensor_state": {"items": [{
"id": "b", "kind": "jk.bms", "online": sample is not None,
"bms_status": {"sample": sample, "sample_at": at, "fresh": True},
}]}}
def status_of(value):
return cache.hydrate(copy.deepcopy(value))["sensor_state"]["items"][0]["bms_status"]
latest = row({"soc_percent": 82}, "2026-11-03T10:00:00+00:00")
cache.persist(latest)
now[0] = 600
saved = cache.persist(row(None, None))
assert "soc_percent" not in json.dumps(saved)
status = status_of(saved)
assert status["sample"]["soc_percent"] == 82
assert status["sample_at"] == "2026-11-03T10:00:00+00:00"
assert status["fresh"] is False
cache.persist(row({"soc_percent": 81}, "2026-11-03T09:00:00+00:00"))
assert status_of(saved)["sample"]["soc_percent"] == 82
cache.persist(row({"soc_percent": 83}, "2026-11-03T11:00:00+00:00"))
assert status_of(saved)["sample"]["soc_percent"] == 83
assert status_of(row(None, None, "other"))["sample"] is None
day[0] = 2
assert cache.hydrate(copy.deepcopy(saved)) == saved