test_library_slice_api.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. """Integration tests for the slice-via-API flow.
  2. Routes under test:
  3. - POST /library/files/{id}/slice (returns 202 + job_id; bg task does the work)
  4. - POST /archives/{id}/slice (same shape; result lands in archives table)
  5. - GET /slice-jobs/{id} (poll for terminal state)
  6. The synchronous validation paths (404 missing source, 400 wrong file type)
  7. are tested directly. The bg-task paths poll until the job finishes and then
  8. assert on the captured state.
  9. """
  10. from __future__ import annotations
  11. import asyncio
  12. import io
  13. import json
  14. import zipfile
  15. from collections.abc import Callable
  16. import httpx
  17. import pytest
  18. from httpx import AsyncClient
  19. from backend.app.core.config import settings as app_settings
  20. from backend.app.models.library import LibraryFile
  21. from backend.app.models.local_preset import LocalPreset
  22. from backend.app.models.settings import Settings as SettingsModel
  23. from backend.app.services import slicer_api as slicer_api_module
  24. from backend.app.services.slice_dispatch import slice_dispatch
  25. # ---------------------------------------------------------------------------
  26. # Helpers
  27. # ---------------------------------------------------------------------------
  28. def _make_3mf_with_settings(settings_payload: dict | None = None) -> bytes:
  29. """Build a tiny in-memory 3MF zip that has a `Metadata/project_settings.config`
  30. entry. Used to verify the strip-before-forwarding behavior."""
  31. buf = io.BytesIO()
  32. with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
  33. zf.writestr("3D/3dmodel.model", "<model/>")
  34. zf.writestr(
  35. "Metadata/project_settings.config",
  36. json.dumps(settings_payload or {"prime_tower_brim_width": "-1"}),
  37. )
  38. return buf.getvalue()
  39. def _install_mock_sidecar(handler: Callable[[httpx.Request], httpx.Response]) -> httpx.AsyncClient:
  40. """Pin a MockTransport-backed httpx client onto the slicer_api singleton
  41. so per-request `SlicerApiService` instances reuse it instead of opening
  42. a real connection."""
  43. client = httpx.AsyncClient(transport=httpx.MockTransport(handler), timeout=10.0)
  44. slicer_api_module.set_shared_http_client(client)
  45. return client
  46. async def _wait_for_job(client: AsyncClient, job_id: int, timeout: float = 5.0) -> dict:
  47. """Poll `/api/v1/slice-jobs/{id}` until the job hits a terminal state.
  48. The dispatcher runs work as an asyncio task on the same event loop, so
  49. poll-with-sleep here is enough — a few yields and the task finishes.
  50. """
  51. deadline = asyncio.get_event_loop().time() + timeout
  52. while asyncio.get_event_loop().time() < deadline:
  53. r = await client.get(f"/api/v1/slice-jobs/{job_id}")
  54. if r.status_code != 200:
  55. raise AssertionError(f"slice-jobs poll failed: {r.status_code} {r.text}")
  56. body = r.json()
  57. if body["status"] in ("completed", "failed"):
  58. return body
  59. await asyncio.sleep(0.05)
  60. raise AssertionError(f"slice job {job_id} did not finish in {timeout}s")
  61. # ---------------------------------------------------------------------------
  62. # Fixtures
  63. # ---------------------------------------------------------------------------
  64. @pytest.fixture
  65. async def slice_test_setup(db_session, tmp_path):
  66. """Source LibraryFile + 3 LocalPresets + preferred_slicer=orcaslicer."""
  67. storage_dir = tmp_path / "library" / "files"
  68. storage_dir.mkdir(parents=True, exist_ok=True)
  69. src_path = storage_dir / "Cube.stl"
  70. src_path.write_bytes(b"solid Cube\nendsolid\n")
  71. original_base_dir = app_settings.base_dir
  72. app_settings.base_dir = tmp_path
  73. src_file = LibraryFile(
  74. filename="Cube.stl",
  75. file_path=str(src_path.relative_to(tmp_path)),
  76. file_type="stl",
  77. file_size=src_path.stat().st_size,
  78. )
  79. db_session.add(src_file)
  80. presets = {}
  81. for kind in ("printer", "process", "filament"):
  82. p = LocalPreset(
  83. name=f"Test {kind}",
  84. preset_type=kind,
  85. source="orcaslicer",
  86. setting=json.dumps({"name": f"Test {kind}", "type": kind}),
  87. )
  88. db_session.add(p)
  89. presets[kind] = p
  90. db_session.add(SettingsModel(key="preferred_slicer", value="orcaslicer"))
  91. await db_session.commit()
  92. for p in presets.values():
  93. await db_session.refresh(p)
  94. await db_session.refresh(src_file)
  95. yield {
  96. "src_file_id": src_file.id,
  97. "printer_id": presets["printer"].id,
  98. "process_id": presets["process"].id,
  99. "filament_id": presets["filament"].id,
  100. "tmp_path": tmp_path,
  101. }
  102. app_settings.base_dir = original_base_dir
  103. slicer_api_module.set_shared_http_client(None)
  104. # ---------------------------------------------------------------------------
  105. # POST /library/files/{id}/slice — synchronous validation paths
  106. # ---------------------------------------------------------------------------
  107. class TestSliceValidation:
  108. @pytest.mark.asyncio
  109. @pytest.mark.integration
  110. async def test_returns_404_when_source_missing(self, async_client: AsyncClient, slice_test_setup):
  111. _install_mock_sidecar(lambda r: httpx.Response(200, content=b""))
  112. response = await async_client.post(
  113. "/api/v1/library/files/999999/slice",
  114. json={
  115. "printer_preset_id": slice_test_setup["printer_id"],
  116. "process_preset_id": slice_test_setup["process_id"],
  117. "filament_preset_id": slice_test_setup["filament_id"],
  118. },
  119. )
  120. assert response.status_code == 404
  121. @pytest.mark.asyncio
  122. @pytest.mark.integration
  123. async def test_returns_400_for_wrong_file_type(self, async_client: AsyncClient, db_session, slice_test_setup):
  124. gcode_path = slice_test_setup["tmp_path"] / "library" / "files" / "out.gcode"
  125. gcode_path.write_bytes(b"; gcode\n")
  126. gfile = LibraryFile(
  127. filename="out.gcode",
  128. file_path=str(gcode_path.relative_to(slice_test_setup["tmp_path"])),
  129. file_type="gcode",
  130. file_size=10,
  131. )
  132. db_session.add(gfile)
  133. await db_session.commit()
  134. await db_session.refresh(gfile)
  135. _install_mock_sidecar(lambda r: httpx.Response(200, content=b""))
  136. response = await async_client.post(
  137. f"/api/v1/library/files/{gfile.id}/slice",
  138. json={
  139. "printer_preset_id": slice_test_setup["printer_id"],
  140. "process_preset_id": slice_test_setup["process_id"],
  141. "filament_preset_id": slice_test_setup["filament_id"],
  142. },
  143. )
  144. assert response.status_code == 400
  145. assert "STL, 3MF, or STEP" in response.json()["detail"]
  146. # ---------------------------------------------------------------------------
  147. # POST /library/files/{id}/slice — async dispatch + bg job
  148. # ---------------------------------------------------------------------------
  149. class TestSliceLibraryFile:
  150. @pytest.mark.asyncio
  151. @pytest.mark.integration
  152. async def test_happy_path_returns_202_then_job_completes_with_library_file(
  153. self, async_client: AsyncClient, slice_test_setup
  154. ):
  155. captured: dict = {}
  156. def handler(request: httpx.Request) -> httpx.Response:
  157. captured["url"] = str(request.url)
  158. return httpx.Response(
  159. status_code=200,
  160. content=b"PK\x03\x04 fake-3mf",
  161. headers={
  162. "x-print-time-seconds": "656",
  163. "x-filament-used-g": "0.94",
  164. "x-filament-used-mm": "302.5",
  165. },
  166. )
  167. _install_mock_sidecar(handler)
  168. response = await async_client.post(
  169. f"/api/v1/library/files/{slice_test_setup['src_file_id']}/slice",
  170. json={
  171. "printer_preset_id": slice_test_setup["printer_id"],
  172. "process_preset_id": slice_test_setup["process_id"],
  173. "filament_preset_id": slice_test_setup["filament_id"],
  174. },
  175. )
  176. assert response.status_code == 202, response.text
  177. body = response.json()
  178. assert body["status"] == "pending"
  179. assert body["status_url"].startswith("/api/v1/slice-jobs/")
  180. final = await _wait_for_job(async_client, body["job_id"])
  181. assert final["status"] == "completed", final
  182. assert final["result"]["library_file_id"] != slice_test_setup["src_file_id"]
  183. assert final["result"]["print_time_seconds"] == 656
  184. assert captured["url"].endswith("/slice")
  185. @pytest.mark.asyncio
  186. @pytest.mark.integration
  187. async def test_invalid_preset_id_surfaces_as_failed_job_with_status_400(
  188. self, async_client: AsyncClient, slice_test_setup
  189. ):
  190. _install_mock_sidecar(lambda r: httpx.Response(200, content=b""))
  191. response = await async_client.post(
  192. f"/api/v1/library/files/{slice_test_setup['src_file_id']}/slice",
  193. json={
  194. # Swap printer/filament — both exist but wrong preset_type.
  195. "printer_preset_id": slice_test_setup["filament_id"],
  196. "process_preset_id": slice_test_setup["process_id"],
  197. "filament_preset_id": slice_test_setup["printer_id"],
  198. },
  199. )
  200. assert response.status_code == 202
  201. final = await _wait_for_job(async_client, response.json()["job_id"])
  202. assert final["status"] == "failed"
  203. assert final["error_status"] == 400
  204. assert "preset_type" in (final["error_detail"] or "")
  205. @pytest.mark.asyncio
  206. @pytest.mark.integration
  207. async def test_unknown_preferred_slicer_fails_with_400(
  208. self, async_client: AsyncClient, db_session, slice_test_setup
  209. ):
  210. await db_session.execute(
  211. SettingsModel.__table__.update().where(SettingsModel.key == "preferred_slicer").values(value="prusaslicer")
  212. )
  213. await db_session.commit()
  214. _install_mock_sidecar(lambda r: httpx.Response(200, content=b""))
  215. response = await async_client.post(
  216. f"/api/v1/library/files/{slice_test_setup['src_file_id']}/slice",
  217. json={
  218. "printer_preset_id": slice_test_setup["printer_id"],
  219. "process_preset_id": slice_test_setup["process_id"],
  220. "filament_preset_id": slice_test_setup["filament_id"],
  221. },
  222. )
  223. assert response.status_code == 202
  224. final = await _wait_for_job(async_client, response.json()["job_id"])
  225. assert final["status"] == "failed"
  226. assert final["error_status"] == 400
  227. assert "preferred_slicer" in (final["error_detail"] or "")
  228. @pytest.mark.asyncio
  229. @pytest.mark.integration
  230. async def test_sidecar_unreachable_fails_with_502(self, async_client: AsyncClient, slice_test_setup):
  231. def handler(_: httpx.Request) -> httpx.Response:
  232. raise httpx.ConnectError("connection refused")
  233. _install_mock_sidecar(handler)
  234. response = await async_client.post(
  235. f"/api/v1/library/files/{slice_test_setup['src_file_id']}/slice",
  236. json={
  237. "printer_preset_id": slice_test_setup["printer_id"],
  238. "process_preset_id": slice_test_setup["process_id"],
  239. "filament_preset_id": slice_test_setup["filament_id"],
  240. },
  241. )
  242. assert response.status_code == 202
  243. final = await _wait_for_job(async_client, response.json()["job_id"])
  244. assert final["status"] == "failed"
  245. assert final["error_status"] == 502
  246. assert "unreachable" in (final["error_detail"] or "").lower()
  247. @pytest.mark.asyncio
  248. @pytest.mark.integration
  249. async def test_3mf_falls_back_to_embedded_settings_on_cli_failure(
  250. self, async_client: AsyncClient, db_session, slice_test_setup
  251. ):
  252. # When the slicer CLI fails on the --load-settings path (segfault
  253. # on complex H2D models), Bambuddy retries with no profile triplet
  254. # so the CLI uses the file's embedded settings.
  255. src_3mf_path = slice_test_setup["tmp_path"] / "library" / "files" / "complex.3mf"
  256. src_3mf_path.write_bytes(_make_3mf_with_settings({"prime_tower_brim_width": "-1"}))
  257. threemf = LibraryFile(
  258. filename="complex.3mf",
  259. file_path=str(src_3mf_path.relative_to(slice_test_setup["tmp_path"])),
  260. file_type="3mf",
  261. file_size=src_3mf_path.stat().st_size,
  262. )
  263. db_session.add(threemf)
  264. await db_session.commit()
  265. await db_session.refresh(threemf)
  266. call_count = {"n": 0}
  267. def handler(request: httpx.Request) -> httpx.Response:
  268. call_count["n"] += 1
  269. # First call: profile triplet present → simulate CLI 5xx
  270. if call_count["n"] == 1:
  271. return httpx.Response(
  272. status_code=500,
  273. json={"message": "Failed to slice the model"},
  274. )
  275. # Retry: no profile triplet → succeed with embedded settings
  276. return httpx.Response(
  277. status_code=200,
  278. content=b"PK\x03\x04 fake-3mf",
  279. headers={
  280. "x-print-time-seconds": "100",
  281. "x-filament-used-g": "1.0",
  282. "x-filament-used-mm": "100",
  283. },
  284. )
  285. _install_mock_sidecar(handler)
  286. response = await async_client.post(
  287. f"/api/v1/library/files/{threemf.id}/slice",
  288. json={
  289. "printer_preset_id": slice_test_setup["printer_id"],
  290. "process_preset_id": slice_test_setup["process_id"],
  291. "filament_preset_id": slice_test_setup["filament_id"],
  292. },
  293. )
  294. assert response.status_code == 202
  295. final = await _wait_for_job(async_client, response.json()["job_id"])
  296. assert final["status"] == "completed", final
  297. assert final["result"]["used_embedded_settings"] is True
  298. assert call_count["n"] == 2 # primary + fallback retry
  299. @pytest.mark.asyncio
  300. @pytest.mark.integration
  301. async def test_stl_does_not_fall_back_on_cli_failure(self, async_client: AsyncClient, slice_test_setup):
  302. # STL has no embedded settings — the CLI 5xx is terminal.
  303. call_count = {"n": 0}
  304. def handler(_: httpx.Request) -> httpx.Response:
  305. call_count["n"] += 1
  306. return httpx.Response(
  307. status_code=500,
  308. json={"message": "Failed to slice the model"},
  309. )
  310. _install_mock_sidecar(handler)
  311. response = await async_client.post(
  312. f"/api/v1/library/files/{slice_test_setup['src_file_id']}/slice",
  313. json={
  314. "printer_preset_id": slice_test_setup["printer_id"],
  315. "process_preset_id": slice_test_setup["process_id"],
  316. "filament_preset_id": slice_test_setup["filament_id"],
  317. },
  318. )
  319. assert response.status_code == 202
  320. final = await _wait_for_job(async_client, response.json()["job_id"])
  321. assert final["status"] == "failed"
  322. assert final["error_status"] == 502
  323. assert call_count["n"] == 1 # No retry for STL
  324. @pytest.mark.asyncio
  325. @pytest.mark.integration
  326. async def test_3mf_input_strips_embedded_settings_before_forwarding(
  327. self, async_client: AsyncClient, db_session, slice_test_setup
  328. ):
  329. src_3mf_path = slice_test_setup["tmp_path"] / "library" / "files" / "real.3mf"
  330. src_3mf_path.write_bytes(_make_3mf_with_settings({"prime_tower_brim_width": "-1"}))
  331. threemf = LibraryFile(
  332. filename="real.3mf",
  333. file_path=str(src_3mf_path.relative_to(slice_test_setup["tmp_path"])),
  334. file_type="3mf",
  335. file_size=src_3mf_path.stat().st_size,
  336. )
  337. db_session.add(threemf)
  338. await db_session.commit()
  339. await db_session.refresh(threemf)
  340. captured: dict = {}
  341. def handler(request: httpx.Request) -> httpx.Response:
  342. captured["body"] = request.content
  343. return httpx.Response(
  344. status_code=200,
  345. content=b"PK\x03\x04 fake-3mf",
  346. headers={
  347. "x-print-time-seconds": "1",
  348. "x-filament-used-g": "0",
  349. "x-filament-used-mm": "0",
  350. },
  351. )
  352. _install_mock_sidecar(handler)
  353. response = await async_client.post(
  354. f"/api/v1/library/files/{threemf.id}/slice",
  355. json={
  356. "printer_preset_id": slice_test_setup["printer_id"],
  357. "process_preset_id": slice_test_setup["process_id"],
  358. "filament_preset_id": slice_test_setup["filament_id"],
  359. },
  360. )
  361. assert response.status_code == 202
  362. final = await _wait_for_job(async_client, response.json()["job_id"])
  363. assert final["status"] == "completed", final
  364. # Recover the embedded zip from the multipart body — the strip
  365. # removed Metadata/project_settings.config but kept geometry.
  366. body = captured["body"]
  367. pk = body.find(b"PK\x03\x04")
  368. assert pk >= 0, "3MF body not found in multipart payload"
  369. with zipfile.ZipFile(io.BytesIO(body[pk:]), "r") as zin:
  370. names = set(zin.namelist())
  371. assert "Metadata/project_settings.config" not in names
  372. assert "3D/3dmodel.model" in names
  373. # ---------------------------------------------------------------------------
  374. # GET /slice-jobs/{id}
  375. # ---------------------------------------------------------------------------
  376. class TestSliceJobs:
  377. @pytest.mark.asyncio
  378. @pytest.mark.integration
  379. async def test_unknown_job_returns_404(self, async_client: AsyncClient):
  380. # Sweep dispatcher state so a fresh ID is unknown.
  381. slice_dispatch._jobs.clear()
  382. r = await async_client.get("/api/v1/slice-jobs/999999")
  383. assert r.status_code == 404