test_printers_api.py 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. """Integration tests for Printers API endpoints.
  2. Tests the full request/response cycle for /api/v1/printers/ endpoints.
  3. """
  4. from unittest.mock import MagicMock, patch
  5. import pytest
  6. from httpx import AsyncClient
  7. class TestPrintersAPI:
  8. """Integration tests for /api/v1/printers/ endpoints."""
  9. # ========================================================================
  10. # List endpoints
  11. # ========================================================================
  12. @pytest.mark.asyncio
  13. @pytest.mark.integration
  14. async def test_list_printers_empty(self, async_client: AsyncClient):
  15. """Verify empty list is returned when no printers exist."""
  16. response = await async_client.get("/api/v1/printers/")
  17. assert response.status_code == 200
  18. assert response.json() == []
  19. @pytest.mark.asyncio
  20. @pytest.mark.integration
  21. async def test_list_printers_with_data(self, async_client: AsyncClient, printer_factory, db_session):
  22. """Verify list returns existing printers."""
  23. await printer_factory(name="Test Printer")
  24. response = await async_client.get("/api/v1/printers/")
  25. assert response.status_code == 200
  26. data = response.json()
  27. assert len(data) >= 1
  28. assert any(p["name"] == "Test Printer" for p in data)
  29. # ========================================================================
  30. # Create endpoints
  31. # ========================================================================
  32. @pytest.mark.asyncio
  33. @pytest.mark.integration
  34. async def test_create_printer(self, async_client: AsyncClient):
  35. """Verify printer can be created."""
  36. data = {
  37. "name": "New Printer",
  38. "serial_number": "00M09A111111111",
  39. "ip_address": "192.168.1.100",
  40. "access_code": "12345678",
  41. "is_active": True,
  42. "model": "X1C",
  43. }
  44. response = await async_client.post("/api/v1/printers/", json=data)
  45. assert response.status_code == 200
  46. result = response.json()
  47. assert result["name"] == "New Printer"
  48. assert result["serial_number"] == "00M09A111111111"
  49. assert result["model"] == "X1C"
  50. @pytest.mark.asyncio
  51. @pytest.mark.integration
  52. async def test_create_printer_with_hostname(self, async_client: AsyncClient):
  53. """Verify printer can be created with a hostname instead of IP address."""
  54. data = {
  55. "name": "DNS Printer",
  56. "serial_number": "00M09A555555555",
  57. "ip_address": "printer.local",
  58. "access_code": "12345678",
  59. "model": "P1S",
  60. }
  61. response = await async_client.post("/api/v1/printers/", json=data)
  62. assert response.status_code == 200
  63. result = response.json()
  64. assert result["name"] == "DNS Printer"
  65. assert result["ip_address"] == "printer.local"
  66. @pytest.mark.asyncio
  67. @pytest.mark.integration
  68. async def test_create_printer_with_fqdn(self, async_client: AsyncClient):
  69. """Verify printer can be created with a fully qualified domain name."""
  70. data = {
  71. "name": "FQDN Printer",
  72. "serial_number": "00M09A666666666",
  73. "ip_address": "my-printer.home.lan",
  74. "access_code": "12345678",
  75. "model": "X1C",
  76. }
  77. response = await async_client.post("/api/v1/printers/", json=data)
  78. assert response.status_code == 200
  79. result = response.json()
  80. assert result["ip_address"] == "my-printer.home.lan"
  81. @pytest.mark.asyncio
  82. @pytest.mark.integration
  83. async def test_create_printer_invalid_hostname(self, async_client: AsyncClient):
  84. """Verify invalid hostnames are rejected."""
  85. data = {
  86. "name": "Bad Printer",
  87. "serial_number": "00M09A777777777",
  88. "ip_address": "-invalid",
  89. "access_code": "12345678",
  90. }
  91. response = await async_client.post("/api/v1/printers/", json=data)
  92. assert response.status_code == 422
  93. @pytest.mark.asyncio
  94. @pytest.mark.integration
  95. async def test_create_printer_duplicate_serial(self, async_client: AsyncClient, printer_factory, db_session):
  96. """Verify duplicate serial number is rejected."""
  97. await printer_factory(serial_number="00M09A222222222")
  98. data = {
  99. "name": "Duplicate Printer",
  100. "serial_number": "00M09A222222222",
  101. "ip_address": "192.168.1.101",
  102. "access_code": "12345678",
  103. }
  104. response = await async_client.post("/api/v1/printers/", json=data)
  105. # Should fail due to duplicate serial
  106. assert response.status_code in [400, 409, 422, 500]
  107. # ========================================================================
  108. # Get single endpoint
  109. # ========================================================================
  110. @pytest.mark.asyncio
  111. @pytest.mark.integration
  112. async def test_get_printer(self, async_client: AsyncClient, printer_factory, db_session):
  113. """Verify single printer can be retrieved."""
  114. printer = await printer_factory(name="Get Test Printer")
  115. response = await async_client.get(f"/api/v1/printers/{printer.id}")
  116. assert response.status_code == 200
  117. result = response.json()
  118. assert result["id"] == printer.id
  119. assert result["name"] == "Get Test Printer"
  120. @pytest.mark.asyncio
  121. @pytest.mark.integration
  122. async def test_get_printer_not_found(self, async_client: AsyncClient):
  123. """Verify 404 for non-existent printer."""
  124. response = await async_client.get("/api/v1/printers/9999")
  125. assert response.status_code == 404
  126. # ========================================================================
  127. # Update endpoints
  128. # ========================================================================
  129. @pytest.mark.asyncio
  130. @pytest.mark.integration
  131. async def test_update_printer_name(self, async_client: AsyncClient, printer_factory, db_session):
  132. """Verify printer name can be updated."""
  133. printer = await printer_factory(name="Original Name")
  134. response = await async_client.patch(f"/api/v1/printers/{printer.id}", json={"name": "Updated Name"})
  135. assert response.status_code == 200
  136. assert response.json()["name"] == "Updated Name"
  137. @pytest.mark.asyncio
  138. @pytest.mark.integration
  139. async def test_update_printer_active_status(self, async_client: AsyncClient, printer_factory, db_session):
  140. """Verify printer active status can be updated."""
  141. printer = await printer_factory(is_active=True)
  142. response = await async_client.patch(f"/api/v1/printers/{printer.id}", json={"is_active": False})
  143. assert response.status_code == 200
  144. assert response.json()["is_active"] is False
  145. @pytest.mark.asyncio
  146. @pytest.mark.integration
  147. async def test_update_printer_auto_archive(self, async_client: AsyncClient, printer_factory, db_session):
  148. """Verify auto_archive setting can be updated."""
  149. printer = await printer_factory(auto_archive=True)
  150. response = await async_client.patch(f"/api/v1/printers/{printer.id}", json={"auto_archive": False})
  151. assert response.status_code == 200
  152. assert response.json()["auto_archive"] is False
  153. @pytest.mark.asyncio
  154. @pytest.mark.integration
  155. async def test_update_nonexistent_printer(self, async_client: AsyncClient):
  156. """Verify updating non-existent printer returns 404."""
  157. response = await async_client.patch("/api/v1/printers/9999", json={"name": "New Name"})
  158. assert response.status_code == 404
  159. # ========================================================================
  160. # Delete endpoints
  161. # ========================================================================
  162. @pytest.mark.asyncio
  163. @pytest.mark.integration
  164. async def test_delete_printer(self, async_client: AsyncClient, printer_factory, db_session):
  165. """Verify printer can be deleted."""
  166. printer = await printer_factory()
  167. printer_id = printer.id
  168. response = await async_client.delete(f"/api/v1/printers/{printer_id}")
  169. assert response.status_code == 200
  170. # Verify deleted
  171. response = await async_client.get(f"/api/v1/printers/{printer_id}")
  172. assert response.status_code == 404
  173. @pytest.mark.asyncio
  174. @pytest.mark.integration
  175. async def test_delete_nonexistent_printer(self, async_client: AsyncClient):
  176. """Verify deleting non-existent printer returns 404."""
  177. response = await async_client.delete("/api/v1/printers/9999")
  178. assert response.status_code == 404
  179. # ========================================================================
  180. # Status endpoint
  181. # ========================================================================
  182. @pytest.mark.asyncio
  183. @pytest.mark.integration
  184. async def test_get_printer_status(
  185. self, async_client: AsyncClient, printer_factory, mock_printer_manager, db_session
  186. ):
  187. """Verify printer status can be retrieved."""
  188. printer = await printer_factory()
  189. response = await async_client.get(f"/api/v1/printers/{printer.id}/status")
  190. assert response.status_code == 200
  191. result = response.json()
  192. assert "connected" in result
  193. assert "state" in result
  194. @pytest.mark.asyncio
  195. @pytest.mark.integration
  196. async def test_get_printer_status_not_found(self, async_client: AsyncClient):
  197. """Verify 404 for status of non-existent printer."""
  198. response = await async_client.get("/api/v1/printers/9999/status")
  199. assert response.status_code == 404
  200. # ========================================================================
  201. # Test connection endpoint
  202. # ========================================================================
  203. class TestPrinterDataIntegrity:
  204. """Tests for printer data integrity."""
  205. @pytest.mark.asyncio
  206. @pytest.mark.integration
  207. async def test_printer_stores_all_fields(self, async_client: AsyncClient, printer_factory, db_session):
  208. """Verify printer stores all fields correctly."""
  209. printer = await printer_factory(
  210. name="Full Test Printer",
  211. serial_number="00M09A444444444",
  212. ip_address="192.168.1.150",
  213. model="P1S",
  214. is_active=True,
  215. auto_archive=False,
  216. )
  217. response = await async_client.get(f"/api/v1/printers/{printer.id}")
  218. assert response.status_code == 200
  219. result = response.json()
  220. assert result["name"] == "Full Test Printer"
  221. assert result["serial_number"] == "00M09A444444444"
  222. assert result["ip_address"] == "192.168.1.150"
  223. assert result["model"] == "P1S"
  224. assert result["is_active"] is True
  225. assert result["auto_archive"] is False
  226. @pytest.mark.asyncio
  227. @pytest.mark.integration
  228. async def test_printer_update_persists(self, async_client: AsyncClient, printer_factory, db_session):
  229. """CRITICAL: Verify printer updates persist."""
  230. printer = await printer_factory(name="Original", is_active=True)
  231. # Update
  232. await async_client.patch(f"/api/v1/printers/{printer.id}", json={"name": "Updated", "is_active": False})
  233. # Verify persistence
  234. response = await async_client.get(f"/api/v1/printers/{printer.id}")
  235. result = response.json()
  236. assert result["name"] == "Updated"
  237. assert result["is_active"] is False
  238. # ========================================================================
  239. # Refresh status endpoint
  240. # ========================================================================
  241. @pytest.mark.asyncio
  242. @pytest.mark.integration
  243. async def test_refresh_status_not_found(self, async_client: AsyncClient):
  244. """Verify 404 for non-existent printer."""
  245. response = await async_client.post("/api/v1/printers/99999/refresh-status")
  246. assert response.status_code == 404
  247. @pytest.mark.asyncio
  248. @pytest.mark.integration
  249. async def test_refresh_status_not_connected(self, async_client: AsyncClient, printer_factory):
  250. """Verify 400 when printer is not connected."""
  251. printer = await printer_factory(name="Disconnected Printer")
  252. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  253. mock_pm.request_status_update.return_value = False
  254. response = await async_client.post(f"/api/v1/printers/{printer.id}/refresh-status")
  255. assert response.status_code == 400
  256. assert "not connected" in response.json()["detail"].lower()
  257. @pytest.mark.asyncio
  258. @pytest.mark.integration
  259. async def test_refresh_status_success(self, async_client: AsyncClient, printer_factory):
  260. """Verify successful refresh request."""
  261. printer = await printer_factory(name="Connected Printer")
  262. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  263. mock_pm.request_status_update.return_value = True
  264. response = await async_client.post(f"/api/v1/printers/{printer.id}/refresh-status")
  265. assert response.status_code == 200
  266. assert response.json()["status"] == "refresh_requested"
  267. mock_pm.request_status_update.assert_called_once_with(printer.id)
  268. # ========================================================================
  269. # Current print user endpoint (Issue #206)
  270. # ========================================================================
  271. @pytest.mark.asyncio
  272. @pytest.mark.integration
  273. async def test_get_current_print_user_not_found(self, async_client: AsyncClient):
  274. """Verify 404 for non-existent printer."""
  275. response = await async_client.get("/api/v1/printers/99999/current-print-user")
  276. assert response.status_code == 404
  277. @pytest.mark.asyncio
  278. @pytest.mark.integration
  279. async def test_get_current_print_user_returns_empty_when_no_user(self, async_client: AsyncClient, printer_factory):
  280. """Verify empty object returned when no user is tracked."""
  281. printer = await printer_factory(name="Test Printer")
  282. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  283. mock_pm.get_current_print_user.return_value = None
  284. response = await async_client.get(f"/api/v1/printers/{printer.id}/current-print-user")
  285. assert response.status_code == 200
  286. assert response.json() == {}
  287. @pytest.mark.asyncio
  288. @pytest.mark.integration
  289. async def test_get_current_print_user_returns_user_info(self, async_client: AsyncClient, printer_factory):
  290. """Verify user info is returned when tracked."""
  291. printer = await printer_factory(name="Test Printer")
  292. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  293. mock_pm.get_current_print_user.return_value = {"user_id": 42, "username": "testuser"}
  294. response = await async_client.get(f"/api/v1/printers/{printer.id}/current-print-user")
  295. assert response.status_code == 200
  296. result = response.json()
  297. assert result["user_id"] == 42
  298. assert result["username"] == "testuser"
  299. class TestPrintControlAPI:
  300. """Integration tests for print control endpoints (stop, pause, resume)."""
  301. # ========================================================================
  302. # Stop print endpoint
  303. # ========================================================================
  304. @pytest.mark.asyncio
  305. @pytest.mark.integration
  306. async def test_stop_print_not_found(self, async_client: AsyncClient):
  307. """Verify 404 for non-existent printer."""
  308. response = await async_client.post("/api/v1/printers/99999/print/stop")
  309. assert response.status_code == 404
  310. @pytest.mark.asyncio
  311. @pytest.mark.integration
  312. async def test_stop_print_not_connected(self, async_client: AsyncClient, printer_factory):
  313. """Verify error when printer is not connected."""
  314. printer = await printer_factory(name="Disconnected Printer")
  315. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  316. mock_pm.get_client.return_value = None
  317. response = await async_client.post(f"/api/v1/printers/{printer.id}/print/stop")
  318. assert response.status_code == 400
  319. assert "not connected" in response.json()["detail"].lower()
  320. @pytest.mark.asyncio
  321. @pytest.mark.integration
  322. async def test_stop_print_success(self, async_client: AsyncClient, printer_factory):
  323. """Verify successful stop print request."""
  324. printer = await printer_factory(name="Printing Printer")
  325. mock_client = MagicMock()
  326. mock_client.stop_print.return_value = True
  327. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  328. mock_pm.get_client.return_value = mock_client
  329. response = await async_client.post(f"/api/v1/printers/{printer.id}/print/stop")
  330. assert response.status_code == 200
  331. assert response.json()["success"] is True
  332. mock_client.stop_print.assert_called_once()
  333. # ========================================================================
  334. # Pause print endpoint
  335. # ========================================================================
  336. @pytest.mark.asyncio
  337. @pytest.mark.integration
  338. async def test_pause_print_not_found(self, async_client: AsyncClient):
  339. """Verify 404 for non-existent printer."""
  340. response = await async_client.post("/api/v1/printers/99999/print/pause")
  341. assert response.status_code == 404
  342. @pytest.mark.asyncio
  343. @pytest.mark.integration
  344. async def test_pause_print_not_connected(self, async_client: AsyncClient, printer_factory):
  345. """Verify error when printer is not connected."""
  346. printer = await printer_factory(name="Disconnected Printer")
  347. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  348. mock_pm.get_client.return_value = None
  349. response = await async_client.post(f"/api/v1/printers/{printer.id}/print/pause")
  350. assert response.status_code == 400
  351. assert "not connected" in response.json()["detail"].lower()
  352. @pytest.mark.asyncio
  353. @pytest.mark.integration
  354. async def test_pause_print_success(self, async_client: AsyncClient, printer_factory):
  355. """Verify successful pause print request."""
  356. printer = await printer_factory(name="Printing Printer")
  357. mock_client = MagicMock()
  358. mock_client.pause_print.return_value = True
  359. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  360. mock_pm.get_client.return_value = mock_client
  361. response = await async_client.post(f"/api/v1/printers/{printer.id}/print/pause")
  362. assert response.status_code == 200
  363. assert response.json()["success"] is True
  364. mock_client.pause_print.assert_called_once()
  365. # ========================================================================
  366. # Resume print endpoint
  367. # ========================================================================
  368. @pytest.mark.asyncio
  369. @pytest.mark.integration
  370. async def test_resume_print_not_found(self, async_client: AsyncClient):
  371. """Verify 404 for non-existent printer."""
  372. response = await async_client.post("/api/v1/printers/99999/print/resume")
  373. assert response.status_code == 404
  374. @pytest.mark.asyncio
  375. @pytest.mark.integration
  376. async def test_resume_print_not_connected(self, async_client: AsyncClient, printer_factory):
  377. """Verify error when printer is not connected."""
  378. printer = await printer_factory(name="Disconnected Printer")
  379. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  380. mock_pm.get_client.return_value = None
  381. response = await async_client.post(f"/api/v1/printers/{printer.id}/print/resume")
  382. assert response.status_code == 400
  383. assert "not connected" in response.json()["detail"].lower()
  384. @pytest.mark.asyncio
  385. @pytest.mark.integration
  386. async def test_resume_print_success(self, async_client: AsyncClient, printer_factory):
  387. """Verify successful resume print request."""
  388. printer = await printer_factory(name="Paused Printer")
  389. mock_client = MagicMock()
  390. mock_client.resume_print.return_value = True
  391. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  392. mock_pm.get_client.return_value = mock_client
  393. response = await async_client.post(f"/api/v1/printers/{printer.id}/print/resume")
  394. assert response.status_code == 200
  395. assert response.json()["success"] is True
  396. mock_client.resume_print.assert_called_once()
  397. class TestAMSRefreshAPI:
  398. """Integration tests for AMS slot refresh endpoint."""
  399. @pytest.mark.asyncio
  400. @pytest.mark.integration
  401. async def test_ams_refresh_not_found(self, async_client: AsyncClient):
  402. """Verify 404 for non-existent printer."""
  403. response = await async_client.post("/api/v1/printers/99999/ams/0/slot/0/refresh")
  404. assert response.status_code == 404
  405. @pytest.mark.asyncio
  406. @pytest.mark.integration
  407. async def test_ams_refresh_not_connected(self, async_client: AsyncClient, printer_factory):
  408. """Verify error when printer is not connected."""
  409. printer = await printer_factory(name="Disconnected Printer")
  410. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  411. mock_pm.get_client.return_value = None
  412. response = await async_client.post(f"/api/v1/printers/{printer.id}/ams/0/slot/0/refresh")
  413. assert response.status_code == 400
  414. assert "not connected" in response.json()["detail"].lower()
  415. @pytest.mark.asyncio
  416. @pytest.mark.integration
  417. async def test_ams_refresh_success(self, async_client: AsyncClient, printer_factory):
  418. """Verify successful AMS refresh request."""
  419. printer = await printer_factory(name="Printer with AMS")
  420. mock_client = MagicMock()
  421. mock_client.ams_refresh_tray.return_value = (True, "Refreshing AMS 0 tray 1")
  422. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  423. mock_pm.get_client.return_value = mock_client
  424. response = await async_client.post(f"/api/v1/printers/{printer.id}/ams/0/slot/1/refresh")
  425. assert response.status_code == 200
  426. result = response.json()
  427. assert result["success"] is True
  428. mock_client.ams_refresh_tray.assert_called_once_with(0, 1)
  429. @pytest.mark.asyncio
  430. @pytest.mark.integration
  431. async def test_ams_refresh_filament_loaded(self, async_client: AsyncClient, printer_factory):
  432. """Verify error when filament is loaded (can't refresh while loaded)."""
  433. printer = await printer_factory(name="Printer with AMS")
  434. mock_client = MagicMock()
  435. mock_client.ams_refresh_tray.return_value = (False, "Please unload filament first")
  436. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  437. mock_pm.get_client.return_value = mock_client
  438. response = await async_client.post(f"/api/v1/printers/{printer.id}/ams/0/slot/0/refresh")
  439. assert response.status_code == 400
  440. assert "unload" in response.json()["detail"].lower()
  441. class TestConfigureAMSSlotAPI:
  442. """Integration tests for AMS slot configure endpoint — tray_info_idx resolution."""
  443. @pytest.mark.asyncio
  444. @pytest.mark.integration
  445. async def test_configure_not_connected(self, async_client: AsyncClient, printer_factory):
  446. """Verify error when printer is not connected."""
  447. printer = await printer_factory(name="Disconnected")
  448. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  449. mock_pm.get_client.return_value = None
  450. response = await async_client.post(
  451. f"/api/v1/printers/{printer.id}/slots/0/0/configure",
  452. params={
  453. "tray_info_idx": "GFL99",
  454. "tray_type": "PLA",
  455. "tray_sub_brands": "PLA Basic",
  456. "tray_color": "FF0000FF",
  457. "nozzle_temp_min": 190,
  458. "nozzle_temp_max": 230,
  459. },
  460. )
  461. assert response.status_code == 400
  462. assert "not connected" in response.json()["detail"].lower()
  463. @pytest.mark.asyncio
  464. @pytest.mark.integration
  465. async def test_configure_with_gf_id_keeps_it(self, async_client: AsyncClient, printer_factory):
  466. """Standard Bambu GF* filament IDs are sent as-is."""
  467. printer = await printer_factory(name="H2D")
  468. mock_client = MagicMock()
  469. mock_client.ams_set_filament_setting.return_value = True
  470. mock_client.extrusion_cali_sel.return_value = True
  471. mock_client.request_status_update.return_value = True
  472. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  473. mock_pm.get_client.return_value = mock_client
  474. mock_pm.get_status.return_value = None # No existing state
  475. response = await async_client.post(
  476. f"/api/v1/printers/{printer.id}/slots/2/3/configure",
  477. params={
  478. "tray_info_idx": "GFL05",
  479. "tray_type": "PLA",
  480. "tray_sub_brands": "PLA Basic",
  481. "tray_color": "FFFFFFFF",
  482. "nozzle_temp_min": 190,
  483. "nozzle_temp_max": 230,
  484. },
  485. )
  486. assert response.status_code == 200
  487. call_kwargs = mock_client.ams_set_filament_setting.call_args
  488. assert call_kwargs.kwargs["tray_info_idx"] == "GFL05"
  489. @pytest.mark.asyncio
  490. @pytest.mark.integration
  491. async def test_configure_pfus_sent_directly(self, async_client: AsyncClient, printer_factory):
  492. """PFUS* cloud-synced custom preset IDs are sent to the printer."""
  493. printer = await printer_factory(name="H2D")
  494. mock_client = MagicMock()
  495. mock_client.ams_set_filament_setting.return_value = True
  496. mock_client.extrusion_cali_sel.return_value = True
  497. mock_client.request_status_update.return_value = True
  498. mock_status = MagicMock()
  499. mock_status.raw_data = {"ams": {"ams": []}} # No existing tray data
  500. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  501. mock_pm.get_client.return_value = mock_client
  502. mock_pm.get_status.return_value = mock_status
  503. response = await async_client.post(
  504. f"/api/v1/printers/{printer.id}/slots/2/3/configure",
  505. params={
  506. "tray_info_idx": "PFUS9ac902733670a9",
  507. "tray_type": "PLA",
  508. "tray_sub_brands": "Devil Design PLA",
  509. "tray_color": "FF0000FF",
  510. "nozzle_temp_min": 190,
  511. "nozzle_temp_max": 230,
  512. },
  513. )
  514. assert response.status_code == 200
  515. call_kwargs = mock_client.ams_set_filament_setting.call_args
  516. assert call_kwargs.kwargs["tray_info_idx"] == "PFUS9ac902733670a9"
  517. @pytest.mark.asyncio
  518. @pytest.mark.integration
  519. async def test_configure_pfus_takes_priority_over_slot(self, async_client: AsyncClient, printer_factory):
  520. """Provided PFUS* preset takes priority over slot's existing preset."""
  521. printer = await printer_factory(name="H2D")
  522. mock_client = MagicMock()
  523. mock_client.ams_set_filament_setting.return_value = True
  524. mock_client.extrusion_cali_sel.return_value = True
  525. mock_client.request_status_update.return_value = True
  526. # Simulate slot already configured by slicer with cloud-synced preset
  527. mock_status = MagicMock()
  528. mock_status.raw_data = {
  529. "ams": {
  530. "ams": [
  531. {
  532. "id": 2,
  533. "tray": [
  534. {
  535. "id": 3,
  536. "tray_info_idx": "P4d64437",
  537. "tray_type": "PLA",
  538. "tray_color": "FF0000FF",
  539. }
  540. ],
  541. }
  542. ]
  543. }
  544. }
  545. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  546. mock_pm.get_client.return_value = mock_client
  547. mock_pm.get_status.return_value = mock_status
  548. response = await async_client.post(
  549. f"/api/v1/printers/{printer.id}/slots/2/3/configure",
  550. params={
  551. "tray_info_idx": "PFUS9ac902733670a9",
  552. "tray_type": "PLA",
  553. "tray_sub_brands": "Devil Design PLA",
  554. "tray_color": "FF0000FF",
  555. "nozzle_temp_min": 190,
  556. "nozzle_temp_max": 230,
  557. },
  558. )
  559. assert response.status_code == 200
  560. call_kwargs = mock_client.ams_set_filament_setting.call_args
  561. # Provided preset wins over slot's existing one
  562. assert call_kwargs.kwargs["tray_info_idx"] == "PFUS9ac902733670a9"
  563. @pytest.mark.asyncio
  564. @pytest.mark.integration
  565. async def test_configure_pfus_used_regardless_of_slot_material(self, async_client: AsyncClient, printer_factory):
  566. """Provided PFUS* preset is used even when slot has a different material."""
  567. printer = await printer_factory(name="H2D")
  568. mock_client = MagicMock()
  569. mock_client.ams_set_filament_setting.return_value = True
  570. mock_client.extrusion_cali_sel.return_value = True
  571. mock_client.request_status_update.return_value = True
  572. # Slot currently has PETG but user is configuring PLA
  573. mock_status = MagicMock()
  574. mock_status.raw_data = {
  575. "ams": {
  576. "ams": [
  577. {
  578. "id": 2,
  579. "tray": [{"id": 3, "tray_info_idx": "GFG99", "tray_type": "PETG", "tray_color": "FFFFFFFF"}],
  580. }
  581. ]
  582. }
  583. }
  584. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  585. mock_pm.get_client.return_value = mock_client
  586. mock_pm.get_status.return_value = mock_status
  587. response = await async_client.post(
  588. f"/api/v1/printers/{printer.id}/slots/2/3/configure",
  589. params={
  590. "tray_info_idx": "PFUS9ac902733670a9",
  591. "tray_type": "PLA",
  592. "tray_sub_brands": "Devil Design PLA",
  593. "tray_color": "FF0000FF",
  594. "nozzle_temp_min": 190,
  595. "nozzle_temp_max": 230,
  596. },
  597. )
  598. assert response.status_code == 200
  599. call_kwargs = mock_client.ams_set_filament_setting.call_args
  600. # Provided preset wins — slot's material is irrelevant
  601. assert call_kwargs.kwargs["tray_info_idx"] == "PFUS9ac902733670a9"
  602. @pytest.mark.asyncio
  603. @pytest.mark.integration
  604. async def test_configure_empty_id_uses_generic(self, async_client: AsyncClient, printer_factory):
  605. """Empty tray_info_idx (local preset) is replaced with generic."""
  606. printer = await printer_factory(name="H2D")
  607. mock_client = MagicMock()
  608. mock_client.ams_set_filament_setting.return_value = True
  609. mock_client.extrusion_cali_sel.return_value = True
  610. mock_client.request_status_update.return_value = True
  611. mock_status = MagicMock()
  612. mock_status.raw_data = {"ams": {"ams": []}}
  613. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  614. mock_pm.get_client.return_value = mock_client
  615. mock_pm.get_status.return_value = mock_status
  616. response = await async_client.post(
  617. f"/api/v1/printers/{printer.id}/slots/2/3/configure",
  618. params={
  619. "tray_info_idx": "",
  620. "tray_type": "PETG",
  621. "tray_sub_brands": "PETG Basic",
  622. "tray_color": "FFFFFFFF",
  623. "nozzle_temp_min": 220,
  624. "nozzle_temp_max": 260,
  625. },
  626. )
  627. assert response.status_code == 200
  628. call_kwargs = mock_client.ams_set_filament_setting.call_args
  629. assert call_kwargs.kwargs["tray_info_idx"] == "GFG99"
  630. @pytest.mark.asyncio
  631. @pytest.mark.integration
  632. async def test_configure_pfus_preserves_setting_id_pair(self, async_client: AsyncClient, printer_factory):
  633. """Both tray_info_idx=PFUS* and setting_id=PFUS* are forwarded untouched.
  634. Pins the end-to-end contract the frontend #1053 fix relies on: when the
  635. user configures a slot with a custom cloud preset whose cloud detail
  636. has filament_id=null, the frontend sends the setting_id in BOTH fields
  637. and the backend must not collapse either to a generic GF* ID.
  638. """
  639. printer = await printer_factory(name="H2D")
  640. mock_client = MagicMock()
  641. mock_client.ams_set_filament_setting.return_value = True
  642. mock_client.extrusion_cali_sel.return_value = True
  643. mock_client.request_status_update.return_value = True
  644. mock_status = MagicMock()
  645. mock_status.raw_data = {"ams": {"ams": []}}
  646. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  647. mock_pm.get_client.return_value = mock_client
  648. mock_pm.get_status.return_value = mock_status
  649. response = await async_client.post(
  650. f"/api/v1/printers/{printer.id}/slots/128/0/configure",
  651. params={
  652. "tray_info_idx": "PFUSa8fb76f9733e3c",
  653. "tray_type": "ABS",
  654. "tray_sub_brands": "Sting3D ABS",
  655. "tray_color": "000000FF",
  656. "nozzle_temp_min": 240,
  657. "nozzle_temp_max": 280,
  658. "setting_id": "PFUSa8fb76f9733e3c",
  659. },
  660. )
  661. assert response.status_code == 200
  662. call_kwargs = mock_client.ams_set_filament_setting.call_args
  663. assert call_kwargs.kwargs["tray_info_idx"] == "PFUSa8fb76f9733e3c"
  664. assert call_kwargs.kwargs["setting_id"] == "PFUSa8fb76f9733e3c"
  665. # Explicitly assert no generic-collapse happened for this HT slot.
  666. assert call_kwargs.kwargs["tray_info_idx"] != "GFB99"
  667. class TestSkipObjectsAPI:
  668. """Integration tests for skip objects endpoints."""
  669. # ========================================================================
  670. # Get printable objects endpoint
  671. # ========================================================================
  672. @pytest.mark.asyncio
  673. @pytest.mark.integration
  674. async def test_get_objects_not_found(self, async_client: AsyncClient):
  675. """Verify 404 for non-existent printer."""
  676. response = await async_client.get("/api/v1/printers/99999/print/objects")
  677. assert response.status_code == 404
  678. @pytest.mark.asyncio
  679. @pytest.mark.integration
  680. async def test_get_objects_not_connected(self, async_client: AsyncClient, printer_factory):
  681. """Verify error when printer is not connected."""
  682. printer = await printer_factory(name="Disconnected Printer")
  683. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  684. mock_pm.get_client.return_value = None
  685. response = await async_client.get(f"/api/v1/printers/{printer.id}/print/objects")
  686. assert response.status_code == 400
  687. assert "not connected" in response.json()["detail"].lower()
  688. @pytest.mark.asyncio
  689. @pytest.mark.integration
  690. async def test_get_objects_empty(self, async_client: AsyncClient, printer_factory):
  691. """Verify empty objects list when no print is active."""
  692. printer = await printer_factory(name="Idle Printer")
  693. mock_client = MagicMock()
  694. mock_client.state.printable_objects = {}
  695. mock_client.state.skipped_objects = []
  696. mock_client.state.state = "IDLE"
  697. mock_client.state.subtask_name = None # Prevent FTP download attempt
  698. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  699. mock_pm.get_client.return_value = mock_client
  700. response = await async_client.get(f"/api/v1/printers/{printer.id}/print/objects")
  701. assert response.status_code == 200
  702. result = response.json()
  703. assert result["objects"] == []
  704. assert result["total"] == 0
  705. assert result["skipped_count"] == 0
  706. assert result["is_printing"] is False
  707. @pytest.mark.asyncio
  708. @pytest.mark.integration
  709. async def test_get_objects_with_data(self, async_client: AsyncClient, printer_factory):
  710. """Verify objects list when print is active."""
  711. printer = await printer_factory(name="Printing Printer")
  712. mock_client = MagicMock()
  713. mock_client.state.printable_objects = {100: "Part A", 200: "Part B", 300: "Part C"}
  714. mock_client.state.skipped_objects = [200]
  715. mock_client.state.state = "RUNNING"
  716. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  717. mock_pm.get_client.return_value = mock_client
  718. response = await async_client.get(f"/api/v1/printers/{printer.id}/print/objects")
  719. assert response.status_code == 200
  720. result = response.json()
  721. assert result["total"] == 3
  722. assert result["skipped_count"] == 1
  723. assert result["is_printing"] is True
  724. # Check objects have correct structure
  725. objects_by_id = {obj["id"]: obj for obj in result["objects"]}
  726. assert objects_by_id[100]["name"] == "Part A"
  727. assert objects_by_id[100]["skipped"] is False
  728. assert objects_by_id[200]["name"] == "Part B"
  729. assert objects_by_id[200]["skipped"] is True
  730. assert objects_by_id[300]["name"] == "Part C"
  731. assert objects_by_id[300]["skipped"] is False
  732. # ========================================================================
  733. # Skip objects endpoint
  734. # ========================================================================
  735. @pytest.mark.asyncio
  736. @pytest.mark.integration
  737. async def test_get_objects_with_positions(self, async_client: AsyncClient, printer_factory):
  738. """Verify objects list includes position data when available."""
  739. printer = await printer_factory(name="Printing Printer")
  740. # New format with position data
  741. mock_client = MagicMock()
  742. mock_client.state.printable_objects = {
  743. 100: {"name": "Part A", "x": 50.0, "y": 100.0},
  744. 200: {"name": "Part B", "x": 150.0, "y": 100.0},
  745. }
  746. mock_client.state.skipped_objects = []
  747. mock_client.state.state = "RUNNING"
  748. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  749. mock_pm.get_client.return_value = mock_client
  750. response = await async_client.get(f"/api/v1/printers/{printer.id}/print/objects")
  751. assert response.status_code == 200
  752. result = response.json()
  753. assert result["total"] == 2
  754. # Check objects have position data
  755. objects_by_id = {obj["id"]: obj for obj in result["objects"]}
  756. assert objects_by_id[100]["name"] == "Part A"
  757. assert objects_by_id[100]["x"] == 50.0
  758. assert objects_by_id[100]["y"] == 100.0
  759. assert objects_by_id[200]["name"] == "Part B"
  760. assert objects_by_id[200]["x"] == 150.0
  761. assert objects_by_id[200]["y"] == 100.0
  762. # ========================================================================
  763. # Skip objects endpoint
  764. # ========================================================================
  765. @pytest.mark.asyncio
  766. @pytest.mark.integration
  767. async def test_skip_objects_not_found(self, async_client: AsyncClient):
  768. """Verify 404 for non-existent printer."""
  769. response = await async_client.post("/api/v1/printers/99999/print/skip-objects", json=[100])
  770. assert response.status_code == 404
  771. @pytest.mark.asyncio
  772. @pytest.mark.integration
  773. async def test_skip_objects_not_connected(self, async_client: AsyncClient, printer_factory):
  774. """Verify error when printer is not connected."""
  775. printer = await printer_factory(name="Disconnected Printer")
  776. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  777. mock_pm.get_client.return_value = None
  778. response = await async_client.post(f"/api/v1/printers/{printer.id}/print/skip-objects", json=[100])
  779. assert response.status_code == 400
  780. assert "not connected" in response.json()["detail"].lower()
  781. @pytest.mark.asyncio
  782. @pytest.mark.integration
  783. async def test_skip_objects_empty_list(self, async_client: AsyncClient, printer_factory):
  784. """Verify error when no object IDs provided."""
  785. printer = await printer_factory(name="Printing Printer")
  786. mock_client = MagicMock()
  787. mock_client.state.printable_objects = {100: "Part A"}
  788. mock_client.state.skipped_objects = []
  789. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  790. mock_pm.get_client.return_value = mock_client
  791. response = await async_client.post(f"/api/v1/printers/{printer.id}/print/skip-objects", json=[])
  792. assert response.status_code == 400
  793. assert "no object" in response.json()["detail"].lower()
  794. @pytest.mark.asyncio
  795. @pytest.mark.integration
  796. async def test_skip_objects_invalid_id(self, async_client: AsyncClient, printer_factory):
  797. """Verify error when object ID doesn't exist."""
  798. printer = await printer_factory(name="Printing Printer")
  799. mock_client = MagicMock()
  800. mock_client.state.printable_objects = {100: "Part A"}
  801. mock_client.state.skipped_objects = []
  802. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  803. mock_pm.get_client.return_value = mock_client
  804. response = await async_client.post(f"/api/v1/printers/{printer.id}/print/skip-objects", json=[999])
  805. assert response.status_code == 400
  806. assert "invalid" in response.json()["detail"].lower()
  807. @pytest.mark.asyncio
  808. @pytest.mark.integration
  809. async def test_skip_objects_success(self, async_client: AsyncClient, printer_factory):
  810. """Verify successful skip objects request."""
  811. printer = await printer_factory(name="Printing Printer")
  812. mock_client = MagicMock()
  813. mock_client.state.printable_objects = {100: "Part A", 200: "Part B"}
  814. mock_client.state.skipped_objects = []
  815. mock_client.skip_objects.return_value = True
  816. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  817. mock_pm.get_client.return_value = mock_client
  818. response = await async_client.post(f"/api/v1/printers/{printer.id}/print/skip-objects", json=[100])
  819. assert response.status_code == 200
  820. result = response.json()
  821. assert result["success"] is True
  822. assert 100 in result["skipped_objects"]
  823. mock_client.skip_objects.assert_called_once_with([100])
  824. @pytest.mark.asyncio
  825. @pytest.mark.integration
  826. async def test_skip_objects_multiple(self, async_client: AsyncClient, printer_factory):
  827. """Verify skipping multiple objects at once."""
  828. printer = await printer_factory(name="Printing Printer")
  829. mock_client = MagicMock()
  830. mock_client.state.printable_objects = {100: "Part A", 200: "Part B", 300: "Part C"}
  831. mock_client.state.skipped_objects = []
  832. mock_client.skip_objects.return_value = True
  833. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  834. mock_pm.get_client.return_value = mock_client
  835. response = await async_client.post(f"/api/v1/printers/{printer.id}/print/skip-objects", json=[100, 200])
  836. assert response.status_code == 200
  837. result = response.json()
  838. assert result["success"] is True
  839. assert 100 in result["skipped_objects"]
  840. assert 200 in result["skipped_objects"]
  841. mock_client.skip_objects.assert_called_once_with([100, 200])
  842. class TestChamberLightAPI:
  843. """Integration tests for chamber light control endpoint."""
  844. @pytest.mark.asyncio
  845. @pytest.mark.integration
  846. async def test_chamber_light_not_found(self, async_client: AsyncClient):
  847. """Verify 404 for non-existent printer."""
  848. response = await async_client.post("/api/v1/printers/99999/chamber-light?on=true")
  849. assert response.status_code == 404
  850. @pytest.mark.asyncio
  851. @pytest.mark.integration
  852. async def test_chamber_light_not_connected(self, async_client: AsyncClient, printer_factory):
  853. """Verify error when printer is not connected."""
  854. printer = await printer_factory(name="Disconnected Printer")
  855. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  856. mock_pm.get_client.return_value = None
  857. response = await async_client.post(f"/api/v1/printers/{printer.id}/chamber-light?on=true")
  858. assert response.status_code == 400
  859. assert "not connected" in response.json()["detail"].lower()
  860. @pytest.mark.asyncio
  861. @pytest.mark.integration
  862. async def test_chamber_light_on_success(self, async_client: AsyncClient, printer_factory):
  863. """Verify successful chamber light on request."""
  864. printer = await printer_factory(name="Test Printer")
  865. mock_client = MagicMock()
  866. mock_client.set_chamber_light.return_value = True
  867. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  868. mock_pm.get_client.return_value = mock_client
  869. response = await async_client.post(f"/api/v1/printers/{printer.id}/chamber-light?on=true")
  870. assert response.status_code == 200
  871. result = response.json()
  872. assert result["success"] is True
  873. assert "on" in result["message"].lower()
  874. mock_client.set_chamber_light.assert_called_once_with(True)
  875. @pytest.mark.asyncio
  876. @pytest.mark.integration
  877. async def test_chamber_light_off_success(self, async_client: AsyncClient, printer_factory):
  878. """Verify successful chamber light off request."""
  879. printer = await printer_factory(name="Test Printer")
  880. mock_client = MagicMock()
  881. mock_client.set_chamber_light.return_value = True
  882. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  883. mock_pm.get_client.return_value = mock_client
  884. response = await async_client.post(f"/api/v1/printers/{printer.id}/chamber-light?on=false")
  885. assert response.status_code == 200
  886. result = response.json()
  887. assert result["success"] is True
  888. assert "off" in result["message"].lower()
  889. mock_client.set_chamber_light.assert_called_once_with(False)
  890. @pytest.mark.asyncio
  891. @pytest.mark.integration
  892. async def test_chamber_light_failure(self, async_client: AsyncClient, printer_factory):
  893. """Verify error handling when chamber light control fails."""
  894. printer = await printer_factory(name="Test Printer")
  895. mock_client = MagicMock()
  896. mock_client.set_chamber_light.return_value = False
  897. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  898. mock_pm.get_client.return_value = mock_client
  899. response = await async_client.post(f"/api/v1/printers/{printer.id}/chamber-light?on=true")
  900. assert response.status_code == 500
  901. assert "failed" in response.json()["detail"].lower()
  902. class TestAirductModeAPI:
  903. """Integration tests for the airduct mode endpoint (P2S/H2*)."""
  904. @pytest.mark.asyncio
  905. @pytest.mark.integration
  906. async def test_invalid_mode_rejected(self, async_client: AsyncClient, printer_factory):
  907. printer = await printer_factory(name="P", model="P2S")
  908. response = await async_client.post(f"/api/v1/printers/{printer.id}/airduct-mode?mode=foo")
  909. assert response.status_code == 400
  910. @pytest.mark.asyncio
  911. @pytest.mark.integration
  912. async def test_not_connected(self, async_client: AsyncClient, printer_factory):
  913. printer = await printer_factory(name="P", model="P2S")
  914. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  915. mock_pm.get_client.return_value = None
  916. response = await async_client.post(f"/api/v1/printers/{printer.id}/airduct-mode?mode=cooling")
  917. assert response.status_code == 400
  918. @pytest.mark.asyncio
  919. @pytest.mark.integration
  920. async def test_cooling_success(self, async_client: AsyncClient, printer_factory):
  921. printer = await printer_factory(name="P", model="P2S")
  922. mock_client = MagicMock()
  923. mock_client.set_airduct_mode.return_value = True
  924. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  925. mock_pm.get_client.return_value = mock_client
  926. response = await async_client.post(f"/api/v1/printers/{printer.id}/airduct-mode?mode=cooling")
  927. assert response.status_code == 200
  928. assert response.json()["success"] is True
  929. mock_client.set_airduct_mode.assert_called_once_with("cooling")
  930. @pytest.mark.asyncio
  931. @pytest.mark.integration
  932. async def test_heating_failure_returns_500(self, async_client: AsyncClient, printer_factory):
  933. printer = await printer_factory(name="P", model="P2S")
  934. mock_client = MagicMock()
  935. mock_client.set_airduct_mode.return_value = False
  936. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  937. mock_pm.get_client.return_value = mock_client
  938. response = await async_client.post(f"/api/v1/printers/{printer.id}/airduct-mode?mode=heating")
  939. assert response.status_code == 500
  940. class TestClearHMSErrorsAPI:
  941. """Integration tests for clear HMS errors endpoint."""
  942. @pytest.mark.asyncio
  943. @pytest.mark.integration
  944. async def test_clear_hms_errors_not_found(self, async_client: AsyncClient):
  945. """Verify 404 for non-existent printer."""
  946. response = await async_client.post("/api/v1/printers/99999/hms/clear")
  947. assert response.status_code == 404
  948. @pytest.mark.asyncio
  949. @pytest.mark.integration
  950. async def test_clear_hms_errors_not_connected(self, async_client: AsyncClient, printer_factory):
  951. """Verify error when printer is not connected."""
  952. printer = await printer_factory(name="Disconnected Printer")
  953. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  954. mock_pm.get_client.return_value = None
  955. response = await async_client.post(f"/api/v1/printers/{printer.id}/hms/clear")
  956. assert response.status_code == 400
  957. assert "not connected" in response.json()["detail"].lower()
  958. @pytest.mark.asyncio
  959. @pytest.mark.integration
  960. async def test_clear_hms_errors_success(self, async_client: AsyncClient, printer_factory):
  961. """Verify successful clear HMS errors request."""
  962. printer = await printer_factory(name="Test Printer")
  963. mock_client = MagicMock()
  964. mock_client.clear_hms_errors.return_value = True
  965. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  966. mock_pm.get_client.return_value = mock_client
  967. response = await async_client.post(f"/api/v1/printers/{printer.id}/hms/clear")
  968. assert response.status_code == 200
  969. result = response.json()
  970. assert result["success"] is True
  971. assert "cleared" in result["message"].lower()
  972. mock_client.clear_hms_errors.assert_called_once()
  973. @pytest.mark.asyncio
  974. @pytest.mark.integration
  975. async def test_clear_hms_errors_failure(self, async_client: AsyncClient, printer_factory):
  976. """Verify error handling when clear HMS errors fails."""
  977. printer = await printer_factory(name="Test Printer")
  978. mock_client = MagicMock()
  979. mock_client.clear_hms_errors.return_value = False
  980. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  981. mock_pm.get_client.return_value = mock_client
  982. response = await async_client.post(f"/api/v1/printers/{printer.id}/hms/clear")
  983. assert response.status_code == 500
  984. assert "failed" in response.json()["detail"].lower()