| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073 |
- """Unit tests for cost tracking in usage_tracker.py.
- Tests cost calculation scenarios:
- - Spool-specific cost_per_kg
- - Default fallback cost from settings
- - Spools without cost (None)
- - Completed prints
- - Failed/partial prints
- - Cost aggregation to archives
- """
- import os
- import tempfile
- from datetime import datetime, timezone
- from types import SimpleNamespace
- from unittest.mock import AsyncMock, MagicMock, patch
- import pytest
- from backend.app.services.usage_tracker import (
- PrintSession,
- _active_sessions,
- _track_from_3mf,
- on_print_complete,
- )
- def _make_spool(spool_id=1, label_weight=1000, weight_used=0, cost_per_kg=None):
- """Create a mock Spool object with cost fields."""
- spool = MagicMock()
- spool.id = spool_id
- spool.label_weight = label_weight
- spool.weight_used = weight_used
- spool.cost_per_kg = cost_per_kg
- spool.last_used = None
- spool.material = "PLA"
- return spool
- def _make_assignment(spool_id=1, printer_id=1, ams_id=0, tray_id=0):
- """Create a mock SpoolAssignment object."""
- assignment = MagicMock()
- assignment.spool_id = spool_id
- assignment.printer_id = printer_id
- assignment.ams_id = ams_id
- assignment.tray_id = tray_id
- return assignment
- def _make_archive(archive_id=1, file_path=None):
- """Create a mock PrintArchive object with a temp file, and register cleanup."""
- if file_path is None:
- with tempfile.NamedTemporaryFile(delete=False, suffix=".3mf", prefix="test_print_") as tmp:
- file_path = tmp.name
- # Register cleanup for this file after the test
- import pytest
- frame = None
- try:
- raise Exception
- except Exception:
- import sys
- frame = sys._getframe(1)
- request = frame.f_locals.get("request")
- if request is not None:
- def cleanup():
- try:
- os.remove(file_path)
- except Exception:
- pass
- request.addfinalizer(cleanup)
- archive = MagicMock()
- archive.id = archive_id
- archive.file_path = file_path
- # Explicit numeric default so the #1344 top-up logic (archive_grams -
- # tracked_grams) doesn't compare a MagicMock to a float. Tests that
- # exercise the top-up path overwrite this with a real number.
- archive.filament_used_grams = 0
- return archive
- @pytest.fixture(autouse=True)
- def cleanup_temp_archives():
- yield
- # Cleanup any temp .3mf files created by _make_archive
- import glob
- for f in glob.glob("test_print_*.3mf"):
- try:
- os.remove(f)
- except Exception:
- pass
- @pytest.fixture(autouse=True)
- def cleanup_test_print_gcode():
- yield
- import os
- path = "archives/test/test_print.gcode.3mf"
- if os.path.exists(path):
- try:
- os.remove(path)
- except Exception:
- pass
- @pytest.fixture
- def archive_factory_temp():
- import tempfile
- def _factory(*args, **kwargs):
- with tempfile.NamedTemporaryFile(delete=False, suffix=".3mf", prefix="test_print_", dir="archives/test") as tmp:
- kwargs["file_path"] = tmp.name
- return kwargs["file_path"]
- yield _factory
- # Cleanup
- import glob
- import os
- for f in glob.glob("archives/test/test_print_*.3mf"):
- try:
- os.remove(f)
- except Exception:
- pass
- def _mock_db_sequential(responses):
- """Create mock db that returns responses in order."""
- db = AsyncMock()
- call_count = [0]
- async def mock_execute(*args, **kwargs):
- idx = call_count[0]
- call_count[0] += 1
- result = MagicMock()
- if idx < len(responses):
- result.scalar_one_or_none.return_value = responses[idx]
- else:
- result.scalar_one_or_none.return_value = None
- return result
- db.execute = mock_execute
- return db
- class TestCostCalculation:
- """Tests for cost calculation in usage tracking."""
- @pytest.fixture(autouse=True)
- def _clear_sessions(self):
- _active_sessions.clear()
- yield
- _active_sessions.clear()
- @pytest.mark.asyncio
- async def test_cost_with_spool_specific_cost_per_kg(self):
- """Cost is calculated using spool-specific cost_per_kg when available."""
- # Spool with cost_per_kg = 25.00 USD/kg
- spool = _make_spool(spool_id=1, label_weight=1000, cost_per_kg=25.0)
- assignment = _make_assignment(spool_id=1)
- archive = _make_archive(archive_id=10)
- _active_sessions[1] = PrintSession(
- printer_id=1,
- print_name="Test",
- started_at=datetime.now(timezone.utc),
- tray_remain_start={(0, 0): 80},
- tray_now_at_start=0,
- )
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
- progress=100,
- layer_num=50,
- tray_now=0,
- )
- # db returns: archive, queue_item(None), assignment, spool
- db = _mock_db_sequential([archive, None, assignment, spool])
- # 20g used from 3MF
- filament_usage = [{"slot_id": 1, "used_g": 20.0, "type": "PLA", "color": "#FF0000"}]
- with (
- patch("backend.app.core.config.settings") as mock_settings,
- patch("backend.app.api.routes.settings.get_setting", return_value="15.0"), # default cost
- patch(
- "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
- return_value=filament_usage,
- ),
- ):
- mock_settings.base_dir = MagicMock()
- mock_path = MagicMock()
- mock_path.exists.return_value = True
- mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
- results = await on_print_complete(
- printer_id=1,
- data={"status": "completed"},
- printer_manager=printer_manager,
- db=db,
- archive_id=10,
- )
- assert len(results) == 1
- assert results[0]["spool_id"] == 1
- assert results[0]["weight_used"] == 20.0
- # Cost = 20g / 1000 * 25.0 = 0.50
- assert results[0]["cost"] == 0.50
- @pytest.mark.asyncio
- async def test_cost_with_default_fallback(self):
- """Cost uses default_filament_cost from settings when spool cost is None."""
- # Spool without cost_per_kg
- spool = _make_spool(spool_id=1, label_weight=1000, cost_per_kg=None)
- assignment = _make_assignment(spool_id=1)
- archive = _make_archive(archive_id=10)
- _active_sessions[1] = PrintSession(
- printer_id=1,
- print_name="Test",
- started_at=datetime.now(timezone.utc),
- tray_remain_start={(0, 0): 80},
- tray_now_at_start=0,
- )
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
- progress=100,
- layer_num=50,
- tray_now=0,
- )
- # db returns: archive, queue_item(None), assignment, spool
- db = _mock_db_sequential([archive, None, assignment, spool])
- # 30g used from 3MF
- filament_usage = [{"slot_id": 1, "used_g": 30.0, "type": "PLA", "color": "#FF0000"}]
- with (
- patch("backend.app.core.config.settings") as mock_settings,
- patch("backend.app.api.routes.settings.get_setting", return_value="15.0"), # default: 15.0/kg
- patch(
- "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
- return_value=filament_usage,
- ),
- ):
- mock_settings.base_dir = MagicMock()
- mock_path = MagicMock()
- mock_path.exists.return_value = True
- mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
- results = await on_print_complete(
- printer_id=1,
- data={"status": "completed"},
- printer_manager=printer_manager,
- db=db,
- archive_id=10,
- )
- assert len(results) == 1
- assert results[0]["spool_id"] == 1
- assert results[0]["weight_used"] == 30.0
- # Cost = 30g / 1000 * 15.0 = 0.45
- assert results[0]["cost"] == 0.45
- @pytest.mark.asyncio
- async def test_cost_uses_the_settings_default_when_none_is_saved(self):
- """Never saved, the default is the 25/kg the Settings page shows, as for
- every other reader of it; not 0, which left unpriced spools uncosted."""
- # Spool without cost_per_kg
- spool = _make_spool(spool_id=1, label_weight=1000, cost_per_kg=None)
- assignment = _make_assignment(spool_id=1)
- archive = _make_archive(archive_id=10)
- _active_sessions[1] = PrintSession(
- printer_id=1,
- print_name="Test",
- started_at=datetime.now(timezone.utc),
- tray_remain_start={(0, 0): 80},
- tray_now_at_start=0,
- )
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
- progress=100,
- layer_num=50,
- tray_now=0,
- )
- # db returns: archive, queue_item(None), assignment, spool
- db = _mock_db_sequential([archive, None, assignment, spool])
- # 30g used from 3MF
- filament_usage = [{"slot_id": 1, "used_g": 30.0, "type": "PLA", "color": "#FF0000"}]
- with (
- patch("backend.app.core.config.settings") as mock_settings,
- patch("backend.app.api.routes.settings.get_setting", return_value=None),
- patch(
- "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
- return_value=filament_usage,
- ),
- ):
- mock_settings.base_dir = MagicMock()
- mock_path = MagicMock()
- mock_path.exists.return_value = True
- mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
- results = await on_print_complete(
- printer_id=1,
- data={"status": "completed"},
- printer_manager=printer_manager,
- db=db,
- archive_id=10,
- )
- assert len(results) == 1
- assert results[0]["spool_id"] == 1
- assert results[0]["weight_used"] == 30.0
- # Cost = 30g / 1000 * 25.0 = 0.75
- assert results[0]["cost"] == 0.75
- @pytest.mark.asyncio
- async def test_cost_zero_when_default_cost_is_zero(self):
- """Cost is None when both spool cost and default cost are 0."""
- # Spool without cost_per_kg
- spool = _make_spool(spool_id=1, label_weight=1000, cost_per_kg=None)
- assignment = _make_assignment(spool_id=1)
- archive = _make_archive(archive_id=10)
- _active_sessions[1] = PrintSession(
- printer_id=1,
- print_name="Test",
- started_at=datetime.now(timezone.utc),
- tray_remain_start={(0, 0): 80},
- tray_now_at_start=0,
- )
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
- progress=100,
- layer_num=50,
- tray_now=0,
- )
- # db returns: archive, queue_item(None), assignment, spool
- db = _mock_db_sequential([archive, None, assignment, spool])
- filament_usage = [{"slot_id": 1, "used_g": 10.0, "type": "PLA", "color": "#FF0000"}]
- with (
- patch("backend.app.core.config.settings") as mock_settings,
- patch("backend.app.api.routes.settings.get_setting", return_value="0.0"), # no default cost
- patch(
- "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
- return_value=filament_usage,
- ),
- ):
- mock_settings.base_dir = MagicMock()
- mock_path = MagicMock()
- mock_path.exists.return_value = True
- mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
- results = await on_print_complete(
- printer_id=1,
- data={"status": "completed"},
- printer_manager=printer_manager,
- db=db,
- archive_id=10,
- )
- assert len(results) == 1
- assert results[0]["cost"] is None
- @pytest.mark.asyncio
- async def test_cost_for_failed_print_uses_actual_usage(self):
- """Failed print at 50% progress calculates cost from actual usage."""
- spool = _make_spool(spool_id=1, label_weight=1000, cost_per_kg=20.0)
- assignment = _make_assignment(spool_id=1)
- archive = _make_archive(archive_id=10)
- _active_sessions[1] = PrintSession(
- printer_id=1,
- print_name="Test",
- started_at=datetime.now(timezone.utc),
- tray_remain_start={(0, 0): 80},
- tray_now_at_start=0,
- )
- # Failed at 50% progress
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
- progress=50,
- layer_num=25,
- tray_now=0,
- )
- # db returns: archive, queue_item(None), assignment, spool
- db = _mock_db_sequential([archive, None, assignment, spool])
- # 40g total, but only 50% used
- filament_usage = [{"slot_id": 1, "used_g": 40.0, "type": "PLA", "color": "#FF0000"}]
- with (
- patch("backend.app.core.config.settings") as mock_settings,
- patch("backend.app.api.routes.settings.get_setting", return_value="15.0"),
- patch(
- "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
- return_value=filament_usage,
- ),
- patch(
- "backend.app.utils.threemf_tools.extract_layer_filament_usage_from_3mf",
- return_value=None, # No layer data, use linear scaling
- ),
- ):
- mock_settings.base_dir = MagicMock()
- mock_path = MagicMock()
- mock_path.exists.return_value = True
- mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
- results = await on_print_complete(
- printer_id=1,
- data={"status": "failed", "last_progress": 50.0},
- printer_manager=printer_manager,
- db=db,
- archive_id=10,
- )
- assert len(results) == 1
- # 50% of 40g = 20g
- assert results[0]["weight_used"] == 20.0
- # Cost = 20g / 1000 * 20.0 = 0.40
- assert results[0]["cost"] == 0.40
- async def _failed_after_firmware_reset(self, *, last_progress=0.0, last_layer_num=0, layer_usage=None):
- """A failed print whose printer already reset progress and layer to 0."""
- spool = _make_spool(spool_id=1, label_weight=1000, cost_per_kg=20.0)
- _active_sessions[1] = PrintSession(
- printer_id=1,
- print_name="Test",
- started_at=datetime.now(timezone.utc),
- tray_remain_start={(0, 0): 80},
- tray_now_at_start=0,
- )
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]}, progress=0, layer_num=0, tray_now=0
- )
- db = _mock_db_sequential([_make_archive(archive_id=10), None, _make_assignment(spool_id=1), spool])
- cumulative = MagicMock(return_value={0: 1000.0}) # filament 0 = slot 1
- with (
- patch("backend.app.core.config.settings") as mock_settings,
- patch("backend.app.api.routes.settings.get_setting", return_value="15.0"),
- patch(
- "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
- return_value=[{"slot_id": 1, "used_g": 40.0, "type": "PLA", "color": "#FF0000"}],
- ),
- patch("backend.app.utils.threemf_tools.extract_layer_filament_usage_from_3mf", return_value=layer_usage),
- patch("backend.app.utils.threemf_tools.get_cumulative_usage_at_layer", cumulative),
- patch("backend.app.utils.threemf_tools.extract_filament_properties_from_3mf", return_value={}),
- patch("backend.app.utils.threemf_tools.mm_to_grams", return_value=7.0),
- ):
- mock_settings.base_dir = MagicMock()
- mock_path = MagicMock()
- mock_path.exists.return_value = True
- mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
- results = await on_print_complete(
- printer_id=1,
- data={"status": "failed", "last_progress": last_progress, "last_layer_num": last_layer_num},
- printer_manager=printer_manager,
- db=db,
- archive_id=10,
- )
- return results, cumulative
- @pytest.mark.asyncio
- async def test_a_progress_reset_to_zero_falls_back_to_the_last_progress(self):
- """Firmware zeroes progress on cancel; 0 % would charge nothing."""
- results, _ = await self._failed_after_firmware_reset(last_progress=40.0)
- # 40 % of 40 g
- assert results[0]["weight_used"] == 16.0
- @pytest.mark.asyncio
- async def test_a_layer_reset_to_zero_falls_back_to_the_last_layer(self):
- results, cumulative = await self._failed_after_firmware_reset(
- last_progress=40.0, last_layer_num=12, layer_usage={1: {12: 1000.0}}
- )
- assert cumulative.call_args.args[1] == 12
- # The per-layer grams, not the 40 % estimate
- assert results[0]["weight_used"] == 7.0
- @pytest.mark.asyncio
- async def test_cost_with_ams_fallback_tracking(self):
- """AMS fallback tracking also calculates cost correctly."""
- spool = _make_spool(spool_id=2, label_weight=1000, cost_per_kg=30.0)
- assignment = _make_assignment(spool_id=2)
- _active_sessions[1] = PrintSession(
- printer_id=1,
- print_name="Test",
- started_at=datetime.now(timezone.utc),
- tray_remain_start={(0, 0): 80},
- )
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
- tray_now=0,
- last_loaded_tray=-1,
- )
- # Pad 2 Nones for _find_3mf_by_filename DB queries (library + archive search),
- # then assignment and spool for the AMS fallback path
- db = _mock_db_sequential([None, None, assignment, spool])
- with patch("backend.app.api.routes.settings.get_setting", return_value="15.0"):
- results = await on_print_complete(
- printer_id=1,
- data={"status": "completed"},
- printer_manager=printer_manager,
- db=db,
- archive_id=None, # No archive = AMS fallback
- )
- assert len(results) == 1
- assert results[0]["spool_id"] == 2
- # 10% of 1000g = 100g
- assert results[0]["weight_used"] == 100.0
- # Cost = 100g / 1000 * 30.0 = 3.00
- assert results[0]["cost"] == 3.0
- @pytest.mark.asyncio
- async def test_multi_filament_cost_aggregation(self):
- """Multiple spools in one print have their costs tracked separately."""
- spool1 = _make_spool(spool_id=1, label_weight=1000, cost_per_kg=20.0)
- spool2 = _make_spool(spool_id=2, label_weight=1000, cost_per_kg=25.0)
- assignment1 = _make_assignment(spool_id=1, ams_id=0, tray_id=0)
- assignment2 = _make_assignment(spool_id=2, ams_id=0, tray_id=1)
- archive = _make_archive(archive_id=10)
- _active_sessions[1] = PrintSession(
- printer_id=1,
- print_name="Test",
- started_at=datetime.now(timezone.utc),
- tray_remain_start={(0, 0): 80, (0, 1): 90},
- tray_now_at_start=0,
- )
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}, {"id": 1, "remain": 80}]}]},
- progress=100,
- layer_num=50,
- tray_now=0,
- )
- # Mock slot-to-tray mapping: slot 1 -> tray 0, slot 2 -> tray 1
- ams_mapping = [0, 1]
- # db returns: archive, assignment1, spool1, assignment2, spool2
- # db: archive, queue item for the plate (None), then assignments and
- # spools; the given ams_mapping skips only the mapping fallback
- db = _mock_db_sequential([archive, None, assignment1, spool1, assignment2, spool2])
- # Two filaments used
- filament_usage = [
- {"slot_id": 1, "used_g": 15.0, "type": "PLA", "color": "#FF0000"},
- {"slot_id": 2, "used_g": 25.0, "type": "PLA", "color": "#00FF00"},
- ]
- with (
- patch("backend.app.core.config.settings") as mock_settings,
- patch("backend.app.api.routes.settings.get_setting", return_value="15.0"),
- patch(
- "backend.app.utils.threemf_tools.extract_filament_usage_from_3mf",
- return_value=filament_usage,
- ),
- ):
- mock_settings.base_dir = MagicMock()
- mock_path = MagicMock()
- mock_path.exists.return_value = True
- mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
- results = await on_print_complete(
- printer_id=1,
- data={"status": "completed"},
- printer_manager=printer_manager,
- db=db,
- archive_id=10,
- ams_mapping=ams_mapping,
- )
- assert len(results) == 2
- # First spool: 15g at 20/kg = 0.30
- spool1_result = next(r for r in results if r["spool_id"] == 1)
- assert spool1_result["weight_used"] == 15.0
- assert spool1_result["cost"] == 0.30
- # Second spool: 25g at 25/kg = 0.625, rounded to 0.62
- spool2_result = next(r for r in results if r["spool_id"] == 2)
- assert spool2_result["weight_used"] == 25.0
- assert spool2_result["cost"] == 0.62
- class TestCostAggregation:
- """Tests for cost aggregation to PrintArchive."""
- @pytest.mark.asyncio
- async def test_costs_summed_in_results(self):
- """Multiple spool costs are correctly summed from result dicts."""
- results = [
- {"spool_id": 1, "weight_used": 20.0, "cost": 0.50},
- {"spool_id": 2, "weight_used": 30.0, "cost": 0.75},
- ]
- total_cost = sum(r.get("cost", 0) or 0 for r in results)
- assert total_cost == 1.25
- @pytest.mark.asyncio
- async def test_null_costs_handled_in_aggregation(self):
- """None costs don't break aggregation."""
- results = [
- {"spool_id": 1, "weight_used": 20.0, "cost": 0.50},
- {"spool_id": 2, "weight_used": 30.0, "cost": None}, # No cost
- {"spool_id": 3, "weight_used": 10.0, "cost": 0.25},
- ]
- total_cost = sum(r.get("cost", 0) or 0 for r in results)
- assert total_cost == 0.75 # Only spools 1 and 3
- @pytest.mark.asyncio
- async def test_archive_cost_not_overwritten_with_zero(self):
- """archive.cost is preserved when no spool usage has cost data."""
- # Spool without cost_per_kg, default_filament_cost also 0 → cost=None per usage
- spool = _make_spool(spool_id=1, label_weight=1000, cost_per_kg=None)
- assignment = _make_assignment(spool_id=1)
- archive = _make_archive(archive_id=10)
- archive.cost = 5.00 # Pre-existing cost from catalog
- archive.print_name = "TestPrint"
- archive.printer_id = 1
- _active_sessions[1] = PrintSession(
- printer_id=1,
- print_name="TestPrint",
- started_at=datetime.now(timezone.utc),
- tray_remain_start={(0, 0): 80},
- tray_now_at_start=0,
- )
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
- progress=100,
- layer_num=50,
- tray_now=0,
- )
- # Build mock db that returns proper scalars for the aggregation queries
- responses = []
- # 1. select(PrintArchive) → archive
- responses.append(("scalar_one_or_none", archive))
- # 2. select(PrintQueueItem) → None
- responses.append(("scalar_one_or_none", None))
- # 3. select(SpoolAssignment) → assignment
- responses.append(("scalar_one_or_none", assignment))
- # 4. select(Spool) → spool
- responses.append(("scalar_one_or_none", spool))
- # 5. cost aggregation: select archive to update cost
- responses.append(("scalar_one_or_none", archive))
- db = AsyncMock()
- call_count = [0]
- async def mock_execute(*args, **kwargs):
- idx = call_count[0]
- call_count[0] += 1
- result = MagicMock()
- if idx < len(responses):
- method, value = responses[idx]
- if method == "scalar":
- result.scalar.return_value = value
- result.scalar_one_or_none.return_value = value
- else:
- result.scalar_one_or_none.return_value = value
- result.scalar.return_value = value
- else:
- result.scalar_one_or_none.return_value = None
- result.scalar.return_value = None
- return result
- db.execute = mock_execute
- filament_usage = [{"slot_id": 1, "used_g": 10.0, "type": "PLA", "color": "#FF0000"}]
- with (
- patch("backend.app.core.config.settings") as mock_settings,
- patch("backend.app.api.routes.settings.get_setting", return_value="0.0"), # no default cost
- patch("backend.app.utils.threemf_tools.extract_filament_usage_from_3mf", return_value=filament_usage),
- ):
- mock_settings.base_dir = MagicMock()
- mock_path = MagicMock()
- mock_path.exists.return_value = True
- mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
- results = await on_print_complete(
- printer_id=1,
- data={"status": "completed"},
- printer_manager=printer_manager,
- db=db,
- archive_id=10,
- )
- # Usage tracked but cost is None (no cost_per_kg, no default)
- assert len(results) == 1
- assert results[0]["cost"] is None
- # Archive cost should NOT have been overwritten — still 5.00
- assert archive.cost == 5.00
- @pytest.mark.asyncio
- async def test_archive_cost_set_when_spool_has_cost(self):
- """archive.cost is set from spool usage when cost data exists."""
- spool = _make_spool(spool_id=1, label_weight=1000, cost_per_kg=25.0)
- assignment = _make_assignment(spool_id=1)
- archive = _make_archive(archive_id=10)
- archive.cost = None # No pre-existing cost
- archive.print_name = "TestPrint"
- archive.printer_id = 1
- _active_sessions[1] = PrintSession(
- printer_id=1,
- print_name="TestPrint",
- started_at=datetime.now(timezone.utc),
- tray_remain_start={(0, 0): 80},
- tray_now_at_start=0,
- )
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
- progress=100,
- layer_num=50,
- tray_now=0,
- )
- # 20g at 25/kg = 0.50
- expected_cost = 0.50
- responses = []
- responses.append(("scalar_one_or_none", archive))
- responses.append(("scalar_one_or_none", None)) # queue item
- responses.append(("scalar_one_or_none", assignment))
- responses.append(("scalar_one_or_none", spool))
- # cost aggregation: select archive to update cost
- responses.append(("scalar_one_or_none", archive))
- db = AsyncMock()
- call_count = [0]
- async def mock_execute(*args, **kwargs):
- idx = call_count[0]
- call_count[0] += 1
- result = MagicMock()
- if idx < len(responses):
- method, value = responses[idx]
- result.scalar.return_value = value
- result.scalar_one_or_none.return_value = value
- else:
- result.scalar_one_or_none.return_value = None
- result.scalar.return_value = None
- return result
- db.execute = mock_execute
- filament_usage = [{"slot_id": 1, "used_g": 20.0, "type": "PLA", "color": "#FF0000"}]
- with (
- # The first run of this archive
- patch("backend.app.services.print_log.other_runs_of", AsyncMock(return_value=0)),
- patch("backend.app.core.config.settings") as mock_settings,
- patch("backend.app.api.routes.settings.get_setting", return_value="15.0"),
- patch("backend.app.utils.threemf_tools.extract_filament_usage_from_3mf", return_value=filament_usage),
- ):
- mock_settings.base_dir = MagicMock()
- mock_path = MagicMock()
- mock_path.exists.return_value = True
- mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
- results = await on_print_complete(
- printer_id=1,
- data={"status": "completed"},
- printer_manager=printer_manager,
- db=db,
- archive_id=10,
- )
- assert len(results) == 1
- assert results[0]["cost"] == expected_cost
- # Archive cost should have been updated
- assert archive.cost == expected_cost
- @pytest.mark.asyncio
- async def test_archive_cost_includes_untracked_filament_at_default_rate(self):
- """#1344: when only some AMS trays have inventory spools, the untracked
- filament weight is charged at the global default rate so the total
- archive cost still reflects the whole print."""
- spool = _make_spool(spool_id=1, label_weight=1000, cost_per_kg=10.0)
- assignment = _make_assignment(spool_id=1)
- archive = _make_archive(archive_id=10)
- archive.cost = None
- archive.print_name = "TestPrint"
- archive.printer_id = 1
- archive.filament_used_grams = 110.0 # whole-print weight from slicer
- _active_sessions[1] = PrintSession(
- printer_id=1,
- print_name="TestPrint",
- started_at=datetime.now(timezone.utc),
- tray_remain_start={(0, 0): 80},
- tray_now_at_start=0,
- )
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
- progress=100,
- layer_num=50,
- tray_now=0,
- )
- responses = [
- ("scalar_one_or_none", archive),
- ("scalar_one_or_none", None), # queue item
- ("scalar_one_or_none", assignment),
- ("scalar_one_or_none", spool),
- ("scalar_one_or_none", archive), # cost-update select
- ]
- db = AsyncMock()
- call_count = [0]
- async def mock_execute(*args, **kwargs):
- idx = call_count[0]
- call_count[0] += 1
- result = MagicMock()
- if idx < len(responses):
- _, value = responses[idx]
- result.scalar.return_value = value
- result.scalar_one_or_none.return_value = value
- else:
- result.scalar_one_or_none.return_value = None
- result.scalar.return_value = None
- return result
- db.execute = mock_execute
- # 3MF reports a single slot using 10g, but archive.filament_used_grams
- # says the whole print was 110g -- the other 100g came from spools that
- # aren't in inventory.
- filament_usage = [{"slot_id": 1, "used_g": 10.0, "type": "PLA", "color": "#FF0000"}]
- with (
- # The first run of this archive
- patch("backend.app.services.print_log.other_runs_of", AsyncMock(return_value=0)),
- patch("backend.app.core.config.settings") as mock_settings,
- patch("backend.app.api.routes.settings.get_setting", return_value="10.0"),
- patch("backend.app.utils.threemf_tools.extract_filament_usage_from_3mf", return_value=filament_usage),
- ):
- mock_settings.base_dir = MagicMock()
- mock_path = MagicMock()
- mock_path.exists.return_value = True
- mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
- results = await on_print_complete(
- printer_id=1,
- data={"status": "completed"},
- printer_manager=printer_manager,
- db=db,
- archive_id=10,
- )
- # Tracked slot: 10g * $10/kg = $0.10
- assert len(results) == 1
- assert results[0]["cost"] == 0.10
- # Untracked: 110g - 10g = 100g at $10/kg default = $1.00
- # Archive total: $0.10 + $1.00 = $1.10 (was $0.01 pre-fix because only
- # the tracked slot's tiny share was kept)
- assert archive.cost == 1.10
- @pytest.mark.asyncio
- async def test_archive_cost_fully_tracked_unchanged_by_topup(self):
- """When every gram is covered by inventory spools, the default-rate
- top-up adds nothing -- the archive cost is just the sum of tracked
- costs, same as before #1344."""
- spool = _make_spool(spool_id=1, label_weight=1000, cost_per_kg=25.0)
- assignment = _make_assignment(spool_id=1)
- archive = _make_archive(archive_id=10)
- archive.cost = None
- archive.print_name = "TestPrint"
- archive.printer_id = 1
- archive.filament_used_grams = 20.0 # exactly what the slot reports
- _active_sessions[1] = PrintSession(
- printer_id=1,
- print_name="TestPrint",
- started_at=datetime.now(timezone.utc),
- tray_remain_start={(0, 0): 80},
- tray_now_at_start=0,
- )
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
- progress=100,
- layer_num=50,
- tray_now=0,
- )
- responses = [
- ("scalar_one_or_none", archive),
- ("scalar_one_or_none", None),
- ("scalar_one_or_none", assignment),
- ("scalar_one_or_none", spool),
- ("scalar_one_or_none", archive),
- ]
- db = AsyncMock()
- call_count = [0]
- async def mock_execute(*args, **kwargs):
- idx = call_count[0]
- call_count[0] += 1
- result = MagicMock()
- if idx < len(responses):
- _, value = responses[idx]
- result.scalar.return_value = value
- result.scalar_one_or_none.return_value = value
- else:
- result.scalar_one_or_none.return_value = None
- result.scalar.return_value = None
- return result
- db.execute = mock_execute
- filament_usage = [{"slot_id": 1, "used_g": 20.0, "type": "PLA", "color": "#FF0000"}]
- with (
- # The first run of this archive
- patch("backend.app.services.print_log.other_runs_of", AsyncMock(return_value=0)),
- patch("backend.app.core.config.settings") as mock_settings,
- patch("backend.app.api.routes.settings.get_setting", return_value="15.0"),
- patch("backend.app.utils.threemf_tools.extract_filament_usage_from_3mf", return_value=filament_usage),
- ):
- mock_settings.base_dir = MagicMock()
- mock_path = MagicMock()
- mock_path.exists.return_value = True
- mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
- results = await on_print_complete(
- printer_id=1,
- data={"status": "completed"},
- printer_manager=printer_manager,
- db=db,
- archive_id=10,
- )
- # 20g at $25/kg = $0.50 -- no top-up because tracked >= archive grams
- assert len(results) == 1
- assert results[0]["cost"] == 0.50
- assert archive.cost == 0.50
- @pytest.mark.asyncio
- async def test_cost_with_archive_id(self):
- """Test cost aggregation using archive_id (3MF path)."""
- spool_new = _make_spool(spool_id=1, label_weight=1000, cost_per_kg=25.0)
- assignment_new = _make_assignment(spool_id=1)
- archive_new = _make_archive(archive_id=20)
- filament_usage_new = [{"slot_id": 1, "used_g": 20.0, "type": "PLA", "color": "#FF0000"}]
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
- progress=100,
- layer_num=50,
- tray_now=0,
- )
- db = _mock_db_sequential([archive_new, None, assignment_new, spool_new])
- with (
- patch("backend.app.core.config.settings") as mock_settings,
- patch("backend.app.api.routes.settings.get_setting", return_value="15.0"),
- patch("backend.app.utils.threemf_tools.extract_filament_usage_from_3mf", return_value=filament_usage_new),
- ):
- mock_settings.base_dir = MagicMock()
- mock_path = MagicMock()
- mock_path.exists.return_value = True
- mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
- results_new = await on_print_complete(
- printer_id=1,
- data={"status": "completed"},
- printer_manager=printer_manager,
- db=db,
- archive_id=20,
- )
- assert len(results_new) == 1
- assert results_new[0]["spool_id"] == 1
- assert results_new[0]["cost"] == 0.50 # 20g / 1000 * 25.0
- @pytest.mark.asyncio
- async def test_cost_with_print_name_ams_fallback(self):
- """Test cost aggregation using print_name (AMS fallback, legacy path)."""
- spool_old = _make_spool(spool_id=2, label_weight=1000, cost_per_kg=15.0)
- assignment_old = _make_assignment(spool_id=2, ams_id=0, tray_id=0)
- legacy_print_name = "LegacyPrint"
- _active_sessions[1] = PrintSession(
- printer_id=1,
- print_name=legacy_print_name,
- started_at=datetime.now(timezone.utc),
- tray_remain_start={(0, 0): 80},
- tray_now_at_start=0,
- )
- printer_manager = MagicMock()
- printer_manager.get_status.return_value = SimpleNamespace(
- raw_data={"ams": [{"id": 0, "tray": [{"id": 0, "remain": 70}]}]},
- progress=100,
- layer_num=50,
- tray_now=0,
- )
- # Pad 2 Nones for _find_3mf_by_filename DB queries (library + archive search),
- # then assignment and spool for the AMS fallback path
- db = _mock_db_sequential([None, None, assignment_old, spool_old])
- with (
- patch("backend.app.core.config.settings") as mock_settings,
- patch("backend.app.api.routes.settings.get_setting", return_value="15.0"),
- patch("backend.app.utils.threemf_tools.extract_filament_usage_from_3mf", return_value=None),
- ):
- mock_settings.base_dir = MagicMock()
- mock_path = MagicMock()
- mock_path.exists.return_value = True
- mock_settings.base_dir.__truediv__ = MagicMock(return_value=mock_path)
- results_old = await on_print_complete(
- printer_id=1,
- data={"status": "completed", "subtask_name": legacy_print_name, "filename": legacy_print_name},
- printer_manager=printer_manager,
- db=db,
- archive_id=None,
- )
- assert len(results_old) == 1
- assert results_old[0]["spool_id"] == 2
- assert results_old[0]["cost"] == 1.5 # 100g / 1000 * 15.0
|