test_printer_models.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. STEEL_ROD_MODELS,
  7. get_rod_type,
  8. has_ethernet,
  9. has_external_storage,
  10. is_dual_nozzle_model,
  11. normalize_printer_model,
  12. normalize_printer_model_id,
  13. )
  14. class TestGetRodType:
  15. """Tests for get_rod_type() rod/rail classification."""
  16. @pytest.mark.parametrize("model", ["X1C", "X1", "X1E", "P1P", "P1S"])
  17. def test_carbon_rod_models(self, model: str):
  18. assert get_rod_type(model) == "carbon"
  19. @pytest.mark.parametrize("model", ["C11", "C12", "C13"])
  20. def test_carbon_rod_internal_codes(self, model: str):
  21. assert get_rod_type(model) == "carbon"
  22. def test_p2s_is_steel_rod(self):
  23. """P2S uses hardened steel rods, not carbon rods (#640)."""
  24. assert get_rod_type("P2S") == "steel_rod"
  25. def test_p2s_internal_code_is_steel_rod(self):
  26. """N7 (P2S internal code) uses steel rods."""
  27. assert get_rod_type("N7") == "steel_rod"
  28. @pytest.mark.parametrize("model", ["A1", "A1 Mini", "H2D", "H2D Pro", "H2C", "H2S"])
  29. def test_linear_rail_models(self, model: str):
  30. assert get_rod_type(model) == "linear_rail"
  31. @pytest.mark.parametrize("model", ["N1", "N2S", "A11", "A12", "O1D", "O1E", "O2D", "O1C", "O1C2", "O1S"])
  32. def test_linear_rail_internal_codes(self, model: str):
  33. assert get_rod_type(model) == "linear_rail"
  34. def test_unknown_model_returns_none(self):
  35. assert get_rod_type("UNKNOWN") is None
  36. def test_none_returns_none(self):
  37. assert get_rod_type(None) is None
  38. def test_case_insensitive(self):
  39. assert get_rod_type("p2s") == "steel_rod"
  40. assert get_rod_type("x1c") == "carbon"
  41. assert get_rod_type("a1") == "linear_rail"
  42. def test_strips_whitespace_and_dashes(self):
  43. assert get_rod_type(" P2S ") == "steel_rod"
  44. assert get_rod_type("A1-Mini") == "linear_rail"
  45. class TestX2DModel:
  46. """X2D printer support (issue #988).
  47. The X2D is a dual-nozzle enclosed printer launched April 2026. It shares
  48. the hardened steel rod hardware with P2S (NOT carbon rods) and uses
  49. RTSP on port 322 like other X/H series printers. Internal SSDP/MQTT
  50. model code is "N6"; serial numbers begin with "20P9".
  51. """
  52. def test_x2d_is_steel_rod_display_name(self):
  53. assert get_rod_type("X2D") == "steel_rod"
  54. def test_x2d_is_steel_rod_internal_code(self):
  55. assert get_rod_type("N6") == "steel_rod"
  56. def test_x2d_model_id_map(self):
  57. assert normalize_printer_model_id("N6") == "X2D"
  58. def test_x2d_model_map(self):
  59. assert normalize_printer_model("Bambu Lab X2D") == "X2D"
  60. def test_x2d_has_ethernet_display_name(self):
  61. assert has_ethernet("X2D") is True
  62. def test_x2d_has_ethernet_internal_code(self):
  63. assert has_ethernet("N6") is True
  64. def test_x2d_supports_rtsp_display_name(self):
  65. assert supports_rtsp("X2D") is True
  66. def test_x2d_supports_rtsp_internal_code(self):
  67. assert supports_rtsp("N6") is True
  68. def test_x2d_camera_port_is_rtsp(self):
  69. assert get_camera_port("N6") == 322
  70. assert get_camera_port("X2D") == 322
  71. def test_x2d_not_in_carbon_rod_set(self):
  72. """Regression guard: X2D has hardened steel rods, not carbon (#988).
  73. A prior PR classified X2D as carbon; the reporter confirmed it uses
  74. the same stainless steel rod gantry as P2S. This assertion pins the
  75. classification so a future change that reverts it will fail loudly.
  76. """
  77. assert "X2D" not in CARBON_ROD_MODELS
  78. assert "N6" not in CARBON_ROD_MODELS
  79. assert "X2D" in STEEL_ROD_MODELS
  80. assert "N6" in STEEL_ROD_MODELS
  81. class TestA1SeriesModelIds:
  82. """Regression guard for the A1-family internal-code → display-name map.
  83. The serial-prefix and firmware-API key tables across the codebase agree
  84. that N2S is the A1 (serial prefix 039) and N1 is the A1 Mini (serial
  85. prefix 030). PRINTER_MODEL_ID_MAP had these swapped, which silently
  86. misclassified A1 as A1 Mini in any path that resolved by internal code.
  87. """
  88. def test_n2s_is_a1(self):
  89. assert normalize_printer_model_id("N2S") == "A1"
  90. def test_n1_is_a1_mini(self):
  91. assert normalize_printer_model_id("N1") == "A1 Mini"
  92. class TestDualNozzleModel:
  93. """is_dual_nozzle_model — the single source of truth for nozzle class,
  94. consumed by start_print, the K-profile routes, and the re-slice guard."""
  95. def test_h2d_and_pro_are_dual(self):
  96. # Takes a normalized model code (like has_ethernet) — "H2D Pro" with a
  97. # space is accepted; full "Bambu Lab …" names are normalized by callers.
  98. assert is_dual_nozzle_model("H2D") is True
  99. assert is_dual_nozzle_model("H2D Pro") is True
  100. assert is_dual_nozzle_model("H2DPRO") is True
  101. def test_internal_codes_are_dual(self):
  102. assert is_dual_nozzle_model("O1D") is True # H2D
  103. assert is_dual_nozzle_model("O1E") is True # H2D Pro
  104. def test_single_nozzle_models_are_not_dual(self):
  105. # H2S is in the H2 family but single-nozzle (#1386) — must be False.
  106. for model in ("X1C", "X1E", "P1S", "P1P", "A1", "A1 Mini", "P2S", "H2S"):
  107. assert is_dual_nozzle_model(model) is False, model
  108. def test_none_and_empty_are_not_dual(self):
  109. assert is_dual_nozzle_model(None) is False
  110. assert is_dual_nozzle_model("") is False
  111. class TestHasExternalStorage:
  112. """Pins which Bambu models have a MicroSD slot. The connection
  113. diagnostic flips its ``external_storage`` check from ``fail`` to
  114. ``skip`` based on this — a false add (X1C marked as no-storage) would
  115. silently disable a genuine fail signal for X1/P1/P2S/H2 users."""
  116. @pytest.mark.parametrize("model", ["A1", "A1 Mini", "A1MINI", "A1-Mini", "a1"])
  117. def test_a1_series_has_no_external_storage(self, model: str):
  118. assert has_external_storage(model) is False
  119. @pytest.mark.parametrize("model", ["N1", "N2S", "A04", "A11", "A12"])
  120. def test_a1_internal_codes_have_no_external_storage(self, model: str):
  121. assert has_external_storage(model) is False
  122. @pytest.mark.parametrize(
  123. "model",
  124. ["X1C", "X1E", "X1", "P1S", "P1P", "P2S", "H2D", "H2D Pro", "H2C", "H2S", "X2D"],
  125. )
  126. def test_other_models_have_external_storage(self, model: str):
  127. assert has_external_storage(model) is True
  128. def test_unknown_model_defaults_to_true(self):
  129. # Default-true keeps the diagnostic active for new Bambu models;
  130. # add them to NO_EXTERNAL_STORAGE_MODELS explicitly when they ship
  131. # without a slot.
  132. assert has_external_storage("BrandNewModel2027") is True
  133. def test_none_and_empty_default_to_true(self):
  134. assert has_external_storage(None) is True
  135. assert has_external_storage("") is True