"""Commands must be admitted by live, bound Node evidence, not remembered inventory.""" import copy import pytest from k1link.fleet.uplinks import Uplinks A, B, REV, OP = "a" * 36, "b" * 36, "c" * 64, "d" * 32 ROW = {"node_id": "board", "enrollment": "paired", "binding": {"binding_id": "pair"}} PAYLOAD = { "execution_binding": {"agent_instance_id": "runtime"}, "uplink": { "supported": True, "protocol": 2, "path_verified": True, "revision": REV, "current_id": A, "candidates": [ {"id": A, "label": "radio", "interface": "radio7", "kind": "wifi", "verified": True}, {"id": B, "label": "cable", "interface": "port8", "kind": "ethernet", "verified": True}, ], }, } @pytest.fixture def owner(): now = [1000] owner = Uplinks(lambda: now[0]) owner.heartbeat(ROW, PAYLOAD) return owner, now def start(u): return u.start(ROW, B, REV, "runtime", OP) def test_fresh_authority_duplicate_and_ack(owner): u, _ = owner assert start(u)["phase"] == "queued" assert start(u)["id"] == OP assert u.heartbeat(ROW, PAYLOAD)["uplink_change"]["target"] == B p = copy.deepcopy(PAYLOAD) p["uplink"]["operation"] = dict( id=OP, binding="pair:runtime", target=B, phase="verifying", nonce="f" * 32, path_verified=True, ) assert u.heartbeat(ROW, p)["uplink_ack"]["id"] == OP p["uplink"]["operation"]["phase"] = "applied" assert u.heartbeat(ROW, p) == {} assert u.view(ROW)["operation"]["phase"] == "applied" def test_old_missing_and_stale_telemetry_has_no_authority(owner): u, now = owner now[0] += 21 assert not u.view(ROW)["available"] with pytest.raises(ValueError): start(u) assert u.heartbeat(ROW, {"execution_binding": {"agent_instance_id": "runtime"}}) == {} assert u.view(ROW)["candidates"] == [] @pytest.mark.parametrize("kind", ["runtime", "binding", "unverified", "unsupported"]) def test_changed_scope_and_failed_checks_reject_command(owner, kind): u, _ = owner p, row = copy.deepcopy(PAYLOAD), copy.deepcopy(ROW) if kind == "runtime": p["execution_binding"]["agent_instance_id"] = "new-runtime" elif kind == "binding": row["binding"]["binding_id"] = "new-pair" elif kind == "unverified": p["uplink"]["candidates"][1]["verified"] = False else: p["uplink"]["supported"] = False u.heartbeat(row, p) with pytest.raises(ValueError): start(u) def test_restarted_node_never_replays_or_receives_old_ack(owner): u, _ = owner start(u) p = copy.deepcopy(PAYLOAD) p["execution_binding"]["agent_instance_id"] = "new-runtime" assert not u.heartbeat(ROW, p) assert u.view(ROW)["operation"]["phase"] == "unknown" def test_timeout_is_unknown_not_false_rollback_success(owner): u, now = owner start(u) now[0] += 31 assert not u.heartbeat(ROW, PAYLOAD) assert u.view(ROW)["operation"]["phase"] == "unknown" def test_invalid_payload_does_not_interrupt_other_heartbeat_work(owner): u, _ = owner p = copy.deepcopy(PAYLOAD) p["uplink"]["candidates"] *= 20 assert u.heartbeat(ROW, p) == {} assert not u.view(ROW)["available"] def test_motion_guard_blocks_admission_and_is_released_after_rollback(): from k1link.fleet.rover_control import RoverControl control = RoverControl() u = Uplinks(lambda: 1000, guard=control.network_change) u.heartbeat(ROW, PAYLOAD) start(u) with pytest.raises(ValueError, match="переключения"): control.arm("board", {"standstill_confirmed": True, "current_a": 20, "max_erpm": 500}) p = copy.deepcopy(PAYLOAD) p["uplink"]["operation"] = dict(id=OP, binding="pair:runtime", target=B, phase="rolled_back") u.heartbeat(ROW, p) assert "board" not in control.network_changes def test_unproved_vpn_path_cannot_receive_commit_ack(owner): u, _ = owner start(u) p = copy.deepcopy(PAYLOAD) p["uplink"]["operation"] = dict( id=OP, binding="pair:runtime", target=B, phase="verifying", nonce="f" * 32, path_verified=False, ) assert "uplink_ack" not in u.heartbeat(ROW, p) def test_old_metric_only_node_cannot_admit_selection(owner): u, _ = owner p = copy.deepcopy(PAYLOAD) p["uplink"].pop("protocol") u.heartbeat(ROW, p) assert not u.view(ROW)["available"]