from unittest.mock import MagicMock import pytest from aarecommon.math.coordinate import Coordinate, SmargonCoordinate from aarecommon.models.raster_grid import RasterGridRequest from aarecommon.models.rotation_scan import RotationScanRequest from mx_bec.decisions import smargon_nudge from mx_bec.tasks import MxTasks, Server # where the mock smargon sits SMARGON_POSITION = {"x": 1.0, "y": 2.0, "z": 3.0, "phi": 0.0, "chi": 0.0} @pytest.fixture def client(): client = MagicMock() for axis, position in SMARGON_POSITION.items(): getattr(client.devices.smargon, axis).get.return_value = position return client @pytest.fixture def tasks(client): return MxTasks[Server](client) @pytest.mark.parametrize( "name", ["aaredaq_mount_sample", "aaredaq_raster_scan", "aaredaq_rotation_scan"] ) def test_aaredaq_methods_are_tasks(name): assert MxTasks.is_task(name) def test_mount_sample(tasks, client): tasks.aaredaq_mount_sample(42) client.aaredaq.mount_sample.assert_called_once_with(42, reference=False) tasks.aaredaq_mount_sample(7, reference=True) client.aaredaq.mount_sample.assert_called_with(7, reference=True) def test_raster_scan_builds_request_from_flat_arguments(tasks, client): tasks.aaredaq_raster_scan( n_x=4, n_y=3, step_x_mm=0.05, step_y_mm=0.04, exp_time_s=0.1, omega_deg=12.0, dtz=150.0 ) request, kwargs = ( client.aaredaq.raster_scan.call_args.args[0], client.aaredaq.raster_scan.call_args.kwargs, ) # without a top left corner, the grid is centred on the current smargon position: the corner # is half the grid away, in the lab frame at the scan's omega half_grid = Coordinate(x=4 * 0.05 / 2, y=3 * 0.04 / 2, z=0) top_left = Coordinate(x=1.0, y=2.0, z=3.0) - smargon_nudge(0.0, 0.0, 12.0, half_grid) assert request == RasterGridRequest( n_x=4, n_y=3, grid_size_mm=Coordinate(x=0.05, y=0.04), exp_time_s=0.1, omega_deg=12.0, dtz=150.0, smargon_top_left=SmargonCoordinate(sh_mm=top_left, chi_deg=0.0, phi_deg=0.0), ) assert kwargs == {"auto_center": False} def test_raster_scan_uses_a_given_top_left_corner(tasks, client): top_left = SmargonCoordinate(sh_mm=Coordinate(x=0.1, y=0.2, z=0.3), chi_deg=10.0, phi_deg=20.0) tasks.aaredaq_raster_scan(1, 1, 0.05, 0.05, 0.1, smargon_top_left=top_left) assert client.aaredaq.raster_scan.call_args.args[0].smargon_top_left == top_left client.devices.smargon.x.get.assert_not_called() def test_raster_scan_passes_auto_center(tasks, client): tasks.aaredaq_raster_scan(1, 1, 0.05, 0.05, 0.1, auto_center=True) assert client.aaredaq.raster_scan.call_args.kwargs == {"auto_center": True} def test_rotation_scan_builds_request_from_flat_arguments(tasks, client): tasks.aaredaq_rotation_scan( exp_time_s=0.05, incr_omega_deg=0.1, steps=100, start_omega_deg=90.0, file_prefix="rot" ) assert client.aaredaq.rotation_scan.call_args.args[0] == RotationScanRequest( exp_time_s=0.05, incr_omega_deg=0.1, steps=100, start_omega_deg=90.0, file_prefix="rot" ) def test_screening_scan(tasks, client): tasks.aaredaq_rotation_scan(0.05, 1.0, 4, screening=True, wedge_omega_deg=90.0) request = client.aaredaq.rotation_scan.call_args.args[0] assert request.screening is True and request.wedge_omega_deg == 90.0 def test_failed_aaredaq_call_pauses_queue_and_reraises(tasks, client): client.aaredaq.mount_sample.side_effect = RuntimeError("sample not found") with pytest.raises(RuntimeError, match="sample not found"): tasks.aaredaq_mount_sample(1) client.task_queue.pause.assert_called_once()