test_print_dispatch_context.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Tests for the injected-End-G-code flag the finish photo depends on (#2547).
  2. The flag decides whether the finish photo comes from the camera (the print is
  3. still on the plate) or from the in-print frame bank (a SwapMod snippet ejected
  4. it — #1867). Getting it wrong in either direction ships the wrong photo, so the
  5. two-step pending/adopt handoff exists to guarantee the flag can never outlive
  6. the print it was recorded for.
  7. """
  8. import pytest
  9. from backend.app.services import print_dispatch_context
  10. @pytest.fixture(autouse=True)
  11. def _clean():
  12. for printer_id in (1, 2):
  13. print_dispatch_context.clear(printer_id)
  14. yield
  15. for printer_id in (1, 2):
  16. print_dispatch_context.clear(printer_id)
  17. def test_unknown_printer_reports_no_injection():
  18. assert print_dispatch_context.end_gcode_injected(1) is False
  19. def test_pending_flag_only_counts_once_the_print_starts():
  20. """Dispatch can fail between upload and start. Until the printer confirms a
  21. print running, the flag must not affect anything."""
  22. print_dispatch_context.mark_pending(1)
  23. assert print_dispatch_context.end_gcode_injected(1) is False
  24. assert print_dispatch_context.adopt(1) is True
  25. assert print_dispatch_context.end_gcode_injected(1) is True
  26. def test_adopting_consumes_the_pending_flag():
  27. """A second print must not inherit the first print's snippet."""
  28. print_dispatch_context.mark_pending(1)
  29. print_dispatch_context.adopt(1)
  30. assert print_dispatch_context.adopt(1) is False
  31. assert print_dispatch_context.end_gcode_injected(1) is False
  32. def test_a_print_we_did_not_dispatch_clears_the_previous_flag():
  33. """The failure this two-step design exists to prevent: a print started from
  34. the slicer or SD card right after a SwapMod job would otherwise inherit its
  35. flag and get a mid-print banked frame instead of its own finish photo."""
  36. print_dispatch_context.mark_pending(1)
  37. print_dispatch_context.adopt(1)
  38. assert print_dispatch_context.end_gcode_injected(1) is True
  39. # Next print start, with nothing pending — i.e. Bambuddy didn't send it.
  40. assert print_dispatch_context.adopt(1) is False
  41. assert print_dispatch_context.end_gcode_injected(1) is False
  42. def test_printers_do_not_share_flags():
  43. print_dispatch_context.mark_pending(1)
  44. print_dispatch_context.adopt(1)
  45. assert print_dispatch_context.end_gcode_injected(1) is True
  46. assert print_dispatch_context.end_gcode_injected(2) is False
  47. def test_clear_forgets_pending_and_active():
  48. print_dispatch_context.mark_pending(1)
  49. print_dispatch_context.adopt(1)
  50. print_dispatch_context.mark_pending(1)
  51. print_dispatch_context.clear(1)
  52. assert print_dispatch_context.end_gcode_injected(1) is False
  53. assert print_dispatch_context.adopt(1) is False