test_bambu_mqtt.py 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  1. """
  2. Tests for the BambuMQTTClient service.
  3. These tests focus on timelapse tracking during prints.
  4. """
  5. import json
  6. import pytest
  7. class TestTimelapseTracking:
  8. """Tests for timelapse state tracking during prints."""
  9. @pytest.fixture
  10. def mqtt_client(self):
  11. """Create a BambuMQTTClient instance for testing."""
  12. from backend.app.services.bambu_mqtt import BambuMQTTClient
  13. client = BambuMQTTClient(
  14. ip_address="192.168.1.100",
  15. serial_number="TEST123",
  16. access_code="12345678",
  17. )
  18. return client
  19. def test_timelapse_flag_initializes_to_false(self, mqtt_client):
  20. """Verify _timelapse_during_print starts as False."""
  21. assert mqtt_client._timelapse_during_print is False
  22. def test_timelapse_flag_set_when_timelapse_active_during_running(self, mqtt_client):
  23. """Verify timelapse flag is set when timelapse is active while printing."""
  24. # Simulate print running
  25. mqtt_client._was_running = True
  26. mqtt_client.state.timelapse = False
  27. # Simulate xcam data showing timelapse is enabled
  28. xcam_data = {"timelapse": "enable"}
  29. mqtt_client._parse_xcam_data(xcam_data)
  30. assert mqtt_client.state.timelapse is True
  31. assert mqtt_client._timelapse_during_print is True
  32. def test_timelapse_flag_not_set_when_not_running(self, mqtt_client):
  33. """Verify timelapse flag is NOT set when printer not running."""
  34. # Printer is idle (not running)
  35. mqtt_client._was_running = False
  36. mqtt_client.state.timelapse = False
  37. # Timelapse is enabled but we're not printing
  38. xcam_data = {"timelapse": "enable"}
  39. mqtt_client._parse_xcam_data(xcam_data)
  40. assert mqtt_client.state.timelapse is True
  41. # Flag should NOT be set since we're not printing
  42. assert mqtt_client._timelapse_during_print is False
  43. def test_timelapse_flag_persists_after_timelapse_stops(self, mqtt_client):
  44. """Verify timelapse flag stays True even after recording stops."""
  45. # Simulate print running with timelapse
  46. mqtt_client._was_running = True
  47. # Enable timelapse during print
  48. xcam_data = {"timelapse": "enable"}
  49. mqtt_client._parse_xcam_data(xcam_data)
  50. assert mqtt_client._timelapse_during_print is True
  51. # Disable timelapse (recording stops at end of print)
  52. xcam_data = {"timelapse": "disable"}
  53. mqtt_client._parse_xcam_data(xcam_data)
  54. # Flag should still be True (persists until reset)
  55. assert mqtt_client.state.timelapse is False
  56. assert mqtt_client._timelapse_during_print is True
  57. def test_timelapse_flag_from_print_data(self, mqtt_client):
  58. """Verify timelapse flag is set from print data (not just xcam)."""
  59. # Simulate print running
  60. mqtt_client._was_running = True
  61. mqtt_client.state.timelapse = False
  62. mqtt_client._timelapse_during_print = False
  63. # Manually test the timelapse parsing logic from _parse_print_data
  64. # This tests the "timelapse" field in the main print data
  65. data = {"timelapse": True}
  66. mqtt_client.state.timelapse = data["timelapse"] is True
  67. if mqtt_client.state.timelapse and mqtt_client._was_running:
  68. mqtt_client._timelapse_during_print = True
  69. assert mqtt_client._timelapse_during_print is True
  70. class TestPrintCompletionWithTimelapse:
  71. """Tests for print completion including timelapse flag."""
  72. @pytest.fixture
  73. def mqtt_client(self):
  74. """Create a BambuMQTTClient instance for testing."""
  75. from backend.app.services.bambu_mqtt import BambuMQTTClient
  76. client = BambuMQTTClient(
  77. ip_address="192.168.1.100",
  78. serial_number="TEST123",
  79. access_code="12345678",
  80. )
  81. return client
  82. def test_print_complete_includes_timelapse_flag(self, mqtt_client):
  83. """Verify print complete callback includes timelapse_was_active."""
  84. # Set up completion callback
  85. callback_data = {}
  86. def on_complete(data):
  87. callback_data.update(data)
  88. mqtt_client.on_print_complete = on_complete
  89. # Simulate a print that had timelapse active
  90. mqtt_client._was_running = True
  91. mqtt_client._completion_triggered = False
  92. mqtt_client._timelapse_during_print = True
  93. mqtt_client._previous_gcode_state = "RUNNING"
  94. mqtt_client._previous_gcode_file = "test.gcode"
  95. mqtt_client.state.subtask_name = "Test Print"
  96. # Simulate print finish
  97. mqtt_client.state.state = "FINISH"
  98. # Manually trigger the completion logic (simplified)
  99. # In real code this happens in _parse_print_data
  100. should_trigger = (
  101. mqtt_client.state.state in ("FINISH", "FAILED")
  102. and not mqtt_client._completion_triggered
  103. and mqtt_client.on_print_complete
  104. and mqtt_client._previous_gcode_state == "RUNNING"
  105. )
  106. if should_trigger:
  107. status = "completed" if mqtt_client.state.state == "FINISH" else "failed"
  108. timelapse_was_active = mqtt_client._timelapse_during_print
  109. mqtt_client._completion_triggered = True
  110. mqtt_client._was_running = False
  111. mqtt_client._timelapse_during_print = False
  112. mqtt_client.on_print_complete(
  113. {
  114. "status": status,
  115. "filename": mqtt_client._previous_gcode_file,
  116. "subtask_name": mqtt_client.state.subtask_name,
  117. "timelapse_was_active": timelapse_was_active,
  118. }
  119. )
  120. assert "timelapse_was_active" in callback_data
  121. assert callback_data["timelapse_was_active"] is True
  122. def test_print_complete_timelapse_flag_false_when_no_timelapse(self, mqtt_client):
  123. """Verify timelapse_was_active is False when no timelapse during print."""
  124. callback_data = {}
  125. def on_complete(data):
  126. callback_data.update(data)
  127. mqtt_client.on_print_complete = on_complete
  128. # Print without timelapse
  129. mqtt_client._was_running = True
  130. mqtt_client._completion_triggered = False
  131. mqtt_client._timelapse_during_print = False # No timelapse
  132. mqtt_client._previous_gcode_state = "RUNNING"
  133. mqtt_client._previous_gcode_file = "test.gcode"
  134. mqtt_client.state.subtask_name = "Test Print"
  135. mqtt_client.state.state = "FINISH"
  136. # Trigger completion
  137. timelapse_was_active = mqtt_client._timelapse_during_print
  138. mqtt_client.on_print_complete(
  139. {
  140. "status": "completed",
  141. "filename": mqtt_client._previous_gcode_file,
  142. "subtask_name": mqtt_client.state.subtask_name,
  143. "timelapse_was_active": timelapse_was_active,
  144. }
  145. )
  146. assert callback_data["timelapse_was_active"] is False
  147. def test_timelapse_flag_reset_after_completion(self, mqtt_client):
  148. """Verify _timelapse_during_print is reset after print completion."""
  149. mqtt_client._timelapse_during_print = True
  150. mqtt_client._was_running = True
  151. mqtt_client._completion_triggered = False
  152. # Simulate completion reset
  153. mqtt_client._completion_triggered = True
  154. mqtt_client._was_running = False
  155. mqtt_client._timelapse_during_print = False
  156. assert mqtt_client._timelapse_during_print is False
  157. class TestRealisticMessageFlow:
  158. """Tests that simulate realistic MQTT message sequences.
  159. These tests process messages through _process_message to test the full flow,
  160. including the order of xcam parsing vs state detection.
  161. """
  162. @pytest.fixture
  163. def mqtt_client(self):
  164. """Create a BambuMQTTClient instance for testing."""
  165. from backend.app.services.bambu_mqtt import BambuMQTTClient
  166. client = BambuMQTTClient(
  167. ip_address="192.168.1.100",
  168. serial_number="TEST123",
  169. access_code="12345678",
  170. )
  171. return client
  172. def test_timelapse_detected_at_print_start_in_same_message(self, mqtt_client):
  173. """Test that timelapse is detected when xcam and state come in same message.
  174. This is the critical race condition test - xcam data is parsed BEFORE
  175. state detection, so the timelapse flag must be set AFTER _was_running is True.
  176. """
  177. # Callbacks to track events
  178. start_callback_data = {}
  179. def on_start(data):
  180. start_callback_data.update(data)
  181. mqtt_client.on_print_start = on_start
  182. # Initial state - idle
  183. mqtt_client._was_running = False
  184. mqtt_client._timelapse_during_print = False
  185. mqtt_client._previous_gcode_state = None
  186. # Simulate first message when print starts - contains both xcam and gcode_state
  187. # This is the realistic scenario from the printer
  188. # NOTE: Real MQTT messages wrap print data inside a "print" key
  189. payload = {
  190. "print": {
  191. "gcode_state": "RUNNING",
  192. "gcode_file": "/data/Metadata/test_print.gcode",
  193. "subtask_name": "Test_Print",
  194. "xcam": {
  195. "timelapse": "enable", # Timelapse is enabled in this print
  196. "printing_monitor": True,
  197. },
  198. "mc_percent": 0,
  199. "mc_remaining_time": 3600,
  200. }
  201. }
  202. # Process the message (this is what happens in real MQTT flow)
  203. mqtt_client._process_message(payload)
  204. # Verify timelapse was detected even though xcam is parsed before state
  205. assert mqtt_client._was_running is True, "_was_running should be True after RUNNING state"
  206. assert mqtt_client.state.timelapse is True, "state.timelapse should be True"
  207. assert mqtt_client._timelapse_during_print is True, (
  208. "timelapse_during_print should be True when timelapse is in the same message as RUNNING state"
  209. )
  210. def test_timelapse_not_detected_when_disabled(self, mqtt_client):
  211. """Test that timelapse is NOT detected when disabled in xcam data."""
  212. mqtt_client.on_print_start = lambda data: None
  213. # Initial state - idle
  214. mqtt_client._was_running = False
  215. mqtt_client._timelapse_during_print = False
  216. mqtt_client._previous_gcode_state = None
  217. # Print starts without timelapse
  218. payload = {
  219. "print": {
  220. "gcode_state": "RUNNING",
  221. "gcode_file": "/data/Metadata/test_print.gcode",
  222. "subtask_name": "Test_Print",
  223. "xcam": {
  224. "timelapse": "disable", # Timelapse is disabled
  225. "printing_monitor": True,
  226. },
  227. }
  228. }
  229. mqtt_client._process_message(payload)
  230. assert mqtt_client._was_running is True
  231. assert mqtt_client.state.timelapse is False
  232. assert mqtt_client._timelapse_during_print is False
  233. def test_timelapse_detected_when_enabled_after_print_start(self, mqtt_client):
  234. """Test timelapse detected when enabled in a message after print starts."""
  235. mqtt_client.on_print_start = lambda data: None
  236. # First message - print starts without timelapse info
  237. payload_start = {
  238. "print": {
  239. "gcode_state": "RUNNING",
  240. "gcode_file": "/data/Metadata/test_print.gcode",
  241. "subtask_name": "Test_Print",
  242. }
  243. }
  244. mqtt_client._process_message(payload_start)
  245. assert mqtt_client._was_running is True
  246. assert mqtt_client._timelapse_during_print is False # Not detected yet
  247. # Second message - xcam data arrives with timelapse enabled
  248. payload_xcam = {
  249. "print": {
  250. "gcode_state": "RUNNING",
  251. "gcode_file": "/data/Metadata/test_print.gcode",
  252. "subtask_name": "Test_Print",
  253. "xcam": {
  254. "timelapse": "enable",
  255. },
  256. }
  257. }
  258. mqtt_client._process_message(payload_xcam)
  259. # Now timelapse should be detected because _was_running is already True
  260. assert mqtt_client._timelapse_during_print is True
  261. def test_print_complete_includes_timelapse_flag_full_flow(self, mqtt_client):
  262. """Test full print lifecycle with timelapse - from start to completion."""
  263. start_data = {}
  264. complete_data = {}
  265. def on_start(data):
  266. start_data.update(data)
  267. def on_complete(data):
  268. complete_data.update(data)
  269. mqtt_client.on_print_start = on_start
  270. mqtt_client.on_print_complete = on_complete
  271. # 1. Print starts with timelapse
  272. mqtt_client._process_message(
  273. {
  274. "print": {
  275. "gcode_state": "RUNNING",
  276. "gcode_file": "/data/Metadata/test.gcode",
  277. "subtask_name": "Test",
  278. "xcam": {"timelapse": "enable"},
  279. }
  280. }
  281. )
  282. assert mqtt_client._timelapse_during_print is True
  283. assert "subtask_name" in start_data
  284. # 2. Print continues (multiple messages)
  285. for _ in range(3):
  286. mqtt_client._process_message(
  287. {
  288. "print": {
  289. "gcode_state": "RUNNING",
  290. "gcode_file": "/data/Metadata/test.gcode",
  291. "subtask_name": "Test",
  292. "mc_percent": 50,
  293. }
  294. }
  295. )
  296. # Timelapse flag should still be True
  297. assert mqtt_client._timelapse_during_print is True
  298. # 3. Print completes
  299. mqtt_client._process_message(
  300. {
  301. "print": {
  302. "gcode_state": "FINISH",
  303. "gcode_file": "/data/Metadata/test.gcode",
  304. "subtask_name": "Test",
  305. }
  306. }
  307. )
  308. # Verify completion callback received timelapse flag
  309. assert "timelapse_was_active" in complete_data
  310. assert complete_data["timelapse_was_active"] is True
  311. assert complete_data["status"] == "completed"
  312. # Flags should be reset after completion
  313. assert mqtt_client._timelapse_during_print is False
  314. assert mqtt_client._was_running is False
  315. def test_print_failed_includes_timelapse_flag(self, mqtt_client):
  316. """Test that failed print also includes timelapse flag."""
  317. complete_data = {}
  318. def on_complete(data):
  319. complete_data.update(data)
  320. mqtt_client.on_print_start = lambda data: None
  321. mqtt_client.on_print_complete = on_complete
  322. # Start with timelapse
  323. mqtt_client._process_message(
  324. {
  325. "print": {
  326. "gcode_state": "RUNNING",
  327. "gcode_file": "/data/Metadata/test.gcode",
  328. "subtask_name": "Test",
  329. "xcam": {"timelapse": "enable"},
  330. }
  331. }
  332. )
  333. # Print fails
  334. mqtt_client._process_message(
  335. {
  336. "print": {
  337. "gcode_state": "FAILED",
  338. "gcode_file": "/data/Metadata/test.gcode",
  339. "subtask_name": "Test",
  340. }
  341. }
  342. )
  343. assert complete_data["timelapse_was_active"] is True
  344. assert complete_data["status"] == "failed"
  345. class TestAMSDataMerging:
  346. """Tests for AMS data merging, particularly handling empty slots."""
  347. @pytest.fixture
  348. def mqtt_client(self):
  349. """Create a BambuMQTTClient instance for testing."""
  350. from backend.app.services.bambu_mqtt import BambuMQTTClient
  351. client = BambuMQTTClient(
  352. ip_address="192.168.1.100",
  353. serial_number="TEST123",
  354. access_code="12345678",
  355. )
  356. return client
  357. def test_empty_slot_clears_tray_type(self, mqtt_client):
  358. """Test that empty slot update clears tray_type (Issue #147).
  359. When a spool is removed from an old AMS, the printer sends empty values.
  360. These must overwrite the previous values to show the slot as empty.
  361. """
  362. # Initial state: AMS unit with a loaded spool
  363. initial_ams = {
  364. "ams": [
  365. {
  366. "id": 0,
  367. "tray": [
  368. {
  369. "id": 0,
  370. "tray_type": "PLA",
  371. "tray_sub_brands": "Bambu PLA Basic",
  372. "tray_color": "FF0000",
  373. "tag_uid": "1234567890ABCDEF",
  374. "remain": 80,
  375. }
  376. ],
  377. }
  378. ]
  379. }
  380. mqtt_client._handle_ams_data(initial_ams)
  381. # Verify initial state
  382. ams_data = mqtt_client.state.raw_data.get("ams", [])
  383. assert len(ams_data) == 1
  384. tray = ams_data[0]["tray"][0]
  385. assert tray["tray_type"] == "PLA"
  386. assert tray["tray_color"] == "FF0000"
  387. # Now simulate spool removal - printer sends empty values
  388. empty_update = {
  389. "ams": [
  390. {
  391. "id": 0,
  392. "tray": [
  393. {
  394. "id": 0,
  395. "tray_type": "", # Empty = slot is empty
  396. "tray_sub_brands": "",
  397. "tray_color": "",
  398. "tag_uid": "0000000000000000", # Zero UID
  399. "remain": 0,
  400. }
  401. ],
  402. }
  403. ]
  404. }
  405. mqtt_client._handle_ams_data(empty_update)
  406. # Verify empty values were applied (not ignored by merge logic)
  407. ams_data = mqtt_client.state.raw_data.get("ams", [])
  408. tray = ams_data[0]["tray"][0]
  409. assert tray["tray_type"] == "", "tray_type should be cleared when slot is empty"
  410. assert tray["tray_color"] == "", "tray_color should be cleared when slot is empty"
  411. assert tray["tray_sub_brands"] == "", "tray_sub_brands should be cleared"
  412. assert tray["tag_uid"] == "0000000000000000", "tag_uid should be cleared"
  413. def test_partial_update_preserves_other_fields(self, mqtt_client):
  414. """Test that partial updates still preserve non-slot-status fields."""
  415. # Initial state with full data
  416. initial_ams = {
  417. "ams": [
  418. {
  419. "id": 0,
  420. "humidity": "3",
  421. "temp": "25.5",
  422. "tray": [
  423. {
  424. "id": 0,
  425. "tray_type": "PLA",
  426. "tray_color": "00FF00",
  427. "remain": 90,
  428. "k": 0.02,
  429. }
  430. ],
  431. }
  432. ]
  433. }
  434. mqtt_client._handle_ams_data(initial_ams)
  435. # Partial update - only remain changes
  436. partial_update = {
  437. "ams": [
  438. {
  439. "id": 0,
  440. "tray": [
  441. {
  442. "id": 0,
  443. "remain": 85, # Only this changed
  444. }
  445. ],
  446. }
  447. ]
  448. }
  449. mqtt_client._handle_ams_data(partial_update)
  450. # Verify remain was updated but other fields preserved
  451. ams_data = mqtt_client.state.raw_data.get("ams", [])
  452. tray = ams_data[0]["tray"][0]
  453. assert tray["remain"] == 85, "remain should be updated"
  454. assert tray["tray_type"] == "PLA", "tray_type should be preserved"
  455. assert tray["tray_color"] == "00FF00", "tray_color should be preserved"
  456. assert tray["k"] == 0.02, "k should be preserved"
  457. def test_tray_exist_bits_clears_empty_slots(self, mqtt_client):
  458. """Test that tray_exist_bits clears slots marked as empty (Issue #147).
  459. New AMS models (AMS 2 Pro) don't send empty tray data when a spool is removed.
  460. Instead, they update tray_exist_bits to indicate which slots have spools.
  461. """
  462. # Initial state: AMS 0 and AMS 1 with loaded spools
  463. initial_ams = {
  464. "ams": [
  465. {
  466. "id": 0,
  467. "tray": [
  468. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000", "remain": 80},
  469. {"id": 1, "tray_type": "PETG", "tray_color": "00FF00", "remain": 60},
  470. {"id": 2, "tray_type": "ABS", "tray_color": "0000FF", "remain": 40},
  471. {"id": 3, "tray_type": "TPU", "tray_color": "FFFF00", "remain": 20},
  472. ],
  473. },
  474. {
  475. "id": 1,
  476. "tray": [
  477. {"id": 0, "tray_type": "PLA", "tray_color": "FFFFFF", "remain": 90},
  478. {"id": 1, "tray_type": "PLA", "tray_color": "000000", "remain": 70},
  479. {"id": 2, "tray_type": "PLA", "tray_color": "FF00FF", "remain": 50},
  480. {"id": 3, "tray_type": "PLA", "tray_color": "00FFFF", "remain": 30},
  481. ],
  482. },
  483. ],
  484. "tray_exist_bits": "ff", # All 8 slots have spools (0xFF = 11111111)
  485. }
  486. mqtt_client._handle_ams_data(initial_ams)
  487. # Verify initial state
  488. ams_data = mqtt_client.state.raw_data.get("ams", [])
  489. assert ams_data[1]["tray"][3]["tray_type"] == "PLA" # AMS 1 slot 3 (B4) has spool
  490. # Now simulate spool removal from AMS 1 slot 3 (B4)
  491. # tray_exist_bits: 0x7f = 01111111 (bit 7 = 0 means AMS 1 slot 3 is empty)
  492. update_ams = {
  493. "ams": [
  494. {"id": 0, "tray": [{"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}]},
  495. {"id": 1, "tray": [{"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}]},
  496. ],
  497. "tray_exist_bits": "7f", # Bit 7 = 0 -> AMS 1 slot 3 is empty
  498. }
  499. mqtt_client._handle_ams_data(update_ams)
  500. # Verify AMS 1 slot 3 was cleared
  501. ams_data = mqtt_client.state.raw_data.get("ams", [])
  502. b4_tray = ams_data[1]["tray"][3]
  503. assert b4_tray["tray_type"] == "", "tray_type should be cleared for empty slot"
  504. assert b4_tray["remain"] == 0, "remain should be 0 for empty slot"
  505. # Verify other slots are preserved
  506. assert ams_data[0]["tray"][0]["tray_type"] == "PLA", "A1 should still have PLA"
  507. assert ams_data[1]["tray"][0]["tray_type"] == "PLA", "B1 should still have PLA"
  508. def test_shutdown_message_preserves_ams_data(self, mqtt_client):
  509. """Printer shutdown (power_on_flag=False) must not wipe AMS slot data (#765).
  510. When a printer shuts down it sends a final MQTT message with
  511. tray_exist_bits='0' and power_on_flag=False. This all-zero value
  512. previously caused every slot to be cleared, which then triggered
  513. auto-unlink of all spool assignments on reconnect.
  514. """
  515. # Initial state: two AMS units with loaded spools
  516. initial_ams = {
  517. "ams": [
  518. {
  519. "id": 0,
  520. "tray": [
  521. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000FF", "remain": 80},
  522. {"id": 1, "tray_type": "PETG", "tray_color": "00FF00FF", "remain": 60},
  523. ],
  524. },
  525. {
  526. "id": 1,
  527. "tray": [
  528. {"id": 0, "tray_type": "PETG", "tray_color": "DBDDD9FF", "remain": 90},
  529. {"id": 1, "tray_type": "PETG", "tray_color": "67DB25FF", "remain": 70},
  530. ],
  531. },
  532. ],
  533. "tray_exist_bits": "33", # Slots 0,1 of each AMS (0b00110011)
  534. "power_on_flag": True,
  535. }
  536. mqtt_client._handle_ams_data(initial_ams)
  537. # Verify initial state
  538. ams_data = mqtt_client.state.raw_data["ams"]
  539. assert ams_data[0]["tray"][0]["tray_type"] == "PLA"
  540. assert ams_data[1]["tray"][0]["tray_type"] == "PETG"
  541. # Simulate printer shutdown — all-zero bits with power_on_flag=False
  542. shutdown_ams = {
  543. "ams_exist_bits": "0",
  544. "tray_exist_bits": "0",
  545. "power_on_flag": False,
  546. "insert_flag": False,
  547. "tray_now": "0",
  548. "version": 0,
  549. }
  550. mqtt_client._handle_ams_data(shutdown_ams)
  551. # AMS slot data MUST be preserved — shutdown should not clear it
  552. ams_data = mqtt_client.state.raw_data["ams"]
  553. assert ams_data[0]["tray"][0]["tray_type"] == "PLA", "Shutdown must not clear AMS 0 slot 0"
  554. assert ams_data[0]["tray"][0]["tray_color"] == "FF0000FF", "Shutdown must not clear AMS 0 slot 0 color"
  555. assert ams_data[0]["tray"][1]["tray_type"] == "PETG", "Shutdown must not clear AMS 0 slot 1"
  556. assert ams_data[1]["tray"][0]["tray_type"] == "PETG", "Shutdown must not clear AMS 1 slot 0"
  557. assert ams_data[1]["tray"][1]["tray_type"] == "PETG", "Shutdown must not clear AMS 1 slot 1"
  558. def test_genuine_removal_still_clears_with_power_on(self, mqtt_client):
  559. """Genuine spool removal (power_on_flag=True) must still clear slot data.
  560. Ensures the #765 fix doesn't break normal spool removal detection.
  561. """
  562. # Initial state: AMS with loaded spool
  563. initial_ams = {
  564. "ams": [
  565. {
  566. "id": 0,
  567. "tray": [
  568. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000", "remain": 80},
  569. {"id": 1, "tray_type": "PETG", "tray_color": "00FF00", "remain": 60},
  570. ],
  571. },
  572. ],
  573. "tray_exist_bits": "3", # Both slots occupied (0b11)
  574. "power_on_flag": True,
  575. }
  576. mqtt_client._handle_ams_data(initial_ams)
  577. # Spool removed from slot 1 while printer is running
  578. removal_ams = {
  579. "ams": [
  580. {
  581. "id": 0,
  582. "tray": [{"id": 0}, {"id": 1}],
  583. },
  584. ],
  585. "tray_exist_bits": "1", # Only slot 0 occupied (0b01)
  586. "power_on_flag": True,
  587. }
  588. mqtt_client._handle_ams_data(removal_ams)
  589. # Slot 0 preserved, slot 1 cleared
  590. ams_data = mqtt_client.state.raw_data["ams"]
  591. assert ams_data[0]["tray"][0]["tray_type"] == "PLA", "Slot 0 should be preserved"
  592. assert ams_data[0]["tray"][1]["tray_type"] == "", "Slot 1 should be cleared on removal"
  593. assert ams_data[0]["tray"][1]["tray_color"] == "", "Slot 1 color should be cleared"
  594. def test_power_on_flag_defaults_true_when_absent(self, mqtt_client):
  595. """When power_on_flag is not in the MQTT data, clearing must proceed normally.
  596. Ensures backwards compatibility with firmware that doesn't send power_on_flag.
  597. """
  598. # Initial state
  599. initial_ams = {
  600. "ams": [
  601. {
  602. "id": 0,
  603. "tray": [
  604. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000", "remain": 80},
  605. ],
  606. },
  607. ],
  608. "tray_exist_bits": "1",
  609. }
  610. mqtt_client._handle_ams_data(initial_ams)
  611. # Update WITHOUT power_on_flag — should still clear when bit=0
  612. update_ams = {
  613. "ams": [{"id": 0, "tray": [{"id": 0}]}],
  614. "tray_exist_bits": "0",
  615. # No power_on_flag key at all
  616. }
  617. mqtt_client._handle_ams_data(update_ams)
  618. ams_data = mqtt_client.state.raw_data["ams"]
  619. assert ams_data[0]["tray"][0]["tray_type"] == "", (
  620. "Without power_on_flag, clearing should proceed (defaults to True)"
  621. )
  622. class TestNozzleRackData:
  623. """Tests for nozzle rack data parsing from H2 series device.nozzle.info."""
  624. @pytest.fixture
  625. def mqtt_client(self):
  626. """Create a BambuMQTTClient instance for testing."""
  627. from backend.app.services.bambu_mqtt import BambuMQTTClient
  628. client = BambuMQTTClient(
  629. ip_address="192.168.1.100",
  630. serial_number="TEST123",
  631. access_code="12345678",
  632. )
  633. return client
  634. def test_h2c_nozzle_rack_populated_with_8_entries(self, mqtt_client):
  635. """H2C provides 8 nozzle entries: IDs 0,1 (L/R hotend) + 16-21 (rack)."""
  636. payload = {
  637. "print": {
  638. "device": {
  639. "nozzle": {
  640. "info": [
  641. {
  642. "id": 0,
  643. "type": "HS",
  644. "diameter": "0.4",
  645. "wear": 5,
  646. "stat": 1,
  647. "max_temp": 300,
  648. "serial_number": "SN-L",
  649. },
  650. {
  651. "id": 1,
  652. "type": "HS",
  653. "diameter": "0.4",
  654. "wear": 3,
  655. "stat": 0,
  656. "max_temp": 300,
  657. "serial_number": "SN-R",
  658. },
  659. {
  660. "id": 16,
  661. "type": "HS",
  662. "diameter": "0.4",
  663. "wear": 10,
  664. "stat": 0,
  665. "max_temp": 300,
  666. "serial_number": "SN-16",
  667. },
  668. {
  669. "id": 17,
  670. "type": "HH01",
  671. "diameter": "0.6",
  672. "wear": 0,
  673. "stat": 0,
  674. "max_temp": 300,
  675. "serial_number": "SN-17",
  676. },
  677. {
  678. "id": 18,
  679. "type": "HS",
  680. "diameter": "0.4",
  681. "wear": 2,
  682. "stat": 0,
  683. "max_temp": 300,
  684. "serial_number": "SN-18",
  685. },
  686. {
  687. "id": 19,
  688. "type": "",
  689. "diameter": "",
  690. "wear": None,
  691. "stat": None,
  692. "max_temp": 0,
  693. "serial_number": "",
  694. },
  695. {
  696. "id": 20,
  697. "type": "",
  698. "diameter": "",
  699. "wear": None,
  700. "stat": None,
  701. "max_temp": 0,
  702. "serial_number": "",
  703. },
  704. {
  705. "id": 21,
  706. "type": "",
  707. "diameter": "",
  708. "wear": None,
  709. "stat": None,
  710. "max_temp": 0,
  711. "serial_number": "",
  712. },
  713. ]
  714. }
  715. }
  716. }
  717. }
  718. mqtt_client._process_message(payload)
  719. assert len(mqtt_client.state.nozzle_rack) == 8
  720. ids = [n["id"] for n in mqtt_client.state.nozzle_rack]
  721. assert ids == [0, 1, 16, 17, 18, 19, 20, 21]
  722. def test_h2d_nozzle_rack_populated_with_2_entries(self, mqtt_client):
  723. """H2D provides 2 nozzle entries: IDs 0,1 (L/R hotend) — no rack slots."""
  724. payload = {
  725. "print": {
  726. "device": {
  727. "nozzle": {
  728. "info": [
  729. {
  730. "id": 0,
  731. "type": "HS",
  732. "diameter": "0.4",
  733. "wear": 5,
  734. "stat": 1,
  735. "max_temp": 300,
  736. "serial_number": "SN-L",
  737. },
  738. {
  739. "id": 1,
  740. "type": "HS",
  741. "diameter": "0.4",
  742. "wear": 3,
  743. "stat": 1,
  744. "max_temp": 300,
  745. "serial_number": "SN-R",
  746. },
  747. ]
  748. }
  749. }
  750. }
  751. }
  752. mqtt_client._process_message(payload)
  753. assert len(mqtt_client.state.nozzle_rack) == 2
  754. ids = [n["id"] for n in mqtt_client.state.nozzle_rack]
  755. assert ids == [0, 1]
  756. def test_single_nozzle_h2s_populated(self, mqtt_client):
  757. """H2S provides 1 nozzle entry: ID 0 only — single nozzle printer."""
  758. payload = {
  759. "print": {
  760. "device": {
  761. "nozzle": {
  762. "info": [
  763. {
  764. "id": 0,
  765. "type": "HS",
  766. "diameter": "0.4",
  767. "wear": 2,
  768. "stat": 1,
  769. "max_temp": 300,
  770. "serial_number": "SN-0",
  771. },
  772. ]
  773. }
  774. }
  775. }
  776. }
  777. mqtt_client._process_message(payload)
  778. assert len(mqtt_client.state.nozzle_rack) == 1
  779. assert mqtt_client.state.nozzle_rack[0]["id"] == 0
  780. def test_empty_nozzle_info_does_not_populate_rack(self, mqtt_client):
  781. """Empty nozzle info list should not populate nozzle_rack."""
  782. payload = {"print": {"device": {"nozzle": {"info": []}}}}
  783. mqtt_client._process_message(payload)
  784. assert mqtt_client.state.nozzle_rack == []
  785. def test_nozzle_rack_sorted_by_id(self, mqtt_client):
  786. """Nozzle rack entries should be sorted by ID regardless of input order."""
  787. payload = {
  788. "print": {
  789. "device": {
  790. "nozzle": {
  791. "info": [
  792. {"id": 17, "type": "HS", "diameter": "0.6"},
  793. {"id": 0, "type": "HS", "diameter": "0.4"},
  794. {"id": 16, "type": "HS", "diameter": "0.4"},
  795. {"id": 1, "type": "HS", "diameter": "0.4"},
  796. ]
  797. }
  798. }
  799. }
  800. }
  801. mqtt_client._process_message(payload)
  802. ids = [n["id"] for n in mqtt_client.state.nozzle_rack]
  803. assert ids == [0, 1, 16, 17]
  804. def test_nozzle_rack_field_mapping(self, mqtt_client):
  805. """Verify field mapping from MQTT nozzle_info to nozzle_rack dict keys."""
  806. payload = {
  807. "print": {
  808. "device": {
  809. "nozzle": {
  810. "info": [
  811. {
  812. "id": 16,
  813. "type": "HH01",
  814. "diameter": "0.6",
  815. "wear": 15,
  816. "stat": 0,
  817. "max_temp": 320,
  818. "serial_number": "SN-ABC123",
  819. "filament_colour": "FF8800",
  820. "filament_id": "F42",
  821. "tray_type": "ABS",
  822. }
  823. ]
  824. }
  825. }
  826. }
  827. }
  828. mqtt_client._process_message(payload)
  829. slot = mqtt_client.state.nozzle_rack[0]
  830. assert slot["id"] == 16
  831. assert slot["type"] == "HH01"
  832. assert slot["diameter"] == "0.6"
  833. assert slot["wear"] == 15
  834. assert slot["stat"] == 0
  835. assert slot["max_temp"] == 320
  836. assert slot["serial_number"] == "SN-ABC123"
  837. assert slot["filament_color"] == "FF8800"
  838. assert slot["filament_id"] == "F42"
  839. assert slot["filament_type"] == "ABS"
  840. def test_nozzle_info_updates_nozzle_state(self, mqtt_client):
  841. """Nozzle info for IDs 0,1 should also update nozzle state (type/diameter)."""
  842. payload = {
  843. "print": {
  844. "device": {
  845. "nozzle": {
  846. "info": [
  847. {"id": 0, "type": "HS", "diameter": "0.4"},
  848. {"id": 1, "type": "HH01", "diameter": "0.6"},
  849. ]
  850. }
  851. }
  852. }
  853. }
  854. mqtt_client._process_message(payload)
  855. assert mqtt_client.state.nozzles[0].nozzle_type == "HS"
  856. assert mqtt_client.state.nozzles[0].nozzle_diameter == "0.4"
  857. assert mqtt_client.state.nozzles[1].nozzle_type == "HH01"
  858. assert mqtt_client.state.nozzles[1].nozzle_diameter == "0.6"
  859. class TestRequestTopicFailSafe:
  860. """Tests for graceful degradation when broker rejects request topic subscription."""
  861. @pytest.fixture
  862. def mqtt_client(self):
  863. from backend.app.services.bambu_mqtt import BambuMQTTClient
  864. client = BambuMQTTClient(
  865. ip_address="192.168.1.100",
  866. serial_number="TEST123",
  867. access_code="12345678",
  868. )
  869. return client
  870. def test_request_topic_supported_by_default(self, mqtt_client):
  871. """Request topic subscription is attempted by default."""
  872. assert mqtt_client._request_topic_supported is True
  873. assert mqtt_client._request_topic_confirmed is False
  874. def test_on_subscribe_confirms_success(self, mqtt_client):
  875. """Successful SUBACK marks request topic as confirmed."""
  876. from paho.mqtt.reasoncodes import ReasonCode
  877. mqtt_client._request_topic_sub_mid = 42
  878. rc = ReasonCode(9, identifier=0) # SUBACK packetType=9, QoS 0 = success
  879. mqtt_client._on_subscribe(None, None, 42, [rc], None)
  880. assert mqtt_client._request_topic_confirmed is True
  881. assert mqtt_client._request_topic_supported is True
  882. assert mqtt_client._request_topic_sub_mid is None
  883. assert mqtt_client._request_topic_sub_time == 0.0
  884. def test_on_subscribe_detects_rejection(self, mqtt_client):
  885. """SUBACK with failure code disables request topic."""
  886. from paho.mqtt.reasoncodes import ReasonCode
  887. mqtt_client._request_topic_sub_mid = 42
  888. rc = ReasonCode(9, identifier=0x80) # SUBACK packetType=9, 0x80 = failure
  889. mqtt_client._on_subscribe(None, None, 42, [rc], None)
  890. assert mqtt_client._request_topic_supported is False
  891. assert mqtt_client._request_topic_confirmed is False
  892. def test_on_subscribe_ignores_other_mids(self, mqtt_client):
  893. """SUBACK for other subscriptions (e.g. report topic) is ignored."""
  894. from paho.mqtt.reasoncodes import ReasonCode
  895. mqtt_client._request_topic_sub_mid = 42
  896. rc = ReasonCode(9, identifier=0x80)
  897. mqtt_client._on_subscribe(None, None, 99, [rc], None)
  898. # Not affected — mid doesn't match
  899. assert mqtt_client._request_topic_supported is True
  900. def test_disconnect_after_subscription_disables_topic(self, mqtt_client):
  901. """Disconnect within 10s of subscription attempt disables request topic."""
  902. import time
  903. mqtt_client._request_topic_sub_time = time.time()
  904. mqtt_client._request_topic_confirmed = False
  905. mqtt_client._last_message_time = 0.0
  906. mqtt_client._on_disconnect(None, None)
  907. assert mqtt_client._request_topic_supported is False
  908. assert mqtt_client._request_topic_sub_time == 0.0
  909. def test_disconnect_after_confirmation_does_not_disable(self, mqtt_client):
  910. """Disconnect after SUBACK confirmation keeps request topic enabled."""
  911. import time
  912. mqtt_client._request_topic_sub_time = time.time()
  913. mqtt_client._request_topic_confirmed = True
  914. mqtt_client._last_message_time = 0.0
  915. mqtt_client._on_disconnect(None, None)
  916. assert mqtt_client._request_topic_supported is True
  917. def test_late_disconnect_does_not_disable(self, mqtt_client):
  918. """Disconnect long after subscription (>10s) doesn't blame request topic."""
  919. import time
  920. mqtt_client._request_topic_sub_time = time.time() - 30.0
  921. mqtt_client._request_topic_confirmed = False
  922. mqtt_client._last_message_time = 0.0
  923. mqtt_client._on_disconnect(None, None)
  924. assert mqtt_client._request_topic_supported is True
  925. def test_on_connect_skips_request_topic_when_unsupported(self, mqtt_client):
  926. """After marking unsupported, reconnect skips request topic subscription."""
  927. mqtt_client._request_topic_supported = False
  928. subscribe_calls = []
  929. mock_client = type(
  930. "MockClient",
  931. (),
  932. {
  933. "subscribe": lambda self, topic: subscribe_calls.append(topic) or (0, 1),
  934. },
  935. )()
  936. mqtt_client._on_connect(mock_client, None, None, 0)
  937. # Only report topic subscribed, not request topic
  938. assert len(subscribe_calls) == 1
  939. assert subscribe_calls[0] == mqtt_client.topic_subscribe
  940. class TestRequestTopicAmsMapping:
  941. """Tests for capturing ams_mapping from the MQTT request topic."""
  942. @pytest.fixture
  943. def mqtt_client(self):
  944. """Create a BambuMQTTClient instance for testing."""
  945. from backend.app.services.bambu_mqtt import BambuMQTTClient
  946. client = BambuMQTTClient(
  947. ip_address="192.168.1.100",
  948. serial_number="TEST123",
  949. access_code="12345678",
  950. )
  951. return client
  952. def test_captured_ams_mapping_initializes_to_none(self, mqtt_client):
  953. """Verify _captured_ams_mapping starts as None."""
  954. assert mqtt_client._captured_ams_mapping is None
  955. def test_handle_request_message_captures_ams_mapping(self, mqtt_client):
  956. """project_file command with ams_mapping stores the mapping."""
  957. data = {
  958. "print": {
  959. "command": "project_file",
  960. "ams_mapping": [0, 4, -1, -1],
  961. "url": "ftp://192.168.1.100/test.3mf",
  962. }
  963. }
  964. mqtt_client._handle_request_message(data)
  965. assert mqtt_client._captured_ams_mapping == [0, 4, -1, -1]
  966. def test_handle_request_message_ignores_non_print_commands(self, mqtt_client):
  967. """Non-project_file commands don't store ams_mapping."""
  968. data = {
  969. "print": {
  970. "command": "pause",
  971. }
  972. }
  973. mqtt_client._handle_request_message(data)
  974. assert mqtt_client._captured_ams_mapping is None
  975. def test_handle_request_message_ignores_missing_ams_mapping(self, mqtt_client):
  976. """project_file command without ams_mapping doesn't store anything."""
  977. data = {
  978. "print": {
  979. "command": "project_file",
  980. "url": "ftp://192.168.1.100/test.3mf",
  981. }
  982. }
  983. mqtt_client._handle_request_message(data)
  984. assert mqtt_client._captured_ams_mapping is None
  985. def test_handle_request_message_ignores_non_dict_print(self, mqtt_client):
  986. """Non-dict print value is safely ignored."""
  987. data = {"print": "not_a_dict"}
  988. mqtt_client._handle_request_message(data)
  989. assert mqtt_client._captured_ams_mapping is None
  990. def test_handle_request_message_ignores_missing_print(self, mqtt_client):
  991. """Message without print key is safely ignored."""
  992. data = {"pushing": {"command": "pushall"}}
  993. mqtt_client._handle_request_message(data)
  994. assert mqtt_client._captured_ams_mapping is None
  995. def test_captured_mapping_overwrites_previous(self, mqtt_client):
  996. """A new print command overwrites a previously captured mapping."""
  997. mqtt_client._captured_ams_mapping = [0, -1, -1, -1]
  998. data = {
  999. "print": {
  1000. "command": "project_file",
  1001. "ams_mapping": [4, 8, -1, -1],
  1002. }
  1003. }
  1004. mqtt_client._handle_request_message(data)
  1005. assert mqtt_client._captured_ams_mapping == [4, 8, -1, -1]
  1006. def test_print_start_callback_includes_ams_mapping(self, mqtt_client):
  1007. """on_print_start callback data includes captured ams_mapping."""
  1008. start_data = {}
  1009. def on_start(data):
  1010. start_data.update(data)
  1011. mqtt_client.on_print_start = on_start
  1012. mqtt_client._captured_ams_mapping = [0, 4, -1, -1]
  1013. # Trigger print start
  1014. mqtt_client._process_message(
  1015. {
  1016. "print": {
  1017. "gcode_state": "RUNNING",
  1018. "gcode_file": "/data/Metadata/test.gcode",
  1019. "subtask_name": "Test",
  1020. }
  1021. }
  1022. )
  1023. assert start_data.get("ams_mapping") == [0, 4, -1, -1]
  1024. def test_print_start_callback_ams_mapping_none_when_not_captured(self, mqtt_client):
  1025. """on_print_start callback has ams_mapping=None when no mapping captured."""
  1026. start_data = {}
  1027. def on_start(data):
  1028. start_data.update(data)
  1029. mqtt_client.on_print_start = on_start
  1030. mqtt_client._process_message(
  1031. {
  1032. "print": {
  1033. "gcode_state": "RUNNING",
  1034. "gcode_file": "/data/Metadata/test.gcode",
  1035. "subtask_name": "Test",
  1036. }
  1037. }
  1038. )
  1039. assert "ams_mapping" in start_data
  1040. assert start_data["ams_mapping"] is None
  1041. def test_print_complete_callback_includes_ams_mapping(self, mqtt_client):
  1042. """on_print_complete callback data includes captured ams_mapping."""
  1043. complete_data = {}
  1044. def on_complete(data):
  1045. complete_data.update(data)
  1046. mqtt_client.on_print_start = lambda d: None
  1047. mqtt_client.on_print_complete = on_complete
  1048. mqtt_client._captured_ams_mapping = [0, 9, -1, -1]
  1049. # Start print
  1050. mqtt_client._process_message(
  1051. {
  1052. "print": {
  1053. "gcode_state": "RUNNING",
  1054. "gcode_file": "/data/Metadata/test.gcode",
  1055. "subtask_name": "Test",
  1056. }
  1057. }
  1058. )
  1059. # Complete print
  1060. mqtt_client._process_message(
  1061. {
  1062. "print": {
  1063. "gcode_state": "FINISH",
  1064. "gcode_file": "/data/Metadata/test.gcode",
  1065. "subtask_name": "Test",
  1066. }
  1067. }
  1068. )
  1069. assert complete_data.get("ams_mapping") == [0, 9, -1, -1]
  1070. def test_captured_mapping_cleared_after_print_complete(self, mqtt_client):
  1071. """_captured_ams_mapping is reset to None after print completion."""
  1072. mqtt_client.on_print_start = lambda d: None
  1073. mqtt_client.on_print_complete = lambda d: None
  1074. mqtt_client._captured_ams_mapping = [0, 4, -1, -1]
  1075. # Start print
  1076. mqtt_client._process_message(
  1077. {
  1078. "print": {
  1079. "gcode_state": "RUNNING",
  1080. "gcode_file": "/data/Metadata/test.gcode",
  1081. "subtask_name": "Test",
  1082. }
  1083. }
  1084. )
  1085. # Complete print
  1086. mqtt_client._process_message(
  1087. {
  1088. "print": {
  1089. "gcode_state": "FINISH",
  1090. "gcode_file": "/data/Metadata/test.gcode",
  1091. "subtask_name": "Test",
  1092. }
  1093. }
  1094. )
  1095. assert mqtt_client._captured_ams_mapping is None
  1096. def test_full_flow_capture_and_deliver(self, mqtt_client):
  1097. """Full flow: slicer sends print command → MQTT captures mapping → completion delivers it."""
  1098. complete_data = {}
  1099. def on_complete(data):
  1100. complete_data.update(data)
  1101. mqtt_client.on_print_start = lambda d: None
  1102. mqtt_client.on_print_complete = on_complete
  1103. # 1. Slicer sends print command (captured from request topic)
  1104. mqtt_client._handle_request_message(
  1105. {
  1106. "print": {
  1107. "command": "project_file",
  1108. "ams_mapping": [4, 9, -1, -1],
  1109. "url": "ftp://192.168.1.100/model.3mf",
  1110. }
  1111. }
  1112. )
  1113. assert mqtt_client._captured_ams_mapping == [4, 9, -1, -1]
  1114. # 2. Printer reports RUNNING
  1115. mqtt_client._process_message(
  1116. {
  1117. "print": {
  1118. "gcode_state": "RUNNING",
  1119. "gcode_file": "/data/Metadata/model.gcode",
  1120. "subtask_name": "Model",
  1121. }
  1122. }
  1123. )
  1124. # 3. Printer reports FINISH
  1125. mqtt_client._process_message(
  1126. {
  1127. "print": {
  1128. "gcode_state": "FINISH",
  1129. "gcode_file": "/data/Metadata/model.gcode",
  1130. "subtask_name": "Model",
  1131. }
  1132. }
  1133. )
  1134. assert complete_data["ams_mapping"] == [4, 9, -1, -1]
  1135. assert complete_data["status"] == "completed"
  1136. # Mapping cleared after completion
  1137. assert mqtt_client._captured_ams_mapping is None
  1138. # ---------------------------------------------------------------------------
  1139. # tray_now disambiguation helpers
  1140. # ---------------------------------------------------------------------------
  1141. def _ams_payload(tray_now, ams_units=None, tray_exist_bits=None, ams_exist_bits=None):
  1142. """Build minimal print.ams payload for tray_now disambiguation tests."""
  1143. ams = {"tray_now": str(tray_now)}
  1144. if ams_units is not None:
  1145. ams["ams"] = ams_units
  1146. if tray_exist_bits is not None:
  1147. ams["tray_exist_bits"] = tray_exist_bits
  1148. if ams_exist_bits is not None:
  1149. ams["ams_exist_bits"] = ams_exist_bits
  1150. return {"print": {"ams": ams}}
  1151. def _extruder_info_payload(extruders):
  1152. """Build device.extruder.info payload (dual-nozzle detection + snow).
  1153. Each entry in *extruders* is a dict with at least ``id`` and ``snow``.
  1154. """
  1155. return {
  1156. "print": {
  1157. "device": {
  1158. "extruder": {
  1159. "info": extruders,
  1160. }
  1161. }
  1162. }
  1163. }
  1164. def _extruder_state_payload(state_val):
  1165. """Build device.extruder.state payload (active extruder via bit 8)."""
  1166. return {
  1167. "print": {
  1168. "device": {
  1169. "extruder": {
  1170. "state": state_val,
  1171. }
  1172. }
  1173. }
  1174. }
  1175. # ---------------------------------------------------------------------------
  1176. # 1. Single-nozzle X1E — direct passthrough
  1177. # ---------------------------------------------------------------------------
  1178. class TestTrayNowSingleNozzleX1E:
  1179. """Single-nozzle, 1 AMS — tray_now is a direct passthrough."""
  1180. @pytest.fixture
  1181. def mqtt_client(self):
  1182. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1183. return BambuMQTTClient(
  1184. ip_address="192.168.1.100",
  1185. serial_number="TEST_X1E",
  1186. access_code="12345678",
  1187. )
  1188. def test_tray_now_direct_passthrough_slot_0_to_3(self, mqtt_client):
  1189. """Each tray_now 0-3 maps 1:1 on single-nozzle printers."""
  1190. for slot in range(4):
  1191. mqtt_client._process_message(_ams_payload(slot))
  1192. assert mqtt_client.state.tray_now == slot
  1193. def test_tray_now_255_means_unloaded(self, mqtt_client):
  1194. """tray_now=255 means no filament loaded."""
  1195. mqtt_client._process_message(_ams_payload(255))
  1196. assert mqtt_client.state.tray_now == 255
  1197. def test_single_extruder_does_not_trigger_dual_nozzle(self, mqtt_client):
  1198. """device.extruder.info with 1 entry must NOT set _is_dual_nozzle."""
  1199. mqtt_client._process_message(_extruder_info_payload([{"id": 0, "snow": 0xFF00FF}]))
  1200. assert mqtt_client._is_dual_nozzle is False
  1201. def test_last_loaded_tray_survives_unload(self, mqtt_client):
  1202. """Load tray 2, unload → last_loaded_tray stays 2."""
  1203. mqtt_client._process_message(_ams_payload(2))
  1204. assert mqtt_client.state.last_loaded_tray == 2
  1205. mqtt_client._process_message(_ams_payload(255))
  1206. assert mqtt_client.state.tray_now == 255
  1207. assert mqtt_client.state.last_loaded_tray == 2
  1208. # ---------------------------------------------------------------------------
  1209. # 2. Single-nozzle P2S — multiple AMS, global IDs pass through
  1210. # ---------------------------------------------------------------------------
  1211. class TestTrayNowSingleNozzleP2S:
  1212. """Single-nozzle, 2 AMS — tray_now > 3 passes through as global ID."""
  1213. @pytest.fixture
  1214. def mqtt_client(self):
  1215. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1216. return BambuMQTTClient(
  1217. ip_address="192.168.1.100",
  1218. serial_number="TEST_P2S",
  1219. access_code="12345678",
  1220. )
  1221. def test_tray_now_ams1_global_ids_4_to_7(self, mqtt_client):
  1222. """tray_now 4-7 are global IDs for AMS 1 on single-nozzle printers."""
  1223. for global_id in range(4, 8):
  1224. mqtt_client._process_message(_ams_payload(global_id))
  1225. assert mqtt_client.state.tray_now == global_id
  1226. def test_tray_change_across_ams_units(self, mqtt_client):
  1227. """Switch from AMS 0 slot 1 → AMS 1 slot 2 (global 6)."""
  1228. mqtt_client._process_message(_ams_payload(1))
  1229. assert mqtt_client.state.tray_now == 1
  1230. mqtt_client._process_message(_ams_payload(6))
  1231. assert mqtt_client.state.tray_now == 6
  1232. # ---------------------------------------------------------------------------
  1233. # 2b. Single-nozzle P2S — multi-AMS local slot disambiguation (#420)
  1234. # ---------------------------------------------------------------------------
  1235. class TestTrayNowP2SMultiAmsDisambiguation:
  1236. """P2S firmware sends local slot IDs (0-3) in tray_now even with dual AMS.
  1237. When ams_exist_bits indicates >1 AMS unit and tray_now is 0-3, the backend
  1238. should use the MQTT mapping field (snow-encoded) to resolve the correct
  1239. global tray ID.
  1240. """
  1241. @pytest.fixture
  1242. def mqtt_client(self):
  1243. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1244. client = BambuMQTTClient(
  1245. ip_address="192.168.1.100",
  1246. serial_number="TEST_P2S_DUAL",
  1247. access_code="12345678",
  1248. )
  1249. return client
  1250. def test_resolves_ams1_slot1_from_mapping(self, mqtt_client):
  1251. """tray_now=1 with mapping=[257] → global ID 5 (AMS1-T1).
  1252. 257 snow-decoded: ams_hw_id=1, slot=1 → global 1*4+1=5.
  1253. """
  1254. # Set mapping field in raw_data (as the MQTT handler would)
  1255. mqtt_client.state.raw_data["mapping"] = [257]
  1256. mqtt_client._process_message(
  1257. _ams_payload(1, ams_exist_bits="3") # '3' = 0b11 → AMS 0 and 1
  1258. )
  1259. assert mqtt_client.state.tray_now == 5
  1260. def test_resolves_ams1_slot0_from_mapping(self, mqtt_client):
  1261. """tray_now=0 with mapping=[256] → global ID 4 (AMS1-T0).
  1262. 256 snow-decoded: ams_hw_id=1, slot=0 → global 1*4+0=4.
  1263. """
  1264. mqtt_client.state.raw_data["mapping"] = [256]
  1265. mqtt_client._process_message(_ams_payload(0, ams_exist_bits="3"))
  1266. assert mqtt_client.state.tray_now == 4
  1267. def test_resolves_ams1_slot3_from_mapping(self, mqtt_client):
  1268. """tray_now=3 with mapping=[259] → global ID 7 (AMS1-T3).
  1269. 259 snow-decoded: ams_hw_id=1, slot=3 → global 1*4+3=7.
  1270. """
  1271. mqtt_client.state.raw_data["mapping"] = [259]
  1272. mqtt_client._process_message(_ams_payload(3, ams_exist_bits="3"))
  1273. assert mqtt_client.state.tray_now == 7
  1274. def test_ams0_slot_unchanged_when_mapping_confirms_ams0(self, mqtt_client):
  1275. """tray_now=1 with mapping=[1] → stays 1 (AMS0-T1).
  1276. 1 snow-decoded: ams_hw_id=0, slot=1 → global 0*4+1=1.
  1277. """
  1278. mqtt_client.state.raw_data["mapping"] = [1]
  1279. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1280. assert mqtt_client.state.tray_now == 1
  1281. def test_multicolor_resolves_ams1_from_multi_entry_mapping(self, mqtt_client):
  1282. """Multi-color print: mapping=[0, 257] → tray_now=1 resolves to AMS1-T1 (5).
  1283. Entry 0: ams_hw_id=0, slot=0 (local 0) — doesn't match tray_now=1.
  1284. Entry 257: ams_hw_id=1, slot=1 (local 1) — matches tray_now=1 → global 5.
  1285. """
  1286. mqtt_client.state.raw_data["mapping"] = [0, 257]
  1287. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1288. assert mqtt_client.state.tray_now == 5
  1289. def test_multicolor_four_slot_mapping(self, mqtt_client):
  1290. """mapping=[65535, 65535, 65535, 257] → tray_now=1 resolves to global 5.
  1291. Only entry 257 has local slot=1, other entries are unmapped (65535).
  1292. Reproduces exact data from issue #420 support package.
  1293. """
  1294. mqtt_client.state.raw_data["mapping"] = [65535, 65535, 65535, 257]
  1295. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1296. assert mqtt_client.state.tray_now == 5
  1297. def test_ambiguous_mapping_falls_back_to_local_slot(self, mqtt_client):
  1298. """Two AMS units with same local slot in mapping → ambiguous, keep local slot.
  1299. mapping=[1, 257]: both have local slot 1 (AMS0-T1 and AMS1-T1).
  1300. Cannot disambiguate → fall back to tray_now=1.
  1301. """
  1302. mqtt_client.state.raw_data["mapping"] = [1, 257]
  1303. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1304. assert mqtt_client.state.tray_now == 1
  1305. def test_no_mapping_falls_back_to_local_slot(self, mqtt_client):
  1306. """No mapping field available → fall back to raw tray_now."""
  1307. # No mapping in raw_data (e.g. manual filament load, not during print)
  1308. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1309. assert mqtt_client.state.tray_now == 1
  1310. def test_empty_mapping_falls_back_to_local_slot(self, mqtt_client):
  1311. """Empty mapping list → fall back to raw tray_now."""
  1312. mqtt_client.state.raw_data["mapping"] = []
  1313. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1314. assert mqtt_client.state.tray_now == 1
  1315. def test_single_ams_passthrough(self, mqtt_client):
  1316. """Single AMS (ams_exist_bits='1') → tray_now 0-3 is direct global ID."""
  1317. mqtt_client._process_message(_ams_payload(2, ams_exist_bits="1"))
  1318. assert mqtt_client.state.tray_now == 2
  1319. def test_no_ams_exist_bits_passthrough(self, mqtt_client):
  1320. """No ams_exist_bits in payload → fall back to raw tray_now."""
  1321. mqtt_client._process_message(_ams_payload(1))
  1322. assert mqtt_client.state.tray_now == 1
  1323. def test_tray_now_255_unaffected_by_multi_ams(self, mqtt_client):
  1324. """tray_now=255 (unloaded) passes through regardless of AMS count."""
  1325. mqtt_client.state.raw_data["mapping"] = [257]
  1326. mqtt_client._process_message(_ams_payload(255, ams_exist_bits="3"))
  1327. assert mqtt_client.state.tray_now == 255
  1328. def test_tray_now_above_3_unaffected(self, mqtt_client):
  1329. """tray_now > 3 is already a global ID and passes through directly."""
  1330. mqtt_client._process_message(_ams_payload(6, ams_exist_bits="3"))
  1331. assert mqtt_client.state.tray_now == 6
  1332. def test_last_loaded_tray_uses_resolved_global_id(self, mqtt_client):
  1333. """last_loaded_tray should reflect the resolved global ID, not local slot."""
  1334. mqtt_client.state.raw_data["mapping"] = [257]
  1335. mqtt_client.state.state = "RUNNING"
  1336. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1337. assert mqtt_client.state.tray_now == 5
  1338. assert mqtt_client.state.last_loaded_tray == 5
  1339. class TestResolveLocalSlotFromMapping:
  1340. """Unit tests for _resolve_local_slot_from_mapping static method."""
  1341. def test_single_match_ams0(self):
  1342. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1343. assert BambuMQTTClient._resolve_local_slot_from_mapping(1, [1]) == 1
  1344. def test_single_match_ams1(self):
  1345. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1346. # 257 = 1*256 + 1 → AMS1 slot1 → global 5
  1347. assert BambuMQTTClient._resolve_local_slot_from_mapping(1, [257]) == 5
  1348. def test_single_match_ams2(self):
  1349. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1350. # 514 = 2*256 + 2 → AMS2 slot2 → global 10
  1351. assert BambuMQTTClient._resolve_local_slot_from_mapping(2, [514]) == 10
  1352. def test_unmapped_entries_skipped(self):
  1353. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1354. assert BambuMQTTClient._resolve_local_slot_from_mapping(1, [65535, 65535, 65535, 257]) == 5
  1355. def test_no_match_returns_none(self):
  1356. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1357. # mapping has slot 0 only, looking for slot 2
  1358. assert BambuMQTTClient._resolve_local_slot_from_mapping(2, [0]) is None
  1359. def test_ambiguous_returns_none(self):
  1360. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1361. # Both AMS0 slot1 (1) and AMS1 slot1 (257) → ambiguous
  1362. assert BambuMQTTClient._resolve_local_slot_from_mapping(1, [1, 257]) is None
  1363. def test_none_mapping_returns_none(self):
  1364. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1365. assert BambuMQTTClient._resolve_local_slot_from_mapping(1, None) is None
  1366. def test_empty_mapping_returns_none(self):
  1367. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1368. assert BambuMQTTClient._resolve_local_slot_from_mapping(1, []) is None
  1369. def test_ams_ht_slot0_match(self):
  1370. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1371. # AMS-HT id=128: snow = 128*256 + 0 = 32768
  1372. assert BambuMQTTClient._resolve_local_slot_from_mapping(0, [32768]) == 128
  1373. # ---------------------------------------------------------------------------
  1374. # 3. H2D Pro — initial state detection
  1375. # ---------------------------------------------------------------------------
  1376. class TestTrayNowDualNozzleH2DSetup:
  1377. """H2D Pro initial state detection."""
  1378. @pytest.fixture
  1379. def mqtt_client(self):
  1380. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1381. return BambuMQTTClient(
  1382. ip_address="192.168.1.100",
  1383. serial_number="TEST_H2D",
  1384. access_code="12345678",
  1385. )
  1386. def test_dual_nozzle_detected_from_extruder_info(self, mqtt_client):
  1387. """2 entries in device.extruder.info → _is_dual_nozzle=True."""
  1388. mqtt_client._process_message(
  1389. _extruder_info_payload(
  1390. [
  1391. {"id": 0, "snow": 0xFF00FF},
  1392. {"id": 1, "snow": 0xFF00FF},
  1393. ]
  1394. )
  1395. )
  1396. assert mqtt_client._is_dual_nozzle is True
  1397. def test_ams_extruder_map_parsed_from_info_field(self, mqtt_client):
  1398. """AMS info field is hex: 0x2003 → ext 0 (right), 0x2104 → ext 1 (left)."""
  1399. # MQTT sends info as string; BambuStudio parses as hex via stoull(str, 16)
  1400. ams_units = [
  1401. {"id": 0, "info": "2003", "tray": [{"id": i} for i in range(4)]},
  1402. {"id": 128, "info": "2104", "tray": [{"id": 0}]},
  1403. ]
  1404. payload = {
  1405. "print": {
  1406. "ams": {
  1407. "ams": ams_units,
  1408. "tray_now": "255",
  1409. "tray_exist_bits": "1000f",
  1410. },
  1411. }
  1412. }
  1413. mqtt_client._process_message(payload)
  1414. # 0x2003: bits 8-11 = (0x2003 >> 8) & 0xF = 0x20 & 0xF = 0 → extruder 0 (right)
  1415. # 0x2104: bits 8-11 = (0x2104 >> 8) & 0xF = 0x21 & 0xF = 1 → extruder 1 (left)
  1416. assert mqtt_client.state.ams_extruder_map == {"0": 0, "128": 1}
  1417. def test_ams_extruder_map_real_h2d_values(self, mqtt_client):
  1418. """Real H2D MQTT values: AMS2 Pro on right, AMS-HT on left."""
  1419. ams_units = [
  1420. {"id": 0, "info": "10001003", "tray": [{"id": i} for i in range(4)]},
  1421. {"id": 128, "info": "10002104", "tray": [{"id": 0}]},
  1422. ]
  1423. payload = {
  1424. "print": {
  1425. "ams": {
  1426. "ams": ams_units,
  1427. "tray_now": "255",
  1428. "tray_exist_bits": "1000a",
  1429. },
  1430. }
  1431. }
  1432. mqtt_client._process_message(payload)
  1433. # 0x10001003: bits 8-11 = (0x10001003 >> 8) & 0xF = 0x10 & 0xF = 0 → right
  1434. # 0x10002104: bits 8-11 = (0x10002104 >> 8) & 0xF = 0x21 & 0xF = 1 → left
  1435. assert mqtt_client.state.ams_extruder_map == {"0": 0, "128": 1}
  1436. def test_ams_extruder_map_skips_uninitialized(self, mqtt_client):
  1437. """extruder_id 0xE means uninitialized AMS — should be skipped."""
  1438. ams_units = [
  1439. {"id": 0, "info": "e03", "tray": [{"id": i} for i in range(4)]},
  1440. ]
  1441. payload = {
  1442. "print": {
  1443. "ams": {
  1444. "ams": ams_units,
  1445. "tray_now": "255",
  1446. "tray_exist_bits": "f",
  1447. },
  1448. }
  1449. }
  1450. mqtt_client._process_message(payload)
  1451. assert mqtt_client.state.ams_extruder_map == {}
  1452. def test_ams_extruder_map_partial_update_preserves_entries(self, mqtt_client):
  1453. """Partial MQTT update with one AMS should not overwrite other entries."""
  1454. # First: full update with both AMS units
  1455. full_payload = {
  1456. "print": {
  1457. "ams": {
  1458. "ams": [
  1459. {"id": 0, "info": "2003", "tray": [{"id": i} for i in range(4)]},
  1460. {"id": 128, "info": "2104", "tray": [{"id": 0}]},
  1461. ],
  1462. "tray_now": "255",
  1463. "tray_exist_bits": "1000f",
  1464. },
  1465. }
  1466. }
  1467. mqtt_client._process_message(full_payload)
  1468. assert mqtt_client.state.ams_extruder_map == {"0": 0, "128": 1}
  1469. # Then: partial update with only AMS 0 (no info field this time)
  1470. partial_payload = {
  1471. "print": {
  1472. "ams": {
  1473. "ams": [
  1474. {"id": 0, "tray": [{"id": 0, "remain": 50}]},
  1475. ],
  1476. "tray_now": "0",
  1477. "tray_exist_bits": "1000f",
  1478. },
  1479. }
  1480. }
  1481. mqtt_client._process_message(partial_payload)
  1482. # Both entries should still be present
  1483. assert mqtt_client.state.ams_extruder_map == {"0": 0, "128": 1}
  1484. def test_dual_nozzle_detection_before_ams_in_same_message(self, mqtt_client):
  1485. """Dual-nozzle detection at line 538 happens before _handle_ams_data() at line 549.
  1486. If both arrive in the same message, tray_now disambiguation already uses dual-nozzle logic.
  1487. """
  1488. payload = {
  1489. "print": {
  1490. "device": {
  1491. "extruder": {
  1492. "info": [
  1493. {"id": 0, "snow": 0xFF00FF},
  1494. {"id": 1, "snow": 0xFF00FF},
  1495. ],
  1496. "state": 0x0001,
  1497. }
  1498. },
  1499. "ams": {
  1500. "ams": [
  1501. {"id": 0, "info": "2003", "tray": [{"id": i} for i in range(4)]},
  1502. ],
  1503. "tray_now": "2",
  1504. "tray_exist_bits": "f",
  1505. },
  1506. }
  1507. }
  1508. mqtt_client._process_message(payload)
  1509. # Dual-nozzle was detected; AMS 0 on right extruder (active by default);
  1510. # snow is 0xFF00FF (unloaded), so falls through to ams_extruder_map fallback.
  1511. # Single AMS on extruder 0 → global_id = 0*4+2 = 2
  1512. assert mqtt_client._is_dual_nozzle is True
  1513. assert mqtt_client.state.tray_now == 2
  1514. # ---------------------------------------------------------------------------
  1515. # Shared H2D fixture for classes 4-8
  1516. # ---------------------------------------------------------------------------
  1517. class _H2DFixtureMixin:
  1518. """Mixin providing a pre-configured H2D Pro client."""
  1519. @pytest.fixture
  1520. def mqtt_client(self):
  1521. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1522. return BambuMQTTClient(
  1523. ip_address="192.168.1.100",
  1524. serial_number="TEST_H2D",
  1525. access_code="12345678",
  1526. )
  1527. @pytest.fixture
  1528. def h2d_client(self, mqtt_client):
  1529. """Pre-configure as H2D Pro: dual-nozzle + ams_extruder_map."""
  1530. mqtt_client._process_message(
  1531. {
  1532. "print": {
  1533. "device": {
  1534. "extruder": {
  1535. "info": [
  1536. {"id": 0, "snow": 0xFF00FF},
  1537. {"id": 1, "snow": 0xFF00FF},
  1538. ],
  1539. "state": 0x0001, # right extruder active
  1540. }
  1541. },
  1542. "ams": {
  1543. "ams": [
  1544. {"id": 0, "info": "2003", "tray": [{"id": i} for i in range(4)]},
  1545. {"id": 128, "info": "2104", "tray": [{"id": 0}]},
  1546. ],
  1547. "tray_now": "255",
  1548. "tray_exist_bits": "1000f",
  1549. },
  1550. }
  1551. }
  1552. )
  1553. assert mqtt_client._is_dual_nozzle is True
  1554. assert mqtt_client.state.ams_extruder_map == {"0": 0, "128": 1}
  1555. return mqtt_client
  1556. # ---------------------------------------------------------------------------
  1557. # 4. H2D Snow field disambiguation
  1558. # ---------------------------------------------------------------------------
  1559. class TestTrayNowDualNozzleH2DSnow(_H2DFixtureMixin):
  1560. """Snow field disambiguation (primary path)."""
  1561. def test_snow_disambiguates_ams0_slot(self, h2d_client):
  1562. """snow ext[0]=AMS 0 slot 2, tray_now='2' → global 2."""
  1563. # Send snow update FIRST (snow is parsed AFTER tray_now in the same message,
  1564. # so we need it in a prior message).
  1565. snow_val = 0 << 8 | 2 # AMS 0 slot 2 = raw 2
  1566. h2d_client._process_message(
  1567. _extruder_info_payload(
  1568. [
  1569. {"id": 0, "snow": snow_val},
  1570. {"id": 1, "snow": 0xFF00FF},
  1571. ]
  1572. )
  1573. )
  1574. assert h2d_client.state.h2d_extruder_snow.get(0) == 2
  1575. # Now send tray_now=2
  1576. h2d_client._process_message(_ams_payload(2))
  1577. assert h2d_client.state.tray_now == 2
  1578. def test_snow_disambiguates_ams_ht_to_128(self, h2d_client):
  1579. """snow ext[1]=AMS HT (128), left active, tray_now='0' → global 128."""
  1580. # Snow: extruder 1 → AMS 128 slot 0
  1581. snow_val = 128 << 8 | 0 # = 32768
  1582. h2d_client._process_message(
  1583. _extruder_info_payload(
  1584. [
  1585. {"id": 0, "snow": 0xFF00FF},
  1586. {"id": 1, "snow": snow_val},
  1587. ]
  1588. )
  1589. )
  1590. assert h2d_client.state.h2d_extruder_snow.get(1) == 128
  1591. # Switch to left extruder
  1592. h2d_client._process_message(_extruder_state_payload(0x0100))
  1593. assert h2d_client.state.active_extruder == 1
  1594. # tray_now="0" with left extruder active, snow says AMS HT (128)
  1595. # AMS HT snow_slot = 0 (single slot), parsed_tray_now = 0 → match
  1596. h2d_client._process_message(_ams_payload(0))
  1597. assert h2d_client.state.tray_now == 128
  1598. def test_snow_updates_h2d_extruder_snow_state(self, h2d_client):
  1599. """Verify state.h2d_extruder_snow dict is populated correctly."""
  1600. snow_ext0 = 1 << 8 | 3 # AMS 1 slot 3 → global 7
  1601. snow_ext1 = 0 << 8 | 0 # AMS 0 slot 0 → global 0
  1602. h2d_client._process_message(
  1603. _extruder_info_payload(
  1604. [
  1605. {"id": 0, "snow": snow_ext0},
  1606. {"id": 1, "snow": snow_ext1},
  1607. ]
  1608. )
  1609. )
  1610. assert h2d_client.state.h2d_extruder_snow[0] == 7
  1611. assert h2d_client.state.h2d_extruder_snow[1] == 0
  1612. def test_snow_unloaded_value(self, h2d_client):
  1613. """snow=0xFFFF (ams_id=255, slot=255) → 255 (unloaded)."""
  1614. h2d_client._process_message(
  1615. _extruder_info_payload(
  1616. [
  1617. {"id": 0, "snow": 0xFFFF},
  1618. {"id": 1, "snow": 0xFFFF},
  1619. ]
  1620. )
  1621. )
  1622. assert h2d_client.state.h2d_extruder_snow[0] == 255
  1623. assert h2d_client.state.h2d_extruder_snow[1] == 255
  1624. def test_snow_initial_sentinel_not_stored(self, h2d_client):
  1625. """snow=0xFF00FF (firmware initial sentinel) is not parsed into h2d_extruder_snow."""
  1626. # 0xFF00FF has ams_id=0xFF00=65280 which doesn't match any branch
  1627. h2d_client._process_message(
  1628. _extruder_info_payload(
  1629. [
  1630. {"id": 0, "snow": 0xFF00FF},
  1631. {"id": 1, "snow": 0xFF00FF},
  1632. ]
  1633. )
  1634. )
  1635. # Snow dict should remain empty (no matching branch)
  1636. assert h2d_client.state.h2d_extruder_snow == {}
  1637. # ---------------------------------------------------------------------------
  1638. # 5. H2D Pending target disambiguation
  1639. # ---------------------------------------------------------------------------
  1640. class TestTrayNowDualNozzleH2DPendingTarget(_H2DFixtureMixin):
  1641. """Pending target disambiguation (when Bambuddy initiates load)."""
  1642. def test_pending_target_matches_slot(self, h2d_client):
  1643. """pending=5, tray_now='1' (5%4=1 matches) → tray_now=5."""
  1644. h2d_client.state.pending_tray_target = 5
  1645. h2d_client._process_message(_ams_payload(1))
  1646. assert h2d_client.state.tray_now == 5
  1647. assert h2d_client.state.pending_tray_target is None # cleared
  1648. def test_pending_target_slot_mismatch(self, h2d_client):
  1649. """pending=5, tray_now='2' → uses raw slot, clears pending."""
  1650. h2d_client.state.pending_tray_target = 5
  1651. h2d_client._process_message(_ams_payload(2))
  1652. # Slot 2 != 5%4=1 → mismatch, uses raw slot 2
  1653. assert h2d_client.state.tray_now == 2
  1654. assert h2d_client.state.pending_tray_target is None
  1655. def test_pending_target_takes_priority_over_snow(self, h2d_client):
  1656. """When both pending and snow are set, pending wins."""
  1657. # Set up snow for extruder 0 → AMS 0 slot 1 → global 1
  1658. snow_val = 0 << 8 | 1
  1659. h2d_client._process_message(
  1660. _extruder_info_payload(
  1661. [
  1662. {"id": 0, "snow": snow_val},
  1663. {"id": 1, "snow": 0xFF00FF},
  1664. ]
  1665. )
  1666. )
  1667. assert h2d_client.state.h2d_extruder_snow.get(0) == 1
  1668. # Set pending target to AMS 1 slot 1 (global 5)
  1669. h2d_client.state.pending_tray_target = 5
  1670. # tray_now="1" — matches pending (5%4=1), pending should win over snow
  1671. h2d_client._process_message(_ams_payload(1))
  1672. assert h2d_client.state.tray_now == 5
  1673. # ---------------------------------------------------------------------------
  1674. # 6. H2D ams_extruder_map fallback
  1675. # ---------------------------------------------------------------------------
  1676. class TestTrayNowDualNozzleH2DFallback(_H2DFixtureMixin):
  1677. """ams_extruder_map fallback (no pending, no snow)."""
  1678. def test_single_ams_on_extruder_computes_global_id(self, h2d_client):
  1679. """AMS 0 on right extruder, tray_now='2' → 0*4+2=2."""
  1680. # h2d_client has snow=0xFF00FF (unloaded) by default, so snow path skips
  1681. h2d_client._process_message(_ams_payload(2))
  1682. # AMS 0 is the only AMS on extruder 0 (right, active by default)
  1683. # Fallback: single AMS → global = 0*4+2 = 2
  1684. assert h2d_client.state.tray_now == 2
  1685. def test_multiple_ams_keeps_current_if_valid(self, h2d_client):
  1686. """Current tray matches slot → keeps it (multi-AMS on same extruder)."""
  1687. # Set up: two AMS units on the same extruder (right, ext 0)
  1688. h2d_client.state.ams_extruder_map = {"0": 0, "1": 0}
  1689. # Pre-set tray_now=5 (AMS 1 slot 1) — current_ams=1 which is in ams_on_extruder
  1690. h2d_client.state.tray_now = 5
  1691. # tray_now="1" → 5%4=1 matches → keep current=5
  1692. h2d_client._process_message(_ams_payload(1))
  1693. assert h2d_client.state.tray_now == 5
  1694. def test_no_ams_on_extruder_uses_raw_slot(self, h2d_client):
  1695. """No AMS mapped to the active extruder → raw slot as global ID."""
  1696. # All AMS on left extruder, but right is active
  1697. h2d_client.state.ams_extruder_map = {"0": 1, "128": 1}
  1698. h2d_client._process_message(_ams_payload(2))
  1699. assert h2d_client.state.tray_now == 2
  1700. def test_single_ams_ht_on_extruder_returns_unit_id(self, h2d_client):
  1701. """AMS-HT 128 alone on left extruder, slot 0 → global ID 128 (not 512)."""
  1702. # Switch to left extruder (where AMS-HT 128 is mapped)
  1703. h2d_client._process_message(_extruder_state_payload(0x0100))
  1704. # Only AMS-HT 128 on left extruder; no snow available
  1705. h2d_client._process_message(_ams_payload(0))
  1706. assert h2d_client.state.tray_now == 128
  1707. def test_single_ams_ht_ignores_nonzero_slot(self, h2d_client):
  1708. """AMS-HT has single slot; even if printer reports slot 1, global ID = unit ID."""
  1709. h2d_client.state.ams_extruder_map = {"129": 0}
  1710. h2d_client._process_message(_ams_payload(1))
  1711. # AMS-HT 129: global ID = 129, not 129*4+1=517
  1712. assert h2d_client.state.tray_now == 129
  1713. def test_multiple_ams_keeps_current_ams_ht(self, h2d_client):
  1714. """Current tray is AMS-HT 128, slot 0 reported → keeps 128."""
  1715. h2d_client.state.ams_extruder_map = {"0": 0, "128": 0}
  1716. h2d_client.state.tray_now = 128
  1717. h2d_client._process_message(_ams_payload(0))
  1718. assert h2d_client.state.tray_now == 128
  1719. def test_multiple_ams_slot_nonzero_excludes_ams_ht(self, h2d_client):
  1720. """Slot > 0 eliminates AMS-HT candidates; single regular AMS left → resolves."""
  1721. # AMS 0 + AMS-HT 128 both on right extruder
  1722. h2d_client.state.ams_extruder_map = {"0": 0, "128": 0}
  1723. h2d_client.state.tray_now = 255 # no current match
  1724. # Slot 2 → can't be AMS-HT → only AMS 0 → global = 0*4+2 = 2
  1725. h2d_client._process_message(_ams_payload(2))
  1726. assert h2d_client.state.tray_now == 2
  1727. def test_multiple_ams_slot_nonzero_narrows_to_single_ht_excluded(self, h2d_client):
  1728. """Two regular AMS + one AMS-HT, slot > 0 → AMS-HT excluded but still ambiguous."""
  1729. h2d_client.state.ams_extruder_map = {"0": 0, "1": 0, "128": 0}
  1730. h2d_client.state.tray_now = 255
  1731. # Slot 3 → excludes AMS-HT, but AMS 0 and AMS 1 both remain → ambiguous
  1732. h2d_client._process_message(_ams_payload(3))
  1733. assert h2d_client.state.tray_now == 3 # raw slot fallback
  1734. # ---------------------------------------------------------------------------
  1735. # 6b. H2D last_loaded_tray validation
  1736. # ---------------------------------------------------------------------------
  1737. class TestLastLoadedTrayValidation(_H2DFixtureMixin):
  1738. """last_loaded_tray only stores physically valid tray IDs."""
  1739. def test_regular_ams_tray_stored(self, h2d_client):
  1740. """Valid regular AMS tray (0-15) → stored in last_loaded_tray."""
  1741. h2d_client.state.tray_now = 7
  1742. # Trigger tray_now processing via AMS message
  1743. h2d_client._process_message(
  1744. _extruder_info_payload(
  1745. [
  1746. {"id": 0, "snow": 1 << 8 | 3}, # AMS 1 slot 3 → global 7
  1747. {"id": 1, "snow": 0xFF00FF},
  1748. ]
  1749. )
  1750. )
  1751. h2d_client._process_message(_ams_payload(3))
  1752. assert h2d_client.state.tray_now == 7
  1753. assert h2d_client.state.last_loaded_tray == 7
  1754. def test_ams_ht_tray_stored(self, h2d_client):
  1755. """Valid AMS-HT tray (128-135) → stored in last_loaded_tray."""
  1756. h2d_client._process_message(_extruder_state_payload(0x0100))
  1757. h2d_client._process_message(
  1758. _extruder_info_payload(
  1759. [
  1760. {"id": 0, "snow": 0xFF00FF},
  1761. {"id": 1, "snow": 128 << 8 | 0},
  1762. ]
  1763. )
  1764. )
  1765. h2d_client._process_message(_ams_payload(0))
  1766. assert h2d_client.state.tray_now == 128
  1767. assert h2d_client.state.last_loaded_tray == 128
  1768. def test_unloaded_not_stored(self, h2d_client):
  1769. """tray_now=255 (unloaded) → last_loaded_tray unchanged."""
  1770. h2d_client.state.last_loaded_tray = 5
  1771. h2d_client._process_message(_ams_payload(255))
  1772. assert h2d_client.state.tray_now == 255
  1773. assert h2d_client.state.last_loaded_tray == 5
  1774. # ---------------------------------------------------------------------------
  1775. # 7. H2D Active extruder switching
  1776. # ---------------------------------------------------------------------------
  1777. class TestTrayNowDualNozzleH2DActiveExtruder(_H2DFixtureMixin):
  1778. """Active extruder switching via device.extruder.state bit 8."""
  1779. def test_active_extruder_right_by_default(self, h2d_client):
  1780. """Initial state.active_extruder == 0 (right)."""
  1781. assert h2d_client.state.active_extruder == 0
  1782. def test_extruder_state_bit8_switches_to_left(self, h2d_client):
  1783. """state=0x100 → active_extruder=1 (left)."""
  1784. h2d_client._process_message(_extruder_state_payload(0x0100))
  1785. assert h2d_client.state.active_extruder == 1
  1786. def test_extruder_state_bit8_switches_back_to_right(self, h2d_client):
  1787. """Cycle 0 → 1 → 0."""
  1788. h2d_client._process_message(_extruder_state_payload(0x0100))
  1789. assert h2d_client.state.active_extruder == 1
  1790. h2d_client._process_message(_extruder_state_payload(0x0001))
  1791. assert h2d_client.state.active_extruder == 0
  1792. def test_extruder_switch_changes_tray_disambiguation(self, h2d_client):
  1793. """Snow on both extruders; switching active changes which snow is used."""
  1794. # Snow: ext 0 → AMS 0 slot 1 (global 1), ext 1 → AMS 128 slot 0 (global 128)
  1795. h2d_client._process_message(
  1796. _extruder_info_payload(
  1797. [
  1798. {"id": 0, "snow": 0 << 8 | 1}, # AMS 0 slot 1 → global 1
  1799. {"id": 1, "snow": 128 << 8 | 0}, # AMS HT → global 128
  1800. ]
  1801. )
  1802. )
  1803. # Right active (default) — tray_now="1" → snow ext[0] says global 1
  1804. h2d_client._process_message(_ams_payload(1))
  1805. assert h2d_client.state.tray_now == 1
  1806. # Switch to left
  1807. h2d_client._process_message(_extruder_state_payload(0x0100))
  1808. # Left active — tray_now="0" → snow ext[1] says AMS HT (128), slot 0 matches
  1809. h2d_client._process_message(_ams_payload(0))
  1810. assert h2d_client.state.tray_now == 128
  1811. # ---------------------------------------------------------------------------
  1812. # 8. H2D Full multi-message sequences
  1813. # ---------------------------------------------------------------------------
  1814. class TestTrayNowDualNozzleH2DFullSequence(_H2DFixtureMixin):
  1815. """Multi-message sequences simulating real H2D Pro prints."""
  1816. def test_h2d_right_nozzle_ams0_lifecycle(self, h2d_client):
  1817. """Setup → load AMS 0 slot 1 → verify tray_now=1."""
  1818. # Snow update: extruder 0 loading AMS 0 slot 1
  1819. h2d_client._process_message(
  1820. _extruder_info_payload(
  1821. [
  1822. {"id": 0, "snow": 0 << 8 | 1},
  1823. {"id": 1, "snow": 0xFF00FF},
  1824. ]
  1825. )
  1826. )
  1827. # Printer reports tray_now="1"
  1828. h2d_client._process_message(_ams_payload(1))
  1829. assert h2d_client.state.tray_now == 1
  1830. assert h2d_client.state.last_loaded_tray == 1
  1831. def test_h2d_left_nozzle_ams_ht_lifecycle(self, h2d_client):
  1832. """Setup → switch left → load AMS HT → verify tray_now=128."""
  1833. # Switch to left extruder
  1834. h2d_client._process_message(_extruder_state_payload(0x0100))
  1835. # Snow: ext 1 → AMS HT slot 0
  1836. h2d_client._process_message(
  1837. _extruder_info_payload(
  1838. [
  1839. {"id": 0, "snow": 0xFF00FF},
  1840. {"id": 1, "snow": 128 << 8 | 0},
  1841. ]
  1842. )
  1843. )
  1844. # Printer reports tray_now="0" (AMS HT single slot)
  1845. h2d_client._process_message(_ams_payload(0))
  1846. assert h2d_client.state.tray_now == 128
  1847. assert h2d_client.state.last_loaded_tray == 128
  1848. def test_h2d_multi_color_alternating_nozzles(self, h2d_client):
  1849. """Multi-color print alternating between right and left nozzles.
  1850. Sequence:
  1851. 1. Right loads AMS 0 slot 0 (tray=0)
  1852. 2. Switch left, load AMS HT (tray=128)
  1853. 3. Switch right, snow updates, load AMS 0 slot 2 (tray=2)
  1854. 4. Unload (255)
  1855. """
  1856. # Step 1: Right extruder loads AMS 0 slot 0
  1857. h2d_client._process_message(
  1858. _extruder_info_payload(
  1859. [
  1860. {"id": 0, "snow": 0 << 8 | 0},
  1861. {"id": 1, "snow": 0xFF00FF},
  1862. ]
  1863. )
  1864. )
  1865. h2d_client._process_message(_ams_payload(0))
  1866. assert h2d_client.state.tray_now == 0
  1867. # Step 2: Switch to left, load AMS HT
  1868. h2d_client._process_message(_extruder_state_payload(0x0100))
  1869. h2d_client._process_message(
  1870. _extruder_info_payload(
  1871. [
  1872. {"id": 0, "snow": 0 << 8 | 0},
  1873. {"id": 1, "snow": 128 << 8 | 0},
  1874. ]
  1875. )
  1876. )
  1877. h2d_client._process_message(_ams_payload(0))
  1878. assert h2d_client.state.tray_now == 128
  1879. # Step 3: Switch back to right, load AMS 0 slot 2
  1880. h2d_client._process_message(_extruder_state_payload(0x0001))
  1881. h2d_client._process_message(
  1882. _extruder_info_payload(
  1883. [
  1884. {"id": 0, "snow": 0 << 8 | 2},
  1885. {"id": 1, "snow": 128 << 8 | 0},
  1886. ]
  1887. )
  1888. )
  1889. h2d_client._process_message(_ams_payload(2))
  1890. assert h2d_client.state.tray_now == 2
  1891. # Step 4: Unload
  1892. h2d_client._process_message(_ams_payload(255))
  1893. assert h2d_client.state.tray_now == 255
  1894. assert h2d_client.state.last_loaded_tray == 2
  1895. class TestTrayChangeLog:
  1896. """Tests for tray_change_log tracking during prints (mid-print tray switch)."""
  1897. @pytest.fixture
  1898. def mqtt_client(self):
  1899. """Create a BambuMQTTClient instance for testing."""
  1900. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1901. client = BambuMQTTClient(
  1902. ip_address="192.168.1.100",
  1903. serial_number="TRAYLOG1",
  1904. access_code="12345678",
  1905. )
  1906. return client
  1907. def test_tray_change_log_defaults_empty(self, mqtt_client):
  1908. """tray_change_log starts as an empty list."""
  1909. assert mqtt_client.state.tray_change_log == []
  1910. def test_tray_change_log_seeded_on_print_start(self, mqtt_client):
  1911. """Print start clears log and seeds with initial tray at layer 0."""
  1912. mqtt_client.state.tray_now = 2
  1913. mqtt_client.state.last_loaded_tray = 2
  1914. mqtt_client._previous_gcode_state = "IDLE"
  1915. # Transition to RUNNING via _process_message
  1916. mqtt_client._process_message(
  1917. {
  1918. "print": {
  1919. "gcode_state": "RUNNING",
  1920. "gcode_file": "test.3mf",
  1921. }
  1922. }
  1923. )
  1924. assert mqtt_client.state.tray_change_log == [(2, 0)]
  1925. def test_tray_change_log_cleared_on_new_print(self, mqtt_client):
  1926. """Old log entries are cleared when a new print starts."""
  1927. mqtt_client.state.tray_change_log = [(5, 0), (3, 100)]
  1928. mqtt_client.state.tray_now = 1
  1929. mqtt_client.state.last_loaded_tray = 1
  1930. mqtt_client._previous_gcode_state = "IDLE"
  1931. mqtt_client._process_message(
  1932. {
  1933. "print": {
  1934. "gcode_state": "RUNNING",
  1935. "gcode_file": "new.3mf",
  1936. }
  1937. }
  1938. )
  1939. assert mqtt_client.state.tray_change_log == [(1, 0)]
  1940. def test_tray_change_recorded_during_running(self, mqtt_client):
  1941. """Tray change while RUNNING is appended to the log."""
  1942. mqtt_client.state.state = "RUNNING"
  1943. mqtt_client.state.layer_num = 50
  1944. mqtt_client.state.last_loaded_tray = 0
  1945. mqtt_client.state.tray_change_log = [(0, 0)]
  1946. # Simulate tray_now update via AMS data
  1947. mqtt_client.state.tray_now = 1
  1948. # Trigger the tracking code path
  1949. tn = mqtt_client.state.tray_now
  1950. if tn != mqtt_client.state.last_loaded_tray and mqtt_client.state.state in ("RUNNING", "PAUSE"):
  1951. mqtt_client.state.tray_change_log.append((tn, mqtt_client.state.layer_num))
  1952. mqtt_client.state.last_loaded_tray = tn
  1953. assert mqtt_client.state.tray_change_log == [(0, 0), (1, 50)]
  1954. def test_tray_change_not_recorded_when_idle(self, mqtt_client):
  1955. """Tray changes while IDLE are NOT logged."""
  1956. mqtt_client.state.state = "IDLE"
  1957. mqtt_client.state.layer_num = 0
  1958. mqtt_client.state.last_loaded_tray = 0
  1959. mqtt_client.state.tray_change_log = []
  1960. mqtt_client.state.tray_now = 3
  1961. tn = mqtt_client.state.tray_now
  1962. if tn != mqtt_client.state.last_loaded_tray and mqtt_client.state.state in ("RUNNING", "PAUSE"):
  1963. mqtt_client.state.tray_change_log.append((tn, mqtt_client.state.layer_num))
  1964. mqtt_client.state.last_loaded_tray = tn
  1965. assert mqtt_client.state.tray_change_log == []
  1966. def test_tray_change_recorded_during_pause(self, mqtt_client):
  1967. """Tray change while PAUSE is also logged (AMS can swap during pause)."""
  1968. mqtt_client.state.state = "PAUSE"
  1969. mqtt_client.state.layer_num = 75
  1970. mqtt_client.state.last_loaded_tray = 2
  1971. mqtt_client.state.tray_change_log = [(2, 0)]
  1972. mqtt_client.state.tray_now = 5
  1973. tn = mqtt_client.state.tray_now
  1974. if tn != mqtt_client.state.last_loaded_tray and mqtt_client.state.state in ("RUNNING", "PAUSE"):
  1975. mqtt_client.state.tray_change_log.append((tn, mqtt_client.state.layer_num))
  1976. mqtt_client.state.last_loaded_tray = tn
  1977. assert mqtt_client.state.tray_change_log == [(2, 0), (5, 75)]
  1978. def test_same_tray_not_logged_twice(self, mqtt_client):
  1979. """Same tray value doesn't create duplicate log entries."""
  1980. mqtt_client.state.state = "RUNNING"
  1981. mqtt_client.state.layer_num = 30
  1982. mqtt_client.state.last_loaded_tray = 2
  1983. mqtt_client.state.tray_change_log = [(2, 0)]
  1984. # Same tray again
  1985. mqtt_client.state.tray_now = 2
  1986. tn = mqtt_client.state.tray_now
  1987. if tn != mqtt_client.state.last_loaded_tray and mqtt_client.state.state in ("RUNNING", "PAUSE"):
  1988. mqtt_client.state.tray_change_log.append((tn, mqtt_client.state.layer_num))
  1989. mqtt_client.state.last_loaded_tray = tn
  1990. assert mqtt_client.state.tray_change_log == [(2, 0)]
  1991. def test_multiple_tray_changes(self, mqtt_client):
  1992. """Multiple tray changes create a full history."""
  1993. mqtt_client.state.state = "RUNNING"
  1994. mqtt_client.state.last_loaded_tray = 0
  1995. mqtt_client.state.tray_change_log = [(0, 0)]
  1996. changes = [(1, 50), (3, 120), (0, 200)]
  1997. for tray, layer in changes:
  1998. mqtt_client.state.tray_now = tray
  1999. mqtt_client.state.layer_num = layer
  2000. tn = mqtt_client.state.tray_now
  2001. if tn != mqtt_client.state.last_loaded_tray and mqtt_client.state.state in ("RUNNING", "PAUSE"):
  2002. mqtt_client.state.tray_change_log.append((tn, mqtt_client.state.layer_num))
  2003. mqtt_client.state.last_loaded_tray = tn
  2004. assert mqtt_client.state.tray_change_log == [(0, 0), (1, 50), (3, 120), (0, 200)]
  2005. class TestDeveloperModeDetection:
  2006. """Tests for developer LAN mode detection from MQTT 'fun' field."""
  2007. @pytest.fixture
  2008. def mqtt_client(self):
  2009. """Create a BambuMQTTClient instance for testing."""
  2010. from backend.app.services.bambu_mqtt import BambuMQTTClient
  2011. client = BambuMQTTClient(
  2012. ip_address="192.168.1.100",
  2013. serial_number="TEST123",
  2014. access_code="12345678",
  2015. )
  2016. return client
  2017. def test_developer_mode_initially_none(self, mqtt_client):
  2018. """Verify developer_mode starts as None (unknown)."""
  2019. assert mqtt_client.state.developer_mode is None
  2020. def test_developer_mode_on_when_bit_clear(self, mqtt_client):
  2021. """Verify developer_mode is True when bit 0x20000000 is clear."""
  2022. # Bit 29 clear in lower 32 bits = developer mode ON
  2023. payload = {
  2024. "print": {
  2025. "gcode_state": "IDLE",
  2026. "fun": "1C8187FF9CFF",
  2027. }
  2028. }
  2029. mqtt_client._process_message(payload)
  2030. assert mqtt_client.state.developer_mode is True
  2031. def test_developer_mode_off_when_bit_set(self, mqtt_client):
  2032. """Verify developer_mode is False when bit 0x20000000 is set."""
  2033. # Bit 29 set in lower 32 bits = developer mode OFF (encryption required)
  2034. payload = {
  2035. "print": {
  2036. "gcode_state": "IDLE",
  2037. "fun": "1C81A7FF9CFF",
  2038. }
  2039. }
  2040. mqtt_client._process_message(payload)
  2041. assert mqtt_client.state.developer_mode is False
  2042. def test_developer_mode_exact_bit_check(self, mqtt_client):
  2043. """Verify only bit 0x20000000 matters, not other bits."""
  2044. # 0x20000000 in hex = bit 29. Set ONLY that bit.
  2045. payload = {
  2046. "print": {
  2047. "gcode_state": "IDLE",
  2048. "fun": "000020000000",
  2049. }
  2050. }
  2051. mqtt_client._process_message(payload)
  2052. assert mqtt_client.state.developer_mode is False
  2053. # All zeros = all bits clear = developer mode ON
  2054. payload["print"]["fun"] = "000000000000"
  2055. mqtt_client._process_message(payload)
  2056. assert mqtt_client.state.developer_mode is True
  2057. def test_developer_mode_invalid_fun_ignored(self, mqtt_client):
  2058. """Verify invalid fun values don't crash or change state."""
  2059. mqtt_client.state.developer_mode = True
  2060. payload = {
  2061. "print": {
  2062. "gcode_state": "IDLE",
  2063. "fun": "not_a_hex_value",
  2064. }
  2065. }
  2066. mqtt_client._process_message(payload)
  2067. # Should remain unchanged
  2068. assert mqtt_client.state.developer_mode is True
  2069. def test_developer_mode_missing_fun_preserves_state(self, mqtt_client):
  2070. """Verify messages without fun field don't reset developer_mode."""
  2071. mqtt_client.state.developer_mode = False
  2072. payload = {
  2073. "print": {
  2074. "gcode_state": "RUNNING",
  2075. "mc_percent": 50,
  2076. }
  2077. }
  2078. mqtt_client._process_message(payload)
  2079. assert mqtt_client.state.developer_mode is False
  2080. def test_developer_mode_persists_across_messages(self, mqtt_client):
  2081. """Verify developer_mode set by fun persists across messages without fun."""
  2082. # First message sets developer_mode
  2083. mqtt_client._process_message(
  2084. {
  2085. "print": {
  2086. "gcode_state": "IDLE",
  2087. "fun": "3EC1AFFF9CFF",
  2088. }
  2089. }
  2090. )
  2091. assert mqtt_client.state.developer_mode is False
  2092. # Subsequent messages without fun don't change it
  2093. for _ in range(3):
  2094. mqtt_client._process_message(
  2095. {
  2096. "print": {
  2097. "gcode_state": "RUNNING",
  2098. "mc_percent": 50,
  2099. }
  2100. }
  2101. )
  2102. assert mqtt_client.state.developer_mode is False
  2103. class TestSendDryingCommand:
  2104. """Tests for send_drying_command MQTT payload construction."""
  2105. @pytest.fixture
  2106. def mqtt_client(self):
  2107. """Create a BambuMQTTClient with a mock MQTT client."""
  2108. from unittest.mock import MagicMock
  2109. from backend.app.services.bambu_mqtt import BambuMQTTClient
  2110. client = BambuMQTTClient(
  2111. ip_address="192.168.1.100",
  2112. serial_number="TEST123",
  2113. access_code="12345678",
  2114. )
  2115. client._client = MagicMock()
  2116. return client
  2117. def test_rotate_tray_false_by_default(self, mqtt_client):
  2118. """Verify rotate_tray defaults to False in the MQTT payload."""
  2119. mqtt_client.send_drying_command(ams_id=0, temp=55, duration=4, mode=1, filament="PLA")
  2120. call_args = mqtt_client._client.publish.call_args
  2121. payload = json.loads(call_args[0][1])
  2122. assert payload["print"]["rotate_tray"] is False
  2123. def test_rotate_tray_true_when_enabled(self, mqtt_client):
  2124. """Verify rotate_tray is True when explicitly enabled."""
  2125. mqtt_client.send_drying_command(ams_id=0, temp=55, duration=4, mode=1, filament="PLA", rotate_tray=True)
  2126. call_args = mqtt_client._client.publish.call_args
  2127. payload = json.loads(call_args[0][1])
  2128. assert payload["print"]["rotate_tray"] is True
  2129. def test_rotate_tray_false_on_stop(self, mqtt_client):
  2130. """Verify rotate_tray is False when stopping drying (mode=0)."""
  2131. mqtt_client.send_drying_command(ams_id=0, temp=0, duration=0, mode=0)
  2132. call_args = mqtt_client._client.publish.call_args
  2133. payload = json.loads(call_args[0][1])
  2134. assert payload["print"]["rotate_tray"] is False
  2135. def test_all_required_fields_present(self, mqtt_client):
  2136. """Verify all required MQTT fields are present in the drying command."""
  2137. mqtt_client.send_drying_command(ams_id=128, temp=75, duration=8, mode=1, filament="ABS", rotate_tray=True)
  2138. call_args = mqtt_client._client.publish.call_args
  2139. payload = json.loads(call_args[0][1])
  2140. cmd = payload["print"]
  2141. assert cmd["command"] == "ams_filament_drying"
  2142. assert cmd["ams_id"] == 128
  2143. assert cmd["temp"] == 75
  2144. assert cmd["duration"] == 8
  2145. assert cmd["mode"] == 1
  2146. assert cmd["rotate_tray"] is True
  2147. assert cmd["filament"] == "ABS"
  2148. assert cmd["cooling_temp"] == 20
  2149. assert cmd["humidity"] == 0
  2150. assert cmd["close_power_conflict"] is False
  2151. assert "sequence_id" in cmd
  2152. def test_publishes_with_qos_1(self, mqtt_client):
  2153. """Verify drying commands are published with QoS 1."""
  2154. mqtt_client.send_drying_command(ams_id=0, temp=55, duration=4)
  2155. call_args = mqtt_client._client.publish.call_args
  2156. # qos may be positional arg [2] or keyword
  2157. qos = call_args.kwargs.get("qos", call_args[0][2] if len(call_args[0]) > 2 else None)
  2158. assert qos == 1