Files
line-laser-modbus/tests/test_state_machine.py
T
2026-06-18 17:24:59 +08:00

65 lines
1.9 KiB
Python

import pytest
from line_laser_modbus.models import (
NORMAL_MODE_COMMANDS,
DeviceStatus,
ModeCommand,
can_switch_mode,
validate_mode_switch,
)
def test_normal_mode_commands_match_v13_range() -> None:
assert {mode.value for mode in NORMAL_MODE_COMMANDS} == {0, 1, 2, 3, 4}
def test_emergency_stop_is_not_a_normal_mode_switch_target() -> None:
assert not can_switch_mode(ModeCommand.MANUAL_TEACHING, ModeCommand.EMERGENCY_STOP)
def test_teaching_done_can_enter_tracking_or_replay() -> None:
assert can_switch_mode(
ModeCommand.PRE_WELD_TEACHING,
ModeCommand.MANUAL_TEACHING,
DeviceStatus.TEACHING_DONE,
)
assert can_switch_mode(
ModeCommand.PRE_WELD_TEACHING,
ModeCommand.ONLINE_TRACKING,
DeviceStatus.TEACHING_DONE,
)
assert can_switch_mode(
ModeCommand.PRE_WELD_TEACHING,
ModeCommand.TRAJECTORY_REPLAY,
DeviceStatus.TEACHING_DONE,
)
def test_calibration_done_returns_to_idle_command() -> None:
assert can_switch_mode(
ModeCommand.CALIBRATION,
ModeCommand.MANUAL_TEACHING,
DeviceStatus.CALIBRATION_DONE,
)
assert not can_switch_mode(
ModeCommand.CALIBRATION,
ModeCommand.ONLINE_TRACKING,
DeviceStatus.CALIBRATION_DONE,
)
assert not can_switch_mode(
ModeCommand.CALIBRATION,
ModeCommand.TRAJECTORY_REPLAY,
DeviceStatus.CALIBRATION_DONE,
)
def test_tracking_and_replay_only_switch_to_each_other() -> None:
assert can_switch_mode(ModeCommand.ONLINE_TRACKING, ModeCommand.TRAJECTORY_REPLAY)
assert can_switch_mode(ModeCommand.TRAJECTORY_REPLAY, ModeCommand.ONLINE_TRACKING)
assert not can_switch_mode(ModeCommand.ONLINE_TRACKING, ModeCommand.MANUAL_TEACHING)
def test_invalid_transition_raises() -> None:
with pytest.raises(ValueError, match="Illegal mode switch"):
validate_mode_switch(ModeCommand.MANUAL_TEACHING, ModeCommand.EMERGENCY_STOP)