87 lines
2.9 KiB
Python
87 lines
2.9 KiB
Python
"""Output observations do not require GNSS and never imply a physical contact."""
|
|
|
|
import base64
|
|
import struct
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from k1link.device_plugins.arnavi_b2.internal import parse_package
|
|
from k1link.fleet.vps_hosts import VPSHosts
|
|
|
|
|
|
def packet(measured, mask=0x0021, extra=b""):
|
|
tags = b"\x06\x01\x00" + struct.pack("<H", mask) + extra
|
|
data = struct.pack("<I", measured) + tags
|
|
return b"[\x01\x01" + struct.pack("<H", len(tags)) + data + bytes([sum(data) % 256]) + b"]"
|
|
|
|
|
|
def test_repeated_tag6_modes_do_not_hide_discrete_states():
|
|
data = packet(int(time.time()), extra=b"\x06\x08\x03\x55\x21")
|
|
observation = parse_package(data)[1][0]
|
|
assert observation["position"] is None
|
|
assert observation["outputs"] == {"4": False, "5": True, "6": False}
|
|
for offset in range(len(data)):
|
|
assert parse_package(data[:offset]) is None
|
|
with pytest.raises(ValueError, match="checksum"):
|
|
parse_package(data[:-2] + bytes([data[-2] ^ 1]) + data[-1:])
|
|
|
|
|
|
def test_output_order_freshness_authority_and_transaction(tmp_path, monkeypatch):
|
|
now = int(time.time())
|
|
|
|
class Equipment:
|
|
vehicle = "rover-a"
|
|
|
|
def listing(self, vehicle=None):
|
|
return [{"id": "b2", "name": "Synthetic", "vehicle_id": self.vehicle}]
|
|
|
|
eq = Equipment()
|
|
hosts = VPSHosts(tmp_path, "core", eq)
|
|
host = {
|
|
"id": "host",
|
|
"cursor": 0,
|
|
"state": "paired",
|
|
"last_sync_at": now,
|
|
"vehicle_ids": ["rover-a", "rover-b"],
|
|
}
|
|
hosts.save(host)
|
|
|
|
def item(seq, measured, mask=0x21, instance="b2"):
|
|
return {
|
|
"id": seq,
|
|
"received_at": now,
|
|
"instance_id": instance,
|
|
"raw": base64.b64encode(packet(measured, mask)).decode(),
|
|
}
|
|
|
|
hosts.ingest(
|
|
host,
|
|
[
|
|
item(1, now),
|
|
item(2, now - 1000, 0),
|
|
item(3, now + 400, 0),
|
|
item(4, now, 0, "unadmitted"),
|
|
],
|
|
[{"id": "b2"}],
|
|
)
|
|
status = hosts.telemetry()[0]
|
|
assert status["online"] and status["outputs_fresh"] and status["position"] is None
|
|
assert status["outputs"]["pins"]["5"] is True
|
|
assert status["outputs"]["measured_at"] == now
|
|
with hosts.db() as db:
|
|
assert db.execute("SELECT count(*) FROM outputs").fetchone()[0] == 1
|
|
with pytest.raises(ValueError):
|
|
hosts.ingest(host, [item(5, now + 1, 0), {**item(6, now), "raw": "!!"}], [{"id": "b2"}])
|
|
assert hosts.rows()[0]["cursor"] == 4 and hosts.telemetry()[0]["outputs"]["pins"]["5"]
|
|
monkeypatch.setattr("k1link.fleet.vps_hosts.time.time", lambda: now + 100)
|
|
assert not hosts.telemetry()[0]["outputs_fresh"]
|
|
eq.vehicle = "rover-b"
|
|
assert hosts.telemetry()[0]["outputs"] is not None
|
|
eq.vehicle = "rover-other"
|
|
assert hosts.telemetry()[0]["outputs"] is None
|
|
eq.vehicle = "rover-a"
|
|
host["state"] = "replaced"
|
|
hosts.save(host)
|
|
assert hosts.telemetry()[0]["outputs"] is None
|