test_scheduler_ams_mapping.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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"] == 128 # AMS-HT uses ams_id directly
  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]
  212. def test_match_by_tray_info_idx_priority(self, scheduler):
  213. """tray_info_idx match should have highest priority over color match."""
  214. required = [{"slot_id": 1, "type": "PLA", "color": "#000000", "tray_info_idx": "GFA00"}]
  215. loaded = [
  216. {
  217. "type": "PLA",
  218. "color": "#000000",
  219. "global_tray_id": 0,
  220. "tray_info_idx": "GFB00",
  221. }, # Same color, different spool
  222. {
  223. "type": "PLA",
  224. "color": "#000000",
  225. "global_tray_id": 1,
  226. "tray_info_idx": "GFA00",
  227. }, # Same color, exact spool
  228. {
  229. "type": "PLA",
  230. "color": "#000000",
  231. "global_tray_id": 2,
  232. "tray_info_idx": "GFC00",
  233. }, # Same color, different spool
  234. ]
  235. result = scheduler._match_filaments_to_slots(required, loaded)
  236. assert result == [1] # Should pick tray 1 (exact tray_info_idx match)
  237. def test_match_by_tray_info_idx_with_different_colors(self, scheduler):
  238. """tray_info_idx match should work even if colors differ slightly."""
  239. required = [{"slot_id": 1, "type": "PLA", "color": "#000000", "tray_info_idx": "P4d64437"}]
  240. loaded = [
  241. {"type": "PLA", "color": "#000000", "global_tray_id": 0, "tray_info_idx": ""}, # No idx
  242. {
  243. "type": "PLA",
  244. "color": "#000010",
  245. "global_tray_id": 3,
  246. "tray_info_idx": "P4d64437",
  247. }, # Exact spool (slightly different color reported)
  248. ]
  249. result = scheduler._match_filaments_to_slots(required, loaded)
  250. assert result == [3] # Should pick tray 3 (exact tray_info_idx match)
  251. def test_match_fallback_to_color_when_no_tray_info_idx(self, scheduler):
  252. """Should fall back to color matching when tray_info_idx is empty."""
  253. required = [{"slot_id": 1, "type": "PLA", "color": "#FF0000", "tray_info_idx": ""}]
  254. loaded = [
  255. {"type": "PLA", "color": "#00FF00", "global_tray_id": 0, "tray_info_idx": "GFA00"},
  256. {"type": "PLA", "color": "#FF0000", "global_tray_id": 1, "tray_info_idx": "GFB00"}, # Color match
  257. ]
  258. result = scheduler._match_filaments_to_slots(required, loaded)
  259. assert result == [1] # Should pick tray 1 (color match)
  260. def test_match_fallback_to_color_when_no_matching_tray_info_idx(self, scheduler):
  261. """Should fall back to color when tray_info_idx doesn't match any loaded spool."""
  262. required = [{"slot_id": 1, "type": "PLA", "color": "#FF0000", "tray_info_idx": "OLD_SPOOL"}]
  263. loaded = [
  264. {
  265. "type": "PLA",
  266. "color": "#FF0000",
  267. "global_tray_id": 0,
  268. "tray_info_idx": "NEW_SPOOL",
  269. }, # Different idx but same color
  270. ]
  271. result = scheduler._match_filaments_to_slots(required, loaded)
  272. assert result == [0] # Should fall back to color match
  273. def test_match_multiple_same_color_with_tray_info_idx(self, scheduler):
  274. """Multiple identical filaments should be matched by tray_info_idx (H2D Pro scenario)."""
  275. # This is the exact scenario from issue #245 - 3 black PLA spools
  276. required = [
  277. {"slot_id": 1, "type": "PLA", "color": "#000000", "tray_info_idx": "GFA03"}, # Wants tray 3
  278. ]
  279. loaded = [
  280. {"type": "PLA", "color": "#000000", "global_tray_id": 0, "tray_info_idx": "GFA00"}, # Tray 0
  281. {"type": "PLA", "color": "#000000", "global_tray_id": 1, "tray_info_idx": "GFA01"}, # Tray 1
  282. {"type": "PLA", "color": "#000000", "global_tray_id": 2, "tray_info_idx": "GFA02"}, # Tray 2
  283. {
  284. "type": "PLA",
  285. "color": "#000000",
  286. "global_tray_id": 3,
  287. "tray_info_idx": "GFA03",
  288. }, # Tray 3 - the one we want
  289. ]
  290. result = scheduler._match_filaments_to_slots(required, loaded)
  291. assert result == [3] # Should pick tray 3, not tray 0
  292. def test_match_tray_info_idx_not_reused(self, scheduler):
  293. """tray_info_idx matched trays should not be reused for other slots."""
  294. required = [
  295. {"slot_id": 1, "type": "PLA", "color": "#000000", "tray_info_idx": "GFA00"},
  296. {"slot_id": 2, "type": "PLA", "color": "#000000", "tray_info_idx": "GFA01"},
  297. ]
  298. loaded = [
  299. {"type": "PLA", "color": "#000000", "global_tray_id": 0, "tray_info_idx": "GFA00"},
  300. {"type": "PLA", "color": "#000000", "global_tray_id": 1, "tray_info_idx": "GFA01"},
  301. ]
  302. result = scheduler._match_filaments_to_slots(required, loaded)
  303. assert result == [0, 1] # Each slot gets its specific tray
  304. def test_match_non_unique_tray_info_idx_uses_color(self, scheduler):
  305. """Non-unique tray_info_idx should fall back to color matching.
  306. This is the scenario where multiple trays have the same tray_info_idx
  307. (e.g., two spools of generic PLA both have GFA00). The color should
  308. be used as tiebreaker instead of just picking the first match.
  309. """
  310. # User sliced with green PLA (tray_info_idx=GFA00)
  311. # Two trays have GFA00: tray 3 (white) and tray 4 (green)
  312. # Should pick tray 4 because the color matches
  313. required = [
  314. {"slot_id": 2, "type": "PLA", "color": "#00FF00", "tray_info_idx": "GFA00"}, # Green PLA
  315. ]
  316. loaded = [
  317. {"type": "PLA", "color": "#FFFFFF", "global_tray_id": 3, "tray_info_idx": "GFA00"}, # White PLA
  318. {"type": "PLA", "color": "#00FF00", "global_tray_id": 4, "tray_info_idx": "GFA00"}, # Green PLA
  319. ]
  320. result = scheduler._match_filaments_to_slots(required, loaded)
  321. assert result == [-1, 4] # Should pick tray 4 (color match), not tray 3 (first match)
  322. def test_match_non_unique_tray_info_idx_same_color(self, scheduler):
  323. """Non-unique tray_info_idx with identical colors picks first match.
  324. When multiple trays have the same tray_info_idx AND same color,
  325. there's no way to differentiate, so first match is used.
  326. """
  327. required = [
  328. {"slot_id": 2, "type": "PLA", "color": "#FFFFFF", "tray_info_idx": "GFA00"},
  329. ]
  330. loaded = [
  331. {"type": "PLA", "color": "#FFFFFF", "global_tray_id": 3, "tray_info_idx": "GFA00"},
  332. {"type": "PLA", "color": "#FFFFFF", "global_tray_id": 4, "tray_info_idx": "GFA00"},
  333. ]
  334. result = scheduler._match_filaments_to_slots(required, loaded)
  335. # Both have same color, so first is used
  336. assert result == [-1, 3]
  337. class TestBuildLoadedFilamentsTrayInfoIdx:
  338. """Test tray_info_idx extraction in _build_loaded_filaments."""
  339. @pytest.fixture
  340. def scheduler(self):
  341. return PrintScheduler()
  342. def test_build_loaded_filaments_includes_tray_info_idx(self, scheduler):
  343. """Should extract tray_info_idx from AMS trays."""
  344. class MockStatus:
  345. raw_data = {
  346. "ams": [
  347. {
  348. "id": 0,
  349. "tray": [
  350. {"id": 0, "tray_type": "PLA", "tray_color": "000000", "tray_info_idx": "GFA00"},
  351. {"id": 1, "tray_type": "PLA", "tray_color": "000000", "tray_info_idx": "GFA01"},
  352. ],
  353. }
  354. ]
  355. }
  356. result = scheduler._build_loaded_filaments(MockStatus())
  357. assert len(result) == 2
  358. assert result[0]["tray_info_idx"] == "GFA00"
  359. assert result[1]["tray_info_idx"] == "GFA01"
  360. def test_build_loaded_filaments_empty_tray_info_idx(self, scheduler):
  361. """Missing tray_info_idx should default to empty string."""
  362. class MockStatus:
  363. raw_data = {
  364. "ams": [
  365. {
  366. "id": 0,
  367. "tray": [
  368. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000"}, # No tray_info_idx
  369. ],
  370. }
  371. ]
  372. }
  373. result = scheduler._build_loaded_filaments(MockStatus())
  374. assert len(result) == 1
  375. assert result[0]["tray_info_idx"] == ""
  376. def test_build_loaded_filaments_external_spool_tray_info_idx(self, scheduler):
  377. """Should extract tray_info_idx from external spool."""
  378. class MockStatus:
  379. raw_data = {"vt_tray": {"tray_type": "TPU", "tray_color": "0000FF", "tray_info_idx": "P4d64437"}}
  380. result = scheduler._build_loaded_filaments(MockStatus())
  381. assert len(result) == 1
  382. assert result[0]["tray_info_idx"] == "P4d64437"
  383. assert result[0]["is_external"] is True