test_slicer_resend_joins_archive_1634.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. """The same slice sent again from the slicer adds a run to its archive (#1634).
  2. A slicer sending a slice again writes a new 3MF around the same G-code, so the
  3. file's hash never matched and each send became an archive of its own, its
  4. print count stuck at one. A print sent from the slicer whose G-code matches an
  5. archive (same owner, no project, no File Manager file) now joins that archive:
  6. the archive takes the new file in place of its old one and opens this print's
  7. run on it.
  8. """
  9. from __future__ import annotations
  10. import zipfile
  11. from datetime import datetime, timezone
  12. from unittest.mock import AsyncMock, patch
  13. import pytest
  14. from sqlalchemy import select
  15. from backend.app.core.config import settings as app_settings
  16. from backend.app.models.archive import PrintArchive
  17. from backend.app.models.print_log import INCLUDE_OPEN_RUNS, RUN_OPEN, PrintLogEntry
  18. from backend.app.services import archive_work
  19. from backend.app.services.archive import ArchiveService
  20. from backend.app.services.archive_gcode_hash import gcode_fingerprint
  21. from backend.tests.integration.test_external_print_confirmation_1898 import (
  22. _clear_print_state, # noqa: F401 - autouse fixture
  23. _drive_print_start,
  24. )
  25. pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
  26. GCODE = b"; HEADER_BLOCK_START\nG28\nG1 X10 Y10 F3000\n"
  27. def _sliced(path, gcode: bytes = GCODE, *, export: str = "first"):
  28. """A sliced 3MF; ``export`` stands for what a new export of it changes."""
  29. path.parent.mkdir(parents=True, exist_ok=True)
  30. with zipfile.ZipFile(path, "w") as zf:
  31. zf.writestr("3D/3dmodel.model", f"<model export='{export}'/>")
  32. zf.writestr("Metadata/plate_1.gcode", gcode)
  33. return path
  34. @pytest.fixture
  35. def files(tmp_path, monkeypatch):
  36. monkeypatch.setattr(app_settings, "base_dir", tmp_path)
  37. monkeypatch.setattr(app_settings, "archive_dir", tmp_path / "archive")
  38. return tmp_path
  39. async def _archived(db_session, printer_id: int, files, *, name: str = "Bracket.3mf", **fields) -> PrintArchive:
  40. """The archive of the first send, printed and done."""
  41. archive = await ArchiveService(db_session).archive_print(printer_id, _sliced(files / "first" / name))
  42. for key, value in {"status": "completed", **fields}.items():
  43. setattr(archive, key, value)
  44. await db_session.commit()
  45. return archive
  46. async def _send_again(test_engine, printer, files, gcode: bytes = GCODE, *, owner_id: int | None = None):
  47. """The slicer sends a print; the printer's 3MF is a new export of ``gcode``."""
  48. upload = _sliced(files / "upload" / "sent.3mf", gcode, export="again")
  49. async def download(_ip, _code, _remote, local_path, **_kwargs):
  50. local_path.write_bytes(upload.read_bytes())
  51. return True
  52. return await _drive_print_start(
  53. test_engine,
  54. printer,
  55. download_ok=True,
  56. extra_patches=(
  57. patch("backend.app.main.download_file_async", new=AsyncMock(side_effect=download)),
  58. patch("backend.app.main.ArchiveService", ArchiveService),
  59. patch("backend.app.main._ask_outcome_for_external_print", new=AsyncMock(return_value=False)),
  60. patch("backend.app.main._print_user_id_if_still_there", new=AsyncMock(return_value=owner_id)),
  61. ),
  62. )
  63. async def _archives(db_session) -> list[PrintArchive]:
  64. db_session.expire_all()
  65. return list((await db_session.scalars(select(PrintArchive).order_by(PrintArchive.id))).all())
  66. async def _open_runs(db_session, archive_id: int) -> list[PrintLogEntry]:
  67. return list(
  68. (
  69. await db_session.scalars(
  70. select(PrintLogEntry)
  71. .where(PrintLogEntry.archive_id == archive_id, PrintLogEntry.status == RUN_OPEN)
  72. .execution_options(**{INCLUDE_OPEN_RUNS: True})
  73. )
  74. ).all()
  75. )
  76. class TestJoining:
  77. async def test_the_same_slice_sent_again_adds_a_run_to_its_archive(
  78. self, test_engine, db_session, printer_factory, files
  79. ):
  80. printer = await printer_factory()
  81. archive = await _archived(db_session, printer.id, files)
  82. await _send_again(test_engine, printer, files)
  83. (row,) = await _archives(db_session)
  84. assert row.id == archive.id
  85. assert row.status == "printing"
  86. assert len(await _open_runs(db_session, archive.id)) == 1
  87. from backend.app.main import _active_prints
  88. assert archive.id in _active_prints.values()
  89. async def test_it_takes_the_new_file_into_its_own_folder(self, test_engine, db_session, printer_factory, files):
  90. printer = await printer_factory()
  91. archive = await _archived(db_session, printer.id, files)
  92. folder = (files / archive.file_path).parent
  93. old_file = files / archive.file_path
  94. photo = folder / "photos" / "finish.jpg"
  95. photo.parent.mkdir()
  96. photo.write_bytes(b"jpg")
  97. old_hash = archive.content_hash
  98. await _send_again(test_engine, printer, files)
  99. (row,) = await _archives(db_session)
  100. new_file = files / row.file_path
  101. assert new_file.parent == folder
  102. assert new_file.is_file() and new_file != old_file
  103. assert not old_file.exists()
  104. assert photo.exists()
  105. assert row.content_hash != old_hash
  106. assert row.gcode_hash == gcode_fingerprint(new_file)
  107. async def test_another_slice_is_archived_anew(self, test_engine, db_session, printer_factory, files):
  108. printer = await printer_factory()
  109. archive = await _archived(db_session, printer.id, files)
  110. await _send_again(test_engine, printer, files, GCODE + b"G1 Z0.2\n")
  111. rows = await _archives(db_session)
  112. assert len(rows) == 2
  113. assert rows[0].id == archive.id and rows[0].status == "completed"
  114. @pytest.mark.parametrize("which", ["project", "library file", "deleted"])
  115. async def test_an_archive_that_is_not_the_slicers_is_not_joined(
  116. self, test_engine, db_session, printer_factory, files, which
  117. ):
  118. from backend.app.models.library import LibraryFile
  119. from backend.app.models.project import Project
  120. printer = await printer_factory()
  121. fields: dict = {}
  122. if which == "project":
  123. project = Project(name="Brackets")
  124. db_session.add(project)
  125. await db_session.commit()
  126. fields["project_id"] = project.id
  127. elif which == "library file":
  128. lib = LibraryFile(filename="Bracket.3mf", file_path="library/Bracket.3mf", file_size=1, file_type="3mf")
  129. db_session.add(lib)
  130. await db_session.commit()
  131. fields["library_file_id"] = lib.id
  132. else:
  133. fields["deleted_at"] = datetime.now(timezone.utc)
  134. await _archived(db_session, printer.id, files, **fields)
  135. await _send_again(test_engine, printer, files)
  136. assert len(await _archives(db_session)) == 2
  137. async def test_the_owner_has_to_match(self, test_engine, db_session, printer_factory, files):
  138. from backend.app.models.user import User
  139. user = User(username="maker", password_hash="x", role="user")
  140. db_session.add(user)
  141. await db_session.commit()
  142. user_id = user.id
  143. printer = await printer_factory()
  144. owned = await _archived(db_session, printer.id, files, created_by_id=user_id)
  145. owned_id = owned.id
  146. # Nobody started this print: the owned archive is not its.
  147. await _send_again(test_engine, printer, files)
  148. assert len(await _archives(db_session)) == 2
  149. # Its owner started the next one, once that print ended.
  150. from sqlalchemy import update
  151. await db_session.execute(update(PrintArchive).values(status="completed"))
  152. await db_session.execute(
  153. update(PrintLogEntry).values(status="completed").execution_options(**{INCLUDE_OPEN_RUNS: True})
  154. )
  155. await db_session.commit()
  156. await db_session.refresh(printer)
  157. await _send_again(test_engine, printer, files, owner_id=user_id)
  158. rows = await _archives(db_session)
  159. assert len(rows) == 2
  160. assert len(await _open_runs(db_session, owned_id)) == 1
  161. async def test_an_archive_being_merged_is_left_alone(self, test_engine, db_session, printer_factory, files):
  162. printer = await printer_factory()
  163. archive = await _archived(db_session, printer.id, files)
  164. with archive_work.merging([archive.id]):
  165. await _send_again(test_engine, printer, files)
  166. assert len(await _archives(db_session)) == 2
  167. assert not archive_work.is_held(archive.id)
  168. async def test_the_archive_is_released_once_its_run_is_open(self, test_engine, db_session, printer_factory, files):
  169. printer = await printer_factory()
  170. archive = await _archived(db_session, printer.id, files)
  171. await _send_again(test_engine, printer, files)
  172. assert not archive_work.is_held(archive.id)
  173. class TestPrintingElsewhere:
  174. async def test_the_file_another_print_reads_is_kept(self, test_engine, db_session, printer_factory, files):
  175. printer = await printer_factory()
  176. other = await printer_factory(name="Other")
  177. archive = await _archived(db_session, printer.id, files, status="printing")
  178. old_file = files / archive.file_path
  179. old_hash = archive.content_hash
  180. db_session.add(
  181. PrintLogEntry(
  182. archive_id=archive.id,
  183. printer_id=other.id,
  184. status=RUN_OPEN,
  185. print_name=archive.print_name,
  186. started_at=datetime.now(timezone.utc),
  187. )
  188. )
  189. await db_session.commit()
  190. await _send_again(test_engine, printer, files)
  191. (row,) = await _archives(db_session)
  192. assert (row.content_hash, row.file_path) == (old_hash, archive.file_path)
  193. assert old_file.exists()
  194. assert len(await _open_runs(db_session, archive.id)) == 2
  195. class TestName:
  196. async def test_a_name_the_file_gave_follows_the_file(self, test_engine, db_session, printer_factory, files):
  197. printer = await printer_factory()
  198. archive = await _archived(db_session, printer.id, files, name="Old_name.3mf")
  199. assert archive.print_name == "Old_name"
  200. await _send_again(test_engine, printer, files)
  201. (row,) = await _archives(db_session)
  202. assert row.print_name == "Bracket_plate_1" # the name of the file the printer had
  203. async def test_a_name_set_by_hand_stays(self, test_engine, db_session, printer_factory, files):
  204. printer = await printer_factory()
  205. await _archived(db_session, printer.id, files, name="Old_name.3mf", print_name="Wall bracket, left")
  206. await _send_again(test_engine, printer, files)
  207. (row,) = await _archives(db_session)
  208. assert row.print_name == "Wall bracket, left"
  209. class TestWhatTheArchiveKeeps:
  210. async def test_the_saved_ams_pick_stays(self, test_engine, db_session, printer_factory, files):
  211. printer = await printer_factory()
  212. pick = {"mapping": [0, 2], "printer_id": printer.id}
  213. await _archived(db_session, printer.id, files, extra_data={"slicer_ams_mapping": pick})
  214. await _send_again(test_engine, printer, files)
  215. (row,) = await _archives(db_session)
  216. assert row.extra_data["slicer_ams_mapping"] == pick
  217. async def test_a_copy_that_fails_its_check_leaves_the_old_file(
  218. self, test_engine, db_session, printer_factory, files
  219. ):
  220. """Same file name: the copy goes to a temporary name first, so a
  221. truncated one never lands on the archive's file."""
  222. printer = await printer_factory()
  223. archive = await _archived(db_session, printer.id, files, name="Bracket_plate_1.gcode.3mf")
  224. old_file = files / archive.file_path
  225. old_bytes = old_file.read_bytes()
  226. def truncated(_src, dst, *_args, **_kwargs):
  227. dst.write_bytes(b"not a zip")
  228. with patch("backend.app.services.archive._copy_and_fsync", side_effect=truncated):
  229. await _send_again(test_engine, printer, files)
  230. (row,) = await _archives(db_session)
  231. assert (row.file_path, row.content_hash) == (archive.file_path, archive.content_hash)
  232. assert old_file.read_bytes() == old_bytes
  233. assert len(await _open_runs(db_session, archive.id)) == 1
  234. async def test_a_file_a_queue_dispatch_holds_is_kept(self, test_engine, db_session, printer_factory, files):
  235. """A queue dispatch holding the archive uploads or prints its file: the
  236. resend adds its run but does not take the file away under it."""
  237. printer = await printer_factory()
  238. archive = await _archived(db_session, printer.id, files)
  239. old_file = files / archive.file_path
  240. archive_work.hold(archive.id)
  241. try:
  242. await _send_again(test_engine, printer, files)
  243. assert archive_work.is_held(archive.id), "the dispatch's hold is its own"
  244. finally:
  245. archive_work.release(archive.id)
  246. (row,) = await _archives(db_session)
  247. assert row.file_path == archive.file_path
  248. assert old_file.exists()
  249. assert len(await _open_runs(db_session, archive.id)) == 1
  250. class TestEvents:
  251. async def test_a_joined_archive_is_updated_not_created(self, test_engine, db_session, printer_factory, files):
  252. printer = await printer_factory()
  253. archive = await _archived(db_session, printer.id, files)
  254. mocks = await _send_again(test_engine, printer, files)
  255. mocks["ws_manager"].send_archive_created.assert_not_awaited()
  256. mocks["mqtt_relay"].on_archive_created.assert_not_awaited()
  257. (event,), _ = mocks["ws_manager"].send_archive_updated.await_args
  258. assert event["id"] == archive.id
  259. async def test_a_new_archive_is_created(self, test_engine, db_session, printer_factory, files):
  260. printer = await printer_factory()
  261. mocks = await _send_again(test_engine, printer, files)
  262. mocks["ws_manager"].send_archive_created.assert_awaited_once()
  263. mocks["mqtt_relay"].on_archive_created.assert_awaited_once()
  264. class TestPrinterDeleted:
  265. async def test_a_joined_archive_is_not_removed_as_one_just_written(
  266. self, test_engine, db_session, printer_factory, files
  267. ):
  268. """The check after writing removes an archive this start wrote for a
  269. printer deleted meanwhile. A joined archive is not this start's: it
  270. holds earlier runs, and its adoption checked the printer itself."""
  271. printer = await printer_factory()
  272. archive = await _archived(db_session, printer.id, files)
  273. drop = AsyncMock(return_value=True)
  274. with patch("backend.app.main._drop_archive_of_deleted_printer", drop):
  275. await _send_again(test_engine, printer, files)
  276. drop.assert_not_awaited()
  277. (row,) = await _archives(db_session)
  278. assert row.id == archive.id
  279. assert len(await _open_runs(db_session, archive.id)) == 1