test_archive_merge.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. """Duplicate archives of one file merge into one (archive redesign P2).
  2. Same file, owner and project: the oldest archive stays and takes the others'
  3. runs, references, photos and videos; the others stay soft-deleted with an
  4. alias, their money untouched.
  5. """
  6. from __future__ import annotations
  7. import zipfile
  8. from datetime import datetime, timedelta
  9. from pathlib import Path
  10. from types import SimpleNamespace
  11. from unittest.mock import patch
  12. import pytest
  13. from sqlalchemy import select
  14. from backend.app.core.config import settings as app_settings
  15. from backend.app.models.archive import PrintArchive
  16. from backend.app.models.archive_alias import ArchiveAlias
  17. from backend.app.models.finance import WalletTransaction
  18. from backend.app.models.print_log import RUN_OPEN, PrintLogEntry
  19. from backend.app.models.print_queue import PrintQueueItem
  20. from backend.app.models.project import Project
  21. from backend.app.models.project_bom import ProjectBOMItem
  22. pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
  23. HASH = "a" * 64
  24. T1 = datetime(2026, 9, 1, 9)
  25. @pytest.fixture
  26. def files(tmp_path, monkeypatch):
  27. monkeypatch.setattr(app_settings, "base_dir", tmp_path)
  28. monkeypatch.setattr(app_settings, "archive_dir", tmp_path / "archive")
  29. return tmp_path
  30. def _dir(files, name):
  31. path = files / "archive" / "1" / name
  32. (path / "photos").mkdir(parents=True, exist_ok=True)
  33. (path / "file.3mf").write_bytes(b"3mf")
  34. return path
  35. @pytest.fixture
  36. async def dupes(db_session, printer_factory, files):
  37. """X and Y printed once each, Z printed before runs were logged; W is the
  38. same file in another project, V the same file deleted."""
  39. printer = await printer_factory(name="A")
  40. project = Project(name="Other")
  41. db_session.add(project)
  42. await db_session.flush()
  43. def archive(name, **fields):
  44. folder = _dir(files, name)
  45. row = PrintArchive(
  46. printer_id=printer.id,
  47. filename="part.3mf",
  48. print_name="part",
  49. file_path=str((folder / "file.3mf").relative_to(files)),
  50. file_size=3,
  51. content_hash=HASH,
  52. thumbnail_path=f"archive/1/{name}/thumb.png",
  53. **({"status": "completed"} | fields),
  54. )
  55. db_session.add(row)
  56. return row, folder
  57. x, x_dir = archive("x", notes="first", tags="red", started_at=T1, completed_at=T1 + timedelta(hours=1))
  58. y, y_dir = archive(
  59. "y",
  60. notes="second",
  61. tags="blue, red",
  62. is_favorite=True,
  63. started_at=T1 + timedelta(days=1),
  64. completed_at=T1 + timedelta(days=1, hours=1),
  65. confirm_requested=True,
  66. confirm_token="tok-y",
  67. photos=["finish_y.jpg"],
  68. timelapse_path="archive/1/y/y.mp4",
  69. )
  70. z, z_dir = archive(
  71. "z",
  72. status="failed",
  73. failure_reason="spaghetti",
  74. started_at=T1 + timedelta(days=2),
  75. completed_at=T1 + timedelta(days=2, hours=1),
  76. photos=["finish_z.jpg"],
  77. timelapse_path="archive/1/z/z.mp4",
  78. filament_used_grams=12.0,
  79. )
  80. w, _ = archive("w", project_id=project.id)
  81. v, _ = archive("v", deleted_at=datetime(2026, 10, 1))
  82. await db_session.flush()
  83. (y_dir / "photos" / "finish_y.jpg").write_bytes(b"y")
  84. (y_dir / "y.mp4").write_bytes(b"yy")
  85. (z_dir / "photos" / "finish_z.jpg").write_bytes(b"z")
  86. (z_dir / "z.mp4").write_bytes(b"zz")
  87. r1 = PrintLogEntry(
  88. archive_id=x.id, printer_id=printer.id, status="completed", started_at=T1, completed_at=T1 + timedelta(hours=1)
  89. )
  90. r2 = PrintLogEntry(
  91. archive_id=y.id,
  92. printer_id=printer.id,
  93. status="completed",
  94. started_at=T1 + timedelta(days=1),
  95. completed_at=T1 + timedelta(days=1, hours=1),
  96. photos=["finish_y.jpg"],
  97. timelapse_path="archive/1/y/y.mp4",
  98. thumbnail_path="archive/1/y/thumb.png",
  99. )
  100. item = PrintQueueItem(printer_id=printer.id, archive_id=y.id, status="completed", position=1)
  101. bom = ProjectBOMItem(project_id=project.id, name="part", archive_id=y.id)
  102. charge = WalletTransaction(transaction_type="print_charge", amount=-1.0, print_archive_id=y.id)
  103. db_session.add_all([r1, r2, item, bom, charge])
  104. await db_session.commit()
  105. return SimpleNamespace(
  106. x=x.id,
  107. y=y.id,
  108. z=z.id,
  109. w=w.id,
  110. v=v.id,
  111. r1=r1.id,
  112. r2=r2.id,
  113. item=item.id,
  114. bom=bom.id,
  115. charge=charge.id,
  116. x_dir=x_dir,
  117. y_dir=y_dir,
  118. z_dir=z_dir,
  119. printer=printer.id,
  120. )
  121. async def _row(db_session, model, row_id):
  122. db_session.expire_all()
  123. return await db_session.get(model, row_id)
  124. async def _runs(db_session, archive_id):
  125. db_session.expire_all()
  126. return list(
  127. await db_session.scalars(
  128. select(PrintLogEntry).where(PrintLogEntry.archive_id == archive_id).order_by(PrintLogEntry.started_at)
  129. )
  130. )
  131. async def test_the_preview_shows_the_group(async_client, dupes):
  132. response = await async_client.get("/api/v1/archives/merge-preview")
  133. assert response.status_code == 200, response.text
  134. [group] = response.json()["groups"]
  135. assert group["survivor"]["id"] == dupes.x
  136. assert [(m["id"], m["run_count"]) for m in group["merged"]] == [(dupes.y, 1), (dupes.z, 0)]
  137. assert group["skipped"] is None
  138. async def test_merging_keeps_the_oldest_with_every_print(async_client, db_session, dupes):
  139. response = await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  140. assert response.status_code == 200, response.text
  141. assert response.json() == {"merged": [{"survivor_id": dupes.x, "merged_ids": [dupes.y, dupes.z]}], "skipped": []}
  142. runs = await _runs(db_session, dupes.x)
  143. assert [r.status for r in runs] == ["completed", "completed", "failed"]
  144. from_z = runs[2]
  145. assert (from_z.failure_reason, from_z.filament_used_grams, from_z.photos) == ("spaghetti", 12.0, ["finish_z.jpg"])
  146. archive = await _row(db_session, PrintArchive, dupes.x)
  147. # The file's description, and its latest print.
  148. assert (archive.notes, archive.tags, archive.is_favorite) == ("first\n\nsecond", "red, blue", True)
  149. assert (archive.status, archive.failure_reason) == ("failed", "spaghetti")
  150. assert archive.photos == ["finish_y.jpg", "finish_z.jpg"]
  151. async def test_the_others_stay_deleted_with_their_money(async_client, db_session, dupes):
  152. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  153. for merged in (dupes.y, dupes.z):
  154. assert (await _row(db_session, PrintArchive, merged)).deleted_at is not None
  155. assert (await db_session.get(ArchiveAlias, merged)).archive_id == dupes.x
  156. assert (await _row(db_session, WalletTransaction, dupes.charge)).print_archive_id == dupes.y
  157. assert (await _row(db_session, PrintArchive, dupes.w)).deleted_at is None
  158. async def test_their_references_move(async_client, db_session, dupes):
  159. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  160. assert (await _row(db_session, PrintQueueItem, dupes.item)).archive_id == dupes.x
  161. assert (await _row(db_session, ProjectBOMItem, dupes.bom)).archive_id == dupes.x
  162. async def test_their_photos_and_videos_move_into_its_folder(async_client, db_session, dupes):
  163. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  164. assert (dupes.x_dir / "photos" / "finish_y.jpg").read_bytes() == b"y"
  165. assert (dupes.x_dir / "y.mp4").read_bytes() == b"yy"
  166. r2 = await _row(db_session, PrintLogEntry, dupes.r2)
  167. assert (r2.timelapse_path, r2.thumbnail_path) == ("archive/1/x/y.mp4", "archive/1/x/thumb.png")
  168. runs = await _runs(db_session, dupes.x)
  169. assert runs[2].timelapse_path == "archive/1/x/z.mp4"
  170. assert (await _row(db_session, PrintArchive, dupes.x)).timelapse_path == "archive/1/x/z.mp4"
  171. assert not dupes.y_dir.exists() and not dupes.z_dir.exists()
  172. async def test_a_merged_id_finds_the_archive(async_client, dupes):
  173. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  174. response = await async_client.get(f"/api/v1/archives/{dupes.y}")
  175. assert response.status_code == 200, response.text
  176. assert response.json()["id"] == dupes.x
  177. async def test_its_one_tap_link_still_answers_its_print(async_client, db_session, dupes):
  178. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  179. response = await async_client.post("/api/v1/archives/confirm/tok-y/good")
  180. assert response.status_code == 200, response.text
  181. assert (await _row(db_session, PrintLogEntry, dupes.r2)).user_verdict == "good"
  182. async def test_a_group_at_work_waits(async_client, db_session, dupes):
  183. db_session.add(PrintLogEntry(archive_id=dupes.y, printer_id=dupes.printer, status=RUN_OPEN, started_at=T1))
  184. await db_session.commit()
  185. [group] = (await async_client.get("/api/v1/archives/merge-preview")).json()["groups"]
  186. assert group["skipped"] == "busy"
  187. response = await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  188. assert response.json() == {"merged": [], "skipped": [{"survivor_id": dupes.x, "reason": "busy"}]}
  189. assert (await _row(db_session, PrintArchive, dupes.y)).deleted_at is None
  190. async def test_a_group_just_printed_waits(async_client, db_session, dupes):
  191. """Its timelapse and finish photo may still be on their way."""
  192. from backend.app.utils.local_time import utcnow_naive
  193. run = await _row(db_session, PrintLogEntry, dupes.r2)
  194. run.completed_at = utcnow_naive() - timedelta(minutes=5)
  195. await db_session.commit()
  196. [group] = (await async_client.get("/api/v1/archives/merge-preview")).json()["groups"]
  197. assert group["skipped"] == "busy"
  198. async def test_a_queued_group_waits(async_client, db_session, dupes):
  199. db_session.add(PrintQueueItem(printer_id=dupes.printer, archive_id=dupes.z, status="pending", position=2))
  200. await db_session.commit()
  201. [group] = (await async_client.get("/api/v1/archives/merge-preview")).json()["groups"]
  202. assert group["skipped"] == "busy"
  203. async def test_a_survivor_printed_before_runs_keeps_its_print(async_client, db_session, dupes):
  204. await db_session.delete(await _row(db_session, PrintLogEntry, dupes.r1))
  205. await db_session.commit()
  206. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  207. runs = await _runs(db_session, dupes.x)
  208. assert [r.started_at for r in runs] == [T1, T1 + timedelta(days=1), T1 + timedelta(days=2)]
  209. async def test_a_failed_merge_leaves_everything(async_client, db_session, dupes):
  210. with patch("backend.app.services.print_log.mirror_latest_run", side_effect=RuntimeError("boom")):
  211. response = await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  212. assert response.json() == {"merged": [], "skipped": [{"survivor_id": dupes.x, "reason": "failed"}]}
  213. assert (await _row(db_session, PrintArchive, dupes.y)).deleted_at is None
  214. assert (await _row(db_session, PrintLogEntry, dupes.r2)).archive_id == dupes.y
  215. assert not (dupes.x_dir / "y.mp4").exists()
  216. assert (dupes.y_dir / "y.mp4").exists()
  217. async def test_merging_needs_delete_all(async_client, db_session, dupes):
  218. from backend.tests.integration.test_archives_last_per_printer import _admin_token, _auth, _user
  219. admin = await _admin_token(async_client)
  220. jwt, _ = await _user(async_client, admin, "editor", ["archives:read_all", "archives:update_all"])
  221. response = await async_client.post("/api/v1/archives/merge", headers=_auth(jwt), json={"survivor_ids": [dupes.x]})
  222. assert response.status_code == 403
  223. async def test_a_scoped_user_merges_only_archives_wholly_theirs(async_client, db_session, printer_factory, dupes):
  224. from backend.tests.integration.test_archives_last_per_printer import _admin_token, _auth, _user
  225. other = await printer_factory(name="B")
  226. db_session.add(PrintLogEntry(archive_id=dupes.z, printer_id=other.id, status="completed", started_at=T1))
  227. await db_session.commit()
  228. admin = await _admin_token(async_client)
  229. jwt, _ = await _user(
  230. async_client,
  231. admin,
  232. "scoped",
  233. ["archives:read_all", "archives:update_all", "archives:delete_all"],
  234. printer_ids=[dupes.printer],
  235. )
  236. [group] = (await async_client.get("/api/v1/archives/merge-preview", headers=_auth(jwt))).json()["groups"]
  237. assert [m["id"] for m in group["merged"]] == [dupes.y]
  238. async def test_a_group_tracked_by_spoolman_waits(async_client, db_session, dupes):
  239. from backend.app.models.active_print_spoolman import ActivePrintSpoolman
  240. db_session.add(ActivePrintSpoolman(printer_id=dupes.printer, archive_id=dupes.y, ams_trays={}))
  241. await db_session.commit()
  242. [group] = (await async_client.get("/api/v1/archives/merge-preview")).json()["groups"]
  243. assert group["skipped"] == "busy"
  244. async def test_a_group_printing_waits(async_client, db_session, dupes):
  245. (await _row(db_session, PrintArchive, dupes.z)).status = "printing"
  246. await db_session.commit()
  247. [group] = (await async_client.get("/api/v1/archives/merge-preview")).json()["groups"]
  248. assert group["skipped"] == "busy"
  249. async def test_an_archive_merged_later_passes_on_its_aliases(db_session, dupes):
  250. """Y went into X; X later goes into O: Y's id finds O in one step."""
  251. from backend.app.services.archive_merge import merge_group, resolve_alias
  252. assert await merge_group(db_session, dupes.x, [dupes.y]) is None
  253. # W takes X in, as a merge keeping W would (same file, same project now).
  254. w = await _row(db_session, PrintArchive, dupes.w)
  255. w.project_id = None
  256. await db_session.commit()
  257. assert await merge_group(db_session, dupes.w, [dupes.x]) is None
  258. assert await resolve_alias(db_session, dupes.y) == dupes.w
  259. assert await resolve_alias(db_session, dupes.x) == dupes.w
  260. async def test_a_video_only_its_archive_held_goes_with_its_print(async_client, db_session, dupes):
  261. """Y's print was logged before runs kept their own video: Y alone held it."""
  262. r2 = await _row(db_session, PrintLogEntry, dupes.r2)
  263. r2.timelapse_path, r2.photos = None, None
  264. await db_session.commit()
  265. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  266. assert (await _row(db_session, PrintLogEntry, dupes.r2)).timelapse_path == "archive/1/x/y.mp4"
  267. async def test_the_survivors_own_old_video_stays_with_its_print(async_client, db_session, files, dupes):
  268. (dupes.x_dir / "x.mp4").write_bytes(b"xx")
  269. (await _row(db_session, PrintArchive, dupes.x)).timelapse_path = "archive/1/x/x.mp4"
  270. await db_session.commit()
  271. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  272. assert (await _row(db_session, PrintLogEntry, dupes.r1)).timelapse_path == "archive/1/x/x.mp4"
  273. async def test_the_survivors_own_one_tap_link_answers_its_own_print(async_client, db_session, dupes):
  274. x = await _row(db_session, PrintArchive, dupes.x)
  275. x.confirm_requested, x.confirm_token = True, "tok-x"
  276. await db_session.commit()
  277. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  278. response = await async_client.post("/api/v1/archives/confirm/tok-x/reject")
  279. assert response.status_code == 200, response.text
  280. assert (await _row(db_session, PrintLogEntry, dupes.r1)).user_verdict == "reject"
  281. assert (await _row(db_session, PrintLogEntry, dupes.r2)).user_verdict is None
  282. async def test_a_folder_shared_with_the_survivor_stays(async_client, db_session, files, dupes):
  283. """Archived in the same second under the same name: one folder."""
  284. y = await _row(db_session, PrintArchive, dupes.y)
  285. y.file_path = "archive/1/x/file.3mf"
  286. await db_session.commit()
  287. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  288. assert (dupes.x_dir / "file.3mf").exists()
  289. # Nothing of the survivor's for a later purge of the merged row to remove.
  290. assert (await _row(db_session, PrintArchive, dupes.y)).file_path == ""
  291. async def test_a_folder_another_archive_uses_stays(async_client, db_session, files, dupes):
  292. other = PrintArchive(filename="o.3mf", file_path="archive/1/y/other.3mf", file_size=1, created_by_id=None)
  293. db_session.add(other)
  294. await db_session.commit()
  295. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  296. assert dupes.y_dir.exists()
  297. async def test_different_source_files_wait_for_the_user(async_client, db_session, files, dupes):
  298. for archive_id, name, content in ((dupes.x, "x", b"one"), (dupes.y, "y", b"two")):
  299. (files / "archive" / "1" / name / "src.3mf").write_bytes(content)
  300. (await _row(db_session, PrintArchive, archive_id)).source_3mf_path = f"archive/1/{name}/src.3mf"
  301. await db_session.commit()
  302. [group] = (await async_client.get("/api/v1/archives/merge-preview")).json()["groups"]
  303. assert group["skipped"] == "conflicting_files"
  304. async def test_the_same_source_file_twice_merges(async_client, db_session, files, dupes):
  305. for archive_id, name in ((dupes.x, "x"), (dupes.y, "y")):
  306. (files / "archive" / "1" / name / "src.3mf").write_bytes(b"same")
  307. (await _row(db_session, PrintArchive, archive_id)).source_3mf_path = f"archive/1/{name}/src.3mf"
  308. await db_session.commit()
  309. [group] = (await async_client.get("/api/v1/archives/merge-preview")).json()["groups"]
  310. assert group["skipped"] is None
  311. @pytest.mark.parametrize("held_by", ["dispatch", "active_print", "pipeline", "timelapse_job"])
  312. async def test_an_archive_held_by_work_in_flight_waits(async_client, db_session, dupes, held_by, monkeypatch):
  313. from backend.app import main as main_module
  314. from backend.app.services import archive_work, timelapse_jobs
  315. if held_by == "dispatch":
  316. archive_work.hold(dupes.y)
  317. elif held_by == "active_print":
  318. monkeypatch.setitem(main_module._active_prints, (dupes.printer, "part.3mf"), dupes.y)
  319. elif held_by == "pipeline":
  320. from backend.app.models.pipeline_run import PipelineRun
  321. db_session.add(PipelineRun(source_archive_id=dupes.y, status="slicing"))
  322. await db_session.commit()
  323. else:
  324. monkeypatch.setitem(timelapse_jobs._jobs, dupes.y, SimpleNamespace(status=timelapse_jobs.RUNNING))
  325. [group] = (await async_client.get("/api/v1/archives/merge-preview")).json()["groups"]
  326. assert group["skipped"] == "busy"
  327. async def test_a_held_archive_is_left_alone_even_after_the_preview(db_session, dupes):
  328. """Held between the group search and its merge: a dispatch chose it."""
  329. from backend.app.services import archive_merge, archive_work
  330. real_find = archive_merge.find_groups
  331. async def find_then_hold(db, visible):
  332. groups = await real_find(db, visible)
  333. archive_work.hold(dupes.y)
  334. return groups
  335. with patch.object(archive_merge, "find_groups", find_then_hold):
  336. merged, skipped = await archive_merge.merge(db_session, [dupes.x], [])
  337. assert (merged, skipped) == ([], [{"survivor_id": dupes.x, "reason": "busy"}])
  338. async def test_an_edit_made_after_the_preview_is_kept(db_session, test_engine, dupes):
  339. from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
  340. from backend.app.services.archive_merge import merge_group
  341. x = await _row(db_session, PrintArchive, dupes.x)
  342. assert x.notes == "first"
  343. async with async_sessionmaker(test_engine, class_=AsyncSession)() as other:
  344. (await other.get(PrintArchive, dupes.x)).notes = "edited"
  345. await other.commit()
  346. assert await merge_group(db_session, dupes.x, [dupes.y, dupes.z]) is None
  347. assert (await _row(db_session, PrintArchive, dupes.x)).notes == "edited\n\nsecond"
  348. async def test_a_dispatch_leaves_an_archive_being_merged(db_session, dupes):
  349. from backend.app.services import archive_work
  350. with archive_work.merging([dupes.y]) as marked:
  351. assert marked
  352. with archive_work.merging([dupes.y]) as again:
  353. assert not again
  354. async def test_a_group_that_got_to_work_after_the_preview_waits(db_session, dupes):
  355. """Queued between the group search and its merge."""
  356. from backend.app.services import archive_merge
  357. real_find = archive_merge.find_groups
  358. async def find_then_queue(db, visible):
  359. groups = await real_find(db, visible)
  360. db.add(PrintQueueItem(printer_id=dupes.printer, archive_id=dupes.z, status="pending", position=3))
  361. await db.commit()
  362. return groups
  363. with patch.object(archive_merge, "find_groups", find_then_queue):
  364. merged, skipped = await archive_merge.merge(db_session, [dupes.x], [])
  365. assert (merged, skipped) == ([], [{"survivor_id": dupes.x, "reason": "busy"}])
  366. async def test_a_failing_group_leaves_the_next_to_merge(db_session, files, dupes):
  367. from backend.app.services import archive_merge
  368. first = PrintArchive(filename="o.3mf", file_path="archive/1/o1/file.3mf", file_size=1, content_hash="b" * 64)
  369. second = PrintArchive(filename="o.3mf", file_path="archive/1/o2/file.3mf", file_size=1, content_hash="b" * 64)
  370. db_session.add_all([first, second])
  371. await db_session.commit()
  372. first_id, second_id = first.id, second.id
  373. real = archive_merge._merge
  374. calls = []
  375. async def fail_the_first(db, survivor_id, merged_ids, files_):
  376. calls.append(survivor_id)
  377. if len(calls) == 1:
  378. await db.flush()
  379. raise RuntimeError("boom")
  380. return await real(db, survivor_id, merged_ids, files_)
  381. with patch.object(archive_merge, "_merge", fail_the_first):
  382. merged, skipped = await archive_merge.merge(db_session, [dupes.x, first_id], [])
  383. assert skipped == [{"survivor_id": dupes.x, "reason": "failed"}]
  384. assert merged == [{"survivor_id": first_id, "merged_ids": [second_id]}]
  385. async def test_a_video_attached_by_hand_to_an_unprinted_archive_stays(async_client, db_session, files, dupes):
  386. """Y was uploaded, never printed, and given a video by hand: the group's
  387. only video, so the archive keeps it."""
  388. await db_session.delete(await _row(db_session, PrintLogEntry, dupes.r2))
  389. await db_session.commit()
  390. for archive_id in (dupes.y, dupes.z):
  391. (await _row(db_session, PrintArchive, archive_id)).status = "archived"
  392. await db_session.commit()
  393. (await _row(db_session, PrintArchive, dupes.z)).timelapse_path = None
  394. await db_session.commit()
  395. response = await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  396. assert response.json()["skipped"] == [], response.text
  397. assert (await _row(db_session, PrintArchive, dupes.x)).timelapse_path == "archive/1/x/y.mp4"
  398. assert (dupes.x_dir / "y.mp4").exists()
  399. async def test_a_video_attached_by_hand_beside_another_waits(async_client, db_session, files, dupes):
  400. """Z's print brings its own video: the archive could show only one."""
  401. await db_session.delete(await _row(db_session, PrintLogEntry, dupes.r2))
  402. await db_session.commit()
  403. (await _row(db_session, PrintArchive, dupes.y)).status = "archived"
  404. await db_session.commit()
  405. [group] = (await async_client.get("/api/v1/archives/merge-preview")).json()["groups"]
  406. response = await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  407. assert group["skipped"] == "conflicting_files"
  408. assert response.json()["skipped"] == [{"survivor_id": dupes.x, "reason": "conflicting_files"}]
  409. async def test_the_survivors_lost_file_comes_from_a_duplicate(async_client, db_session, files, dupes):
  410. (dupes.x_dir / "file.3mf").unlink()
  411. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  412. archive = await _row(db_session, PrintArchive, dupes.x)
  413. assert (files / archive.file_path).read_bytes() == b"3mf"
  414. async def test_the_survivor_reads_its_plates_from_the_file_it_takes_over(async_client, db_session, files, dupes):
  415. from backend.tests.integration.test_plate_estimates_3375 import PLATES, _three_mf
  416. (dupes.x_dir / "file.3mf").unlink()
  417. for folder in (dupes.y_dir, dupes.z_dir):
  418. _three_mf(folder / "file.3mf")
  419. survivor = await db_session.get(PrintArchive, dupes.x)
  420. survivor.plate_estimates = {} # read while its file was missing
  421. await db_session.commit()
  422. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  423. archive = await _row(db_session, PrintArchive, dupes.x)
  424. assert archive.plate_estimates == PLATES
  425. async def test_the_survivor_reads_the_gcode_of_the_file_it_takes_over(async_client, db_session, files, dupes):
  426. """#1634: a slice sent again from the slicer finds the survivor by it."""
  427. from backend.app.services.archive_gcode_hash import NO_GCODE, gcode_fingerprint
  428. (dupes.x_dir / "file.3mf").unlink()
  429. for folder in (dupes.y_dir, dupes.z_dir):
  430. with zipfile.ZipFile(folder / "file.3mf", "w") as zf:
  431. zf.writestr("Metadata/plate_1.gcode", "G28\n")
  432. survivor = await db_session.get(PrintArchive, dupes.x)
  433. survivor.gcode_hash = NO_GCODE # read while its file was missing
  434. await db_session.commit()
  435. await async_client.post("/api/v1/archives/merge", json={"survivor_ids": [dupes.x]})
  436. archive = await _row(db_session, PrintArchive, dupes.x)
  437. assert archive.gcode_hash == gcode_fingerprint(files / archive.file_path)
  438. assert archive.gcode_hash not in (None, NO_GCODE)
  439. async def test_a_copy_that_fails_leaves_no_partial_file(files):
  440. from backend.app.services.archive_merge import _Files
  441. source = files / "big.mp4"
  442. source.write_bytes(b"x" * 100)
  443. target_dir = files / "target"
  444. def partial_copy(src, dst):
  445. Path(dst).write_bytes(b"x")
  446. raise OSError("disk full")
  447. with (
  448. patch("backend.app.services.archive_merge.os.link", side_effect=OSError("cross-device")),
  449. patch("backend.app.services.archive_merge.shutil.copy2", side_effect=partial_copy),
  450. pytest.raises(OSError),
  451. ):
  452. _Files().place(source, target_dir)
  453. assert list(target_dir.iterdir()) == []
  454. async def test_an_avi_conversion_holds_its_archive_from_the_spawn(db_session, files, dupes):
  455. from backend.app.services import archive_work
  456. from backend.app.services.archive import ArchiveService
  457. spawned = []
  458. with (
  459. patch("backend.app.services.archive.spawn_background_task", side_effect=lambda c, **_: spawned.append(c)),
  460. patch("backend.app.services.archive._announce_timelapse"),
  461. ):
  462. assert await ArchiveService(db_session).attach_timelapse(dupes.x, b"avi", "v.avi")
  463. assert archive_work.is_held(dupes.x)
  464. for coro in spawned:
  465. coro.close()
  466. async def test_the_count_offers_every_group(async_client, db_session, dupes):
  467. """Also one at work: the count only offers the merge."""
  468. db_session.add(PrintLogEntry(archive_id=dupes.y, printer_id=dupes.printer, status=RUN_OPEN, started_at=T1))
  469. other = [
  470. PrintArchive(filename="o.3mf", file_path=f"archive/1/o{i}/f.3mf", file_size=1, content_hash="c" * 64)
  471. for i in range(2)
  472. ]
  473. db_session.add_all(other)
  474. await db_session.commit()
  475. response = await async_client.get("/api/v1/archives/merge-count")
  476. assert response.status_code == 200, response.text
  477. assert response.json() == {"groups": 2}
  478. async def test_the_count_needs_delete_all(async_client, dupes):
  479. from backend.tests.integration.test_archives_last_per_printer import _admin_token, _auth, _user
  480. admin = await _admin_token(async_client)
  481. jwt, _ = await _user(async_client, admin, "counter", ["archives:read_all", "archives:update_all"])
  482. assert (await async_client.get("/api/v1/archives/merge-count", headers=_auth(jwt))).status_code == 403