test_scheduler_ams_mapping.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. """Tests for the AMS mapping computation in the print scheduler."""
  2. import io
  3. import json
  4. import zipfile
  5. import pytest
  6. from backend.app.services.print_scheduler import PrintScheduler
  7. from backend.app.utils.threemf_tools import extract_nozzle_mapping_from_3mf
  8. class TestSchedulerAmsMappingHelpers:
  9. """Test the AMS mapping helper methods in PrintScheduler."""
  10. @pytest.fixture
  11. def scheduler(self):
  12. return PrintScheduler()
  13. def test_normalize_color_with_hash(self, scheduler):
  14. """Color with hash should return #RRGGBB format."""
  15. result = scheduler._normalize_color("#FF5500")
  16. assert result == "#FF5500"
  17. def test_normalize_color_without_hash(self, scheduler):
  18. """Color without hash should add hash prefix."""
  19. result = scheduler._normalize_color("FF5500")
  20. assert result == "#FF5500"
  21. def test_normalize_color_with_alpha(self, scheduler):
  22. """Color with alpha channel should strip it."""
  23. result = scheduler._normalize_color("FF5500AA")
  24. assert result == "#FF5500"
  25. def test_normalize_color_none(self, scheduler):
  26. """None color should return default gray."""
  27. result = scheduler._normalize_color(None)
  28. assert result == "#808080"
  29. def test_normalize_color_empty(self, scheduler):
  30. """Empty color should return default gray."""
  31. result = scheduler._normalize_color("")
  32. assert result == "#808080"
  33. def test_normalize_color_for_compare(self, scheduler):
  34. """Color for compare should be lowercase without hash."""
  35. result = scheduler._normalize_color_for_compare("#FF5500")
  36. assert result == "ff5500"
  37. def test_normalize_color_for_compare_with_alpha(self, scheduler):
  38. """Alpha channel should be stripped for comparison."""
  39. result = scheduler._normalize_color_for_compare("#FF5500AA")
  40. assert result == "ff5500"
  41. def test_colors_are_similar_exact_match(self, scheduler):
  42. """Exact same colors should be similar."""
  43. assert scheduler._colors_are_similar("#FF5500", "#FF5500") is True
  44. def test_colors_are_similar_within_threshold(self, scheduler):
  45. """Colors within threshold should be similar."""
  46. # Red difference of 10, well within default threshold of 40
  47. assert scheduler._colors_are_similar("#FF5500", "#F55500") is True
  48. def test_colors_are_similar_outside_threshold(self, scheduler):
  49. """Colors outside threshold should not be similar."""
  50. # Red: FF (255) vs 00 (0) = 255 difference
  51. assert scheduler._colors_are_similar("#FF0000", "#00FF00") is False
  52. def test_colors_are_similar_none_colors(self, scheduler):
  53. """None colors should not be similar."""
  54. assert scheduler._colors_are_similar(None, "#FF5500") is False
  55. assert scheduler._colors_are_similar("#FF5500", None) is False
  56. class TestBuildLoadedFilaments:
  57. """Test the _build_loaded_filaments method."""
  58. @pytest.fixture
  59. def scheduler(self):
  60. return PrintScheduler()
  61. def test_build_loaded_filaments_empty_status(self, scheduler):
  62. """Empty status should return empty list."""
  63. class MockStatus:
  64. raw_data = {}
  65. result = scheduler._build_loaded_filaments(MockStatus())
  66. assert result == []
  67. def test_build_loaded_filaments_with_ams(self, scheduler):
  68. """Should extract filaments from AMS units."""
  69. class MockStatus:
  70. raw_data = {
  71. "ams": [
  72. {
  73. "id": 0,
  74. "tray": [
  75. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000"},
  76. {"id": 1, "tray_type": "PETG", "tray_color": "00FF00"},
  77. ],
  78. }
  79. ]
  80. }
  81. result = scheduler._build_loaded_filaments(MockStatus())
  82. assert len(result) == 2
  83. # First filament
  84. assert result[0]["type"] == "PLA"
  85. assert result[0]["color"] == "#FF0000"
  86. assert result[0]["ams_id"] == 0
  87. assert result[0]["tray_id"] == 0
  88. assert result[0]["global_tray_id"] == 0 # 0 * 4 + 0
  89. # Second filament
  90. assert result[1]["type"] == "PETG"
  91. assert result[1]["global_tray_id"] == 1 # 0 * 4 + 1
  92. def test_build_loaded_filaments_with_ht_ams(self, scheduler):
  93. """AMS-HT (single tray) should be marked as is_ht."""
  94. class MockStatus:
  95. raw_data = {
  96. "ams": [
  97. {
  98. "id": 128,
  99. "tray": [{"id": 0, "tray_type": "PLA-CF", "tray_color": "000000"}],
  100. }
  101. ]
  102. }
  103. result = scheduler._build_loaded_filaments(MockStatus())
  104. assert len(result) == 1
  105. assert result[0]["is_ht"] is True
  106. assert result[0]["global_tray_id"] == 128 # AMS-HT uses ams_id directly
  107. def test_build_loaded_filaments_with_external(self, scheduler):
  108. """Should include external spool."""
  109. class MockStatus:
  110. raw_data = {"vt_tray": [{"tray_type": "TPU", "tray_color": "0000FF"}]}
  111. result = scheduler._build_loaded_filaments(MockStatus())
  112. assert len(result) == 1
  113. assert result[0]["type"] == "TPU"
  114. assert result[0]["is_external"] is True
  115. assert result[0]["global_tray_id"] == 254
  116. def test_build_loaded_filaments_skips_empty_trays(self, scheduler):
  117. """Trays without tray_type should be skipped."""
  118. class MockStatus:
  119. raw_data = {
  120. "ams": [
  121. {
  122. "id": 0,
  123. "tray": [
  124. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000"},
  125. {"id": 1, "tray_type": "", "tray_color": ""}, # Empty
  126. {"id": 2}, # No tray_type key
  127. ],
  128. }
  129. ]
  130. }
  131. result = scheduler._build_loaded_filaments(MockStatus())
  132. assert len(result) == 1
  133. assert result[0]["type"] == "PLA"
  134. class TestMatchFilamentsToSlots:
  135. """Test the _match_filaments_to_slots method."""
  136. @pytest.fixture
  137. def scheduler(self):
  138. return PrintScheduler()
  139. def test_match_empty_required(self, scheduler):
  140. """Empty required list should return None."""
  141. result = scheduler._match_filaments_to_slots([], [])
  142. assert result is None
  143. def test_match_exact_color(self, scheduler):
  144. """Should prefer exact color match."""
  145. required = [{"slot_id": 1, "type": "PLA", "color": "#FF0000"}]
  146. loaded = [
  147. {"type": "PLA", "color": "#00FF00", "global_tray_id": 0}, # Wrong color
  148. {"type": "PLA", "color": "#FF0000", "global_tray_id": 1}, # Exact match
  149. ]
  150. result = scheduler._match_filaments_to_slots(required, loaded)
  151. assert result == [1] # Should pick tray 1 (exact color match)
  152. def test_match_similar_color(self, scheduler):
  153. """Should match similar colors when no exact match."""
  154. required = [{"slot_id": 1, "type": "PLA", "color": "#FF5500"}]
  155. loaded = [
  156. {"type": "PLA", "color": "#FF5510", "global_tray_id": 0}, # Similar
  157. ]
  158. result = scheduler._match_filaments_to_slots(required, loaded)
  159. assert result == [0]
  160. def test_match_type_only(self, scheduler):
  161. """Should match by type when colors don't match."""
  162. required = [{"slot_id": 1, "type": "PLA", "color": "#FF0000"}]
  163. loaded = [
  164. {"type": "PLA", "color": "#0000FF", "global_tray_id": 5}, # Type match, color way off
  165. ]
  166. result = scheduler._match_filaments_to_slots(required, loaded)
  167. assert result == [5]
  168. def test_match_no_match_returns_minus_one(self, scheduler):
  169. """Unmatched filaments should have -1 in mapping."""
  170. required = [{"slot_id": 1, "type": "PLA", "color": "#FF0000"}]
  171. loaded = [
  172. {"type": "PETG", "color": "#FF0000", "global_tray_id": 0}, # Wrong type
  173. ]
  174. result = scheduler._match_filaments_to_slots(required, loaded)
  175. assert result == [-1]
  176. def test_match_multiple_filaments(self, scheduler):
  177. """Should match multiple filaments correctly."""
  178. required = [
  179. {"slot_id": 1, "type": "PLA", "color": "#FF0000"},
  180. {"slot_id": 2, "type": "PETG", "color": "#00FF00"},
  181. ]
  182. loaded = [
  183. {"type": "PLA", "color": "#FF0000", "global_tray_id": 0},
  184. {"type": "PETG", "color": "#00FF00", "global_tray_id": 1},
  185. ]
  186. result = scheduler._match_filaments_to_slots(required, loaded)
  187. assert result == [0, 1]
  188. def test_match_avoids_duplicate_assignment(self, scheduler):
  189. """Same tray should not be assigned to multiple slots."""
  190. required = [
  191. {"slot_id": 1, "type": "PLA", "color": "#FF0000"},
  192. {"slot_id": 2, "type": "PLA", "color": "#FF0000"}, # Same requirements
  193. ]
  194. loaded = [
  195. {"type": "PLA", "color": "#FF0000", "global_tray_id": 0}, # Only one PLA
  196. ]
  197. result = scheduler._match_filaments_to_slots(required, loaded)
  198. # First slot gets the match, second slot gets -1
  199. assert result == [0, -1]
  200. def test_match_h2d_pro_ams_ids(self, scheduler):
  201. """Should work with H2D Pro's high AMS IDs (128+)."""
  202. required = [{"slot_id": 1, "type": "PLA", "color": "#FF0000"}]
  203. loaded = [
  204. {"type": "PLA", "color": "#FF0000", "global_tray_id": 512}, # AMS 128, slot 0
  205. ]
  206. result = scheduler._match_filaments_to_slots(required, loaded)
  207. assert result == [512]
  208. def test_match_external_spool(self, scheduler):
  209. """Should match external spool with ID 254."""
  210. required = [{"slot_id": 1, "type": "TPU", "color": "#0000FF"}]
  211. loaded = [
  212. {"type": "TPU", "color": "#0000FF", "global_tray_id": 254, "is_external": True},
  213. ]
  214. result = scheduler._match_filaments_to_slots(required, loaded)
  215. assert result == [254]
  216. def test_match_by_tray_info_idx_priority(self, scheduler):
  217. """tray_info_idx match should have highest priority over color match."""
  218. required = [{"slot_id": 1, "type": "PLA", "color": "#000000", "tray_info_idx": "GFA00"}]
  219. loaded = [
  220. {
  221. "type": "PLA",
  222. "color": "#000000",
  223. "global_tray_id": 0,
  224. "tray_info_idx": "GFB00",
  225. }, # Same color, different spool
  226. {
  227. "type": "PLA",
  228. "color": "#000000",
  229. "global_tray_id": 1,
  230. "tray_info_idx": "GFA00",
  231. }, # Same color, exact spool
  232. {
  233. "type": "PLA",
  234. "color": "#000000",
  235. "global_tray_id": 2,
  236. "tray_info_idx": "GFC00",
  237. }, # Same color, different spool
  238. ]
  239. result = scheduler._match_filaments_to_slots(required, loaded)
  240. assert result == [1] # Should pick tray 1 (exact tray_info_idx match)
  241. def test_match_by_tray_info_idx_with_different_colors(self, scheduler):
  242. """tray_info_idx match should work even if colors differ slightly."""
  243. required = [{"slot_id": 1, "type": "PLA", "color": "#000000", "tray_info_idx": "P4d64437"}]
  244. loaded = [
  245. {"type": "PLA", "color": "#000000", "global_tray_id": 0, "tray_info_idx": ""}, # No idx
  246. {
  247. "type": "PLA",
  248. "color": "#000010",
  249. "global_tray_id": 3,
  250. "tray_info_idx": "P4d64437",
  251. }, # Exact spool (slightly different color reported)
  252. ]
  253. result = scheduler._match_filaments_to_slots(required, loaded)
  254. assert result == [3] # Should pick tray 3 (exact tray_info_idx match)
  255. def test_match_fallback_to_color_when_no_tray_info_idx(self, scheduler):
  256. """Should fall back to color matching when tray_info_idx is empty."""
  257. required = [{"slot_id": 1, "type": "PLA", "color": "#FF0000", "tray_info_idx": ""}]
  258. loaded = [
  259. {"type": "PLA", "color": "#00FF00", "global_tray_id": 0, "tray_info_idx": "GFA00"},
  260. {"type": "PLA", "color": "#FF0000", "global_tray_id": 1, "tray_info_idx": "GFB00"}, # Color match
  261. ]
  262. result = scheduler._match_filaments_to_slots(required, loaded)
  263. assert result == [1] # Should pick tray 1 (color match)
  264. def test_match_fallback_to_color_when_no_matching_tray_info_idx(self, scheduler):
  265. """Should fall back to color when tray_info_idx doesn't match any loaded spool."""
  266. required = [{"slot_id": 1, "type": "PLA", "color": "#FF0000", "tray_info_idx": "OLD_SPOOL"}]
  267. loaded = [
  268. {
  269. "type": "PLA",
  270. "color": "#FF0000",
  271. "global_tray_id": 0,
  272. "tray_info_idx": "NEW_SPOOL",
  273. }, # Different idx but same color
  274. ]
  275. result = scheduler._match_filaments_to_slots(required, loaded)
  276. assert result == [0] # Should fall back to color match
  277. def test_match_multiple_same_color_with_tray_info_idx(self, scheduler):
  278. """Multiple identical filaments should be matched by tray_info_idx (H2D Pro scenario)."""
  279. # This is the exact scenario from issue #245 - 3 black PLA spools
  280. required = [
  281. {"slot_id": 1, "type": "PLA", "color": "#000000", "tray_info_idx": "GFA03"}, # Wants tray 3
  282. ]
  283. loaded = [
  284. {"type": "PLA", "color": "#000000", "global_tray_id": 0, "tray_info_idx": "GFA00"}, # Tray 0
  285. {"type": "PLA", "color": "#000000", "global_tray_id": 1, "tray_info_idx": "GFA01"}, # Tray 1
  286. {"type": "PLA", "color": "#000000", "global_tray_id": 2, "tray_info_idx": "GFA02"}, # Tray 2
  287. {
  288. "type": "PLA",
  289. "color": "#000000",
  290. "global_tray_id": 3,
  291. "tray_info_idx": "GFA03",
  292. }, # Tray 3 - the one we want
  293. ]
  294. result = scheduler._match_filaments_to_slots(required, loaded)
  295. assert result == [3] # Should pick tray 3, not tray 0
  296. def test_match_tray_info_idx_not_reused(self, scheduler):
  297. """tray_info_idx matched trays should not be reused for other slots."""
  298. required = [
  299. {"slot_id": 1, "type": "PLA", "color": "#000000", "tray_info_idx": "GFA00"},
  300. {"slot_id": 2, "type": "PLA", "color": "#000000", "tray_info_idx": "GFA01"},
  301. ]
  302. loaded = [
  303. {"type": "PLA", "color": "#000000", "global_tray_id": 0, "tray_info_idx": "GFA00"},
  304. {"type": "PLA", "color": "#000000", "global_tray_id": 1, "tray_info_idx": "GFA01"},
  305. ]
  306. result = scheduler._match_filaments_to_slots(required, loaded)
  307. assert result == [0, 1] # Each slot gets its specific tray
  308. def test_match_non_unique_tray_info_idx_uses_color(self, scheduler):
  309. """Non-unique tray_info_idx should fall back to color matching.
  310. This is the scenario where multiple trays have the same tray_info_idx
  311. (e.g., two spools of generic PLA both have GFA00). The color should
  312. be used as tiebreaker instead of just picking the first match.
  313. """
  314. # User sliced with green PLA (tray_info_idx=GFA00)
  315. # Two trays have GFA00: tray 3 (white) and tray 4 (green)
  316. # Should pick tray 4 because the color matches
  317. required = [
  318. {"slot_id": 2, "type": "PLA", "color": "#00FF00", "tray_info_idx": "GFA00"}, # Green PLA
  319. ]
  320. loaded = [
  321. {"type": "PLA", "color": "#FFFFFF", "global_tray_id": 3, "tray_info_idx": "GFA00"}, # White PLA
  322. {"type": "PLA", "color": "#00FF00", "global_tray_id": 4, "tray_info_idx": "GFA00"}, # Green PLA
  323. ]
  324. result = scheduler._match_filaments_to_slots(required, loaded)
  325. assert result == [-1, 4] # Should pick tray 4 (color match), not tray 3 (first match)
  326. def test_match_non_unique_tray_info_idx_same_color(self, scheduler):
  327. """Non-unique tray_info_idx with identical colors picks first match.
  328. When multiple trays have the same tray_info_idx AND same color,
  329. there's no way to differentiate, so first match is used.
  330. """
  331. required = [
  332. {"slot_id": 2, "type": "PLA", "color": "#FFFFFF", "tray_info_idx": "GFA00"},
  333. ]
  334. loaded = [
  335. {"type": "PLA", "color": "#FFFFFF", "global_tray_id": 3, "tray_info_idx": "GFA00"},
  336. {"type": "PLA", "color": "#FFFFFF", "global_tray_id": 4, "tray_info_idx": "GFA00"},
  337. ]
  338. result = scheduler._match_filaments_to_slots(required, loaded)
  339. # Both have same color, so first is used
  340. assert result == [-1, 3]
  341. class TestBuildLoadedFilamentsTrayInfoIdx:
  342. """Test tray_info_idx extraction in _build_loaded_filaments."""
  343. @pytest.fixture
  344. def scheduler(self):
  345. return PrintScheduler()
  346. def test_build_loaded_filaments_includes_tray_info_idx(self, scheduler):
  347. """Should extract tray_info_idx from AMS trays."""
  348. class MockStatus:
  349. raw_data = {
  350. "ams": [
  351. {
  352. "id": 0,
  353. "tray": [
  354. {"id": 0, "tray_type": "PLA", "tray_color": "000000", "tray_info_idx": "GFA00"},
  355. {"id": 1, "tray_type": "PLA", "tray_color": "000000", "tray_info_idx": "GFA01"},
  356. ],
  357. }
  358. ]
  359. }
  360. result = scheduler._build_loaded_filaments(MockStatus())
  361. assert len(result) == 2
  362. assert result[0]["tray_info_idx"] == "GFA00"
  363. assert result[1]["tray_info_idx"] == "GFA01"
  364. def test_build_loaded_filaments_empty_tray_info_idx(self, scheduler):
  365. """Missing tray_info_idx should default to empty string."""
  366. class MockStatus:
  367. raw_data = {
  368. "ams": [
  369. {
  370. "id": 0,
  371. "tray": [
  372. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000"}, # No tray_info_idx
  373. ],
  374. }
  375. ]
  376. }
  377. result = scheduler._build_loaded_filaments(MockStatus())
  378. assert len(result) == 1
  379. assert result[0]["tray_info_idx"] == ""
  380. def test_build_loaded_filaments_external_spool_tray_info_idx(self, scheduler):
  381. """Should extract tray_info_idx from external spool."""
  382. class MockStatus:
  383. raw_data = {"vt_tray": [{"tray_type": "TPU", "tray_color": "0000FF", "tray_info_idx": "P4d64437"}]}
  384. result = scheduler._build_loaded_filaments(MockStatus())
  385. assert len(result) == 1
  386. assert result[0]["tray_info_idx"] == "P4d64437"
  387. assert result[0]["is_external"] is True
  388. def _make_3mf_zip(project_settings: dict | None = None) -> zipfile.ZipFile:
  389. """Create an in-memory ZipFile mimicking a 3MF with project_settings.config."""
  390. buf = io.BytesIO()
  391. with zipfile.ZipFile(buf, "w") as zf:
  392. if project_settings is not None:
  393. zf.writestr("Metadata/project_settings.config", json.dumps(project_settings))
  394. buf.seek(0)
  395. return zipfile.ZipFile(buf, "r")
  396. class TestExtractNozzleMappingFrom3mf:
  397. """Test the extract_nozzle_mapping_from_3mf utility."""
  398. def test_dual_nozzle_mapping(self):
  399. """Should return slot->extruder mapping for dual-nozzle files."""
  400. zf = _make_3mf_zip(
  401. {
  402. "filament_nozzle_map": ["0", "1", "0"],
  403. "physical_extruder_map": ["0", "1"],
  404. }
  405. )
  406. result = extract_nozzle_mapping_from_3mf(zf)
  407. assert result == {1: 0, 2: 1, 3: 0}
  408. zf.close()
  409. def test_single_nozzle_returns_none(self):
  410. """All slots on same extruder should return None (single-nozzle)."""
  411. zf = _make_3mf_zip(
  412. {
  413. "filament_nozzle_map": ["0", "0", "0"],
  414. "physical_extruder_map": ["0"],
  415. }
  416. )
  417. result = extract_nozzle_mapping_from_3mf(zf)
  418. assert result is None
  419. zf.close()
  420. def test_missing_project_settings_returns_none(self):
  421. """Missing project_settings.config should return None."""
  422. zf = _make_3mf_zip(None)
  423. result = extract_nozzle_mapping_from_3mf(zf)
  424. assert result is None
  425. zf.close()
  426. def test_missing_fields_returns_none(self):
  427. """Missing filament_nozzle_map or physical_extruder_map should return None."""
  428. zf = _make_3mf_zip({"some_other_key": "value"})
  429. result = extract_nozzle_mapping_from_3mf(zf)
  430. assert result is None
  431. zf.close()
  432. def test_physical_extruder_map_remapping(self):
  433. """Should apply physical_extruder_map to remap slicer extruder to MQTT extruder."""
  434. # Slicer ext 0 -> MQTT ext 1, slicer ext 1 -> MQTT ext 0
  435. zf = _make_3mf_zip(
  436. {
  437. "filament_nozzle_map": ["0", "1"],
  438. "physical_extruder_map": ["1", "0"],
  439. }
  440. )
  441. result = extract_nozzle_mapping_from_3mf(zf)
  442. assert result == {1: 1, 2: 0}
  443. zf.close()
  444. class TestNozzleAwareMapping:
  445. """Test nozzle-aware filament matching in the print scheduler."""
  446. @pytest.fixture
  447. def scheduler(self):
  448. return PrintScheduler()
  449. def test_dual_nozzle_matching(self, scheduler):
  450. """Filaments assigned to different nozzles should match to correct AMS units."""
  451. required = [
  452. {"slot_id": 1, "type": "PLA", "color": "#FF0000", "nozzle_id": 0}, # Right nozzle
  453. {"slot_id": 2, "type": "PLA", "color": "#00FF00", "nozzle_id": 1}, # Left nozzle
  454. ]
  455. loaded = [
  456. {"type": "PLA", "color": "#00FF00", "global_tray_id": 0, "extruder_id": 0}, # AMS0 on right
  457. {"type": "PLA", "color": "#FF0000", "global_tray_id": 4, "extruder_id": 1}, # AMS1 on left
  458. ]
  459. # Without nozzle filtering, slot 1 (red, right) would match tray 4 (red, left) by color.
  460. # With nozzle filtering, slot 1 (right nozzle) can only use tray 0 (right extruder),
  461. # and slot 2 (left nozzle) can only use tray 4 (left extruder).
  462. result = scheduler._match_filaments_to_slots(required, loaded)
  463. assert result == [0, 4]
  464. def test_nozzle_fallback_when_no_match(self, scheduler):
  465. """Should fall back to unfiltered list when nozzle-filtered list is empty."""
  466. required = [
  467. {"slot_id": 1, "type": "PLA", "color": "#FF0000", "nozzle_id": 0}, # Right nozzle
  468. ]
  469. loaded = [
  470. # Only a tray on the left nozzle, none on right
  471. {"type": "PLA", "color": "#FF0000", "global_tray_id": 4, "extruder_id": 1},
  472. ]
  473. # No trays on extruder 0, so fallback to unfiltered -> should still match
  474. result = scheduler._match_filaments_to_slots(required, loaded)
  475. assert result == [4]
  476. def test_no_nozzle_id_skips_filtering(self, scheduler):
  477. """When nozzle_id is None, no nozzle filtering should be applied."""
  478. required = [
  479. {"slot_id": 1, "type": "PLA", "color": "#FF0000"}, # No nozzle_id
  480. ]
  481. loaded = [
  482. {"type": "PLA", "color": "#FF0000", "global_tray_id": 0, "extruder_id": 0},
  483. {"type": "PLA", "color": "#FF0000", "global_tray_id": 4, "extruder_id": 1},
  484. ]
  485. # Should match first available (tray 0) regardless of extruder
  486. result = scheduler._match_filaments_to_slots(required, loaded)
  487. assert result == [0]
  488. def test_extruder_id_in_loaded_filaments(self, scheduler):
  489. """_build_loaded_filaments should include extruder_id from ams_extruder_map."""
  490. class MockStatus:
  491. raw_data = {
  492. "ams": [
  493. {"id": 0, "tray": [{"id": 0, "tray_type": "PLA", "tray_color": "FF0000"}]},
  494. {"id": 1, "tray": [{"id": 0, "tray_type": "PLA", "tray_color": "00FF00"}]},
  495. ],
  496. "ams_extruder_map": {"0": 0, "1": 1},
  497. }
  498. result = scheduler._build_loaded_filaments(MockStatus())
  499. assert len(result) == 2
  500. assert result[0]["extruder_id"] == 0
  501. assert result[1]["extruder_id"] == 1
  502. def test_extruder_id_none_without_map(self, scheduler):
  503. """extruder_id should be None when ams_extruder_map is absent."""
  504. class MockStatus:
  505. raw_data = {
  506. "ams": [
  507. {"id": 0, "tray": [{"id": 0, "tray_type": "PLA", "tray_color": "FF0000"}]},
  508. ]
  509. }
  510. result = scheduler._build_loaded_filaments(MockStatus())
  511. assert len(result) == 1
  512. assert result[0]["extruder_id"] is None
  513. def test_external_spool_extruder_id(self, scheduler):
  514. """External spool should have extruder_id=0 when ams_extruder_map exists."""
  515. class MockStatus:
  516. raw_data = {
  517. "vt_tray": [{"tray_type": "TPU", "tray_color": "0000FF"}],
  518. "ams_extruder_map": {"0": 0},
  519. }
  520. result = scheduler._build_loaded_filaments(MockStatus())
  521. assert len(result) == 1
  522. assert result[0]["extruder_id"] == 0
  523. assert result[0]["is_external"] is True
  524. def test_external_spool_no_extruder_map(self, scheduler):
  525. """External spool extruder_id should be None without ams_extruder_map."""
  526. class MockStatus:
  527. raw_data = {"vt_tray": [{"tray_type": "TPU", "tray_color": "0000FF"}]}
  528. result = scheduler._build_loaded_filaments(MockStatus())
  529. assert len(result) == 1
  530. assert result[0]["extruder_id"] is None
  531. def test_dual_nozzle_with_tray_info_idx(self, scheduler):
  532. """Nozzle filtering should work together with tray_info_idx matching."""
  533. required = [
  534. {"slot_id": 1, "type": "PLA", "color": "#000000", "tray_info_idx": "GFA00", "nozzle_id": 0},
  535. {"slot_id": 2, "type": "PLA", "color": "#000000", "tray_info_idx": "GFA01", "nozzle_id": 1},
  536. ]
  537. loaded = [
  538. {"type": "PLA", "color": "#000000", "global_tray_id": 0, "tray_info_idx": "GFA00", "extruder_id": 0},
  539. {"type": "PLA", "color": "#000000", "global_tray_id": 4, "tray_info_idx": "GFA01", "extruder_id": 1},
  540. ]
  541. result = scheduler._match_filaments_to_slots(required, loaded)
  542. assert result == [0, 4]