test_gcode_hash_paths_1634.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. """The G-code fingerprint every archive keeps (#1634).
  2. A print sent from the slicer joins the archive with the same G-code, so each
  3. way an archive gets a file reads it: a new archive, a fallback given its 3MF, a
  4. re-slice, a merge survivor that takes over a file. A restore has no file and
  5. takes the backup's; archives made before are read once after startup. All of
  6. them read it off the event loop: unzipping a large 3MF takes seconds.
  7. """
  8. from __future__ import annotations
  9. import json
  10. import zipfile
  11. from types import SimpleNamespace
  12. from unittest.mock import patch
  13. import pytest
  14. from sqlalchemy import delete, select
  15. from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
  16. from backend.app.core.config import settings as app_settings
  17. from backend.app.models.archive import PrintArchive
  18. from backend.app.services import archive_gcode_hash
  19. from backend.app.services.archive_gcode_hash import NO_GCODE, fill_gcode_hashes, gcode_fingerprint
  20. pytestmark = pytest.mark.integration
  21. DIR = "archives/gcode"
  22. GCODE = b"; HEADER_BLOCK_START\nG28\nG1 X10 Y10 F3000\n"
  23. def _three_mf(path, plates: dict[int, bytes] | None = None, *, model: str = "<model/>", when=(2026, 1, 1, 0, 0, 0)):
  24. """A sliced 3MF; ``when`` and ``model`` stand for what a new export changes."""
  25. path.parent.mkdir(parents=True, exist_ok=True)
  26. with zipfile.ZipFile(path, "w") as zf:
  27. zf.writestr(zipfile.ZipInfo("3D/3dmodel.model", date_time=when), model)
  28. for number, gcode in (plates if plates is not None else {1: GCODE}).items():
  29. zf.writestr(zipfile.ZipInfo(f"Metadata/plate_{number}.gcode", date_time=when), gcode)
  30. return path
  31. @pytest.fixture
  32. def files(tmp_path, monkeypatch):
  33. monkeypatch.setattr(app_settings, "base_dir", tmp_path)
  34. monkeypatch.setattr(app_settings, "archive_dir", tmp_path / "archive")
  35. return tmp_path
  36. @pytest.fixture
  37. def off_the_loop(monkeypatch):
  38. """Records what is read through asyncio.to_thread."""
  39. import asyncio
  40. calls: list = []
  41. real = asyncio.to_thread
  42. async def to_thread(fn, *args, **kwargs):
  43. calls.append(fn)
  44. return await real(fn, *args, **kwargs)
  45. monkeypatch.setattr(asyncio, "to_thread", to_thread)
  46. return calls
  47. class TestFingerprint:
  48. def test_a_new_export_of_the_same_slice_has_the_same_fingerprint(self, tmp_path):
  49. first = _three_mf(tmp_path / "a.3mf")
  50. again = _three_mf(tmp_path / "b.3mf", model="<model changed='1'/>", when=(2026, 2, 2, 12, 0, 0))
  51. assert first.read_bytes() != again.read_bytes()
  52. assert gcode_fingerprint(first) == gcode_fingerprint(again)
  53. def test_another_slice_has_another(self, tmp_path):
  54. first = _three_mf(tmp_path / "a.3mf")
  55. resliced = _three_mf(tmp_path / "b.3mf", {1: GCODE + b"G1 Z0.2\n"})
  56. assert gcode_fingerprint(first) != gcode_fingerprint(resliced)
  57. def test_the_same_gcode_on_another_plate_is_another_file(self, tmp_path):
  58. assert gcode_fingerprint(_three_mf(tmp_path / "a.3mf", {1: GCODE})) != gcode_fingerprint(
  59. _three_mf(tmp_path / "b.3mf", {2: GCODE})
  60. )
  61. def test_every_plate_counts_in_plate_order(self, tmp_path):
  62. both = _three_mf(tmp_path / "a.3mf", {1: GCODE, 2: b"G28\n"})
  63. swapped = _three_mf(tmp_path / "b.3mf", {2: b"G28\n", 1: GCODE})
  64. one = _three_mf(tmp_path / "c.3mf", {1: GCODE})
  65. assert gcode_fingerprint(both) == gcode_fingerprint(swapped)
  66. assert gcode_fingerprint(both) != gcode_fingerprint(one)
  67. def test_a_plain_gcode_file_is_its_bytes(self, tmp_path):
  68. import hashlib
  69. path = tmp_path / "cube.gcode"
  70. path.write_bytes(GCODE)
  71. assert gcode_fingerprint(path) == hashlib.sha256(GCODE).hexdigest()
  72. @pytest.mark.parametrize(
  73. "write",
  74. [
  75. lambda p: _three_mf(p, {}), # a project, not sliced
  76. lambda p: p.write_bytes(b"PK\x03\x04 not really a zip"),
  77. lambda p: p.write_bytes(b"solid cube\nendsolid\n"),
  78. ],
  79. )
  80. def test_a_file_without_readable_gcode_has_none(self, tmp_path, write):
  81. path = tmp_path / "f.3mf"
  82. write(path)
  83. assert gcode_fingerprint(path) is None
  84. class TestNewArchives:
  85. async def test_an_archived_print_keeps_its_fingerprint(self, files, db_session, printer_factory, off_the_loop):
  86. from backend.app.services.archive import ArchiveService
  87. printer = await printer_factory()
  88. source = _three_mf(files / "upload" / "a.gcode.3mf")
  89. archive = await ArchiveService(db_session).archive_print(printer.id, source)
  90. assert archive.gcode_hash == gcode_fingerprint(source)
  91. assert gcode_fingerprint in off_the_loop
  92. async def test_a_file_without_gcode_is_marked_read(self, files, db_session, printer_factory):
  93. from backend.app.services.archive import ArchiveService
  94. printer = await printer_factory()
  95. source = _three_mf(files / "upload" / "project.3mf", {})
  96. archive = await ArchiveService(db_session).archive_print(printer.id, source)
  97. assert archive.gcode_hash == NO_GCODE
  98. async def test_a_print_given_its_3mf_later_reads_its_fingerprint(self, files, db_session, printer_factory):
  99. from backend.app.services.archive import ArchiveService
  100. printer = await printer_factory()
  101. fallback = PrintArchive(printer_id=printer.id, filename="a.3mf", file_path="", file_size=0, status="printing")
  102. db_session.add(fallback)
  103. await db_session.commit()
  104. source = _three_mf(files / "upload" / "a.gcode.3mf")
  105. archive = await ArchiveService(db_session).archive_print(printer.id, source, update_archive_id=fallback.id)
  106. assert (archive.id, archive.gcode_hash) == (fallback.id, gcode_fingerprint(source))
  107. async def test_a_reslice_reads_its_fingerprint(
  108. self, files, db_session, archive_factory, printer_factory, off_the_loop
  109. ):
  110. from backend.tests.integration.test_slice_archive_output_path_2832 import _slice, _stub_slicer
  111. printer = await printer_factory()
  112. source = await archive_factory(printer.id, print_name="Bracket", filename="Bracket.3mf")
  113. sliced = _three_mf(files / "sliced.3mf").read_bytes()
  114. # _slice stubs the slicer with a file that is not a 3MF: this one is.
  115. with patch(
  116. "backend.tests.integration.test_slice_archive_output_path_2832._stub_slicer", lambda: _stub_slicer(sliced)
  117. ):
  118. response = await _slice(db_session, source, "Bracket.3mf")
  119. archive = await db_session.get(PrintArchive, response.archive_id)
  120. assert archive.gcode_hash == gcode_fingerprint(files / "sliced.3mf")
  121. assert archive_gcode_hash.read_gcode_hash in off_the_loop
  122. class TestBackupAndRestore:
  123. async def test_a_restored_archive_keeps_its_fingerprint(self, db_session, printer_factory):
  124. """A restore brings back no file to read it from."""
  125. from backend.app.schemas.github_backup import RestoreCategory
  126. from backend.app.services.github_backup import github_backup_service
  127. from backend.app.services.github_restore import GitHubRestoreService
  128. printer = await printer_factory()
  129. db_session.add(
  130. PrintArchive(
  131. printer_id=printer.id,
  132. filename="a.3mf",
  133. file_path="archives/t/a.3mf",
  134. file_size=1,
  135. content_hash="hash-a",
  136. gcode_hash="a" * 64,
  137. print_name="A",
  138. status="completed",
  139. )
  140. )
  141. await db_session.commit()
  142. backup: dict = {}
  143. await github_backup_service._collect_archives(db_session, backup)
  144. backup = json.loads(json.dumps(backup))
  145. await db_session.execute(delete(PrintArchive))
  146. await db_session.commit()
  147. await GitHubRestoreService()._apply(db_session, backup, [RestoreCategory.ARCHIVES], False)
  148. restored = (await db_session.scalars(select(PrintArchive))).one()
  149. assert (restored.file_path, restored.gcode_hash) == ("", "a" * 64)
  150. @pytest.mark.parametrize("value", [None, "", "not-a-hash", 42])
  151. def test_a_backup_without_one_restores_none(self, value):
  152. from backend.app.services.github_restore import _gcode_hash_of
  153. assert _gcode_hash_of({"gcode_hash": value} if value is not None else {}) is None
  154. class TestFill:
  155. async def test_archives_made_before_get_theirs_once(self, files, db_session, test_engine, off_the_loop):
  156. _three_mf(files / DIR / "a.gcode.3mf")
  157. older = PrintArchive(filename="a.gcode.3mf", file_path=f"{DIR}/a.gcode.3mf", file_size=1)
  158. gone = PrintArchive(filename="gone.3mf", file_path=f"{DIR}/gone.3mf", file_size=1)
  159. read = PrintArchive(filename="r.3mf", file_path=f"{DIR}/a.gcode.3mf", file_size=1, gcode_hash=NO_GCODE)
  160. db_session.add_all([older, gone, read])
  161. await db_session.commit()
  162. sessions = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
  163. assert await fill_gcode_hashes(sessions, batch=1) == 2
  164. assert await fill_gcode_hashes(sessions) == 0
  165. for row in (older, gone, read):
  166. await db_session.refresh(row)
  167. fingerprint = gcode_fingerprint(files / DIR / "a.gcode.3mf")
  168. assert (older.gcode_hash, gone.gcode_hash, read.gcode_hash) == (fingerprint, NO_GCODE, NO_GCODE)
  169. assert archive_gcode_hash.read_gcode_hash in off_the_loop
  170. async def test_an_archive_read_meanwhile_keeps_what_was_read(self, files, db_session, test_engine, monkeypatch):
  171. racing = PrintArchive(filename="a.gcode.3mf", file_path="", file_size=0)
  172. db_session.add(racing)
  173. await db_session.commit()
  174. sessions = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
  175. async def meanwhile(fn, row):
  176. # Its 3MF recovered and read while the fill reads it.
  177. async with sessions() as other:
  178. (await other.get(PrintArchive, racing.id)).gcode_hash = "f" * 64
  179. await other.commit()
  180. return fn(row)
  181. monkeypatch.setattr(archive_gcode_hash, "asyncio", SimpleNamespace(to_thread=meanwhile))
  182. assert await fill_gcode_hashes(sessions) == 0
  183. await db_session.refresh(racing)
  184. assert racing.gcode_hash == "f" * 64