test_scheduler_ams_mapping.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. """Tests for the AMS mapping computation in the print scheduler."""
  2. import pytest
  3. from backend.app.services.print_scheduler import PrintScheduler
  4. class TestSchedulerAmsMappingHelpers:
  5. """Test the AMS mapping helper methods in PrintScheduler."""
  6. @pytest.fixture
  7. def scheduler(self):
  8. return PrintScheduler()
  9. def test_normalize_color_with_hash(self, scheduler):
  10. """Color with hash should return #RRGGBB format."""
  11. result = scheduler._normalize_color("#FF5500")
  12. assert result == "#FF5500"
  13. def test_normalize_color_without_hash(self, scheduler):
  14. """Color without hash should add hash prefix."""
  15. result = scheduler._normalize_color("FF5500")
  16. assert result == "#FF5500"
  17. def test_normalize_color_with_alpha(self, scheduler):
  18. """Color with alpha channel should strip it."""
  19. result = scheduler._normalize_color("FF5500AA")
  20. assert result == "#FF5500"
  21. def test_normalize_color_none(self, scheduler):
  22. """None color should return default gray."""
  23. result = scheduler._normalize_color(None)
  24. assert result == "#808080"
  25. def test_normalize_color_empty(self, scheduler):
  26. """Empty color should return default gray."""
  27. result = scheduler._normalize_color("")
  28. assert result == "#808080"
  29. def test_normalize_color_for_compare(self, scheduler):
  30. """Color for compare should be lowercase without hash."""
  31. result = scheduler._normalize_color_for_compare("#FF5500")
  32. assert result == "ff5500"
  33. def test_normalize_color_for_compare_with_alpha(self, scheduler):
  34. """Alpha channel should be stripped for comparison."""
  35. result = scheduler._normalize_color_for_compare("#FF5500AA")
  36. assert result == "ff5500"
  37. def test_colors_are_similar_exact_match(self, scheduler):
  38. """Exact same colors should be similar."""
  39. assert scheduler._colors_are_similar("#FF5500", "#FF5500") is True
  40. def test_colors_are_similar_within_threshold(self, scheduler):
  41. """Colors within threshold should be similar."""
  42. # Red difference of 10, well within default threshold of 40
  43. assert scheduler._colors_are_similar("#FF5500", "#F55500") is True
  44. def test_colors_are_similar_outside_threshold(self, scheduler):
  45. """Colors outside threshold should not be similar."""
  46. # Red: FF (255) vs 00 (0) = 255 difference
  47. assert scheduler._colors_are_similar("#FF0000", "#00FF00") is False
  48. def test_colors_are_similar_none_colors(self, scheduler):
  49. """None colors should not be similar."""
  50. assert scheduler._colors_are_similar(None, "#FF5500") is False
  51. assert scheduler._colors_are_similar("#FF5500", None) is False
  52. class TestBuildLoadedFilaments:
  53. """Test the _build_loaded_filaments method."""
  54. @pytest.fixture
  55. def scheduler(self):
  56. return PrintScheduler()
  57. def test_build_loaded_filaments_empty_status(self, scheduler):
  58. """Empty status should return empty list."""
  59. class MockStatus:
  60. raw_data = {}
  61. result = scheduler._build_loaded_filaments(MockStatus())
  62. assert result == []
  63. def test_build_loaded_filaments_with_ams(self, scheduler):
  64. """Should extract filaments from AMS units."""
  65. class MockStatus:
  66. raw_data = {
  67. "ams": [
  68. {
  69. "id": 0,
  70. "tray": [
  71. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000"},
  72. {"id": 1, "tray_type": "PETG", "tray_color": "00FF00"},
  73. ],
  74. }
  75. ]
  76. }
  77. result = scheduler._build_loaded_filaments(MockStatus())
  78. assert len(result) == 2
  79. # First filament
  80. assert result[0]["type"] == "PLA"
  81. assert result[0]["color"] == "#FF0000"
  82. assert result[0]["ams_id"] == 0
  83. assert result[0]["tray_id"] == 0
  84. assert result[0]["global_tray_id"] == 0 # 0 * 4 + 0
  85. # Second filament
  86. assert result[1]["type"] == "PETG"
  87. assert result[1]["global_tray_id"] == 1 # 0 * 4 + 1
  88. def test_build_loaded_filaments_with_ht_ams(self, scheduler):
  89. """AMS-HT (single tray) should be marked as is_ht."""
  90. class MockStatus:
  91. raw_data = {
  92. "ams": [
  93. {
  94. "id": 128,
  95. "tray": [{"id": 0, "tray_type": "PLA-CF", "tray_color": "000000"}],
  96. }
  97. ]
  98. }
  99. result = scheduler._build_loaded_filaments(MockStatus())
  100. assert len(result) == 1
  101. assert result[0]["is_ht"] is True
  102. assert result[0]["global_tray_id"] == 512 # 128 * 4 + 0
  103. def test_build_loaded_filaments_with_external(self, scheduler):
  104. """Should include external spool."""
  105. class MockStatus:
  106. raw_data = {"vt_tray": {"tray_type": "TPU", "tray_color": "0000FF"}}
  107. result = scheduler._build_loaded_filaments(MockStatus())
  108. assert len(result) == 1
  109. assert result[0]["type"] == "TPU"
  110. assert result[0]["is_external"] is True
  111. assert result[0]["global_tray_id"] == 254
  112. def test_build_loaded_filaments_skips_empty_trays(self, scheduler):
  113. """Trays without tray_type should be skipped."""
  114. class MockStatus:
  115. raw_data = {
  116. "ams": [
  117. {
  118. "id": 0,
  119. "tray": [
  120. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000"},
  121. {"id": 1, "tray_type": "", "tray_color": ""}, # Empty
  122. {"id": 2}, # No tray_type key
  123. ],
  124. }
  125. ]
  126. }
  127. result = scheduler._build_loaded_filaments(MockStatus())
  128. assert len(result) == 1
  129. assert result[0]["type"] == "PLA"
  130. class TestMatchFilamentsToSlots:
  131. """Test the _match_filaments_to_slots method."""
  132. @pytest.fixture
  133. def scheduler(self):
  134. return PrintScheduler()
  135. def test_match_empty_required(self, scheduler):
  136. """Empty required list should return None."""
  137. result = scheduler._match_filaments_to_slots([], [])
  138. assert result is None
  139. def test_match_exact_color(self, scheduler):
  140. """Should prefer exact color match."""
  141. required = [{"slot_id": 1, "type": "PLA", "color": "#FF0000"}]
  142. loaded = [
  143. {"type": "PLA", "color": "#00FF00", "global_tray_id": 0}, # Wrong color
  144. {"type": "PLA", "color": "#FF0000", "global_tray_id": 1}, # Exact match
  145. ]
  146. result = scheduler._match_filaments_to_slots(required, loaded)
  147. assert result == [1] # Should pick tray 1 (exact color match)
  148. def test_match_similar_color(self, scheduler):
  149. """Should match similar colors when no exact match."""
  150. required = [{"slot_id": 1, "type": "PLA", "color": "#FF5500"}]
  151. loaded = [
  152. {"type": "PLA", "color": "#FF5510", "global_tray_id": 0}, # Similar
  153. ]
  154. result = scheduler._match_filaments_to_slots(required, loaded)
  155. assert result == [0]
  156. def test_match_type_only(self, scheduler):
  157. """Should match by type when colors don't match."""
  158. required = [{"slot_id": 1, "type": "PLA", "color": "#FF0000"}]
  159. loaded = [
  160. {"type": "PLA", "color": "#0000FF", "global_tray_id": 5}, # Type match, color way off
  161. ]
  162. result = scheduler._match_filaments_to_slots(required, loaded)
  163. assert result == [5]
  164. def test_match_no_match_returns_minus_one(self, scheduler):
  165. """Unmatched filaments should have -1 in mapping."""
  166. required = [{"slot_id": 1, "type": "PLA", "color": "#FF0000"}]
  167. loaded = [
  168. {"type": "PETG", "color": "#FF0000", "global_tray_id": 0}, # Wrong type
  169. ]
  170. result = scheduler._match_filaments_to_slots(required, loaded)
  171. assert result == [-1]
  172. def test_match_multiple_filaments(self, scheduler):
  173. """Should match multiple filaments correctly."""
  174. required = [
  175. {"slot_id": 1, "type": "PLA", "color": "#FF0000"},
  176. {"slot_id": 2, "type": "PETG", "color": "#00FF00"},
  177. ]
  178. loaded = [
  179. {"type": "PLA", "color": "#FF0000", "global_tray_id": 0},
  180. {"type": "PETG", "color": "#00FF00", "global_tray_id": 1},
  181. ]
  182. result = scheduler._match_filaments_to_slots(required, loaded)
  183. assert result == [0, 1]
  184. def test_match_avoids_duplicate_assignment(self, scheduler):
  185. """Same tray should not be assigned to multiple slots."""
  186. required = [
  187. {"slot_id": 1, "type": "PLA", "color": "#FF0000"},
  188. {"slot_id": 2, "type": "PLA", "color": "#FF0000"}, # Same requirements
  189. ]
  190. loaded = [
  191. {"type": "PLA", "color": "#FF0000", "global_tray_id": 0}, # Only one PLA
  192. ]
  193. result = scheduler._match_filaments_to_slots(required, loaded)
  194. # First slot gets the match, second slot gets -1
  195. assert result == [0, -1]
  196. def test_match_h2d_pro_ams_ids(self, scheduler):
  197. """Should work with H2D Pro's high AMS IDs (128+)."""
  198. required = [{"slot_id": 1, "type": "PLA", "color": "#FF0000"}]
  199. loaded = [
  200. {"type": "PLA", "color": "#FF0000", "global_tray_id": 512}, # AMS 128, slot 0
  201. ]
  202. result = scheduler._match_filaments_to_slots(required, loaded)
  203. assert result == [512]
  204. def test_match_external_spool(self, scheduler):
  205. """Should match external spool with ID 254."""
  206. required = [{"slot_id": 1, "type": "TPU", "color": "#0000FF"}]
  207. loaded = [
  208. {"type": "TPU", "color": "#0000FF", "global_tray_id": 254, "is_external": True},
  209. ]
  210. result = scheduler._match_filaments_to_slots(required, loaded)
  211. assert result == [254]