test_3mf_files_for_print.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """Which file of a printer directory listing is the starting print's 3MF (SN-C4).
  2. When no direct download path works, print start lists the printer's
  3. directories and takes the file named for the print. A file that only contains
  4. the name is another print's ("Cube_large" is not "Cube").
  5. """
  6. import pytest
  7. from backend.app.main import _3mf_files_for_print
  8. @pytest.mark.parametrize(
  9. ("listed", "term", "picked"),
  10. [
  11. (["Cube_large.gcode.3mf", "Cube.gcode.3mf"], "cube", ["Cube.gcode.3mf"]),
  12. (["Cube_large.gcode.3mf", "Cube.3mf"], "cube", ["Cube.3mf"]),
  13. # The printer's underscores for spaces
  14. (["Cable Clip.gcode.3mf"], "cable_clip", ["Cable Clip.gcode.3mf"]),
  15. # No file of its own name: one that contains it is taken only alone
  16. (["Cube_v2.gcode.3mf"], "cube", ["Cube_v2.gcode.3mf"]),
  17. (["Cube_large.gcode.3mf", "Cube_small.gcode.3mf"], "cube", []),
  18. (["Cube.gcode", "notes.txt"], "cube", []),
  19. # Across folders: the print's own file in a later folder wins
  20. (["/cache/Cube_large.3mf", "/Cube.3mf"], "cube", ["/Cube.3mf"]),
  21. (["/cache/Cube.3mf", "/model/Cube.gcode.3mf"], "cube", ["/cache/Cube.3mf", "/model/Cube.gcode.3mf"]),
  22. # A folder named like the print does not make its files the print's
  23. (["/cube/other.3mf"], "cube", []),
  24. ],
  25. )
  26. def test_the_print_s_own_file(listed, term, picked):
  27. assert _3mf_files_for_print(listed, term) == picked
  28. @pytest.mark.asyncio
  29. @pytest.mark.integration
  30. async def test_print_start_downloads_only_the_print_s_own_file(test_engine, printer_factory):
  31. """Direct paths fail, so print start searches the printer's directories."""
  32. from unittest.mock import AsyncMock, patch
  33. from backend.tests.integration.test_external_print_confirmation_1898 import SUBTASK, _drive_print_start
  34. printer = await printer_factory()
  35. listing = [{"name": f"{SUBTASK}_large.gcode.3mf"}, {"name": f"{SUBTASK}.gcode.3mf"}]
  36. listed = patch("backend.app.services.bambu_ftp.list_files_async", new=AsyncMock(return_value=listing))
  37. mocks = await _drive_print_start(test_engine, printer, download_ok=False, extra_patches=[listed])
  38. requested = [c.args[2] for c in mocks["download_file_async"].await_args_list]
  39. assert "/cache/" + listing[1]["name"] in requested, "the directory search did not try the print's file"
  40. assert not [path for path in requested if "_large" in path], requested
  41. @pytest.mark.asyncio
  42. @pytest.mark.integration
  43. async def test_the_print_s_own_file_in_a_later_folder_wins(test_engine, printer_factory):
  44. from unittest.mock import AsyncMock, patch
  45. from backend.tests.integration.test_external_print_confirmation_1898 import SUBTASK, _drive_print_start
  46. printer = await printer_factory()
  47. async def listing(_ip, _code, folder, **_kwargs):
  48. return {"/cache": [{"name": f"{SUBTASK}_large.gcode.3mf"}], "/": [{"name": f"{SUBTASK}.3mf"}]}.get(folder, [])
  49. listed = patch("backend.app.services.bambu_ftp.list_files_async", new=AsyncMock(side_effect=listing))
  50. mocks = await _drive_print_start(test_engine, printer, download_ok=False, extra_patches=[listed])
  51. requested = [c.args[2] for c in mocks["download_file_async"].await_args_list]
  52. assert not [path for path in requested if "_large" in path], requested