test_printer_models.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. has_remote_storage_toggle,
  12. is_dual_nozzle_model,
  13. normalize_printer_model,
  14. normalize_printer_model_id,
  15. supports_nozzle_flow_type,
  16. )
  17. class TestGetRodType:
  18. """Tests for get_rod_type() rod/rail classification."""
  19. @pytest.mark.parametrize("model", ["X1C", "X1", "X1E", "P1P", "P1S"])
  20. def test_carbon_rod_models(self, model: str):
  21. assert get_rod_type(model) == "carbon"
  22. @pytest.mark.parametrize("model", ["C11", "C12", "C13"])
  23. def test_carbon_rod_internal_codes(self, model: str):
  24. assert get_rod_type(model) == "carbon"
  25. def test_p2s_is_steel_rod(self):
  26. """P2S uses hardened steel rods, not carbon rods (#640)."""
  27. assert get_rod_type("P2S") == "steel_rod"
  28. def test_p2s_internal_code_is_steel_rod(self):
  29. """N7 (P2S internal code) uses steel rods."""
  30. assert get_rod_type("N7") == "steel_rod"
  31. @pytest.mark.parametrize("model", ["A1", "A1 Mini", "H2D", "H2D Pro", "H2C", "H2S"])
  32. def test_linear_rail_models(self, model: str):
  33. assert get_rod_type(model) == "linear_rail"
  34. @pytest.mark.parametrize("model", ["N1", "N2S", "A11", "A12", "O1D", "O1E", "O2D", "O1C", "O1C2", "O1S"])
  35. def test_linear_rail_internal_codes(self, model: str):
  36. assert get_rod_type(model) == "linear_rail"
  37. def test_unknown_model_returns_none(self):
  38. assert get_rod_type("UNKNOWN") is None
  39. def test_none_returns_none(self):
  40. assert get_rod_type(None) is None
  41. def test_case_insensitive(self):
  42. assert get_rod_type("p2s") == "steel_rod"
  43. assert get_rod_type("x1c") == "carbon"
  44. assert get_rod_type("a1") == "linear_rail"
  45. def test_strips_whitespace_and_dashes(self):
  46. assert get_rod_type(" P2S ") == "steel_rod"
  47. assert get_rod_type("A1-Mini") == "linear_rail"
  48. class TestX2DModel:
  49. """X2D printer support (issue #988).
  50. The X2D is a dual-nozzle enclosed printer launched April 2026. It shares
  51. the hardened steel rod hardware with P2S (NOT carbon rods) and uses
  52. RTSP on port 322 like other X/H series printers. Internal SSDP/MQTT
  53. model code is "N6"; serial numbers begin with "20P9".
  54. """
  55. def test_x2d_is_steel_rod_display_name(self):
  56. assert get_rod_type("X2D") == "steel_rod"
  57. def test_x2d_is_steel_rod_internal_code(self):
  58. assert get_rod_type("N6") == "steel_rod"
  59. def test_x2d_model_id_map(self):
  60. assert normalize_printer_model_id("N6") == "X2D"
  61. def test_x2d_model_map(self):
  62. assert normalize_printer_model("Bambu Lab X2D") == "X2D"
  63. def test_x2d_has_ethernet_display_name(self):
  64. assert has_ethernet("X2D") is True
  65. def test_x2d_has_ethernet_internal_code(self):
  66. assert has_ethernet("N6") is True
  67. def test_x2d_supports_rtsp_display_name(self):
  68. assert supports_rtsp("X2D") is True
  69. def test_x2d_supports_rtsp_internal_code(self):
  70. assert supports_rtsp("N6") is True
  71. def test_x2d_camera_port_is_rtsp(self):
  72. assert get_camera_port("N6") == 322
  73. assert get_camera_port("X2D") == 322
  74. def test_x2d_not_in_carbon_rod_set(self):
  75. """Regression guard: X2D has hardened steel rods, not carbon (#988).
  76. A prior PR classified X2D as carbon; the reporter confirmed it uses
  77. the same stainless steel rod gantry as P2S. This assertion pins the
  78. classification so a future change that reverts it will fail loudly.
  79. """
  80. assert "X2D" not in CARBON_ROD_MODELS
  81. assert "N6" not in CARBON_ROD_MODELS
  82. assert "X2D" in STEEL_ROD_MODELS
  83. assert "N6" in STEEL_ROD_MODELS
  84. class TestA2LModel:
  85. """A2L printer support (#1684).
  86. The A2L is a hybrid 3D printer + cutter/plotter announced June 2026. It
  87. uses linear rails like the A1 family, has NO Ethernet (Wi-Fi 2.4 GHz only),
  88. a low-rate chamber-image camera on port 6000 (no RTSP), and a single FDM
  89. extruder (the second "tool head" in BambuStudio's profile is the cutter,
  90. not a second extruder — must NOT be classified as dual-nozzle). Internal
  91. SSDP/MQTT model code is "N9"; serial numbers begin with "26A19".
  92. """
  93. def test_a2l_is_linear_rail_display_name(self):
  94. assert get_rod_type("A2L") == "linear_rail"
  95. def test_a2l_is_linear_rail_internal_code(self):
  96. assert get_rod_type("N9") == "linear_rail"
  97. def test_a2l_model_id_map(self):
  98. assert normalize_printer_model_id("N9") == "A2L"
  99. def test_a2l_model_map(self):
  100. assert normalize_printer_model("Bambu Lab A2L") == "A2L"
  101. def test_a2l_has_no_ethernet_display_name(self):
  102. """A2L specs (bambulab.com/de-de/a2l/specs) list Ethernet 'Nicht verfügbar'."""
  103. assert has_ethernet("A2L") is False
  104. def test_a2l_has_no_ethernet_internal_code(self):
  105. assert has_ethernet("N9") is False
  106. def test_a2l_does_not_support_rtsp_display_name(self):
  107. """A2L uses the low-rate chamber-image protocol on port 6000, not RTSP."""
  108. assert supports_rtsp("A2L") is False
  109. def test_a2l_does_not_support_rtsp_internal_code(self):
  110. assert supports_rtsp("N9") is False
  111. def test_a2l_camera_port_is_chamber_image(self):
  112. assert get_camera_port("A2L") == 6000
  113. assert get_camera_port("N9") == 6000
  114. def test_a2l_is_not_dual_nozzle(self):
  115. """A2L has a single FDM extruder + a cutter/plotter head. The
  116. BambuStudio profile flag ``use_double_extruder_default_texture`` flags
  117. the dual TOOL HEADS, not dual filament extrusion — A2L must not land
  118. in the dual-nozzle group or AMS routing will target the deputy slot
  119. and the firmware will reject the print with 07FF_8012.
  120. """
  121. assert is_dual_nozzle_model("A2L") is False
  122. assert is_dual_nozzle_model("N9") is False
  123. def test_a2l_in_linear_rail_set(self):
  124. assert "A2L" in LINEAR_RAIL_MODELS
  125. assert "N9" in LINEAR_RAIL_MODELS
  126. def test_a2l_not_in_carbon_or_steel_rod_sets(self):
  127. assert "A2L" not in CARBON_ROD_MODELS
  128. assert "N9" not in CARBON_ROD_MODELS
  129. assert "A2L" not in STEEL_ROD_MODELS
  130. assert "N9" not in STEEL_ROD_MODELS
  131. class TestA1SeriesModelIds:
  132. """Regression guard for the A1-family internal-code → display-name map.
  133. The serial-prefix and firmware-API key tables across the codebase agree
  134. that N2S is the A1 (serial prefix 039) and N1 is the A1 Mini (serial
  135. prefix 030). PRINTER_MODEL_ID_MAP had these swapped, which silently
  136. misclassified A1 as A1 Mini in any path that resolved by internal code.
  137. """
  138. def test_n2s_is_a1(self):
  139. assert normalize_printer_model_id("N2S") == "A1"
  140. def test_n1_is_a1_mini(self):
  141. assert normalize_printer_model_id("N1") == "A1 Mini"
  142. class TestDualNozzleModel:
  143. """is_dual_nozzle_model — the single source of truth for nozzle class,
  144. consumed by start_print, the K-profile routes, and the re-slice guard."""
  145. def test_h2d_and_pro_are_dual(self):
  146. # Takes a normalized model code (like has_ethernet) — "H2D Pro" with a
  147. # space is accepted; full "Bambu Lab …" names are normalized by callers.
  148. assert is_dual_nozzle_model("H2D") is True
  149. assert is_dual_nozzle_model("H2D Pro") is True
  150. assert is_dual_nozzle_model("H2DPRO") is True
  151. def test_internal_codes_are_dual(self):
  152. assert is_dual_nozzle_model("O1D") is True # H2D
  153. assert is_dual_nozzle_model("O1E") is True # H2D Pro
  154. def test_single_nozzle_models_are_not_dual(self):
  155. # H2S is in the H2 family but single-nozzle (#1386) — must be False.
  156. for model in ("X1C", "X1E", "P1S", "P1P", "A1", "A1 Mini", "P2S", "H2S"):
  157. assert is_dual_nozzle_model(model) is False, model
  158. def test_none_and_empty_are_not_dual(self):
  159. assert is_dual_nozzle_model(None) is False
  160. assert is_dual_nozzle_model("") is False
  161. class TestSupportsNozzleFlowType:
  162. """Which models offer a Standard / High Flow choice on a K-profile.
  163. Mirrors the slicer's own rule — BambuStudio/OrcaSlicer gate their
  164. Nozzle-Flow control on ``len(nozzle_volume) // len(nozzle_diameter) > 1``
  165. read from the machine preset. Evaluated over every bundled Bambu profile,
  166. only the A-series lands on one variant. Getting this wrong in the
  167. permissive direction shows a redundant dropdown; getting it wrong in the
  168. other direction makes half a printer's calibration table unreachable.
  169. """
  170. def test_a_series_has_one_flow_variant(self):
  171. for model in ("A1", "A1 Mini", "A1MINI", "A2L"):
  172. assert supports_nozzle_flow_type(model) is False, model
  173. def test_a_series_internal_codes(self):
  174. for code in ("N1", "N2S", "N9", "A04", "A11", "A12"):
  175. assert supports_nozzle_flow_type(code) is False, code
  176. def test_single_nozzle_models_still_offer_both_flows(self):
  177. # The split is NOT nozzle count: all of these are single-nozzle and
  178. # all carry two nozzle_volume variants in their machine preset.
  179. for model in ("X1", "X1C", "X1E", "P1P", "P1S", "P2S", "H2S"):
  180. assert supports_nozzle_flow_type(model) is True, model
  181. def test_dual_nozzle_models_offer_both_flows(self):
  182. for model in ("H2D", "H2D Pro", "H2C"):
  183. assert supports_nozzle_flow_type(model) is True, model
  184. def test_unknown_and_empty_default_to_supported(self):
  185. # Fail open: a redundant dropdown beats an unreachable half-table.
  186. assert supports_nozzle_flow_type(None) is True
  187. assert supports_nozzle_flow_type("") is True
  188. assert supports_nozzle_flow_type("SomeFuturePrinter") is True
  189. class TestHasExternalStorage:
  190. """Pins which Bambu models have a MicroSD slot. The connection
  191. diagnostic flips its ``external_storage`` check from ``fail`` to
  192. ``skip`` based on this — a false add (X1C marked as no-storage) would
  193. silently disable a genuine fail signal for X1/P1/P2S/H2 users."""
  194. @pytest.mark.parametrize("model", ["A1", "A1 Mini", "A1MINI", "A1-Mini", "a1"])
  195. def test_a1_series_has_no_external_storage(self, model: str):
  196. assert has_external_storage(model) is False
  197. @pytest.mark.parametrize("model", ["N1", "N2S", "A04", "A11", "A12"])
  198. def test_a1_internal_codes_have_no_external_storage(self, model: str):
  199. assert has_external_storage(model) is False
  200. @pytest.mark.parametrize(
  201. "model",
  202. ["X1C", "X1E", "X1", "P1S", "P1P", "P2S", "H2D", "H2D Pro", "H2C", "H2S", "X2D"],
  203. )
  204. def test_other_models_have_external_storage(self, model: str):
  205. assert has_external_storage(model) is True
  206. def test_unknown_model_defaults_to_true(self):
  207. # Default-true keeps the diagnostic active for new Bambu models;
  208. # add them to NO_EXTERNAL_STORAGE_MODELS explicitly when they ship
  209. # without a slot.
  210. assert has_external_storage("BrandNewModel2027") is True
  211. def test_none_and_empty_default_to_true(self):
  212. assert has_external_storage(None) is True
  213. assert has_external_storage("") is True
  214. class TestHasRemoteStorageToggle:
  215. """#2524: P1-series have a slot but no reachable control to enable the
  216. "Store sent files on external storage" option. The diagnostic uses this
  217. to skip (not fail) on those models. A false add here would silently
  218. disable the genuine fail signal for X1/P2S/H2 users."""
  219. @pytest.mark.parametrize("model", ["P1S", "P1P", "p1s", "P1-S", "P1 S"])
  220. def test_p1_series_has_no_reachable_toggle(self, model: str):
  221. assert has_remote_storage_toggle(model) is False
  222. @pytest.mark.parametrize(
  223. "model",
  224. ["X1C", "X1E", "X1", "P2S", "H2D", "H2D Pro", "H2C", "H2S", "X2D", "A1", "A1 Mini"],
  225. )
  226. def test_other_models_have_reachable_toggle(self, model: str):
  227. assert has_remote_storage_toggle(model) is True
  228. def test_unknown_and_empty_default_to_true(self):
  229. # Default-true keeps the fail signal active for models not explicitly
  230. # listed — the skip only applies to known no-toggle firmware.
  231. assert has_remote_storage_toggle("BrandNewModel2027") is True
  232. assert has_remote_storage_toggle(None) is True
  233. assert has_remote_storage_toggle("") is True