Files
bambuddy/backend/tests/unit/test_printer_models.py
maziggy 6fb814c5ea feat(printer): add X2D support — camera, dual-nozzle, K-profile, maintenance (#988)
The Bambu Lab X2D (launched April 2026, dual-nozzle, enclosed, hardened
  steel rod gantry, AMS 2 Pro compatible) identifies itself as internal
  model code N6 via SSDP/MQTT, and real serials begin with 20P9. None of
  these identifiers existed in Bambuddy's registries, so the camera
  service fell back to the chamber-image protocol on port 6000 (X2D
  doesn't speak it), firmware-check logged "Unknown printer model: N6",
  and the dual-nozzle K-profile paths — gated on the H2D serial prefix
  "094" — would have treated X2D as single-nozzle.

  Backend:
  - Register N6 → X2D across every registry (PRINTER_MODEL_ID_MAP,
    PRINTER_MODEL_MAP, STEEL_ROD_MODELS, ETHERNET_MODELS,
    CHAMBER_TEMP_SUPPORTED_MODELS, firmware-check API keys + wiki path,
    virtual-printer SSDP/product/serial tables, DB vp_model_fixes).
  - supports_rtsp(): match the X2 display-name prefix and the N6 internal
    code; camera now routes to RTSP on port 322.
  - Dual-nozzle serial prefix check in bambu_mqtt.delete_kprofile and
    kprofiles.set_kprofile broadened to ("094", "20P9") — X2D now takes
    the H2D-style cali_idx in-place edit path.
  - is_h2d model gate in bambu_mqtt.start_print extended with "X2D" so
    timelapse / bed_leveling / flow_cali / vibration_cali / layer_inspect
    are sent as integers and external-spool ams_id 254/255 routing is
    preserved (H2D-style deputy-nozzle addressing).

  X2D uses hardened steel rods like P2S — it is intentionally placed in
  STEEL_ROD_MODELS, not CARBON_ROD_MODELS. A regression-guard test pins
  the classification.

  Frontend:
  - mapModelCode in PrintersPage and SpoolBuddyAmsPage handle N6 and X2D.
  - Enclosure-door badge and airduct-mode whitelists include X2D.
  - MaintenancePage.getMaintenanceWikiUrl routes X2D to P2S wiki URLs for
    steel-rod lubrication, belt tension, cold-pull, and PTFE tube
    (exported to enable direct unit testing).

  Tests:
  - test_printer_models.py: TestX2DModel (10 assertions).
  - test_bambu_mqtt.py: X2D in start_print ams_mapping and is_h2d gate;
    TestDeleteKProfileDualNozzleDetection across H2D, X2D, P2S, X1C.
  - MaintenancePageWikiUrls.test.tsx: 15 assertions covering X2D, P2S
    regression, X1C/H2D/A1Mini regression, and model-name normalisation.

  Docs:
  - README: added X2 series to the supported printers table.
  - CHANGELOG: new entry under 0.2.3b4 Fixed.

  Credit to @krautech for the report and debug bundle, and to @legend813
  for PR #989 which seeded most of the registry changes — rod-type
  classification was corrected (steel, not carbon) and the dual-nozzle /
  K-profile / is_h2d gaps were added on top.
2026-04-16 10:40:32 +02:00

107 lines
3.7 KiB
Python

"""Unit tests for printer model utilities."""
import pytest
from backend.app.services.camera import get_camera_port, supports_rtsp
from backend.app.utils.printer_models import (
CARBON_ROD_MODELS,
STEEL_ROD_MODELS,
get_rod_type,
has_ethernet,
normalize_printer_model,
normalize_printer_model_id,
)
class TestGetRodType:
"""Tests for get_rod_type() rod/rail classification."""
@pytest.mark.parametrize("model", ["X1C", "X1", "X1E", "P1P", "P1S"])
def test_carbon_rod_models(self, model: str):
assert get_rod_type(model) == "carbon"
@pytest.mark.parametrize("model", ["C11", "C12", "C13"])
def test_carbon_rod_internal_codes(self, model: str):
assert get_rod_type(model) == "carbon"
def test_p2s_is_steel_rod(self):
"""P2S uses hardened steel rods, not carbon rods (#640)."""
assert get_rod_type("P2S") == "steel_rod"
def test_p2s_internal_code_is_steel_rod(self):
"""N7 (P2S internal code) uses steel rods."""
assert get_rod_type("N7") == "steel_rod"
@pytest.mark.parametrize("model", ["A1", "A1 Mini", "H2D", "H2D Pro", "H2C", "H2S"])
def test_linear_rail_models(self, model: str):
assert get_rod_type(model) == "linear_rail"
@pytest.mark.parametrize("model", ["N1", "N2S", "A11", "A12", "O1D", "O1E", "O2D", "O1C", "O1C2", "O1S"])
def test_linear_rail_internal_codes(self, model: str):
assert get_rod_type(model) == "linear_rail"
def test_unknown_model_returns_none(self):
assert get_rod_type("UNKNOWN") is None
def test_none_returns_none(self):
assert get_rod_type(None) is None
def test_case_insensitive(self):
assert get_rod_type("p2s") == "steel_rod"
assert get_rod_type("x1c") == "carbon"
assert get_rod_type("a1") == "linear_rail"
def test_strips_whitespace_and_dashes(self):
assert get_rod_type(" P2S ") == "steel_rod"
assert get_rod_type("A1-Mini") == "linear_rail"
class TestX2DModel:
"""X2D printer support (issue #988).
The X2D is a dual-nozzle enclosed printer launched April 2026. It shares
the hardened steel rod hardware with P2S (NOT carbon rods) and uses
RTSP on port 322 like other X/H series printers. Internal SSDP/MQTT
model code is "N6"; serial numbers begin with "20P9".
"""
def test_x2d_is_steel_rod_display_name(self):
assert get_rod_type("X2D") == "steel_rod"
def test_x2d_is_steel_rod_internal_code(self):
assert get_rod_type("N6") == "steel_rod"
def test_x2d_model_id_map(self):
assert normalize_printer_model_id("N6") == "X2D"
def test_x2d_model_map(self):
assert normalize_printer_model("Bambu Lab X2D") == "X2D"
def test_x2d_has_ethernet_display_name(self):
assert has_ethernet("X2D") is True
def test_x2d_has_ethernet_internal_code(self):
assert has_ethernet("N6") is True
def test_x2d_supports_rtsp_display_name(self):
assert supports_rtsp("X2D") is True
def test_x2d_supports_rtsp_internal_code(self):
assert supports_rtsp("N6") is True
def test_x2d_camera_port_is_rtsp(self):
assert get_camera_port("N6") == 322
assert get_camera_port("X2D") == 322
def test_x2d_not_in_carbon_rod_set(self):
"""Regression guard: X2D has hardened steel rods, not carbon (#988).
A prior PR classified X2D as carbon; the reporter confirmed it uses
the same stainless steel rod gantry as P2S. This assertion pins the
classification so a future change that reverts it will fail loudly.
"""
assert "X2D" not in CARBON_ROD_MODELS
assert "N6" not in CARBON_ROD_MODELS
assert "X2D" in STEEL_ROD_MODELS
assert "N6" in STEEL_ROD_MODELS