test_print_storage_2780.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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_HISTORY,
  13. REASON_INTERNAL_STORAGE,
  14. REASON_NO_EXTERNAL_STORAGE,
  15. external_storage_present,
  16. last_print_storage_verdict,
  17. print_file_reachable_over_ftp,
  18. url_is_external_storage,
  19. )
  20. pytestmark = pytest.mark.unit
  21. class FakeState:
  22. """Stand-in for PrinterState with only the fields the helper reads."""
  23. def __init__(self, current_project_url=None, sdcard=False, sdcard_reported=False, last_project_url=None):
  24. self.current_project_url = current_project_url
  25. # Defaults to the per-print value: for every test that does not care
  26. # about the distinction, the two readings agree.
  27. self.last_project_url = current_project_url if last_project_url is None else last_project_url
  28. self.sdcard = sdcard
  29. self.sdcard_reported = sdcard_reported
  30. class TestUrlScheme:
  31. @pytest.mark.parametrize(
  32. "url",
  33. [
  34. "ftp://Benchy.gcode.3mf",
  35. # Real dispatches carry names with spaces and non-ASCII; the scheme
  36. # is all that is being read and none of that should disturb it.
  37. "ftp://Halterung Kühlschrank V2.gcode.3mf",
  38. "FTP://Benchy.gcode.3mf",
  39. ],
  40. )
  41. def test_ftp_means_external_storage(self, url):
  42. assert url_is_external_storage(url) is True
  43. @pytest.mark.parametrize(
  44. "url",
  45. [
  46. # The scheme every H2C and P2S dispatch in #2780's bundle carried,
  47. # 35 out of 35.
  48. "brtc://emmc/169356_204314.STEP.gcode.3mf",
  49. "brtc://emmc/Benchy.gcode.3mf",
  50. ],
  51. )
  52. def test_brtc_means_internal_storage(self, url):
  53. assert url_is_external_storage(url) is False
  54. def test_an_unknown_scheme_is_not_assumed_reachable(self):
  55. """Matching the reachable value, not the unreachable one.
  56. If Bambu ships a third scheme, the safe reading is "somewhere we can't
  57. see", not "fine" -- an unrecognised scheme that read as reachable would
  58. put the storm straight back.
  59. """
  60. assert url_is_external_storage("sftp://Benchy.3mf") is False
  61. @pytest.mark.parametrize("url", [None, "", "Benchy.gcode.3mf"])
  62. def test_no_usable_url_declines_to_answer(self, url):
  63. """None is a third answer and must not collapse into False."""
  64. assert url_is_external_storage(url) is None
  65. class TestFileScheme:
  66. """A print of a file that was already on the printer.
  67. ``file://`` is what the printer reports for a reprint from its own screen,
  68. from Handy, or after a slicer sends to storage and then prints. Measured on
  69. an H2D 2026-08-17: ``file:///media/usb0/foobar.gcode.3mf`` while that exact
  70. file was listable and downloadable over FTPS. Reading it as internal storage
  71. skipped the sweep and produced an archive with no 3MF, for a file sitting
  72. right there -- and it did so on every model, not just the H2 series.
  73. """
  74. @pytest.mark.parametrize(
  75. "url",
  76. [
  77. "file:///media/usb0/foobar.gcode.3mf",
  78. "file:///media/sdcard/Benchy.gcode.3mf",
  79. "file:///media/usb0/timelapse/video.mp4",
  80. ],
  81. )
  82. def test_an_external_mount_is_not_evidence_of_internal_storage(self, url):
  83. """None, not True: the path is good reason to look, and looking is what
  84. the caller's default already does."""
  85. assert url_is_external_storage(url) is None
  86. def test_the_model_cache_is_internal(self):
  87. """``/userdata/model/history/<name>`` is where the printer's own file
  88. listing puts cached models, and port 990 does not serve it."""
  89. assert url_is_external_storage("file:///userdata/model/history/Cube.gcode.3mf") is False
  90. def test_an_unrecognised_path_sweeps_rather_than_skips(self):
  91. """Skip only on positive evidence -- a path we do not know is not that."""
  92. assert url_is_external_storage("file:///somewhere/new/Cube.3mf") is None
  93. def test_the_sweep_runs_for_a_file_on_the_card(self):
  94. """The regression in one assertion."""
  95. state = FakeState(
  96. current_project_url="file:///media/usb0/foobar.gcode.3mf",
  97. sdcard=True,
  98. sdcard_reported=True,
  99. )
  100. assert print_file_reachable_over_ftp(state).reachable is True
  101. def test_an_empty_slot_still_wins(self):
  102. """With nothing in the slot the file cannot be on it, whatever the path
  103. says -- and the operator gets the reason they can act on."""
  104. state = FakeState(
  105. current_project_url="file:///media/usb0/foobar.gcode.3mf",
  106. sdcard=False,
  107. sdcard_reported=True,
  108. )
  109. verdict = print_file_reachable_over_ftp(state)
  110. assert verdict.reachable is False
  111. assert verdict.reason == REASON_NO_EXTERNAL_STORAGE
  112. def test_the_model_cache_still_skips(self):
  113. state = FakeState(
  114. current_project_url="file:///userdata/model/history/Cube.gcode.3mf",
  115. sdcard=True,
  116. sdcard_reported=True,
  117. )
  118. verdict = print_file_reachable_over_ftp(state)
  119. assert verdict.reachable is False
  120. # Its own reason, not the dispatch one: nothing was sent for this print,
  121. # so the advice attached to REASON_INTERNAL_STORAGE -- pick External in
  122. # the slicer's Send dialog -- describes a step that never happened
  123. # (#1820).
  124. assert verdict.reason == REASON_INTERNAL_HISTORY
  125. @pytest.mark.parametrize("url", [12345, [], {}, object()])
  126. def test_a_non_string_url_declines_too(self, url):
  127. """The value arrives straight off the wire, so it is whatever the
  128. sender put there. Truth-testing alone would let a non-string fall
  129. through to the scheme comparison and read as internal storage --
  130. which is a silent skip of a sweep that should have run.
  131. """
  132. assert url_is_external_storage(url) is None
  133. class TestSweepIsSkipped:
  134. def test_a_print_kept_on_internal_storage(self):
  135. verdict = print_file_reachable_over_ftp(FakeState(current_project_url="brtc://emmc/Benchy.gcode.3mf"))
  136. assert verdict.reachable is False
  137. assert verdict.reason == REASON_INTERNAL_STORAGE
  138. def test_a_printer_that_says_its_slot_is_empty(self):
  139. """#2780's H2C: `sdcard` False for three weeks, 800 clean FTPS
  140. connections, and a 550 on every single path it asked for."""
  141. verdict = print_file_reachable_over_ftp(FakeState(sdcard=False, sdcard_reported=True))
  142. assert verdict.reachable is False
  143. assert verdict.reason == REASON_NO_EXTERNAL_STORAGE
  144. class TestSweepStillRuns:
  145. """The regression guard. Every case here worked before the gate existed."""
  146. def test_a_print_on_external_storage(self):
  147. assert print_file_reachable_over_ftp(FakeState(current_project_url="ftp://Benchy.gcode.3mf")).reachable
  148. def test_a_printer_that_never_mentioned_its_card(self):
  149. """Silence is not evidence.
  150. `sdcard` defaults to False, so a printer whose firmware simply never
  151. publishes the field looks identical to an empty slot unless the
  152. "did it ever say so" flag is honoured. Reading the default as an
  153. answer would skip the sweep for every one of them.
  154. """
  155. assert print_file_reachable_over_ftp(FakeState(sdcard=False, sdcard_reported=False)).reachable
  156. def test_a_printer_with_a_card_and_no_dispatch_seen(self):
  157. """Some brokers refuse the request-topic subscription, so no URL ever
  158. arrives. That install must behave exactly as it did before."""
  159. assert print_file_reachable_over_ftp(FakeState(sdcard=True, sdcard_reported=True)).reachable
  160. def test_an_explicit_ftp_url_outranks_a_disagreeing_card_flag(self):
  161. """A false skip is a regression; a needless sweep is only slow.
  162. When the dispatcher says the file went to external storage, believe
  163. the specific claim over the general one.
  164. """
  165. state = FakeState(current_project_url="ftp://Benchy.gcode.3mf", sdcard=False, sdcard_reported=True)
  166. assert print_file_reachable_over_ftp(state).reachable
  167. def test_no_state_at_all(self):
  168. """Printer not connected, or status not yet populated."""
  169. assert print_file_reachable_over_ftp(None).reachable
  170. def test_a_state_missing_the_fields_entirely(self):
  171. """The helper is duck-typed, and a PrinterState from a pickled or
  172. partially-constructed source may predate these fields."""
  173. class Bare:
  174. pass
  175. assert print_file_reachable_over_ftp(Bare()).reachable
  176. class TestReasonIsAlwaysPresentWhenUnreachable:
  177. @pytest.mark.parametrize(
  178. "state",
  179. [
  180. FakeState(current_project_url="brtc://emmc/x.3mf"),
  181. FakeState(sdcard=False, sdcard_reported=True),
  182. FakeState(current_project_url="file:///userdata/model/history/x.3mf"),
  183. ],
  184. )
  185. def test_unreachable_carries_a_reason(self, state):
  186. """The reason crosses into the API and picks the banner text. An
  187. unreachable verdict without one would render the generic advice --
  188. which is the wrong advice, and the whole point of the change."""
  189. verdict = print_file_reachable_over_ftp(state)
  190. assert verdict.reachable is False
  191. assert verdict.reason
  192. def test_reachable_carries_no_reason(self):
  193. assert print_file_reachable_over_ftp(FakeState(sdcard=True, sdcard_reported=True)).reason is None
  194. class TestTheGateUsesThePerPrintUrlOnly:
  195. """A stale URL must never gate a sweep.
  196. ``current_project_url`` is cleared when a print ends; ``last_project_url``
  197. is sticky for reporting. The gate reads only the first, and that is
  198. load-bearing rather than tidiness: 18% of the print starts in #2780's
  199. support bundle (14 of 79) had no ``project_file`` on the request topic at
  200. all -- touchscreen reprints, restart recovery, anything Bambuddy did not
  201. see dispatched. If those inherited the previous job's destination, a
  202. printer that ran one Studio print to internal storage would skip the FTPS
  203. sweep for every subsequent screen-started print, losing archives that work
  204. today.
  205. The asymmetry is what makes it worth pinning: a stale ``ftp://`` costs
  206. only a pointless sweep, while a stale ``brtc://`` costs an archive.
  207. """
  208. def test_a_print_with_no_dispatch_of_its_own_still_sweeps(self):
  209. """The previous print went to internal storage; this one Bambuddy
  210. never saw dispatched. Unknown, so sweep."""
  211. state = FakeState(
  212. current_project_url=None,
  213. last_project_url="brtc://emmc/previous.gcode.3mf",
  214. sdcard=True,
  215. sdcard_reported=True,
  216. )
  217. assert print_file_reachable_over_ftp(state).reachable
  218. def test_the_sticky_reading_still_reports_it(self):
  219. """The diagnostic is normally run after the print that prompted it, so
  220. it needs the answer the gate has rightly forgotten."""
  221. state = FakeState(
  222. current_project_url=None,
  223. last_project_url="brtc://emmc/previous.gcode.3mf",
  224. sdcard=True,
  225. sdcard_reported=True,
  226. )
  227. verdict = last_print_storage_verdict(state)
  228. assert verdict.reachable is False
  229. assert verdict.reason == REASON_INTERNAL_STORAGE
  230. def test_an_empty_slot_is_reported_by_both(self):
  231. """Not URL-derived, so clearing the per-print value changes nothing."""
  232. state = FakeState(sdcard=False, sdcard_reported=True)
  233. assert print_file_reachable_over_ftp(state).reason == REASON_NO_EXTERNAL_STORAGE
  234. assert last_print_storage_verdict(state).reason == REASON_NO_EXTERNAL_STORAGE
  235. def test_the_sticky_reading_never_gates_a_sweep(self):
  236. """Guard against someone swapping the two back: if the gate ever reads
  237. the sticky field, the case above starts failing -- and so does this."""
  238. import inspect
  239. from backend.app.services import print_storage
  240. source = inspect.getsource(print_storage.print_file_reachable_over_ftp)
  241. assert "current_project_url" in source
  242. assert "last_project_url" not in source.split('"""')[-1]
  243. class TestTheTwoInternalReasonsAreToldApart:
  244. """Same verdict, different advice (#1820).
  245. Both URLs mean "port 990 cannot serve this", and until the report topic was
  246. read there was only ever one of them to see. A screen-started print names
  247. the other, and giving it the dispatch reason puts a banner in front of the
  248. operator telling them to pick External in a Send dialog they never opened.
  249. """
  250. def test_a_dispatch_that_chose_internal_storage(self):
  251. verdict = print_file_reachable_over_ftp(
  252. FakeState(current_project_url="brtc://emmc/Benchy.gcode.3mf", sdcard=True, sdcard_reported=True)
  253. )
  254. assert verdict.reason == REASON_INTERNAL_STORAGE
  255. @pytest.mark.parametrize(
  256. "url",
  257. [
  258. # Both forms measured on the H2S in #1820: the printer's own file
  259. # library, reached from its screen and from Handy.
  260. "file:///userdata/model/history/JOB_A.gcode.3mf",
  261. "file:///userdata/model/history/Halterung Kuehlschrank V2.gcode.3mf",
  262. ],
  263. )
  264. def test_a_print_of_a_file_that_was_already_there(self, url):
  265. verdict = print_file_reachable_over_ftp(FakeState(current_project_url=url, sdcard=True, sdcard_reported=True))
  266. assert verdict.reason == REASON_INTERNAL_HISTORY
  267. def test_both_still_earn_a_probe(self):
  268. """The reason split changes what the banner says, not what is tried.
  269. An H2S keeps a copy of screen-started jobs under /cache for a while, and
  270. that copy is what archived #1820's reporter's print 231."""
  271. for url in ("brtc://emmc/Cube.gcode.3mf", "file:///userdata/model/history/Cube.gcode.3mf"):
  272. verdict = print_file_reachable_over_ftp(
  273. FakeState(current_project_url=url, sdcard=True, sdcard_reported=True)
  274. )
  275. assert verdict.probe_filename == "Cube.gcode.3mf"
  276. def test_the_sticky_reading_splits_them_too(self):
  277. """The diagnostic reads the same helper, so a divergence here would
  278. surface as one wording in the banner and another in Settings."""
  279. state = FakeState(
  280. current_project_url=None,
  281. last_project_url="file:///userdata/model/history/Cube.gcode.3mf",
  282. sdcard=True,
  283. sdcard_reported=True,
  284. )
  285. assert last_print_storage_verdict(state).reason == REASON_INTERNAL_HISTORY
  286. class TestTimelapseUsesTheNarrowerRule:
  287. """The printer writes its timelapse to the card itself.
  288. Where the *sliced file* went says nothing about whether a video exists, so
  289. gating the timelapse scan on the URL would silently stop finding videos on
  290. every H2C and P2S that has a card in -- a new bug, introduced by the fix
  291. for this one.
  292. """
  293. def test_internal_storage_does_not_suppress_the_timelapse_scan(self):
  294. state = FakeState(current_project_url="brtc://emmc/x.3mf", sdcard=True, sdcard_reported=True)
  295. assert print_file_reachable_over_ftp(state).reachable is False
  296. assert external_storage_present(state) is True
  297. def test_an_empty_slot_does_suppress_it(self):
  298. assert external_storage_present(FakeState(sdcard=False, sdcard_reported=True)) is False
  299. def test_silence_does_not(self):
  300. assert external_storage_present(FakeState(sdcard=False, sdcard_reported=False)) is True
  301. def test_no_state_does_not(self):
  302. assert external_storage_present(None) is True