test_spoolbuddy.py 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602
  1. """Integration tests for SpoolBuddy API endpoints."""
  2. from datetime import datetime, timedelta, timezone
  3. from unittest.mock import AsyncMock, MagicMock, patch
  4. import pytest
  5. from httpx import AsyncClient
  6. from sqlalchemy.ext.asyncio import AsyncSession
  7. import backend.app.services.spoolbuddy_ssh # noqa: F401 — ensures patch() can resolve the dotted path
  8. from backend.app.api.routes import spoolbuddy as spoolbuddy_routes
  9. from backend.app.models.spool import Spool
  10. from backend.app.models.spoolbuddy_device import SpoolBuddyDevice
  11. from backend.app.services.spoolman import SpoolmanNotFoundError, SpoolmanUnavailableError
  12. API = "/api/v1/spoolbuddy"
  13. @pytest.fixture
  14. def device_factory(db_session: AsyncSession):
  15. """Factory to create SpoolBuddyDevice records."""
  16. _counter = [0]
  17. async def _create(**kwargs):
  18. _counter[0] += 1
  19. n = _counter[0]
  20. defaults = {
  21. "device_id": f"sb-{n:04d}",
  22. "hostname": f"spoolbuddy-{n}",
  23. "ip_address": f"10.0.0.{n}",
  24. "firmware_version": "1.0.0",
  25. "has_nfc": True,
  26. "has_scale": True,
  27. "tare_offset": 0,
  28. "calibration_factor": 1.0,
  29. "last_seen": datetime.now(timezone.utc),
  30. }
  31. defaults.update(kwargs)
  32. device = SpoolBuddyDevice(**defaults)
  33. db_session.add(device)
  34. await db_session.commit()
  35. await db_session.refresh(device)
  36. return device
  37. return _create
  38. @pytest.fixture
  39. def spool_factory(db_session: AsyncSession):
  40. """Factory to create Spool records."""
  41. _counter = [0]
  42. async def _create(**kwargs):
  43. _counter[0] += 1
  44. defaults = {
  45. "material": "PLA",
  46. "subtype": "Basic",
  47. "brand": "Polymaker",
  48. "color_name": "Red",
  49. "rgba": "FF0000FF",
  50. "label_weight": 1000,
  51. "core_weight": 250,
  52. "weight_used": 0,
  53. }
  54. defaults.update(kwargs)
  55. spool = Spool(**defaults)
  56. db_session.add(spool)
  57. await db_session.commit()
  58. await db_session.refresh(spool)
  59. return spool
  60. return _create
  61. # ============================================================================
  62. # Device endpoints
  63. # ============================================================================
  64. class TestDeviceEndpoints:
  65. @pytest.mark.asyncio
  66. @pytest.mark.integration
  67. async def test_register_new_device(self, async_client: AsyncClient):
  68. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  69. mock_ws.broadcast = AsyncMock()
  70. resp = await async_client.post(
  71. f"{API}/devices/register",
  72. json={
  73. "device_id": "sb-new",
  74. "hostname": "spoolbuddy-new",
  75. "ip_address": "10.0.0.99",
  76. "firmware_version": "1.2.0",
  77. },
  78. )
  79. assert resp.status_code == 200
  80. data = resp.json()
  81. assert data["device_id"] == "sb-new"
  82. assert data["hostname"] == "spoolbuddy-new"
  83. assert data["online"] is True
  84. mock_ws.broadcast.assert_called_once()
  85. msg = mock_ws.broadcast.call_args[0][0]
  86. assert msg["type"] == "spoolbuddy_online"
  87. @pytest.mark.asyncio
  88. @pytest.mark.integration
  89. async def test_re_register_existing_device(self, async_client: AsyncClient, device_factory):
  90. device = await device_factory(
  91. device_id="sb-exist",
  92. tare_offset=12345,
  93. calibration_factor=0.0042,
  94. )
  95. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  96. mock_ws.broadcast = AsyncMock()
  97. resp = await async_client.post(
  98. f"{API}/devices/register",
  99. json={
  100. "device_id": "sb-exist",
  101. "hostname": "updated-host",
  102. "ip_address": "10.0.0.200",
  103. "firmware_version": "2.0.0",
  104. },
  105. )
  106. assert resp.status_code == 200
  107. data = resp.json()
  108. assert data["id"] == device.id
  109. assert data["hostname"] == "updated-host"
  110. assert data["ip_address"] == "10.0.0.200"
  111. assert data["firmware_version"] == "2.0.0"
  112. # Calibration preserved on re-register
  113. assert data["tare_offset"] == 12345
  114. assert data["calibration_factor"] == pytest.approx(0.0042)
  115. @pytest.mark.asyncio
  116. @pytest.mark.integration
  117. async def test_list_devices_empty(self, async_client: AsyncClient):
  118. resp = await async_client.get(f"{API}/devices")
  119. assert resp.status_code == 200
  120. assert resp.json() == []
  121. @pytest.mark.asyncio
  122. @pytest.mark.integration
  123. async def test_list_devices(self, async_client: AsyncClient, device_factory):
  124. await device_factory(device_id="sb-a", hostname="alpha")
  125. await device_factory(device_id="sb-b", hostname="beta")
  126. resp = await async_client.get(f"{API}/devices")
  127. assert resp.status_code == 200
  128. devices = resp.json()
  129. assert len(devices) == 2
  130. hostnames = {d["hostname"] for d in devices}
  131. assert hostnames == {"alpha", "beta"}
  132. @pytest.mark.asyncio
  133. @pytest.mark.integration
  134. async def test_unregister_device(self, async_client: AsyncClient, device_factory, db_session):
  135. await device_factory(device_id="sb-keep", hostname="keep")
  136. await device_factory(device_id="sb-drop", hostname="drop")
  137. spoolbuddy_routes._spoolbuddy_online_last_broadcast["sb-drop"] = 123.0
  138. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  139. mock_ws.broadcast = AsyncMock()
  140. resp = await async_client.delete(f"{API}/devices/sb-drop")
  141. assert resp.status_code == 200
  142. assert resp.json() == {"status": "deleted", "device_id": "sb-drop"}
  143. assert "sb-drop" not in spoolbuddy_routes._spoolbuddy_online_last_broadcast
  144. mock_ws.broadcast.assert_called_once()
  145. msg = mock_ws.broadcast.call_args[0][0]
  146. assert msg["type"] == "spoolbuddy_unregistered"
  147. assert msg["device_id"] == "sb-drop"
  148. # Other device still present
  149. resp = await async_client.get(f"{API}/devices")
  150. remaining = {d["device_id"] for d in resp.json()}
  151. assert remaining == {"sb-keep"}
  152. @pytest.mark.asyncio
  153. @pytest.mark.integration
  154. async def test_unregister_device_not_found(self, async_client: AsyncClient):
  155. resp = await async_client.delete(f"{API}/devices/sb-ghost")
  156. assert resp.status_code == 404
  157. @pytest.mark.asyncio
  158. @pytest.mark.integration
  159. async def test_heartbeat_updates_status(self, async_client: AsyncClient, device_factory):
  160. device = await device_factory(device_id="sb-hb")
  161. spoolbuddy_routes._spoolbuddy_online_last_broadcast.clear()
  162. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  163. mock_ws.broadcast = AsyncMock()
  164. resp = await async_client.post(
  165. f"{API}/devices/sb-hb/heartbeat",
  166. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 600},
  167. )
  168. assert resp.status_code == 200
  169. data = resp.json()
  170. assert data["tare_offset"] == device.tare_offset
  171. assert data["calibration_factor"] == pytest.approx(device.calibration_factor)
  172. mock_ws.broadcast.assert_called_once()
  173. msg = mock_ws.broadcast.call_args[0][0]
  174. assert msg["type"] == "spoolbuddy_online"
  175. assert msg["device_id"] == "sb-hb"
  176. @pytest.mark.asyncio
  177. @pytest.mark.integration
  178. async def test_heartbeat_returns_ssh_public_key(self, async_client: AsyncClient, device_factory):
  179. """Heartbeat response carries the current SSH public key so the daemon
  180. can re-deploy it whenever Bambuddy's keypair rotates without waiting
  181. for a service restart."""
  182. await device_factory(device_id="sb-ssh-hb")
  183. fake_key = "ssh-ed25519 AAAATESTKEY bambuddy-spoolbuddy"
  184. with (
  185. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  186. patch(
  187. "backend.app.services.spoolbuddy_ssh.get_public_key",
  188. AsyncMock(return_value=fake_key),
  189. ),
  190. ):
  191. mock_ws.broadcast = AsyncMock()
  192. resp = await async_client.post(
  193. f"{API}/devices/sb-ssh-hb/heartbeat",
  194. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 5},
  195. )
  196. assert resp.status_code == 200
  197. assert resp.json()["ssh_public_key"] == fake_key
  198. @pytest.mark.asyncio
  199. @pytest.mark.integration
  200. async def test_heartbeat_ssh_key_failure_does_not_break_heartbeat(self, async_client: AsyncClient, device_factory):
  201. """If the backend can't read its own SSH key, the heartbeat must still
  202. succeed — telemetry/commands are far more critical than key sync."""
  203. await device_factory(device_id="sb-ssh-fail")
  204. with (
  205. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  206. patch(
  207. "backend.app.services.spoolbuddy_ssh.get_public_key",
  208. AsyncMock(side_effect=OSError("disk full")),
  209. ),
  210. ):
  211. mock_ws.broadcast = AsyncMock()
  212. resp = await async_client.post(
  213. f"{API}/devices/sb-ssh-fail/heartbeat",
  214. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 5},
  215. )
  216. assert resp.status_code == 200
  217. assert resp.json()["ssh_public_key"] is None
  218. @pytest.mark.asyncio
  219. @pytest.mark.integration
  220. async def test_heartbeat_returns_pending_command(self, async_client: AsyncClient, device_factory):
  221. await device_factory(device_id="sb-cmd", pending_command="tare")
  222. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  223. mock_ws.broadcast = AsyncMock()
  224. resp = await async_client.post(
  225. f"{API}/devices/sb-cmd/heartbeat",
  226. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 10},
  227. )
  228. assert resp.status_code == 200
  229. assert resp.json()["pending_command"] == "tare"
  230. # Second heartbeat should have no pending command (cleared)
  231. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  232. mock_ws.broadcast = AsyncMock()
  233. resp2 = await async_client.post(
  234. f"{API}/devices/sb-cmd/heartbeat",
  235. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 20},
  236. )
  237. assert resp2.json()["pending_command"] is None
  238. @pytest.mark.asyncio
  239. @pytest.mark.integration
  240. async def test_heartbeat_unknown_device_404(self, async_client: AsyncClient):
  241. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  242. mock_ws.broadcast = AsyncMock()
  243. resp = await async_client.post(
  244. f"{API}/devices/nonexistent/heartbeat",
  245. json={"nfc_ok": False, "scale_ok": False, "uptime_s": 0},
  246. )
  247. assert resp.status_code == 404
  248. @pytest.mark.asyncio
  249. @pytest.mark.integration
  250. async def test_heartbeat_broadcasts_online_when_was_offline(self, async_client: AsyncClient, device_factory):
  251. # Create device with last_seen far in the past (offline)
  252. spoolbuddy_routes._spoolbuddy_online_last_broadcast.clear()
  253. await device_factory(
  254. device_id="sb-offline",
  255. last_seen=datetime.now(timezone.utc) - timedelta(seconds=120),
  256. )
  257. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  258. mock_ws.broadcast = AsyncMock()
  259. resp = await async_client.post(
  260. f"{API}/devices/sb-offline/heartbeat",
  261. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 5},
  262. )
  263. assert resp.status_code == 200
  264. # Should broadcast online since device was offline
  265. mock_ws.broadcast.assert_called_once()
  266. msg = mock_ws.broadcast.call_args[0][0]
  267. assert msg["type"] == "spoolbuddy_online"
  268. assert msg["device_id"] == "sb-offline"
  269. @pytest.mark.asyncio
  270. @pytest.mark.integration
  271. async def test_heartbeat_broadcasts_online_when_already_online(self, async_client: AsyncClient, device_factory):
  272. spoolbuddy_routes._spoolbuddy_online_last_broadcast.clear()
  273. await device_factory(
  274. device_id="sb-already-online",
  275. last_seen=datetime.now(timezone.utc),
  276. )
  277. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  278. mock_ws.broadcast = AsyncMock()
  279. resp = await async_client.post(
  280. f"{API}/devices/sb-already-online/heartbeat",
  281. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 42},
  282. )
  283. assert resp.status_code == 200
  284. mock_ws.broadcast.assert_called_once()
  285. msg = mock_ws.broadcast.call_args[0][0]
  286. assert msg["type"] == "spoolbuddy_online"
  287. assert msg["device_id"] == "sb-already-online"
  288. @pytest.mark.asyncio
  289. @pytest.mark.integration
  290. async def test_heartbeat_online_broadcast_is_throttled(self, async_client: AsyncClient, device_factory):
  291. spoolbuddy_routes._spoolbuddy_online_last_broadcast.clear()
  292. await device_factory(
  293. device_id="sb-throttle",
  294. last_seen=datetime.now(timezone.utc),
  295. )
  296. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  297. mock_ws.broadcast = AsyncMock()
  298. resp1 = await async_client.post(
  299. f"{API}/devices/sb-throttle/heartbeat",
  300. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 10},
  301. )
  302. resp2 = await async_client.post(
  303. f"{API}/devices/sb-throttle/heartbeat",
  304. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 11},
  305. )
  306. assert resp1.status_code == 200
  307. assert resp2.status_code == 200
  308. mock_ws.broadcast.assert_called_once()
  309. msg = mock_ws.broadcast.call_args[0][0]
  310. assert msg["type"] == "spoolbuddy_online"
  311. assert msg["device_id"] == "sb-throttle"
  312. # ============================================================================
  313. # NFC endpoints
  314. # ============================================================================
  315. class TestNfcEndpoints:
  316. @pytest.mark.asyncio
  317. @pytest.mark.integration
  318. async def test_tag_scanned_matched(self, async_client: AsyncClient, spool_factory):
  319. spool = await spool_factory(tag_uid="AABB1122", material="PLA")
  320. mock_spool = MagicMock()
  321. mock_spool.id = spool.id
  322. mock_spool.material = spool.material
  323. mock_spool.subtype = spool.subtype
  324. mock_spool.color_name = spool.color_name
  325. mock_spool.rgba = spool.rgba
  326. mock_spool.brand = spool.brand
  327. mock_spool.label_weight = spool.label_weight
  328. mock_spool.core_weight = spool.core_weight
  329. mock_spool.weight_used = spool.weight_used
  330. with (
  331. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  332. patch("backend.app.api.routes.spoolbuddy.get_spool_by_tag", new_callable=AsyncMock) as mock_lookup,
  333. ):
  334. mock_ws.broadcast = AsyncMock()
  335. mock_lookup.return_value = mock_spool
  336. resp = await async_client.post(
  337. f"{API}/nfc/tag-scanned",
  338. json={"device_id": "sb-1", "tag_uid": "AABB1122"},
  339. )
  340. assert resp.status_code == 200
  341. data = resp.json()
  342. assert data["matched"] is True
  343. assert data["spool_id"] == spool.id
  344. msg = mock_ws.broadcast.call_args[0][0]
  345. assert msg["type"] == "spoolbuddy_tag_matched"
  346. assert msg["spool"]["id"] == spool.id
  347. @pytest.mark.asyncio
  348. @pytest.mark.integration
  349. async def test_tag_scanned_unmatched(self, async_client: AsyncClient):
  350. with (
  351. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  352. patch("backend.app.api.routes.spoolbuddy.get_spool_by_tag", new_callable=AsyncMock) as mock_lookup,
  353. ):
  354. mock_ws.broadcast = AsyncMock()
  355. mock_lookup.return_value = None
  356. resp = await async_client.post(
  357. f"{API}/nfc/tag-scanned",
  358. json={"device_id": "sb-1", "tag_uid": "DEADBEEF"},
  359. )
  360. assert resp.status_code == 200
  361. data = resp.json()
  362. assert data["matched"] is False
  363. assert data["spool_id"] is None
  364. msg = mock_ws.broadcast.call_args[0][0]
  365. assert msg["type"] == "spoolbuddy_unknown_tag"
  366. @pytest.mark.asyncio
  367. @pytest.mark.integration
  368. async def test_tag_scanned_spoolman_mode_skips_local_lookup(self, async_client: AsyncClient, db_session):
  369. """When spoolman_enabled=true, /nfc/tag-scanned must use Spoolman
  370. exclusively — local DB lookup must not be consulted at all. The
  371. previous always-local-first behaviour caused stale local rows to
  372. win over the authoritative Spoolman data (#1228 follow-up).
  373. """
  374. from backend.app.models.settings import Settings
  375. db_session.add(Settings(key="spoolman_enabled", value="true"))
  376. db_session.add(Settings(key="spoolman_url", value="http://127.0.0.1:7912"))
  377. await db_session.commit()
  378. # Mock Spoolman match and verify get_spool_by_tag (the local-DB lookup)
  379. # is never called in Spoolman-enabled mode.
  380. sm_match = {
  381. "id": 7,
  382. "filament": {
  383. "material": "PLA",
  384. "name": "PLA Basic Red",
  385. "color_hex": "FF0000",
  386. "weight": 1000.0,
  387. "vendor": {"name": "Bambu Lab"},
  388. },
  389. "extra": {"tag": '"AABB1122"'},
  390. "used_weight": 0.0,
  391. }
  392. mock_client = MagicMock()
  393. mock_client.get_spools = AsyncMock(return_value=[sm_match])
  394. mock_client.find_spool_by_tag = AsyncMock(return_value=sm_match)
  395. with (
  396. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  397. patch(
  398. "backend.app.api.routes.spoolbuddy._get_spoolman_client_or_none",
  399. new_callable=AsyncMock,
  400. ) as mock_get_client,
  401. patch(
  402. "backend.app.api.routes.spoolbuddy.get_spool_by_tag",
  403. new_callable=AsyncMock,
  404. ) as mock_local_lookup,
  405. ):
  406. mock_ws.broadcast = AsyncMock()
  407. mock_get_client.return_value = mock_client
  408. # Sentinel so a misrouted call would surface as a wrong spool_id.
  409. mock_local_lookup.return_value = MagicMock(id=999)
  410. resp = await async_client.post(
  411. f"{API}/nfc/tag-scanned",
  412. json={"device_id": "sb-1", "tag_uid": "AABB1122"},
  413. )
  414. assert resp.status_code == 200
  415. data = resp.json()
  416. assert data["matched"] is True
  417. # Spoolman result, not local DB sentinel — proves the local lookup was skipped.
  418. assert data["spool_id"] == 7
  419. mock_local_lookup.assert_not_called()
  420. @pytest.mark.asyncio
  421. @pytest.mark.integration
  422. async def test_write_result_clears_duplicate_tag_binding(
  423. self, async_client: AsyncClient, db_session, device_factory
  424. ):
  425. """Writing a tag for spool B must clear the same tag binding from any
  426. other spool that currently has it. Without this guard, find_spool_by_tag
  427. returns whichever spool comes first in the cached list (typically the
  428. older one), so the dashboard shows the wrong spool when the tag is
  429. scanned.
  430. """
  431. import json as _json
  432. from backend.app.models.settings import Settings
  433. from backend.app.models.spoolbuddy_device import SpoolBuddyDevice
  434. db_session.add(Settings(key="spoolman_enabled", value="true"))
  435. db_session.add(Settings(key="spoolman_url", value="http://127.0.0.1:7912"))
  436. await device_factory(
  437. device_id="sb-write",
  438. pending_command="write_tag",
  439. pending_write_payload=_json.dumps({"spool_id": 22, "ndef_data_hex": "DEAD", "data_origin": "spoolman"}),
  440. )
  441. await db_session.commit()
  442. # Spool A (id=11) currently holds the tag we're about to bind to spool B (id=22).
  443. spool_a_with_tag = {
  444. "id": 11,
  445. "filament": {"material": "PLA", "name": "PLA Old", "color_hex": "AAAAAA", "weight": 1000.0},
  446. "extra": {"tag": '"DEADBEEF"'},
  447. }
  448. mock_client = MagicMock()
  449. mock_client.get_spools = AsyncMock(return_value=[spool_a_with_tag])
  450. mock_client.find_spool_by_tag = AsyncMock(return_value=spool_a_with_tag)
  451. mock_client.merge_spool_extra = AsyncMock(return_value={})
  452. with (
  453. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  454. patch(
  455. "backend.app.api.routes.spoolbuddy._get_spoolman_client_or_none",
  456. new_callable=AsyncMock,
  457. ) as mock_get_client,
  458. ):
  459. mock_ws.broadcast = AsyncMock()
  460. mock_get_client.return_value = mock_client
  461. resp = await async_client.post(
  462. f"{API}/nfc/write-result",
  463. json={
  464. "device_id": "sb-write",
  465. "spool_id": 22,
  466. "tag_uid": "DEADBEEF",
  467. "success": True,
  468. },
  469. )
  470. assert resp.status_code == 200
  471. # merge_spool_extra was called twice:
  472. # 1. clear tag from spool A (id=11) — set tag to ""
  473. # 2. set tag on spool B (id=22) — set tag to "DEADBEEF" (JSON-encoded)
  474. assert mock_client.merge_spool_extra.await_count == 2
  475. clear_call, bind_call = mock_client.merge_spool_extra.await_args_list
  476. assert clear_call.args[0] == 11
  477. assert clear_call.args[1] == {"tag": ""}
  478. assert bind_call.args[0] == 22
  479. assert bind_call.args[1] == {"tag": '"DEADBEEF"'}
  480. @pytest.mark.asyncio
  481. @pytest.mark.integration
  482. async def test_tag_removed(self, async_client: AsyncClient):
  483. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  484. mock_ws.broadcast = AsyncMock()
  485. resp = await async_client.post(
  486. f"{API}/nfc/tag-removed",
  487. json={"device_id": "sb-1", "tag_uid": "AABB1122"},
  488. )
  489. assert resp.status_code == 200
  490. msg = mock_ws.broadcast.call_args[0][0]
  491. assert msg["type"] == "spoolbuddy_tag_removed"
  492. assert msg["device_id"] == "sb-1"
  493. assert msg["tag_uid"] == "AABB1122"
  494. # ============================================================================
  495. # NFC write-tag endpoints
  496. # ============================================================================
  497. class TestWriteTagEndpoints:
  498. @pytest.mark.asyncio
  499. @pytest.mark.integration
  500. async def test_write_tag_queues_command(self, async_client: AsyncClient, device_factory, spool_factory):
  501. device = await device_factory(device_id="sb-wt")
  502. spool = await spool_factory(material="PLA", brand="Polymaker", color_name="Red", rgba="FF0000FF")
  503. resp = await async_client.post(
  504. f"{API}/nfc/write-tag",
  505. json={"device_id": device.device_id, "spool_id": spool.id},
  506. )
  507. assert resp.status_code == 200
  508. assert resp.json()["status"] == "queued"
  509. # Verify heartbeat returns write_tag command with payload
  510. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  511. mock_ws.broadcast = AsyncMock()
  512. hb = await async_client.post(
  513. f"{API}/devices/{device.device_id}/heartbeat",
  514. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 10},
  515. )
  516. hb_data = hb.json()
  517. assert hb_data["pending_command"] == "write_tag"
  518. assert hb_data["pending_write_payload"] is not None
  519. assert hb_data["pending_write_payload"]["spool_id"] == spool.id
  520. assert "ndef_data_hex" in hb_data["pending_write_payload"]
  521. @pytest.mark.asyncio
  522. @pytest.mark.integration
  523. async def test_write_tag_heartbeat_not_cleared(self, async_client: AsyncClient, device_factory, spool_factory):
  524. """write_tag command persists across heartbeats until write-result clears it."""
  525. device = await device_factory(device_id="sb-wt-persist")
  526. spool = await spool_factory(material="PETG")
  527. await async_client.post(
  528. f"{API}/nfc/write-tag",
  529. json={"device_id": device.device_id, "spool_id": spool.id},
  530. )
  531. # First heartbeat — command present
  532. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  533. mock_ws.broadcast = AsyncMock()
  534. hb1 = await async_client.post(
  535. f"{API}/devices/{device.device_id}/heartbeat",
  536. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 10},
  537. )
  538. assert hb1.json()["pending_command"] == "write_tag"
  539. # Second heartbeat — should still be present (not cleared like tare)
  540. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  541. mock_ws.broadcast = AsyncMock()
  542. hb2 = await async_client.post(
  543. f"{API}/devices/{device.device_id}/heartbeat",
  544. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 20},
  545. )
  546. assert hb2.json()["pending_command"] == "write_tag"
  547. @pytest.mark.asyncio
  548. @pytest.mark.integration
  549. async def test_write_tag_missing_spool_404(self, async_client: AsyncClient, device_factory):
  550. device = await device_factory(device_id="sb-wt-nospool")
  551. resp = await async_client.post(
  552. f"{API}/nfc/write-tag",
  553. json={"device_id": device.device_id, "spool_id": 99999},
  554. )
  555. assert resp.status_code == 404
  556. @pytest.mark.asyncio
  557. @pytest.mark.integration
  558. async def test_write_tag_missing_device_404(self, async_client: AsyncClient, spool_factory):
  559. spool = await spool_factory()
  560. resp = await async_client.post(
  561. f"{API}/nfc/write-tag",
  562. json={"device_id": "nonexistent", "spool_id": spool.id},
  563. )
  564. assert resp.status_code == 404
  565. @pytest.mark.asyncio
  566. @pytest.mark.integration
  567. async def test_write_result_success_links_tag(self, async_client: AsyncClient, device_factory, spool_factory):
  568. device = await device_factory(device_id="sb-wr", pending_command="write_tag")
  569. spool = await spool_factory(material="PLA", tag_uid=None)
  570. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  571. mock_ws.broadcast = AsyncMock()
  572. resp = await async_client.post(
  573. f"{API}/nfc/write-result",
  574. json={
  575. "device_id": device.device_id,
  576. "spool_id": spool.id,
  577. "tag_uid": "04AABB11223344",
  578. "success": True,
  579. },
  580. )
  581. assert resp.status_code == 200
  582. msg = mock_ws.broadcast.call_args[0][0]
  583. assert msg["type"] == "spoolbuddy_tag_written"
  584. assert msg["spool_id"] == spool.id
  585. assert msg["tag_uid"] == "04AABB11223344"
  586. # Verify spool got tag linked
  587. spool_resp = await async_client.get(f"/api/v1/inventory/spools/{spool.id}")
  588. spool_data = spool_resp.json()
  589. assert spool_data["tag_uid"] == "04AABB11223344"
  590. assert spool_data["tag_type"] == "ntag"
  591. assert spool_data["data_origin"] == "opentag3d"
  592. assert spool_data["encode_time"] is not None
  593. @pytest.mark.asyncio
  594. @pytest.mark.integration
  595. async def test_write_result_failure_broadcasts_error(
  596. self, async_client: AsyncClient, device_factory, spool_factory
  597. ):
  598. device = await device_factory(device_id="sb-wr-fail", pending_command="write_tag")
  599. spool = await spool_factory(material="PLA", tag_uid=None)
  600. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  601. mock_ws.broadcast = AsyncMock()
  602. resp = await async_client.post(
  603. f"{API}/nfc/write-result",
  604. json={
  605. "device_id": device.device_id,
  606. "spool_id": spool.id,
  607. "tag_uid": "04AABBCC",
  608. "success": False,
  609. "message": "Write or verification failed",
  610. },
  611. )
  612. assert resp.status_code == 200
  613. msg = mock_ws.broadcast.call_args[0][0]
  614. assert msg["type"] == "spoolbuddy_tag_write_failed"
  615. assert msg["message"] == "Write or verification failed"
  616. # Verify spool NOT linked
  617. spool_resp = await async_client.get(f"/api/v1/inventory/spools/{spool.id}")
  618. assert spool_resp.json()["tag_uid"] is None
  619. @pytest.mark.asyncio
  620. @pytest.mark.integration
  621. async def test_write_result_clears_pending_command(self, async_client: AsyncClient, device_factory, spool_factory):
  622. device = await device_factory(
  623. device_id="sb-wr-clear",
  624. pending_command="write_tag",
  625. pending_write_payload='{"spool_id": 1, "ndef_data_hex": "E110120003"}',
  626. )
  627. spool = await spool_factory()
  628. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  629. mock_ws.broadcast = AsyncMock()
  630. await async_client.post(
  631. f"{API}/nfc/write-result",
  632. json={
  633. "device_id": device.device_id,
  634. "spool_id": spool.id,
  635. "tag_uid": "AABBCCDD",
  636. "success": True,
  637. },
  638. )
  639. # Heartbeat should have no pending command
  640. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  641. mock_ws.broadcast = AsyncMock()
  642. hb = await async_client.post(
  643. f"{API}/devices/{device.device_id}/heartbeat",
  644. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 30},
  645. )
  646. assert hb.json()["pending_command"] is None
  647. assert hb.json()["pending_write_payload"] is None
  648. @pytest.mark.asyncio
  649. @pytest.mark.integration
  650. async def test_cancel_write(self, async_client: AsyncClient, device_factory, spool_factory):
  651. device = await device_factory(device_id="sb-cancel")
  652. spool = await spool_factory()
  653. # Queue a write
  654. await async_client.post(
  655. f"{API}/nfc/write-tag",
  656. json={"device_id": device.device_id, "spool_id": spool.id},
  657. )
  658. # Cancel it
  659. resp = await async_client.post(f"{API}/devices/{device.device_id}/cancel-write", json={})
  660. assert resp.status_code == 200
  661. # Heartbeat should have no pending command
  662. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  663. mock_ws.broadcast = AsyncMock()
  664. hb = await async_client.post(
  665. f"{API}/devices/{device.device_id}/heartbeat",
  666. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 10},
  667. )
  668. assert hb.json()["pending_command"] is None
  669. @pytest.mark.asyncio
  670. @pytest.mark.integration
  671. async def test_cancel_write_unknown_device_404(self, async_client: AsyncClient):
  672. resp = await async_client.post(f"{API}/devices/ghost/cancel-write", json={})
  673. assert resp.status_code == 404
  674. @pytest.mark.asyncio
  675. @pytest.mark.integration
  676. async def test_write_tag_ndef_data_is_valid(self, async_client: AsyncClient, device_factory, spool_factory):
  677. """Verify the NDEF data in the heartbeat is a valid OpenTag3D message."""
  678. device = await device_factory(device_id="sb-wt-ndef")
  679. spool = await spool_factory(
  680. material="PLA",
  681. brand="Polymaker",
  682. color_name="White",
  683. rgba="FFFFFFFF",
  684. label_weight=1000,
  685. )
  686. await async_client.post(
  687. f"{API}/nfc/write-tag",
  688. json={"device_id": device.device_id, "spool_id": spool.id},
  689. )
  690. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  691. mock_ws.broadcast = AsyncMock()
  692. hb = await async_client.post(
  693. f"{API}/devices/{device.device_id}/heartbeat",
  694. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 10},
  695. )
  696. payload = hb.json()["pending_write_payload"]
  697. ndef_bytes = bytes.fromhex(payload["ndef_data_hex"])
  698. # CC bytes
  699. assert ndef_bytes[:4] == bytes([0xE1, 0x10, 0x12, 0x00])
  700. # TLV type
  701. assert ndef_bytes[4] == 0x03
  702. # NDEF record: TNF=MIME, type=application/opentag3d
  703. assert ndef_bytes[6] == 0xD2
  704. assert ndef_bytes[9:30] == b"application/opentag3d"
  705. # Terminator
  706. assert ndef_bytes[-1] == 0xFE
  707. # Total size fits NTAG213
  708. assert len(ndef_bytes) <= 144
  709. # ============================================================================
  710. # Scale endpoints
  711. # ============================================================================
  712. class TestScaleEndpoints:
  713. @pytest.mark.asyncio
  714. @pytest.mark.integration
  715. async def test_scale_reading_broadcast(self, async_client: AsyncClient):
  716. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  717. mock_ws.broadcast = AsyncMock()
  718. resp = await async_client.post(
  719. f"{API}/scale/reading",
  720. json={
  721. "device_id": "sb-1",
  722. "weight_grams": 823.5,
  723. "stable": True,
  724. "raw_adc": 456789,
  725. },
  726. )
  727. assert resp.status_code == 200
  728. msg = mock_ws.broadcast.call_args[0][0]
  729. assert msg["type"] == "spoolbuddy_weight"
  730. assert msg["device_id"] == "sb-1"
  731. assert msg["weight_grams"] == 823.5
  732. assert msg["stable"] is True
  733. assert msg["raw_adc"] == 456789
  734. @pytest.mark.asyncio
  735. @pytest.mark.integration
  736. async def test_update_spool_weight_calculates_correctly(self, async_client: AsyncClient, spool_factory):
  737. # label=1000g, core=250g, scale reads 750g
  738. # net_filament = max(0, 750 - 250) = 500
  739. # weight_used = max(0, 1000 - 500) = 500
  740. spool = await spool_factory(label_weight=1000, core_weight=250, weight_used=0)
  741. resp = await async_client.post(
  742. f"{API}/scale/update-spool-weight",
  743. json={"spool_id": spool.id, "weight_grams": 750},
  744. )
  745. assert resp.status_code == 200
  746. data = resp.json()
  747. assert data["weight_used"] == 500
  748. @pytest.mark.asyncio
  749. @pytest.mark.integration
  750. async def test_update_spool_weight_full_spool(self, async_client: AsyncClient, spool_factory):
  751. # label=1000g, core=250g, scale reads 1250g (full spool)
  752. # net_filament = max(0, 1250 - 250) = 1000
  753. # weight_used = max(0, 1000 - 1000) = 0
  754. spool = await spool_factory(label_weight=1000, core_weight=250, weight_used=200)
  755. resp = await async_client.post(
  756. f"{API}/scale/update-spool-weight",
  757. json={"spool_id": spool.id, "weight_grams": 1250},
  758. )
  759. assert resp.status_code == 200
  760. data = resp.json()
  761. assert data["weight_used"] == 0
  762. @pytest.mark.asyncio
  763. @pytest.mark.integration
  764. async def test_update_spool_weight_stores_scale_reading(self, async_client: AsyncClient, spool_factory):
  765. """Verify last_scale_weight and last_weighed_at are stored after weight sync."""
  766. spool = await spool_factory(label_weight=1000, core_weight=250, weight_used=0)
  767. resp = await async_client.post(
  768. f"{API}/scale/update-spool-weight",
  769. json={"spool_id": spool.id, "weight_grams": 750},
  770. )
  771. assert resp.status_code == 200
  772. # Fetch the spool via inventory API to verify stored fields
  773. spool_resp = await async_client.get(f"/api/v1/inventory/spools/{spool.id}")
  774. assert spool_resp.status_code == 200
  775. spool_data = spool_resp.json()
  776. assert spool_data["last_scale_weight"] == 750
  777. assert spool_data["last_weighed_at"] is not None
  778. @pytest.mark.asyncio
  779. @pytest.mark.integration
  780. async def test_update_spool_weight_missing_spool_404(self, async_client: AsyncClient):
  781. resp = await async_client.post(
  782. f"{API}/scale/update-spool-weight",
  783. json={"spool_id": 99999, "weight_grams": 500},
  784. )
  785. assert resp.status_code == 404
  786. # ============================================================================
  787. # Calibration endpoints
  788. # ============================================================================
  789. class TestCalibrationEndpoints:
  790. @pytest.mark.asyncio
  791. @pytest.mark.integration
  792. async def test_tare_queues_command(self, async_client: AsyncClient, device_factory):
  793. await device_factory(device_id="sb-tare")
  794. resp = await async_client.post(f"{API}/devices/sb-tare/calibration/tare", json={})
  795. assert resp.status_code == 200
  796. assert resp.json()["status"] == "ok"
  797. # Verify pending_command via heartbeat
  798. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  799. mock_ws.broadcast = AsyncMock()
  800. hb = await async_client.post(
  801. f"{API}/devices/sb-tare/heartbeat",
  802. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 1},
  803. )
  804. assert hb.json()["pending_command"] == "tare"
  805. @pytest.mark.asyncio
  806. @pytest.mark.integration
  807. async def test_tare_unknown_device_404(self, async_client: AsyncClient):
  808. resp = await async_client.post(f"{API}/devices/ghost/calibration/tare", json={})
  809. assert resp.status_code == 404
  810. @pytest.mark.asyncio
  811. @pytest.mark.integration
  812. async def test_set_tare_offset(self, async_client: AsyncClient, device_factory):
  813. await device_factory(device_id="sb-st", calibration_factor=0.005)
  814. resp = await async_client.post(
  815. f"{API}/devices/sb-st/calibration/set-tare",
  816. json={"tare_offset": 54321},
  817. )
  818. assert resp.status_code == 200
  819. data = resp.json()
  820. assert data["tare_offset"] == 54321
  821. assert data["calibration_factor"] == pytest.approx(0.005)
  822. @pytest.mark.asyncio
  823. @pytest.mark.integration
  824. async def test_set_calibration_factor(self, async_client: AsyncClient, device_factory):
  825. # known_weight=200g, raw_adc=50000, tare=10000 → factor=200/(50000-10000)=0.005
  826. await device_factory(device_id="sb-cf", tare_offset=10000)
  827. resp = await async_client.post(
  828. f"{API}/devices/sb-cf/calibration/set-factor",
  829. json={"known_weight_grams": 200, "raw_adc": 50000},
  830. )
  831. assert resp.status_code == 200
  832. data = resp.json()
  833. assert data["calibration_factor"] == pytest.approx(0.005)
  834. assert data["tare_offset"] == 10000
  835. @pytest.mark.asyncio
  836. @pytest.mark.integration
  837. async def test_set_calibration_factor_zero_delta_400(self, async_client: AsyncClient, device_factory):
  838. # raw_adc == tare_offset → delta is 0 → 400 error
  839. await device_factory(device_id="sb-zero", tare_offset=5000)
  840. resp = await async_client.post(
  841. f"{API}/devices/sb-zero/calibration/set-factor",
  842. json={"known_weight_grams": 100, "raw_adc": 5000},
  843. )
  844. assert resp.status_code == 400
  845. @pytest.mark.asyncio
  846. @pytest.mark.integration
  847. async def test_get_calibration(self, async_client: AsyncClient, device_factory):
  848. await device_factory(
  849. device_id="sb-gcal",
  850. tare_offset=11111,
  851. calibration_factor=0.0042,
  852. )
  853. resp = await async_client.get(f"{API}/devices/sb-gcal/calibration")
  854. assert resp.status_code == 200
  855. data = resp.json()
  856. assert data["tare_offset"] == 11111
  857. assert data["calibration_factor"] == pytest.approx(0.0042)
  858. # ============================================================================
  859. # Display endpoints
  860. # ============================================================================
  861. class TestDisplayEndpoints:
  862. @pytest.mark.asyncio
  863. @pytest.mark.integration
  864. async def test_update_display_settings(self, async_client: AsyncClient, device_factory):
  865. await device_factory(device_id="sb-disp", display_brightness=100, display_blank_timeout=0)
  866. resp = await async_client.put(
  867. f"{API}/devices/sb-disp/display",
  868. json={"brightness": 75, "blank_timeout": 300},
  869. )
  870. assert resp.status_code == 200
  871. data = resp.json()
  872. assert data["brightness"] == 75
  873. assert data["blank_timeout"] == 300
  874. @pytest.mark.asyncio
  875. @pytest.mark.integration
  876. async def test_update_display_persists_via_heartbeat(self, async_client: AsyncClient, device_factory):
  877. await device_factory(device_id="sb-disp-hb")
  878. await async_client.put(
  879. f"{API}/devices/sb-disp-hb/display",
  880. json={"brightness": 50, "blank_timeout": 600},
  881. )
  882. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  883. mock_ws.broadcast = AsyncMock()
  884. hb = await async_client.post(
  885. f"{API}/devices/sb-disp-hb/heartbeat",
  886. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 10},
  887. )
  888. assert hb.json()["display_brightness"] == 50
  889. assert hb.json()["display_blank_timeout"] == 600
  890. @pytest.mark.asyncio
  891. @pytest.mark.integration
  892. async def test_update_display_unknown_device_404(self, async_client: AsyncClient):
  893. resp = await async_client.put(
  894. f"{API}/devices/ghost/display",
  895. json={"brightness": 50, "blank_timeout": 60},
  896. )
  897. assert resp.status_code == 404
  898. @pytest.mark.asyncio
  899. @pytest.mark.integration
  900. async def test_update_display_validates_brightness(self, async_client: AsyncClient, device_factory):
  901. await device_factory(device_id="sb-disp-val")
  902. resp = await async_client.put(
  903. f"{API}/devices/sb-disp-val/display",
  904. json={"brightness": 150, "blank_timeout": 0},
  905. )
  906. assert resp.status_code == 422 # Validation error: brightness > 100
  907. @pytest.mark.asyncio
  908. @pytest.mark.integration
  909. async def test_get_display_settings(self, async_client: AsyncClient, device_factory):
  910. """The kiosk idle watchdog (install/spoolbuddy-idle.sh) reads this
  911. endpoint on autostart to configure swayidle with the user-selected
  912. blank timeout before launching. See issue #937."""
  913. await device_factory(device_id="sb-disp-get", display_brightness=60, display_blank_timeout=450)
  914. resp = await async_client.get(f"{API}/devices/sb-disp-get/display")
  915. assert resp.status_code == 200
  916. data = resp.json()
  917. assert data["brightness"] == 60
  918. assert data["blank_timeout"] == 450
  919. @pytest.mark.asyncio
  920. @pytest.mark.integration
  921. async def test_get_display_unknown_device_404(self, async_client: AsyncClient):
  922. resp = await async_client.get(f"{API}/devices/ghost/display")
  923. assert resp.status_code == 404
  924. # ============================================================================
  925. # Update endpoints
  926. # ============================================================================
  927. class TestUpdateEndpoints:
  928. @pytest.mark.asyncio
  929. @pytest.mark.integration
  930. async def test_trigger_update_starts_ssh_update(self, async_client: AsyncClient, device_factory):
  931. await device_factory(device_id="sb-upd")
  932. with (
  933. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  934. patch("backend.app.services.spoolbuddy_ssh.perform_ssh_update", new_callable=AsyncMock),
  935. ):
  936. mock_ws.broadcast = AsyncMock()
  937. resp = await async_client.post(f"{API}/devices/sb-upd/update")
  938. assert resp.status_code == 200
  939. assert resp.json()["status"] == "ok"
  940. @pytest.mark.asyncio
  941. @pytest.mark.integration
  942. async def test_trigger_update_offline_device_409(self, async_client: AsyncClient, device_factory):
  943. await device_factory(
  944. device_id="sb-upd-off",
  945. last_seen=datetime.now(timezone.utc) - timedelta(seconds=120),
  946. )
  947. resp = await async_client.post(f"{API}/devices/sb-upd-off/update")
  948. assert resp.status_code == 409
  949. @pytest.mark.asyncio
  950. @pytest.mark.integration
  951. async def test_trigger_update_unknown_device_404(self, async_client: AsyncClient):
  952. resp = await async_client.post(f"{API}/devices/ghost/update")
  953. assert resp.status_code == 404
  954. @pytest.mark.asyncio
  955. @pytest.mark.integration
  956. async def test_trigger_update_already_updating(self, async_client: AsyncClient, device_factory):
  957. await device_factory(device_id="sb-upd-dup", update_status="updating")
  958. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  959. mock_ws.broadcast = AsyncMock()
  960. resp = await async_client.post(f"{API}/devices/sb-upd-dup/update")
  961. assert resp.status_code == 200
  962. assert resp.json()["status"] == "already_updating"
  963. @pytest.mark.asyncio
  964. @pytest.mark.integration
  965. async def test_report_update_status_updating(self, async_client: AsyncClient, device_factory):
  966. await device_factory(device_id="sb-upd-st", pending_command="update", update_status="pending")
  967. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  968. mock_ws.broadcast = AsyncMock()
  969. resp = await async_client.post(
  970. f"{API}/devices/sb-upd-st/update-status",
  971. json={"status": "updating", "message": "Fetching latest code..."},
  972. )
  973. assert resp.status_code == 200
  974. mock_ws.broadcast.assert_called_once()
  975. msg = mock_ws.broadcast.call_args[0][0]
  976. assert msg["type"] == "spoolbuddy_update"
  977. assert msg["update_status"] == "updating"
  978. @pytest.mark.asyncio
  979. @pytest.mark.integration
  980. async def test_report_update_status_complete_clears_command(self, async_client: AsyncClient, device_factory):
  981. await device_factory(device_id="sb-upd-done", pending_command="update", update_status="updating")
  982. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  983. mock_ws.broadcast = AsyncMock()
  984. await async_client.post(
  985. f"{API}/devices/sb-upd-done/update-status",
  986. json={"status": "complete", "message": "Update complete, restarting..."},
  987. )
  988. # Heartbeat should have no pending command
  989. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  990. mock_ws.broadcast = AsyncMock()
  991. hb = await async_client.post(
  992. f"{API}/devices/sb-upd-done/heartbeat",
  993. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 10},
  994. )
  995. assert hb.json()["pending_command"] is None
  996. @pytest.mark.asyncio
  997. @pytest.mark.integration
  998. async def test_report_update_status_error(self, async_client: AsyncClient, device_factory):
  999. await device_factory(device_id="sb-upd-err", pending_command="update", update_status="updating")
  1000. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  1001. mock_ws.broadcast = AsyncMock()
  1002. resp = await async_client.post(
  1003. f"{API}/devices/sb-upd-err/update-status",
  1004. json={"status": "error", "message": "git fetch failed: network unreachable"},
  1005. )
  1006. assert resp.status_code == 200
  1007. msg = mock_ws.broadcast.call_args[0][0]
  1008. assert msg["update_status"] == "error"
  1009. assert "git fetch failed" in msg["update_message"]
  1010. @pytest.mark.asyncio
  1011. @pytest.mark.integration
  1012. async def test_report_update_status_unknown_device_404(self, async_client: AsyncClient):
  1013. resp = await async_client.post(
  1014. f"{API}/devices/ghost/update-status",
  1015. json={"status": "updating", "message": "test"},
  1016. )
  1017. assert resp.status_code == 404
  1018. @pytest.mark.asyncio
  1019. @pytest.mark.integration
  1020. async def test_report_update_status_invalid_status_422(self, async_client: AsyncClient, device_factory):
  1021. """Arbitrary status strings must be rejected with 422 (H2: UpdateStatusRequest validation)."""
  1022. await device_factory(device_id="sb-upd-inv")
  1023. resp = await async_client.post(
  1024. f"{API}/devices/sb-upd-inv/update-status",
  1025. json={"status": "hacked", "message": "injected"},
  1026. )
  1027. assert resp.status_code == 422
  1028. @pytest.mark.asyncio
  1029. @pytest.mark.integration
  1030. async def test_report_update_status_oversized_message_422(self, async_client: AsyncClient, device_factory):
  1031. """Message exceeding 255 chars must be rejected with 422 (H2/M4)."""
  1032. await device_factory(device_id="sb-upd-big")
  1033. resp = await async_client.post(
  1034. f"{API}/devices/sb-upd-big/update-status",
  1035. json={"status": "updating", "message": "x" * 256},
  1036. )
  1037. assert resp.status_code == 422
  1038. @pytest.mark.asyncio
  1039. @pytest.mark.integration
  1040. async def test_ssh_public_key_error_does_not_leak_exception_text(self, async_client: AsyncClient):
  1041. """SSH public-key 500 must not expose raw exception details (M3)."""
  1042. from backend.app.services.spoolbuddy_ssh import get_public_key
  1043. with patch(
  1044. "backend.app.services.spoolbuddy_ssh.get_public_key",
  1045. AsyncMock(side_effect=RuntimeError("REDACT_ME internal path /data/keys/id_ed25519")),
  1046. ):
  1047. resp = await async_client.get(f"{API}/ssh/public-key")
  1048. assert resp.status_code == 500
  1049. body = resp.json()["detail"]
  1050. assert "REDACT_ME" not in body
  1051. assert "/data/keys" not in body
  1052. assert "id_ed25519" not in body
  1053. @pytest.mark.asyncio
  1054. @pytest.mark.integration
  1055. async def test_device_response_includes_update_fields(self, async_client: AsyncClient, device_factory):
  1056. await device_factory(device_id="sb-upd-resp", update_status="complete", update_message="Done!")
  1057. resp = await async_client.get(f"{API}/devices")
  1058. assert resp.status_code == 200
  1059. device = next(d for d in resp.json() if d["device_id"] == "sb-upd-resp")
  1060. assert device["update_status"] == "complete"
  1061. assert device["update_message"] == "Done!"
  1062. @pytest.mark.asyncio
  1063. @pytest.mark.integration
  1064. async def test_update_check_returns_version_info(self, async_client: AsyncClient, device_factory):
  1065. """GET /devices/{id}/update-check compares device version against APP_VERSION."""
  1066. await device_factory(device_id="sb-uc", firmware_version="0.1.0")
  1067. resp = await async_client.get(f"{API}/devices/sb-uc/update-check")
  1068. assert resp.status_code == 200
  1069. data = resp.json()
  1070. assert data["current_version"] == "0.1.0"
  1071. assert data["latest_version"] is not None
  1072. assert data["update_available"] is True
  1073. @pytest.mark.asyncio
  1074. @pytest.mark.integration
  1075. async def test_update_check_up_to_date(self, async_client: AsyncClient, device_factory):
  1076. from backend.app.core.config import APP_VERSION
  1077. await device_factory(device_id="sb-uc2", firmware_version=APP_VERSION)
  1078. resp = await async_client.get(f"{API}/devices/sb-uc2/update-check")
  1079. assert resp.status_code == 200
  1080. assert resp.json()["update_available"] is False
  1081. @pytest.mark.asyncio
  1082. @pytest.mark.integration
  1083. async def test_update_check_unknown_device_404(self, async_client: AsyncClient):
  1084. resp = await async_client.get(f"{API}/devices/ghost/update-check")
  1085. assert resp.status_code == 404
  1086. @pytest.mark.asyncio
  1087. @pytest.mark.integration
  1088. async def test_trigger_update_broadcasts_websocket(self, async_client: AsyncClient, device_factory):
  1089. await device_factory(device_id="sb-upd-ws")
  1090. with (
  1091. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  1092. patch("backend.app.services.spoolbuddy_ssh.perform_ssh_update", new_callable=AsyncMock),
  1093. ):
  1094. mock_ws.broadcast = AsyncMock()
  1095. await async_client.post(f"{API}/devices/sb-upd-ws/update")
  1096. mock_ws.broadcast.assert_called_once()
  1097. msg = mock_ws.broadcast.call_args[0][0]
  1098. assert msg["type"] == "spoolbuddy_update"
  1099. assert msg["device_id"] == "sb-upd-ws"
  1100. assert msg["update_status"] == "pending"
  1101. # ============================================================================
  1102. # System command endpoints
  1103. # ============================================================================
  1104. class TestSystemCommandEndpoints:
  1105. @pytest.mark.asyncio
  1106. @pytest.mark.integration
  1107. async def test_queue_reboot(self, async_client: AsyncClient, device_factory):
  1108. await device_factory(device_id="sb-reboot")
  1109. resp = await async_client.post(
  1110. f"{API}/devices/sb-reboot/system/command",
  1111. json={"command": "reboot"},
  1112. )
  1113. assert resp.status_code == 200
  1114. data = resp.json()
  1115. assert data["status"] == "queued"
  1116. assert data["command"] == "reboot"
  1117. @pytest.mark.asyncio
  1118. @pytest.mark.integration
  1119. async def test_queue_shutdown(self, async_client: AsyncClient, device_factory):
  1120. await device_factory(device_id="sb-shutdown")
  1121. resp = await async_client.post(
  1122. f"{API}/devices/sb-shutdown/system/command",
  1123. json={"command": "shutdown"},
  1124. )
  1125. assert resp.status_code == 200
  1126. assert resp.json()["command"] == "shutdown"
  1127. @pytest.mark.asyncio
  1128. @pytest.mark.integration
  1129. async def test_queue_restart_daemon(self, async_client: AsyncClient, device_factory):
  1130. await device_factory(device_id="sb-rd")
  1131. resp = await async_client.post(
  1132. f"{API}/devices/sb-rd/system/command",
  1133. json={"command": "restart_daemon"},
  1134. )
  1135. assert resp.status_code == 200
  1136. assert resp.json()["command"] == "restart_daemon"
  1137. @pytest.mark.asyncio
  1138. @pytest.mark.integration
  1139. async def test_queue_restart_browser(self, async_client: AsyncClient, device_factory):
  1140. await device_factory(device_id="sb-rb")
  1141. resp = await async_client.post(
  1142. f"{API}/devices/sb-rb/system/command",
  1143. json={"command": "restart_browser"},
  1144. )
  1145. assert resp.status_code == 200
  1146. assert resp.json()["command"] == "restart_browser"
  1147. @pytest.mark.asyncio
  1148. @pytest.mark.integration
  1149. async def test_invalid_command_rejected(self, async_client: AsyncClient, device_factory):
  1150. await device_factory(device_id="sb-invalid")
  1151. resp = await async_client.post(
  1152. f"{API}/devices/sb-invalid/system/command",
  1153. json={"command": "format_disk"},
  1154. )
  1155. assert resp.status_code == 400
  1156. assert "Invalid command" in resp.json()["detail"]
  1157. @pytest.mark.asyncio
  1158. @pytest.mark.integration
  1159. async def test_command_unknown_device_404(self, async_client: AsyncClient):
  1160. resp = await async_client.post(
  1161. f"{API}/devices/ghost/system/command",
  1162. json={"command": "reboot"},
  1163. )
  1164. assert resp.status_code == 404
  1165. @pytest.mark.asyncio
  1166. @pytest.mark.integration
  1167. async def test_command_offline_device_409(self, async_client: AsyncClient, device_factory):
  1168. await device_factory(
  1169. device_id="sb-offline-cmd",
  1170. last_seen=datetime.now(timezone.utc) - timedelta(seconds=120),
  1171. )
  1172. resp = await async_client.post(
  1173. f"{API}/devices/sb-offline-cmd/system/command",
  1174. json={"command": "reboot"},
  1175. )
  1176. assert resp.status_code == 409
  1177. assert "offline" in resp.json()["detail"].lower()
  1178. @pytest.mark.asyncio
  1179. @pytest.mark.integration
  1180. async def test_command_sets_pending_command(self, async_client: AsyncClient, device_factory, db_session):
  1181. device = await device_factory(device_id="sb-pending")
  1182. await async_client.post(
  1183. f"{API}/devices/sb-pending/system/command",
  1184. json={"command": "restart_daemon"},
  1185. )
  1186. await db_session.refresh(device)
  1187. assert device.pending_command == "restart_daemon"
  1188. @pytest.mark.asyncio
  1189. @pytest.mark.integration
  1190. async def test_heartbeat_clears_system_command(self, async_client: AsyncClient, device_factory):
  1191. """System commands (reboot/shutdown/restart_*) are fire-and-forget — heartbeat clears them."""
  1192. await device_factory(device_id="sb-hb-clear")
  1193. # Queue a command
  1194. await async_client.post(
  1195. f"{API}/devices/sb-hb-clear/system/command",
  1196. json={"command": "restart_browser"},
  1197. )
  1198. # Heartbeat should return the command and clear it
  1199. resp = await async_client.post(
  1200. f"{API}/devices/sb-hb-clear/heartbeat",
  1201. json={"nfc_ok": True, "scale_ok": True, "uptime_s": 100},
  1202. )
  1203. assert resp.status_code == 200
  1204. data = resp.json()
  1205. assert data["pending_command"] == "restart_browser"
  1206. # ============================================================================
  1207. # Spoolman-aware SpoolBuddy endpoints
  1208. # ============================================================================
  1209. @pytest.fixture
  1210. async def spoolman_settings(db_session: AsyncSession):
  1211. """Create Spoolman settings in the database (enabled with URL)."""
  1212. from backend.app.models.settings import Settings
  1213. settings = [
  1214. Settings(key="spoolman_enabled", value="true"),
  1215. Settings(key="spoolman_url", value="http://spoolman.local:7912"),
  1216. ]
  1217. for s in settings:
  1218. db_session.add(s)
  1219. await db_session.commit()
  1220. return settings
  1221. def _mock_spoolman_client(base_url: str = "http://spoolman.local:7912") -> MagicMock:
  1222. client = MagicMock()
  1223. client.base_url = base_url
  1224. client.get_spools = AsyncMock(return_value=[])
  1225. client.get_spool = AsyncMock(return_value={})
  1226. client.find_spool_by_tag = AsyncMock(return_value=None)
  1227. client.update_spool = AsyncMock(return_value=None)
  1228. client.merge_spool_extra = AsyncMock(return_value={"id": 0})
  1229. return client
  1230. def _spoolman_spool_fixture(
  1231. spool_id: int,
  1232. spool_weight: float = 196.0,
  1233. filament_weight: float = 1000.0,
  1234. spool_level_spool_weight=None,
  1235. ) -> dict:
  1236. """Build a minimal Spoolman spool dict with realistic core weight from filament.spool_weight."""
  1237. raw = {
  1238. "id": spool_id,
  1239. "filament": {"weight": filament_weight, "spool_weight": spool_weight},
  1240. "used_weight": 0.0,
  1241. }
  1242. if spool_level_spool_weight is not None:
  1243. raw["spool_weight"] = spool_level_spool_weight
  1244. return raw
  1245. class TestUpdateSpoolWeightSpoolman:
  1246. """update-spool-weight routes to Spoolman when Spoolman mode is active."""
  1247. @pytest.mark.asyncio
  1248. @pytest.mark.integration
  1249. async def test_spoolman_mode_uses_filament_spool_weight(self, async_client: AsyncClient, spoolman_settings):
  1250. """core_weight comes from filament.spool_weight, not a hardcoded constant."""
  1251. sm_spool = _spoolman_spool_fixture(42, spool_weight=196.0, filament_weight=1000.0)
  1252. mock_client = _mock_spoolman_client()
  1253. mock_client.get_spool = AsyncMock(return_value=sm_spool)
  1254. mock_client.update_spool = AsyncMock(return_value=sm_spool)
  1255. with (
  1256. patch(
  1257. "backend.app.services.spoolman.get_spoolman_client",
  1258. AsyncMock(return_value=mock_client),
  1259. ),
  1260. patch(
  1261. "backend.app.services.spoolman.init_spoolman_client",
  1262. AsyncMock(return_value=mock_client),
  1263. ),
  1264. ):
  1265. resp = await async_client.post(
  1266. f"{API}/scale/update-spool-weight",
  1267. json={"spool_id": 42, "weight_grams": 750},
  1268. )
  1269. assert resp.status_code == 200
  1270. data = resp.json()
  1271. assert data["status"] == "ok"
  1272. # remaining = max(0, 750 - 196) = 554 → weight_used = 1000 - 554 = 446
  1273. assert data["weight_used"] == pytest.approx(446.0)
  1274. mock_client.update_spool.assert_called_once_with(spool_id=42, remaining_weight=pytest.approx(554.0))
  1275. @pytest.mark.asyncio
  1276. @pytest.mark.integration
  1277. async def test_spoolman_mode_clamps_remaining_to_zero(self, async_client: AsyncClient, spoolman_settings):
  1278. """Scale weight below core weight → remaining_weight = 0."""
  1279. sm_spool = _spoolman_spool_fixture(7, spool_weight=196.0, filament_weight=1000.0)
  1280. mock_client = _mock_spoolman_client()
  1281. mock_client.get_spool = AsyncMock(return_value=sm_spool)
  1282. mock_client.update_spool = AsyncMock(return_value=sm_spool)
  1283. with (
  1284. patch(
  1285. "backend.app.services.spoolman.get_spoolman_client",
  1286. AsyncMock(return_value=mock_client),
  1287. ),
  1288. patch(
  1289. "backend.app.services.spoolman.init_spoolman_client",
  1290. AsyncMock(return_value=mock_client),
  1291. ),
  1292. ):
  1293. resp = await async_client.post(
  1294. f"{API}/scale/update-spool-weight",
  1295. json={"spool_id": 7, "weight_grams": 100},
  1296. )
  1297. assert resp.status_code == 200
  1298. mock_client.update_spool.assert_called_once_with(spool_id=7, remaining_weight=0.0)
  1299. @pytest.mark.asyncio
  1300. @pytest.mark.integration
  1301. async def test_spoolman_mode_404_when_spool_not_found(self, async_client: AsyncClient, spoolman_settings):
  1302. """404 when Spoolman doesn't know the spool."""
  1303. mock_client = _mock_spoolman_client()
  1304. mock_client.get_spool = AsyncMock(side_effect=SpoolmanNotFoundError("Spool 9999 not found"))
  1305. with (
  1306. patch(
  1307. "backend.app.services.spoolman.get_spoolman_client",
  1308. AsyncMock(return_value=mock_client),
  1309. ),
  1310. patch(
  1311. "backend.app.services.spoolman.init_spoolman_client",
  1312. AsyncMock(return_value=mock_client),
  1313. ),
  1314. ):
  1315. resp = await async_client.post(
  1316. f"{API}/scale/update-spool-weight",
  1317. json={"spool_id": 9999, "weight_grams": 500},
  1318. )
  1319. assert resp.status_code == 404
  1320. @pytest.mark.asyncio
  1321. @pytest.mark.integration
  1322. async def test_spoolman_mode_503_on_client_failure(self, async_client: AsyncClient, spoolman_settings):
  1323. """503 is returned when Spoolman is unreachable during weight update."""
  1324. sm_spool = _spoolman_spool_fixture(99)
  1325. mock_client = _mock_spoolman_client()
  1326. mock_client.get_spool = AsyncMock(return_value=sm_spool)
  1327. mock_client.update_spool = AsyncMock(side_effect=SpoolmanUnavailableError("Spoolman down"))
  1328. with (
  1329. patch(
  1330. "backend.app.services.spoolman.get_spoolman_client",
  1331. AsyncMock(return_value=mock_client),
  1332. ),
  1333. patch(
  1334. "backend.app.services.spoolman.init_spoolman_client",
  1335. AsyncMock(return_value=mock_client),
  1336. ),
  1337. ):
  1338. resp = await async_client.post(
  1339. f"{API}/scale/update-spool-weight",
  1340. json={"spool_id": 99, "weight_grams": 500},
  1341. )
  1342. assert resp.status_code == 503
  1343. @pytest.mark.asyncio
  1344. @pytest.mark.integration
  1345. async def test_local_mode_unchanged(self, async_client: AsyncClient, spool_factory):
  1346. """When Spoolman is NOT enabled, local DB update still works."""
  1347. spool = await spool_factory(label_weight=1000, core_weight=250, weight_used=0)
  1348. resp = await async_client.post(
  1349. f"{API}/scale/update-spool-weight",
  1350. json={"spool_id": spool.id, "weight_grams": 750},
  1351. )
  1352. assert resp.status_code == 200
  1353. assert resp.json()["weight_used"] == 500
  1354. @pytest.mark.asyncio
  1355. @pytest.mark.integration
  1356. async def test_stale_local_row_does_not_shadow_spoolman(
  1357. self, async_client: AsyncClient, db_session, spool_factory, spoolman_settings
  1358. ):
  1359. """Regression for #1530: when Spoolman mode is on, a stale local Spool
  1360. sharing the same numeric id must NOT absorb the update — Spoolman is
  1361. the authoritative target."""
  1362. local_spool = await spool_factory(label_weight=1000, core_weight=250, weight_used=0)
  1363. # Spoolman spool with the SAME numeric id as the local stale row.
  1364. sm_spool = _spoolman_spool_fixture(local_spool.id, spool_weight=250.0, filament_weight=1000.0)
  1365. mock_client = _mock_spoolman_client()
  1366. mock_client.get_spool = AsyncMock(return_value=sm_spool)
  1367. mock_client.update_spool = AsyncMock(return_value=sm_spool)
  1368. with (
  1369. patch("backend.app.services.spoolman.get_spoolman_client", AsyncMock(return_value=mock_client)),
  1370. patch("backend.app.services.spoolman.init_spoolman_client", AsyncMock(return_value=mock_client)),
  1371. ):
  1372. resp = await async_client.post(
  1373. f"{API}/scale/update-spool-weight",
  1374. json={"spool_id": local_spool.id, "weight_grams": 750},
  1375. )
  1376. assert resp.status_code == 200
  1377. # Spoolman got the update.
  1378. mock_client.update_spool.assert_called_once_with(spool_id=local_spool.id, remaining_weight=pytest.approx(500.0))
  1379. # Local row is untouched — the bug was that the local update silently
  1380. # absorbed the request while Spoolman stayed stale.
  1381. await db_session.refresh(local_spool)
  1382. assert local_spool.weight_used == 0
  1383. assert local_spool.last_scale_weight is None
  1384. @pytest.mark.asyncio
  1385. @pytest.mark.integration
  1386. async def test_spool_level_spool_weight_takes_priority(self, async_client: AsyncClient, spoolman_settings):
  1387. """spool.spool_weight overrides filament.spool_weight for tare calculation."""
  1388. sm_spool = _spoolman_spool_fixture(42, spool_weight=196.0, filament_weight=1000.0, spool_level_spool_weight=300)
  1389. mock_client = _mock_spoolman_client()
  1390. mock_client.get_spool = AsyncMock(return_value=sm_spool)
  1391. mock_client.update_spool = AsyncMock(return_value=sm_spool)
  1392. with (
  1393. patch("backend.app.services.spoolman.get_spoolman_client", AsyncMock(return_value=mock_client)),
  1394. patch("backend.app.services.spoolman.init_spoolman_client", AsyncMock(return_value=mock_client)),
  1395. ):
  1396. resp = await async_client.post(
  1397. f"{API}/scale/update-spool-weight",
  1398. json={"spool_id": 42, "weight_grams": 750},
  1399. )
  1400. assert resp.status_code == 200
  1401. # remaining = 750 - 300 = 450; weight_used = 1000 - 450 = 550
  1402. assert resp.json()["weight_used"] == pytest.approx(550.0)
  1403. mock_client.update_spool.assert_called_once_with(spool_id=42, remaining_weight=pytest.approx(450.0))
  1404. @pytest.mark.asyncio
  1405. @pytest.mark.integration
  1406. async def test_spool_level_zero_spool_weight_not_treated_as_missing(
  1407. self, async_client: AsyncClient, spoolman_settings
  1408. ):
  1409. """spool.spool_weight=0 is valid (0g tare), not treated as missing/fallback."""
  1410. sm_spool = _spoolman_spool_fixture(42, spool_weight=196.0, filament_weight=1000.0, spool_level_spool_weight=0)
  1411. mock_client = _mock_spoolman_client()
  1412. mock_client.get_spool = AsyncMock(return_value=sm_spool)
  1413. mock_client.update_spool = AsyncMock(return_value=sm_spool)
  1414. with (
  1415. patch("backend.app.services.spoolman.get_spoolman_client", AsyncMock(return_value=mock_client)),
  1416. patch("backend.app.services.spoolman.init_spoolman_client", AsyncMock(return_value=mock_client)),
  1417. ):
  1418. resp = await async_client.post(
  1419. f"{API}/scale/update-spool-weight",
  1420. json={"spool_id": 42, "weight_grams": 750},
  1421. )
  1422. assert resp.status_code == 200
  1423. # remaining = 750 - 0 = 750; weight_used = 1000 - 750 = 250
  1424. assert resp.json()["weight_used"] == pytest.approx(250.0)
  1425. mock_client.update_spool.assert_called_once_with(spool_id=42, remaining_weight=pytest.approx(750.0))
  1426. @pytest.mark.asyncio
  1427. @pytest.mark.integration
  1428. async def test_both_levels_none_uses_250g_fallback_and_warns(self, async_client: AsyncClient, spoolman_settings):
  1429. """When both spool_weight and filament.spool_weight are None, 250g fallback is used with a warning."""
  1430. sm_spool = {"id": 42, "filament": {"weight": 1000.0, "spool_weight": None}, "used_weight": 0.0}
  1431. mock_client = _mock_spoolman_client()
  1432. mock_client.get_spool = AsyncMock(return_value=sm_spool)
  1433. mock_client.update_spool = AsyncMock(return_value=sm_spool)
  1434. with (
  1435. patch("backend.app.services.spoolman.get_spoolman_client", AsyncMock(return_value=mock_client)),
  1436. patch("backend.app.services.spoolman.init_spoolman_client", AsyncMock(return_value=mock_client)),
  1437. ):
  1438. resp = await async_client.post(
  1439. f"{API}/scale/update-spool-weight",
  1440. json={"spool_id": 42, "weight_grams": 750},
  1441. )
  1442. assert resp.status_code == 200
  1443. # remaining = 750 - 250 = 500; weight_used = 1000 - 500 = 500
  1444. assert resp.json()["weight_used"] == pytest.approx(500.0)
  1445. assert resp.json().get("warnings")
  1446. class TestTagScannedSpoolmanFallback:
  1447. """nfc/tag-scanned falls back to Spoolman when local DB has no match."""
  1448. @pytest.mark.asyncio
  1449. @pytest.mark.integration
  1450. async def test_spoolman_fallback_on_local_miss(self, async_client: AsyncClient, spoolman_settings):
  1451. raw_spool = {
  1452. "id": 5,
  1453. "filament": {
  1454. "material": "PETG",
  1455. "name": "PETG Basic",
  1456. "color_hex": "00FF00",
  1457. "weight": 1000,
  1458. "vendor": {"name": "Polymaker"},
  1459. },
  1460. "used_weight": 100.0,
  1461. "archived": False,
  1462. "registered": "2024-01-01T00:00:00+00:00",
  1463. "extra": {"tag": '"DEADBEEF12345678"'},
  1464. }
  1465. mock_client = _mock_spoolman_client()
  1466. mock_client.get_spools = AsyncMock(return_value=[raw_spool])
  1467. mock_client.find_spool_by_tag = AsyncMock(return_value=raw_spool)
  1468. with (
  1469. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  1470. patch(
  1471. "backend.app.api.routes.spoolbuddy.get_spool_by_tag",
  1472. new_callable=AsyncMock,
  1473. return_value=None,
  1474. ),
  1475. patch(
  1476. "backend.app.services.spoolman.get_spoolman_client",
  1477. AsyncMock(return_value=mock_client),
  1478. ),
  1479. patch(
  1480. "backend.app.services.spoolman.init_spoolman_client",
  1481. AsyncMock(return_value=mock_client),
  1482. ),
  1483. ):
  1484. mock_ws.broadcast = AsyncMock()
  1485. resp = await async_client.post(
  1486. f"{API}/nfc/tag-scanned",
  1487. json={"device_id": "sb-1", "tag_uid": "DEADBEEF12345678"},
  1488. )
  1489. assert resp.status_code == 200
  1490. data = resp.json()
  1491. assert data["matched"] is True
  1492. assert data["spool_id"] == 5
  1493. mock_ws.broadcast.assert_called_once()
  1494. msg = mock_ws.broadcast.call_args[0][0]
  1495. assert msg["type"] == "spoolbuddy_tag_matched"
  1496. assert msg["spool"]["id"] == 5
  1497. assert msg["spool"]["material"] == "PETG"
  1498. @pytest.mark.asyncio
  1499. @pytest.mark.integration
  1500. async def test_spoolman_fallback_unknown_when_no_spoolman_match(self, async_client: AsyncClient, spoolman_settings):
  1501. """Unknown tag broadcast when both local DB and Spoolman miss."""
  1502. mock_client = _mock_spoolman_client()
  1503. mock_client.get_spools = AsyncMock(return_value=[])
  1504. mock_client.find_spool_by_tag = AsyncMock(return_value=None)
  1505. with (
  1506. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  1507. patch(
  1508. "backend.app.api.routes.spoolbuddy.get_spool_by_tag",
  1509. new_callable=AsyncMock,
  1510. return_value=None,
  1511. ),
  1512. patch(
  1513. "backend.app.services.spoolman.get_spoolman_client",
  1514. AsyncMock(return_value=mock_client),
  1515. ),
  1516. patch(
  1517. "backend.app.services.spoolman.init_spoolman_client",
  1518. AsyncMock(return_value=mock_client),
  1519. ),
  1520. ):
  1521. mock_ws.broadcast = AsyncMock()
  1522. resp = await async_client.post(
  1523. f"{API}/nfc/tag-scanned",
  1524. json={"device_id": "sb-1", "tag_uid": "UNKNOWN0000000FF"},
  1525. )
  1526. assert resp.status_code == 200
  1527. data = resp.json()
  1528. assert data["matched"] is False
  1529. assert data["spool_id"] is None
  1530. mock_ws.broadcast.assert_called_once()
  1531. msg = mock_ws.broadcast.call_args[0][0]
  1532. assert msg["type"] == "spoolbuddy_unknown_tag"
  1533. @pytest.mark.asyncio
  1534. @pytest.mark.integration
  1535. async def test_malformed_spoolman_data_degrades_gracefully(self, async_client: AsyncClient, spoolman_settings):
  1536. """ValueError from _map_spoolman_spool (e.g. spool_id=0) must return matched=False without broadcasting unknown_tag."""
  1537. bad_spool = {
  1538. "id": 0, # _map_spoolman_spool raises ValueError for id <= 0
  1539. "filament": {"material": "PLA", "name": "PLA Basic", "color_hex": "FF0000", "weight": 1000},
  1540. "used_weight": 0.0,
  1541. "archived": False,
  1542. "registered": "2024-01-01T00:00:00Z",
  1543. "extra": {"tag": '"DEADBEEF12345678"'},
  1544. }
  1545. mock_client = _mock_spoolman_client()
  1546. mock_client.find_spool_by_tag = AsyncMock(return_value=bad_spool)
  1547. with (
  1548. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  1549. patch(
  1550. "backend.app.api.routes.spoolbuddy.get_spool_by_tag",
  1551. new_callable=AsyncMock,
  1552. return_value=None,
  1553. ),
  1554. patch(
  1555. "backend.app.services.spoolman.get_spoolman_client",
  1556. AsyncMock(return_value=mock_client),
  1557. ),
  1558. patch(
  1559. "backend.app.services.spoolman.init_spoolman_client",
  1560. AsyncMock(return_value=mock_client),
  1561. ),
  1562. ):
  1563. mock_ws.broadcast = AsyncMock()
  1564. resp = await async_client.post(
  1565. f"{API}/nfc/tag-scanned",
  1566. json={"device_id": "sb-1", "tag_uid": "DEADBEEF12345678"},
  1567. )
  1568. assert resp.status_code == 200
  1569. data = resp.json()
  1570. assert data["matched"] is False
  1571. assert data["spool_id"] is None
  1572. # No broadcast: UI must not get a spurious unknown_tag event on Spoolman data errors
  1573. mock_ws.broadcast.assert_not_called()
  1574. @pytest.mark.asyncio
  1575. @pytest.mark.integration
  1576. async def test_local_match_skips_spoolman(self, async_client: AsyncClient, spool_factory):
  1577. """When local DB matches, Spoolman is never queried."""
  1578. spool = await spool_factory(tag_uid="AABB1122", material="PLA")
  1579. mock_spool = MagicMock()
  1580. mock_spool.id = spool.id
  1581. mock_spool.material = spool.material
  1582. mock_spool.subtype = spool.subtype
  1583. mock_spool.color_name = spool.color_name
  1584. mock_spool.rgba = spool.rgba
  1585. mock_spool.brand = spool.brand
  1586. mock_spool.label_weight = spool.label_weight
  1587. mock_spool.core_weight = spool.core_weight
  1588. mock_spool.weight_used = spool.weight_used
  1589. with (
  1590. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  1591. patch(
  1592. "backend.app.api.routes.spoolbuddy.get_spool_by_tag",
  1593. new_callable=AsyncMock,
  1594. return_value=mock_spool,
  1595. ),
  1596. ):
  1597. mock_ws.broadcast = AsyncMock()
  1598. resp = await async_client.post(
  1599. f"{API}/nfc/tag-scanned",
  1600. json={"device_id": "sb-1", "tag_uid": "AABB1122"},
  1601. )
  1602. assert resp.status_code == 200
  1603. data = resp.json()
  1604. assert data["matched"] is True
  1605. assert data["spool_id"] == spool.id
  1606. # ============================================================================
  1607. # NFC write-tag / write-result — Spoolman-aware
  1608. # ============================================================================
  1609. def _full_spoolman_spool(spool_id: int) -> dict:
  1610. """Complete Spoolman spool dict sufficient for NDEF encoding."""
  1611. return {
  1612. "id": spool_id,
  1613. "filament": {
  1614. "material": "PLA",
  1615. "name": "PLA Basic",
  1616. "color_hex": "FF0000",
  1617. "weight": 1000.0,
  1618. "spool_weight": 196.0,
  1619. "vendor": {"name": "Bambu Lab"},
  1620. },
  1621. "used_weight": 0.0,
  1622. "archived": False,
  1623. "registered": "2024-01-01T00:00:00Z",
  1624. }
  1625. class TestNfcWriteTagSpoolman:
  1626. """nfc/write-tag falls back to Spoolman when local DB has no matching spool."""
  1627. @pytest.mark.asyncio
  1628. @pytest.mark.integration
  1629. async def test_spoolman_spool_queued_when_local_miss(
  1630. self, async_client: AsyncClient, device_factory, spoolman_settings
  1631. ):
  1632. """write-tag encodes NDEF from Spoolman data when spool not in local DB."""
  1633. await device_factory(device_id="sb-write-sm")
  1634. sm_spool = _full_spoolman_spool(77)
  1635. mock_client = _mock_spoolman_client()
  1636. mock_client.get_spool = AsyncMock(return_value=sm_spool)
  1637. with (
  1638. patch(
  1639. "backend.app.services.spoolman.get_spoolman_client",
  1640. AsyncMock(return_value=mock_client),
  1641. ),
  1642. patch(
  1643. "backend.app.services.spoolman.init_spoolman_client",
  1644. AsyncMock(return_value=mock_client),
  1645. ),
  1646. ):
  1647. resp = await async_client.post(
  1648. f"{API}/nfc/write-tag",
  1649. json={"device_id": "sb-write-sm", "spool_id": 77},
  1650. )
  1651. assert resp.status_code == 200
  1652. assert resp.json()["status"] == "queued"
  1653. mock_client.get_spool.assert_called_once_with(77)
  1654. @pytest.mark.asyncio
  1655. @pytest.mark.integration
  1656. async def test_data_origin_spoolman_stored_in_payload(
  1657. self, async_client: AsyncClient, device_factory, db_session, spoolman_settings
  1658. ):
  1659. """Pending write payload records data_origin=spoolman for Spoolman spools."""
  1660. import json as _json
  1661. device = await device_factory(device_id="sb-origin")
  1662. sm_spool = _full_spoolman_spool(88)
  1663. mock_client = _mock_spoolman_client()
  1664. mock_client.get_spool = AsyncMock(return_value=sm_spool)
  1665. with (
  1666. patch(
  1667. "backend.app.services.spoolman.get_spoolman_client",
  1668. AsyncMock(return_value=mock_client),
  1669. ),
  1670. patch(
  1671. "backend.app.services.spoolman.init_spoolman_client",
  1672. AsyncMock(return_value=mock_client),
  1673. ),
  1674. ):
  1675. await async_client.post(
  1676. f"{API}/nfc/write-tag",
  1677. json={"device_id": "sb-origin", "spool_id": 88},
  1678. )
  1679. await db_session.refresh(device)
  1680. payload = _json.loads(device.pending_write_payload)
  1681. assert payload["data_origin"] == "spoolman"
  1682. assert payload["spool_id"] == 88
  1683. assert "ndef_data_hex" in payload
  1684. @pytest.mark.asyncio
  1685. @pytest.mark.integration
  1686. async def test_404_when_neither_local_nor_spoolman(
  1687. self, async_client: AsyncClient, device_factory, spoolman_settings
  1688. ):
  1689. """404 returned when spool is missing from both local DB and Spoolman."""
  1690. await device_factory(device_id="sb-miss")
  1691. mock_client = _mock_spoolman_client()
  1692. mock_client.get_spool = AsyncMock(side_effect=SpoolmanNotFoundError("Spool 9999 not found"))
  1693. with (
  1694. patch(
  1695. "backend.app.services.spoolman.get_spoolman_client",
  1696. AsyncMock(return_value=mock_client),
  1697. ),
  1698. patch(
  1699. "backend.app.services.spoolman.init_spoolman_client",
  1700. AsyncMock(return_value=mock_client),
  1701. ),
  1702. ):
  1703. resp = await async_client.post(
  1704. f"{API}/nfc/write-tag",
  1705. json={"device_id": "sb-miss", "spool_id": 9999},
  1706. )
  1707. assert resp.status_code == 404
  1708. @pytest.mark.asyncio
  1709. @pytest.mark.integration
  1710. async def test_local_spool_used_when_present(self, async_client: AsyncClient, device_factory, spool_factory):
  1711. """Local DB spool is encoded directly without contacting Spoolman."""
  1712. await device_factory(device_id="sb-local-write")
  1713. spool = await spool_factory(material="PETG")
  1714. resp = await async_client.post(
  1715. f"{API}/nfc/write-tag",
  1716. json={"device_id": "sb-local-write", "spool_id": spool.id},
  1717. )
  1718. assert resp.status_code == 200
  1719. assert resp.json()["status"] == "queued"
  1720. class TestNfcWriteResultSpoolman:
  1721. """nfc/write-result updates Spoolman extra.tag on success for Spoolman spools."""
  1722. @pytest.mark.asyncio
  1723. @pytest.mark.integration
  1724. async def test_success_updates_spoolman_extra_tag(
  1725. self, async_client: AsyncClient, device_factory, spoolman_settings
  1726. ):
  1727. """Successful write for a Spoolman spool calls merge_spool_extra with extra.tag."""
  1728. import json as _json
  1729. await device_factory(
  1730. device_id="sb-wr-sm",
  1731. pending_command="write_tag",
  1732. pending_write_payload=_json.dumps({"spool_id": 55, "ndef_data_hex": "deadbeef", "data_origin": "spoolman"}),
  1733. )
  1734. mock_client = _mock_spoolman_client()
  1735. mock_client.merge_spool_extra = AsyncMock(return_value={"id": 55})
  1736. with (
  1737. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  1738. patch(
  1739. "backend.app.services.spoolman.get_spoolman_client",
  1740. AsyncMock(return_value=mock_client),
  1741. ),
  1742. patch(
  1743. "backend.app.services.spoolman.init_spoolman_client",
  1744. AsyncMock(return_value=mock_client),
  1745. ),
  1746. ):
  1747. mock_ws.broadcast = AsyncMock()
  1748. resp = await async_client.post(
  1749. f"{API}/nfc/write-result",
  1750. json={
  1751. "device_id": "sb-wr-sm",
  1752. "spool_id": 55,
  1753. "tag_uid": "AABBCCDD11223344",
  1754. "success": True,
  1755. },
  1756. )
  1757. assert resp.status_code == 200
  1758. mock_client.merge_spool_extra.assert_called_once_with(55, {"tag": '"AABBCCDD11223344"'})
  1759. msg = mock_ws.broadcast.call_args[0][0]
  1760. assert msg["type"] == "spoolbuddy_tag_written"
  1761. assert msg["tag_uid"] == "AABBCCDD11223344"
  1762. @pytest.mark.asyncio
  1763. @pytest.mark.integration
  1764. async def test_failure_does_not_call_spoolman(self, async_client: AsyncClient, device_factory, spoolman_settings):
  1765. """Failed write never calls Spoolman update."""
  1766. import json as _json
  1767. await device_factory(
  1768. device_id="sb-wr-fail",
  1769. pending_command="write_tag",
  1770. pending_write_payload=_json.dumps({"spool_id": 66, "ndef_data_hex": "deadbeef", "data_origin": "spoolman"}),
  1771. )
  1772. mock_client = _mock_spoolman_client()
  1773. with (
  1774. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  1775. patch(
  1776. "backend.app.services.spoolman.get_spoolman_client",
  1777. AsyncMock(return_value=mock_client),
  1778. ),
  1779. ):
  1780. mock_ws.broadcast = AsyncMock()
  1781. resp = await async_client.post(
  1782. f"{API}/nfc/write-result",
  1783. json={
  1784. "device_id": "sb-wr-fail",
  1785. "spool_id": 66,
  1786. "tag_uid": "AABBCCDD11223344",
  1787. "success": False,
  1788. "message": "write timeout",
  1789. },
  1790. )
  1791. assert resp.status_code == 200
  1792. mock_client.update_spool.assert_not_called()
  1793. msg = mock_ws.broadcast.call_args[0][0]
  1794. assert msg["type"] == "spoolbuddy_tag_write_failed"
  1795. @pytest.mark.asyncio
  1796. @pytest.mark.integration
  1797. async def test_success_local_spool_writes_to_db(
  1798. self, async_client: AsyncClient, device_factory, spool_factory, db_session
  1799. ):
  1800. """Successful write for a local spool still updates local DB tag_uid."""
  1801. import json as _json
  1802. spool = await spool_factory()
  1803. await device_factory(
  1804. device_id="sb-wr-local",
  1805. pending_command="write_tag",
  1806. pending_write_payload=_json.dumps(
  1807. {"spool_id": spool.id, "ndef_data_hex": "deadbeef", "data_origin": "local"}
  1808. ),
  1809. )
  1810. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  1811. mock_ws.broadcast = AsyncMock()
  1812. resp = await async_client.post(
  1813. f"{API}/nfc/write-result",
  1814. json={
  1815. "device_id": "sb-wr-local",
  1816. "spool_id": spool.id,
  1817. "tag_uid": "DEADBEEF12345678",
  1818. "success": True,
  1819. },
  1820. )
  1821. assert resp.status_code == 200
  1822. await db_session.refresh(spool)
  1823. assert spool.tag_uid == "DEADBEEF12345678"
  1824. assert spool.tag_type == "ntag"
  1825. # ============================================================================
  1826. # Security fix tests — write-tag ValueError + write-result exception safety
  1827. # ============================================================================
  1828. class TestNfcWriteTagSpoolmanSecurityFixes:
  1829. """Regression tests for security fixes in nfc/write-tag Spoolman path."""
  1830. @pytest.mark.asyncio
  1831. @pytest.mark.integration
  1832. async def test_invalid_spoolman_spool_id_returns_502(
  1833. self, async_client: AsyncClient, device_factory, spoolman_settings
  1834. ):
  1835. """Malformed Spoolman spool (invalid id=0) raises 502, not 404 — spool exists but is bad data."""
  1836. await device_factory(device_id="sb-invalid-id")
  1837. # Spoolman returns spool with id=0 (invalid — caught by _map_spoolman_spool guard)
  1838. bad_spool = {**_full_spoolman_spool(1), "id": 0}
  1839. mock_client = _mock_spoolman_client()
  1840. mock_client.get_spool = AsyncMock(return_value=bad_spool)
  1841. with (
  1842. patch(
  1843. "backend.app.services.spoolman.get_spoolman_client",
  1844. AsyncMock(return_value=mock_client),
  1845. ),
  1846. patch(
  1847. "backend.app.services.spoolman.init_spoolman_client",
  1848. AsyncMock(return_value=mock_client),
  1849. ),
  1850. ):
  1851. resp = await async_client.post(
  1852. f"{API}/nfc/write-tag",
  1853. json={"device_id": "sb-invalid-id", "spool_id": 99},
  1854. )
  1855. # 502: spool exists in Spoolman but its data is malformed — not a "not found"
  1856. assert resp.status_code == 502
  1857. @pytest.mark.asyncio
  1858. @pytest.mark.integration
  1859. async def test_oversized_label_weight_does_not_crash(
  1860. self, async_client: AsyncClient, device_factory, spoolman_settings
  1861. ):
  1862. """label_weight > 65535 from Spoolman must not crash with struct.error."""
  1863. await device_factory(device_id="sb-overflow")
  1864. big_weight_spool = {
  1865. **_full_spoolman_spool(42),
  1866. "filament": {**_full_spoolman_spool(42)["filament"], "weight": 70000},
  1867. }
  1868. mock_client = _mock_spoolman_client()
  1869. mock_client.get_spool = AsyncMock(return_value=big_weight_spool)
  1870. with (
  1871. patch(
  1872. "backend.app.services.spoolman.get_spoolman_client",
  1873. AsyncMock(return_value=mock_client),
  1874. ),
  1875. patch(
  1876. "backend.app.services.spoolman.init_spoolman_client",
  1877. AsyncMock(return_value=mock_client),
  1878. ),
  1879. ):
  1880. resp = await async_client.post(
  1881. f"{API}/nfc/write-tag",
  1882. json={"device_id": "sb-overflow", "spool_id": 42},
  1883. )
  1884. assert resp.status_code == 200
  1885. assert resp.json()["status"] == "queued"
  1886. class TestNfcWriteResultSpoolmanSecurityFixes:
  1887. """Regression tests for transaction safety in nfc/write-result Spoolman path."""
  1888. @pytest.mark.asyncio
  1889. @pytest.mark.integration
  1890. async def test_spoolman_client_exception_still_clears_device_state(
  1891. self, async_client: AsyncClient, device_factory, db_session, spoolman_settings
  1892. ):
  1893. """If Spoolman client raises, device pending_command is still cleared in DB."""
  1894. import json as _json
  1895. device = await device_factory(
  1896. device_id="sb-exc-safe",
  1897. pending_command="write_tag",
  1898. pending_write_payload=_json.dumps({"spool_id": 77, "ndef_data_hex": "deadbeef", "data_origin": "spoolman"}),
  1899. )
  1900. mock_client = _mock_spoolman_client()
  1901. mock_client.merge_spool_extra = AsyncMock(side_effect=Exception("connection refused"))
  1902. with (
  1903. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  1904. patch(
  1905. "backend.app.services.spoolman.get_spoolman_client",
  1906. AsyncMock(return_value=mock_client),
  1907. ),
  1908. patch(
  1909. "backend.app.services.spoolman.init_spoolman_client",
  1910. AsyncMock(return_value=mock_client),
  1911. ),
  1912. ):
  1913. mock_ws.broadcast = AsyncMock()
  1914. resp = await async_client.post(
  1915. f"{API}/nfc/write-result",
  1916. json={
  1917. "device_id": "sb-exc-safe",
  1918. "spool_id": 77,
  1919. "tag_uid": "AABBCCDD11223344",
  1920. "success": True,
  1921. },
  1922. )
  1923. # 502: tag written to NFC but Spoolman link failed (not best-effort — caller must retry)
  1924. assert resp.status_code == 502
  1925. # Device state must be cleared despite the exception (no spurious re-write)
  1926. await db_session.refresh(device)
  1927. assert device.pending_command is None
  1928. assert device.pending_write_payload is None
  1929. # Failure broadcast fires so the UI can show the error
  1930. msg = mock_ws.broadcast.call_args[0][0]
  1931. assert msg["type"] == "spoolbuddy_tag_link_failed"
  1932. @pytest.mark.asyncio
  1933. @pytest.mark.integration
  1934. async def test_spoolman_not_found_error_broadcasts_link_failed(
  1935. self, async_client: AsyncClient, device_factory, db_session, spoolman_settings
  1936. ):
  1937. """SpoolmanNotFoundError from merge_spool_extra must clear device state and broadcast link_failed."""
  1938. import json as _json
  1939. device = await device_factory(
  1940. device_id="sb-notfound",
  1941. pending_command="write_tag",
  1942. pending_write_payload=_json.dumps({"spool_id": 55, "ndef_data_hex": "deadbeef", "data_origin": "spoolman"}),
  1943. )
  1944. mock_client = _mock_spoolman_client()
  1945. mock_client.merge_spool_extra = AsyncMock(side_effect=SpoolmanNotFoundError("Spool 55 not found"))
  1946. with (
  1947. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  1948. patch(
  1949. "backend.app.services.spoolman.get_spoolman_client",
  1950. AsyncMock(return_value=mock_client),
  1951. ),
  1952. patch(
  1953. "backend.app.services.spoolman.init_spoolman_client",
  1954. AsyncMock(return_value=mock_client),
  1955. ),
  1956. ):
  1957. mock_ws.broadcast = AsyncMock()
  1958. resp = await async_client.post(
  1959. f"{API}/nfc/write-result",
  1960. json={
  1961. "device_id": "sb-notfound",
  1962. "spool_id": 55,
  1963. "tag_uid": "AABBCCDD11223344",
  1964. "success": True,
  1965. },
  1966. )
  1967. assert resp.status_code == 502
  1968. await db_session.refresh(device)
  1969. assert device.pending_command is None
  1970. assert device.pending_write_payload is None
  1971. msg = mock_ws.broadcast.call_args[0][0]
  1972. assert msg["type"] == "spoolbuddy_tag_link_failed"
  1973. assert msg["spool_id"] == 55
  1974. class TestNfcWriteResultOrphanedSpool:
  1975. """nfc/write-result when the local spool was deleted between write-queue and write-result."""
  1976. @pytest.mark.asyncio
  1977. @pytest.mark.integration
  1978. async def test_local_spool_deleted_before_write_back(self, async_client: AsyncClient, device_factory, db_session):
  1979. """When local spool is deleted between write-queue and write-result, return linked=False and broadcast link_failed."""
  1980. import json as _json
  1981. device = await device_factory(
  1982. device_id="sb-orphan",
  1983. pending_command="write_tag",
  1984. pending_write_payload=_json.dumps(
  1985. {
  1986. "spool_id": 99999, # non-existent spool
  1987. "ndef_data_hex": "aabbccdd",
  1988. "data_origin": "local",
  1989. }
  1990. ),
  1991. )
  1992. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  1993. mock_ws.broadcast = AsyncMock()
  1994. resp = await async_client.post(
  1995. f"{API}/nfc/write-result",
  1996. json={"device_id": device.device_id, "spool_id": 99999, "success": True, "tag_uid": "AABBCCDD"},
  1997. )
  1998. assert resp.status_code == 200
  1999. data = resp.json()
  2000. assert data["linked"] is False
  2001. # pending command should be cleared
  2002. await db_session.refresh(device)
  2003. assert device.pending_command is None
  2004. # broadcast should be spoolbuddy_tag_link_failed
  2005. broadcast_calls = mock_ws.broadcast.call_args_list
  2006. link_failed = [c[0][0] for c in broadcast_calls if c[0][0].get("type") == "spoolbuddy_tag_link_failed"]
  2007. assert len(link_failed) >= 1
  2008. class TestNfcWriteResultInputValidation:
  2009. """Input validation and JSON safety for nfc/write-result."""
  2010. @pytest.mark.asyncio
  2011. @pytest.mark.integration
  2012. async def test_tag_uid_too_long_rejected(self, async_client: AsyncClient, device_factory):
  2013. """tag_uid longer than 32 chars must be rejected with 422."""
  2014. import json as _json
  2015. await device_factory(
  2016. device_id="sb-uid-long",
  2017. pending_command="write_tag",
  2018. pending_write_payload=_json.dumps({"spool_id": 1, "ndef_data_hex": "dead", "data_origin": "local"}),
  2019. )
  2020. resp = await async_client.post(
  2021. f"{API}/nfc/write-result",
  2022. json={
  2023. "device_id": "sb-uid-long",
  2024. "spool_id": 1,
  2025. "tag_uid": "A" * 65,
  2026. "success": True,
  2027. },
  2028. )
  2029. assert resp.status_code == 422
  2030. @pytest.mark.asyncio
  2031. @pytest.mark.integration
  2032. async def test_malformed_pending_payload_falls_back_to_local(
  2033. self, async_client: AsyncClient, device_factory, spool_factory, db_session
  2034. ):
  2035. """Corrupted pending_write_payload JSON falls back to local mode gracefully."""
  2036. spool = await spool_factory()
  2037. await device_factory(
  2038. device_id="sb-corrupt-json",
  2039. pending_command="write_tag",
  2040. pending_write_payload="{not valid json!!!",
  2041. )
  2042. with patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws:
  2043. mock_ws.broadcast = AsyncMock()
  2044. resp = await async_client.post(
  2045. f"{API}/nfc/write-result",
  2046. json={
  2047. "device_id": "sb-corrupt-json",
  2048. "spool_id": spool.id,
  2049. "tag_uid": "DEADBEEF12345678",
  2050. "success": True,
  2051. },
  2052. )
  2053. # Must return 200, not 500
  2054. assert resp.status_code == 200
  2055. # Falls back to local mode — tag written to DB
  2056. await db_session.refresh(spool)
  2057. assert spool.tag_uid == "DEADBEEF12345678"
  2058. # ============================================================================
  2059. # B1: NFC write-tag warnings appear in response body
  2060. # ============================================================================
  2061. class TestNfcWriteTagWarningsBody:
  2062. """B1: resp.json()['warnings'] is populated when Spoolman fields are absent."""
  2063. @pytest.mark.asyncio
  2064. @pytest.mark.integration
  2065. async def test_warnings_returned_for_missing_color_and_temp(
  2066. self, async_client: AsyncClient, device_factory, spoolman_settings
  2067. ):
  2068. """Both color_name=None and settings_extruder_temp=None produce 2 warnings."""
  2069. await device_factory(device_id="sb-warn-b1")
  2070. # Spoolman spool with no color_name or nozzle temp
  2071. sparse_spool = {
  2072. "id": 99,
  2073. "filament": {
  2074. "material": "PLA",
  2075. "name": "PLA Basic",
  2076. "color_hex": "808080",
  2077. # color_name absent → None after mapping
  2078. # settings_extruder_temp absent → nozzle_temp_min=None
  2079. "weight": 1000.0,
  2080. "vendor": {"name": "Bambu Lab"},
  2081. },
  2082. "used_weight": 0.0,
  2083. "archived": False,
  2084. "registered": "2024-01-01T00:00:00Z",
  2085. }
  2086. mock_client = _mock_spoolman_client()
  2087. mock_client.get_spool = AsyncMock(return_value=sparse_spool)
  2088. with (
  2089. patch(
  2090. "backend.app.services.spoolman.get_spoolman_client",
  2091. AsyncMock(return_value=mock_client),
  2092. ),
  2093. patch(
  2094. "backend.app.services.spoolman.init_spoolman_client",
  2095. AsyncMock(return_value=mock_client),
  2096. ),
  2097. ):
  2098. resp = await async_client.post(
  2099. f"{API}/nfc/write-tag",
  2100. json={"device_id": "sb-warn-b1", "spool_id": 99},
  2101. )
  2102. assert resp.status_code == 200
  2103. body = resp.json()
  2104. assert "warnings" in body, "Response should contain 'warnings' key when fields are absent"
  2105. warnings = body["warnings"]
  2106. assert len(warnings) >= 2, f"Expected at least 2 warnings for missing color_name + nozzle_temp, got: {warnings}"
  2107. # Confirm the specific fields are mentioned
  2108. warn_text = " ".join(warnings)
  2109. assert "color_name" in warn_text
  2110. assert "nozzle_temp" in warn_text
  2111. @pytest.mark.asyncio
  2112. @pytest.mark.integration
  2113. async def test_no_warnings_key_when_all_fields_present(
  2114. self, async_client: AsyncClient, device_factory, spoolman_settings
  2115. ):
  2116. """No 'warnings' key in response when all fields are populated."""
  2117. await device_factory(device_id="sb-nowarn")
  2118. full_spool = _full_spoolman_spool(100)
  2119. # Add color_name and extruder temp
  2120. full_spool["filament"]["color_name"] = "Red"
  2121. full_spool["filament"]["settings_extruder_temp"] = 220
  2122. mock_client = _mock_spoolman_client()
  2123. mock_client.get_spool = AsyncMock(return_value=full_spool)
  2124. with (
  2125. patch(
  2126. "backend.app.services.spoolman.get_spoolman_client",
  2127. AsyncMock(return_value=mock_client),
  2128. ),
  2129. patch(
  2130. "backend.app.services.spoolman.init_spoolman_client",
  2131. AsyncMock(return_value=mock_client),
  2132. ),
  2133. ):
  2134. resp = await async_client.post(
  2135. f"{API}/nfc/write-tag",
  2136. json={"device_id": "sb-nowarn", "spool_id": 100},
  2137. )
  2138. assert resp.status_code == 200
  2139. body = resp.json()
  2140. assert "warnings" not in body or body["warnings"] == []
  2141. # ============================================================================
  2142. # B5: Exception text scrubbed from WebSocket broadcast message
  2143. # ============================================================================
  2144. class TestNfcWriteResultExceptionScrubbing:
  2145. """B5: Internal exception details must not appear in WebSocket 'message' field."""
  2146. @pytest.mark.asyncio
  2147. @pytest.mark.integration
  2148. async def test_exception_text_not_leaked_in_ws_message(
  2149. self, async_client: AsyncClient, device_factory, db_session, spoolman_settings
  2150. ):
  2151. """When Spoolman merge raises, WS message is generic; 'connection refused' absent."""
  2152. import json as _json
  2153. await device_factory(
  2154. device_id="sb-scrub-b5",
  2155. pending_command="write_tag",
  2156. pending_write_payload=_json.dumps({"spool_id": 77, "ndef_data_hex": "deadbeef", "data_origin": "spoolman"}),
  2157. )
  2158. mock_client = _mock_spoolman_client()
  2159. mock_client.merge_spool_extra = AsyncMock(side_effect=Exception("connection refused to 192.168.1.1:7912"))
  2160. with (
  2161. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  2162. patch(
  2163. "backend.app.services.spoolman.get_spoolman_client",
  2164. AsyncMock(return_value=mock_client),
  2165. ),
  2166. patch(
  2167. "backend.app.services.spoolman.init_spoolman_client",
  2168. AsyncMock(return_value=mock_client),
  2169. ),
  2170. ):
  2171. mock_ws.broadcast = AsyncMock()
  2172. resp = await async_client.post(
  2173. f"{API}/nfc/write-result",
  2174. json={
  2175. "device_id": "sb-scrub-b5",
  2176. "spool_id": 77,
  2177. "tag_uid": "AABBCCDD11223344",
  2178. "success": True,
  2179. },
  2180. )
  2181. assert resp.status_code == 502
  2182. msg = mock_ws.broadcast.call_args[0][0]
  2183. assert msg["type"] == "spoolbuddy_tag_link_failed"
  2184. # Generic message — no internal exception details leaked
  2185. assert msg["message"] == "Spoolman link failed", f"Expected generic message but got: {msg['message']!r}"
  2186. assert "connection refused" not in str(msg), f"Exception text must not appear in WS message: {msg}"
  2187. assert "192.168.1" not in str(msg), f"Internal IP must not appear in WS message: {msg}"
  2188. # ============================================================================
  2189. # _get_spoolman_client_or_none: graceful degradation on ValueError during reinit
  2190. # ============================================================================
  2191. class TestSpoolmanClientOrNoneGraceful:
  2192. """_get_spoolman_client_or_none returns None when init_spoolman_client raises ValueError."""
  2193. @pytest.mark.asyncio
  2194. @pytest.mark.integration
  2195. async def test_returns_none_when_init_raises_value_error(self, async_client: AsyncClient, db_session):
  2196. """_get_spoolman_client_or_none returns None when init_spoolman_client raises ValueError,
  2197. so the device endpoint degrades gracefully instead of propagating a 500 error."""
  2198. from backend.app.models.settings import Settings
  2199. db_session.add(Settings(key="spoolman_enabled", value="true"))
  2200. db_session.add(Settings(key="spoolman_url", value="http://spoolman.local:7912"))
  2201. await db_session.commit()
  2202. with (
  2203. patch("backend.app.api.routes._spoolman_helpers.assert_safe_spoolman_url"),
  2204. patch(
  2205. "backend.app.services.spoolman.get_spoolman_client",
  2206. AsyncMock(return_value=None),
  2207. ),
  2208. patch(
  2209. "backend.app.services.spoolman.init_spoolman_client",
  2210. AsyncMock(side_effect=ValueError("invalid URL")),
  2211. ),
  2212. patch("backend.app.api.routes.spoolbuddy.ws_manager") as mock_ws,
  2213. ):
  2214. mock_ws.broadcast = AsyncMock()
  2215. # nfc/tag-scanned calls _get_spoolman_client_or_none; with None returned it
  2216. # must broadcast unknown_tag (not raise 500 due to ValueError propagating).
  2217. resp = await async_client.post(
  2218. f"{API}/nfc/tag-scanned",
  2219. json={"device_id": "sb-vale", "tag_uid": "AABBCCDD"},
  2220. )
  2221. # Must not be 500 — ValueError is caught and client returns None, degrading gracefully
  2222. assert resp.status_code == 200
  2223. data = resp.json()
  2224. assert data["matched"] is False