feat(fleet): integrate independent trackers, VPS pairing and recoverable commands
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import time
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from k1link.fleet.equipment_bootstrap import apply
|
||||
from k1link.fleet.independent_equipment import EquipmentError
|
||||
|
||||
|
||||
def context():
|
||||
item = {
|
||||
"id": "device",
|
||||
"revision": 1,
|
||||
"vehicle_id": "rover",
|
||||
"settings": {"endpoint": {"slot": 1, "host": "owner.example", "port": 9922}},
|
||||
}
|
||||
host = {
|
||||
"state": "paired",
|
||||
"vehicle_ids": ["rover"],
|
||||
"status": {"version": "0.1.15", "tracker": {"host": "owner.example", "port": 9922}},
|
||||
"last_sync_at": time.time(),
|
||||
}
|
||||
import threading
|
||||
|
||||
equipment = SimpleNamespace(
|
||||
listing=lambda: [item], require_idle=Mock(), apply=Mock(return_value={"ok": True})
|
||||
)
|
||||
hosts = SimpleNamespace(
|
||||
equipment=equipment, lock=threading.RLock(), rows=lambda: [host], sync=Mock()
|
||||
)
|
||||
return hosts, item, host
|
||||
|
||||
|
||||
def test_initial_usb_apply_admits_the_device_to_paired_vps_before_write():
|
||||
hosts, item, host = context()
|
||||
hosts.equipment.apply.side_effect = lambda *args: (
|
||||
hosts.sync.assert_called_once_with(host) or {"ok": True}
|
||||
)
|
||||
assert apply(hosts, "device", 1) == {"ok": True}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"case", ["stale", "wrong_vehicle", "not_paired", "old_host", "unqualified_host", "no_endpoint"]
|
||||
)
|
||||
def test_initial_usb_apply_rejects_unowned_or_incompatible_destination(case):
|
||||
hosts, item, host = context()
|
||||
if case == "stale":
|
||||
item["revision"] = 2
|
||||
if case == "wrong_vehicle":
|
||||
host["vehicle_ids"] = []
|
||||
if case == "not_paired":
|
||||
host["state"] = "offline"
|
||||
if case == "old_host":
|
||||
host["status"]["version"] = "0.1.14"
|
||||
if case == "unqualified_host":
|
||||
host["status"]["version"] = "0.1.999"
|
||||
if case == "no_endpoint":
|
||||
item["settings"]["endpoint"] = None
|
||||
with pytest.raises(EquipmentError):
|
||||
apply(hosts, "device", 1)
|
||||
hosts.equipment.apply.assert_not_called()
|
||||
|
||||
|
||||
def test_initial_usb_apply_accepts_current_shipped_host():
|
||||
import ast
|
||||
from pathlib import Path
|
||||
|
||||
# Read the shipped artifact's version without importing its hardware service.
|
||||
source = Path(__file__).resolve().parents[1] / "apps/vps-host/ndc_vps/__init__.py"
|
||||
module = ast.parse(source.read_text())
|
||||
version = next(
|
||||
ast.literal_eval(n.value)
|
||||
for n in module.body
|
||||
if isinstance(n, ast.Assign)
|
||||
and any(isinstance(t, ast.Name) and t.id == "VERSION" for t in n.targets)
|
||||
)
|
||||
hosts, _, host = context()
|
||||
host["status"]["version"] = version
|
||||
assert apply(hosts, "device", 1) == {"ok": True}
|
||||
hosts.equipment.apply.assert_called_once_with("device", 1)
|
||||
Reference in New Issue
Block a user