Files
NODEDC_MISSION_CORE/tests/fleet/test_board_events.py
T

139 lines
5.4 KiB
Python

"""Disconnected simulated board, actual history and settings, no socket or hardware."""
from test_pairing import cert, create, heartbeat
from test_pairing import setup as setup
from test_rover_control import arm, cmd, exchange
from test_rover_control import hub as hub
from k1link.fleet.board_event_transitions import BoardPresence, publish_control
from k1link.fleet.node_operator_events import accept_operator_events
from k1link.fleet.power import SCHEMA, BoardPower
from k1link.notifications import NotificationHistory
ROW = {
"id": "board",
"node_id": "node",
"binding": {"binding_id": "owner"},
"name": "Тест",
"enrollment": "paired",
}
def test_silence_and_return_use_local_deadline_only(tmp_path):
h = NotificationHistory(tmp_path)
now = [50]
presence = BoardPresence(h, lambda: now[0], lambda: now[0] + 100)
presence.expire(ROW)
presence.received(ROW)
for now[0] in (58, 65, 80):
presence.received(ROW)
assert not h.listing()["items"]
for now[0] in (100, 102, 119):
presence.expire(ROW)
presence.received(ROW)
presence.received(ROW)
now[0] = 150
presence.received(ROW) # Race: heartbeat before the expiry worker.
assert [i["at"] for i in h.listing(ascending=True)["items"]] == [200, 219, 239, 250]
reopened = BoardPresence(h)
reopened.expire(ROW)
assert len(h.listing()["items"]) == 4
def test_power_ack_only_and_switches_and_bounded_replay(tmp_path):
history = NotificationHistory(tmp_path)
power = BoardPower()
power.notifications = history
epoch, ident = "a" * 43, "00000000-0000-4000-8000-000000000000"
data = dict(schema=SCHEMA, epoch=epoch, available=True)
power.heartbeat(ROW, {"power": data})
power.start(ROW, epoch, ident)
assert not history.listing()["items"]
for _ in range(6):
power.heartbeat(ROW, {"power": {**data, "result": {"id": ident, "state": "accepted"}}})
history.configure("frames.loss", False)
data = [
{
"id": str(n) * 43,
"at": 150,
"kind": kind,
**(
{"device": "instax4_" + "f" * 32} if kind.startswith(("frames.", "camera.")) else {}
),
}
for n, kind in enumerate(
(
"board.boot",
"camera.loss",
"frames.loss",
"frames.recovery",
"camera.recovery",
"thermal.loss",
"thermal.recovery",
)
)
]
for _ in range(3):
assert accept_operator_events(history, ROW, data)["operator_events_ack"] == data[-1]["id"]
assert len(history.listing()["items"]) == 7
assert "frames.loss" in {i["key"] for i in history.preferences() if not i["enabled"]}
assert not accept_operator_events(history, ROW, [{**data[1], "device": "forged"}])
assert not accept_operator_events(history, ROW, [{**data[0], "kind": "board.lost"}])
def test_authority_stop_and_new_ready_without_driver_commands(hub, tmp_path): # noqa: F811
control, clock = hub
history = NotificationHistory(tmp_path)
session = arm(control)
# Keep the operator lease alive past the telemetry deadline.
for n in range(2, 15):
clock[0] += 0.085
control.command("node", cmd(session, n))
control.stream("node", "a" * 32)
for _ in range(3):
publish_control(history, ROW, control)
items = history.listing()["items"]
assert len(items) == 1 and "1000 мс" in items[0]["description"]
driver = {"kind": "control.stop", "session": session, "id": "a" * 43, "at": 150}
for _ in range(3):
assert accept_operator_events(history, ROW, [driver])["operator_events_ack"] == driver["id"]
assert len(history.listing()["items"]) == 1
exchange(control)
second = arm(control)
exchange(control, state="ready", session_id=second)
exchange(control, state="ready", session_id=second)
control.command("node", cmd(second, 3, stop=True))
publish_control(history, ROW, control)
assert len(history.listing()["items"]) == 2
assert "готовность" in history.listing()["items"][0]["description"]
def test_paired_heartbeat_and_liveness_emit_without_core_requests(setup):
fleet, _, _, requests = setup
public, _ = create(setup)
fleet.advance(public["id"])
row = fleet.find(public["id"])
leaf, body = cert(row), heartbeat(row)
requests.clear()
now = [100.0]
fleet.board_presence = BoardPresence(fleet.notifications, lambda: now[0], lambda: now[0])
event = {"id": "h" * 43, "kind": "thermal.loss", "at": 100}
for _ in range(3):
status, reply = fleet.receive(
leaf, "/v1/node/heartbeat", {**body, "operator_events_v1": [event]}
)
assert status == 200 and reply["operator_events_ack"] == event["id"]
now[0] = 122
fleet.board_presence.expire(row)
live = {k: v for k, v in body.items() if k not in ("host", "devices")}
assert fleet.receive(leaf, "/v1/node/liveness", {**live, "binding_id": "invalid"})[0] != 200
for _ in range(3):
assert fleet.receive(leaf, "/v1/node/liveness", live)[0] == 200
items = fleet.notifications.listing(ascending=True)["items"]
assert [r["description"] for r in items] == [
"Ядро включило тепловое ограничение процессора.",
"Борт перестал выходить на связь.",
"Связь восстановлена.",
]
assert requests == []