test_nearest_colour_match_2804.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """Regression tests for slot-order-dependent colour matching (#2804).
  2. When no spool matches a required colour exactly, the matcher falls back to
  3. spools inside a per-channel tolerance. It took the first of those in tray order,
  4. so the winner depended on which slot a spool sat in rather than on which colour
  5. was closest.
  6. The worked example throughout is the maintainer's: a required ``#3A7BD5`` with a
  7. purple ``#6253AD`` in tray 1 (40/40/40 off — admitted by the box, distance ~69)
  8. and a near-identical ``#3B7AD2`` in tray 3 (distance ~3). Both qualify; the
  9. purple used to win on position alone.
  10. """
  11. import pytest
  12. from backend.app.services.print_scheduler import PrintScheduler
  13. REQUIRED_COLOR = "#3A7BD5"
  14. NEAR = "3B7AD2FF" # distance ~3
  15. FAR_BUT_ADMITTED = "6253ADFF" # 40/40/40 off — inside the box, distance ~69
  16. @pytest.fixture
  17. def scheduler():
  18. return PrintScheduler.__new__(PrintScheduler)
  19. def tray(global_tray_id, color, type_="PETG"):
  20. return {
  21. "global_tray_id": global_tray_id,
  22. "type": type_,
  23. "color": color,
  24. "tray_info_idx": "",
  25. "extruder_id": 0,
  26. "remain": 100,
  27. }
  28. def req(color=REQUIRED_COLOR, slot_id=1, type_="PETG", tray_info_idx=""):
  29. return {"slot_id": slot_id, "type": type_, "color": color, "tray_info_idx": tray_info_idx}
  30. class TestColorDistance:
  31. def test_identical_colours_are_zero_apart(self, scheduler):
  32. assert scheduler._color_distance("#3A7BD5", "3A7BD5FF") == 0
  33. def test_alpha_is_ignored_so_a_transparent_filament_matches_itself(self, scheduler):
  34. """The alpha a slicer writes is not a colour the user chose."""
  35. assert scheduler._color_distance("#76D9F4", "76D9F400") == 0
  36. def test_distance_is_euclidean_not_per_channel(self, scheduler):
  37. # 40 off on each channel is sqrt(3 * 40^2) ~= 69.28, not 40.
  38. assert scheduler._color_distance("#000000", "#282828") == pytest.approx(69.28, abs=0.01)
  39. def test_unusable_input_is_none_rather_than_a_number(self, scheduler):
  40. assert scheduler._color_distance(None, "#3A7BD5") is None
  41. assert scheduler._color_distance("", "#3A7BD5") is None
  42. assert scheduler._color_distance("#abc", "#3A7BD5") is None
  43. assert scheduler._color_distance("#zzzzzz", "#3A7BD5") is None
  44. class TestNearestSimilarWins:
  45. def test_closest_admitted_colour_wins_regardless_of_slot_order(self, scheduler):
  46. loaded = [tray(1, FAR_BUT_ADMITTED), tray(3, NEAR)]
  47. assert scheduler._match_filaments_to_slots([req()], loaded) == [3]
  48. def test_result_does_not_depend_on_tray_order(self, scheduler):
  49. """The bug in one line: reversing the AMS used to reverse the answer."""
  50. forward = scheduler._match_filaments_to_slots([req()], [tray(1, FAR_BUT_ADMITTED), tray(3, NEAR)])
  51. reversed_ = scheduler._match_filaments_to_slots([req()], [tray(3, NEAR), tray(1, FAR_BUT_ADMITTED)])
  52. assert forward == reversed_ == [3]
  53. def test_an_exact_match_still_outranks_a_near_one(self, scheduler):
  54. loaded = [tray(1, NEAR), tray(2, "3A7BD5FF")]
  55. assert scheduler._match_filaments_to_slots([req()], loaded) == [2]
  56. def test_eligibility_is_unchanged_so_a_far_colour_is_still_type_only(self, scheduler):
  57. """Ranking must not admit spools the tolerance excluded: 41 off on one
  58. channel fails the box and can only win as a type-only fallback."""
  59. loaded = [tray(5, "3A7BFEFF")] # blue channel 41 away
  60. assert scheduler._match_filaments_to_slots([req()], loaded) == [5]
  61. def test_ties_keep_the_caller_order_so_prefer_lowest_still_decides(self, scheduler):
  62. """Two spools equally close: the incoming order wins, which is the
  63. prefer-lowest sort when that preference is on."""
  64. loaded = [tray(2, "3A7BD0FF"), tray(7, "3A7BDAFF")] # both 5 away
  65. assert scheduler._match_filaments_to_slots([req()], loaded) == [2]
  66. assert scheduler._match_filaments_to_slots([req()], list(reversed(loaded))) == [7]
  67. def test_nearest_applies_within_a_shared_tray_info_idx_too(self, scheduler):
  68. """The tray_info_idx subset walks its own colour comparison."""
  69. loaded = [
  70. tray(1, FAR_BUT_ADMITTED) | {"tray_info_idx": "GFG99"},
  71. tray(3, NEAR) | {"tray_info_idx": "GFG99"},
  72. ]
  73. assert scheduler._match_filaments_to_slots([req(tray_info_idx="GFG99")], loaded) == [3]
  74. def test_type_is_still_a_hard_filter(self, scheduler):
  75. """A perfect colour in the wrong material never wins."""
  76. loaded = [tray(1, "3A7BD5FF", type_="ASA"), tray(3, NEAR)]
  77. assert scheduler._match_filaments_to_slots([req()], loaded) == [3]
  78. def test_each_slot_consumes_its_tray(self, scheduler):
  79. """Two slots wanting the same colour take different spools, nearest first."""
  80. loaded = [tray(1, FAR_BUT_ADMITTED), tray(3, NEAR)]
  81. mapping = scheduler._match_filaments_to_slots([req(slot_id=1), req(slot_id=2)], loaded)
  82. assert mapping == [3, 1]