test_completion_subtask_match_2829.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """Matching a completion event to the queue row it belongs to (#2829).
  2. ``on_print_complete`` finds its row by printer and ``status='printing'`` alone,
  3. so #b5a34b7ba added a check that the completion's subtask name agrees with the
  4. file the row was dispatched with -- otherwise the printer's own calibration
  5. runs close whoever's job happens to be printing.
  6. The check compared the two names with plain equality, and the printer does not
  7. echo the name back verbatim. Three days later two users had queues that would
  8. not advance: the row stayed ``printing``, ``check_queue`` counts every such row
  9. as a busy printer, and nothing anywhere ever closes it. Cancelling by hand was
  10. the only way out.
  11. The strings below are the real ones from the maintainer's own H2D, queue item
  12. 649, and from a support bundle showing the truncation case.
  13. """
  14. import pytest
  15. from backend.app.main import _normalise_subtask_name, _subtask_name_from_filename, _subtask_names_match
  16. pytestmark = pytest.mark.unit
  17. class TestTheReportedCase:
  18. def test_spaces_come_back_as_underscores(self):
  19. """Queue item 649, verbatim from the warning it logged twice."""
  20. dispatched = "H2D_Carbon_Filter_(V2)_Body & Solid Lid"
  21. reported = "H2D_Carbon_Filter_(V2)_Body_&_Solid_Lid"
  22. assert _subtask_names_match(dispatched, reported)
  23. def test_from_the_archive_filename_it_was_dispatched_with(self):
  24. """End to end from the stored filename, which is where the check
  25. actually gets its side of the comparison."""
  26. expected = _subtask_name_from_filename("H2D_Carbon_Filter_(V2)_Body & Solid Lid.gcode.3mf")
  27. assert _subtask_names_match(expected, "H2D_Carbon_Filter_(V2)_Body_&_Solid_Lid")
  28. class TestTruncation:
  29. """The printer cuts long names and marks the cut with '...'.
  30. Observed at ~100 characters, but not a fixed count -- a name with multibyte
  31. characters came back at 98 -- so the marker is what is matched, not a
  32. length. Without this every print with a long name strands its row the same
  33. way the space substitution did.
  34. """
  35. def test_a_truncated_echo_matches_the_full_name(self):
  36. full = (
  37. "169356_204314.STEP + 169356_204314.STEP + 169356_204314.STEP + "
  38. "169356_204314.STEP + 169356_204314.STEP + 169356_204314.STEP"
  39. )
  40. truncated = (
  41. "169356_204314.STEP + 169356_204314.STEP + 169356_204314.STEP + 169356_204314.STEP + 169356_204314..."
  42. )
  43. assert _subtask_names_match(full, truncated)
  44. def test_a_truncated_name_on_the_archive_side_matches_too(self):
  45. """An archive whose filename was recorded from an earlier truncated
  46. echo carries the marker itself, so the cut can be on either side."""
  47. stored = "EXXXX-A001-Barriere Mundstück.STEP + EXXXX-A001-Barriere M..."
  48. reported = "EXXXX-A001-Barriere_Mundstück.STEP_+_EXXXX-A001-Barriere_Mundstück.STEP"
  49. assert _subtask_names_match(stored, reported)
  50. def test_truncation_does_not_match_a_different_print(self):
  51. """The prefix still has to agree -- '...' is not a wildcard."""
  52. assert not _subtask_names_match("Benchy_Calibration_Cube_Large", "Something_Else_Entirely...")
  53. class TestItStillRefusesADifferentPrint:
  54. """The check has to keep doing its job, or #b5a34b7ba's bug comes back:
  55. a completion for another print closing a job that is still running.
  56. """
  57. def test_the_printers_own_calibration_run(self):
  58. """The second rejection on queue item 649, and a correct one."""
  59. assert not _subtask_names_match("H2D_Carbon_Filter_(V2)_Body & Solid Lid", "auto_pa_line_calib_mode")
  60. def test_an_unrelated_print(self):
  61. assert not _subtask_names_match("Benchy", "Calibration Cube")
  62. def test_a_name_that_merely_starts_the_same(self):
  63. assert not _subtask_names_match("Bracket_v1", "Bracket_v2")
  64. class TestNormalisation:
  65. def test_case_is_ignored(self):
  66. assert _subtask_names_match("BENCHY BOAT", "benchy_boat")
  67. def test_surrounding_whitespace_is_ignored(self):
  68. assert _subtask_names_match(" Benchy ", "Benchy")
  69. @pytest.mark.parametrize(
  70. "raw,expected",
  71. [
  72. ("A B", "a_b"),
  73. ("A_B", "a_b"),
  74. (" A B ", "a__b"),
  75. ("Mundstück", "mundstück"),
  76. ],
  77. )
  78. def test_canonical_form(self, raw, expected):
  79. assert _normalise_subtask_name(raw) == expected
  80. def test_spaces_and_underscores_are_the_same_rule_the_3mf_lookup_uses(self):
  81. """The 3MF lookup has always built space-to-underscore variants of its
  82. candidates. The completion check growing its own comparison instead of
  83. reading the same rule is how the two came to disagree."""
  84. assert _normalise_subtask_name("My Model") == _normalise_subtask_name("My_Model")
  85. class TestFilenameDerivation:
  86. @pytest.mark.parametrize(
  87. "filename,expected",
  88. [
  89. ("Benchy.gcode.3mf", "Benchy"),
  90. ("Benchy.3mf", "Benchy"),
  91. ("My.Model.3mf", "My.Model"),
  92. ("/cache/Nested Path/Benchy.gcode.3mf", "Benchy"),
  93. ],
  94. )
  95. def test_extensions_come_off_and_nothing_else_does(self, filename, expected):
  96. assert _subtask_name_from_filename(filename) == expected