test_bambu_ftp.py 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. """Comprehensive FTP test suite for BambuFTPClient.
  2. Tests against a real mock implicit FTPS server, covering:
  3. - Connection (auth, SSL modes, timeout, caching)
  4. - File listing
  5. - Download (bytes, to_file, 0-byte regression)
  6. - Upload (chunked transfer, progress, error codes)
  7. - Delete
  8. - File size
  9. - Storage info (AVBL, directory scan, diagnose_storage)
  10. - Model-specific behavior (X1C prot_p, A1 prot_c fallback)
  11. - Async wrappers
  12. - Failure injection scenarios (regressions for 0.1.8 bugs)
  13. """
  14. import time
  15. from pathlib import Path
  16. import pytest
  17. from backend.app.services.bambu_ftp import (
  18. BambuFTPClient,
  19. FileNotOnPrinterError,
  20. cache_3mf_download,
  21. clear_3mf_cache,
  22. delete_file_async,
  23. download_file_async,
  24. download_file_try_paths_async,
  25. get_cached_3mf,
  26. list_files_async,
  27. normalize_3mf_name,
  28. upload_file_async,
  29. with_ftp_retry,
  30. )
  31. # Brief delay to allow pyftpdlib to flush uploaded files to disk.
  32. # Needed because upload_file() skips voidresp() for all models,
  33. # so the server may still be processing the data channel close event.
  34. _UPLOAD_FLUSH_DELAY = 0.3
  35. # ---------------------------------------------------------------------------
  36. # TestConnection
  37. # ---------------------------------------------------------------------------
  38. class TestConnection:
  39. """Tests for FTP connect/disconnect behavior."""
  40. def test_connect_success(self, ftp_client_factory):
  41. """Successful implicit FTPS connection and login."""
  42. client = ftp_client_factory()
  43. assert client.connect() is True
  44. client.disconnect()
  45. def test_connect_wrong_access_code(self, ftp_client_factory):
  46. """Wrong access code returns False."""
  47. client = ftp_client_factory(access_code="wrongcode")
  48. assert client.connect() is False
  49. def test_connect_unreachable_host(self, ftp_server):
  50. """Unreachable host returns False."""
  51. client = BambuFTPClient(
  52. ip_address="192.0.2.1", # TEST-NET, guaranteed unreachable
  53. access_code="12345678",
  54. timeout=1.0,
  55. printer_model="X1C",
  56. )
  57. client.FTP_PORT = ftp_server.port
  58. assert client.connect() is False
  59. def test_connect_timeout(self, ftp_server):
  60. """Very short timeout triggers timeout error."""
  61. client = BambuFTPClient(
  62. ip_address="192.0.2.1",
  63. access_code="12345678",
  64. timeout=0.001, # Extremely short
  65. printer_model="X1C",
  66. )
  67. client.FTP_PORT = ftp_server.port
  68. assert client.connect() is False
  69. def test_disconnect_clean(self, ftp_client_factory):
  70. """Clean disconnect after successful connect."""
  71. client = ftp_client_factory()
  72. client.connect()
  73. client.disconnect()
  74. assert client._ftp is None
  75. def test_disconnect_without_connect(self, ftp_client_factory):
  76. """Disconnect without connect does not raise."""
  77. client = ftp_client_factory()
  78. client.disconnect() # Should not raise
  79. assert client._ftp is None
  80. def test_x1c_uses_prot_p(self, ftp_client_factory):
  81. """X1C model connects with prot_p (protected data channel)."""
  82. client = ftp_client_factory(printer_model="X1C")
  83. assert client.connect() is True
  84. assert client._should_use_prot_c() is False
  85. client.disconnect()
  86. def test_a1_defaults_prot_p(self, ftp_client_factory):
  87. """A1 model defaults to prot_p when no cache exists."""
  88. client = ftp_client_factory(printer_model="A1")
  89. assert client._should_use_prot_c() is False
  90. assert client.connect() is True
  91. client.disconnect()
  92. def test_a1_force_prot_c(self, ftp_client_factory):
  93. """A1 model with force_prot_c uses clear data channel."""
  94. client = ftp_client_factory(printer_model="A1", force_prot_c=True)
  95. assert client._should_use_prot_c() is True
  96. assert client.connect() is True
  97. client.disconnect()
  98. def test_cached_mode_respected(self, ftp_client_factory):
  99. """Cached mode is used on subsequent connections."""
  100. BambuFTPClient.cache_mode("127.0.0.1", "prot_c")
  101. client = ftp_client_factory(printer_model="A1")
  102. assert client._should_use_prot_c() is True
  103. assert client.connect() is True
  104. client.disconnect()
  105. # ---------------------------------------------------------------------------
  106. # TestDisconnectServerGone — isolated class because server.stop() calls
  107. # close_all() which nukes all asyncore sockets globally.
  108. # ---------------------------------------------------------------------------
  109. class TestDisconnectServerGone:
  110. """Test disconnect behavior when the server has stopped."""
  111. def test_disconnect_after_server_gone(self, ftp_certs, tmp_path):
  112. """Disconnect after server has stopped does not raise.
  113. disconnect() catches OSError, ftplib.Error, and EOFError so that
  114. best-effort cleanup never propagates exceptions to the caller.
  115. """
  116. from backend.tests.unit.services.mock_ftp_server import (
  117. MockBambuFTPServer,
  118. )
  119. from .conftest import _find_free_port
  120. cert_path, key_path = ftp_certs
  121. port = _find_free_port()
  122. server = MockBambuFTPServer("127.0.0.1", port, str(tmp_path), cert_path, key_path)
  123. server.start()
  124. client = BambuFTPClient("127.0.0.1", "12345678", timeout=5.0)
  125. client.FTP_PORT = port
  126. client.connect()
  127. server.stop()
  128. # Should not raise — disconnect() catches all connection errors
  129. client.disconnect()
  130. assert client._ftp is None
  131. # ---------------------------------------------------------------------------
  132. # TestListFiles
  133. # ---------------------------------------------------------------------------
  134. class TestListFiles:
  135. """Tests for directory listing."""
  136. def test_list_empty_directory(self, ftp_client_factory):
  137. """Listing an empty directory returns empty list."""
  138. client = ftp_client_factory()
  139. client.connect()
  140. files = client.list_files("/cache")
  141. assert files == []
  142. client.disconnect()
  143. def test_list_directory_with_files(self, ftp_client_factory, ftp_server):
  144. """Files in directory are listed correctly."""
  145. ftp_server.add_file("cache/test.3mf", b"x" * 1024)
  146. ftp_server.add_file("cache/test2.gcode", b"y" * 512)
  147. client = ftp_client_factory()
  148. client.connect()
  149. files = client.list_files("/cache")
  150. names = {f["name"] for f in files}
  151. assert "test.3mf" in names
  152. assert "test2.gcode" in names
  153. client.disconnect()
  154. def test_directories_marked(self, ftp_client_factory, ftp_server):
  155. """Subdirectories are identified with is_directory=True."""
  156. ftp_server.add_directory("model/subdir")
  157. client = ftp_client_factory()
  158. client.connect()
  159. files = client.list_files("/model")
  160. dirs = [f for f in files if f["is_directory"]]
  161. assert len(dirs) >= 1
  162. assert dirs[0]["name"] == "subdir"
  163. client.disconnect()
  164. def test_nonexistent_path_returns_empty(self, ftp_client_factory):
  165. """Listing a nonexistent path returns empty list."""
  166. client = ftp_client_factory()
  167. client.connect()
  168. files = client.list_files("/nonexistent/path")
  169. assert files == []
  170. client.disconnect()
  171. def test_file_sizes_and_paths(self, ftp_client_factory, ftp_server):
  172. """File sizes and full paths are parsed correctly."""
  173. ftp_server.add_file("cache/sized.bin", b"a" * 2048)
  174. client = ftp_client_factory()
  175. client.connect()
  176. files = client.list_files("/cache")
  177. sized = [f for f in files if f["name"] == "sized.bin"]
  178. assert len(sized) == 1
  179. assert sized[0]["size"] == 2048
  180. assert sized[0]["path"] == "/cache/sized.bin"
  181. client.disconnect()
  182. # ---------------------------------------------------------------------------
  183. # TestDownload
  184. # ---------------------------------------------------------------------------
  185. class TestDownload:
  186. """Tests for file download operations."""
  187. def test_download_file_returns_bytes(self, ftp_client_factory, ftp_server):
  188. """download_file() returns file content as bytes."""
  189. content = b"Hello FTP World!"
  190. ftp_server.add_file("cache/hello.txt", content)
  191. client = ftp_client_factory()
  192. client.connect()
  193. result = client.download_file("/cache/hello.txt")
  194. assert result == content
  195. client.disconnect()
  196. def test_download_file_missing(self, ftp_client_factory):
  197. """download_file() returns None for missing file."""
  198. client = ftp_client_factory()
  199. client.connect()
  200. result = client.download_file("/cache/does_not_exist.txt")
  201. assert result is None
  202. client.disconnect()
  203. def test_download_to_file_writes_to_disk(self, ftp_client_factory, ftp_server, tmp_path):
  204. """download_to_file() writes content to local filesystem."""
  205. content = b"Downloaded content"
  206. ftp_server.add_file("cache/dl.bin", content)
  207. local = tmp_path / "output" / "dl.bin"
  208. client = ftp_client_factory()
  209. client.connect()
  210. result = client.download_to_file("/cache/dl.bin", local)
  211. assert result is True
  212. assert local.read_bytes() == content
  213. client.disconnect()
  214. def test_download_to_file_creates_parent_dirs(self, ftp_client_factory, ftp_server, tmp_path):
  215. """download_to_file() creates parent directories automatically."""
  216. ftp_server.add_file("cache/nested.txt", b"nested content")
  217. local = tmp_path / "deep" / "nested" / "path" / "nested.txt"
  218. client = ftp_client_factory()
  219. client.connect()
  220. result = client.download_to_file("/cache/nested.txt", local)
  221. assert result is True
  222. assert local.exists()
  223. client.disconnect()
  224. def test_zero_byte_download_returns_false(self, ftp_client_factory, ftp_server, tmp_path):
  225. """0-byte download returns False and cleans up (regression test)."""
  226. ftp_server.add_file("cache/empty.bin", b"")
  227. local = tmp_path / "empty.bin"
  228. client = ftp_client_factory()
  229. client.connect()
  230. result = client.download_to_file("/cache/empty.bin", local)
  231. assert result is False
  232. assert not local.exists()
  233. client.disconnect()
  234. def test_download_to_file_missing_raises_not_on_printer(self, ftp_client_factory, tmp_path):
  235. """Missing file raises FileNotOnPrinterError so callers can short-circuit
  236. the retry loop — 550 means the file isn't there and retrying won't help."""
  237. from backend.app.services.bambu_ftp import FileNotOnPrinterError
  238. local = tmp_path / "missing.bin"
  239. client = ftp_client_factory()
  240. client.connect()
  241. try:
  242. with pytest.raises(FileNotOnPrinterError):
  243. client.download_to_file("/cache/no_such_file.bin", local)
  244. finally:
  245. client.disconnect()
  246. def test_download_large_file(self, ftp_client_factory, ftp_server):
  247. """Large file download (>1MB) works correctly."""
  248. large_content = b"X" * (1024 * 1024 + 500) # ~1MB + 500 bytes
  249. ftp_server.add_file("cache/large.bin", large_content)
  250. client = ftp_client_factory()
  251. client.connect()
  252. result = client.download_file("/cache/large.bin")
  253. assert result == large_content
  254. client.disconnect()
  255. def test_download_not_connected(self):
  256. """download_file() returns None when not connected."""
  257. client = BambuFTPClient("127.0.0.1", "12345678")
  258. assert client.download_file("/cache/test.bin") is None
  259. # ---------------------------------------------------------------------------
  260. # TestUpload
  261. # ---------------------------------------------------------------------------
  262. class TestUpload:
  263. """Tests for file upload operations."""
  264. def test_upload_success(self, ftp_client_factory, ftp_server, tmp_path):
  265. """Successful upload via transfercmd (not storbinary)."""
  266. content = b"Upload test content"
  267. local = tmp_path / "upload.3mf"
  268. local.write_bytes(content)
  269. client = ftp_client_factory()
  270. client.connect()
  271. result = client.upload_file(local, "/cache/upload.3mf")
  272. assert result is True
  273. client.disconnect()
  274. # Verify via fresh connection (upload_file skips voidresp() for all
  275. # models, so the original session can't be reused for download)
  276. time.sleep(_UPLOAD_FLUSH_DELAY)
  277. client2 = ftp_client_factory()
  278. client2.connect()
  279. downloaded = client2.download_file("/cache/upload.3mf")
  280. assert downloaded == content
  281. client2.disconnect()
  282. def test_upload_progress_callback(self, ftp_client_factory, ftp_server, tmp_path):
  283. """Progress callback receives updates during upload."""
  284. content = b"P" * 2048
  285. local = tmp_path / "progress.bin"
  286. local.write_bytes(content)
  287. progress_calls = []
  288. def on_progress(uploaded, total):
  289. progress_calls.append((uploaded, total))
  290. client = ftp_client_factory()
  291. client.connect()
  292. client.upload_file(local, "/cache/progress.bin", on_progress)
  293. assert len(progress_calls) >= 1
  294. # Last call should report full file uploaded
  295. assert progress_calls[-1][0] == len(content)
  296. assert progress_calls[-1][1] == len(content)
  297. client.disconnect()
  298. def test_upload_not_connected(self, tmp_path):
  299. """Upload when not connected returns False."""
  300. local = tmp_path / "test.bin"
  301. local.write_bytes(b"data")
  302. client = BambuFTPClient("127.0.0.1", "12345678")
  303. assert client.upload_file(local, "/cache/test.bin") is False
  304. def test_upload_553_no_sd_card(self, ftp_client_factory, ftp_server, tmp_path):
  305. """553 error (no SD card) returns False."""
  306. ftp_server.inject_failure("STOR", 553, "Could not create file.")
  307. local = tmp_path / "test.bin"
  308. local.write_bytes(b"data")
  309. client = ftp_client_factory()
  310. client.connect()
  311. result = client.upload_file(local, "/cache/test.bin")
  312. assert result is False
  313. client.disconnect()
  314. def test_upload_550_permission_denied(self, ftp_client_factory, ftp_server, tmp_path):
  315. """550 error (permission denied) returns False."""
  316. ftp_server.inject_failure("STOR", 550, "Permission denied.")
  317. local = tmp_path / "test.bin"
  318. local.write_bytes(b"data")
  319. client = ftp_client_factory()
  320. client.connect()
  321. result = client.upload_file(local, "/cache/test.bin")
  322. assert result is False
  323. client.disconnect()
  324. def test_upload_552_storage_full(self, ftp_client_factory, ftp_server, tmp_path):
  325. """552 error (storage full) returns False."""
  326. ftp_server.inject_failure("STOR", 552, "Storage quota exceeded.")
  327. local = tmp_path / "test.bin"
  328. local.write_bytes(b"data")
  329. client = ftp_client_factory()
  330. client.connect()
  331. result = client.upload_file(local, "/cache/test.bin")
  332. assert result is False
  333. client.disconnect()
  334. def test_upload_426_with_intact_file_proceeds(self, ftp_client_factory, ftp_server, tmp_path):
  335. """Some P2S firmware revisions return 426 on voidresp() even when the
  336. file landed fully (TLS data-channel close races the 226). #1417
  337. follow-up — verify via SIZE: when server size matches, proceed with
  338. a warning instead of failing the dispatch.
  339. Pre-#1417 the catch raised unconditionally and the reporter saw 11
  340. retries fail in a row even though every upload was actually
  341. succeeding on the printer side (v0.2.4.1 worked because the prior
  342. proceed-with-warning branch tolerated the noise).
  343. """
  344. import ftplib # nosec B402 — tests need the real ftplib to construct mock 426 responses
  345. local = tmp_path / "test.bin"
  346. local.write_bytes(b"data" * 256) # 1024 bytes
  347. client = ftp_client_factory()
  348. client.connect()
  349. def raise_426():
  350. raise ftplib.error_temp("426 Failure reading network stream.")
  351. def fake_size(_path):
  352. # Real P2S firmware: voidresp returns 426 but the file IS on
  353. # the SD card at its full size. Mock can't reproduce both
  354. # halves naturally because pyftpdlib only flushes on a clean
  355. # voidresp, so we inject SIZE explicitly to model the
  356. # printer-side state the user observes.
  357. return 1024
  358. client._ftp.voidresp = raise_426
  359. client._ftp.size = fake_size
  360. result = client.upload_file(local, "/cache/test.bin")
  361. assert result is True, "intact file (SIZE match) tolerates 426 noise"
  362. client.disconnect()
  363. def test_upload_426_with_truncated_file_returns_false(self, ftp_client_factory, ftp_server, tmp_path):
  364. """The original #1401 fix is preserved: when SIZE confirms the file
  365. isn't on the server at full size (or SIZE itself fails), the upload
  366. must fail so the dispatcher doesn't send a print command for a
  367. partial 3MF."""
  368. import ftplib # nosec B402 — tests need the real ftplib to construct mock 426 responses
  369. local = tmp_path / "test.bin"
  370. local.write_bytes(b"data" * 256)
  371. client = ftp_client_factory()
  372. client.connect()
  373. def raise_426():
  374. raise ftplib.error_temp("426 Failure reading network stream.")
  375. # Make SIZE report a smaller value — file is genuinely truncated.
  376. def fake_size(_path):
  377. return 100
  378. client._ftp.voidresp = raise_426
  379. client._ftp.size = fake_size
  380. result = client.upload_file(local, "/cache/test.bin")
  381. assert result is False, "truncated file (SIZE mismatch) must fail"
  382. client.disconnect()
  383. def test_upload_426_with_size_check_failing_returns_false(self, ftp_client_factory, ftp_server, tmp_path):
  384. """If SIZE itself fails (e.g. server too broken to answer), assume
  385. the worst and fail — better a retry than a print on a partial file.
  386. """
  387. import ftplib # nosec B402 — tests need the real ftplib to construct mock 426 responses
  388. local = tmp_path / "test.bin"
  389. local.write_bytes(b"data" * 256)
  390. client = ftp_client_factory()
  391. client.connect()
  392. def raise_426():
  393. raise ftplib.error_temp("426 Failure reading network stream.")
  394. def raise_size(_path):
  395. raise ftplib.error_perm("550 File not found.")
  396. client._ftp.voidresp = raise_426
  397. client._ftp.size = raise_size
  398. result = client.upload_file(local, "/cache/test.bin")
  399. assert result is False
  400. client.disconnect()
  401. def test_upload_bytes_426_with_intact_file_proceeds(self, ftp_client_factory, ftp_server):
  402. """upload_bytes() mirrors the same SIZE-verify logic as upload_file."""
  403. import ftplib # nosec B402 — tests need the real ftplib to construct mock 426 responses
  404. client = ftp_client_factory()
  405. client.connect()
  406. data = b"x" * 1024
  407. def raise_426():
  408. raise ftplib.error_temp("426 Failure reading network stream.")
  409. def fake_size(_path):
  410. return 1024 # printer-side file matches expected size
  411. client._ftp.voidresp = raise_426
  412. client._ftp.size = fake_size
  413. result = client.upload_bytes(data, "/cache/bytes.bin")
  414. assert result is True
  415. client.disconnect()
  416. def test_upload_bytes_426_with_truncated_file_returns_false(self, ftp_client_factory, ftp_server):
  417. """The truncated branch for upload_bytes()."""
  418. import ftplib # nosec B402 — tests need the real ftplib to construct mock 426 responses
  419. client = ftp_client_factory()
  420. client.connect()
  421. data = b"x" * 1024
  422. def raise_426():
  423. raise ftplib.error_temp("426 Failure reading network stream.")
  424. def fake_size(_path):
  425. return 100
  426. client._ftp.voidresp = raise_426
  427. client._ftp.size = fake_size
  428. result = client.upload_bytes(data, "/cache/bytes.bin")
  429. assert result is False
  430. client.disconnect()
  431. def test_upload_bytes_success(self, ftp_client_factory, ftp_server):
  432. """upload_bytes() writes data to server."""
  433. data = b"Bytes upload content"
  434. client = ftp_client_factory()
  435. client.connect()
  436. result = client.upload_bytes(data, "/cache/bytes.bin")
  437. assert result is True
  438. client.disconnect()
  439. # Verify via fresh connection
  440. time.sleep(_UPLOAD_FLUSH_DELAY)
  441. client2 = ftp_client_factory()
  442. client2.connect()
  443. downloaded = client2.download_file("/cache/bytes.bin")
  444. assert downloaded == data
  445. client2.disconnect()
  446. def test_upload_bytes_failure(self, ftp_client_factory, ftp_server):
  447. """upload_bytes() returns False on STOR failure."""
  448. ftp_server.inject_failure("STOR", 553, "No space.")
  449. client = ftp_client_factory()
  450. client.connect()
  451. result = client.upload_bytes(b"data", "/cache/fail.bin")
  452. assert result is False
  453. client.disconnect()
  454. def test_upload_large_chunked(self, ftp_client_factory, ftp_server, tmp_path):
  455. """Large file upload in chunks completes without error.
  456. Uses 2.5MB to trigger multiple chunks with 64KB CHUNK_SIZE.
  457. Content verification skipped because upload_file() skips
  458. voidresp() for all models, so the server may still be flushing
  459. when we check. The upload result=True confirms the client sent
  460. all chunks without error.
  461. """
  462. content = b"C" * (1024 * 1024 * 2 + 512 * 1024)
  463. local = tmp_path / "large.bin"
  464. local.write_bytes(content)
  465. progress_calls = []
  466. def on_progress(uploaded, total):
  467. progress_calls.append((uploaded, total))
  468. client = ftp_client_factory()
  469. client.connect()
  470. result = client.upload_file(local, "/cache/large.bin", on_progress)
  471. assert result is True
  472. # Verify many chunks were sent (2.5MB / 64KB = 40 chunks)
  473. assert len(progress_calls) >= 38
  474. assert progress_calls[-1][0] == len(content)
  475. client.disconnect()
  476. # ---------------------------------------------------------------------------
  477. # TestDelete
  478. # ---------------------------------------------------------------------------
  479. class TestDelete:
  480. """Tests for file deletion."""
  481. def test_delete_success(self, ftp_client_factory, ftp_server):
  482. """Successful file deletion."""
  483. ftp_server.add_file("cache/to_delete.bin", b"delete me")
  484. client = ftp_client_factory()
  485. client.connect()
  486. result = client.delete_file("/cache/to_delete.bin")
  487. assert result is True
  488. assert not ftp_server.file_exists("cache/to_delete.bin")
  489. client.disconnect()
  490. def test_delete_not_found(self, ftp_client_factory):
  491. """Deleting a nonexistent file returns False."""
  492. client = ftp_client_factory()
  493. client.connect()
  494. result = client.delete_file("/cache/no_such_file.bin")
  495. assert result is False
  496. client.disconnect()
  497. def test_delete_not_connected(self):
  498. """Delete when not connected returns False."""
  499. client = BambuFTPClient("127.0.0.1", "12345678")
  500. assert client.delete_file("/cache/test.bin") is False
  501. # ---------------------------------------------------------------------------
  502. # TestFileSize
  503. # ---------------------------------------------------------------------------
  504. class TestFileSize:
  505. """Tests for get_file_size."""
  506. def test_file_size_correct(self, ftp_client_factory, ftp_server):
  507. """Returns correct file size."""
  508. ftp_server.add_file("cache/sized.bin", b"a" * 4096)
  509. client = ftp_client_factory()
  510. client.connect()
  511. size = client.get_file_size("/cache/sized.bin")
  512. assert size == 4096
  513. client.disconnect()
  514. def test_file_size_missing(self, ftp_client_factory):
  515. """Returns None for missing file."""
  516. client = ftp_client_factory()
  517. client.connect()
  518. size = client.get_file_size("/cache/no_file.bin")
  519. assert size is None
  520. client.disconnect()
  521. def test_file_size_not_connected(self):
  522. """Returns None when not connected."""
  523. client = BambuFTPClient("127.0.0.1", "12345678")
  524. assert client.get_file_size("/cache/test.bin") is None
  525. # ---------------------------------------------------------------------------
  526. # TestStorageInfo
  527. # ---------------------------------------------------------------------------
  528. class TestStorageInfo:
  529. """Tests for storage info and diagnostics."""
  530. def test_avbl_parsed(self, ftp_client_factory, ftp_server):
  531. """AVBL response is parsed for free_bytes."""
  532. ftp_server.set_avbl_bytes(5000000000)
  533. client = ftp_client_factory()
  534. client.connect()
  535. info = client.get_storage_info()
  536. assert info is not None
  537. assert info["free_bytes"] == 5000000000
  538. client.disconnect()
  539. def test_used_bytes_from_scan(self, ftp_client_factory, ftp_server):
  540. """used_bytes calculated from directory scan."""
  541. ftp_server.add_file("cache/file1.bin", b"a" * 1000)
  542. ftp_server.add_file("cache/file2.bin", b"b" * 2000)
  543. client = ftp_client_factory()
  544. client.connect()
  545. info = client.get_storage_info()
  546. assert info is not None
  547. assert info["used_bytes"] >= 3000 # At least these two files
  548. client.disconnect()
  549. def test_storage_info_not_connected(self):
  550. """Returns None when not connected."""
  551. client = BambuFTPClient("127.0.0.1", "12345678")
  552. assert client.get_storage_info() is None
  553. def test_diagnose_storage_success(self, ftp_client_factory, ftp_server):
  554. """diagnose_storage() returns connected=True with working diagnostics."""
  555. client = ftp_client_factory()
  556. client.connect()
  557. diag = client.diagnose_storage()
  558. assert diag["connected"] is True
  559. assert diag["can_list_root"] is True
  560. assert diag["can_list_cache"] is True
  561. assert diag["pwd"] is not None
  562. assert diag["storage_info"] is not None
  563. client.disconnect()
  564. def test_diagnose_storage_not_connected(self):
  565. """diagnose_storage() reports not connected."""
  566. client = BambuFTPClient("127.0.0.1", "12345678")
  567. diag = client.diagnose_storage()
  568. assert diag["connected"] is False
  569. assert "FTP not connected" in diag["errors"]
  570. # ---------------------------------------------------------------------------
  571. # TestModelSpecificBehavior
  572. # ---------------------------------------------------------------------------
  573. class TestModelSpecificBehavior:
  574. """Tests for printer model-specific FTP behavior."""
  575. def test_x1c_upload(self, ftp_client_factory, ftp_server, tmp_path):
  576. """X1C upload with session reuse succeeds."""
  577. content = b"X1C upload data"
  578. local = tmp_path / "x1c.3mf"
  579. local.write_bytes(content)
  580. client = ftp_client_factory(printer_model="X1C")
  581. client.connect()
  582. result = client.upload_file(local, "/cache/x1c.3mf")
  583. assert result is True
  584. client.disconnect()
  585. # Verify via fresh connection
  586. time.sleep(_UPLOAD_FLUSH_DELAY)
  587. client2 = ftp_client_factory(printer_model="X1C")
  588. client2.connect()
  589. downloaded = client2.download_file("/cache/x1c.3mf")
  590. assert downloaded == content
  591. client2.disconnect()
  592. def test_a1_upload_prot_c(self, ftp_client_factory, ftp_server, tmp_path):
  593. """A1 model upload with prot_c succeeds."""
  594. content = b"A1 upload data"
  595. local = tmp_path / "a1.3mf"
  596. local.write_bytes(content)
  597. client = ftp_client_factory(printer_model="A1", force_prot_c=True)
  598. client.connect()
  599. result = client.upload_file(local, "/cache/a1.3mf")
  600. assert result is True
  601. client.disconnect()
  602. # Verify via fresh connection
  603. time.sleep(_UPLOAD_FLUSH_DELAY)
  604. client2 = ftp_client_factory(printer_model="A1", force_prot_c=True)
  605. client2.connect()
  606. downloaded = client2.download_file("/cache/a1.3mf")
  607. assert downloaded == content
  608. client2.disconnect()
  609. def test_a1_mini_upload(self, ftp_client_factory, ftp_server, tmp_path):
  610. """A1 Mini model upload succeeds."""
  611. content = b"A1 Mini data"
  612. local = tmp_path / "a1mini.3mf"
  613. local.write_bytes(content)
  614. client = ftp_client_factory(printer_model="A1 Mini", force_prot_c=True)
  615. client.connect()
  616. result = client.upload_file(local, "/cache/a1mini.3mf")
  617. assert result is True
  618. client.disconnect()
  619. def test_p1s_upload(self, ftp_client_factory, ftp_server, tmp_path):
  620. """P1S model upload with session reuse succeeds."""
  621. content = b"P1S upload data"
  622. local = tmp_path / "p1s.3mf"
  623. local.write_bytes(content)
  624. client = ftp_client_factory(printer_model="P1S")
  625. client.connect()
  626. result = client.upload_file(local, "/cache/p1s.3mf")
  627. assert result is True
  628. client.disconnect()
  629. def test_unknown_model_defaults_prot_p(self, ftp_client_factory):
  630. """Unknown model defaults to prot_p."""
  631. client = ftp_client_factory(printer_model="FuturePrinter3000")
  632. assert client._is_a1_model() is False
  633. assert client._should_use_prot_c() is False
  634. assert client.connect() is True
  635. client.disconnect()
  636. def test_mode_cache_persists_and_clears(self, ftp_client_factory):
  637. """Mode cache works within a test and clears between tests."""
  638. # Cache should be empty at start (autouse fixture clears it)
  639. assert BambuFTPClient._mode_cache == {}
  640. # Connect and cache a mode
  641. BambuFTPClient.cache_mode("127.0.0.1", "prot_p")
  642. assert BambuFTPClient._mode_cache["127.0.0.1"] == "prot_p"
  643. # New client for same IP uses cached mode
  644. client = ftp_client_factory(printer_model="A1")
  645. assert client._get_cached_mode() == "prot_p"
  646. assert client._should_use_prot_c() is False
  647. client.disconnect()
  648. # ---------------------------------------------------------------------------
  649. # TestAsyncWrappers
  650. # ---------------------------------------------------------------------------
  651. class TestAsyncWrappers:
  652. """Tests for async wrapper functions using patch_ftp_port fixture."""
  653. @pytest.mark.asyncio
  654. async def test_upload_file_async_success(self, patch_ftp_port, tmp_path):
  655. """upload_file_async succeeds for X1C."""
  656. content = b"async upload"
  657. local = tmp_path / "async_up.3mf"
  658. local.write_bytes(content)
  659. result = await upload_file_async(
  660. "127.0.0.1",
  661. "12345678",
  662. local,
  663. "/cache/async_up.3mf",
  664. timeout=30.0,
  665. printer_model="X1C",
  666. )
  667. assert result is True
  668. @pytest.mark.asyncio
  669. async def test_upload_file_async_a1_fallback(self, patch_ftp_port, tmp_path):
  670. """upload_file_async tries prot_p then falls back to prot_c for A1."""
  671. content = b"a1 async upload"
  672. local = tmp_path / "a1_async.3mf"
  673. local.write_bytes(content)
  674. # For A1 models, if prot_p succeeds we get True.
  675. # If prot_p fails, it tries prot_c. Either way should succeed
  676. # against our mock server which accepts both.
  677. result = await upload_file_async(
  678. "127.0.0.1",
  679. "12345678",
  680. local,
  681. "/cache/a1_async.3mf",
  682. timeout=30.0,
  683. printer_model="A1",
  684. )
  685. assert result is True
  686. @pytest.mark.asyncio
  687. async def test_download_file_async_success(self, patch_ftp_port, tmp_path):
  688. """download_file_async succeeds."""
  689. server = patch_ftp_port
  690. content = b"async download content"
  691. server.add_file("cache/async_dl.bin", content)
  692. local = tmp_path / "async_dl.bin"
  693. result = await download_file_async(
  694. "127.0.0.1",
  695. "12345678",
  696. "/cache/async_dl.bin",
  697. local,
  698. timeout=30.0,
  699. printer_model="X1C",
  700. )
  701. assert result is True
  702. assert local.read_bytes() == content
  703. @pytest.mark.asyncio
  704. async def test_download_file_async_a1_fallback(self, patch_ftp_port, tmp_path):
  705. """download_file_async falls back for A1 models."""
  706. server = patch_ftp_port
  707. server.add_file("cache/a1_dl.bin", b"a1 data")
  708. local = tmp_path / "a1_dl.bin"
  709. result = await download_file_async(
  710. "127.0.0.1",
  711. "12345678",
  712. "/cache/a1_dl.bin",
  713. local,
  714. timeout=30.0,
  715. printer_model="A1",
  716. )
  717. assert result is True
  718. @pytest.mark.asyncio
  719. async def test_download_file_async_timeout_salvages_completed_zombie(self, tmp_path, monkeypatch):
  720. """Executor thread that completes after wait_for timeout is salvaged.
  721. asyncio.wait_for cannot cancel run_in_executor threads, so the FTP
  722. download may still complete after we give up waiting. If the thread
  723. genuinely finished (signalled via completion["success"] and the file
  724. is on disk), download_file_async should return True rather than False.
  725. Regression for #972: A1 user with 14 MB 3MF hit the hardcoded 60s
  726. timeout, but the download thread finished ~45s later. The successful
  727. file was written to disk but the async wrapper returned False, so the
  728. archive was created as a fallback with no 3MF data.
  729. """
  730. from backend.app.services import bambu_ftp
  731. # Clear mode cache so prot_p path is exercised.
  732. bambu_ftp.BambuFTPClient._mode_cache.pop("127.0.0.1", None)
  733. local = tmp_path / "zombie.bin"
  734. expected_content = b"late arrival but complete"
  735. class FakeClient:
  736. """Connects instantly, download_to_file sleeps past wait_for's
  737. timeout then writes the file and returns True."""
  738. def __init__(self, *args, **kwargs):
  739. pass
  740. def connect(self):
  741. return True
  742. def download_to_file(self, remote_path, local_path):
  743. time.sleep(0.4) # longer than wait_for timeout=0.1
  744. local_path.write_bytes(expected_content)
  745. return True
  746. def disconnect(self):
  747. pass
  748. monkeypatch.setattr(bambu_ftp, "BambuFTPClient", FakeClient)
  749. monkeypatch.setattr(FakeClient, "_mode_cache", {}, raising=False)
  750. monkeypatch.setattr(FakeClient, "A1_MODELS", {"A1"}, raising=False)
  751. def _noop_cache(ip, mode):
  752. pass
  753. monkeypatch.setattr(FakeClient, "cache_mode", staticmethod(_noop_cache), raising=False)
  754. result = await download_file_async(
  755. "127.0.0.1",
  756. "12345678",
  757. "/cache/zombie.bin",
  758. local,
  759. timeout=0.1,
  760. printer_model="X1C",
  761. )
  762. assert result is True
  763. assert local.read_bytes() == expected_content
  764. @pytest.mark.asyncio
  765. async def test_download_file_async_timeout_no_salvage_when_incomplete(self, tmp_path, monkeypatch):
  766. """Timeout returns False when thread has not signalled success.
  767. A partial file on disk (mid-retrbinary) must NOT be mistaken for a
  768. completed download — only the thread's explicit success flag permits
  769. salvage.
  770. """
  771. from backend.app.services import bambu_ftp
  772. bambu_ftp.BambuFTPClient._mode_cache.pop("127.0.0.1", None)
  773. local = tmp_path / "partial.bin"
  774. class FakeClient:
  775. def __init__(self, *args, **kwargs):
  776. pass
  777. def connect(self):
  778. return True
  779. def download_to_file(self, remote_path, local_path):
  780. # Simulate an in-progress partial write that never completes
  781. # within the salvage grace period.
  782. local_path.write_bytes(b"partial...")
  783. time.sleep(2.0)
  784. return True # would complete eventually, but too late
  785. def disconnect(self):
  786. pass
  787. monkeypatch.setattr(bambu_ftp, "BambuFTPClient", FakeClient)
  788. monkeypatch.setattr(FakeClient, "_mode_cache", {}, raising=False)
  789. monkeypatch.setattr(FakeClient, "A1_MODELS", set(), raising=False)
  790. monkeypatch.setattr(FakeClient, "cache_mode", staticmethod(lambda ip, mode: None), raising=False)
  791. result = await download_file_async(
  792. "127.0.0.1",
  793. "12345678",
  794. "/cache/partial.bin",
  795. local,
  796. timeout=0.1,
  797. printer_model="X1C",
  798. )
  799. assert result is False
  800. @pytest.mark.asyncio
  801. async def test_download_file_async_timeout_waits_for_slow_zombie(self, tmp_path, monkeypatch):
  802. """A zombie that completes within the 30s grace window is salvaged.
  803. Regression for #1014: on slow WiFi, download_to_file can overshoot the
  804. user's ftp_timeout by 10–30 s without being stuck. The old fixed 0.5 s
  805. post-timeout sleep was too short — it gave up and started attempt 2
  806. while attempt 1's zombie thread kept running, and by the time the zombie
  807. wrote the file to disk with a success flag, attempt 2 had already
  808. reported failure (its own completion dict was still False). The async
  809. wrapper now waits up to min(timeout, 30 s) for the worker thread to
  810. finish before returning, so a slow-but-progressing download salvages.
  811. """
  812. from backend.app.services import bambu_ftp
  813. bambu_ftp.BambuFTPClient._mode_cache.pop("127.0.0.1", None)
  814. local = tmp_path / "slow_zombie.bin"
  815. expected_content = b"finished during grace window"
  816. class FakeClient:
  817. """Mimics a slow FTP: wait_for gives up at 1.0 s but RETR takes
  818. 1.5 s total. Old 0.5 s fixed sleep would have bailed (0.5 < 0.5
  819. extra); new grace = max(min(1.0, 30), 0.5) = 1.0 s covers the
  820. remaining 0.5 s so salvage succeeds."""
  821. def __init__(self, *args, **kwargs):
  822. pass
  823. def connect(self):
  824. return True
  825. def download_to_file(self, remote_path, local_path):
  826. time.sleep(1.5) # wait_for times out at 1.0 s; zombie finishes 0.5 s later
  827. local_path.write_bytes(expected_content)
  828. return True
  829. def disconnect(self):
  830. pass
  831. monkeypatch.setattr(bambu_ftp, "BambuFTPClient", FakeClient)
  832. monkeypatch.setattr(FakeClient, "_mode_cache", {}, raising=False)
  833. monkeypatch.setattr(FakeClient, "A1_MODELS", set(), raising=False)
  834. monkeypatch.setattr(FakeClient, "cache_mode", staticmethod(lambda ip, mode: None), raising=False)
  835. result = await download_file_async(
  836. "127.0.0.1",
  837. "12345678",
  838. "/cache/slow_zombie.bin",
  839. local,
  840. timeout=1.0,
  841. printer_model="X1C",
  842. )
  843. assert result is True
  844. assert local.read_bytes() == expected_content
  845. @pytest.mark.asyncio
  846. async def test_download_file_try_paths_first_succeeds(self, patch_ftp_port, tmp_path):
  847. """download_file_try_paths_async succeeds on first path."""
  848. server = patch_ftp_port
  849. server.add_file("cache/try1.bin", b"first path")
  850. local = tmp_path / "try.bin"
  851. result = await download_file_try_paths_async(
  852. "127.0.0.1",
  853. "12345678",
  854. ["/cache/try1.bin", "/cache/try2.bin"],
  855. local,
  856. printer_model="X1C",
  857. )
  858. assert result is True
  859. assert local.read_bytes() == b"first path"
  860. @pytest.mark.asyncio
  861. async def test_download_file_try_paths_fallback(self, patch_ftp_port, tmp_path):
  862. """download_file_try_paths_async falls back to second path."""
  863. server = patch_ftp_port
  864. server.add_file("cache/second.bin", b"second path")
  865. local = tmp_path / "fallback.bin"
  866. result = await download_file_try_paths_async(
  867. "127.0.0.1",
  868. "12345678",
  869. ["/cache/missing.bin", "/cache/second.bin"],
  870. local,
  871. printer_model="X1C",
  872. )
  873. assert result is True
  874. assert local.read_bytes() == b"second path"
  875. @pytest.mark.asyncio
  876. async def test_list_files_async_success(self, patch_ftp_port):
  877. """list_files_async returns file list."""
  878. server = patch_ftp_port
  879. server.add_file("cache/listed.bin", b"data")
  880. result = await list_files_async(
  881. "127.0.0.1",
  882. "12345678",
  883. "/cache",
  884. timeout=30.0,
  885. printer_model="X1C",
  886. )
  887. names = {f["name"] for f in result}
  888. assert "listed.bin" in names
  889. @pytest.mark.asyncio
  890. async def test_delete_file_async_success(self, patch_ftp_port):
  891. """delete_file_async deletes a file."""
  892. server = patch_ftp_port
  893. server.add_file("cache/to_async_del.bin", b"delete me")
  894. result = await delete_file_async(
  895. "127.0.0.1",
  896. "12345678",
  897. "/cache/to_async_del.bin",
  898. printer_model="X1C",
  899. )
  900. assert result is True
  901. assert not server.file_exists("cache/to_async_del.bin")
  902. # ---------------------------------------------------------------------------
  903. # TestFailureScenarios
  904. # ---------------------------------------------------------------------------
  905. class TestFailureScenarios:
  906. """Regression tests for known FTP failure modes."""
  907. def test_550_caught_by_broad_except(self, ftp_client_factory, ftp_server, tmp_path):
  908. """550 error_perm is caught by (OSError, ftplib.Error) handler.
  909. Regression: error_perm is a subclass of ftplib.Error, so the
  910. broad except clause in upload_file catches it correctly.
  911. """
  912. ftp_server.inject_failure("STOR", 550, "Permission denied.")
  913. local = tmp_path / "test.bin"
  914. local.write_bytes(b"data")
  915. client = ftp_client_factory()
  916. client.connect()
  917. result = client.upload_file(local, "/cache/test.bin")
  918. assert result is False
  919. client.disconnect()
  920. def test_zero_byte_download_detected(self, ftp_client_factory, ftp_server, tmp_path):
  921. """0-byte download is detected and file is cleaned up.
  922. Regression: Prior to fix, 0-byte downloads were reported as success.
  923. """
  924. ftp_server.add_file("cache/zero.bin", b"")
  925. local = tmp_path / "zero.bin"
  926. client = ftp_client_factory()
  927. client.connect()
  928. result = client.download_to_file("/cache/zero.bin", local)
  929. assert result is False
  930. assert not local.exists()
  931. client.disconnect()
  932. def test_connection_refused_handled(self):
  933. """Connection refused is handled gracefully."""
  934. client = BambuFTPClient("127.0.0.1", "12345678", timeout=2.0)
  935. client.FTP_PORT = 1 # Almost certainly not listening
  936. assert client.connect() is False
  937. def test_auth_failure_530(self, ftp_client_factory, ftp_server):
  938. """530 authentication failure returns False."""
  939. ftp_server.inject_failure("PASS", 530, "Login incorrect.")
  940. client = ftp_client_factory()
  941. result = client.connect()
  942. assert result is False
  943. def test_retr_550_handled(self, ftp_client_factory, ftp_server):
  944. """RETR 550 (file not found) returns None."""
  945. ftp_server.inject_failure("RETR", 550, "File not found.")
  946. ftp_server.add_file("cache/exists.bin", b"data")
  947. client = ftp_client_factory()
  948. client.connect()
  949. result = client.download_file("/cache/exists.bin")
  950. assert result is None
  951. client.disconnect()
  952. def test_cwd_550_handled(self, ftp_client_factory, ftp_server):
  953. """CWD 550 is handled in list_files."""
  954. ftp_server.inject_failure("CWD", 550, "Directory not found.")
  955. client = ftp_client_factory()
  956. client.connect()
  957. result = client.list_files("/nonexistent")
  958. assert result == []
  959. client.disconnect()
  960. def test_stor_553_handled(self, ftp_client_factory, ftp_server, tmp_path):
  961. """STOR 553 (no SD card) handled gracefully."""
  962. ftp_server.inject_failure("STOR", 553, "Could not create file.")
  963. local = tmp_path / "test.bin"
  964. local.write_bytes(b"test")
  965. client = ftp_client_factory()
  966. client.connect()
  967. result = client.upload_file(local, "/cache/test.bin")
  968. assert result is False
  969. client.disconnect()
  970. def test_diagnose_storage_cwd_failure_doesnt_propagate(self, ftp_client_factory, ftp_server):
  971. """diagnose_storage CWD failure doesn't crash the whole operation.
  972. Regression: diagnose_storage() was called in the upload path and
  973. a CWD failure would propagate and crash the upload.
  974. """
  975. ftp_server.inject_failure("CWD", 550, "No such directory.", count=2)
  976. client = ftp_client_factory()
  977. client.connect()
  978. diag = client.diagnose_storage()
  979. # Should still return results (with errors noted)
  980. assert diag["connected"] is True
  981. assert len(diag["errors"]) > 0
  982. client.disconnect()
  983. def test_failure_injection_count_decrements(self, ftp_client_factory, ftp_server):
  984. """Failure injection with count decrements and eventually succeeds."""
  985. ftp_server.add_file("cache/retry.bin", b"data after retry")
  986. ftp_server.inject_failure("RETR", 550, "Temporary error.", count=1)
  987. client = ftp_client_factory()
  988. client.connect()
  989. # First attempt fails
  990. result1 = client.download_file("/cache/retry.bin")
  991. assert result1 is None
  992. # Second attempt succeeds (failure count exhausted)
  993. result2 = client.download_file("/cache/retry.bin")
  994. assert result2 == b"data after retry"
  995. client.disconnect()
  996. def test_upload_skips_voidresp(self, ftp_client_factory, ftp_server, tmp_path):
  997. """Upload returns True without calling voidresp() for any model.
  998. voidresp() is skipped for all models: A1 printers hang on it,
  999. H2D printers delay the 226 response by 30+ seconds, and X1C/P1S
  1000. gain nothing from waiting. The file is on the SD card once
  1001. sendall() returns.
  1002. """
  1003. content = b"voidresp test data"
  1004. local = tmp_path / "voidresp_test.3mf"
  1005. local.write_bytes(content)
  1006. for model in ("X1C", "A1", "H2D", None):
  1007. client = ftp_client_factory(printer_model=model)
  1008. client.connect()
  1009. result = client.upload_file(local, "/cache/voidresp_test.3mf")
  1010. assert result is True, f"Upload failed for model={model}"
  1011. client.disconnect()
  1012. # Verify the file is actually on the server
  1013. time.sleep(_UPLOAD_FLUSH_DELAY)
  1014. client2 = ftp_client_factory()
  1015. client2.connect()
  1016. downloaded = client2.download_file("/cache/voidresp_test.3mf")
  1017. assert downloaded == content, f"Content mismatch for model={model}"
  1018. client2.disconnect()
  1019. # ---------------------------------------------------------------------------
  1020. # Short-circuit retries on 550 (#972)
  1021. # ---------------------------------------------------------------------------
  1022. class TestFileNotOnPrinterShortCircuit:
  1023. """FileNotOnPrinterError must bypass the retry budget.
  1024. Before this fix, a 3MF path that wasn't on the printer (550) cost
  1025. `ftp_retry_count + 1` attempts × `ftp_retry_delay` seconds per candidate
  1026. path. With ftp_retry_count=10 and four candidate paths, that's ~22 min
  1027. of dead retries before the real path is tried. #972 in the wild showed
  1028. 48 min of retrying paths that didn't exist.
  1029. """
  1030. async def test_with_ftp_retry_propagates_file_not_on_printer_without_retrying(self):
  1031. """with_ftp_retry raises FileNotOnPrinterError on first attempt.
  1032. Verifies non_retry_exceptions short-circuits before the retry loop
  1033. has a chance to sleep and try again.
  1034. """
  1035. attempts = {"n": 0}
  1036. async def always_missing(*_args, **_kwargs):
  1037. attempts["n"] += 1
  1038. raise FileNotOnPrinterError("/cache/absent.3mf: 550")
  1039. with pytest.raises(FileNotOnPrinterError):
  1040. await with_ftp_retry(
  1041. always_missing,
  1042. max_retries=10,
  1043. retry_delay=0.01,
  1044. operation_name="test 550 short-circuit",
  1045. non_retry_exceptions=(FileNotOnPrinterError,),
  1046. )
  1047. assert attempts["n"] == 1, "550 must not trigger any retry"
  1048. async def test_with_ftp_retry_still_retries_transient_errors(self):
  1049. """Non-550 exceptions continue to retry up to max_retries + 1."""
  1050. attempts = {"n": 0}
  1051. async def flaky(*_args, **_kwargs):
  1052. attempts["n"] += 1
  1053. raise TimeoutError("transient")
  1054. result = await with_ftp_retry(
  1055. flaky,
  1056. max_retries=2,
  1057. retry_delay=0.01,
  1058. operation_name="test transient retries",
  1059. non_retry_exceptions=(FileNotOnPrinterError,),
  1060. )
  1061. assert result is None
  1062. assert attempts["n"] == 3, "Transient errors should retry to exhaustion"
  1063. def test_download_to_file_raises_on_missing_path(self, ftp_client_factory, tmp_path):
  1064. """download_to_file surfaces 550 as FileNotOnPrinterError end-to-end
  1065. against the real mock FTPS server, not just a hand-rolled mock."""
  1066. local = tmp_path / "never_downloaded.3mf"
  1067. client = ftp_client_factory()
  1068. client.connect()
  1069. try:
  1070. with pytest.raises(FileNotOnPrinterError):
  1071. client.download_to_file("/cache/does_not_exist.3mf", local)
  1072. finally:
  1073. client.disconnect()
  1074. assert not local.exists(), "Partial file must be cleaned up on 550"
  1075. # ---------------------------------------------------------------------------
  1076. # 3MF download cache (#972)
  1077. # ---------------------------------------------------------------------------
  1078. class TestThreeMFCache:
  1079. """Cover endpoint and archive flow share downloaded 3MF bytes via this
  1080. cache. Tests isolate themselves with clear_3mf_cache(delete_files=False)
  1081. so they don't clobber each other."""
  1082. def setup_method(self):
  1083. clear_3mf_cache(delete_files=False)
  1084. def teardown_method(self):
  1085. clear_3mf_cache(delete_files=False)
  1086. def test_normalize_collapses_filename_variants(self):
  1087. """Bambu names vary (.3mf, .gcode.3mf, with spaces) — they all map
  1088. to the same cache slot so both flows agree on the key."""
  1089. canonical = normalize_3mf_name("Broly_Legendary.gcode.3mf")
  1090. assert normalize_3mf_name("Broly_Legendary.3mf") == canonical
  1091. assert normalize_3mf_name("Broly_Legendary") == canonical
  1092. # Bambu Studio rewrites spaces to underscores on upload — treat as equal
  1093. assert normalize_3mf_name("Broly Legendary") == canonical
  1094. # Case is also collapsed so keys match across capitalizations
  1095. assert normalize_3mf_name("BROLY_LEGENDARY.3MF") == canonical
  1096. def test_cache_hit_returns_stored_path(self, tmp_path):
  1097. """get_cached_3mf returns the same Path that was put in."""
  1098. f = tmp_path / "Broly.gcode.3mf"
  1099. f.write_bytes(b"fake 3mf content")
  1100. cache_3mf_download(1, "Broly.gcode.3mf", f)
  1101. assert get_cached_3mf(1, "Broly.gcode.3mf") == f
  1102. def test_cache_lookup_uses_normalized_name(self, tmp_path):
  1103. """Caching under .gcode.3mf and querying with bare name still hits."""
  1104. f = tmp_path / "Broly.gcode.3mf"
  1105. f.write_bytes(b"x")
  1106. cache_3mf_download(1, "Broly.gcode.3mf", f)
  1107. assert get_cached_3mf(1, "Broly.3mf") == f
  1108. assert get_cached_3mf(1, "Broly") == f
  1109. def test_cache_miss_on_different_printer(self, tmp_path):
  1110. """Printer id is part of the key — two printers never collide."""
  1111. f = tmp_path / "A.3mf"
  1112. f.write_bytes(b"x")
  1113. cache_3mf_download(1, "A.3mf", f)
  1114. assert get_cached_3mf(2, "A.3mf") is None
  1115. def test_cache_evicts_when_file_deleted(self, tmp_path):
  1116. """Stale entry (file gone) returns None and is dropped from the dict."""
  1117. f = tmp_path / "A.3mf"
  1118. f.write_bytes(b"x")
  1119. cache_3mf_download(1, "A.3mf", f)
  1120. f.unlink()
  1121. assert get_cached_3mf(1, "A.3mf") is None
  1122. # Re-populating after eviction works — no ghost entries remain.
  1123. f.write_bytes(b"y")
  1124. cache_3mf_download(1, "A.3mf", f)
  1125. assert get_cached_3mf(1, "A.3mf") == f
  1126. def test_clear_by_printer_scoped(self, tmp_path, monkeypatch):
  1127. """Clearing one printer leaves the other untouched."""
  1128. from backend.app.core import config as _config
  1129. monkeypatch.setattr(_config.settings, "archive_dir", tmp_path)
  1130. temp_dir = tmp_path / "temp"
  1131. temp_dir.mkdir()
  1132. f1 = temp_dir / "one.3mf"
  1133. f1.write_bytes(b"1")
  1134. f2 = temp_dir / "two.3mf"
  1135. f2.write_bytes(b"2")
  1136. cache_3mf_download(1, "one.3mf", f1)
  1137. cache_3mf_download(2, "two.3mf", f2)
  1138. clear_3mf_cache(1)
  1139. assert get_cached_3mf(1, "one.3mf") is None
  1140. assert get_cached_3mf(2, "two.3mf") == f2
  1141. # clear_3mf_cache defaulted to delete_files=True, so the temp file is gone
  1142. assert not f1.exists()
  1143. assert f2.exists()
  1144. def test_clear_without_deleting_files(self, tmp_path, monkeypatch):
  1145. """delete_files=False leaves files on disk — used by tests."""
  1146. from backend.app.core import config as _config
  1147. monkeypatch.setattr(_config.settings, "archive_dir", tmp_path)
  1148. temp_dir = tmp_path / "temp"
  1149. temp_dir.mkdir()
  1150. f = temp_dir / "keep.3mf"
  1151. f.write_bytes(b"x")
  1152. cache_3mf_download(1, "keep.3mf", f)
  1153. clear_3mf_cache(1, delete_files=False)
  1154. assert get_cached_3mf(1, "keep.3mf") is None
  1155. assert f.exists()
  1156. def test_clear_does_not_delete_persistent_files(self, tmp_path, monkeypatch):
  1157. """Regression for #1212 / "file disappeared overnight" reports.
  1158. Dispatch sites added in #1166 cache the live archive copy and library
  1159. file bytes — paths outside ``archive_dir/temp`` — so /cover can skip
  1160. FTP. Those files are user data; the cache cleanup must never unlink
  1161. them. Pre-fix, ``clear_3mf_cache(printer_id, delete_files=True)`` ran
  1162. on every ``on_print_complete`` and silently destroyed them, leaving a
  1163. DB row whose ``file_path`` pointed at nothing — breaking Reprint and
  1164. View G-code with a 404.
  1165. """
  1166. from backend.app.core import config as _config
  1167. monkeypatch.setattr(_config.settings, "archive_dir", tmp_path / "archive")
  1168. (tmp_path / "archive" / "temp").mkdir(parents=True)
  1169. archive_file = tmp_path / "archive" / "1" / "20260504_wallhooks" / "wallhooks.gcode.3mf"
  1170. archive_file.parent.mkdir(parents=True)
  1171. archive_file.write_bytes(b"archive bytes")
  1172. library_file = tmp_path / "library_files" / "abcd.3mf"
  1173. library_file.parent.mkdir(parents=True)
  1174. library_file.write_bytes(b"library bytes")
  1175. temp_file = tmp_path / "archive" / "temp" / "cover_1_x.3mf"
  1176. temp_file.write_bytes(b"temp bytes")
  1177. cache_3mf_download(1, "wallhooks.gcode.3mf", archive_file)
  1178. cache_3mf_download(1, "library.3mf", library_file)
  1179. cache_3mf_download(1, "cover_1_x.3mf", temp_file)
  1180. clear_3mf_cache(1)
  1181. # All three cache entries are dropped from the dict.
  1182. assert get_cached_3mf(1, "wallhooks.gcode.3mf") is None
  1183. assert get_cached_3mf(1, "library.3mf") is None
  1184. assert get_cached_3mf(1, "cover_1_x.3mf") is None
  1185. # But only the temp file is unlinked — user data survives.
  1186. assert archive_file.exists(), "archive 3mf must not be deleted by cache cleanup"
  1187. assert library_file.exists(), "library 3mf must not be deleted by cache cleanup"
  1188. assert not temp_file.exists(), "temp file should still be cleaned up"