test_printer_models.py 15 KB

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