Add portable shared WireGuard profiles while retaining Tailscale and Worker boundaries. Bound rover command/telemetry delivery, recovery and diagnostic capture across Core and Node.
163 lines
6.0 KiB
Python
163 lines
6.0 KiB
Python
import copy
|
|
|
|
import pytest
|
|
|
|
from k1link.fleet.rover_control import RoverControl
|
|
|
|
|
|
def packet(hub, at=100, revision=1, details=True, **changes):
|
|
snapshot = {
|
|
"instance": "driver",
|
|
"state": "stopped",
|
|
"release_confirmed": None,
|
|
"control_active": False,
|
|
"session_id": None,
|
|
"devices": [
|
|
{
|
|
"id": "motor",
|
|
"session_id": "attachment",
|
|
"age_ms": 25,
|
|
"values": {"erpm": 0, "motor_current_a": 0.2, "fault_code": 0},
|
|
}
|
|
],
|
|
}
|
|
snapshot.update(changes)
|
|
envelope = {"revision": revision}
|
|
if details:
|
|
envelope["details"] = {
|
|
"supported": True,
|
|
"control_modes": ["rpm", "duty"],
|
|
"profile": {"revision": 2, "bindings": {}},
|
|
"devices": {"motor": {"uuid": "physical", "slot": "left.1", "label": "Left"}},
|
|
}
|
|
return {
|
|
"relay_id": "a" * 32,
|
|
"rover": snapshot,
|
|
"rover_details_v1": envelope,
|
|
"sample_clock": {"instance": hub.clock_id, "monotonic_ms": at * 1000},
|
|
}
|
|
|
|
|
|
def test_compact_metadata_preserves_every_live_value_age_and_release_truth():
|
|
now = [100.0]
|
|
hub = RoverControl(lambda: now[0])
|
|
body = packet(hub)
|
|
unchanged = copy.deepcopy(body)
|
|
hub.exchange("node", body)
|
|
assert body == unchanged
|
|
for release in (None, False, True):
|
|
now[0] += 0.1
|
|
live = packet(hub, now[0] - 0.03, details=False, release_confirmed=release)
|
|
live["rover"]["devices"][0]["values"]["erpm"] = 123
|
|
hub.exchange("node", live)
|
|
view = hub.view("node")
|
|
assert view["snapshot"]["release_confirmed"] is release
|
|
row = view["snapshot"]["devices"][0]
|
|
assert row["values"]["erpm"] == 123 and row["uuid"] == "physical"
|
|
assert row["session_id"] == "attachment" and 54 <= row["age_ms"] <= 56
|
|
assert view["snapshot"]["profile"]["revision"] == 2
|
|
assert not view["controlling"]
|
|
|
|
|
|
def test_metadata_reference_survives_delay_without_renewing_measurements():
|
|
now = [100.0]
|
|
hub = RoverControl(lambda: now[0])
|
|
hub.exchange("node", packet(hub, 99.2))
|
|
now[0] = 101.21
|
|
# A fresh sample asserts the unchanged metadata revision after a delayed
|
|
# preceding exchange. Labels/profile age must not reject fresh measurements.
|
|
hub.exchange("node", packet(hub, 101.2, details=False))
|
|
assert hub.view("node")["fresh"]
|
|
assert hub.view("node")["snapshot"]["devices"][0]["uuid"] == "physical"
|
|
now[0] = 103.3
|
|
hub.exchange("node", packet(hub, 101.3, details=False))
|
|
assert not hub.view("node")["fresh"]
|
|
assert hub.boards["node"]["seen"] == 101.3
|
|
assert not hub.view("node")["controlling"]
|
|
|
|
|
|
def test_late_full_metadata_does_not_rejuvenate_sample_or_lease():
|
|
now = [103.0]
|
|
hub = RoverControl(lambda: now[0])
|
|
hub.exchange("node", packet(hub, 100.0))
|
|
assert not hub.view("node")["fresh"]
|
|
assert hub.boards["node"]["seen"] == 100.0
|
|
with pytest.raises(ValueError, match="недоступен"):
|
|
hub.arm("node", {"standstill_confirmed": True, "current_a": 1, "max_erpm": 300})
|
|
# Latest-only cache is scoped to this relay/driver/revision, not a time
|
|
# series. A reference can recover publication, but cannot create a lease.
|
|
hub.exchange("node", packet(hub, 102.9, details=False))
|
|
assert hub.view("node")["fresh"]
|
|
assert not hub.view("node")["controlling"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"change", [{"relay_id": "b" * 32}, {"revision": 2}, {"instance": "new-driver"}]
|
|
)
|
|
def test_cached_identity_cannot_cross_relay_driver_or_revision(change):
|
|
hub = RoverControl(lambda: 100.0)
|
|
hub.exchange("node", packet(hub, 99.9))
|
|
value = packet(hub, details=False)
|
|
for k, v in change.items():
|
|
if k == "revision":
|
|
value["rover_details_v1"][k] = v
|
|
elif k == "instance":
|
|
value["rover"][k] = v
|
|
else:
|
|
value[k] = v
|
|
with pytest.raises(ValueError):
|
|
hub.exchange("node", value)
|
|
assert hub.boards["node"]["seen"] == 99.9
|
|
|
|
|
|
def test_metadata_revision_changes_immediately_and_old_packet_cannot_replace_it():
|
|
hub = RoverControl(lambda: 100.0)
|
|
hub.exchange("node", packet(hub, 99.8))
|
|
updated = packet(hub, 99.9, revision=2)
|
|
updated["rover_details_v1"]["details"]["profile"]["revision"] = 3
|
|
hub.exchange("node", updated)
|
|
hub.exchange("node", packet(hub, 99.8))
|
|
assert hub.view("node")["snapshot"]["profile"]["revision"] == 3
|
|
invalid = copy.deepcopy(updated)
|
|
invalid["sample_clock"]["monotonic_ms"] = 100000
|
|
invalid["rover_details_v1"]["details"]["profile"]["revision"] = 99
|
|
with pytest.raises(ValueError):
|
|
hub.exchange("node", invalid)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"change", ["duplicate", "foreign", "static_override", "missing_session", "bad_revision"]
|
|
)
|
|
def test_invalid_compact_snapshot_never_publishes_or_creates_authority(change):
|
|
hub = RoverControl(lambda: 100.0)
|
|
hub.exchange("node", packet(hub, 99.9))
|
|
value = packet(hub, details=False)
|
|
row = value["rover"]["devices"][0]
|
|
if change == "duplicate":
|
|
value["rover"]["devices"].append(copy.deepcopy(row))
|
|
if change == "foreign":
|
|
row["id"] = "foreign"
|
|
if change == "static_override":
|
|
row["uuid"] = "wrong"
|
|
if change == "missing_session":
|
|
del row["session_id"]
|
|
if change == "bad_revision":
|
|
value["rover_details_v1"]["revision"] = True
|
|
with pytest.raises(ValueError):
|
|
hub.exchange("node", value)
|
|
assert hub.boards["node"]["seen"] == 99.9
|
|
assert not hub.view("node")["controlling"]
|
|
|
|
|
|
def test_legacy_full_snapshot_remains_compatible_and_invalidates_metadata_cache():
|
|
hub = RoverControl(lambda: 100.0)
|
|
hub.exchange("node", packet(hub, 99.9))
|
|
legacy = {
|
|
"relay_id": "a" * 32,
|
|
"rover": {"instance": "driver", "supported": True, "state": "stopped", "devices": []},
|
|
}
|
|
hub.exchange("node", legacy)
|
|
assert hub.view("node")["snapshot"] == legacy["rover"]
|
|
with pytest.raises(ValueError):
|
|
hub.exchange("node", packet(hub, details=False))
|