test_resolve_expected_tray_2587.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """Unit tests for runout expected-slot resolution (#2587).
  2. When a print pauses on an AMS filament runout, the firmware reports the slot it
  3. now expects (``tray_tar``) and the slot that ran out (``tray_pre``) as bare
  4. numbers whose meaning depends on the AMS layout. ``resolve_expected_tray``
  5. globalises them to the same numbering the AMS graphic already highlights, so the
  6. UI can point the operator at the right physical slot — critical with AMS
  7. Filament Backup, which advances to the next compatible slot rather than
  8. re-accepting the depleted one (reporter @Jostxxl saw the print wait on Slot 3
  9. after Slot 2 ran out).
  10. """
  11. from backend.app.services.printer_manager import resolve_expected_tray
  12. def _single_regular():
  13. return [(0, False)]
  14. def _dual_regular():
  15. return [(0, False), (1, False)]
  16. class TestSingleRegularAms:
  17. """One 4-slot AMS: the local slot IS the global tray ID (matches tray_now)."""
  18. def test_reporter_scenario_tray_tar_2_is_slot_3(self):
  19. # @Jostxxl: tray_tar=2 (zero-based) -> physical Slot 3.
  20. assert resolve_expected_tray(2, _single_regular(), None) == 2
  21. def test_reporter_scenario_tray_pre_1_is_slot_2(self):
  22. # @Jostxxl: tray_pre=1 (zero-based) -> physical Slot 2 (the one that ran out).
  23. assert resolve_expected_tray(1, _single_regular(), None) == 1
  24. def test_all_four_slots_pass_through(self):
  25. for slot in range(4):
  26. assert resolve_expected_tray(slot, _single_regular(), None) == slot
  27. def test_single_ams_with_nonzero_id(self):
  28. # A lone AMS reporting id=1 globalises to 4+slot, not the bare slot.
  29. assert resolve_expected_tray(2, [(1, False)], None) == 6
  30. class TestSentinels:
  31. """255 = none/idle, 254 = external spool, -1 = never-set."""
  32. def test_none_input(self):
  33. assert resolve_expected_tray(None, _single_regular(), None) is None
  34. def test_255_is_none(self):
  35. assert resolve_expected_tray(255, _single_regular(), None) is None
  36. def test_minus_one_is_none(self):
  37. assert resolve_expected_tray(-1, _single_regular(), None) is None
  38. def test_254_external_passes_through(self):
  39. assert resolve_expected_tray(254, _single_regular(), None) == 254
  40. class TestAmsHt:
  41. """AMS-HT reports a global ID (128-135) directly — return it unchanged."""
  42. def test_ht_global_id_passthrough(self):
  43. assert resolve_expected_tray(129, [(129, True)], None) == 129
  44. def test_ht_alongside_regular(self):
  45. layout = [(0, False), (128, True)]
  46. assert resolve_expected_tray(128, layout, None) == 128
  47. # A 0-3 target still resolves against the single regular unit.
  48. assert resolve_expected_tray(3, layout, None) == 3
  49. class TestMultiRegularAms:
  50. """Several 4-slot AMS: local slot is ambiguous; resolve via the mapping field.
  51. The snow-encoded mapping is ``ams_hw_id*256 + slot`` per entry.
  52. """
  53. def test_resolves_unambiguous_slot_via_mapping(self):
  54. # Mapping says slot 2 lives on AMS 1 -> global 1*4+2 = 6.
  55. mapping = [1 * 256 + 2]
  56. assert resolve_expected_tray(2, _dual_regular(), mapping) == 6
  57. def test_resolves_slot_on_ams0(self):
  58. mapping = [0 * 256 + 3]
  59. assert resolve_expected_tray(3, _dual_regular(), mapping) == 3
  60. def test_ambiguous_when_two_units_match_returns_none(self):
  61. # Both AMS 0 and AMS 1 have slot 1 mapped -> can't disambiguate.
  62. mapping = [0 * 256 + 1, 1 * 256 + 1]
  63. assert resolve_expected_tray(1, _dual_regular(), mapping) is None
  64. def test_no_mapping_returns_none(self):
  65. # Honest "can't determine" rather than guessing AMS 0.
  66. assert resolve_expected_tray(2, _dual_regular(), None) is None
  67. def test_unmapped_sentinel_ignored(self):
  68. # 65535 = unmapped; only the real entry counts.
  69. mapping = [65535, 1 * 256 + 0]
  70. assert resolve_expected_tray(0, _dual_regular(), mapping) == 4
  71. def test_ht_in_multi_layout_via_mapping(self):
  72. # A slot-0 target that maps to an AMS-HT hw id resolves to that global ID.
  73. layout = [(0, False), (1, False), (128, True)]
  74. mapping = [128 * 256 + 0]
  75. assert resolve_expected_tray(0, layout, mapping) == 128
  76. class TestGlobalAndOutOfRange:
  77. def test_already_global_regular_id_passthrough(self):
  78. # 4-15 is already a global regular-AMS ID.
  79. assert resolve_expected_tray(6, _dual_regular(), None) == 6
  80. def test_out_of_range_returns_none(self):
  81. assert resolve_expected_tray(200, _single_regular(), None) is None
  82. def test_zero_slot_no_regular_ams_returns_none(self):
  83. # Only an AMS-HT present but a 0-3 target — can't place it.
  84. assert resolve_expected_tray(1, [(128, True)], None) is None