test_printer_models.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. """Unit tests for printer model utilities."""
  2. import pytest
  3. from backend.app.services.camera import get_camera_port, supports_rtsp
  4. from backend.app.utils.printer_models import (
  5. CARBON_ROD_MODELS,
  6. LINEAR_RAIL_MODELS,
  7. STEEL_ROD_MODELS,
  8. get_rod_type,
  9. has_ethernet,
  10. has_external_storage,
  11. is_dual_nozzle_model,
  12. normalize_printer_model,
  13. normalize_printer_model_id,
  14. )
  15. class TestGetRodType:
  16. """Tests for get_rod_type() rod/rail classification."""
  17. @pytest.mark.parametrize("model", ["X1C", "X1", "X1E", "P1P", "P1S"])
  18. def test_carbon_rod_models(self, model: str):
  19. assert get_rod_type(model) == "carbon"
  20. @pytest.mark.parametrize("model", ["C11", "C12", "C13"])
  21. def test_carbon_rod_internal_codes(self, model: str):
  22. assert get_rod_type(model) == "carbon"
  23. def test_p2s_is_steel_rod(self):
  24. """P2S uses hardened steel rods, not carbon rods (#640)."""
  25. assert get_rod_type("P2S") == "steel_rod"
  26. def test_p2s_internal_code_is_steel_rod(self):
  27. """N7 (P2S internal code) uses steel rods."""
  28. assert get_rod_type("N7") == "steel_rod"
  29. @pytest.mark.parametrize("model", ["A1", "A1 Mini", "H2D", "H2D Pro", "H2C", "H2S"])
  30. def test_linear_rail_models(self, model: str):
  31. assert get_rod_type(model) == "linear_rail"
  32. @pytest.mark.parametrize("model", ["N1", "N2S", "A11", "A12", "O1D", "O1E", "O2D", "O1C", "O1C2", "O1S"])
  33. def test_linear_rail_internal_codes(self, model: str):
  34. assert get_rod_type(model) == "linear_rail"
  35. def test_unknown_model_returns_none(self):
  36. assert get_rod_type("UNKNOWN") is None
  37. def test_none_returns_none(self):
  38. assert get_rod_type(None) is None
  39. def test_case_insensitive(self):
  40. assert get_rod_type("p2s") == "steel_rod"
  41. assert get_rod_type("x1c") == "carbon"
  42. assert get_rod_type("a1") == "linear_rail"
  43. def test_strips_whitespace_and_dashes(self):
  44. assert get_rod_type(" P2S ") == "steel_rod"
  45. assert get_rod_type("A1-Mini") == "linear_rail"
  46. class TestX2DModel:
  47. """X2D printer support (issue #988).
  48. The X2D is a dual-nozzle enclosed printer launched April 2026. It shares
  49. the hardened steel rod hardware with P2S (NOT carbon rods) and uses
  50. RTSP on port 322 like other X/H series printers. Internal SSDP/MQTT
  51. model code is "N6"; serial numbers begin with "20P9".
  52. """
  53. def test_x2d_is_steel_rod_display_name(self):
  54. assert get_rod_type("X2D") == "steel_rod"
  55. def test_x2d_is_steel_rod_internal_code(self):
  56. assert get_rod_type("N6") == "steel_rod"
  57. def test_x2d_model_id_map(self):
  58. assert normalize_printer_model_id("N6") == "X2D"
  59. def test_x2d_model_map(self):
  60. assert normalize_printer_model("Bambu Lab X2D") == "X2D"
  61. def test_x2d_has_ethernet_display_name(self):
  62. assert has_ethernet("X2D") is True
  63. def test_x2d_has_ethernet_internal_code(self):
  64. assert has_ethernet("N6") is True
  65. def test_x2d_supports_rtsp_display_name(self):
  66. assert supports_rtsp("X2D") is True
  67. def test_x2d_supports_rtsp_internal_code(self):
  68. assert supports_rtsp("N6") is True
  69. def test_x2d_camera_port_is_rtsp(self):
  70. assert get_camera_port("N6") == 322
  71. assert get_camera_port("X2D") == 322
  72. def test_x2d_not_in_carbon_rod_set(self):
  73. """Regression guard: X2D has hardened steel rods, not carbon (#988).
  74. A prior PR classified X2D as carbon; the reporter confirmed it uses
  75. the same stainless steel rod gantry as P2S. This assertion pins the
  76. classification so a future change that reverts it will fail loudly.
  77. """
  78. assert "X2D" not in CARBON_ROD_MODELS
  79. assert "N6" not in CARBON_ROD_MODELS
  80. assert "X2D" in STEEL_ROD_MODELS
  81. assert "N6" in STEEL_ROD_MODELS
  82. class TestA2LModel:
  83. """A2L printer support (#1684).
  84. The A2L is a hybrid 3D printer + cutter/plotter announced June 2026. It
  85. uses linear rails like the A1 family, has NO Ethernet (Wi-Fi 2.4 GHz only),
  86. a low-rate chamber-image camera on port 6000 (no RTSP), and a single FDM
  87. extruder (the second "tool head" in BambuStudio's profile is the cutter,
  88. not a second extruder — must NOT be classified as dual-nozzle). Internal
  89. SSDP/MQTT model code is "N9"; serial numbers begin with "26A19".
  90. """
  91. def test_a2l_is_linear_rail_display_name(self):
  92. assert get_rod_type("A2L") == "linear_rail"
  93. def test_a2l_is_linear_rail_internal_code(self):
  94. assert get_rod_type("N9") == "linear_rail"
  95. def test_a2l_model_id_map(self):
  96. assert normalize_printer_model_id("N9") == "A2L"
  97. def test_a2l_model_map(self):
  98. assert normalize_printer_model("Bambu Lab A2L") == "A2L"
  99. def test_a2l_has_no_ethernet_display_name(self):
  100. """A2L specs (bambulab.com/de-de/a2l/specs) list Ethernet 'Nicht verfügbar'."""
  101. assert has_ethernet("A2L") is False
  102. def test_a2l_has_no_ethernet_internal_code(self):
  103. assert has_ethernet("N9") is False
  104. def test_a2l_does_not_support_rtsp_display_name(self):
  105. """A2L uses the low-rate chamber-image protocol on port 6000, not RTSP."""
  106. assert supports_rtsp("A2L") is False
  107. def test_a2l_does_not_support_rtsp_internal_code(self):
  108. assert supports_rtsp("N9") is False
  109. def test_a2l_camera_port_is_chamber_image(self):
  110. assert get_camera_port("A2L") == 6000
  111. assert get_camera_port("N9") == 6000
  112. def test_a2l_is_not_dual_nozzle(self):
  113. """A2L has a single FDM extruder + a cutter/plotter head. The
  114. BambuStudio profile flag ``use_double_extruder_default_texture`` flags
  115. the dual TOOL HEADS, not dual filament extrusion — A2L must not land
  116. in the dual-nozzle group or AMS routing will target the deputy slot
  117. and the firmware will reject the print with 07FF_8012.
  118. """
  119. assert is_dual_nozzle_model("A2L") is False
  120. assert is_dual_nozzle_model("N9") is False
  121. def test_a2l_in_linear_rail_set(self):
  122. assert "A2L" in LINEAR_RAIL_MODELS
  123. assert "N9" in LINEAR_RAIL_MODELS
  124. def test_a2l_not_in_carbon_or_steel_rod_sets(self):
  125. assert "A2L" not in CARBON_ROD_MODELS
  126. assert "N9" not in CARBON_ROD_MODELS
  127. assert "A2L" not in STEEL_ROD_MODELS
  128. assert "N9" not in STEEL_ROD_MODELS
  129. class TestA1SeriesModelIds:
  130. """Regression guard for the A1-family internal-code → display-name map.
  131. The serial-prefix and firmware-API key tables across the codebase agree
  132. that N2S is the A1 (serial prefix 039) and N1 is the A1 Mini (serial
  133. prefix 030). PRINTER_MODEL_ID_MAP had these swapped, which silently
  134. misclassified A1 as A1 Mini in any path that resolved by internal code.
  135. """
  136. def test_n2s_is_a1(self):
  137. assert normalize_printer_model_id("N2S") == "A1"
  138. def test_n1_is_a1_mini(self):
  139. assert normalize_printer_model_id("N1") == "A1 Mini"
  140. class TestDualNozzleModel:
  141. """is_dual_nozzle_model — the single source of truth for nozzle class,
  142. consumed by start_print, the K-profile routes, and the re-slice guard."""
  143. def test_h2d_and_pro_are_dual(self):
  144. # Takes a normalized model code (like has_ethernet) — "H2D Pro" with a
  145. # space is accepted; full "Bambu Lab …" names are normalized by callers.
  146. assert is_dual_nozzle_model("H2D") is True
  147. assert is_dual_nozzle_model("H2D Pro") is True
  148. assert is_dual_nozzle_model("H2DPRO") is True
  149. def test_internal_codes_are_dual(self):
  150. assert is_dual_nozzle_model("O1D") is True # H2D
  151. assert is_dual_nozzle_model("O1E") is True # H2D Pro
  152. def test_single_nozzle_models_are_not_dual(self):
  153. # H2S is in the H2 family but single-nozzle (#1386) — must be False.
  154. for model in ("X1C", "X1E", "P1S", "P1P", "A1", "A1 Mini", "P2S", "H2S"):
  155. assert is_dual_nozzle_model(model) is False, model
  156. def test_none_and_empty_are_not_dual(self):
  157. assert is_dual_nozzle_model(None) is False
  158. assert is_dual_nozzle_model("") is False
  159. class TestHasExternalStorage:
  160. """Pins which Bambu models have a MicroSD slot. The connection
  161. diagnostic flips its ``external_storage`` check from ``fail`` to
  162. ``skip`` based on this — a false add (X1C marked as no-storage) would
  163. silently disable a genuine fail signal for X1/P1/P2S/H2 users."""
  164. @pytest.mark.parametrize("model", ["A1", "A1 Mini", "A1MINI", "A1-Mini", "a1"])
  165. def test_a1_series_has_no_external_storage(self, model: str):
  166. assert has_external_storage(model) is False
  167. @pytest.mark.parametrize("model", ["N1", "N2S", "A04", "A11", "A12"])
  168. def test_a1_internal_codes_have_no_external_storage(self, model: str):
  169. assert has_external_storage(model) is False
  170. @pytest.mark.parametrize(
  171. "model",
  172. ["X1C", "X1E", "X1", "P1S", "P1P", "P2S", "H2D", "H2D Pro", "H2C", "H2S", "X2D"],
  173. )
  174. def test_other_models_have_external_storage(self, model: str):
  175. assert has_external_storage(model) is True
  176. def test_unknown_model_defaults_to_true(self):
  177. # Default-true keeps the diagnostic active for new Bambu models;
  178. # add them to NO_EXTERNAL_STORAGE_MODELS explicitly when they ship
  179. # without a slot.
  180. assert has_external_storage("BrandNewModel2027") is True
  181. def test_none_and_empty_default_to_true(self):
  182. assert has_external_storage(None) is True
  183. assert has_external_storage("") is True