test_print_queue_api.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. """Integration tests for Print Queue API endpoints."""
  2. import pytest
  3. from httpx import AsyncClient
  4. class TestPrintQueueAPI:
  5. """Integration tests for /api/v1/queue endpoints."""
  6. @pytest.fixture
  7. async def printer_factory(self, db_session):
  8. """Factory to create test printers."""
  9. _counter = [0]
  10. async def _create_printer(**kwargs):
  11. from backend.app.models.printer import Printer
  12. _counter[0] += 1
  13. counter = _counter[0]
  14. defaults = {
  15. "name": f"Test Printer {counter}",
  16. "ip_address": f"192.168.1.{100 + counter}",
  17. "serial_number": f"TESTSERIAL{counter:04d}",
  18. "access_code": "12345678",
  19. "model": "X1C",
  20. }
  21. defaults.update(kwargs)
  22. printer = Printer(**defaults)
  23. db_session.add(printer)
  24. await db_session.commit()
  25. await db_session.refresh(printer)
  26. return printer
  27. return _create_printer
  28. @pytest.fixture
  29. async def archive_factory(self, db_session):
  30. """Factory to create test archives."""
  31. _counter = [0]
  32. async def _create_archive(**kwargs):
  33. from backend.app.models.archive import PrintArchive
  34. _counter[0] += 1
  35. counter = _counter[0]
  36. defaults = {
  37. "filename": f"test_print_{counter}.3mf",
  38. "print_name": f"Test Print {counter}",
  39. "file_path": f"/tmp/test_print_{counter}.3mf",
  40. "file_size": 1024,
  41. "content_hash": f"testhash{counter:08d}",
  42. "status": "completed",
  43. }
  44. defaults.update(kwargs)
  45. archive = PrintArchive(**defaults)
  46. db_session.add(archive)
  47. await db_session.commit()
  48. await db_session.refresh(archive)
  49. return archive
  50. return _create_archive
  51. @pytest.fixture
  52. async def queue_item_factory(self, db_session, printer_factory, archive_factory):
  53. """Factory to create test queue items."""
  54. _counter = [0]
  55. async def _create_queue_item(**kwargs):
  56. from backend.app.models.print_queue import PrintQueueItem
  57. _counter[0] += 1
  58. counter = _counter[0]
  59. # Create printer and archive if not provided
  60. if "printer_id" not in kwargs:
  61. printer = await printer_factory()
  62. kwargs["printer_id"] = printer.id
  63. if "archive_id" not in kwargs:
  64. archive = await archive_factory()
  65. kwargs["archive_id"] = archive.id
  66. defaults = {
  67. "status": "pending",
  68. "position": counter,
  69. }
  70. defaults.update(kwargs)
  71. item = PrintQueueItem(**defaults)
  72. db_session.add(item)
  73. await db_session.commit()
  74. await db_session.refresh(item)
  75. return item
  76. return _create_queue_item
  77. @pytest.mark.asyncio
  78. @pytest.mark.integration
  79. async def test_list_queue_empty(self, async_client: AsyncClient):
  80. """Verify empty list when no queue items exist."""
  81. response = await async_client.get("/api/v1/queue/")
  82. assert response.status_code == 200
  83. assert isinstance(response.json(), list)
  84. @pytest.mark.asyncio
  85. @pytest.mark.integration
  86. async def test_add_to_queue(self, async_client: AsyncClient, printer_factory, archive_factory, db_session):
  87. """Verify item can be added to queue."""
  88. printer = await printer_factory()
  89. archive = await archive_factory()
  90. data = {
  91. "printer_id": printer.id,
  92. "archive_id": archive.id,
  93. }
  94. response = await async_client.post("/api/v1/queue/", json=data)
  95. assert response.status_code == 200
  96. result = response.json()
  97. assert result["printer_id"] == printer.id
  98. assert result["archive_id"] == archive.id
  99. assert result["status"] == "pending"
  100. assert result["manual_start"] is False
  101. @pytest.mark.asyncio
  102. @pytest.mark.integration
  103. async def test_add_to_queue_with_manual_start(
  104. self, async_client: AsyncClient, printer_factory, archive_factory, db_session
  105. ):
  106. """Verify item can be added to queue with manual_start=True."""
  107. printer = await printer_factory()
  108. archive = await archive_factory()
  109. data = {
  110. "printer_id": printer.id,
  111. "archive_id": archive.id,
  112. "manual_start": True,
  113. }
  114. response = await async_client.post("/api/v1/queue/", json=data)
  115. assert response.status_code == 200
  116. result = response.json()
  117. assert result["printer_id"] == printer.id
  118. assert result["archive_id"] == archive.id
  119. assert result["status"] == "pending"
  120. assert result["manual_start"] is True
  121. @pytest.mark.asyncio
  122. @pytest.mark.integration
  123. async def test_add_to_queue_with_ams_mapping(
  124. self, async_client: AsyncClient, printer_factory, archive_factory, db_session
  125. ):
  126. """Verify item can be added to queue with ams_mapping."""
  127. printer = await printer_factory()
  128. archive = await archive_factory()
  129. data = {
  130. "printer_id": printer.id,
  131. "archive_id": archive.id,
  132. "ams_mapping": [5, -1, 2, -1], # Slot 1 -> tray 5, slot 3 -> tray 2
  133. }
  134. response = await async_client.post("/api/v1/queue/", json=data)
  135. assert response.status_code == 200
  136. result = response.json()
  137. assert result["printer_id"] == printer.id
  138. assert result["archive_id"] == archive.id
  139. assert result["ams_mapping"] == [5, -1, 2, -1]
  140. @pytest.mark.asyncio
  141. @pytest.mark.integration
  142. async def test_add_to_queue_with_plate_id(
  143. self, async_client: AsyncClient, printer_factory, archive_factory, db_session
  144. ):
  145. """Verify item can be added to queue with plate_id for multi-plate 3MF."""
  146. printer = await printer_factory()
  147. archive = await archive_factory()
  148. data = {
  149. "printer_id": printer.id,
  150. "archive_id": archive.id,
  151. "plate_id": 3,
  152. }
  153. response = await async_client.post("/api/v1/queue/", json=data)
  154. assert response.status_code == 200
  155. result = response.json()
  156. assert result["plate_id"] == 3
  157. @pytest.mark.asyncio
  158. @pytest.mark.integration
  159. async def test_add_to_queue_with_print_options(
  160. self, async_client: AsyncClient, printer_factory, archive_factory, db_session
  161. ):
  162. """Verify item can be added to queue with print options."""
  163. printer = await printer_factory()
  164. archive = await archive_factory()
  165. data = {
  166. "printer_id": printer.id,
  167. "archive_id": archive.id,
  168. "bed_levelling": False,
  169. "flow_cali": True,
  170. "vibration_cali": False,
  171. "layer_inspect": True,
  172. "timelapse": True,
  173. "use_ams": False,
  174. }
  175. response = await async_client.post("/api/v1/queue/", json=data)
  176. assert response.status_code == 200
  177. result = response.json()
  178. assert result["bed_levelling"] is False
  179. assert result["flow_cali"] is True
  180. assert result["vibration_cali"] is False
  181. assert result["layer_inspect"] is True
  182. assert result["timelapse"] is True
  183. assert result["use_ams"] is False
  184. @pytest.mark.asyncio
  185. @pytest.mark.integration
  186. async def test_update_queue_item_plate_id(self, async_client: AsyncClient, queue_item_factory, db_session):
  187. """Verify queue item plate_id can be updated."""
  188. item = await queue_item_factory()
  189. response = await async_client.patch(f"/api/v1/queue/{item.id}", json={"plate_id": 5})
  190. assert response.status_code == 200
  191. result = response.json()
  192. assert result["plate_id"] == 5
  193. @pytest.mark.asyncio
  194. @pytest.mark.integration
  195. async def test_update_queue_item_print_options(self, async_client: AsyncClient, queue_item_factory, db_session):
  196. """Verify queue item print options can be updated."""
  197. item = await queue_item_factory()
  198. response = await async_client.patch(
  199. f"/api/v1/queue/{item.id}",
  200. json={
  201. "bed_levelling": False,
  202. "timelapse": True,
  203. },
  204. )
  205. assert response.status_code == 200
  206. result = response.json()
  207. assert result["bed_levelling"] is False
  208. assert result["timelapse"] is True
  209. @pytest.mark.asyncio
  210. @pytest.mark.integration
  211. async def test_get_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  212. """Verify single queue item can be retrieved."""
  213. item = await queue_item_factory()
  214. response = await async_client.get(f"/api/v1/queue/{item.id}")
  215. assert response.status_code == 200
  216. assert response.json()["id"] == item.id
  217. @pytest.mark.asyncio
  218. @pytest.mark.integration
  219. async def test_get_queue_item_not_found(self, async_client: AsyncClient):
  220. """Verify 404 for non-existent queue item."""
  221. response = await async_client.get("/api/v1/queue/9999")
  222. assert response.status_code == 404
  223. @pytest.mark.asyncio
  224. @pytest.mark.integration
  225. async def test_update_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  226. """Verify queue item can be updated."""
  227. item = await queue_item_factory()
  228. response = await async_client.patch(f"/api/v1/queue/{item.id}", json={"auto_off_after": True})
  229. assert response.status_code == 200
  230. result = response.json()
  231. assert result["auto_off_after"] is True
  232. @pytest.mark.asyncio
  233. @pytest.mark.integration
  234. async def test_update_queue_item_manual_start(self, async_client: AsyncClient, queue_item_factory, db_session):
  235. """Verify queue item manual_start can be updated."""
  236. item = await queue_item_factory(manual_start=False)
  237. response = await async_client.patch(f"/api/v1/queue/{item.id}", json={"manual_start": True})
  238. assert response.status_code == 200
  239. result = response.json()
  240. assert result["manual_start"] is True
  241. @pytest.mark.asyncio
  242. @pytest.mark.integration
  243. async def test_delete_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  244. """Verify queue item can be deleted."""
  245. item = await queue_item_factory()
  246. response = await async_client.delete(f"/api/v1/queue/{item.id}")
  247. assert response.status_code == 200
  248. assert response.json()["message"] == "Queue item deleted"
  249. @pytest.mark.asyncio
  250. @pytest.mark.integration
  251. async def test_delete_queue_item_not_found(self, async_client: AsyncClient):
  252. """Verify 404 for deleting non-existent queue item."""
  253. response = await async_client.delete("/api/v1/queue/9999")
  254. assert response.status_code == 404
  255. class TestQueueStartEndpoint:
  256. """Tests for the /queue/{item_id}/start endpoint."""
  257. @pytest.fixture
  258. async def printer_factory(self, db_session):
  259. """Factory to create test printers."""
  260. _counter = [0]
  261. async def _create_printer(**kwargs):
  262. from backend.app.models.printer import Printer
  263. _counter[0] += 1
  264. counter = _counter[0]
  265. defaults = {
  266. "name": f"Test Printer {counter}",
  267. "ip_address": f"192.168.1.{100 + counter}",
  268. "serial_number": f"TESTSERIAL{counter:04d}",
  269. "access_code": "12345678",
  270. "model": "X1C",
  271. }
  272. defaults.update(kwargs)
  273. printer = Printer(**defaults)
  274. db_session.add(printer)
  275. await db_session.commit()
  276. await db_session.refresh(printer)
  277. return printer
  278. return _create_printer
  279. @pytest.fixture
  280. async def archive_factory(self, db_session):
  281. """Factory to create test archives."""
  282. _counter = [0]
  283. async def _create_archive(**kwargs):
  284. from backend.app.models.archive import PrintArchive
  285. _counter[0] += 1
  286. counter = _counter[0]
  287. defaults = {
  288. "filename": f"test_print_{counter}.3mf",
  289. "print_name": f"Test Print {counter}",
  290. "file_path": f"/tmp/test_print_{counter}.3mf",
  291. "file_size": 1024,
  292. "content_hash": f"testhash{counter:08d}",
  293. "status": "completed",
  294. }
  295. defaults.update(kwargs)
  296. archive = PrintArchive(**defaults)
  297. db_session.add(archive)
  298. await db_session.commit()
  299. await db_session.refresh(archive)
  300. return archive
  301. return _create_archive
  302. @pytest.fixture
  303. async def queue_item_factory(self, db_session, printer_factory, archive_factory):
  304. """Factory to create test queue items."""
  305. _counter = [0]
  306. async def _create_queue_item(**kwargs):
  307. from backend.app.models.print_queue import PrintQueueItem
  308. _counter[0] += 1
  309. counter = _counter[0]
  310. if "printer_id" not in kwargs:
  311. printer = await printer_factory()
  312. kwargs["printer_id"] = printer.id
  313. if "archive_id" not in kwargs:
  314. archive = await archive_factory()
  315. kwargs["archive_id"] = archive.id
  316. defaults = {
  317. "status": "pending",
  318. "position": counter,
  319. }
  320. defaults.update(kwargs)
  321. item = PrintQueueItem(**defaults)
  322. db_session.add(item)
  323. await db_session.commit()
  324. await db_session.refresh(item)
  325. return item
  326. return _create_queue_item
  327. @pytest.mark.asyncio
  328. @pytest.mark.integration
  329. async def test_start_staged_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  330. """Verify starting a staged (manual_start=True) queue item clears the flag."""
  331. item = await queue_item_factory(manual_start=True)
  332. assert item.manual_start is True
  333. response = await async_client.post(f"/api/v1/queue/{item.id}/start")
  334. assert response.status_code == 200
  335. result = response.json()
  336. assert result["manual_start"] is False
  337. assert result["status"] == "pending"
  338. @pytest.mark.asyncio
  339. @pytest.mark.integration
  340. async def test_start_non_staged_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  341. """Verify starting a non-staged queue item still works (idempotent)."""
  342. item = await queue_item_factory(manual_start=False)
  343. assert item.manual_start is False
  344. response = await async_client.post(f"/api/v1/queue/{item.id}/start")
  345. assert response.status_code == 200
  346. result = response.json()
  347. assert result["manual_start"] is False
  348. @pytest.mark.asyncio
  349. @pytest.mark.integration
  350. async def test_start_queue_item_not_found(self, async_client: AsyncClient):
  351. """Verify 404 for non-existent queue item."""
  352. response = await async_client.post("/api/v1/queue/9999/start")
  353. assert response.status_code == 404
  354. @pytest.mark.asyncio
  355. @pytest.mark.integration
  356. async def test_start_non_pending_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  357. """Verify 400 error when trying to start a non-pending queue item."""
  358. item = await queue_item_factory(status="printing", manual_start=True)
  359. response = await async_client.post(f"/api/v1/queue/{item.id}/start")
  360. assert response.status_code == 400
  361. assert "pending" in response.json()["detail"].lower()
  362. @pytest.mark.asyncio
  363. @pytest.mark.integration
  364. async def test_start_completed_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  365. """Verify 400 error when trying to start a completed queue item."""
  366. item = await queue_item_factory(status="completed", manual_start=True)
  367. response = await async_client.post(f"/api/v1/queue/{item.id}/start")
  368. assert response.status_code == 400
  369. class TestQueueCancelEndpoint:
  370. """Tests for the /queue/{item_id}/cancel endpoint."""
  371. @pytest.fixture
  372. async def printer_factory(self, db_session):
  373. """Factory to create test printers."""
  374. async def _create_printer(**kwargs):
  375. from backend.app.models.printer import Printer
  376. defaults = {
  377. "name": "Cancel Test Printer",
  378. "ip_address": "192.168.1.200",
  379. "serial_number": "TESTCANCEL001",
  380. "access_code": "12345678",
  381. "model": "X1C",
  382. }
  383. defaults.update(kwargs)
  384. printer = Printer(**defaults)
  385. db_session.add(printer)
  386. await db_session.commit()
  387. await db_session.refresh(printer)
  388. return printer
  389. return _create_printer
  390. @pytest.fixture
  391. async def archive_factory(self, db_session):
  392. """Factory to create test archives."""
  393. async def _create_archive(**kwargs):
  394. from backend.app.models.archive import PrintArchive
  395. defaults = {
  396. "filename": "cancel_test.3mf",
  397. "print_name": "Cancel Test Print",
  398. "file_path": "/tmp/cancel_test.3mf",
  399. "file_size": 1024,
  400. "content_hash": "cancelhash001",
  401. "status": "completed",
  402. }
  403. defaults.update(kwargs)
  404. archive = PrintArchive(**defaults)
  405. db_session.add(archive)
  406. await db_session.commit()
  407. await db_session.refresh(archive)
  408. return archive
  409. return _create_archive
  410. @pytest.fixture
  411. async def queue_item_factory(self, db_session, printer_factory, archive_factory):
  412. """Factory to create test queue items."""
  413. async def _create_queue_item(**kwargs):
  414. from backend.app.models.print_queue import PrintQueueItem
  415. if "printer_id" not in kwargs:
  416. printer = await printer_factory()
  417. kwargs["printer_id"] = printer.id
  418. if "archive_id" not in kwargs:
  419. archive = await archive_factory()
  420. kwargs["archive_id"] = archive.id
  421. defaults = {
  422. "status": "pending",
  423. "position": 1,
  424. }
  425. defaults.update(kwargs)
  426. item = PrintQueueItem(**defaults)
  427. db_session.add(item)
  428. await db_session.commit()
  429. await db_session.refresh(item)
  430. return item
  431. return _create_queue_item
  432. @pytest.mark.asyncio
  433. @pytest.mark.integration
  434. async def test_cancel_pending_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  435. """Verify cancelling a pending queue item."""
  436. item = await queue_item_factory(status="pending")
  437. response = await async_client.post(f"/api/v1/queue/{item.id}/cancel")
  438. assert response.status_code == 200
  439. assert response.json()["message"] == "Queue item cancelled"
  440. @pytest.mark.asyncio
  441. @pytest.mark.integration
  442. async def test_cancel_non_pending_queue_item(self, async_client: AsyncClient, queue_item_factory, db_session):
  443. """Verify 400 error when trying to cancel a non-pending queue item."""
  444. item = await queue_item_factory(status="printing")
  445. response = await async_client.post(f"/api/v1/queue/{item.id}/cancel")
  446. assert response.status_code == 400