test_run_filament_helper.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """Unit tests for the per-run filament helper (#1378).
  2. The helper computes what value to write into PrintLogEntry.filament_used_grams
  3. for a given print event — partial-aware so failed / cancelled / stopped prints
  4. don't inflate stats with the full slicer estimate.
  5. """
  6. from backend.app.main import _compute_run_filament_grams
  7. class TestComputeRunFilamentGrams:
  8. def test_completed_returns_archive_estimate(self):
  9. # Completed print: the slicer estimate is approximately what was used.
  10. assert _compute_run_filament_grams("completed", 100.0, 100, []) == 100.0
  11. def test_completed_returns_estimate_even_when_tracked_differs(self):
  12. # When a print completes, the estimate is the canonical "this print used X"
  13. # value — the tracked spool delta might be lower (some slots untracked)
  14. # but the print is done, so the full estimate is the right answer.
  15. assert _compute_run_filament_grams("completed", 100.0, 100, [{"weight_used": 10}]) == 100.0
  16. def test_failed_uses_tracked_spool_delta(self):
  17. # Failed reprint at 10g actual: inventory tracked the spool delta.
  18. # The estimate was 100g; we want 10g recorded for stats.
  19. assert _compute_run_filament_grams("failed", 100.0, 10, [{"weight_used": 10.0}]) == 10.0
  20. def test_cancelled_uses_tracked_spool_delta(self):
  21. # Same logic for cancelled.
  22. assert _compute_run_filament_grams("cancelled", 100.0, 12, [{"weight_used": 8.5}]) == 8.5
  23. def test_stopped_uses_tracked_spool_delta(self):
  24. assert _compute_run_filament_grams("stopped", 100.0, 15, [{"weight_used": 12.0}]) == 12.0
  25. def test_failed_with_no_tracked_falls_back_to_progress_scale(self):
  26. # No inventory tracking: scale estimate by progress% (10% of 100g = 10g).
  27. assert _compute_run_filament_grams("failed", 100.0, 10, []) == 10.0
  28. def test_failed_with_no_tracked_and_no_progress_returns_none(self):
  29. # Nothing to infer from — return None rather than guess the estimate.
  30. assert _compute_run_filament_grams("failed", 100.0, 0, []) is None
  31. def test_failed_with_partial_progress_rounds_correctly(self):
  32. # 100g × 33% = 33.0g (rounded to 1 decimal)
  33. assert _compute_run_filament_grams("failed", 100.0, 33, []) == 33.0
  34. def test_failed_with_no_estimate_returns_none(self):
  35. # No estimate, no tracked usage → can't compute anything.
  36. assert _compute_run_filament_grams("failed", None, 50, []) is None
  37. def test_failed_with_no_estimate_but_tracked_uses_tracked(self):
  38. # Tracked spool delta is authoritative even without an estimate.
  39. assert _compute_run_filament_grams("failed", None, 50, [{"weight_used": 5.0}]) == 5.0
  40. def test_tracked_overrides_progress_scale_when_both_available(self):
  41. # If inventory says 8g but progress says 15g, trust inventory (it's measured).
  42. assert _compute_run_filament_grams("failed", 100.0, 15, [{"weight_used": 8.0}]) == 8.0
  43. def test_progress_above_100_clamps_to_full_estimate(self):
  44. # Defensive: progress overshoot doesn't multiply past the estimate.
  45. assert _compute_run_filament_grams("failed", 100.0, 150, []) == 100.0
  46. def test_multiple_tracked_slots_summed(self):
  47. # Multi-filament print, two slots tracked.
  48. usage = [{"weight_used": 5.0}, {"weight_used": 3.5}, {"weight_used": 1.0}]
  49. assert _compute_run_filament_grams("failed", 100.0, 20, usage) == 9.5
  50. def test_completed_with_none_estimate_returns_none(self):
  51. # Archive somehow has no estimate (rare; archive_print parsed nothing).
  52. assert _compute_run_filament_grams("completed", None, 100, []) is None