test_print_cost_estimate.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from types import SimpleNamespace
  2. from unittest.mock import AsyncMock
  3. import pytest
  4. from backend.app.services import print_cost_estimate
  5. @pytest.fixture
  6. def library_file(tmp_path):
  7. path = tmp_path / "queued.gcode.3mf"
  8. path.write_bytes(b"stub")
  9. return SimpleNamespace(file_path=str(path), file_metadata={})
  10. @pytest.mark.asyncio
  11. async def test_library_estimate_uses_server_default_cost(monkeypatch, library_file):
  12. monkeypatch.setattr(
  13. print_cost_estimate.threemf_tools,
  14. "extract_plate_metadata_from_3mf",
  15. lambda *_args: SimpleNamespace(
  16. filament_usage=[
  17. {"slot_id": 1, "used_g": 100.0},
  18. {"slot_id": 2, "used_g": 50.0},
  19. ]
  20. ),
  21. )
  22. monkeypatch.setattr(print_cost_estimate, "_default_cost_per_kg", AsyncMock(return_value=20.0))
  23. cost = await print_cost_estimate.estimate_queue_source_cost(
  24. SimpleNamespace(),
  25. library_file=library_file,
  26. plate_id=1,
  27. )
  28. assert cost == 3.0
  29. @pytest.mark.asyncio
  30. async def test_library_estimate_uses_server_spool_assignment_costs(monkeypatch, library_file):
  31. monkeypatch.setattr(
  32. print_cost_estimate.threemf_tools,
  33. "extract_plate_metadata_from_3mf",
  34. lambda *_args: SimpleNamespace(
  35. filament_usage=[
  36. {"slot_id": 1, "used_g": 100.0},
  37. {"slot_id": 2, "used_g": 50.0},
  38. ]
  39. ),
  40. )
  41. monkeypatch.setattr(print_cost_estimate, "_default_cost_per_kg", AsyncMock(return_value=20.0))
  42. assignments = [
  43. SimpleNamespace(ams_id=0, tray_id=0, spool=SimpleNamespace(cost_per_kg=10.0)),
  44. SimpleNamespace(ams_id=0, tray_id=1, spool=SimpleNamespace(cost_per_kg=30.0)),
  45. ]
  46. result = SimpleNamespace(scalars=lambda: SimpleNamespace(all=lambda: assignments))
  47. db = SimpleNamespace(execute=AsyncMock(return_value=result))
  48. cost = await print_cost_estimate.estimate_queue_source_cost(
  49. db,
  50. library_file=library_file,
  51. plate_id=1,
  52. ams_mapping=[0, 1],
  53. printer_id=7,
  54. )
  55. assert cost == 2.5
  56. @pytest.mark.asyncio
  57. async def test_missing_server_metadata_does_not_fall_back_to_client_hint(monkeypatch, library_file):
  58. monkeypatch.setattr(
  59. print_cost_estimate.threemf_tools,
  60. "extract_plate_metadata_from_3mf",
  61. lambda *_args: SimpleNamespace(filament_usage=[]),
  62. )
  63. cost = await print_cost_estimate.estimate_queue_source_cost(
  64. SimpleNamespace(),
  65. library_file=library_file,
  66. plate_id=1,
  67. )
  68. assert cost is None