|
| 1 | +"""Test Flex Stacker home command implementation.""" |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +import pytest |
| 6 | +from decoy import Decoy, matchers |
| 7 | + |
| 8 | +from opentrons.drivers.flex_stacker.types import StackerAxis |
| 9 | +from opentrons.hardware_control.modules import FlexStacker |
| 10 | +from opentrons.protocol_engine.commands.flex_stacker.common import ( |
| 11 | + FlexStackerStallOrCollisionError, |
| 12 | +) |
| 13 | +from opentrons.protocol_engine.resources import ModelUtils |
| 14 | +from opentrons.protocol_engine.state.state import StateView |
| 15 | +from opentrons.protocol_engine.state.module_substates import ( |
| 16 | + FlexStackerSubState, |
| 17 | + FlexStackerId, |
| 18 | +) |
| 19 | +from opentrons.protocol_engine.execution import EquipmentHandler |
| 20 | +from opentrons.protocol_engine.commands import flex_stacker |
| 21 | +from opentrons.protocol_engine.commands.command import SuccessData, DefinedErrorData |
| 22 | +from opentrons.protocol_engine.commands.flex_stacker.home import HomeImpl |
| 23 | + |
| 24 | +from opentrons_shared_data.errors.exceptions import FlexStackerStallError |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def subject( |
| 29 | + state_view: StateView, equipment: EquipmentHandler, model_utils: ModelUtils |
| 30 | +) -> HomeImpl: |
| 31 | + """Get a home command to test.""" |
| 32 | + return HomeImpl(state_view=state_view, equipment=equipment, model_utils=model_utils) |
| 33 | + |
| 34 | + |
| 35 | +async def test_home_command( |
| 36 | + decoy: Decoy, |
| 37 | + state_view: StateView, |
| 38 | + equipment: EquipmentHandler, |
| 39 | + subject: HomeImpl, |
| 40 | + stacker_id: FlexStackerId, |
| 41 | + stacker_hardware: FlexStacker, |
| 42 | +) -> None: |
| 43 | + """It should return a success data.""" |
| 44 | + data = flex_stacker.HomeParams(moduleId=stacker_id) |
| 45 | + |
| 46 | + fs_module_substate = FlexStackerSubState( |
| 47 | + module_id=stacker_id, |
| 48 | + pool_primary_definition=None, |
| 49 | + pool_adapter_definition=None, |
| 50 | + pool_lid_definition=None, |
| 51 | + pool_count=0, |
| 52 | + max_pool_count=0, |
| 53 | + ) |
| 54 | + decoy.when( |
| 55 | + state_view.modules.get_flex_stacker_substate(module_id=stacker_id) |
| 56 | + ).then_return(fs_module_substate) |
| 57 | + |
| 58 | + result = await subject.execute(data) |
| 59 | + |
| 60 | + decoy.verify(await stacker_hardware.home_all(), times=1) |
| 61 | + |
| 62 | + assert result == SuccessData(public=flex_stacker.HomeResult()) |
| 63 | + |
| 64 | + |
| 65 | +async def test_home_command_with_stall_detected( |
| 66 | + decoy: Decoy, |
| 67 | + state_view: StateView, |
| 68 | + equipment: EquipmentHandler, |
| 69 | + subject: HomeImpl, |
| 70 | + model_utils: ModelUtils, |
| 71 | + stacker_id: FlexStackerId, |
| 72 | + stacker_hardware: FlexStacker, |
| 73 | +) -> None: |
| 74 | + """It should return a success data.""" |
| 75 | + err_id = "error-id" |
| 76 | + err_timestamp = datetime(year=2025, month=3, day=19) |
| 77 | + |
| 78 | + data = flex_stacker.HomeParams(moduleId=stacker_id) |
| 79 | + |
| 80 | + fs_module_substate = FlexStackerSubState( |
| 81 | + module_id=stacker_id, |
| 82 | + pool_primary_definition=None, |
| 83 | + pool_adapter_definition=None, |
| 84 | + pool_lid_definition=None, |
| 85 | + pool_count=0, |
| 86 | + max_pool_count=0, |
| 87 | + ) |
| 88 | + decoy.when( |
| 89 | + state_view.modules.get_flex_stacker_substate(module_id=stacker_id) |
| 90 | + ).then_return(fs_module_substate) |
| 91 | + decoy.when(model_utils.generate_id()).then_return(err_id) |
| 92 | + decoy.when(model_utils.get_timestamp()).then_return(err_timestamp) |
| 93 | + |
| 94 | + decoy.when(await stacker_hardware.home_all()).then_raise( |
| 95 | + FlexStackerStallError(serial="123", axis=StackerAxis.Z) |
| 96 | + ) |
| 97 | + |
| 98 | + result = await subject.execute(data) |
| 99 | + |
| 100 | + assert result == DefinedErrorData( |
| 101 | + public=FlexStackerStallOrCollisionError.model_construct( |
| 102 | + id=err_id, |
| 103 | + createdAt=err_timestamp, |
| 104 | + wrappedErrors=[matchers.Anything()], |
| 105 | + ) |
| 106 | + ) |
0 commit comments