Files
bambuddy/backend/tests/unit/test_slot_nozzle_resolution.py
T
maziggy a7b563334e Configure a spool's filament preset and K profile per nozzle
A slicer preset is bound to a printer model: "Bambu PLA Basic @BBL X1C" is
not the same preset as "@BBL H2C", and Bambu names a nozzle size in it as
well. A spool carried exactly one, which was right until the same spool was
used on a second machine -- the AMS slot on the other one was then
configured with a preset that machine has no profile for. K profiles had
the matching gap from the other side: the tables have always been keyed per
hotend, but the picker could not express it.

spool_filament_preset and its Spoolman twin store the exceptions, keyed
(spool, printer_model, nozzle_diameter). Model rather than printer because
the preset is a property of the model -- "@BBL X1C" is the same preset on
every X1C, and asking per machine would mean picking the identical value
twice. K profiles stay on printer_id, because a K value is measured on one
physical hotend and two machines of the same model legitimately differ.
Resolution is exact (model, diameter) -> (model, "") -> the spool's own
preset, so a spool nobody has configured behaves exactly as it did before.
The form writes one row per nozzle size and never the "" row; that level is
kept for API clients wanting one value to cover a model.

Both halves cover every standard nozzle size rather than the size currently
fitted, because a spool is configured once and nozzles get swapped. The PA
Profile tab becomes a Printers tab: a model list beside a detail pane
holding a preset row per size and a K-profile grid of size by hotend. Each
model is offered only the presets that name it, through the same matcher
the Configure AMS Slot modal filters with, which moves out of that
component into utils/slicerPrinterMatch. Presets whose name identifies no
model -- most user-authored and OrcaSlicer ones -- stay offered everywhere,
as does whatever is already selected, so a saved override cannot vanish
from the control that shows it. Every preset carries an origin badge in the
wording and colours that modal already uses.

Every path that configures a slot now respects both: manual assign in
either inventory mode, RFID auto-assign, the Spoolman tag link, the re-fire
when a slot goes empty to loaded, the re-apply after a calibration-table
refresh, and the re-selection when a Filament Track Switch moves an AMS to
the other nozzle. Which nozzle a slot feeds, and how wide it is, was worked
out independently in seven of those places, each reading nozzles[0] for
every slot on the machine -- correct on a single-nozzle printer and on a
dual-nozzle printer with matching nozzles, wrong the moment two sizes are
fitted. That resolution is now services/slot_nozzle.

Which array entry belongs to which hotend is no longer inferred. Measured
on an H2D fitted with a 0.4 high flow on the left and a 0.6 on the right,
nozzles[0] reads the right hotend, so the array is indexed by extruder id
and the H2/X2 parser's convention is the one that holds. The legacy
parser's opposite convention never governs a real dual-nozzle machine:
every model in DUAL_NOZZLE_MODELS reports device.nozzle.info, and
left_nozzle_diameter appears in no log or wire capture. Two comments that
said otherwise were wrong and are fixed; amsHelpers' code was right all
along and only its comment lied.

Four defects surfaced while wiring it, all pre-existing except the last.
The picker identified a chosen calibration by cali_idx alone, and the
printer numbers its calibration table per nozzle -- on a dual-nozzle
machine the same index exists on both hotends meaning different things, so
saving could persist the other hotend's K value and diameter; SpoolBuddy's
write-tag page carried a verbatim copy and gets the same fix. RFID
auto-assign chose a K profile with no extruder test at all, so a spool
calibrated on both hotends had a coin toss decide which pressure-advance
value the slot got, on the path that runs unattended every time a Bambu
spool is loaded. The Spoolman tag-link path resolved no preset whatsoever,
configuring every linked slot with a generic material id and discarding a
preset set in inventory -- the same defect #1713 fixed on the assign path,
one function over. And an FTS inlet move re-selected K for nozzle 0 rather
than for the nozzle the AMS had just been moved to.

The last one is new here: a per-model override can be a cloud USER preset,
whose PFUS-prefixed id the slicer rejects, and passing it straight into
extrusion_cali_sel would silently lose the K-profile link. Reached the
printer only where such an override exists, which is why nothing in the
suite caught it. printer_safe_filament_id falls through to the spool's own
preset and then the tray's RFID value instead.

Reading a printer's calibration table asks for one nozzle size at a time.
H2-series firmware answers only the first one or two of a concurrent burst
of extrusion_cali_get and silently drops the rest, each dropped request
costing a five-second timeout before its retry: measured at 11 and 23
seconds on an H2C and an H2D for four parallel requests, against roughly
one second in series. An X1C answers all four at once, which is why this
only ever surfaced on dual-diameter printers. Printers themselves are read
in parallel -- separate machines are separate connections.

The Configure AMS Slot dialog opens on the spool's own configured values,
falling back to the slot's last manual configuration and then the tray's
RFID data. The spool form is wider for the two-pane layout, colour, weight,
cost and location move to their own tab in two columns, and a printer card
in expanded view lists every fitted nozzle size rather than the first entry
alone.
2026-08-27 13:03:15 +02:00

121 lines
5.0 KiB
Python

