Files
bambuddy/backend/tests/unit/test_spoolman_tracking.py
T
maziggy 72044e3a53 fix(usage): scope 3MF filament tracking to dispatched plate (#1697)
When a print targets a single plate from a multi-plate 3MF, both the
  internal Filament Inventory tracker and the Spoolman-mode tracker parsed
  the 3MF without a plate filter and summed every plate's filament — so a
  single lid print debited the spool the entire file's grey + black totals.

  The 3MF parser already supports plate_id (queue pre-flight uses it at
  print_queue.py:254/:286). Plumbed it through both dispatch paths:

  Queue path:
  - PrintSession gains a plate_id field; on_print_start queries the
    printer's currently-printing queue row and records queue_item.plate_id
    onto the session.
  - _track_from_3mf accepts plate_id and passes it to the extractor.
  - store_print_data moves its existing queue-item lookup above the
    extract and uses queue_item.plate_id as the plate filter.

  Direct-Print path (reprintArchive / printLibraryFile — never goes
  through the queue):
  - _print_plate_ids dict added in main.py, parallel to _print_ams_mappings.
  - register_expected_print accepts plate_id and stores it; the 2 sites in
    background_dispatch.py and the 1 site in print_scheduler.py now pass
    it (resolve was already happening, just needed reordering before the
    register call so the value is available).
  - Expected-print promotion in main.py injects _print_plate_ids[archive_id]
    into the session, guarded so a queue capture wins over the dict.
  - _get_start_plate_id helper feeds plate_id into all 3
    _store_spoolman_print_data call sites; spoolman_tracking.store_print_data
    takes the caller value first, falls back to queue_item.plate_id.

  PrintArchive.filament_used_grams stays file-level summed by design
  (#1593's contract — the archive describes the file, not the run); only
  the per-run usage attribution becomes plate-aware. Single-plate direct
  prints resolve to plate_id=1 → plate 1 = whole file, identical to the
  prior no-filter behaviour.
2026-06-09 09:16:36 +02:00

275 lines
11 KiB
Python

"""Unit tests for Spoolman tracking service helpers."""
import json
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from backend.app.services.spoolman_tracking import (
_get_fallback_spool_tag,
_global_tray_id_to_ams_slot,
_hash_serial_to_hex32,
_resolve_global_tray_id,
_resolve_spool_tag,
build_ams_tray_lookup,
store_print_data,
)
class TestResolveSpoolTag:
"""Tests for _resolve_spool_tag()."""
def test_prefers_tray_uuid_over_tag_uid(self):
tray = {"tray_uuid": "A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4", "tag_uid": "DEADBEEF"}
assert _resolve_spool_tag(tray) == "A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4"
def test_falls_back_to_tag_uid_when_no_uuid(self):
tray = {"tray_uuid": "", "tag_uid": "DEADBEEF"}
assert _resolve_spool_tag(tray) == "DEADBEEF"
def test_falls_back_to_tag_uid_when_uuid_zero(self):
tray = {"tray_uuid": "00000000000000000000000000000000", "tag_uid": "DEADBEEF"}
assert _resolve_spool_tag(tray) == "DEADBEEF"
def test_rejects_zero_tag_uid(self):
tray = {"tray_uuid": "", "tag_uid": "0000000000000000"}
assert _resolve_spool_tag(tray) == ""
def test_uses_fallback_tag_when_ids_missing(self):
tray = {"tray_uuid": "", "tag_uid": ""}
# global_tray_id 0 -> ams_id 0, tray_id 0
assert _resolve_spool_tag(tray, "01P00A000000000", 0) == "ABA7845700000000"
def test_uses_fallback_tag_when_ids_zero(self):
tray = {"tray_uuid": "00000000000000000000000000000000", "tag_uid": "0000000000000000"}
# global_tray_id 5 -> ams_id 1, tray_id 1
assert _resolve_spool_tag(tray, "01P00A000000000", 5) == "ABA7845700010001"
def test_prefers_tray_uuid_over_fallback_when_non_zero(self):
tray = {"tray_uuid": "A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4", "tag_uid": ""}
assert _resolve_spool_tag(tray, "01P00A000000000", 0) == "A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4"
def test_empty_both(self):
tray = {"tray_uuid": "", "tag_uid": ""}
assert _resolve_spool_tag(tray) == ""
def test_missing_keys(self):
assert _resolve_spool_tag({}) == ""
def test_zero_uuid_no_tag(self):
tray = {"tray_uuid": "00000000000000000000000000000000", "tag_uid": ""}
assert _resolve_spool_tag(tray) == ""
class TestResolveGlobalTrayId:
"""Tests for _resolve_global_tray_id()."""
def test_default_mapping(self):
"""slot 1 -> tray 0, slot 2 -> tray 1, etc."""
assert _resolve_global_tray_id(1, None) == 0
assert _resolve_global_tray_id(2, None) == 1
assert _resolve_global_tray_id(4, None) == 3
def test_custom_mapping(self):
"""Custom slot_to_tray overrides default."""
mapping = [5, 2, -1, 0]
assert _resolve_global_tray_id(1, mapping) == 5
assert _resolve_global_tray_id(2, mapping) == 2
assert _resolve_global_tray_id(4, mapping) == 0
def test_unmapped_slot(self):
"""Slot with -1 in mapping uses default."""
mapping = [5, -1, 2, 0]
assert _resolve_global_tray_id(2, mapping) == 1 # default: slot 2 -> tray 1
def test_slot_beyond_mapping(self):
"""Slot beyond mapping length uses default."""
mapping = [5, 2]
assert _resolve_global_tray_id(3, mapping) == 2 # default: slot 3 -> tray 2
def test_empty_mapping(self):
mapping = []
assert _resolve_global_tray_id(1, mapping) == 0
class TestFallbackTagHelpers:
"""Tests for frontend-mirrored fallback tag helpers."""
def test_hash_serial_matches_frontend_algorithm(self):
assert _hash_serial_to_hex32("01P00A000000000") == "ABA78457"
# Frontend trims and uppercases before hashing
assert _hash_serial_to_hex32(" 01p00a000000000 ") == "ABA78457"
def test_global_tray_to_ams_slot_standard_ams(self):
assert _global_tray_id_to_ams_slot(0) == (0, 0)
assert _global_tray_id_to_ams_slot(7) == (1, 3)
def test_global_tray_to_ams_slot_ams_ht(self):
assert _global_tray_id_to_ams_slot(128) == (128, 0)
assert _global_tray_id_to_ams_slot(135) == (135, 0)
def test_global_tray_to_ams_slot_external(self):
assert _global_tray_id_to_ams_slot(254) == (255, 0)
assert _global_tray_id_to_ams_slot(255) == (255, 1)
def test_get_fallback_spool_tag_standard(self):
assert _get_fallback_spool_tag("01P00A000000000", 5) == "ABA7845700010001"
def test_get_fallback_spool_tag_ams_ht(self):
assert _get_fallback_spool_tag("01P00A000000000", 128) == "ABA7845700800000"
def test_get_fallback_spool_tag_external(self):
assert _get_fallback_spool_tag("01P00A000000000", 255) == "ABA7845700FF0001"
class TestBuildAmsTrayLookup:
"""Tests for build_ams_tray_lookup()."""
def test_single_ams_unit(self):
raw = {
"ams": [
{
"id": 0,
"tray": [
{"id": 0, "tray_uuid": "AAA", "tag_uid": "111", "tray_type": "PLA"},
{"id": 1, "tray_uuid": "BBB", "tag_uid": "222", "tray_type": "ABS"},
],
}
]
}
lookup = build_ams_tray_lookup(raw)
assert lookup[0] == {"tray_uuid": "AAA", "tag_uid": "111", "tray_type": "PLA"}
assert lookup[1] == {"tray_uuid": "BBB", "tag_uid": "222", "tray_type": "ABS"}
def test_multiple_ams_units(self):
raw = {
"ams": [
{"id": 0, "tray": [{"id": 0, "tray_uuid": "A", "tag_uid": "", "tray_type": "PLA"}]},
{"id": 1, "tray": [{"id": 0, "tray_uuid": "B", "tag_uid": "", "tray_type": "PETG"}]},
]
}
lookup = build_ams_tray_lookup(raw)
assert 0 in lookup # AMS 0, tray 0
assert 4 in lookup # AMS 1, tray 0 (1*4+0)
assert lookup[4]["tray_uuid"] == "B"
def test_external_spool(self):
raw = {
"ams": [],
"vt_tray": [{"tray_uuid": "EXT", "tag_uid": "X", "tray_type": "TPU"}],
}
lookup = build_ams_tray_lookup(raw)
assert 254 in lookup
assert lookup[254]["tray_type"] == "TPU"
def test_empty_external_spool_skipped(self):
raw = {"ams": [], "vt_tray": [{"tray_type": ""}]}
lookup = build_ams_tray_lookup(raw)
assert 254 not in lookup
def test_no_ams_data(self):
assert build_ams_tray_lookup({}) == {}
assert build_ams_tray_lookup({"ams": []}) == {}
def test_missing_fields_default(self):
raw = {"ams": [{"id": 0, "tray": [{"id": 0}]}]}
lookup = build_ams_tray_lookup(raw)
assert lookup[0] == {"tray_uuid": "", "tag_uid": "", "tray_type": ""}
class TestStorePrintData:
"""Tests for store_print_data()."""
@pytest.mark.asyncio
async def test_prefers_explicit_ams_mapping_over_queue_mapping(self):
db = AsyncMock()
# store_print_data now queries the queue item unconditionally (to pick up
# plate_id for multi-plate 3MFs, #1697), then deletes any stale spoolman
# row before inserting the new one. Two execute calls in that order.
queue_item = SimpleNamespace(ams_mapping=json.dumps([2, -1, -1, -1]), plate_id=None)
queue_result = MagicMock()
queue_result.scalar_one_or_none.return_value = queue_item
delete_result = MagicMock()
db.execute = AsyncMock(side_effect=[queue_result, delete_result])
db.add = MagicMock()
db.commit = AsyncMock()
printer_manager = MagicMock()
printer_manager.get_status.return_value = SimpleNamespace(
raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "tray_type": "PLA"}, {"id": 1, "tray_type": "PLA"}]}]}
)
mock_settings = MagicMock()
mock_path = MagicMock()
mock_path.exists.return_value = True
mock_settings.base_dir.__truediv__.return_value = mock_path
with (
patch("backend.app.services.spoolman_tracking.app_settings", mock_settings),
patch("backend.app.api.routes.settings.get_setting", AsyncMock(side_effect=["true", "true"])),
patch(
"backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
return_value=[{"slot_id": 1, "used_g": 3.83, "type": "PLA", "color": "#FF0000"}],
),
patch("backend.app.utils.threemf_tools.extract_layer_filament_usage_from_3mf", return_value=None),
patch("backend.app.utils.threemf_tools.extract_filament_properties_from_3mf", return_value={}),
):
await store_print_data(
printer_id=1,
archive_id=15,
file_path="archives/test.3mf",
db=db,
printer_manager=printer_manager,
ams_mapping=[1, -1, -1, -1],
)
db.add.assert_called_once()
tracking = db.add.call_args.args[0]
assert tracking.slot_to_tray == [1, -1, -1, -1]
assert db.execute.await_count == 2
@pytest.mark.asyncio
async def test_passes_queue_plate_id_to_3mf_extract(self):
"""Multi-plate 3MFs queued for one plate must only count that plate's filament (#1697)."""
db = AsyncMock()
queue_item = SimpleNamespace(ams_mapping=None, plate_id=2)
queue_result = MagicMock()
queue_result.scalar_one_or_none.return_value = queue_item
delete_result = MagicMock()
db.execute = AsyncMock(side_effect=[queue_result, delete_result])
db.add = MagicMock()
db.commit = AsyncMock()
printer_manager = MagicMock()
printer_manager.get_status.return_value = SimpleNamespace(
raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "tray_type": "PLA"}]}]}
)
mock_settings = MagicMock()
mock_path = MagicMock()
mock_path.exists.return_value = True
mock_settings.base_dir.__truediv__.return_value = mock_path
extract_mock = MagicMock(return_value=[{"slot_id": 1, "used_g": 190.0, "type": "PETG", "color": "#888888"}])
with (
patch("backend.app.services.spoolman_tracking.app_settings", mock_settings),
patch("backend.app.api.routes.settings.get_setting", AsyncMock(side_effect=["true", "true"])),
patch("backend.app.utils.threemf_tools.extract_filament_usage_from_3mf", extract_mock),
patch("backend.app.utils.threemf_tools.extract_layer_filament_usage_from_3mf", return_value=None),
patch("backend.app.utils.threemf_tools.extract_filament_properties_from_3mf", return_value={}),
):
await store_print_data(
printer_id=1,
archive_id=15,
file_path="archives/test.3mf",
db=db,
printer_manager=printer_manager,
ams_mapping=[1, -1, -1, -1],
)
# plate_id=2 must be passed as the second positional arg
assert extract_mock.call_count == 1
assert extract_mock.call_args.args[1] == 2