test_print_storage_2780.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. """Which prints are worth an FTPS sweep, and which are not (#2780).
  2. The gate this module guards is one-sided on purpose, and both sides matter:
  3. * Missing it costs ~110 doomed FTP connections per print and an archive card
  4. that is blank with no stated reason -- the reported bug.
  5. * Over-applying it costs archives that work today. A printer that never
  6. publishes ``sdcard`` and never had a ``project_file`` reach us must sweep
  7. exactly as before, or the fix is a regression for everyone else.
  8. So the tests below spend most of their weight on the second failure mode.
  9. """
  10. import pytest
  11. from backend.app.services.print_storage import (
  12. REASON_INTERNAL_STORAGE,
  13. REASON_NO_EXTERNAL_STORAGE,
  14. external_storage_present,
  15. last_print_storage_verdict,
  16. print_file_reachable_over_ftp,
  17. url_is_external_storage,
  18. )
  19. pytestmark = pytest.mark.unit
  20. class FakeState:
  21. """Stand-in for PrinterState with only the fields the helper reads."""
  22. def __init__(self, current_project_url=None, sdcard=False, sdcard_reported=False, last_project_url=None):
  23. self.current_project_url = current_project_url
  24. # Defaults to the per-print value: for every test that does not care
  25. # about the distinction, the two readings agree.
  26. self.last_project_url = current_project_url if last_project_url is None else last_project_url
  27. self.sdcard = sdcard
  28. self.sdcard_reported = sdcard_reported
  29. class TestUrlScheme:
  30. @pytest.mark.parametrize(
  31. "url",
  32. [
  33. "ftp://Benchy.gcode.3mf",
  34. # Real dispatches carry names with spaces and non-ASCII; the scheme
  35. # is all that is being read and none of that should disturb it.
  36. "ftp://Halterung Kühlschrank V2.gcode.3mf",
  37. "FTP://Benchy.gcode.3mf",
  38. ],
  39. )
  40. def test_ftp_means_external_storage(self, url):
  41. assert url_is_external_storage(url) is True
  42. @pytest.mark.parametrize(
  43. "url",
  44. [
  45. # The scheme every H2C and P2S dispatch in #2780's bundle carried,
  46. # 35 out of 35.
  47. "brtc://emmc/169356_204314.STEP.gcode.3mf",
  48. "brtc://emmc/Benchy.gcode.3mf",
  49. ],
  50. )
  51. def test_brtc_means_internal_storage(self, url):
  52. assert url_is_external_storage(url) is False
  53. def test_an_unknown_scheme_is_not_assumed_reachable(self):
  54. """Matching the reachable value, not the unreachable one.
  55. If Bambu ships a third scheme, the safe reading is "somewhere we can't
  56. see", not "fine" -- an unrecognised scheme that read as reachable would
  57. put the storm straight back.
  58. """
  59. assert url_is_external_storage("sftp://Benchy.3mf") is False
  60. @pytest.mark.parametrize("url", [None, "", "Benchy.gcode.3mf"])
  61. def test_no_usable_url_declines_to_answer(self, url):
  62. """None is a third answer and must not collapse into False."""
  63. assert url_is_external_storage(url) is None
  64. @pytest.mark.parametrize("url", [12345, [], {}, object()])
  65. def test_a_non_string_url_declines_too(self, url):
  66. """The value arrives straight off the wire, so it is whatever the
  67. sender put there. Truth-testing alone would let a non-string fall
  68. through to the scheme comparison and read as internal storage --
  69. which is a silent skip of a sweep that should have run.
  70. """
  71. assert url_is_external_storage(url) is None
  72. class TestSweepIsSkipped:
  73. def test_a_print_kept_on_internal_storage(self):
  74. verdict = print_file_reachable_over_ftp(FakeState(current_project_url="brtc://emmc/Benchy.gcode.3mf"))
  75. assert verdict.reachable is False
  76. assert verdict.reason == REASON_INTERNAL_STORAGE
  77. def test_a_printer_that_says_its_slot_is_empty(self):
  78. """#2780's H2C: `sdcard` False for three weeks, 800 clean FTPS
  79. connections, and a 550 on every single path it asked for."""
  80. verdict = print_file_reachable_over_ftp(FakeState(sdcard=False, sdcard_reported=True))
  81. assert verdict.reachable is False
  82. assert verdict.reason == REASON_NO_EXTERNAL_STORAGE
  83. class TestSweepStillRuns:
  84. """The regression guard. Every case here worked before the gate existed."""
  85. def test_a_print_on_external_storage(self):
  86. assert print_file_reachable_over_ftp(FakeState(current_project_url="ftp://Benchy.gcode.3mf")).reachable
  87. def test_a_printer_that_never_mentioned_its_card(self):
  88. """Silence is not evidence.
  89. `sdcard` defaults to False, so a printer whose firmware simply never
  90. publishes the field looks identical to an empty slot unless the
  91. "did it ever say so" flag is honoured. Reading the default as an
  92. answer would skip the sweep for every one of them.
  93. """
  94. assert print_file_reachable_over_ftp(FakeState(sdcard=False, sdcard_reported=False)).reachable
  95. def test_a_printer_with_a_card_and_no_dispatch_seen(self):
  96. """Some brokers refuse the request-topic subscription, so no URL ever
  97. arrives. That install must behave exactly as it did before."""
  98. assert print_file_reachable_over_ftp(FakeState(sdcard=True, sdcard_reported=True)).reachable
  99. def test_an_explicit_ftp_url_outranks_a_disagreeing_card_flag(self):
  100. """A false skip is a regression; a needless sweep is only slow.
  101. When the dispatcher says the file went to external storage, believe
  102. the specific claim over the general one.
  103. """
  104. state = FakeState(current_project_url="ftp://Benchy.gcode.3mf", sdcard=False, sdcard_reported=True)
  105. assert print_file_reachable_over_ftp(state).reachable
  106. def test_no_state_at_all(self):
  107. """Printer not connected, or status not yet populated."""
  108. assert print_file_reachable_over_ftp(None).reachable
  109. def test_a_state_missing_the_fields_entirely(self):
  110. """The helper is duck-typed, and a PrinterState from a pickled or
  111. partially-constructed source may predate these fields."""
  112. class Bare:
  113. pass
  114. assert print_file_reachable_over_ftp(Bare()).reachable
  115. class TestReasonIsAlwaysPresentWhenUnreachable:
  116. @pytest.mark.parametrize(
  117. "state",
  118. [
  119. FakeState(current_project_url="brtc://emmc/x.3mf"),
  120. FakeState(sdcard=False, sdcard_reported=True),
  121. ],
  122. )
  123. def test_unreachable_carries_a_reason(self, state):
  124. """The reason crosses into the API and picks the banner text. An
  125. unreachable verdict without one would render the generic advice --
  126. which is the wrong advice, and the whole point of the change."""
  127. verdict = print_file_reachable_over_ftp(state)
  128. assert verdict.reachable is False
  129. assert verdict.reason
  130. def test_reachable_carries_no_reason(self):
  131. assert print_file_reachable_over_ftp(FakeState(sdcard=True, sdcard_reported=True)).reason is None
  132. class TestTheGateUsesThePerPrintUrlOnly:
  133. """A stale URL must never gate a sweep.
  134. ``current_project_url`` is cleared when a print ends; ``last_project_url``
  135. is sticky for reporting. The gate reads only the first, and that is
  136. load-bearing rather than tidiness: 18% of the print starts in #2780's
  137. support bundle (14 of 79) had no ``project_file`` on the request topic at
  138. all -- touchscreen reprints, restart recovery, anything Bambuddy did not
  139. see dispatched. If those inherited the previous job's destination, a
  140. printer that ran one Studio print to internal storage would skip the FTPS
  141. sweep for every subsequent screen-started print, losing archives that work
  142. today.
  143. The asymmetry is what makes it worth pinning: a stale ``ftp://`` costs
  144. only a pointless sweep, while a stale ``brtc://`` costs an archive.
  145. """
  146. def test_a_print_with_no_dispatch_of_its_own_still_sweeps(self):
  147. """The previous print went to internal storage; this one Bambuddy
  148. never saw dispatched. Unknown, so sweep."""
  149. state = FakeState(
  150. current_project_url=None,
  151. last_project_url="brtc://emmc/previous.gcode.3mf",
  152. sdcard=True,
  153. sdcard_reported=True,
  154. )
  155. assert print_file_reachable_over_ftp(state).reachable
  156. def test_the_sticky_reading_still_reports_it(self):
  157. """The diagnostic is normally run after the print that prompted it, so
  158. it needs the answer the gate has rightly forgotten."""
  159. state = FakeState(
  160. current_project_url=None,
  161. last_project_url="brtc://emmc/previous.gcode.3mf",
  162. sdcard=True,
  163. sdcard_reported=True,
  164. )
  165. verdict = last_print_storage_verdict(state)
  166. assert verdict.reachable is False
  167. assert verdict.reason == REASON_INTERNAL_STORAGE
  168. def test_an_empty_slot_is_reported_by_both(self):
  169. """Not URL-derived, so clearing the per-print value changes nothing."""
  170. state = FakeState(sdcard=False, sdcard_reported=True)
  171. assert print_file_reachable_over_ftp(state).reason == REASON_NO_EXTERNAL_STORAGE
  172. assert last_print_storage_verdict(state).reason == REASON_NO_EXTERNAL_STORAGE
  173. def test_the_sticky_reading_never_gates_a_sweep(self):
  174. """Guard against someone swapping the two back: if the gate ever reads
  175. the sticky field, the case above starts failing -- and so does this."""
  176. import inspect
  177. from backend.app.services import print_storage
  178. source = inspect.getsource(print_storage.print_file_reachable_over_ftp)
  179. assert "current_project_url" in source
  180. assert "last_project_url" not in source.split('"""')[-1]
  181. class TestTimelapseUsesTheNarrowerRule:
  182. """The printer writes its timelapse to the card itself.
  183. Where the *sliced file* went says nothing about whether a video exists, so
  184. gating the timelapse scan on the URL would silently stop finding videos on
  185. every H2C and P2S that has a card in -- a new bug, introduced by the fix
  186. for this one.
  187. """
  188. def test_internal_storage_does_not_suppress_the_timelapse_scan(self):
  189. state = FakeState(current_project_url="brtc://emmc/x.3mf", sdcard=True, sdcard_reported=True)
  190. assert print_file_reachable_over_ftp(state).reachable is False
  191. assert external_storage_present(state) is True
  192. def test_an_empty_slot_does_suppress_it(self):
  193. assert external_storage_present(FakeState(sdcard=False, sdcard_reported=True)) is False
  194. def test_silence_does_not(self):
  195. assert external_storage_present(FakeState(sdcard=False, sdcard_reported=False)) is True
  196. def test_no_state_does_not(self):
  197. assert external_storage_present(None) is True