"""Which nozzle an AMS slot feeds.
Every path that configures a slot needs the extruder and that nozzle's
diameter, and both the filament preset and the K profile are stored per
diameter -- so a wrong answer here silently selects the wrong preset AND the
wrong K value. Seven call sites used to work it out independently, each reading
``nozzles[0]`` for every slot on the machine.
``nozzles[0]`` is right on a single-nozzle printer, and right on a dual-nozzle
printer with matching nozzles, which is why it survived. These pin the case it
is wrong for: two different sizes fitted, which is the machine the per-nozzle
feature exists for.
"""
from __future__ import annotations
from backend.app.services.slot_nozzle import (
DEFAULT_NOZZLE_DIAMETER,
nozzle_diameter_for_extruder,
resolve_slot_nozzle,
)
class _Nozzle:
def __init__(self, diameter: str):
self.nozzle_diameter = diameter
class _State:
def __init__(self, diameters, ams_extruder_map=None, ams_switch_inlet=None):
self.nozzles = [_Nozzle(d) for d in diameters]
self.ams_extruder_map = ams_extruder_map
self.ams_switch_inlet = ams_switch_inlet
# extruder 0 is the RIGHT hotend, 1 the left.
MIXED = ["0.4", "0.2"]
class TestDualNozzleMixedDiameters:
"""The case the old shortcut got wrong."""
def test_each_hotend_reports_its_own_diameter(self):
state = _State(MIXED)
assert nozzle_diameter_for_extruder(state, 0, "H2C") == "0.4"
assert nozzle_diameter_for_extruder(state, 1, "H2C") == "0.2"
def test_the_slots_extruder_decides_which_one(self):
# AMS 0 is bound to the left hotend, AMS 1 to the right.
state = _State(MIXED, ams_extruder_map={"0": 1, "1": 0})
left = resolve_slot_nozzle(state, 0, 0, "H2C")
right = resolve_slot_nozzle(state, 1, 0, "H2C")
assert (left.extruder, left.diameter) == (1, "0.2")
assert (right.extruder, right.diameter) == (0, "0.4")
def test_external_slots_name_their_side_by_tray_id(self):
# Ext-L is tray 0 -> extruder 1, Ext-R is tray 1 -> extruder 0.
state = _State(MIXED)
assert resolve_slot_nozzle(state, 255, 0, "H2D").diameter == "0.2"
assert resolve_slot_nozzle(state, 255, 1, "H2D").diameter == "0.4"
def test_an_fts_inlet_answers_when_there_is_no_extruder_map(self):
state = _State(MIXED, ams_switch_inlet={"0": "A"})
resolved = resolve_slot_nozzle(state, 0, 0, "H2C")
assert resolved.extruder is not None
assert resolved.diameter in {"0.4", "0.2"}
class TestSingleNozzle:
"""Must behave exactly as the old shortcut did."""
def test_index_zero_whatever_the_map_says(self):
# A single-nozzle model has one entry; an extruder id from a stale map
# must not index past it.
state = _State(["0.6"], ams_extruder_map={"0": 1})
assert nozzle_diameter_for_extruder(state, 1, "X1C") == "0.6"
assert resolve_slot_nozzle(state, 0, 0, "X1C").diameter == "0.6"
def test_no_map_means_unknown_extruder_not_zero(self):
# "I don't know" and "the right-hand nozzle" are different answers; the
# caller's own default of 0 is correct on a single-nozzle machine, but
# storing it as a fact is what bound a left K-profile to a right slot.
state = _State(["0.4"])
resolved = resolve_slot_nozzle(state, 0, 0, "X1C")
assert resolved.extruder is None
assert resolved.extruder_or_default == 0
class TestMissingHardware:
"""Called on every assign, so it must never raise."""
def test_no_state_at_all(self):
resolved = resolve_slot_nozzle(None, 0, 0, "H2C")
assert (resolved.extruder, resolved.diameter) == (None, DEFAULT_NOZZLE_DIAMETER)
def test_printer_has_reported_no_nozzles(self):
assert nozzle_diameter_for_extruder(_State([]), 0, "H2C") == DEFAULT_NOZZLE_DIAMETER
def test_the_second_hotend_is_absent_from_the_report(self):
# H2C parks a nozzle back in its rack and the entry can go missing;
# falling back to the other hotend beats returning nothing.
assert nozzle_diameter_for_extruder(_State(["0.4"]), 1, "H2C") == "0.4"
def test_a_blank_diameter_is_not_a_diameter(self):
# NozzleInfo starts life with an empty string before MQTT fills it in.
# Falling back to the OTHER hotend's size would invent a fact about a
# different nozzle, so a blank primary takes the default instead --
# which is what every call site did before this module existed.
assert nozzle_diameter_for_extruder(_State(["", "0.6"]), 0, "H2C") == DEFAULT_NOZZLE_DIAMETER
assert nozzle_diameter_for_extruder(_State(["", ""]), 0, "H2C") == DEFAULT_NOZZLE_DIAMETER
class TestMatchingNozzles:
"""The common fleet: both hotends the same size."""
def test_both_conventions_agree_so_the_answer_cannot_be_wrong(self):
state = _State(["0.4", "0.4"], ams_extruder_map={"0": 1})
assert nozzle_diameter_for_extruder(state, 0, "H2C") == "0.4"
assert nozzle_diameter_for_extruder(state, 1, "H2C") == "0.4"