Skip to content

Commit 4b9b69b

Browse files
add test for start_stim_checks
1 parent c6ec579 commit 4b9b69b

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

controller/src/controller/utils/serial_comm.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def convert_status_code_bytes_to_dict(status_code_bytes: bytes) -> dict[str, int
220220
status_code_labels = (
221221
"main_status",
222222
"index_of_thread_with_error",
223-
*[f"module_{i}_status" for i in range(24)],
223+
*[f"module_{i}_status" for i in range(NUM_WELLS)],
224224
)
225225
return {label: status_code_bytes[i] for i, label in enumerate(status_code_labels)}
226226

@@ -446,7 +446,8 @@ def convert_stim_bytes_to_dict(stim_bytes: bytes) -> dict[str, Any]:
446446
stim_info_dict: dict[str, Any] = {
447447
"protocols": [],
448448
"protocol_assignments": {
449-
GENERIC_24_WELL_DEFINITION.get_well_name_from_well_index(well_idx): None for well_idx in range(24)
449+
GENERIC_24_WELL_DEFINITION.get_well_name_from_well_index(well_idx): None
450+
for well_idx in range(NUM_WELLS)
450451
},
451452
}
452453

controller/tests/subsystems/test_instrument_comm.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,31 @@
33
from collections import deque
44
from random import choice
55
from random import randint
6+
import struct
67

78
from controller.constants import CURI_VID
9+
from controller.constants import NUM_WELLS
810
from controller.constants import SERIAL_COMM_BAUD_RATE
911
from controller.constants import SERIAL_COMM_BYTESIZE
1012
from controller.constants import SERIAL_COMM_READ_TIMEOUT
1113
from controller.constants import SerialCommPacketTypes
14+
from controller.constants import STIM_WELL_IDX_TO_MODULE_ID
15+
from controller.constants import StimulatorCircuitStatuses
1216
from controller.constants import STM_VID
1317
from controller.exceptions import InstrumentCommandResponseError
1418
from controller.exceptions import NoInstrumentDetectedError
1519
from controller.subsystems import instrument_comm
1620
from controller.subsystems.instrument_comm import InstrumentComm
1721
from controller.utils.aio import clean_up_tasks
22+
from controller.utils.serial_comm import convert_adc_readings_to_circuit_status
1823
from controller.utils.serial_comm import create_data_packet
1924
import pytest
2025
import serial
2126
from serial.tools.list_ports_common import ListPortInfo
2227

2328
from ..fixtures import fixture__wait_tasks_clean
2429
from ..helpers import compare_exceptions
30+
from ..helpers import random_bool
2531
from ..helpers import random_serial_comm_timestamp
2632

2733

@@ -310,20 +316,52 @@ async def test_InstrumentComm__handles_start_stim_checks_command__success(
310316
):
311317
test_ic, test_instrument = test_instrument_comm_obj_with_connection
312318

313-
test_command = {"command": "start_stim_checks"}
319+
test_well_indices = list(range(4))
320+
test_well_indices.extend([i for i in range(4, NUM_WELLS) if random_bool()])
321+
322+
# set known adc readings in simulator. these first 4 values are hard coded, if this test fails might need to update them
323+
adc_readings = [(0, 0), (0, 2039), (0, 2049), (1113, 0)]
324+
adc_readings.extend([(i, i + 100) for i in range(NUM_WELLS - len(adc_readings))])
325+
326+
test_command = {"command": "start_stim_checks", "well_indices": test_well_indices}
314327

315328
run_task = asyncio.create_task(test_ic.run(asyncio.Future()))
316329

317330
await test_ic._from_monitor_queue.put(test_command)
331+
318332
# set up response
333+
adc_readings_ordered_by_module_id = [None] * NUM_WELLS
334+
for well_idx, readings in enumerate(adc_readings):
335+
module_id = STIM_WELL_IDX_TO_MODULE_ID[well_idx]
336+
adc_readings_ordered_by_module_id[module_id] = readings
337+
response_body = bytes([])
338+
for module_readings in adc_readings_ordered_by_module_id:
339+
status = convert_adc_readings_to_circuit_status(*module_readings)
340+
response_body += struct.pack("<HHB", *module_readings, status)
319341
test_instrument.send.append(
320342
create_data_packet(
321-
random_serial_comm_timestamp(), SerialCommPacketTypes.STIM_IMPEDANCE_CHECK, bytes([0])
343+
random_serial_comm_timestamp(), SerialCommPacketTypes.STIM_IMPEDANCE_CHECK, response_body
322344
)
323345
)
324346

325-
assert await asyncio.wait_for(test_ic._comm_to_monitor_queue.get(), timeout=1) == test_command
326-
327-
# TODO assert something else here?
347+
msg_to_main = await asyncio.wait_for(test_ic._comm_to_monitor_queue.get(), timeout=1)
348+
349+
# make sure that the message sent back to main contains the correct values
350+
stimulator_circuit_statuses = {}
351+
for well_idx in test_well_indices:
352+
well_readings = adc_readings[well_idx]
353+
status_int = convert_adc_readings_to_circuit_status(*well_readings)
354+
status = list(StimulatorCircuitStatuses)[status_int + 1].name.lower()
355+
stimulator_circuit_statuses[well_idx] = status
356+
357+
assert msg_to_main == {
358+
**test_command,
359+
"stimulator_circuit_statuses": stimulator_circuit_statuses,
360+
"adc_readings": {
361+
well_idx: readings
362+
for well_idx, readings in enumerate(adc_readings)
363+
if well_idx in test_well_indices
364+
},
365+
}
328366

329367
await clean_up_tasks({run_task})

0 commit comments

Comments
 (0)