test_completion_guard_wiring_2829.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """The completion guard itself, not just the name comparison (#2829).
  2. There is a unit test for ``_subtask_names_match``. It is not enough on its own:
  3. reverting ``_completion_belongs_to_queue_item`` to the strict equality that
  4. caused the bug leaves every one of those tests green, because they never touch
  5. the guard. So these drive the guard, with a real archive row behind a real
  6. queue row, on the exact strings that stranded the maintainer's H2D.
  7. """
  8. import pytest
  9. pytestmark = [pytest.mark.integration, pytest.mark.asyncio]
  10. async def _archive(db_session, printer, filename):
  11. from backend.app.models.archive import PrintArchive
  12. archive = PrintArchive(
  13. printer_id=printer.id,
  14. filename=filename,
  15. file_path=f"archives/{filename}",
  16. file_size=1024,
  17. status="printing",
  18. )
  19. db_session.add(archive)
  20. await db_session.commit()
  21. await db_session.refresh(archive)
  22. return archive
  23. async def _item(db_session, printer, archive):
  24. from backend.app.models.print_queue import PrintQueueItem
  25. item = PrintQueueItem(printer_id=printer.id, status="printing", archive_id=archive.id)
  26. db_session.add(item)
  27. await db_session.commit()
  28. await db_session.refresh(item)
  29. return item
  30. async def _belongs(db_session, item, subtask_name):
  31. from backend.app.main import _completion_belongs_to_queue_item
  32. return await _completion_belongs_to_queue_item(db_session, item, {"subtask_name": subtask_name})
  33. class TestTheReportedStranding:
  34. async def test_the_completion_for_its_own_print_is_accepted(self, db_session, printer_factory):
  35. """Queue item 649 on the maintainer's H2D, verbatim. The printer echoes
  36. the name back with underscores where the file has spaces; the guard
  37. read that as a different print and left the row printing forever."""
  38. printer = await printer_factory()
  39. archive = await _archive(db_session, printer, "H2D_Carbon_Filter_(V2)_Body & Solid Lid.gcode.3mf")
  40. item = await _item(db_session, printer, archive)
  41. assert await _belongs(db_session, item, "H2D_Carbon_Filter_(V2)_Body_&_Solid_Lid")
  42. async def test_a_truncated_echo_is_accepted(self, db_session, printer_factory):
  43. printer = await printer_factory()
  44. name = "169356_204314.STEP + 169356_204314.STEP + 169356_204314.STEP + 169356_204314.STEP + 169356_204314"
  45. archive = await _archive(db_session, printer, f"{name}.gcode.3mf")
  46. item = await _item(db_session, printer, archive)
  47. assert await _belongs(db_session, item, f"{name[:70]}...")
  48. class TestItStillRefuses:
  49. """The guard's reason for existing: a completion for something else must
  50. not close a job that is still running."""
  51. async def test_the_printers_own_calibration_run(self, db_session, printer_factory):
  52. printer = await printer_factory()
  53. archive = await _archive(db_session, printer, "H2D_Carbon_Filter_(V2)_Body & Solid Lid.gcode.3mf")
  54. item = await _item(db_session, printer, archive)
  55. assert not await _belongs(db_session, item, "auto_pa_line_calib_mode")
  56. async def test_an_unrelated_print(self, db_session, printer_factory):
  57. printer = await printer_factory()
  58. archive = await _archive(db_session, printer, "Benchy.gcode.3mf")
  59. item = await _item(db_session, printer, archive)
  60. assert not await _belongs(db_session, item, "Calibration Cube")
  61. class TestUnverifiableIsNotWrong:
  62. """Refusing what cannot be checked would strand the queue, which is the
  63. worse of the two failures and the one this issue is about."""
  64. async def test_no_subtask_name_in_the_event(self, db_session, printer_factory):
  65. printer = await printer_factory()
  66. archive = await _archive(db_session, printer, "Benchy.gcode.3mf")
  67. item = await _item(db_session, printer, archive)
  68. assert await _belongs(db_session, item, "")
  69. async def test_a_row_with_no_archive(self, db_session, printer_factory):
  70. from backend.app.models.print_queue import PrintQueueItem
  71. printer = await printer_factory()
  72. item = PrintQueueItem(printer_id=printer.id, status="printing")
  73. db_session.add(item)
  74. await db_session.commit()
  75. await db_session.refresh(item)
  76. assert await _belongs(db_session, item, "Anything At All")