test_bambu_mqtt.py 182 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670
  1. """
  2. Tests for the BambuMQTTClient service.
  3. These tests focus on timelapse tracking during prints.
  4. """
  5. import json
  6. import time
  7. import pytest
  8. class TestTimelapseTracking:
  9. """Tests for timelapse state tracking during prints."""
  10. @pytest.fixture
  11. def mqtt_client(self):
  12. """Create a BambuMQTTClient instance for testing."""
  13. from backend.app.services.bambu_mqtt import BambuMQTTClient
  14. client = BambuMQTTClient(
  15. ip_address="192.168.1.100",
  16. serial_number="TEST123",
  17. access_code="12345678",
  18. )
  19. return client
  20. def test_timelapse_flag_initializes_to_false(self, mqtt_client):
  21. """Verify _timelapse_during_print starts as False."""
  22. assert mqtt_client._timelapse_during_print is False
  23. def test_timelapse_flag_set_when_timelapse_active_during_running(self, mqtt_client):
  24. """Verify timelapse flag is set when timelapse is active while printing."""
  25. # Simulate print running
  26. mqtt_client._was_running = True
  27. mqtt_client.state.timelapse = False
  28. # Simulate xcam data showing timelapse is enabled
  29. xcam_data = {"timelapse": "enable"}
  30. mqtt_client._parse_xcam_data(xcam_data)
  31. assert mqtt_client.state.timelapse is True
  32. assert mqtt_client._timelapse_during_print is True
  33. def test_timelapse_flag_not_set_when_not_running(self, mqtt_client):
  34. """Verify timelapse flag is NOT set when printer not running."""
  35. # Printer is idle (not running)
  36. mqtt_client._was_running = False
  37. mqtt_client.state.timelapse = False
  38. # Timelapse is enabled but we're not printing
  39. xcam_data = {"timelapse": "enable"}
  40. mqtt_client._parse_xcam_data(xcam_data)
  41. assert mqtt_client.state.timelapse is True
  42. # Flag should NOT be set since we're not printing
  43. assert mqtt_client._timelapse_during_print is False
  44. def test_timelapse_flag_persists_after_timelapse_stops(self, mqtt_client):
  45. """Verify timelapse flag stays True even after recording stops."""
  46. # Simulate print running with timelapse
  47. mqtt_client._was_running = True
  48. # Enable timelapse during print
  49. xcam_data = {"timelapse": "enable"}
  50. mqtt_client._parse_xcam_data(xcam_data)
  51. assert mqtt_client._timelapse_during_print is True
  52. # Disable timelapse (recording stops at end of print)
  53. xcam_data = {"timelapse": "disable"}
  54. mqtt_client._parse_xcam_data(xcam_data)
  55. # Flag should still be True (persists until reset)
  56. assert mqtt_client.state.timelapse is False
  57. assert mqtt_client._timelapse_during_print is True
  58. def test_timelapse_flag_from_print_data(self, mqtt_client):
  59. """Verify timelapse flag is set from print data (not just xcam)."""
  60. # Simulate print running
  61. mqtt_client._was_running = True
  62. mqtt_client.state.timelapse = False
  63. mqtt_client._timelapse_during_print = False
  64. # Manually test the timelapse parsing logic from _parse_print_data
  65. # This tests the "timelapse" field in the main print data
  66. data = {"timelapse": True}
  67. mqtt_client.state.timelapse = data["timelapse"] is True
  68. if mqtt_client.state.timelapse and mqtt_client._was_running:
  69. mqtt_client._timelapse_during_print = True
  70. assert mqtt_client._timelapse_during_print is True
  71. class TestPrintCompletionWithTimelapse:
  72. """Tests for print completion including timelapse flag."""
  73. @pytest.fixture
  74. def mqtt_client(self):
  75. """Create a BambuMQTTClient instance for testing."""
  76. from backend.app.services.bambu_mqtt import BambuMQTTClient
  77. client = BambuMQTTClient(
  78. ip_address="192.168.1.100",
  79. serial_number="TEST123",
  80. access_code="12345678",
  81. )
  82. return client
  83. def test_print_complete_includes_timelapse_flag(self, mqtt_client):
  84. """Verify print complete callback includes timelapse_was_active."""
  85. # Set up completion callback
  86. callback_data = {}
  87. def on_complete(data):
  88. callback_data.update(data)
  89. mqtt_client.on_print_complete = on_complete
  90. # Simulate a print that had timelapse active
  91. mqtt_client._was_running = True
  92. mqtt_client._completion_triggered = False
  93. mqtt_client._timelapse_during_print = True
  94. mqtt_client._previous_gcode_state = "RUNNING"
  95. mqtt_client._previous_gcode_file = "test.gcode"
  96. mqtt_client.state.subtask_name = "Test Print"
  97. # Simulate print finish
  98. mqtt_client.state.state = "FINISH"
  99. # Manually trigger the completion logic (simplified)
  100. # In real code this happens in _parse_print_data
  101. should_trigger = (
  102. mqtt_client.state.state in ("FINISH", "FAILED")
  103. and not mqtt_client._completion_triggered
  104. and mqtt_client.on_print_complete
  105. and mqtt_client._previous_gcode_state == "RUNNING"
  106. )
  107. if should_trigger:
  108. status = "completed" if mqtt_client.state.state == "FINISH" else "failed"
  109. timelapse_was_active = mqtt_client._timelapse_during_print
  110. mqtt_client._completion_triggered = True
  111. mqtt_client._was_running = False
  112. mqtt_client._timelapse_during_print = False
  113. mqtt_client.on_print_complete(
  114. {
  115. "status": status,
  116. "filename": mqtt_client._previous_gcode_file,
  117. "subtask_name": mqtt_client.state.subtask_name,
  118. "timelapse_was_active": timelapse_was_active,
  119. }
  120. )
  121. assert "timelapse_was_active" in callback_data
  122. assert callback_data["timelapse_was_active"] is True
  123. def test_print_complete_timelapse_flag_false_when_no_timelapse(self, mqtt_client):
  124. """Verify timelapse_was_active is False when no timelapse during print."""
  125. callback_data = {}
  126. def on_complete(data):
  127. callback_data.update(data)
  128. mqtt_client.on_print_complete = on_complete
  129. # Print without timelapse
  130. mqtt_client._was_running = True
  131. mqtt_client._completion_triggered = False
  132. mqtt_client._timelapse_during_print = False # No timelapse
  133. mqtt_client._previous_gcode_state = "RUNNING"
  134. mqtt_client._previous_gcode_file = "test.gcode"
  135. mqtt_client.state.subtask_name = "Test Print"
  136. mqtt_client.state.state = "FINISH"
  137. # Trigger completion
  138. timelapse_was_active = mqtt_client._timelapse_during_print
  139. mqtt_client.on_print_complete(
  140. {
  141. "status": "completed",
  142. "filename": mqtt_client._previous_gcode_file,
  143. "subtask_name": mqtt_client.state.subtask_name,
  144. "timelapse_was_active": timelapse_was_active,
  145. }
  146. )
  147. assert callback_data["timelapse_was_active"] is False
  148. def test_timelapse_flag_reset_after_completion(self, mqtt_client):
  149. """Verify _timelapse_during_print is reset after print completion."""
  150. mqtt_client._timelapse_during_print = True
  151. mqtt_client._was_running = True
  152. mqtt_client._completion_triggered = False
  153. # Simulate completion reset
  154. mqtt_client._completion_triggered = True
  155. mqtt_client._was_running = False
  156. mqtt_client._timelapse_during_print = False
  157. assert mqtt_client._timelapse_during_print is False
  158. class TestRealisticMessageFlow:
  159. """Tests that simulate realistic MQTT message sequences.
  160. These tests process messages through _process_message to test the full flow,
  161. including the order of xcam parsing vs state detection.
  162. """
  163. @pytest.fixture
  164. def mqtt_client(self):
  165. """Create a BambuMQTTClient instance for testing."""
  166. from backend.app.services.bambu_mqtt import BambuMQTTClient
  167. client = BambuMQTTClient(
  168. ip_address="192.168.1.100",
  169. serial_number="TEST123",
  170. access_code="12345678",
  171. )
  172. return client
  173. def test_timelapse_detected_at_print_start_in_same_message(self, mqtt_client):
  174. """Test that timelapse is detected when xcam and state come in same message.
  175. This is the critical race condition test - xcam data is parsed BEFORE
  176. state detection, so the timelapse flag must be set AFTER _was_running is True.
  177. """
  178. # Callbacks to track events
  179. start_callback_data = {}
  180. def on_start(data):
  181. start_callback_data.update(data)
  182. mqtt_client.on_print_start = on_start
  183. # Initial state - idle
  184. mqtt_client._was_running = False
  185. mqtt_client._timelapse_during_print = False
  186. mqtt_client._previous_gcode_state = None
  187. # Simulate first message when print starts - contains both xcam and gcode_state
  188. # This is the realistic scenario from the printer
  189. # NOTE: Real MQTT messages wrap print data inside a "print" key
  190. payload = {
  191. "print": {
  192. "gcode_state": "RUNNING",
  193. "gcode_file": "/data/Metadata/test_print.gcode",
  194. "subtask_name": "Test_Print",
  195. "xcam": {
  196. "timelapse": "enable", # Timelapse is enabled in this print
  197. "printing_monitor": True,
  198. },
  199. "mc_percent": 0,
  200. "mc_remaining_time": 3600,
  201. }
  202. }
  203. # Process the message (this is what happens in real MQTT flow)
  204. mqtt_client._process_message(payload)
  205. # Verify timelapse was detected even though xcam is parsed before state
  206. assert mqtt_client._was_running is True, "_was_running should be True after RUNNING state"
  207. assert mqtt_client.state.timelapse is True, "state.timelapse should be True"
  208. assert mqtt_client._timelapse_during_print is True, (
  209. "timelapse_during_print should be True when timelapse is in the same message as RUNNING state"
  210. )
  211. def test_timelapse_not_detected_when_disabled(self, mqtt_client):
  212. """Test that timelapse is NOT detected when disabled in xcam data."""
  213. mqtt_client.on_print_start = lambda data: None
  214. # Initial state - idle
  215. mqtt_client._was_running = False
  216. mqtt_client._timelapse_during_print = False
  217. mqtt_client._previous_gcode_state = None
  218. # Print starts without timelapse
  219. payload = {
  220. "print": {
  221. "gcode_state": "RUNNING",
  222. "gcode_file": "/data/Metadata/test_print.gcode",
  223. "subtask_name": "Test_Print",
  224. "xcam": {
  225. "timelapse": "disable", # Timelapse is disabled
  226. "printing_monitor": True,
  227. },
  228. }
  229. }
  230. mqtt_client._process_message(payload)
  231. assert mqtt_client._was_running is True
  232. assert mqtt_client.state.timelapse is False
  233. assert mqtt_client._timelapse_during_print is False
  234. def test_timelapse_detected_when_enabled_after_print_start(self, mqtt_client):
  235. """Test timelapse detected when enabled in a message after print starts."""
  236. mqtt_client.on_print_start = lambda data: None
  237. # First message - print starts without timelapse info
  238. payload_start = {
  239. "print": {
  240. "gcode_state": "RUNNING",
  241. "gcode_file": "/data/Metadata/test_print.gcode",
  242. "subtask_name": "Test_Print",
  243. }
  244. }
  245. mqtt_client._process_message(payload_start)
  246. assert mqtt_client._was_running is True
  247. assert mqtt_client._timelapse_during_print is False # Not detected yet
  248. # Second message - xcam data arrives with timelapse enabled
  249. payload_xcam = {
  250. "print": {
  251. "gcode_state": "RUNNING",
  252. "gcode_file": "/data/Metadata/test_print.gcode",
  253. "subtask_name": "Test_Print",
  254. "xcam": {
  255. "timelapse": "enable",
  256. },
  257. }
  258. }
  259. mqtt_client._process_message(payload_xcam)
  260. # Now timelapse should be detected because _was_running is already True
  261. assert mqtt_client._timelapse_during_print is True
  262. def test_print_complete_includes_timelapse_flag_full_flow(self, mqtt_client):
  263. """Test full print lifecycle with timelapse - from start to completion."""
  264. start_data = {}
  265. complete_data = {}
  266. def on_start(data):
  267. start_data.update(data)
  268. def on_complete(data):
  269. complete_data.update(data)
  270. mqtt_client.on_print_start = on_start
  271. mqtt_client.on_print_complete = on_complete
  272. # 1. Print starts with timelapse
  273. mqtt_client._process_message(
  274. {
  275. "print": {
  276. "gcode_state": "RUNNING",
  277. "gcode_file": "/data/Metadata/test.gcode",
  278. "subtask_name": "Test",
  279. "xcam": {"timelapse": "enable"},
  280. }
  281. }
  282. )
  283. assert mqtt_client._timelapse_during_print is True
  284. assert "subtask_name" in start_data
  285. # 2. Print continues (multiple messages)
  286. for _ in range(3):
  287. mqtt_client._process_message(
  288. {
  289. "print": {
  290. "gcode_state": "RUNNING",
  291. "gcode_file": "/data/Metadata/test.gcode",
  292. "subtask_name": "Test",
  293. "mc_percent": 50,
  294. }
  295. }
  296. )
  297. # Timelapse flag should still be True
  298. assert mqtt_client._timelapse_during_print is True
  299. # 3. Print completes
  300. mqtt_client._process_message(
  301. {
  302. "print": {
  303. "gcode_state": "FINISH",
  304. "gcode_file": "/data/Metadata/test.gcode",
  305. "subtask_name": "Test",
  306. }
  307. }
  308. )
  309. # Verify completion callback received timelapse flag
  310. assert "timelapse_was_active" in complete_data
  311. assert complete_data["timelapse_was_active"] is True
  312. assert complete_data["status"] == "completed"
  313. # Flags should be reset after completion
  314. assert mqtt_client._timelapse_during_print is False
  315. assert mqtt_client._was_running is False
  316. def test_print_failed_includes_timelapse_flag(self, mqtt_client):
  317. """Test that failed print also includes timelapse flag."""
  318. complete_data = {}
  319. def on_complete(data):
  320. complete_data.update(data)
  321. mqtt_client.on_print_start = lambda data: None
  322. mqtt_client.on_print_complete = on_complete
  323. # Start with timelapse
  324. mqtt_client._process_message(
  325. {
  326. "print": {
  327. "gcode_state": "RUNNING",
  328. "gcode_file": "/data/Metadata/test.gcode",
  329. "subtask_name": "Test",
  330. "xcam": {"timelapse": "enable"},
  331. }
  332. }
  333. )
  334. # Print fails
  335. mqtt_client._process_message(
  336. {
  337. "print": {
  338. "gcode_state": "FAILED",
  339. "gcode_file": "/data/Metadata/test.gcode",
  340. "subtask_name": "Test",
  341. }
  342. }
  343. )
  344. assert complete_data["timelapse_was_active"] is True
  345. assert complete_data["status"] == "failed"
  346. class TestPrePrintFailureCompletion:
  347. """Tests for completion detection when the print errors before reaching RUNNING (#1111).
  348. Common trigger: a file sliced for the wrong nozzle diameter is dispatched. The
  349. printer transitions IDLE -> PREPARE -> FAILED without ever entering RUNNING, so
  350. the legacy completion detection (which required _previous_gcode_state == 'RUNNING'
  351. or _was_running == True) left the queue item stuck at 'printing' forever.
  352. """
  353. @pytest.fixture
  354. def mqtt_client(self):
  355. from backend.app.services.bambu_mqtt import BambuMQTTClient
  356. return BambuMQTTClient(
  357. ip_address="192.168.1.100",
  358. serial_number="TEST123",
  359. access_code="12345678",
  360. )
  361. def test_prepare_to_failed_triggers_completion(self, mqtt_client):
  362. """PREPARE -> FAILED must fire on_print_complete (wrong nozzle size etc.)."""
  363. complete_data = {}
  364. mqtt_client.on_print_start = lambda data: None
  365. mqtt_client.on_print_complete = lambda data: complete_data.update(data)
  366. mqtt_client._previous_gcode_state = "PREPARE"
  367. mqtt_client._was_running = False
  368. mqtt_client._completion_triggered = False
  369. mqtt_client._process_message(
  370. {
  371. "print": {
  372. "gcode_state": "FAILED",
  373. "gcode_file": "/data/Metadata/plate_1.gcode",
  374. "subtask_name": "WrongNozzle",
  375. }
  376. }
  377. )
  378. assert complete_data.get("status") == "failed"
  379. def test_slicing_to_failed_triggers_completion(self, mqtt_client):
  380. """SLICING -> FAILED also treated as a pre-print failure."""
  381. complete_data = {}
  382. mqtt_client.on_print_start = lambda data: None
  383. mqtt_client.on_print_complete = lambda data: complete_data.update(data)
  384. mqtt_client._previous_gcode_state = "SLICING"
  385. mqtt_client._was_running = False
  386. mqtt_client._completion_triggered = False
  387. mqtt_client._process_message(
  388. {
  389. "print": {
  390. "gcode_state": "FAILED",
  391. "gcode_file": "/data/Metadata/plate_1.gcode",
  392. "subtask_name": "WrongNozzle",
  393. }
  394. }
  395. )
  396. assert complete_data.get("status") == "failed"
  397. def test_initial_failed_does_not_trigger_completion(self, mqtt_client):
  398. """First message arriving with FAILED (no prior state) must NOT fire completion.
  399. Protects against a stale FAILED on reconnect being mistaken for a fresh failure
  400. and marking an unrelated queue item as failed.
  401. """
  402. calls = []
  403. mqtt_client.on_print_start = lambda data: None
  404. mqtt_client.on_print_complete = lambda data: calls.append(data)
  405. assert mqtt_client._previous_gcode_state is None
  406. assert mqtt_client._was_running is False
  407. mqtt_client._process_message(
  408. {
  409. "print": {
  410. "gcode_state": "FAILED",
  411. "gcode_file": "/data/Metadata/plate_1.gcode",
  412. "subtask_name": "Stale",
  413. }
  414. }
  415. )
  416. assert calls == []
  417. def test_idle_to_failed_does_not_trigger_completion(self, mqtt_client):
  418. """IDLE -> FAILED (no print ever dispatched) must NOT fire completion."""
  419. calls = []
  420. mqtt_client.on_print_start = lambda data: None
  421. mqtt_client.on_print_complete = lambda data: calls.append(data)
  422. mqtt_client._previous_gcode_state = "IDLE"
  423. mqtt_client._was_running = False
  424. mqtt_client._completion_triggered = False
  425. mqtt_client._process_message(
  426. {
  427. "print": {
  428. "gcode_state": "FAILED",
  429. "subtask_name": "Stale",
  430. }
  431. }
  432. )
  433. assert calls == []
  434. def test_prepare_to_failed_includes_hms_errors_in_callback(self, mqtt_client):
  435. """Pre-print FAILED callback should carry the current HMS error list so the
  436. queue handler can populate a meaningful error_message."""
  437. complete_data = {}
  438. mqtt_client.on_print_start = lambda data: None
  439. mqtt_client.on_print_complete = lambda data: complete_data.update(data)
  440. mqtt_client._previous_gcode_state = "PREPARE"
  441. mqtt_client._was_running = False
  442. # Message carries HMS data for a nozzle-size mismatch (0500_4038) and the
  443. # PREPARE -> FAILED gcode_state transition in a single update.
  444. mqtt_client._process_message(
  445. {
  446. "print": {
  447. "gcode_state": "FAILED",
  448. "gcode_file": "/data/Metadata/plate_1.gcode",
  449. "hms": [{"attr": 0x05000000, "code": 0x4038}],
  450. }
  451. }
  452. )
  453. assert complete_data.get("status") == "failed"
  454. errs = complete_data.get("hms_errors") or []
  455. assert any(e.get("code") == "0x4038" for e in errs)
  456. class TestAMSDataMerging:
  457. """Tests for AMS data merging, particularly handling empty slots."""
  458. @pytest.fixture
  459. def mqtt_client(self):
  460. """Create a BambuMQTTClient instance for testing."""
  461. from backend.app.services.bambu_mqtt import BambuMQTTClient
  462. client = BambuMQTTClient(
  463. ip_address="192.168.1.100",
  464. serial_number="TEST123",
  465. access_code="12345678",
  466. )
  467. return client
  468. def test_empty_slot_clears_tray_type(self, mqtt_client):
  469. """Test that empty slot update clears tray_type (Issue #147).
  470. When a spool is removed from an old AMS, the printer sends empty values.
  471. These must overwrite the previous values to show the slot as empty.
  472. """
  473. # Initial state: AMS unit with a loaded spool
  474. initial_ams = {
  475. "ams": [
  476. {
  477. "id": 0,
  478. "tray": [
  479. {
  480. "id": 0,
  481. "tray_type": "PLA",
  482. "tray_sub_brands": "Bambu PLA Basic",
  483. "tray_color": "FF0000",
  484. "tag_uid": "1234567890ABCDEF",
  485. "remain": 80,
  486. }
  487. ],
  488. }
  489. ]
  490. }
  491. mqtt_client._handle_ams_data(initial_ams)
  492. # Verify initial state
  493. ams_data = mqtt_client.state.raw_data.get("ams", [])
  494. assert len(ams_data) == 1
  495. tray = ams_data[0]["tray"][0]
  496. assert tray["tray_type"] == "PLA"
  497. assert tray["tray_color"] == "FF0000"
  498. # Now simulate spool removal - printer sends empty values
  499. empty_update = {
  500. "ams": [
  501. {
  502. "id": 0,
  503. "tray": [
  504. {
  505. "id": 0,
  506. "tray_type": "", # Empty = slot is empty
  507. "tray_sub_brands": "",
  508. "tray_color": "",
  509. "tag_uid": "0000000000000000", # Zero UID
  510. "remain": 0,
  511. }
  512. ],
  513. }
  514. ]
  515. }
  516. mqtt_client._handle_ams_data(empty_update)
  517. # Verify empty values were applied (not ignored by merge logic)
  518. ams_data = mqtt_client.state.raw_data.get("ams", [])
  519. tray = ams_data[0]["tray"][0]
  520. assert tray["tray_type"] == "", "tray_type should be cleared when slot is empty"
  521. assert tray["tray_color"] == "", "tray_color should be cleared when slot is empty"
  522. assert tray["tray_sub_brands"] == "", "tray_sub_brands should be cleared"
  523. assert tray["tag_uid"] == "0000000000000000", "tag_uid should be cleared"
  524. def test_partial_update_preserves_other_fields(self, mqtt_client):
  525. """Test that partial updates still preserve non-slot-status fields."""
  526. # Initial state with full data
  527. initial_ams = {
  528. "ams": [
  529. {
  530. "id": 0,
  531. "humidity": "3",
  532. "temp": "25.5",
  533. "tray": [
  534. {
  535. "id": 0,
  536. "tray_type": "PLA",
  537. "tray_color": "00FF00",
  538. "remain": 90,
  539. "k": 0.02,
  540. }
  541. ],
  542. }
  543. ]
  544. }
  545. mqtt_client._handle_ams_data(initial_ams)
  546. # Partial update - only remain changes
  547. partial_update = {
  548. "ams": [
  549. {
  550. "id": 0,
  551. "tray": [
  552. {
  553. "id": 0,
  554. "remain": 85, # Only this changed
  555. }
  556. ],
  557. }
  558. ]
  559. }
  560. mqtt_client._handle_ams_data(partial_update)
  561. # Verify remain was updated but other fields preserved
  562. ams_data = mqtt_client.state.raw_data.get("ams", [])
  563. tray = ams_data[0]["tray"][0]
  564. assert tray["remain"] == 85, "remain should be updated"
  565. assert tray["tray_type"] == "PLA", "tray_type should be preserved"
  566. assert tray["tray_color"] == "00FF00", "tray_color should be preserved"
  567. assert tray["k"] == 0.02, "k should be preserved"
  568. def test_tray_exist_bits_clears_empty_slots(self, mqtt_client):
  569. """Test that tray_exist_bits clears slots marked as empty (Issue #147).
  570. New AMS models (AMS 2 Pro) don't send empty tray data when a spool is removed.
  571. Instead, they update tray_exist_bits to indicate which slots have spools.
  572. """
  573. # Initial state: AMS 0 and AMS 1 with loaded spools
  574. initial_ams = {
  575. "ams": [
  576. {
  577. "id": 0,
  578. "tray": [
  579. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000", "remain": 80},
  580. {"id": 1, "tray_type": "PETG", "tray_color": "00FF00", "remain": 60},
  581. {"id": 2, "tray_type": "ABS", "tray_color": "0000FF", "remain": 40},
  582. {"id": 3, "tray_type": "TPU", "tray_color": "FFFF00", "remain": 20},
  583. ],
  584. },
  585. {
  586. "id": 1,
  587. "tray": [
  588. {"id": 0, "tray_type": "PLA", "tray_color": "FFFFFF", "remain": 90},
  589. {"id": 1, "tray_type": "PLA", "tray_color": "000000", "remain": 70},
  590. {"id": 2, "tray_type": "PLA", "tray_color": "FF00FF", "remain": 50},
  591. {"id": 3, "tray_type": "PLA", "tray_color": "00FFFF", "remain": 30},
  592. ],
  593. },
  594. ],
  595. "tray_exist_bits": "ff", # All 8 slots have spools (0xFF = 11111111)
  596. }
  597. mqtt_client._handle_ams_data(initial_ams)
  598. # Verify initial state
  599. ams_data = mqtt_client.state.raw_data.get("ams", [])
  600. assert ams_data[1]["tray"][3]["tray_type"] == "PLA" # AMS 1 slot 3 (B4) has spool
  601. # Now simulate spool removal from AMS 1 slot 3 (B4)
  602. # tray_exist_bits: 0x7f = 01111111 (bit 7 = 0 means AMS 1 slot 3 is empty)
  603. update_ams = {
  604. "ams": [
  605. {"id": 0, "tray": [{"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}]},
  606. {"id": 1, "tray": [{"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}]},
  607. ],
  608. "tray_exist_bits": "7f", # Bit 7 = 0 -> AMS 1 slot 3 is empty
  609. }
  610. mqtt_client._handle_ams_data(update_ams)
  611. # Verify AMS 1 slot 3 was cleared
  612. ams_data = mqtt_client.state.raw_data.get("ams", [])
  613. b4_tray = ams_data[1]["tray"][3]
  614. assert b4_tray["tray_type"] == "", "tray_type should be cleared for empty slot"
  615. assert b4_tray["remain"] == 0, "remain should be 0 for empty slot"
  616. # Verify other slots are preserved
  617. assert ams_data[0]["tray"][0]["tray_type"] == "PLA", "A1 should still have PLA"
  618. assert ams_data[1]["tray"][0]["tray_type"] == "PLA", "B1 should still have PLA"
  619. def test_shutdown_message_preserves_ams_data(self, mqtt_client):
  620. """Printer shutdown (power_on_flag=False) must not wipe AMS slot data (#765).
  621. When a printer shuts down it sends a final MQTT message with
  622. tray_exist_bits='0' and power_on_flag=False. This all-zero value
  623. previously caused every slot to be cleared, which then triggered
  624. auto-unlink of all spool assignments on reconnect.
  625. """
  626. # Initial state: two AMS units with loaded spools
  627. initial_ams = {
  628. "ams": [
  629. {
  630. "id": 0,
  631. "tray": [
  632. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000FF", "remain": 80},
  633. {"id": 1, "tray_type": "PETG", "tray_color": "00FF00FF", "remain": 60},
  634. ],
  635. },
  636. {
  637. "id": 1,
  638. "tray": [
  639. {"id": 0, "tray_type": "PETG", "tray_color": "DBDDD9FF", "remain": 90},
  640. {"id": 1, "tray_type": "PETG", "tray_color": "67DB25FF", "remain": 70},
  641. ],
  642. },
  643. ],
  644. "tray_exist_bits": "33", # Slots 0,1 of each AMS (0b00110011)
  645. "power_on_flag": True,
  646. }
  647. mqtt_client._handle_ams_data(initial_ams)
  648. # Verify initial state
  649. ams_data = mqtt_client.state.raw_data["ams"]
  650. assert ams_data[0]["tray"][0]["tray_type"] == "PLA"
  651. assert ams_data[1]["tray"][0]["tray_type"] == "PETG"
  652. # Simulate printer shutdown — all-zero bits with power_on_flag=False
  653. shutdown_ams = {
  654. "ams_exist_bits": "0",
  655. "tray_exist_bits": "0",
  656. "power_on_flag": False,
  657. "insert_flag": False,
  658. "tray_now": "0",
  659. "version": 0,
  660. }
  661. mqtt_client._handle_ams_data(shutdown_ams)
  662. # AMS slot data MUST be preserved — shutdown should not clear it
  663. ams_data = mqtt_client.state.raw_data["ams"]
  664. assert ams_data[0]["tray"][0]["tray_type"] == "PLA", "Shutdown must not clear AMS 0 slot 0"
  665. assert ams_data[0]["tray"][0]["tray_color"] == "FF0000FF", "Shutdown must not clear AMS 0 slot 0 color"
  666. assert ams_data[0]["tray"][1]["tray_type"] == "PETG", "Shutdown must not clear AMS 0 slot 1"
  667. assert ams_data[1]["tray"][0]["tray_type"] == "PETG", "Shutdown must not clear AMS 1 slot 0"
  668. assert ams_data[1]["tray"][1]["tray_type"] == "PETG", "Shutdown must not clear AMS 1 slot 1"
  669. def test_genuine_removal_still_clears_with_power_on(self, mqtt_client):
  670. """Genuine spool removal (power_on_flag=True) must still clear slot data.
  671. Ensures the #765 fix doesn't break normal spool removal detection.
  672. """
  673. # Initial state: AMS with loaded spool
  674. initial_ams = {
  675. "ams": [
  676. {
  677. "id": 0,
  678. "tray": [
  679. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000", "remain": 80},
  680. {"id": 1, "tray_type": "PETG", "tray_color": "00FF00", "remain": 60},
  681. ],
  682. },
  683. ],
  684. "tray_exist_bits": "3", # Both slots occupied (0b11)
  685. "power_on_flag": True,
  686. }
  687. mqtt_client._handle_ams_data(initial_ams)
  688. # Spool removed from slot 1 while printer is running
  689. removal_ams = {
  690. "ams": [
  691. {
  692. "id": 0,
  693. "tray": [{"id": 0}, {"id": 1}],
  694. },
  695. ],
  696. "tray_exist_bits": "1", # Only slot 0 occupied (0b01)
  697. "power_on_flag": True,
  698. }
  699. mqtt_client._handle_ams_data(removal_ams)
  700. # Slot 0 preserved, slot 1 cleared
  701. ams_data = mqtt_client.state.raw_data["ams"]
  702. assert ams_data[0]["tray"][0]["tray_type"] == "PLA", "Slot 0 should be preserved"
  703. assert ams_data[0]["tray"][1]["tray_type"] == "", "Slot 1 should be cleared on removal"
  704. assert ams_data[0]["tray"][1]["tray_color"] == "", "Slot 1 color should be cleared"
  705. def test_power_on_flag_defaults_true_when_absent(self, mqtt_client):
  706. """When power_on_flag is not in the MQTT data, clearing must proceed normally.
  707. Ensures backwards compatibility with firmware that doesn't send power_on_flag.
  708. """
  709. # Initial state
  710. initial_ams = {
  711. "ams": [
  712. {
  713. "id": 0,
  714. "tray": [
  715. {"id": 0, "tray_type": "PLA", "tray_color": "FF0000", "remain": 80},
  716. ],
  717. },
  718. ],
  719. "tray_exist_bits": "1",
  720. }
  721. mqtt_client._handle_ams_data(initial_ams)
  722. # Update WITHOUT power_on_flag — should still clear when bit=0
  723. update_ams = {
  724. "ams": [{"id": 0, "tray": [{"id": 0}]}],
  725. "tray_exist_bits": "0",
  726. # No power_on_flag key at all
  727. }
  728. mqtt_client._handle_ams_data(update_ams)
  729. ams_data = mqtt_client.state.raw_data["ams"]
  730. assert ams_data[0]["tray"][0]["tray_type"] == "", (
  731. "Without power_on_flag, clearing should proceed (defaults to True)"
  732. )
  733. class TestAMSTrayStateClearning:
  734. """Tests for AMS tray state-based clearing (#784).
  735. Some printers (e.g. H2D) only send {id, state} in incremental MQTT
  736. updates when a tray is not fully loaded. state=11 means loaded;
  737. other values (9=empty, 10=spool present but filament not in feeder)
  738. should clear stale tray data that was set from an earlier pushall.
  739. """
  740. @pytest.fixture
  741. def mqtt_client(self):
  742. from backend.app.services.bambu_mqtt import BambuMQTTClient
  743. client = BambuMQTTClient(
  744. ip_address="192.168.1.100",
  745. serial_number="TEST_H2D",
  746. access_code="12345678",
  747. )
  748. return client
  749. def _seed_loaded_tray(self, mqtt_client):
  750. """Seed AMS 0 with a fully loaded tray (state=11) and an empty slot."""
  751. initial = {
  752. "ams": [
  753. {
  754. "id": 0,
  755. "tray": [
  756. {
  757. "id": 0,
  758. "tray_type": "PETG",
  759. "tray_sub_brands": "PETG HF",
  760. "tray_color": "00FF00FF",
  761. "tray_id_name": "A00-G1",
  762. "tray_info_idx": "GFG99",
  763. "tag_uid": "AABBCCDD11223344",
  764. "tray_uuid": "AABBCCDD11223344AABBCCDD11223344",
  765. "remain": 75,
  766. "k": 0.02,
  767. "cali_idx": 5,
  768. "state": 11,
  769. },
  770. {
  771. "id": 1,
  772. "tray_type": "PLA",
  773. "tray_color": "FF0000FF",
  774. "remain": 50,
  775. "state": 11,
  776. },
  777. ],
  778. }
  779. ],
  780. "power_on_flag": False, # H2D always sends False
  781. }
  782. mqtt_client._handle_ams_data(initial)
  783. ams = mqtt_client.state.raw_data["ams"]
  784. assert ams[0]["tray"][0]["tray_type"] == "PETG"
  785. assert ams[0]["tray"][1]["tray_type"] == "PLA"
  786. def test_state_10_clears_stale_tray_data(self, mqtt_client):
  787. """Incremental update with state=10 (spool present, not loaded) clears tray."""
  788. self._seed_loaded_tray(mqtt_client)
  789. # H2D sends only {id, state} when filament is retracted
  790. update = {
  791. "ams": [
  792. {
  793. "id": 0,
  794. "tray": [
  795. {"id": 0, "state": 10},
  796. {"id": 1, "state": 11}, # slot 1 still loaded
  797. ],
  798. }
  799. ],
  800. "power_on_flag": False,
  801. }
  802. mqtt_client._handle_ams_data(update)
  803. ams = mqtt_client.state.raw_data["ams"]
  804. tray0 = ams[0]["tray"][0]
  805. tray1 = ams[0]["tray"][1]
  806. # Tray 0 should be cleared
  807. assert tray0["tray_type"] == "", "tray_type must be cleared on state=10"
  808. assert tray0["tray_color"] == "", "tray_color must be cleared"
  809. assert tray0["tray_sub_brands"] == "", "tray_sub_brands must be cleared"
  810. assert tray0["tray_id_name"] == "", "tray_id_name must be cleared"
  811. assert tray0["tray_info_idx"] == "", "tray_info_idx must be cleared"
  812. assert tray0["tag_uid"] == "0000000000000000", "tag_uid must be cleared"
  813. assert tray0["tray_uuid"] == "00000000000000000000000000000000", "tray_uuid must be cleared"
  814. assert tray0["remain"] == 0, "remain must be 0"
  815. assert tray0["k"] is None, "k must be cleared"
  816. assert tray0["cali_idx"] is None, "cali_idx must be cleared"
  817. assert tray0["state"] == 10, "state should be preserved"
  818. # Tray 1 should be untouched
  819. assert tray1["tray_type"] == "PLA", "Loaded slot must be preserved"
  820. assert tray1["remain"] == 50
  821. def test_state_9_clears_stale_tray_data(self, mqtt_client):
  822. """Incremental update with state=9 (empty, no spool) clears tray."""
  823. self._seed_loaded_tray(mqtt_client)
  824. update = {
  825. "ams": [
  826. {
  827. "id": 0,
  828. "tray": [
  829. {"id": 0, "state": 9},
  830. {"id": 1, "state": 11},
  831. ],
  832. }
  833. ],
  834. "power_on_flag": False,
  835. }
  836. mqtt_client._handle_ams_data(update)
  837. tray0 = mqtt_client.state.raw_data["ams"][0]["tray"][0]
  838. assert tray0["tray_type"] == "", "state=9 must clear tray_type"
  839. assert tray0["remain"] == 0
  840. def test_state_11_preserves_tray_data(self, mqtt_client):
  841. """Incremental update with state=11 (loaded) must NOT clear tray."""
  842. self._seed_loaded_tray(mqtt_client)
  843. update = {
  844. "ams": [
  845. {
  846. "id": 0,
  847. "tray": [
  848. {"id": 0, "state": 11},
  849. {"id": 1, "state": 11},
  850. ],
  851. }
  852. ],
  853. "power_on_flag": False,
  854. }
  855. mqtt_client._handle_ams_data(update)
  856. tray0 = mqtt_client.state.raw_data["ams"][0]["tray"][0]
  857. assert tray0["tray_type"] == "PETG", "state=11 must preserve tray data"
  858. assert tray0["tray_color"] == "00FF00FF"
  859. assert tray0["remain"] == 75
  860. def test_no_clearing_when_tray_type_already_empty(self, mqtt_client):
  861. """Don't re-clear a tray that's already empty (avoids log spam)."""
  862. self._seed_loaded_tray(mqtt_client)
  863. # First unload clears
  864. update = {
  865. "ams": [{"id": 0, "tray": [{"id": 0, "state": 10}, {"id": 1, "state": 11}]}],
  866. "power_on_flag": False,
  867. }
  868. mqtt_client._handle_ams_data(update)
  869. assert mqtt_client.state.raw_data["ams"][0]["tray"][0]["tray_type"] == ""
  870. # Second identical update should not trigger clearing again
  871. # (merged_tray.get("tray_type") is already empty/falsy)
  872. mqtt_client._handle_ams_data(update)
  873. assert mqtt_client.state.raw_data["ams"][0]["tray"][0]["tray_type"] == ""
  874. def test_reload_after_unload_restores_data(self, mqtt_client):
  875. """After clearing via state=10, a full update with state=11 restores data."""
  876. self._seed_loaded_tray(mqtt_client)
  877. # Unload
  878. mqtt_client._handle_ams_data(
  879. {
  880. "ams": [{"id": 0, "tray": [{"id": 0, "state": 10}, {"id": 1, "state": 11}]}],
  881. "power_on_flag": False,
  882. }
  883. )
  884. assert mqtt_client.state.raw_data["ams"][0]["tray"][0]["tray_type"] == ""
  885. # Reload — full tray data arrives again
  886. mqtt_client._handle_ams_data(
  887. {
  888. "ams": [
  889. {
  890. "id": 0,
  891. "tray": [
  892. {
  893. "id": 0,
  894. "tray_type": "PETG",
  895. "tray_sub_brands": "PETG HF",
  896. "tray_color": "00FF00FF",
  897. "remain": 75,
  898. "state": 11,
  899. },
  900. {"id": 1, "state": 11},
  901. ],
  902. }
  903. ],
  904. "power_on_flag": False,
  905. }
  906. )
  907. tray0 = mqtt_client.state.raw_data["ams"][0]["tray"][0]
  908. assert tray0["tray_type"] == "PETG", "Reload must restore tray data"
  909. assert tray0["tray_color"] == "00FF00FF"
  910. assert tray0["remain"] == 75
  911. class TestNozzleRackData:
  912. """Tests for nozzle rack data parsing from H2 series device.nozzle.info."""
  913. @pytest.fixture
  914. def mqtt_client(self):
  915. """Create a BambuMQTTClient instance for testing."""
  916. from backend.app.services.bambu_mqtt import BambuMQTTClient
  917. client = BambuMQTTClient(
  918. ip_address="192.168.1.100",
  919. serial_number="TEST123",
  920. access_code="12345678",
  921. )
  922. return client
  923. def test_h2c_nozzle_rack_populated_with_8_entries(self, mqtt_client):
  924. """H2C provides 8 nozzle entries: IDs 0,1 (L/R hotend) + 16-21 (rack)."""
  925. payload = {
  926. "print": {
  927. "device": {
  928. "nozzle": {
  929. "info": [
  930. {
  931. "id": 0,
  932. "type": "HS",
  933. "diameter": "0.4",
  934. "wear": 5,
  935. "stat": 1,
  936. "max_temp": 300,
  937. "serial_number": "SN-L",
  938. },
  939. {
  940. "id": 1,
  941. "type": "HS",
  942. "diameter": "0.4",
  943. "wear": 3,
  944. "stat": 0,
  945. "max_temp": 300,
  946. "serial_number": "SN-R",
  947. },
  948. {
  949. "id": 16,
  950. "type": "HS",
  951. "diameter": "0.4",
  952. "wear": 10,
  953. "stat": 0,
  954. "max_temp": 300,
  955. "serial_number": "SN-16",
  956. },
  957. {
  958. "id": 17,
  959. "type": "HH01",
  960. "diameter": "0.6",
  961. "wear": 0,
  962. "stat": 0,
  963. "max_temp": 300,
  964. "serial_number": "SN-17",
  965. },
  966. {
  967. "id": 18,
  968. "type": "HS",
  969. "diameter": "0.4",
  970. "wear": 2,
  971. "stat": 0,
  972. "max_temp": 300,
  973. "serial_number": "SN-18",
  974. },
  975. {
  976. "id": 19,
  977. "type": "",
  978. "diameter": "",
  979. "wear": None,
  980. "stat": None,
  981. "max_temp": 0,
  982. "serial_number": "",
  983. },
  984. {
  985. "id": 20,
  986. "type": "",
  987. "diameter": "",
  988. "wear": None,
  989. "stat": None,
  990. "max_temp": 0,
  991. "serial_number": "",
  992. },
  993. {
  994. "id": 21,
  995. "type": "",
  996. "diameter": "",
  997. "wear": None,
  998. "stat": None,
  999. "max_temp": 0,
  1000. "serial_number": "",
  1001. },
  1002. ]
  1003. }
  1004. }
  1005. }
  1006. }
  1007. mqtt_client._process_message(payload)
  1008. assert len(mqtt_client.state.nozzle_rack) == 8
  1009. ids = [n["id"] for n in mqtt_client.state.nozzle_rack]
  1010. assert ids == [0, 1, 16, 17, 18, 19, 20, 21]
  1011. def test_h2d_nozzle_rack_populated_with_2_entries(self, mqtt_client):
  1012. """H2D provides 2 nozzle entries: IDs 0,1 (L/R hotend) — no rack slots."""
  1013. payload = {
  1014. "print": {
  1015. "device": {
  1016. "nozzle": {
  1017. "info": [
  1018. {
  1019. "id": 0,
  1020. "type": "HS",
  1021. "diameter": "0.4",
  1022. "wear": 5,
  1023. "stat": 1,
  1024. "max_temp": 300,
  1025. "serial_number": "SN-L",
  1026. },
  1027. {
  1028. "id": 1,
  1029. "type": "HS",
  1030. "diameter": "0.4",
  1031. "wear": 3,
  1032. "stat": 1,
  1033. "max_temp": 300,
  1034. "serial_number": "SN-R",
  1035. },
  1036. ]
  1037. }
  1038. }
  1039. }
  1040. }
  1041. mqtt_client._process_message(payload)
  1042. assert len(mqtt_client.state.nozzle_rack) == 2
  1043. ids = [n["id"] for n in mqtt_client.state.nozzle_rack]
  1044. assert ids == [0, 1]
  1045. def test_single_nozzle_h2s_populated(self, mqtt_client):
  1046. """H2S provides 1 nozzle entry: ID 0 only — single nozzle printer."""
  1047. payload = {
  1048. "print": {
  1049. "device": {
  1050. "nozzle": {
  1051. "info": [
  1052. {
  1053. "id": 0,
  1054. "type": "HS",
  1055. "diameter": "0.4",
  1056. "wear": 2,
  1057. "stat": 1,
  1058. "max_temp": 300,
  1059. "serial_number": "SN-0",
  1060. },
  1061. ]
  1062. }
  1063. }
  1064. }
  1065. }
  1066. mqtt_client._process_message(payload)
  1067. assert len(mqtt_client.state.nozzle_rack) == 1
  1068. assert mqtt_client.state.nozzle_rack[0]["id"] == 0
  1069. def test_empty_nozzle_info_does_not_populate_rack(self, mqtt_client):
  1070. """Empty nozzle info list should not populate nozzle_rack."""
  1071. payload = {"print": {"device": {"nozzle": {"info": []}}}}
  1072. mqtt_client._process_message(payload)
  1073. assert mqtt_client.state.nozzle_rack == []
  1074. def test_nozzle_rack_sorted_by_id(self, mqtt_client):
  1075. """Nozzle rack entries should be sorted by ID regardless of input order."""
  1076. payload = {
  1077. "print": {
  1078. "device": {
  1079. "nozzle": {
  1080. "info": [
  1081. {"id": 17, "type": "HS", "diameter": "0.6"},
  1082. {"id": 0, "type": "HS", "diameter": "0.4"},
  1083. {"id": 16, "type": "HS", "diameter": "0.4"},
  1084. {"id": 1, "type": "HS", "diameter": "0.4"},
  1085. ]
  1086. }
  1087. }
  1088. }
  1089. }
  1090. mqtt_client._process_message(payload)
  1091. ids = [n["id"] for n in mqtt_client.state.nozzle_rack]
  1092. assert ids == [0, 1, 16, 17]
  1093. def test_nozzle_rack_field_mapping(self, mqtt_client):
  1094. """Verify field mapping from MQTT nozzle_info to nozzle_rack dict keys."""
  1095. payload = {
  1096. "print": {
  1097. "device": {
  1098. "nozzle": {
  1099. "info": [
  1100. {
  1101. "id": 16,
  1102. "type": "HH01",
  1103. "diameter": "0.6",
  1104. "wear": 15,
  1105. "stat": 0,
  1106. "max_temp": 320,
  1107. "serial_number": "SN-ABC123",
  1108. "filament_colour": "FF8800",
  1109. "filament_id": "F42",
  1110. "tray_type": "ABS",
  1111. }
  1112. ]
  1113. }
  1114. }
  1115. }
  1116. }
  1117. mqtt_client._process_message(payload)
  1118. slot = mqtt_client.state.nozzle_rack[0]
  1119. assert slot["id"] == 16
  1120. assert slot["type"] == "HH01"
  1121. assert slot["diameter"] == "0.6"
  1122. assert slot["wear"] == 15
  1123. assert slot["stat"] == 0
  1124. assert slot["max_temp"] == 320
  1125. assert slot["serial_number"] == "SN-ABC123"
  1126. assert slot["filament_color"] == "FF8800"
  1127. assert slot["filament_id"] == "F42"
  1128. assert slot["filament_type"] == "ABS"
  1129. def test_nozzle_info_updates_nozzle_state(self, mqtt_client):
  1130. """Nozzle info for IDs 0,1 should also update nozzle state (type/diameter)."""
  1131. payload = {
  1132. "print": {
  1133. "device": {
  1134. "nozzle": {
  1135. "info": [
  1136. {"id": 0, "type": "HS", "diameter": "0.4"},
  1137. {"id": 1, "type": "HH01", "diameter": "0.6"},
  1138. ]
  1139. }
  1140. }
  1141. }
  1142. }
  1143. mqtt_client._process_message(payload)
  1144. assert mqtt_client.state.nozzles[0].nozzle_type == "HS"
  1145. assert mqtt_client.state.nozzles[0].nozzle_diameter == "0.4"
  1146. assert mqtt_client.state.nozzles[1].nozzle_type == "HH01"
  1147. assert mqtt_client.state.nozzles[1].nozzle_diameter == "0.6"
  1148. class TestRequestTopicFailSafe:
  1149. """Tests for graceful degradation when broker rejects request topic subscription."""
  1150. @pytest.fixture(autouse=True)
  1151. def clear_request_topic_cache(self):
  1152. """Clear class-level cache before each test to avoid cross-test pollution."""
  1153. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1154. BambuMQTTClient._request_topic_cache.clear()
  1155. @pytest.fixture
  1156. def mqtt_client(self):
  1157. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1158. client = BambuMQTTClient(
  1159. ip_address="192.168.1.100",
  1160. serial_number="TEST123",
  1161. access_code="12345678",
  1162. )
  1163. return client
  1164. def test_request_topic_supported_by_default(self, mqtt_client):
  1165. """Request topic subscription is attempted by default."""
  1166. assert mqtt_client._request_topic_supported is True
  1167. assert mqtt_client._request_topic_confirmed is False
  1168. def test_on_subscribe_confirms_success(self, mqtt_client):
  1169. """Successful SUBACK marks request topic as confirmed."""
  1170. from paho.mqtt.reasoncodes import ReasonCode
  1171. mqtt_client._request_topic_sub_mid = 42
  1172. rc = ReasonCode(9, identifier=0) # SUBACK packetType=9, QoS 0 = success
  1173. mqtt_client._on_subscribe(None, None, 42, [rc], None)
  1174. assert mqtt_client._request_topic_confirmed is True
  1175. assert mqtt_client._request_topic_supported is True
  1176. assert mqtt_client._request_topic_sub_mid is None
  1177. assert mqtt_client._request_topic_sub_time == 0.0
  1178. def test_on_subscribe_detects_rejection(self, mqtt_client):
  1179. """SUBACK with failure code disables request topic."""
  1180. from paho.mqtt.reasoncodes import ReasonCode
  1181. mqtt_client._request_topic_sub_mid = 42
  1182. rc = ReasonCode(9, identifier=0x80) # SUBACK packetType=9, 0x80 = failure
  1183. mqtt_client._on_subscribe(None, None, 42, [rc], None)
  1184. assert mqtt_client._request_topic_supported is False
  1185. assert mqtt_client._request_topic_confirmed is False
  1186. def test_on_subscribe_ignores_other_mids(self, mqtt_client):
  1187. """SUBACK for other subscriptions (e.g. report topic) is ignored."""
  1188. from paho.mqtt.reasoncodes import ReasonCode
  1189. mqtt_client._request_topic_sub_mid = 42
  1190. rc = ReasonCode(9, identifier=0x80)
  1191. mqtt_client._on_subscribe(None, None, 99, [rc], None)
  1192. # Not affected — mid doesn't match
  1193. assert mqtt_client._request_topic_supported is True
  1194. def test_disconnect_after_subscription_disables_topic(self, mqtt_client):
  1195. """Disconnect within 10s of subscription attempt disables request topic."""
  1196. import time
  1197. mqtt_client._request_topic_sub_time = time.time()
  1198. mqtt_client._request_topic_confirmed = False
  1199. mqtt_client._last_message_time = 0.0
  1200. mqtt_client._on_disconnect(None, None)
  1201. assert mqtt_client._request_topic_supported is False
  1202. assert mqtt_client._request_topic_sub_time == 0.0
  1203. def test_disconnect_after_confirmation_does_not_disable(self, mqtt_client):
  1204. """Disconnect after SUBACK confirmation keeps request topic enabled."""
  1205. import time
  1206. mqtt_client._request_topic_sub_time = time.time()
  1207. mqtt_client._request_topic_confirmed = True
  1208. mqtt_client._last_message_time = 0.0
  1209. mqtt_client._on_disconnect(None, None)
  1210. assert mqtt_client._request_topic_supported is True
  1211. def test_late_disconnect_does_not_disable(self, mqtt_client):
  1212. """Disconnect long after subscription (>10s) doesn't blame request topic."""
  1213. import time
  1214. mqtt_client._request_topic_sub_time = time.time() - 30.0
  1215. mqtt_client._request_topic_confirmed = False
  1216. mqtt_client._last_message_time = 0.0
  1217. mqtt_client._on_disconnect(None, None)
  1218. assert mqtt_client._request_topic_supported is True
  1219. def test_on_connect_skips_request_topic_when_unsupported(self, mqtt_client):
  1220. """After marking unsupported, reconnect skips request topic subscription."""
  1221. mqtt_client._request_topic_supported = False
  1222. subscribe_calls = []
  1223. mock_client = type(
  1224. "MockClient",
  1225. (),
  1226. {
  1227. "subscribe": lambda self, topic: subscribe_calls.append(topic) or (0, 1),
  1228. },
  1229. )()
  1230. mqtt_client._on_connect(mock_client, None, None, 0)
  1231. # Only report topic subscribed, not request topic
  1232. assert len(subscribe_calls) == 1
  1233. assert subscribe_calls[0] == mqtt_client.topic_subscribe
  1234. def test_cache_persists_across_instances(self):
  1235. """New client instance inherits request topic unsupported state from cache."""
  1236. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1237. client1 = BambuMQTTClient(
  1238. ip_address="192.168.1.100",
  1239. serial_number="TEST_CACHE",
  1240. access_code="12345678",
  1241. )
  1242. assert client1._request_topic_supported is True
  1243. # Simulate disconnect-after-subscribe disabling the topic
  1244. client1._request_topic_sub_time = __import__("time").time()
  1245. client1._request_topic_confirmed = False
  1246. client1._last_message_time = 0.0
  1247. client1._on_disconnect(None, None)
  1248. assert client1._request_topic_supported is False
  1249. # New instance for same serial should inherit the cached state
  1250. client2 = BambuMQTTClient(
  1251. ip_address="192.168.1.100",
  1252. serial_number="TEST_CACHE",
  1253. access_code="12345678",
  1254. )
  1255. assert client2._request_topic_supported is False
  1256. def test_cache_does_not_affect_different_serial(self):
  1257. """Cache is per-serial — different printer is unaffected."""
  1258. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1259. BambuMQTTClient._request_topic_cache["SERIAL_A"] = False
  1260. client = BambuMQTTClient(
  1261. ip_address="192.168.1.100",
  1262. serial_number="SERIAL_B",
  1263. access_code="12345678",
  1264. )
  1265. assert client._request_topic_supported is True
  1266. def test_cache_updated_on_suback_success(self):
  1267. """Successful SUBACK caches positive confirmation."""
  1268. from paho.mqtt.reasoncodes import ReasonCode
  1269. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1270. client = BambuMQTTClient(
  1271. ip_address="192.168.1.100",
  1272. serial_number="TEST_SUBACK",
  1273. access_code="12345678",
  1274. )
  1275. client._request_topic_sub_mid = 42
  1276. rc = ReasonCode(9, identifier=0) # Success
  1277. client._on_subscribe(None, None, 42, [rc], None)
  1278. assert BambuMQTTClient._request_topic_cache["TEST_SUBACK"] is True
  1279. def test_cache_updated_on_suback_rejection(self):
  1280. """SUBACK rejection caches negative state."""
  1281. from paho.mqtt.reasoncodes import ReasonCode
  1282. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1283. client = BambuMQTTClient(
  1284. ip_address="192.168.1.100",
  1285. serial_number="TEST_REJECT",
  1286. access_code="12345678",
  1287. )
  1288. client._request_topic_sub_mid = 42
  1289. rc = ReasonCode(9, identifier=0x80) # Failure
  1290. client._on_subscribe(None, None, 42, [rc], None)
  1291. assert BambuMQTTClient._request_topic_cache["TEST_REJECT"] is False
  1292. class TestRequestTopicAmsMapping:
  1293. """Tests for capturing ams_mapping from the MQTT request topic."""
  1294. @pytest.fixture
  1295. def mqtt_client(self):
  1296. """Create a BambuMQTTClient instance for testing."""
  1297. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1298. client = BambuMQTTClient(
  1299. ip_address="192.168.1.100",
  1300. serial_number="TEST123",
  1301. access_code="12345678",
  1302. )
  1303. return client
  1304. def test_captured_ams_mapping_initializes_to_none(self, mqtt_client):
  1305. """Verify _captured_ams_mapping starts as None."""
  1306. assert mqtt_client._captured_ams_mapping is None
  1307. def test_handle_request_message_captures_ams_mapping(self, mqtt_client):
  1308. """project_file command with ams_mapping stores the mapping."""
  1309. data = {
  1310. "print": {
  1311. "command": "project_file",
  1312. "ams_mapping": [0, 4, -1, -1],
  1313. "url": "ftp://192.168.1.100/test.3mf",
  1314. }
  1315. }
  1316. mqtt_client._handle_request_message(data)
  1317. assert mqtt_client._captured_ams_mapping == [0, 4, -1, -1]
  1318. def test_handle_request_message_ignores_non_print_commands(self, mqtt_client):
  1319. """Non-project_file commands don't store ams_mapping."""
  1320. data = {
  1321. "print": {
  1322. "command": "pause",
  1323. }
  1324. }
  1325. mqtt_client._handle_request_message(data)
  1326. assert mqtt_client._captured_ams_mapping is None
  1327. def test_handle_request_message_ignores_missing_ams_mapping(self, mqtt_client):
  1328. """project_file command without ams_mapping doesn't store anything."""
  1329. data = {
  1330. "print": {
  1331. "command": "project_file",
  1332. "url": "ftp://192.168.1.100/test.3mf",
  1333. }
  1334. }
  1335. mqtt_client._handle_request_message(data)
  1336. assert mqtt_client._captured_ams_mapping is None
  1337. def test_handle_request_message_ignores_non_dict_print(self, mqtt_client):
  1338. """Non-dict print value is safely ignored."""
  1339. data = {"print": "not_a_dict"}
  1340. mqtt_client._handle_request_message(data)
  1341. assert mqtt_client._captured_ams_mapping is None
  1342. def test_handle_request_message_ignores_missing_print(self, mqtt_client):
  1343. """Message without print key is safely ignored."""
  1344. data = {"pushing": {"command": "pushall"}}
  1345. mqtt_client._handle_request_message(data)
  1346. assert mqtt_client._captured_ams_mapping is None
  1347. def test_captured_mapping_overwrites_previous(self, mqtt_client):
  1348. """A new print command overwrites a previously captured mapping."""
  1349. mqtt_client._captured_ams_mapping = [0, -1, -1, -1]
  1350. data = {
  1351. "print": {
  1352. "command": "project_file",
  1353. "ams_mapping": [4, 8, -1, -1],
  1354. }
  1355. }
  1356. mqtt_client._handle_request_message(data)
  1357. assert mqtt_client._captured_ams_mapping == [4, 8, -1, -1]
  1358. def test_print_start_callback_includes_ams_mapping(self, mqtt_client):
  1359. """on_print_start callback data includes captured ams_mapping."""
  1360. start_data = {}
  1361. def on_start(data):
  1362. start_data.update(data)
  1363. mqtt_client.on_print_start = on_start
  1364. mqtt_client._captured_ams_mapping = [0, 4, -1, -1]
  1365. # Trigger print start
  1366. mqtt_client._process_message(
  1367. {
  1368. "print": {
  1369. "gcode_state": "RUNNING",
  1370. "gcode_file": "/data/Metadata/test.gcode",
  1371. "subtask_name": "Test",
  1372. }
  1373. }
  1374. )
  1375. assert start_data.get("ams_mapping") == [0, 4, -1, -1]
  1376. def test_print_start_callback_ams_mapping_none_when_not_captured(self, mqtt_client):
  1377. """on_print_start callback has ams_mapping=None when no mapping captured."""
  1378. start_data = {}
  1379. def on_start(data):
  1380. start_data.update(data)
  1381. mqtt_client.on_print_start = on_start
  1382. mqtt_client._process_message(
  1383. {
  1384. "print": {
  1385. "gcode_state": "RUNNING",
  1386. "gcode_file": "/data/Metadata/test.gcode",
  1387. "subtask_name": "Test",
  1388. }
  1389. }
  1390. )
  1391. assert "ams_mapping" in start_data
  1392. assert start_data["ams_mapping"] is None
  1393. def test_print_complete_callback_includes_ams_mapping(self, mqtt_client):
  1394. """on_print_complete callback data includes captured ams_mapping."""
  1395. complete_data = {}
  1396. def on_complete(data):
  1397. complete_data.update(data)
  1398. mqtt_client.on_print_start = lambda d: None
  1399. mqtt_client.on_print_complete = on_complete
  1400. mqtt_client._captured_ams_mapping = [0, 9, -1, -1]
  1401. # Start print
  1402. mqtt_client._process_message(
  1403. {
  1404. "print": {
  1405. "gcode_state": "RUNNING",
  1406. "gcode_file": "/data/Metadata/test.gcode",
  1407. "subtask_name": "Test",
  1408. }
  1409. }
  1410. )
  1411. # Complete print
  1412. mqtt_client._process_message(
  1413. {
  1414. "print": {
  1415. "gcode_state": "FINISH",
  1416. "gcode_file": "/data/Metadata/test.gcode",
  1417. "subtask_name": "Test",
  1418. }
  1419. }
  1420. )
  1421. assert complete_data.get("ams_mapping") == [0, 9, -1, -1]
  1422. def test_captured_mapping_cleared_after_print_complete(self, mqtt_client):
  1423. """_captured_ams_mapping is reset to None after print completion."""
  1424. mqtt_client.on_print_start = lambda d: None
  1425. mqtt_client.on_print_complete = lambda d: None
  1426. mqtt_client._captured_ams_mapping = [0, 4, -1, -1]
  1427. # Start print
  1428. mqtt_client._process_message(
  1429. {
  1430. "print": {
  1431. "gcode_state": "RUNNING",
  1432. "gcode_file": "/data/Metadata/test.gcode",
  1433. "subtask_name": "Test",
  1434. }
  1435. }
  1436. )
  1437. # Complete print
  1438. mqtt_client._process_message(
  1439. {
  1440. "print": {
  1441. "gcode_state": "FINISH",
  1442. "gcode_file": "/data/Metadata/test.gcode",
  1443. "subtask_name": "Test",
  1444. }
  1445. }
  1446. )
  1447. assert mqtt_client._captured_ams_mapping is None
  1448. def test_full_flow_capture_and_deliver(self, mqtt_client):
  1449. """Full flow: slicer sends print command → MQTT captures mapping → completion delivers it."""
  1450. complete_data = {}
  1451. def on_complete(data):
  1452. complete_data.update(data)
  1453. mqtt_client.on_print_start = lambda d: None
  1454. mqtt_client.on_print_complete = on_complete
  1455. # 1. Slicer sends print command (captured from request topic)
  1456. mqtt_client._handle_request_message(
  1457. {
  1458. "print": {
  1459. "command": "project_file",
  1460. "ams_mapping": [4, 9, -1, -1],
  1461. "url": "ftp://192.168.1.100/model.3mf",
  1462. }
  1463. }
  1464. )
  1465. assert mqtt_client._captured_ams_mapping == [4, 9, -1, -1]
  1466. # 2. Printer reports RUNNING
  1467. mqtt_client._process_message(
  1468. {
  1469. "print": {
  1470. "gcode_state": "RUNNING",
  1471. "gcode_file": "/data/Metadata/model.gcode",
  1472. "subtask_name": "Model",
  1473. }
  1474. }
  1475. )
  1476. # 3. Printer reports FINISH
  1477. mqtt_client._process_message(
  1478. {
  1479. "print": {
  1480. "gcode_state": "FINISH",
  1481. "gcode_file": "/data/Metadata/model.gcode",
  1482. "subtask_name": "Model",
  1483. }
  1484. }
  1485. )
  1486. assert complete_data["ams_mapping"] == [4, 9, -1, -1]
  1487. assert complete_data["status"] == "completed"
  1488. # Mapping cleared after completion
  1489. assert mqtt_client._captured_ams_mapping is None
  1490. # ---------------------------------------------------------------------------
  1491. # tray_now disambiguation helpers
  1492. # ---------------------------------------------------------------------------
  1493. def _ams_payload(tray_now, ams_units=None, tray_exist_bits=None, ams_exist_bits=None):
  1494. """Build minimal print.ams payload for tray_now disambiguation tests."""
  1495. ams = {"tray_now": str(tray_now)}
  1496. if ams_units is not None:
  1497. ams["ams"] = ams_units
  1498. if tray_exist_bits is not None:
  1499. ams["tray_exist_bits"] = tray_exist_bits
  1500. if ams_exist_bits is not None:
  1501. ams["ams_exist_bits"] = ams_exist_bits
  1502. return {"print": {"ams": ams}}
  1503. def _extruder_info_payload(extruders):
  1504. """Build device.extruder.info payload (dual-nozzle detection + snow).
  1505. Each entry in *extruders* is a dict with at least ``id`` and ``snow``.
  1506. """
  1507. return {
  1508. "print": {
  1509. "device": {
  1510. "extruder": {
  1511. "info": extruders,
  1512. }
  1513. }
  1514. }
  1515. }
  1516. def _extruder_state_payload(state_val):
  1517. """Build device.extruder.state payload (active extruder via bit 8)."""
  1518. return {
  1519. "print": {
  1520. "device": {
  1521. "extruder": {
  1522. "state": state_val,
  1523. }
  1524. }
  1525. }
  1526. }
  1527. # ---------------------------------------------------------------------------
  1528. # 1. Single-nozzle X1E — direct passthrough
  1529. # ---------------------------------------------------------------------------
  1530. class TestTrayNowSingleNozzleX1E:
  1531. """Single-nozzle, 1 AMS — tray_now is a direct passthrough."""
  1532. @pytest.fixture
  1533. def mqtt_client(self):
  1534. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1535. return BambuMQTTClient(
  1536. ip_address="192.168.1.100",
  1537. serial_number="TEST_X1E",
  1538. access_code="12345678",
  1539. )
  1540. def test_tray_now_direct_passthrough_slot_0_to_3(self, mqtt_client):
  1541. """Each tray_now 0-3 maps 1:1 on single-nozzle printers."""
  1542. for slot in range(4):
  1543. mqtt_client._process_message(_ams_payload(slot))
  1544. assert mqtt_client.state.tray_now == slot
  1545. def test_tray_now_255_means_unloaded(self, mqtt_client):
  1546. """tray_now=255 means no filament loaded."""
  1547. mqtt_client._process_message(_ams_payload(255))
  1548. assert mqtt_client.state.tray_now == 255
  1549. def test_single_extruder_does_not_trigger_dual_nozzle(self, mqtt_client):
  1550. """device.extruder.info with 1 entry must NOT set _is_dual_nozzle."""
  1551. mqtt_client._process_message(_extruder_info_payload([{"id": 0, "snow": 0xFF00FF}]))
  1552. assert mqtt_client._is_dual_nozzle is False
  1553. def test_last_loaded_tray_survives_unload(self, mqtt_client):
  1554. """Load tray 2, unload → last_loaded_tray stays 2."""
  1555. mqtt_client._process_message(_ams_payload(2))
  1556. assert mqtt_client.state.last_loaded_tray == 2
  1557. mqtt_client._process_message(_ams_payload(255))
  1558. assert mqtt_client.state.tray_now == 255
  1559. assert mqtt_client.state.last_loaded_tray == 2
  1560. # ---------------------------------------------------------------------------
  1561. # 2. Single-nozzle P2S — multiple AMS, global IDs pass through
  1562. # ---------------------------------------------------------------------------
  1563. class TestTrayNowSingleNozzleP2S:
  1564. """Single-nozzle, 2 AMS — tray_now > 3 passes through as global ID."""
  1565. @pytest.fixture
  1566. def mqtt_client(self):
  1567. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1568. return BambuMQTTClient(
  1569. ip_address="192.168.1.100",
  1570. serial_number="TEST_P2S",
  1571. access_code="12345678",
  1572. )
  1573. def test_tray_now_ams1_global_ids_4_to_7(self, mqtt_client):
  1574. """tray_now 4-7 are global IDs for AMS 1 on single-nozzle printers."""
  1575. for global_id in range(4, 8):
  1576. mqtt_client._process_message(_ams_payload(global_id))
  1577. assert mqtt_client.state.tray_now == global_id
  1578. def test_tray_change_across_ams_units(self, mqtt_client):
  1579. """Switch from AMS 0 slot 1 → AMS 1 slot 2 (global 6)."""
  1580. mqtt_client._process_message(_ams_payload(1))
  1581. assert mqtt_client.state.tray_now == 1
  1582. mqtt_client._process_message(_ams_payload(6))
  1583. assert mqtt_client.state.tray_now == 6
  1584. # ---------------------------------------------------------------------------
  1585. # 2b. Single-nozzle P2S — multi-AMS local slot disambiguation (#420)
  1586. # ---------------------------------------------------------------------------
  1587. class TestTrayNowP2SMultiAmsDisambiguation:
  1588. """P2S firmware sends local slot IDs (0-3) in tray_now even with dual AMS.
  1589. When ams_exist_bits indicates >1 AMS unit and tray_now is 0-3, the backend
  1590. should use the MQTT mapping field (snow-encoded) to resolve the correct
  1591. global tray ID.
  1592. """
  1593. @pytest.fixture
  1594. def mqtt_client(self):
  1595. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1596. client = BambuMQTTClient(
  1597. ip_address="192.168.1.100",
  1598. serial_number="TEST_P2S_DUAL",
  1599. access_code="12345678",
  1600. )
  1601. return client
  1602. def test_resolves_ams1_slot1_from_mapping(self, mqtt_client):
  1603. """tray_now=1 with mapping=[257] → global ID 5 (AMS1-T1).
  1604. 257 snow-decoded: ams_hw_id=1, slot=1 → global 1*4+1=5.
  1605. """
  1606. # Set mapping field in raw_data (as the MQTT handler would)
  1607. mqtt_client.state.raw_data["mapping"] = [257]
  1608. mqtt_client._process_message(
  1609. _ams_payload(1, ams_exist_bits="3") # '3' = 0b11 → AMS 0 and 1
  1610. )
  1611. assert mqtt_client.state.tray_now == 5
  1612. def test_resolves_ams1_slot0_from_mapping(self, mqtt_client):
  1613. """tray_now=0 with mapping=[256] → global ID 4 (AMS1-T0).
  1614. 256 snow-decoded: ams_hw_id=1, slot=0 → global 1*4+0=4.
  1615. """
  1616. mqtt_client.state.raw_data["mapping"] = [256]
  1617. mqtt_client._process_message(_ams_payload(0, ams_exist_bits="3"))
  1618. assert mqtt_client.state.tray_now == 4
  1619. def test_resolves_ams1_slot3_from_mapping(self, mqtt_client):
  1620. """tray_now=3 with mapping=[259] → global ID 7 (AMS1-T3).
  1621. 259 snow-decoded: ams_hw_id=1, slot=3 → global 1*4+3=7.
  1622. """
  1623. mqtt_client.state.raw_data["mapping"] = [259]
  1624. mqtt_client._process_message(_ams_payload(3, ams_exist_bits="3"))
  1625. assert mqtt_client.state.tray_now == 7
  1626. def test_ams0_slot_unchanged_when_mapping_confirms_ams0(self, mqtt_client):
  1627. """tray_now=1 with mapping=[1] → stays 1 (AMS0-T1).
  1628. 1 snow-decoded: ams_hw_id=0, slot=1 → global 0*4+1=1.
  1629. """
  1630. mqtt_client.state.raw_data["mapping"] = [1]
  1631. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1632. assert mqtt_client.state.tray_now == 1
  1633. def test_multicolor_resolves_ams1_from_multi_entry_mapping(self, mqtt_client):
  1634. """Multi-color print: mapping=[0, 257] → tray_now=1 resolves to AMS1-T1 (5).
  1635. Entry 0: ams_hw_id=0, slot=0 (local 0) — doesn't match tray_now=1.
  1636. Entry 257: ams_hw_id=1, slot=1 (local 1) — matches tray_now=1 → global 5.
  1637. """
  1638. mqtt_client.state.raw_data["mapping"] = [0, 257]
  1639. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1640. assert mqtt_client.state.tray_now == 5
  1641. def test_multicolor_four_slot_mapping(self, mqtt_client):
  1642. """mapping=[65535, 65535, 65535, 257] → tray_now=1 resolves to global 5.
  1643. Only entry 257 has local slot=1, other entries are unmapped (65535).
  1644. Reproduces exact data from issue #420 support package.
  1645. """
  1646. mqtt_client.state.raw_data["mapping"] = [65535, 65535, 65535, 257]
  1647. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1648. assert mqtt_client.state.tray_now == 5
  1649. def test_ambiguous_mapping_falls_back_to_local_slot(self, mqtt_client):
  1650. """Two AMS units with same local slot in mapping → ambiguous, keep local slot.
  1651. mapping=[1, 257]: both have local slot 1 (AMS0-T1 and AMS1-T1).
  1652. Cannot disambiguate → fall back to tray_now=1.
  1653. """
  1654. mqtt_client.state.raw_data["mapping"] = [1, 257]
  1655. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1656. assert mqtt_client.state.tray_now == 1
  1657. def test_no_mapping_falls_back_to_local_slot(self, mqtt_client):
  1658. """No mapping field available → fall back to raw tray_now."""
  1659. # No mapping in raw_data (e.g. manual filament load, not during print)
  1660. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1661. assert mqtt_client.state.tray_now == 1
  1662. def test_empty_mapping_falls_back_to_local_slot(self, mqtt_client):
  1663. """Empty mapping list → fall back to raw tray_now."""
  1664. mqtt_client.state.raw_data["mapping"] = []
  1665. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1666. assert mqtt_client.state.tray_now == 1
  1667. def test_single_ams_passthrough(self, mqtt_client):
  1668. """Single AMS (ams_exist_bits='1') → tray_now 0-3 is direct global ID."""
  1669. mqtt_client._process_message(_ams_payload(2, ams_exist_bits="1"))
  1670. assert mqtt_client.state.tray_now == 2
  1671. def test_no_ams_exist_bits_passthrough(self, mqtt_client):
  1672. """No ams_exist_bits in payload → fall back to raw tray_now."""
  1673. mqtt_client._process_message(_ams_payload(1))
  1674. assert mqtt_client.state.tray_now == 1
  1675. def test_tray_now_255_unaffected_by_multi_ams(self, mqtt_client):
  1676. """tray_now=255 (unloaded) passes through regardless of AMS count."""
  1677. mqtt_client.state.raw_data["mapping"] = [257]
  1678. mqtt_client._process_message(_ams_payload(255, ams_exist_bits="3"))
  1679. assert mqtt_client.state.tray_now == 255
  1680. def test_tray_now_above_3_unaffected(self, mqtt_client):
  1681. """tray_now > 3 is already a global ID and passes through directly."""
  1682. mqtt_client._process_message(_ams_payload(6, ams_exist_bits="3"))
  1683. assert mqtt_client.state.tray_now == 6
  1684. def test_last_loaded_tray_uses_resolved_global_id(self, mqtt_client):
  1685. """last_loaded_tray should reflect the resolved global ID, not local slot."""
  1686. mqtt_client.state.raw_data["mapping"] = [257]
  1687. mqtt_client.state.state = "RUNNING"
  1688. mqtt_client._process_message(_ams_payload(1, ams_exist_bits="3"))
  1689. assert mqtt_client.state.tray_now == 5
  1690. assert mqtt_client.state.last_loaded_tray == 5
  1691. class TestResolveLocalSlotFromMapping:
  1692. """Unit tests for _resolve_local_slot_from_mapping static method."""
  1693. def test_single_match_ams0(self):
  1694. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1695. assert BambuMQTTClient._resolve_local_slot_from_mapping(1, [1]) == 1
  1696. def test_single_match_ams1(self):
  1697. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1698. # 257 = 1*256 + 1 → AMS1 slot1 → global 5
  1699. assert BambuMQTTClient._resolve_local_slot_from_mapping(1, [257]) == 5
  1700. def test_single_match_ams2(self):
  1701. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1702. # 514 = 2*256 + 2 → AMS2 slot2 → global 10
  1703. assert BambuMQTTClient._resolve_local_slot_from_mapping(2, [514]) == 10
  1704. def test_unmapped_entries_skipped(self):
  1705. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1706. assert BambuMQTTClient._resolve_local_slot_from_mapping(1, [65535, 65535, 65535, 257]) == 5
  1707. def test_no_match_returns_none(self):
  1708. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1709. # mapping has slot 0 only, looking for slot 2
  1710. assert BambuMQTTClient._resolve_local_slot_from_mapping(2, [0]) is None
  1711. def test_ambiguous_returns_none(self):
  1712. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1713. # Both AMS0 slot1 (1) and AMS1 slot1 (257) → ambiguous
  1714. assert BambuMQTTClient._resolve_local_slot_from_mapping(1, [1, 257]) is None
  1715. def test_none_mapping_returns_none(self):
  1716. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1717. assert BambuMQTTClient._resolve_local_slot_from_mapping(1, None) is None
  1718. def test_empty_mapping_returns_none(self):
  1719. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1720. assert BambuMQTTClient._resolve_local_slot_from_mapping(1, []) is None
  1721. def test_ams_ht_slot0_match(self):
  1722. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1723. # AMS-HT id=128: snow = 128*256 + 0 = 32768
  1724. assert BambuMQTTClient._resolve_local_slot_from_mapping(0, [32768]) == 128
  1725. # ---------------------------------------------------------------------------
  1726. # 3. H2D Pro — initial state detection
  1727. # ---------------------------------------------------------------------------
  1728. class TestTrayNowDualNozzleH2DSetup:
  1729. """H2D Pro initial state detection."""
  1730. @pytest.fixture
  1731. def mqtt_client(self):
  1732. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1733. return BambuMQTTClient(
  1734. ip_address="192.168.1.100",
  1735. serial_number="TEST_H2D",
  1736. access_code="12345678",
  1737. )
  1738. def test_dual_nozzle_detected_from_extruder_info(self, mqtt_client):
  1739. """2 entries in device.extruder.info → _is_dual_nozzle=True."""
  1740. mqtt_client._process_message(
  1741. _extruder_info_payload(
  1742. [
  1743. {"id": 0, "snow": 0xFF00FF},
  1744. {"id": 1, "snow": 0xFF00FF},
  1745. ]
  1746. )
  1747. )
  1748. assert mqtt_client._is_dual_nozzle is True
  1749. def test_ams_extruder_map_parsed_from_info_field(self, mqtt_client):
  1750. """AMS info field is hex: 0x2003 → ext 0 (right), 0x2104 → ext 1 (left)."""
  1751. # MQTT sends info as string; BambuStudio parses as hex via stoull(str, 16)
  1752. ams_units = [
  1753. {"id": 0, "info": "2003", "tray": [{"id": i} for i in range(4)]},
  1754. {"id": 128, "info": "2104", "tray": [{"id": 0}]},
  1755. ]
  1756. payload = {
  1757. "print": {
  1758. "ams": {
  1759. "ams": ams_units,
  1760. "tray_now": "255",
  1761. "tray_exist_bits": "1000f",
  1762. },
  1763. }
  1764. }
  1765. mqtt_client._process_message(payload)
  1766. # 0x2003: bits 8-11 = (0x2003 >> 8) & 0xF = 0x20 & 0xF = 0 → extruder 0 (right)
  1767. # 0x2104: bits 8-11 = (0x2104 >> 8) & 0xF = 0x21 & 0xF = 1 → extruder 1 (left)
  1768. assert mqtt_client.state.ams_extruder_map == {"0": 0, "128": 1}
  1769. def test_ams_extruder_map_real_h2d_values(self, mqtt_client):
  1770. """Real H2D MQTT values: AMS2 Pro on right, AMS-HT on left."""
  1771. ams_units = [
  1772. {"id": 0, "info": "10001003", "tray": [{"id": i} for i in range(4)]},
  1773. {"id": 128, "info": "10002104", "tray": [{"id": 0}]},
  1774. ]
  1775. payload = {
  1776. "print": {
  1777. "ams": {
  1778. "ams": ams_units,
  1779. "tray_now": "255",
  1780. "tray_exist_bits": "1000a",
  1781. },
  1782. }
  1783. }
  1784. mqtt_client._process_message(payload)
  1785. # 0x10001003: bits 8-11 = (0x10001003 >> 8) & 0xF = 0x10 & 0xF = 0 → right
  1786. # 0x10002104: bits 8-11 = (0x10002104 >> 8) & 0xF = 0x21 & 0xF = 1 → left
  1787. assert mqtt_client.state.ams_extruder_map == {"0": 0, "128": 1}
  1788. def test_ams_extruder_map_skips_uninitialized(self, mqtt_client):
  1789. """extruder_id 0xE means uninitialized AMS — should be skipped."""
  1790. ams_units = [
  1791. {"id": 0, "info": "e03", "tray": [{"id": i} for i in range(4)]},
  1792. ]
  1793. payload = {
  1794. "print": {
  1795. "ams": {
  1796. "ams": ams_units,
  1797. "tray_now": "255",
  1798. "tray_exist_bits": "f",
  1799. },
  1800. }
  1801. }
  1802. mqtt_client._process_message(payload)
  1803. assert mqtt_client.state.ams_extruder_map == {}
  1804. def test_ams_extruder_map_partial_update_preserves_entries(self, mqtt_client):
  1805. """Partial MQTT update with one AMS should not overwrite other entries."""
  1806. # First: full update with both AMS units
  1807. full_payload = {
  1808. "print": {
  1809. "ams": {
  1810. "ams": [
  1811. {"id": 0, "info": "2003", "tray": [{"id": i} for i in range(4)]},
  1812. {"id": 128, "info": "2104", "tray": [{"id": 0}]},
  1813. ],
  1814. "tray_now": "255",
  1815. "tray_exist_bits": "1000f",
  1816. },
  1817. }
  1818. }
  1819. mqtt_client._process_message(full_payload)
  1820. assert mqtt_client.state.ams_extruder_map == {"0": 0, "128": 1}
  1821. # Then: partial update with only AMS 0 (no info field this time)
  1822. partial_payload = {
  1823. "print": {
  1824. "ams": {
  1825. "ams": [
  1826. {"id": 0, "tray": [{"id": 0, "remain": 50}]},
  1827. ],
  1828. "tray_now": "0",
  1829. "tray_exist_bits": "1000f",
  1830. },
  1831. }
  1832. }
  1833. mqtt_client._process_message(partial_payload)
  1834. # Both entries should still be present
  1835. assert mqtt_client.state.ams_extruder_map == {"0": 0, "128": 1}
  1836. def test_dual_nozzle_detection_before_ams_in_same_message(self, mqtt_client):
  1837. """Dual-nozzle detection at line 538 happens before _handle_ams_data() at line 549.
  1838. If both arrive in the same message, tray_now disambiguation already uses dual-nozzle logic.
  1839. """
  1840. payload = {
  1841. "print": {
  1842. "device": {
  1843. "extruder": {
  1844. "info": [
  1845. {"id": 0, "snow": 0xFF00FF},
  1846. {"id": 1, "snow": 0xFF00FF},
  1847. ],
  1848. "state": 0x0001,
  1849. }
  1850. },
  1851. "ams": {
  1852. "ams": [
  1853. {"id": 0, "info": "2003", "tray": [{"id": i} for i in range(4)]},
  1854. ],
  1855. "tray_now": "2",
  1856. "tray_exist_bits": "f",
  1857. },
  1858. }
  1859. }
  1860. mqtt_client._process_message(payload)
  1861. # Dual-nozzle was detected; AMS 0 on right extruder (active by default);
  1862. # snow is 0xFF00FF (unloaded), so falls through to ams_extruder_map fallback.
  1863. # Single AMS on extruder 0 → global_id = 0*4+2 = 2
  1864. assert mqtt_client._is_dual_nozzle is True
  1865. assert mqtt_client.state.tray_now == 2
  1866. # ---------------------------------------------------------------------------
  1867. # Shared H2D fixture for classes 4-8
  1868. # ---------------------------------------------------------------------------
  1869. class _H2DFixtureMixin:
  1870. """Mixin providing a pre-configured H2D Pro client."""
  1871. @pytest.fixture
  1872. def mqtt_client(self):
  1873. from backend.app.services.bambu_mqtt import BambuMQTTClient
  1874. return BambuMQTTClient(
  1875. ip_address="192.168.1.100",
  1876. serial_number="TEST_H2D",
  1877. access_code="12345678",
  1878. )
  1879. @pytest.fixture
  1880. def h2d_client(self, mqtt_client):
  1881. """Pre-configure as H2D Pro: dual-nozzle + ams_extruder_map."""
  1882. mqtt_client._process_message(
  1883. {
  1884. "print": {
  1885. "device": {
  1886. "extruder": {
  1887. "info": [
  1888. {"id": 0, "snow": 0xFF00FF},
  1889. {"id": 1, "snow": 0xFF00FF},
  1890. ],
  1891. "state": 0x0001, # right extruder active
  1892. }
  1893. },
  1894. "ams": {
  1895. "ams": [
  1896. {"id": 0, "info": "2003", "tray": [{"id": i} for i in range(4)]},
  1897. {"id": 128, "info": "2104", "tray": [{"id": 0}]},
  1898. ],
  1899. "tray_now": "255",
  1900. "tray_exist_bits": "1000f",
  1901. },
  1902. }
  1903. }
  1904. )
  1905. assert mqtt_client._is_dual_nozzle is True
  1906. assert mqtt_client.state.ams_extruder_map == {"0": 0, "128": 1}
  1907. return mqtt_client
  1908. # ---------------------------------------------------------------------------
  1909. # 4. H2D Snow field disambiguation
  1910. # ---------------------------------------------------------------------------
  1911. class TestTrayNowDualNozzleH2DSnow(_H2DFixtureMixin):
  1912. """Snow field disambiguation (primary path)."""
  1913. def test_snow_disambiguates_ams0_slot(self, h2d_client):
  1914. """snow ext[0]=AMS 0 slot 2, tray_now='2' → global 2."""
  1915. # Send snow update FIRST (snow is parsed AFTER tray_now in the same message,
  1916. # so we need it in a prior message).
  1917. snow_val = 0 << 8 | 2 # AMS 0 slot 2 = raw 2
  1918. h2d_client._process_message(
  1919. _extruder_info_payload(
  1920. [
  1921. {"id": 0, "snow": snow_val},
  1922. {"id": 1, "snow": 0xFF00FF},
  1923. ]
  1924. )
  1925. )
  1926. assert h2d_client.state.h2d_extruder_snow.get(0) == 2
  1927. # Now send tray_now=2
  1928. h2d_client._process_message(_ams_payload(2))
  1929. assert h2d_client.state.tray_now == 2
  1930. def test_snow_disambiguates_ams_ht_to_128(self, h2d_client):
  1931. """snow ext[1]=AMS HT (128), left active, tray_now='0' → global 128."""
  1932. # Snow: extruder 1 → AMS 128 slot 0
  1933. snow_val = 128 << 8 | 0 # = 32768
  1934. h2d_client._process_message(
  1935. _extruder_info_payload(
  1936. [
  1937. {"id": 0, "snow": 0xFF00FF},
  1938. {"id": 1, "snow": snow_val},
  1939. ]
  1940. )
  1941. )
  1942. assert h2d_client.state.h2d_extruder_snow.get(1) == 128
  1943. # Switch to left extruder
  1944. h2d_client._process_message(_extruder_state_payload(0x0100))
  1945. assert h2d_client.state.active_extruder == 1
  1946. # tray_now="0" with left extruder active, snow says AMS HT (128)
  1947. # AMS HT snow_slot = 0 (single slot), parsed_tray_now = 0 → match
  1948. h2d_client._process_message(_ams_payload(0))
  1949. assert h2d_client.state.tray_now == 128
  1950. def test_snow_updates_h2d_extruder_snow_state(self, h2d_client):
  1951. """Verify state.h2d_extruder_snow dict is populated correctly."""
  1952. snow_ext0 = 1 << 8 | 3 # AMS 1 slot 3 → global 7
  1953. snow_ext1 = 0 << 8 | 0 # AMS 0 slot 0 → global 0
  1954. h2d_client._process_message(
  1955. _extruder_info_payload(
  1956. [
  1957. {"id": 0, "snow": snow_ext0},
  1958. {"id": 1, "snow": snow_ext1},
  1959. ]
  1960. )
  1961. )
  1962. assert h2d_client.state.h2d_extruder_snow[0] == 7
  1963. assert h2d_client.state.h2d_extruder_snow[1] == 0
  1964. def test_snow_unloaded_value(self, h2d_client):
  1965. """snow=0xFFFF (ams_id=255, slot=255) → 255 (unloaded)."""
  1966. h2d_client._process_message(
  1967. _extruder_info_payload(
  1968. [
  1969. {"id": 0, "snow": 0xFFFF},
  1970. {"id": 1, "snow": 0xFFFF},
  1971. ]
  1972. )
  1973. )
  1974. assert h2d_client.state.h2d_extruder_snow[0] == 255
  1975. assert h2d_client.state.h2d_extruder_snow[1] == 255
  1976. def test_snow_initial_sentinel_not_stored(self, h2d_client):
  1977. """snow=0xFF00FF (firmware initial sentinel) is not parsed into h2d_extruder_snow."""
  1978. # 0xFF00FF has ams_id=0xFF00=65280 which doesn't match any branch
  1979. h2d_client._process_message(
  1980. _extruder_info_payload(
  1981. [
  1982. {"id": 0, "snow": 0xFF00FF},
  1983. {"id": 1, "snow": 0xFF00FF},
  1984. ]
  1985. )
  1986. )
  1987. # Snow dict should remain empty (no matching branch)
  1988. assert h2d_client.state.h2d_extruder_snow == {}
  1989. # ---------------------------------------------------------------------------
  1990. # 5. H2D Pending target disambiguation
  1991. # ---------------------------------------------------------------------------
  1992. class TestTrayNowDualNozzleH2DPendingTarget(_H2DFixtureMixin):
  1993. """Pending target disambiguation (when Bambuddy initiates load)."""
  1994. def test_pending_target_matches_slot(self, h2d_client):
  1995. """pending=5, tray_now='1' (5%4=1 matches) → tray_now=5."""
  1996. h2d_client.state.pending_tray_target = 5
  1997. h2d_client._process_message(_ams_payload(1))
  1998. assert h2d_client.state.tray_now == 5
  1999. assert h2d_client.state.pending_tray_target is None # cleared
  2000. def test_pending_target_slot_mismatch(self, h2d_client):
  2001. """pending=5, tray_now='2' → uses raw slot, clears pending."""
  2002. h2d_client.state.pending_tray_target = 5
  2003. h2d_client._process_message(_ams_payload(2))
  2004. # Slot 2 != 5%4=1 → mismatch, uses raw slot 2
  2005. assert h2d_client.state.tray_now == 2
  2006. assert h2d_client.state.pending_tray_target is None
  2007. def test_pending_target_takes_priority_over_snow(self, h2d_client):
  2008. """When both pending and snow are set, pending wins."""
  2009. # Set up snow for extruder 0 → AMS 0 slot 1 → global 1
  2010. snow_val = 0 << 8 | 1
  2011. h2d_client._process_message(
  2012. _extruder_info_payload(
  2013. [
  2014. {"id": 0, "snow": snow_val},
  2015. {"id": 1, "snow": 0xFF00FF},
  2016. ]
  2017. )
  2018. )
  2019. assert h2d_client.state.h2d_extruder_snow.get(0) == 1
  2020. # Set pending target to AMS 1 slot 1 (global 5)
  2021. h2d_client.state.pending_tray_target = 5
  2022. # tray_now="1" — matches pending (5%4=1), pending should win over snow
  2023. h2d_client._process_message(_ams_payload(1))
  2024. assert h2d_client.state.tray_now == 5
  2025. # ---------------------------------------------------------------------------
  2026. # 6. H2D ams_extruder_map fallback
  2027. # ---------------------------------------------------------------------------
  2028. class TestTrayNowDualNozzleH2DFallback(_H2DFixtureMixin):
  2029. """ams_extruder_map fallback (no pending, no snow)."""
  2030. def test_single_ams_on_extruder_computes_global_id(self, h2d_client):
  2031. """AMS 0 on right extruder, tray_now='2' → 0*4+2=2."""
  2032. # h2d_client has snow=0xFF00FF (unloaded) by default, so snow path skips
  2033. h2d_client._process_message(_ams_payload(2))
  2034. # AMS 0 is the only AMS on extruder 0 (right, active by default)
  2035. # Fallback: single AMS → global = 0*4+2 = 2
  2036. assert h2d_client.state.tray_now == 2
  2037. def test_multiple_ams_keeps_current_if_valid(self, h2d_client):
  2038. """Current tray matches slot → keeps it (multi-AMS on same extruder)."""
  2039. # Set up: two AMS units on the same extruder (right, ext 0)
  2040. h2d_client.state.ams_extruder_map = {"0": 0, "1": 0}
  2041. # Pre-set tray_now=5 (AMS 1 slot 1) — current_ams=1 which is in ams_on_extruder
  2042. h2d_client.state.tray_now = 5
  2043. # tray_now="1" → 5%4=1 matches → keep current=5
  2044. h2d_client._process_message(_ams_payload(1))
  2045. assert h2d_client.state.tray_now == 5
  2046. def test_no_ams_on_extruder_uses_raw_slot(self, h2d_client):
  2047. """No AMS mapped to the active extruder → raw slot as global ID."""
  2048. # All AMS on left extruder, but right is active
  2049. h2d_client.state.ams_extruder_map = {"0": 1, "128": 1}
  2050. h2d_client._process_message(_ams_payload(2))
  2051. assert h2d_client.state.tray_now == 2
  2052. def test_single_ams_ht_on_extruder_returns_unit_id(self, h2d_client):
  2053. """AMS-HT 128 alone on left extruder, slot 0 → global ID 128 (not 512)."""
  2054. # Switch to left extruder (where AMS-HT 128 is mapped)
  2055. h2d_client._process_message(_extruder_state_payload(0x0100))
  2056. # Only AMS-HT 128 on left extruder; no snow available
  2057. h2d_client._process_message(_ams_payload(0))
  2058. assert h2d_client.state.tray_now == 128
  2059. def test_single_ams_ht_ignores_nonzero_slot(self, h2d_client):
  2060. """AMS-HT has single slot; even if printer reports slot 1, global ID = unit ID."""
  2061. h2d_client.state.ams_extruder_map = {"129": 0}
  2062. h2d_client._process_message(_ams_payload(1))
  2063. # AMS-HT 129: global ID = 129, not 129*4+1=517
  2064. assert h2d_client.state.tray_now == 129
  2065. def test_multiple_ams_keeps_current_ams_ht(self, h2d_client):
  2066. """Current tray is AMS-HT 128, slot 0 reported → keeps 128."""
  2067. h2d_client.state.ams_extruder_map = {"0": 0, "128": 0}
  2068. h2d_client.state.tray_now = 128
  2069. h2d_client._process_message(_ams_payload(0))
  2070. assert h2d_client.state.tray_now == 128
  2071. def test_multiple_ams_slot_nonzero_excludes_ams_ht(self, h2d_client):
  2072. """Slot > 0 eliminates AMS-HT candidates; single regular AMS left → resolves."""
  2073. # AMS 0 + AMS-HT 128 both on right extruder
  2074. h2d_client.state.ams_extruder_map = {"0": 0, "128": 0}
  2075. h2d_client.state.tray_now = 255 # no current match
  2076. # Slot 2 → can't be AMS-HT → only AMS 0 → global = 0*4+2 = 2
  2077. h2d_client._process_message(_ams_payload(2))
  2078. assert h2d_client.state.tray_now == 2
  2079. def test_multiple_ams_slot_nonzero_narrows_to_single_ht_excluded(self, h2d_client):
  2080. """Two regular AMS + one AMS-HT, slot > 0 → AMS-HT excluded but still ambiguous."""
  2081. h2d_client.state.ams_extruder_map = {"0": 0, "1": 0, "128": 0}
  2082. h2d_client.state.tray_now = 255
  2083. # Slot 3 → excludes AMS-HT, but AMS 0 and AMS 1 both remain → ambiguous
  2084. h2d_client._process_message(_ams_payload(3))
  2085. assert h2d_client.state.tray_now == 3 # raw slot fallback
  2086. # ---------------------------------------------------------------------------
  2087. # 6b. H2D last_loaded_tray validation
  2088. # ---------------------------------------------------------------------------
  2089. class TestLastLoadedTrayValidation(_H2DFixtureMixin):
  2090. """last_loaded_tray only stores physically valid tray IDs."""
  2091. def test_regular_ams_tray_stored(self, h2d_client):
  2092. """Valid regular AMS tray (0-15) → stored in last_loaded_tray."""
  2093. h2d_client.state.tray_now = 7
  2094. # Trigger tray_now processing via AMS message
  2095. h2d_client._process_message(
  2096. _extruder_info_payload(
  2097. [
  2098. {"id": 0, "snow": 1 << 8 | 3}, # AMS 1 slot 3 → global 7
  2099. {"id": 1, "snow": 0xFF00FF},
  2100. ]
  2101. )
  2102. )
  2103. h2d_client._process_message(_ams_payload(3))
  2104. assert h2d_client.state.tray_now == 7
  2105. assert h2d_client.state.last_loaded_tray == 7
  2106. def test_ams_ht_tray_stored(self, h2d_client):
  2107. """Valid AMS-HT tray (128-135) → stored in last_loaded_tray."""
  2108. h2d_client._process_message(_extruder_state_payload(0x0100))
  2109. h2d_client._process_message(
  2110. _extruder_info_payload(
  2111. [
  2112. {"id": 0, "snow": 0xFF00FF},
  2113. {"id": 1, "snow": 128 << 8 | 0},
  2114. ]
  2115. )
  2116. )
  2117. h2d_client._process_message(_ams_payload(0))
  2118. assert h2d_client.state.tray_now == 128
  2119. assert h2d_client.state.last_loaded_tray == 128
  2120. def test_unloaded_not_stored(self, h2d_client):
  2121. """tray_now=255 (unloaded) → last_loaded_tray unchanged."""
  2122. h2d_client.state.last_loaded_tray = 5
  2123. h2d_client._process_message(_ams_payload(255))
  2124. assert h2d_client.state.tray_now == 255
  2125. assert h2d_client.state.last_loaded_tray == 5
  2126. # ---------------------------------------------------------------------------
  2127. # 7. H2D Active extruder switching
  2128. # ---------------------------------------------------------------------------
  2129. class TestTrayNowDualNozzleH2DActiveExtruder(_H2DFixtureMixin):
  2130. """Active extruder switching via device.extruder.state bit 8."""
  2131. def test_active_extruder_right_by_default(self, h2d_client):
  2132. """Initial state.active_extruder == 0 (right)."""
  2133. assert h2d_client.state.active_extruder == 0
  2134. def test_extruder_state_bit8_switches_to_left(self, h2d_client):
  2135. """state=0x100 → active_extruder=1 (left)."""
  2136. h2d_client._process_message(_extruder_state_payload(0x0100))
  2137. assert h2d_client.state.active_extruder == 1
  2138. def test_extruder_state_bit8_switches_back_to_right(self, h2d_client):
  2139. """Cycle 0 → 1 → 0."""
  2140. h2d_client._process_message(_extruder_state_payload(0x0100))
  2141. assert h2d_client.state.active_extruder == 1
  2142. h2d_client._process_message(_extruder_state_payload(0x0001))
  2143. assert h2d_client.state.active_extruder == 0
  2144. def test_extruder_switch_changes_tray_disambiguation(self, h2d_client):
  2145. """Snow on both extruders; switching active changes which snow is used."""
  2146. # Snow: ext 0 → AMS 0 slot 1 (global 1), ext 1 → AMS 128 slot 0 (global 128)
  2147. h2d_client._process_message(
  2148. _extruder_info_payload(
  2149. [
  2150. {"id": 0, "snow": 0 << 8 | 1}, # AMS 0 slot 1 → global 1
  2151. {"id": 1, "snow": 128 << 8 | 0}, # AMS HT → global 128
  2152. ]
  2153. )
  2154. )
  2155. # Right active (default) — tray_now="1" → snow ext[0] says global 1
  2156. h2d_client._process_message(_ams_payload(1))
  2157. assert h2d_client.state.tray_now == 1
  2158. # Switch to left
  2159. h2d_client._process_message(_extruder_state_payload(0x0100))
  2160. # Left active — tray_now="0" → snow ext[1] says AMS HT (128), slot 0 matches
  2161. h2d_client._process_message(_ams_payload(0))
  2162. assert h2d_client.state.tray_now == 128
  2163. # ---------------------------------------------------------------------------
  2164. # 8. H2D Full multi-message sequences
  2165. # ---------------------------------------------------------------------------
  2166. class TestTrayNowDualNozzleH2DFullSequence(_H2DFixtureMixin):
  2167. """Multi-message sequences simulating real H2D Pro prints."""
  2168. def test_h2d_right_nozzle_ams0_lifecycle(self, h2d_client):
  2169. """Setup → load AMS 0 slot 1 → verify tray_now=1."""
  2170. # Snow update: extruder 0 loading AMS 0 slot 1
  2171. h2d_client._process_message(
  2172. _extruder_info_payload(
  2173. [
  2174. {"id": 0, "snow": 0 << 8 | 1},
  2175. {"id": 1, "snow": 0xFF00FF},
  2176. ]
  2177. )
  2178. )
  2179. # Printer reports tray_now="1"
  2180. h2d_client._process_message(_ams_payload(1))
  2181. assert h2d_client.state.tray_now == 1
  2182. assert h2d_client.state.last_loaded_tray == 1
  2183. def test_h2d_left_nozzle_ams_ht_lifecycle(self, h2d_client):
  2184. """Setup → switch left → load AMS HT → verify tray_now=128."""
  2185. # Switch to left extruder
  2186. h2d_client._process_message(_extruder_state_payload(0x0100))
  2187. # Snow: ext 1 → AMS HT slot 0
  2188. h2d_client._process_message(
  2189. _extruder_info_payload(
  2190. [
  2191. {"id": 0, "snow": 0xFF00FF},
  2192. {"id": 1, "snow": 128 << 8 | 0},
  2193. ]
  2194. )
  2195. )
  2196. # Printer reports tray_now="0" (AMS HT single slot)
  2197. h2d_client._process_message(_ams_payload(0))
  2198. assert h2d_client.state.tray_now == 128
  2199. assert h2d_client.state.last_loaded_tray == 128
  2200. def test_h2d_multi_color_alternating_nozzles(self, h2d_client):
  2201. """Multi-color print alternating between right and left nozzles.
  2202. Sequence:
  2203. 1. Right loads AMS 0 slot 0 (tray=0)
  2204. 2. Switch left, load AMS HT (tray=128)
  2205. 3. Switch right, snow updates, load AMS 0 slot 2 (tray=2)
  2206. 4. Unload (255)
  2207. """
  2208. # Step 1: Right extruder loads AMS 0 slot 0
  2209. h2d_client._process_message(
  2210. _extruder_info_payload(
  2211. [
  2212. {"id": 0, "snow": 0 << 8 | 0},
  2213. {"id": 1, "snow": 0xFF00FF},
  2214. ]
  2215. )
  2216. )
  2217. h2d_client._process_message(_ams_payload(0))
  2218. assert h2d_client.state.tray_now == 0
  2219. # Step 2: Switch to left, load AMS HT
  2220. h2d_client._process_message(_extruder_state_payload(0x0100))
  2221. h2d_client._process_message(
  2222. _extruder_info_payload(
  2223. [
  2224. {"id": 0, "snow": 0 << 8 | 0},
  2225. {"id": 1, "snow": 128 << 8 | 0},
  2226. ]
  2227. )
  2228. )
  2229. h2d_client._process_message(_ams_payload(0))
  2230. assert h2d_client.state.tray_now == 128
  2231. # Step 3: Switch back to right, load AMS 0 slot 2
  2232. h2d_client._process_message(_extruder_state_payload(0x0001))
  2233. h2d_client._process_message(
  2234. _extruder_info_payload(
  2235. [
  2236. {"id": 0, "snow": 0 << 8 | 2},
  2237. {"id": 1, "snow": 128 << 8 | 0},
  2238. ]
  2239. )
  2240. )
  2241. h2d_client._process_message(_ams_payload(2))
  2242. assert h2d_client.state.tray_now == 2
  2243. # Step 4: Unload
  2244. h2d_client._process_message(_ams_payload(255))
  2245. assert h2d_client.state.tray_now == 255
  2246. assert h2d_client.state.last_loaded_tray == 2
  2247. class TestTrayChangeLog:
  2248. """Tests for tray_change_log tracking during prints (mid-print tray switch)."""
  2249. @pytest.fixture
  2250. def mqtt_client(self):
  2251. """Create a BambuMQTTClient instance for testing."""
  2252. from backend.app.services.bambu_mqtt import BambuMQTTClient
  2253. client = BambuMQTTClient(
  2254. ip_address="192.168.1.100",
  2255. serial_number="TRAYLOG1",
  2256. access_code="12345678",
  2257. )
  2258. return client
  2259. def test_tray_change_log_defaults_empty(self, mqtt_client):
  2260. """tray_change_log starts as an empty list."""
  2261. assert mqtt_client.state.tray_change_log == []
  2262. def test_tray_change_log_seeded_on_print_start(self, mqtt_client):
  2263. """Print start clears log and seeds with initial tray at layer 0."""
  2264. mqtt_client.state.tray_now = 2
  2265. mqtt_client.state.last_loaded_tray = 2
  2266. mqtt_client._previous_gcode_state = "IDLE"
  2267. # Transition to RUNNING via _process_message
  2268. mqtt_client._process_message(
  2269. {
  2270. "print": {
  2271. "gcode_state": "RUNNING",
  2272. "gcode_file": "test.3mf",
  2273. }
  2274. }
  2275. )
  2276. assert mqtt_client.state.tray_change_log == [(2, 0)]
  2277. def test_tray_change_log_cleared_on_new_print(self, mqtt_client):
  2278. """Old log entries are cleared when a new print starts."""
  2279. mqtt_client.state.tray_change_log = [(5, 0), (3, 100)]
  2280. mqtt_client.state.tray_now = 1
  2281. mqtt_client.state.last_loaded_tray = 1
  2282. mqtt_client._previous_gcode_state = "IDLE"
  2283. mqtt_client._process_message(
  2284. {
  2285. "print": {
  2286. "gcode_state": "RUNNING",
  2287. "gcode_file": "new.3mf",
  2288. }
  2289. }
  2290. )
  2291. assert mqtt_client.state.tray_change_log == [(1, 0)]
  2292. # Helper that mirrors the production gate at bambu_mqtt.py:1571 — tests
  2293. # below replicate the gate so they validate the *contract* without needing
  2294. # to feed a synthetic AMS push through the full _process_message path.
  2295. @staticmethod
  2296. def _record_if_change(client, tn: int) -> None:
  2297. if (0 <= tn <= 15) or (128 <= tn <= 135) or tn == 254:
  2298. if tn != client.state.last_loaded_tray and client._was_running and not client._completion_triggered:
  2299. client.state.tray_change_log.append((tn, client.state.layer_num))
  2300. client.state.last_loaded_tray = tn
  2301. def test_tray_change_recorded_during_running(self, mqtt_client):
  2302. """Tray change while RUNNING is appended to the log."""
  2303. mqtt_client.state.state = "RUNNING"
  2304. mqtt_client._was_running = True
  2305. mqtt_client._completion_triggered = False
  2306. mqtt_client.state.layer_num = 50
  2307. mqtt_client.state.last_loaded_tray = 0
  2308. mqtt_client.state.tray_change_log = [(0, 0)]
  2309. mqtt_client.state.tray_now = 1
  2310. self._record_if_change(mqtt_client, mqtt_client.state.tray_now)
  2311. assert mqtt_client.state.tray_change_log == [(0, 0), (1, 50)]
  2312. def test_tray_change_not_recorded_when_idle(self, mqtt_client):
  2313. """Tray changes outside an active print are NOT logged."""
  2314. # IDLE between prints — both lifecycle flags in the cleared state.
  2315. mqtt_client.state.state = "IDLE"
  2316. mqtt_client._was_running = False
  2317. mqtt_client._completion_triggered = False
  2318. mqtt_client.state.layer_num = 0
  2319. mqtt_client.state.last_loaded_tray = 0
  2320. mqtt_client.state.tray_change_log = []
  2321. mqtt_client.state.tray_now = 3
  2322. self._record_if_change(mqtt_client, mqtt_client.state.tray_now)
  2323. assert mqtt_client.state.tray_change_log == []
  2324. def test_tray_change_recorded_during_pause(self, mqtt_client):
  2325. """Tray change while PAUSE is also logged (AMS can swap during pause)."""
  2326. mqtt_client.state.state = "PAUSE"
  2327. mqtt_client._was_running = True
  2328. mqtt_client._completion_triggered = False
  2329. mqtt_client.state.layer_num = 75
  2330. mqtt_client.state.last_loaded_tray = 2
  2331. mqtt_client.state.tray_change_log = [(2, 0)]
  2332. mqtt_client.state.tray_now = 5
  2333. self._record_if_change(mqtt_client, mqtt_client.state.tray_now)
  2334. assert mqtt_client.state.tray_change_log == [(2, 0), (5, 75)]
  2335. def test_tray_change_recorded_during_intermediate_state(self, mqtt_client):
  2336. """Tray change during a transient non-RUNNING state mid-print is logged.
  2337. Regression for #957: P2S firmware briefly transitions out of RUNNING
  2338. (e.g. into LOADING) when the AMS auto-falls-back from an empty spool to
  2339. a same-material sibling. The previous gate ``state in ("RUNNING",
  2340. "PAUSE")`` missed this transition entirely, so the usage tracker had no
  2341. evidence of the switch and double-credited the original tray with the
  2342. full 3MF estimate while the remain%-delta path added the fallback
  2343. weight on top. The new gate keys on the print-lifecycle flags
  2344. (``_was_running and not _completion_triggered``) so any tray change
  2345. between print start and completion is captured regardless of the
  2346. momentary gcode_state string.
  2347. """
  2348. mqtt_client.state.state = "LOADING" # not RUNNING/PAUSE — old gate would skip
  2349. mqtt_client._was_running = True
  2350. mqtt_client._completion_triggered = False
  2351. mqtt_client.state.layer_num = 42
  2352. mqtt_client.state.last_loaded_tray = 0
  2353. mqtt_client.state.tray_change_log = [(0, 0)]
  2354. # AMS auto-fallback: T0 ran out, swapped to T1 of same material
  2355. mqtt_client.state.tray_now = 1
  2356. self._record_if_change(mqtt_client, mqtt_client.state.tray_now)
  2357. assert mqtt_client.state.tray_change_log == [(0, 0), (1, 42)]
  2358. def test_tray_change_not_recorded_after_completion(self, mqtt_client):
  2359. """Once on_print_complete has fired, further tray changes don't pollute the log."""
  2360. mqtt_client.state.state = "FINISH"
  2361. mqtt_client._was_running = True
  2362. mqtt_client._completion_triggered = True # completion already triggered
  2363. mqtt_client.state.layer_num = 0
  2364. mqtt_client.state.last_loaded_tray = 1
  2365. mqtt_client.state.tray_change_log = [(0, 0), (1, 50)]
  2366. mqtt_client.state.tray_now = 2
  2367. self._record_if_change(mqtt_client, mqtt_client.state.tray_now)
  2368. # Log unchanged — completion already triggered so post-print tray
  2369. # movement (e.g. printer self-cleaning) doesn't bleed into the next
  2370. # print's attribution.
  2371. assert mqtt_client.state.tray_change_log == [(0, 0), (1, 50)]
  2372. def test_same_tray_not_logged_twice(self, mqtt_client):
  2373. """Same tray value doesn't create duplicate log entries."""
  2374. mqtt_client.state.state = "RUNNING"
  2375. mqtt_client._was_running = True
  2376. mqtt_client._completion_triggered = False
  2377. mqtt_client.state.layer_num = 30
  2378. mqtt_client.state.last_loaded_tray = 2
  2379. mqtt_client.state.tray_change_log = [(2, 0)]
  2380. mqtt_client.state.tray_now = 2
  2381. self._record_if_change(mqtt_client, mqtt_client.state.tray_now)
  2382. assert mqtt_client.state.tray_change_log == [(2, 0)]
  2383. def test_multiple_tray_changes(self, mqtt_client):
  2384. """Multiple tray changes create a full history."""
  2385. mqtt_client.state.state = "RUNNING"
  2386. mqtt_client._was_running = True
  2387. mqtt_client._completion_triggered = False
  2388. mqtt_client.state.last_loaded_tray = 0
  2389. mqtt_client.state.tray_change_log = [(0, 0)]
  2390. for tray, layer in [(1, 50), (3, 120), (0, 200)]:
  2391. mqtt_client.state.tray_now = tray
  2392. mqtt_client.state.layer_num = layer
  2393. self._record_if_change(mqtt_client, tray)
  2394. assert mqtt_client.state.tray_change_log == [(0, 0), (1, 50), (3, 120), (0, 200)]
  2395. class TestDeveloperModeDetection:
  2396. """Tests for developer LAN mode detection from MQTT 'fun' field."""
  2397. @pytest.fixture
  2398. def mqtt_client(self):
  2399. """Create a BambuMQTTClient instance for testing."""
  2400. from backend.app.services.bambu_mqtt import BambuMQTTClient
  2401. client = BambuMQTTClient(
  2402. ip_address="192.168.1.100",
  2403. serial_number="TEST123",
  2404. access_code="12345678",
  2405. )
  2406. return client
  2407. def test_developer_mode_initially_none(self, mqtt_client):
  2408. """Verify developer_mode starts as None (unknown)."""
  2409. assert mqtt_client.state.developer_mode is None
  2410. def test_developer_mode_on_when_bit_clear(self, mqtt_client):
  2411. """Verify developer_mode is True when bit 0x20000000 is clear."""
  2412. # Bit 29 clear in lower 32 bits = developer mode ON
  2413. payload = {
  2414. "print": {
  2415. "gcode_state": "IDLE",
  2416. "fun": "1C8187FF9CFF",
  2417. }
  2418. }
  2419. mqtt_client._process_message(payload)
  2420. assert mqtt_client.state.developer_mode is True
  2421. def test_developer_mode_off_when_bit_set(self, mqtt_client):
  2422. """Verify developer_mode is False when bit 0x20000000 is set."""
  2423. # Bit 29 set in lower 32 bits = developer mode OFF (encryption required)
  2424. payload = {
  2425. "print": {
  2426. "gcode_state": "IDLE",
  2427. "fun": "1C81A7FF9CFF",
  2428. }
  2429. }
  2430. mqtt_client._process_message(payload)
  2431. assert mqtt_client.state.developer_mode is False
  2432. def test_developer_mode_exact_bit_check(self, mqtt_client):
  2433. """Verify only bit 0x20000000 matters, not other bits."""
  2434. # 0x20000000 in hex = bit 29. Set ONLY that bit.
  2435. payload = {
  2436. "print": {
  2437. "gcode_state": "IDLE",
  2438. "fun": "000020000000",
  2439. }
  2440. }
  2441. mqtt_client._process_message(payload)
  2442. assert mqtt_client.state.developer_mode is False
  2443. # All zeros = all bits clear = developer mode ON
  2444. payload["print"]["fun"] = "000000000000"
  2445. mqtt_client._process_message(payload)
  2446. assert mqtt_client.state.developer_mode is True
  2447. def test_developer_mode_invalid_fun_ignored(self, mqtt_client):
  2448. """Verify invalid fun values don't crash or change state."""
  2449. mqtt_client.state.developer_mode = True
  2450. payload = {
  2451. "print": {
  2452. "gcode_state": "IDLE",
  2453. "fun": "not_a_hex_value",
  2454. }
  2455. }
  2456. mqtt_client._process_message(payload)
  2457. # Should remain unchanged
  2458. assert mqtt_client.state.developer_mode is True
  2459. def test_developer_mode_missing_fun_preserves_state(self, mqtt_client):
  2460. """Verify messages without fun field don't reset developer_mode."""
  2461. mqtt_client.state.developer_mode = False
  2462. payload = {
  2463. "print": {
  2464. "gcode_state": "RUNNING",
  2465. "mc_percent": 50,
  2466. }
  2467. }
  2468. mqtt_client._process_message(payload)
  2469. assert mqtt_client.state.developer_mode is False
  2470. def test_developer_mode_persists_across_messages(self, mqtt_client):
  2471. """Verify developer_mode set by fun persists across messages without fun."""
  2472. # First message sets developer_mode
  2473. mqtt_client._process_message(
  2474. {
  2475. "print": {
  2476. "gcode_state": "IDLE",
  2477. "fun": "3EC1AFFF9CFF",
  2478. }
  2479. }
  2480. )
  2481. assert mqtt_client.state.developer_mode is False
  2482. # Subsequent messages without fun don't change it
  2483. for _ in range(3):
  2484. mqtt_client._process_message(
  2485. {
  2486. "print": {
  2487. "gcode_state": "RUNNING",
  2488. "mc_percent": 50,
  2489. }
  2490. }
  2491. )
  2492. assert mqtt_client.state.developer_mode is False
  2493. class TestDeveloperModeProbeTimeout:
  2494. """Tests for developer mode probe timeout, retry, and forced reconnect (#887).
  2495. When a printer's MQTT session is half-broken (sends status but ignores
  2496. commands), the developer mode probe gets no response. The timeout logic
  2497. retries once, then force-closes the socket on the second failure.
  2498. """
  2499. @pytest.fixture
  2500. def mqtt_client(self):
  2501. import time
  2502. from unittest.mock import MagicMock
  2503. from backend.app.services.bambu_mqtt import BambuMQTTClient
  2504. client = BambuMQTTClient(
  2505. ip_address="192.168.1.100",
  2506. serial_number="TEST123",
  2507. access_code="12345678",
  2508. )
  2509. # Simulate connected state with a mock MQTT client
  2510. client.state.connected = True
  2511. mock_paho = MagicMock()
  2512. mock_paho.socket.return_value = MagicMock()
  2513. client._client = mock_paho
  2514. # Set connect time in the past so the 5s probe delay is satisfied
  2515. client._connect_time = time.monotonic() - 10.0
  2516. return client
  2517. def _make_pushall_data(self):
  2518. """Create a print data dict with >30 keys (triggers probe) and no 'fun' field."""
  2519. return {f"key_{i}": i for i in range(35)}
  2520. def test_first_timeout_allows_retry(self, mqtt_client):
  2521. """After first probe timeout, _dev_mode_probed resets to allow retry."""
  2522. import time
  2523. data = self._make_pushall_data()
  2524. # First pushall triggers the probe
  2525. mqtt_client._update_state(data)
  2526. assert mqtt_client._dev_mode_probed is True
  2527. assert mqtt_client._dev_mode_probe_seq is not None
  2528. assert mqtt_client.state.developer_mode is None
  2529. # Simulate 11 seconds passing
  2530. mqtt_client._dev_mode_probe_time = time.monotonic() - 11.0
  2531. # Next status message detects the timeout
  2532. mqtt_client._update_state(data)
  2533. assert mqtt_client._dev_mode_probe_failures == 1
  2534. assert mqtt_client._dev_mode_probe_seq is None
  2535. # Should allow retry on next full message
  2536. assert mqtt_client._dev_mode_probed is False
  2537. # Connection should NOT be force-closed after 1 failure
  2538. assert mqtt_client.state.connected is True
  2539. def test_second_timeout_forces_reconnect(self, mqtt_client):
  2540. """After two consecutive probe timeouts, force-close the socket.
  2541. Probe timeout detection runs from paho's network thread (no asyncio
  2542. loop), so force_reconnect_stale_session routes through socket-close
  2543. rather than hard-reset (loop_stop from inside the loop deadlocks)."""
  2544. import time
  2545. data = self._make_pushall_data()
  2546. state_change_called = []
  2547. mqtt_client.on_state_change = lambda s: state_change_called.append(True)
  2548. # First probe + timeout
  2549. mqtt_client._update_state(data)
  2550. mqtt_client._dev_mode_probe_time = time.monotonic() - 11.0
  2551. mqtt_client._update_state(data)
  2552. assert mqtt_client._dev_mode_probe_failures == 1
  2553. # Second probe (retry) + timeout
  2554. mqtt_client._update_state(data) # triggers new probe
  2555. assert mqtt_client._dev_mode_probed is True
  2556. mqtt_client._dev_mode_probe_time = time.monotonic() - 11.0
  2557. mqtt_client._update_state(data) # detects second timeout
  2558. assert mqtt_client._dev_mode_probe_failures == 2
  2559. assert mqtt_client.state.connected is False
  2560. assert mqtt_client._stale_reconnecting is True
  2561. # Sync test → no running loop → socket-close fallback path
  2562. mqtt_client._client.socket().close.assert_called()
  2563. assert len(state_change_called) > 0
  2564. def test_successful_probe_resets_failure_counter(self, mqtt_client):
  2565. """A probe response after a previous failure resets the counter."""
  2566. import time
  2567. data = self._make_pushall_data()
  2568. # First probe + timeout → failure=1
  2569. mqtt_client._update_state(data)
  2570. seq = mqtt_client._dev_mode_probe_seq
  2571. mqtt_client._dev_mode_probe_time = time.monotonic() - 11.0
  2572. mqtt_client._update_state(data)
  2573. assert mqtt_client._dev_mode_probe_failures == 1
  2574. # Retry probe
  2575. mqtt_client._update_state(data)
  2576. new_seq = mqtt_client._dev_mode_probe_seq
  2577. assert new_seq is not None
  2578. assert new_seq != seq
  2579. # Simulate successful response
  2580. mqtt_client._handle_dev_mode_probe_response(
  2581. {
  2582. "command": "ams_filament_setting",
  2583. "sequence_id": new_seq,
  2584. "result": "success",
  2585. }
  2586. )
  2587. assert mqtt_client._dev_mode_probe_failures == 0
  2588. assert mqtt_client.state.developer_mode is True
  2589. assert mqtt_client._dev_mode_probe_seq is None
  2590. def test_no_timeout_when_probe_not_sent(self, mqtt_client):
  2591. """The timeout branch is only entered when a probe is pending."""
  2592. # No probe sent — _dev_mode_probed is False, _dev_mode_probe_seq is None
  2593. data = {"gcode_state": "IDLE", "mc_percent": 0} # < 30 keys
  2594. mqtt_client._update_state(data)
  2595. assert mqtt_client._dev_mode_probe_failures == 0
  2596. def test_on_connect_resets_probe_state_but_preserves_developer_mode(self, mqtt_client):
  2597. """_on_connect resets probe tracking but preserves cached developer_mode."""
  2598. import time
  2599. mqtt_client._dev_mode_probed = True
  2600. mqtt_client._dev_mode_probe_seq = "42"
  2601. mqtt_client._dev_mode_probe_time = time.monotonic()
  2602. mqtt_client._dev_mode_probe_failures = 2
  2603. mqtt_client.state.developer_mode = True
  2604. # subscribe() must return (result, mid) tuple
  2605. mqtt_client._client.subscribe.return_value = (0, 1)
  2606. mqtt_client._on_connect(mqtt_client._client, None, None, 0)
  2607. # developer_mode is preserved across reconnects (#887)
  2608. assert mqtt_client.state.developer_mode is True
  2609. assert mqtt_client._dev_mode_probed is False
  2610. assert mqtt_client._dev_mode_probe_seq is None
  2611. assert mqtt_client._dev_mode_probe_time == 0.0
  2612. assert mqtt_client._dev_mode_probe_failures == 0
  2613. assert mqtt_client._connect_time > 0
  2614. def test_probe_deferred_when_connect_too_recent(self, mqtt_client):
  2615. """Probe is deferred if less than 5s have passed since _on_connect."""
  2616. import time
  2617. data = self._make_pushall_data()
  2618. # Set connect time to 1 second ago — too recent for probe
  2619. mqtt_client._connect_time = time.monotonic() - 1.0
  2620. mqtt_client._update_state(data)
  2621. # Pushall seen, so needs_probe is set, but probe NOT fired yet
  2622. assert mqtt_client._dev_mode_needs_probe is True
  2623. assert mqtt_client._dev_mode_probed is False
  2624. assert mqtt_client._dev_mode_probe_seq is None
  2625. def test_probe_fires_after_delay(self, mqtt_client):
  2626. """Probe fires once 5s have passed since _on_connect."""
  2627. import time
  2628. data = self._make_pushall_data()
  2629. # Set connect time to 6 seconds ago — delay satisfied
  2630. mqtt_client._connect_time = time.monotonic() - 6.0
  2631. mqtt_client._update_state(data)
  2632. # Probe should have fired
  2633. assert mqtt_client._dev_mode_needs_probe is True
  2634. assert mqtt_client._dev_mode_probed is True
  2635. assert mqtt_client._dev_mode_probe_seq is not None
  2636. def test_probe_fires_on_incremental_after_delay(self, mqtt_client):
  2637. """After seeing a pushall within 5s, probe fires on later incremental message."""
  2638. import time
  2639. pushall_data = self._make_pushall_data()
  2640. incremental_data = {"gcode_state": "IDLE", "mc_percent": 0} # < 30 keys
  2641. # Pushall arrives 1s after connect — too early for probe
  2642. mqtt_client._connect_time = time.monotonic() - 1.0
  2643. mqtt_client._update_state(pushall_data)
  2644. assert mqtt_client._dev_mode_needs_probe is True
  2645. assert mqtt_client._dev_mode_probed is False
  2646. # 5s later, an incremental update arrives — probe fires now
  2647. mqtt_client._connect_time = time.monotonic() - 6.0
  2648. mqtt_client._update_state(incremental_data)
  2649. assert mqtt_client._dev_mode_probed is True
  2650. assert mqtt_client._dev_mode_probe_seq is not None
  2651. def test_no_reprobe_when_developer_mode_cached(self, mqtt_client):
  2652. """Auto-reconnect preserves developer_mode, skipping reprobe."""
  2653. import time
  2654. data = self._make_pushall_data()
  2655. # Simulate known developer_mode from previous connection
  2656. mqtt_client.state.developer_mode = True
  2657. mqtt_client._connect_time = time.monotonic() - 10.0
  2658. mqtt_client._update_state(data)
  2659. # Should NOT probe — developer_mode is already known
  2660. assert mqtt_client._dev_mode_needs_probe is False
  2661. assert mqtt_client._dev_mode_probed is False
  2662. assert mqtt_client._dev_mode_probe_seq is None
  2663. assert mqtt_client.state.developer_mode is True
  2664. def test_on_connect_resets_needs_probe(self, mqtt_client):
  2665. """_on_connect resets _dev_mode_needs_probe for a clean start."""
  2666. mqtt_client._dev_mode_needs_probe = True
  2667. mqtt_client._client.subscribe.return_value = (0, 1)
  2668. mqtt_client._on_connect(mqtt_client._client, None, None, 0)
  2669. assert mqtt_client._dev_mode_needs_probe is False
  2670. class TestVtTrayNormalization:
  2671. """Tests for vt_tray dict→list normalization in _update_state.
  2672. MQTT sends vt_tray as a dict for single-slot printers, but all consumers
  2673. expect a list. _update_state must normalize it before any callback can
  2674. read raw_data, because the dev-mode probe may release the GIL and let
  2675. the event loop read the partially-updated state.
  2676. """
  2677. @pytest.fixture
  2678. def mqtt_client(self):
  2679. from backend.app.services.bambu_mqtt import BambuMQTTClient
  2680. client = BambuMQTTClient(
  2681. ip_address="192.168.1.100",
  2682. serial_number="TEST123",
  2683. access_code="12345678",
  2684. )
  2685. return client
  2686. def test_vt_tray_dict_normalized_in_update_state(self, mqtt_client):
  2687. """Verify _update_state wraps a raw vt_tray dict into a list."""
  2688. vt_dict = {
  2689. "id": "254",
  2690. "tray_color": "FF0000",
  2691. "tray_type": "PLA",
  2692. "tag_uid": "0000000000000000",
  2693. "tray_uuid": "00000000000000000000000000000000",
  2694. }
  2695. data = {"gcode_state": "IDLE", "vt_tray": vt_dict}
  2696. mqtt_client._update_state(data)
  2697. stored = mqtt_client.state.raw_data.get("vt_tray")
  2698. assert isinstance(stored, list)
  2699. assert len(stored) == 1
  2700. assert stored[0]["tray_color"] == "FF0000"
  2701. def test_vt_tray_list_unchanged_in_update_state(self, mqtt_client):
  2702. """Verify _update_state keeps an already-list vt_tray unchanged."""
  2703. vt_list = [
  2704. {"id": "254", "tray_type": "PLA"},
  2705. {"id": "255", "tray_type": "PETG"},
  2706. ]
  2707. data = {"gcode_state": "IDLE", "vt_tray": vt_list}
  2708. mqtt_client._update_state(data)
  2709. stored = mqtt_client.state.raw_data.get("vt_tray")
  2710. assert isinstance(stored, list)
  2711. assert len(stored) == 2
  2712. def test_preserved_vt_tray_restored_before_probe(self, mqtt_client):
  2713. """Verify preserved vt_tray is restored before dev-mode probe runs.
  2714. On the first message, the incremental handler wraps vt_tray into a list
  2715. and stores it. _update_state then replaces raw_data with the full data
  2716. dict, but must restore preserved fields BEFORE the probe publishes
  2717. (which can release the GIL).
  2718. """
  2719. # Simulate: incremental handler already stored a wrapped list
  2720. mqtt_client.state.raw_data = {
  2721. "vt_tray": [{"id": "254", "tray_type": "PLA", "tray_color": "00FF00"}],
  2722. }
  2723. # Now _update_state runs with new data that has vt_tray as dict
  2724. new_data = {
  2725. "gcode_state": "IDLE",
  2726. "vt_tray": {"id": "254", "tray_type": "PETG", "tray_color": "FF0000"},
  2727. }
  2728. mqtt_client._update_state(new_data)
  2729. # The preserved list (PLA/green) should take priority over new data
  2730. stored = mqtt_client.state.raw_data["vt_tray"]
  2731. assert isinstance(stored, list)
  2732. assert stored[0]["tray_type"] == "PLA"
  2733. assert stored[0]["tray_color"] == "00FF00"
  2734. def test_first_message_vt_tray_dict_becomes_list(self, mqtt_client):
  2735. """Verify on the very first message, vt_tray dict is still a list.
  2736. When there's no previously preserved data, the normalized dict should
  2737. remain as a list in raw_data.
  2738. """
  2739. # raw_data starts empty — no preserved vt_tray
  2740. mqtt_client.state.raw_data = {}
  2741. data = {
  2742. "gcode_state": "IDLE",
  2743. "vt_tray": {"id": "254", "tray_type": "ABS"},
  2744. }
  2745. mqtt_client._update_state(data)
  2746. stored = mqtt_client.state.raw_data["vt_tray"]
  2747. assert isinstance(stored, list)
  2748. assert stored[0]["tray_type"] == "ABS"
  2749. class TestSendDryingCommand:
  2750. """Tests for send_drying_command MQTT payload construction."""
  2751. @pytest.fixture
  2752. def mqtt_client(self):
  2753. """Create a BambuMQTTClient with a mock MQTT client."""
  2754. from unittest.mock import MagicMock
  2755. from backend.app.services.bambu_mqtt import BambuMQTTClient
  2756. client = BambuMQTTClient(
  2757. ip_address="192.168.1.100",
  2758. serial_number="TEST123",
  2759. access_code="12345678",
  2760. )
  2761. client._client = MagicMock()
  2762. return client
  2763. def test_rotate_tray_false_by_default(self, mqtt_client):
  2764. """Verify rotate_tray defaults to False in the MQTT payload."""
  2765. mqtt_client.send_drying_command(ams_id=0, temp=55, duration=4, mode=1, filament="PLA")
  2766. call_args = mqtt_client._client.publish.call_args
  2767. payload = json.loads(call_args[0][1])
  2768. assert payload["print"]["rotate_tray"] is False
  2769. def test_rotate_tray_true_when_enabled(self, mqtt_client):
  2770. """Verify rotate_tray is True when explicitly enabled."""
  2771. mqtt_client.send_drying_command(ams_id=0, temp=55, duration=4, mode=1, filament="PLA", rotate_tray=True)
  2772. call_args = mqtt_client._client.publish.call_args
  2773. payload = json.loads(call_args[0][1])
  2774. assert payload["print"]["rotate_tray"] is True
  2775. def test_rotate_tray_false_on_stop(self, mqtt_client):
  2776. """Verify rotate_tray is False when stopping drying (mode=0)."""
  2777. mqtt_client.send_drying_command(ams_id=0, temp=0, duration=0, mode=0)
  2778. call_args = mqtt_client._client.publish.call_args
  2779. payload = json.loads(call_args[0][1])
  2780. assert payload["print"]["rotate_tray"] is False
  2781. def test_all_required_fields_present(self, mqtt_client):
  2782. """Verify all required MQTT fields are present in the drying command."""
  2783. mqtt_client.send_drying_command(ams_id=128, temp=75, duration=8, mode=1, filament="ABS", rotate_tray=True)
  2784. call_args = mqtt_client._client.publish.call_args
  2785. payload = json.loads(call_args[0][1])
  2786. cmd = payload["print"]
  2787. assert cmd["command"] == "ams_filament_drying"
  2788. assert cmd["ams_id"] == 128
  2789. assert cmd["temp"] == 75
  2790. assert cmd["duration"] == 8
  2791. assert cmd["mode"] == 1
  2792. assert cmd["rotate_tray"] is True
  2793. assert cmd["filament"] == "ABS"
  2794. assert cmd["cooling_temp"] == 20
  2795. assert cmd["humidity"] == 0
  2796. assert cmd["close_power_conflict"] is False
  2797. assert "sequence_id" in cmd
  2798. def test_publishes_with_qos_1(self, mqtt_client):
  2799. """Verify drying commands are published with QoS 1."""
  2800. mqtt_client.send_drying_command(ams_id=0, temp=55, duration=4)
  2801. call_args = mqtt_client._client.publish.call_args
  2802. # qos may be positional arg [2] or keyword
  2803. qos = call_args.kwargs.get("qos", call_args[0][2] if len(call_args[0]) > 2 else None)
  2804. assert qos == 1
  2805. class TestStartPrintAmsMapping:
  2806. """Tests for ams_mapping/ams_mapping2 construction in start_print().
  2807. BambuStudio converts virtual tray IDs (254/255) to -1 in the flat
  2808. ams_mapping and puts the real external spool info only in ams_mapping2.
  2809. Passing raw 254/255 in the flat array causes H2D firmware to fail
  2810. with 0700_8012 "Failed to get AMS mapping table".
  2811. """
  2812. @pytest.fixture
  2813. def mqtt_client(self):
  2814. from unittest.mock import MagicMock
  2815. from backend.app.services.bambu_mqtt import BambuMQTTClient
  2816. client = BambuMQTTClient(
  2817. ip_address="192.168.1.100",
  2818. serial_number="TEST123",
  2819. access_code="12345678",
  2820. )
  2821. client._client = MagicMock()
  2822. client.state.connected = True
  2823. return client
  2824. def _get_published_command(self, mqtt_client):
  2825. """Extract the parsed print command from the last publish call."""
  2826. call_args = mqtt_client._client.publish.call_args
  2827. return json.loads(call_args[0][1])["print"]
  2828. def test_regular_ams_trays_preserved_in_flat_mapping(self, mqtt_client):
  2829. """Regular AMS tray IDs pass through unchanged in flat ams_mapping."""
  2830. mqtt_client.start_print("test.3mf", ams_mapping=[0, 5, 11])
  2831. cmd = self._get_published_command(mqtt_client)
  2832. assert cmd["ams_mapping"] == [0, 5, 11]
  2833. assert cmd["ams_mapping2"] == [
  2834. {"ams_id": 0, "slot_id": 0},
  2835. {"ams_id": 1, "slot_id": 1},
  2836. {"ams_id": 2, "slot_id": 3},
  2837. ]
  2838. def test_unmapped_slots(self, mqtt_client):
  2839. """Unmapped slots (-1) produce -1 in flat and 0xFF/0xFF in mapping2."""
  2840. mqtt_client.start_print("test.3mf", ams_mapping=[-1, -1])
  2841. cmd = self._get_published_command(mqtt_client)
  2842. assert cmd["ams_mapping"] == [-1, -1]
  2843. assert cmd["ams_mapping2"] == [
  2844. {"ams_id": 255, "slot_id": 255},
  2845. {"ams_id": 255, "slot_id": 255},
  2846. ]
  2847. def test_external_main_nozzle_becomes_minus_one_in_flat(self, mqtt_client):
  2848. """Virtual tray 255 (main nozzle) must be -1 in flat mapping."""
  2849. mqtt_client.start_print("test.3mf", ams_mapping=[255])
  2850. cmd = self._get_published_command(mqtt_client)
  2851. assert cmd["ams_mapping"] == [-1]
  2852. assert cmd["ams_mapping2"] == [{"ams_id": 255, "slot_id": 0}]
  2853. def test_single_nozzle_external_spool_uses_main_id(self, mqtt_client):
  2854. """Single-nozzle external spool (254) maps to ams_id=255 (VIRTUAL_TRAY_MAIN_ID).
  2855. Firmware reports tray_now=254 for external spool, but the print command
  2856. must use ams_id=255 in ams_mapping2. Sending 254 causes the firmware to
  2857. target AMS tray 0 instead of external spool (07FF_8012 error).
  2858. """
  2859. mqtt_client.start_print("test.3mf", ams_mapping=[254])
  2860. cmd = self._get_published_command(mqtt_client)
  2861. assert cmd["ams_mapping"] == [-1]
  2862. assert cmd["ams_mapping2"] == [{"ams_id": 255, "slot_id": 0}]
  2863. def test_h2d_external_spool_mixed_with_ams(self, mqtt_client):
  2864. """H2D scenario: AMS trays + unmapped + external deputy nozzle."""
  2865. # Reproduces the exact scenario from issue #797:
  2866. # 5-slot 3MF, only slot 5 assigned to external deputy nozzle (254)
  2867. mqtt_client.start_print("test.3mf", ams_mapping=[-1, -1, -1, -1, 255])
  2868. cmd = self._get_published_command(mqtt_client)
  2869. # Flat mapping: all -1 (external converted, unmapped stay -1)
  2870. assert cmd["ams_mapping"] == [-1, -1, -1, -1, -1]
  2871. # Detailed mapping: unmapped slots use 0xFF, external uses real ams_id
  2872. assert cmd["ams_mapping2"] == [
  2873. {"ams_id": 255, "slot_id": 255},
  2874. {"ams_id": 255, "slot_id": 255},
  2875. {"ams_id": 255, "slot_id": 255},
  2876. {"ams_id": 255, "slot_id": 255},
  2877. {"ams_id": 255, "slot_id": 0},
  2878. ]
  2879. def test_ams_ht_trays_preserved_in_flat_mapping(self, mqtt_client):
  2880. """AMS-HT tray IDs (>=128) pass through in flat mapping."""
  2881. mqtt_client.start_print("test.3mf", ams_mapping=[128, 131])
  2882. cmd = self._get_published_command(mqtt_client)
  2883. assert cmd["ams_mapping"] == [128, 131]
  2884. assert cmd["ams_mapping2"] == [
  2885. {"ams_id": 128, "slot_id": 0},
  2886. {"ams_id": 131, "slot_id": 0},
  2887. ]
  2888. def test_non_h2d_both_external_maps_to_main_id(self, mqtt_client):
  2889. """Non-H2D: both 254 and 255 map to ams_id=255 (single nozzle)."""
  2890. mqtt_client.start_print("test.3mf", ams_mapping=[254, 255])
  2891. cmd = self._get_published_command(mqtt_client)
  2892. assert cmd["ams_mapping"] == [-1, -1]
  2893. assert cmd["ams_mapping2"] == [
  2894. {"ams_id": 255, "slot_id": 0},
  2895. {"ams_id": 255, "slot_id": 0},
  2896. ]
  2897. def test_h2d_external_preserves_deputy_id(self, mqtt_client):
  2898. """H2D dual-nozzle: 254 (deputy) stays 254, 255 (main) stays 255."""
  2899. mqtt_client.model = "H2D"
  2900. mqtt_client.start_print("test.3mf", ams_mapping=[254, 255])
  2901. cmd = self._get_published_command(mqtt_client)
  2902. assert cmd["ams_mapping"] == [-1, -1]
  2903. assert cmd["ams_mapping2"] == [
  2904. {"ams_id": 254, "slot_id": 0},
  2905. {"ams_id": 255, "slot_id": 0},
  2906. ]
  2907. def test_h2d_single_external_deputy(self, mqtt_client):
  2908. """H2D: single external spool on deputy nozzle (254) keeps ams_id=254."""
  2909. mqtt_client.model = "H2D Pro"
  2910. mqtt_client.start_print("test.3mf", ams_mapping=[254])
  2911. cmd = self._get_published_command(mqtt_client)
  2912. assert cmd["ams_mapping"] == [-1]
  2913. assert cmd["ams_mapping2"] == [{"ams_id": 254, "slot_id": 0}]
  2914. def test_external_spool_only_sets_use_ams_false(self, mqtt_client):
  2915. """Single external spool on non-H2D printer sets use_ams=False."""
  2916. mqtt_client.start_print("test.3mf", ams_mapping=[254], use_ams=True)
  2917. cmd = self._get_published_command(mqtt_client)
  2918. assert cmd["use_ams"] is False
  2919. def test_all_unmapped_sets_use_ams_false(self, mqtt_client):
  2920. """All unmapped slots on non-H2D printer sets use_ams=False."""
  2921. mqtt_client.start_print("test.3mf", ams_mapping=[-1, -1], use_ams=True)
  2922. cmd = self._get_published_command(mqtt_client)
  2923. assert cmd["use_ams"] is False
  2924. def test_mixed_ams_and_external_keeps_use_ams_true(self, mqtt_client):
  2925. """AMS tray + external spool keeps use_ams=True."""
  2926. mqtt_client.start_print("test.3mf", ams_mapping=[0, 254], use_ams=True)
  2927. cmd = self._get_published_command(mqtt_client)
  2928. assert cmd["use_ams"] is True
  2929. def test_h2d_both_external_keeps_use_ams_true(self, mqtt_client):
  2930. """H2D with both external spools keeps use_ams=True (nozzle routing)."""
  2931. mqtt_client.model = "H2D"
  2932. mqtt_client.start_print("test.3mf", ams_mapping=[254, 255], use_ams=True)
  2933. cmd = self._get_published_command(mqtt_client)
  2934. assert cmd["use_ams"] is True
  2935. def test_empty_ams_mapping_keeps_use_ams_true(self, mqtt_client):
  2936. """Empty ams_mapping list does not override use_ams."""
  2937. mqtt_client.start_print("test.3mf", ams_mapping=[], use_ams=True)
  2938. cmd = self._get_published_command(mqtt_client)
  2939. assert cmd["use_ams"] is True
  2940. def test_no_ams_mapping_omits_fields(self, mqtt_client):
  2941. """When ams_mapping is None, neither field is in the command."""
  2942. mqtt_client.start_print("test.3mf", ams_mapping=None)
  2943. cmd = self._get_published_command(mqtt_client)
  2944. assert "ams_mapping" not in cmd
  2945. assert "ams_mapping2" not in cmd
  2946. def test_x2d_external_preserves_deputy_id(self, mqtt_client):
  2947. """X2D dual-nozzle (#988): 254 (deputy) stays 254, like H2D family.
  2948. X2D launched April 2026 and shares the H2D-style dual-extruder
  2949. firmware convention — external spool on the deputy (left) nozzle
  2950. is addressed as ams_id=254, not coerced to 255.
  2951. """
  2952. mqtt_client.model = "X2D"
  2953. mqtt_client.start_print("test.3mf", ams_mapping=[254, 255])
  2954. cmd = self._get_published_command(mqtt_client)
  2955. assert cmd["ams_mapping"] == [-1, -1]
  2956. assert cmd["ams_mapping2"] == [
  2957. {"ams_id": 254, "slot_id": 0},
  2958. {"ams_id": 255, "slot_id": 0},
  2959. ]
  2960. def test_x2d_uses_integer_format_for_calibration_fields(self, mqtt_client):
  2961. """X2D must use H2D-style integer (0/1) format for calibration fields (#988).
  2962. The reporter's support bundle showed X2D running firmware in the same
  2963. family as H2D. Booleans in these fields are interpreted as nozzle
  2964. indexes by H2D firmware; X2D is treated identically until proven
  2965. otherwise.
  2966. """
  2967. mqtt_client.model = "X2D"
  2968. mqtt_client.start_print(
  2969. "test.3mf",
  2970. timelapse=True,
  2971. bed_levelling=False,
  2972. flow_cali=True,
  2973. vibration_cali=False,
  2974. layer_inspect=True,
  2975. )
  2976. cmd = self._get_published_command(mqtt_client)
  2977. assert cmd["timelapse"] == 1
  2978. assert cmd["bed_leveling"] == 0
  2979. assert cmd["flow_cali"] == 1
  2980. assert cmd["vibration_cali"] == 0
  2981. assert cmd["layer_inspect"] == 1
  2982. def test_p2s_still_uses_boolean_format(self, mqtt_client):
  2983. """Regression guard: P2S is NOT in the is_h2d gate — must still use booleans.
  2984. Adding X2D to the is_h2d set must not accidentally affect P2S, which
  2985. is single-nozzle and uses boolean format like X1C/A1/P1.
  2986. """
  2987. mqtt_client.model = "P2S"
  2988. mqtt_client.start_print("test.3mf", timelapse=True, flow_cali=False)
  2989. cmd = self._get_published_command(mqtt_client)
  2990. assert cmd["timelapse"] is True
  2991. assert cmd["flow_cali"] is False
  2992. class TestStartPrintUniqueIdentityFields:
  2993. """Regression guard: project_id/subtask_id/task_id must be unique per submission (#1011).
  2994. Hardcoded "0" values caused third-party MQTT observers (e.g. OctoEverywhere)
  2995. to treat archive reprints as continuations of the same job and report
  2996. compounding durations on repeat replays. Each start_print call must produce
  2997. a distinct, non-zero identity triplet so the printer emits a fresh state
  2998. transition. md5 is deliberately left empty — historically firmware treats
  2999. "" as "skip validation" and we don't have the file's real digest here.
  3000. """
  3001. @pytest.fixture
  3002. def mqtt_client(self):
  3003. from unittest.mock import MagicMock
  3004. from backend.app.services.bambu_mqtt import BambuMQTTClient
  3005. client = BambuMQTTClient(
  3006. ip_address="192.168.1.100",
  3007. serial_number="TEST123",
  3008. access_code="12345678",
  3009. )
  3010. client._client = MagicMock()
  3011. client.state.connected = True
  3012. return client
  3013. def _get_published_command(self, mqtt_client):
  3014. call_args = mqtt_client._client.publish.call_args
  3015. return json.loads(call_args[0][1])["print"]
  3016. def test_identity_fields_are_non_zero(self, mqtt_client):
  3017. mqtt_client.start_print("test.3mf")
  3018. cmd = self._get_published_command(mqtt_client)
  3019. assert cmd["project_id"] != "0"
  3020. assert cmd["subtask_id"] != "0"
  3021. assert cmd["task_id"] != "0"
  3022. def test_identity_fields_are_all_equal_per_submission(self, mqtt_client):
  3023. """All three IDs come from the same submission timestamp — Studio also
  3024. uses a single identity per submission across the three fields."""
  3025. mqtt_client.start_print("test.3mf")
  3026. cmd = self._get_published_command(mqtt_client)
  3027. assert cmd["project_id"] == cmd["subtask_id"] == cmd["task_id"]
  3028. def test_md5_stays_empty(self, mqtt_client):
  3029. """Deliberate: synthetic md5 risks activating firmware validation."""
  3030. mqtt_client.start_print("test.3mf")
  3031. cmd = self._get_published_command(mqtt_client)
  3032. assert cmd["md5"] == ""
  3033. def test_identity_fields_change_between_submissions(self, mqtt_client):
  3034. """Two successive start_print calls must produce different IDs.
  3035. Without this, the printer can't tell replays apart and reuses
  3036. gcode_start_time from the prior job.
  3037. """
  3038. mqtt_client.start_print("test.3mf")
  3039. first = self._get_published_command(mqtt_client)
  3040. time.sleep(0.002)
  3041. mqtt_client.start_print("test.3mf")
  3042. second = self._get_published_command(mqtt_client)
  3043. assert first["task_id"] != second["task_id"]
  3044. assert first["subtask_id"] != second["subtask_id"]
  3045. assert first["project_id"] != second["project_id"]
  3046. def test_submission_id_is_numeric_string(self, mqtt_client):
  3047. """ID format: digits-only string. Studio uses cloud task IDs that are
  3048. also numeric-looking strings; the DB column is VARCHAR(64) and
  3049. Bambuddy's own subtask_id parser treats '0'/'' as absent — any valid
  3050. digit string that isn't '0' is fine."""
  3051. mqtt_client.start_print("test.3mf")
  3052. cmd = self._get_published_command(mqtt_client)
  3053. assert cmd["task_id"].isdigit()
  3054. assert int(cmd["task_id"]) > 0
  3055. assert len(cmd["task_id"]) <= 64
  3056. def test_submission_id_fits_signed_int32(self, mqtt_client):
  3057. """Regression for #1042: P1S firmware clamps oversized task identity
  3058. fields to signed int32 max (2**31-1 = 2147483647). If we send raw
  3059. epoch-ms (~1.7e12), the printer sees a saturated constant on every
  3060. submission and treats fresh dispatches as continuations of the last
  3061. FAILED job — never leaves IDLE. Keep below 2**31.
  3062. """
  3063. mqtt_client.start_print("test.3mf")
  3064. cmd = self._get_published_command(mqtt_client)
  3065. assert int(cmd["task_id"]) < 2**31
  3066. assert int(cmd["project_id"]) < 2**31
  3067. assert int(cmd["subtask_id"]) < 2**31
  3068. def test_unrelated_payload_fields_untouched(self, mqtt_client):
  3069. """Regression guard: fix only touches identity fields; everything else
  3070. (sequence_id, command verb, calibration defaults, profile_id) must be
  3071. unchanged to avoid silently breaking printer behavior."""
  3072. mqtt_client.start_print("test.3mf")
  3073. cmd = self._get_published_command(mqtt_client)
  3074. assert cmd["sequence_id"] == "20000"
  3075. assert cmd["command"] == "project_file"
  3076. assert cmd["param"] == "Metadata/plate_1.gcode"
  3077. assert cmd["url"] == "ftp://test.3mf"
  3078. assert cmd["file"] == "test.3mf"
  3079. assert cmd["profile_id"] == "0"
  3080. assert cmd["cfg"] == "0"
  3081. assert cmd["subtask_name"] == "test"
  3082. class TestDeleteKProfileDualNozzleDetection:
  3083. """Regression guard: dual-nozzle detection by serial prefix (#988).
  3084. delete_kprofile branches on serial-prefix-derived dual-nozzle status.
  3085. H2D serials start with "094"; X2D serials start with "20P9". Non-dual
  3086. families (X1C "00M", P1S "01P", P2S "22E", A1 "039", etc.) must take
  3087. the single-nozzle branch.
  3088. """
  3089. def _make_client(self, serial: str):
  3090. from unittest.mock import MagicMock
  3091. from backend.app.services.bambu_mqtt import BambuMQTTClient
  3092. client = BambuMQTTClient(
  3093. ip_address="192.168.1.100",
  3094. serial_number=serial,
  3095. access_code="12345678",
  3096. )
  3097. client._client = MagicMock()
  3098. client.state.connected = True
  3099. return client
  3100. def _published(self, client):
  3101. return json.loads(client._client.publish.call_args[0][1])["print"]
  3102. def test_h2d_serial_uses_dual_nozzle_format(self):
  3103. client = self._make_client("09400A000000001")
  3104. client.delete_kprofile(cali_idx=1, filament_id="GFA00", nozzle_id="HH00-0.4")
  3105. cmd = self._published(client)
  3106. # Dual-nozzle command omits setting_id.
  3107. assert "setting_id" not in cmd
  3108. assert cmd["extruder_id"] == 0
  3109. def test_x2d_serial_uses_dual_nozzle_format(self):
  3110. client = self._make_client("20P90A000000001")
  3111. client.delete_kprofile(cali_idx=1, filament_id="GFA00", nozzle_id="HH00-0.4")
  3112. cmd = self._published(client)
  3113. assert "setting_id" not in cmd
  3114. assert cmd["extruder_id"] == 0
  3115. def test_h2c_new_prefix_uses_dual_nozzle_format(self):
  3116. """Post-2026 H2C batches ship with '31B8B' prefix instead of '094' (#1105)."""
  3117. client = self._make_client("31B8BP000000001")
  3118. client.delete_kprofile(cali_idx=1, filament_id="GFA00", nozzle_id="HH00-0.4")
  3119. cmd = self._published(client)
  3120. assert "setting_id" not in cmd
  3121. assert cmd["extruder_id"] == 0
  3122. def test_p2s_serial_uses_single_nozzle_format(self):
  3123. """P2S is single-nozzle — must NOT take the dual-nozzle branch."""
  3124. client = self._make_client("22E00A000000001")
  3125. client.delete_kprofile(
  3126. cali_idx=1,
  3127. filament_id="GFA00",
  3128. nozzle_id="HH00-0.4",
  3129. setting_id="PFB123",
  3130. )
  3131. cmd = self._published(client)
  3132. # Single-nozzle command includes setting_id.
  3133. assert cmd["setting_id"] == "PFB123"
  3134. def test_x1c_serial_uses_single_nozzle_format(self):
  3135. client = self._make_client("00M00A000000001")
  3136. client.delete_kprofile(
  3137. cali_idx=1,
  3138. filament_id="GFA00",
  3139. nozzle_id="HH00-0.4",
  3140. setting_id="PFB123",
  3141. )
  3142. cmd = self._published(client)
  3143. assert cmd["setting_id"] == "PFB123"
  3144. class TestStaleReconnect:
  3145. """Tests for stale connection detection and reconnect without UI bouncing."""
  3146. @pytest.fixture
  3147. def mqtt_client(self):
  3148. from backend.app.services.bambu_mqtt import BambuMQTTClient
  3149. client = BambuMQTTClient(
  3150. ip_address="192.168.1.100",
  3151. serial_number="TEST_STALE",
  3152. access_code="12345678",
  3153. )
  3154. return client
  3155. def test_check_staleness_sets_flag_and_broadcasts_once(self, mqtt_client):
  3156. """check_staleness() should set connected=False, broadcast, and set _stale_reconnecting."""
  3157. import time
  3158. state_changes = []
  3159. mqtt_client.on_state_change = lambda s: state_changes.append(s.connected)
  3160. mqtt_client.state.connected = True
  3161. mqtt_client._last_message_time = time.time() - 120 # well past 60s threshold
  3162. result = mqtt_client.check_staleness()
  3163. assert result is False
  3164. assert mqtt_client.state.connected is False
  3165. assert mqtt_client._stale_reconnecting is True
  3166. assert state_changes == [False] # Exactly one broadcast
  3167. def test_check_staleness_noop_when_not_connected(self, mqtt_client):
  3168. """check_staleness() should not set flag when already disconnected."""
  3169. import time
  3170. mqtt_client.state.connected = False
  3171. mqtt_client._last_message_time = time.time() - 120
  3172. mqtt_client.check_staleness()
  3173. assert mqtt_client._stale_reconnecting is False
  3174. def test_check_staleness_noop_when_not_stale(self, mqtt_client):
  3175. """check_staleness() should not set flag when messages are recent."""
  3176. import time
  3177. mqtt_client.state.connected = True
  3178. mqtt_client._last_message_time = time.time() - 5 # 5s ago, well within 60s
  3179. result = mqtt_client.check_staleness()
  3180. assert result is True
  3181. assert mqtt_client.state.connected is True
  3182. assert mqtt_client._stale_reconnecting is False
  3183. def test_on_disconnect_skipped_during_stale_reconnect(self, mqtt_client):
  3184. """_on_disconnect should not broadcast state when _stale_reconnecting is set."""
  3185. state_changes = []
  3186. mqtt_client.on_state_change = lambda s: state_changes.append(s.connected)
  3187. mqtt_client._stale_reconnecting = True
  3188. mqtt_client.state.connected = False
  3189. mqtt_client._on_disconnect(None, None)
  3190. # No state change broadcast — check_staleness() already did it
  3191. assert state_changes == []
  3192. assert mqtt_client.state.connected is False
  3193. def test_on_disconnect_fires_event_during_stale_reconnect(self, mqtt_client):
  3194. """_on_disconnect must still fire _disconnection_event even during stale reconnect.
  3195. If disconnect() is called while _stale_reconnecting is True (e.g. user removes
  3196. the printer before paho reconnects), the event must fire so disconnect() doesn't hang.
  3197. """
  3198. import threading
  3199. mqtt_client._stale_reconnecting = True
  3200. mqtt_client._disconnection_event = threading.Event()
  3201. mqtt_client._on_disconnect(None, None)
  3202. assert mqtt_client._disconnection_event.is_set()
  3203. def test_on_connect_clears_stale_reconnecting_flag(self, mqtt_client):
  3204. """_on_connect should clear _stale_reconnecting and restore connected=True."""
  3205. mqtt_client._stale_reconnecting = True
  3206. mqtt_client.state.connected = False
  3207. subscribe_calls = []
  3208. mock_client = type(
  3209. "MockClient",
  3210. (),
  3211. {
  3212. "subscribe": lambda self, topic: subscribe_calls.append(topic) or (0, 1),
  3213. },
  3214. )()
  3215. mqtt_client._on_connect(mock_client, None, None, 0)
  3216. assert mqtt_client._stale_reconnecting is False
  3217. assert mqtt_client.state.connected is True
  3218. def test_full_stale_reconnect_cycle_no_bounce(self, mqtt_client):
  3219. """Full cycle: stale → disconnect callback → reconnect. UI should see exactly one disconnect."""
  3220. import time
  3221. state_changes = []
  3222. mqtt_client.on_state_change = lambda s: state_changes.append(s.connected)
  3223. mqtt_client.state.connected = True
  3224. mqtt_client._last_message_time = time.time() - 120
  3225. # Step 1: Stale detection triggers
  3226. mqtt_client.check_staleness()
  3227. assert state_changes == [False]
  3228. # Step 2: Paho fires disconnect callback (from socket close)
  3229. mqtt_client._on_disconnect(None, None)
  3230. # Should NOT add another state change
  3231. assert state_changes == [False]
  3232. # Step 3: Paho reconnects
  3233. subscribe_calls = []
  3234. mock_client = type(
  3235. "MockClient",
  3236. (),
  3237. {
  3238. "subscribe": lambda self, topic: subscribe_calls.append(topic) or (0, 1),
  3239. },
  3240. )()
  3241. mqtt_client._on_connect(mock_client, None, None, 0)
  3242. assert state_changes == [False, True] # Now connected again
  3243. assert mqtt_client._stale_reconnecting is False
  3244. def test_spurious_disconnect_suppressed_when_recent_messages(self, mqtt_client):
  3245. """Non-error disconnect with recent messages should be suppressed."""
  3246. import time
  3247. state_changes = []
  3248. mqtt_client.on_state_change = lambda s: state_changes.append(s.connected)
  3249. mqtt_client.state.connected = True
  3250. mqtt_client._last_message_time = time.time() - 3 # 3s ago
  3251. # Non-error disconnect (rc=None)
  3252. mqtt_client._on_disconnect(None, None)
  3253. assert state_changes == []
  3254. assert mqtt_client.state.connected is True
  3255. def test_error_disconnect_not_suppressed_despite_recent_messages(self, mqtt_client):
  3256. """Error disconnect should always be processed, even with recent messages."""
  3257. import time
  3258. import paho.mqtt.client as mqtt
  3259. from paho.mqtt.reasoncodes import ReasonCode
  3260. state_changes = []
  3261. mqtt_client.on_state_change = lambda s: state_changes.append(s.connected)
  3262. mqtt_client.state.connected = True
  3263. mqtt_client._last_message_time = time.time() - 3 # 3s ago
  3264. # Error disconnect (rc.is_failure = True)
  3265. rc = ReasonCode(mqtt.CONNACK >> 4, identifier=0x80) # Failure code
  3266. mqtt_client._on_disconnect(None, None, rc=rc)
  3267. assert state_changes == [False]
  3268. assert mqtt_client.state.connected is False
  3269. class TestDoorOpenParsing:
  3270. """Tests for enclosure door state parsing (X1 home_flag bit 23 vs others stat bit 23)."""
  3271. def _make_client(self, model: str):
  3272. from backend.app.services.bambu_mqtt import BambuMQTTClient
  3273. return BambuMQTTClient(
  3274. ip_address="192.168.1.100",
  3275. serial_number="TEST",
  3276. access_code="12345678",
  3277. model=model,
  3278. )
  3279. def test_x1c_door_open_from_home_flag(self):
  3280. client = self._make_client("X1C")
  3281. # bit 23 set
  3282. client._update_state({"home_flag": 0xC0E5CD98})
  3283. assert client.state.door_open is True
  3284. def test_x1c_door_closed_from_home_flag(self):
  3285. client = self._make_client("X1C")
  3286. client.state.door_open = True # start "open"
  3287. client._update_state({"home_flag": 0xC065CD98})
  3288. assert client.state.door_open is False
  3289. def test_x1c_ignores_stat_field(self):
  3290. # X1C must NOT use stat (bit 23 in stat is unrelated for X1)
  3291. client = self._make_client("X1C")
  3292. client._update_state({"home_flag": 0xC065CD98, "stat": "47A58000"})
  3293. assert client.state.door_open is False # home_flag wins
  3294. def test_h2d_door_open_from_stat(self):
  3295. client = self._make_client("H2D")
  3296. client._update_state({"stat": "640A58000"}) # bit 23 set
  3297. assert client.state.door_open is True
  3298. def test_h2d_door_closed_from_stat(self):
  3299. client = self._make_client("H2D")
  3300. client.state.door_open = True
  3301. client._update_state({"stat": "640258000"}) # bit 23 cleared
  3302. assert client.state.door_open is False
  3303. def test_h2d_ignores_home_flag(self):
  3304. # Non-X1 must NOT consume home_flag for door state
  3305. client = self._make_client("H2D")
  3306. client._update_state({"home_flag": 0xC0E5CD98, "stat": "640258000"})
  3307. assert client.state.door_open is False # stat wins
  3308. def test_invalid_stat_does_not_raise(self):
  3309. client = self._make_client("H2D")
  3310. client._update_state({"stat": "not-hex"})
  3311. assert client.state.door_open is False
  3312. class TestSdCardParsing:
  3313. """SD-card state is only set from the top-level `sdcard` field (bool/int/
  3314. string variants). home_flag is NOT consulted — heartbeat pushes clear those
  3315. bits even when a card is inserted, and the prior badge feature was removed
  3316. entirely because no reliable heartbeat-vs-full-push heuristic existed."""
  3317. def _make_client(self, model: str = "H2D"):
  3318. from backend.app.services.bambu_mqtt import BambuMQTTClient
  3319. return BambuMQTTClient(
  3320. ip_address="192.168.1.100",
  3321. serial_number="TEST",
  3322. access_code="12345678",
  3323. model=model,
  3324. )
  3325. def test_home_flag_alone_does_not_touch_sdcard(self):
  3326. client = self._make_client()
  3327. client.state.sdcard = True
  3328. for home_flag in (0x00000000, 0x00000100, 0x00000200):
  3329. client._update_state({"home_flag": home_flag})
  3330. assert client.state.sdcard is True
  3331. def test_sdcard_string_fallback_when_no_home_flag(self):
  3332. client = self._make_client()
  3333. client._update_state({"sdcard": "HAS_SDCARD_NORMAL"})
  3334. assert client.state.sdcard is True
  3335. def test_sdcard_int_fallback_when_no_home_flag(self):
  3336. # `1 is True` is False — the old strict check flapped here.
  3337. client = self._make_client()
  3338. client._update_state({"sdcard": 1})
  3339. assert client.state.sdcard is True
  3340. def test_sdcard_bool_fallback_when_no_home_flag(self):
  3341. client = self._make_client()
  3342. client._update_state({"sdcard": True})
  3343. assert client.state.sdcard is True
  3344. client._update_state({"sdcard": False})
  3345. assert client.state.sdcard is False
  3346. class TestZombieSessionDetection:
  3347. """Tests for ams_filament_setting response tracking (#887).
  3348. When a printer's MQTT session degrades so that telemetry flows but
  3349. published commands never reach the printer, the zombie detector
  3350. counts consecutive unanswered ams_filament_setting commands and
  3351. force-reconnects after two.
  3352. """
  3353. @pytest.fixture
  3354. def mqtt_client(self):
  3355. import time
  3356. from unittest.mock import MagicMock
  3357. from backend.app.services.bambu_mqtt import BambuMQTTClient
  3358. client = BambuMQTTClient(
  3359. ip_address="192.168.1.100",
  3360. serial_number="TEST123",
  3361. access_code="12345678",
  3362. )
  3363. client.state.connected = True
  3364. mock_paho = MagicMock()
  3365. mock_paho.socket.return_value = MagicMock()
  3366. client._client = mock_paho
  3367. client._connect_time = time.monotonic() - 10.0
  3368. # Set developer_mode so the dev-mode probe branch doesn't interfere
  3369. client.state.developer_mode = True
  3370. return client
  3371. def test_initial_state_is_clean(self, mqtt_client):
  3372. """Tracking fields start at zero / no pending command."""
  3373. assert mqtt_client._last_ams_cmd_time == 0.0
  3374. assert mqtt_client._ams_cmd_unanswered == 0
  3375. def test_publish_sets_pending_time(self, mqtt_client):
  3376. """set_ams_filament_setting records the publish timestamp."""
  3377. import time
  3378. before = time.monotonic()
  3379. mqtt_client.ams_set_filament_setting(
  3380. ams_id=0,
  3381. tray_id=0,
  3382. tray_info_idx="GFL99",
  3383. tray_type="PLA",
  3384. tray_sub_brands="",
  3385. tray_color="FF0000FF",
  3386. nozzle_temp_min=190,
  3387. nozzle_temp_max=230,
  3388. )
  3389. assert mqtt_client._last_ams_cmd_time >= before
  3390. def test_reset_slot_sets_pending_time(self, mqtt_client):
  3391. """reset_ams_slot also records the publish timestamp."""
  3392. import time
  3393. before = time.monotonic()
  3394. mqtt_client.reset_ams_slot(ams_id=0, tray_id=0)
  3395. assert mqtt_client._last_ams_cmd_time >= before
  3396. def test_response_clears_pending(self, mqtt_client):
  3397. """An ams_filament_setting response clears the pending state."""
  3398. import time
  3399. mqtt_client._last_ams_cmd_time = time.monotonic()
  3400. mqtt_client._ams_cmd_unanswered = 1
  3401. # Simulate receiving a user-command response (sequence_id "0")
  3402. print_data = {
  3403. "command": "ams_filament_setting",
  3404. "sequence_id": "0",
  3405. "result": "success",
  3406. }
  3407. # Walk the same path as _on_message: command response check then _update_state
  3408. cmd = print_data.get("command")
  3409. if cmd == "ams_filament_setting" and mqtt_client._last_ams_cmd_time > 0:
  3410. mqtt_client._last_ams_cmd_time = 0.0
  3411. mqtt_client._ams_cmd_unanswered = 0
  3412. assert mqtt_client._last_ams_cmd_time == 0.0
  3413. assert mqtt_client._ams_cmd_unanswered == 0
  3414. def test_single_timeout_increments_counter(self, mqtt_client):
  3415. """One unanswered command increments the counter but does not reconnect."""
  3416. import time
  3417. mqtt_client._last_ams_cmd_time = time.monotonic() - 11.0
  3418. mqtt_client._update_state({"gcode_state": "IDLE"})
  3419. assert mqtt_client._ams_cmd_unanswered == 1
  3420. assert mqtt_client._last_ams_cmd_time == 0.0
  3421. # Should NOT force-reconnect after just one
  3422. assert mqtt_client.state.connected is True
  3423. def test_two_timeouts_force_reconnect(self, mqtt_client):
  3424. """Two consecutive unanswered commands trigger force_reconnect.
  3425. Zombie detection runs from paho's network thread (no asyncio loop), so
  3426. the routing in force_reconnect_stale_session falls back to socket-close
  3427. — which is the safe option since loop_stop() from inside the loop
  3428. thread would deadlock. Hard-reset is reserved for async-context callers
  3429. (background_dispatch dispatch path)."""
  3430. import time
  3431. state_change_called = []
  3432. mqtt_client.on_state_change = lambda s: state_change_called.append(True)
  3433. # First unanswered command
  3434. mqtt_client._last_ams_cmd_time = time.monotonic() - 11.0
  3435. mqtt_client._update_state({"gcode_state": "IDLE"})
  3436. assert mqtt_client._ams_cmd_unanswered == 1
  3437. assert mqtt_client.state.connected is True
  3438. # Second unanswered command
  3439. mqtt_client._last_ams_cmd_time = time.monotonic() - 11.0
  3440. mqtt_client._update_state({"gcode_state": "IDLE"})
  3441. assert mqtt_client._ams_cmd_unanswered == 0 # reset after reconnect
  3442. assert mqtt_client.state.connected is False
  3443. assert mqtt_client._stale_reconnecting is True
  3444. # Sync test → no running loop → socket-close fallback path
  3445. mqtt_client._client.socket().close.assert_called()
  3446. assert len(state_change_called) > 0
  3447. def test_response_between_timeouts_resets_counter(self, mqtt_client):
  3448. """A successful response after one timeout resets the counter."""
  3449. import time
  3450. # First unanswered command
  3451. mqtt_client._last_ams_cmd_time = time.monotonic() - 11.0
  3452. mqtt_client._update_state({"gcode_state": "IDLE"})
  3453. assert mqtt_client._ams_cmd_unanswered == 1
  3454. # Now a response arrives — clear pending
  3455. mqtt_client._last_ams_cmd_time = time.monotonic()
  3456. mqtt_client._last_ams_cmd_time = 0.0
  3457. mqtt_client._ams_cmd_unanswered = 0
  3458. # Next unanswered command should be count=1, not count=2
  3459. mqtt_client._last_ams_cmd_time = time.monotonic() - 11.0
  3460. mqtt_client._update_state({"gcode_state": "IDLE"})
  3461. assert mqtt_client._ams_cmd_unanswered == 1
  3462. assert mqtt_client.state.connected is True # no reconnect
  3463. def test_late_response_after_watchdog_clears_counter_issue_1164(self, mqtt_client):
  3464. """Regression for #1164: a late ams_filament_setting response — one
  3465. that arrives AFTER the watchdog has already zeroed
  3466. `_last_ams_cmd_time` and incremented the unanswered counter — must
  3467. still reset the counter. Without this, a single sluggish response
  3468. leaves the counter armed at 1 indefinitely; the next slow response
  3469. on a totally unrelated command (possibly minutes or hours later)
  3470. takes it to 2 and force-reconnects, surfacing as 'AMS slot config
  3471. doesn't reach the printer ~6 changes in'."""
  3472. import time
  3473. # First command publishes, then doesn't get a response for >10s.
  3474. # Watchdog fires: counter=1, _last_ams_cmd_time zeroed.
  3475. mqtt_client._last_ams_cmd_time = time.monotonic() - 11.0
  3476. mqtt_client._update_state({"gcode_state": "IDLE"})
  3477. assert mqtt_client._ams_cmd_unanswered == 1
  3478. assert mqtt_client._last_ams_cmd_time == 0.0 # watchdog cleared it
  3479. # Late response arrives — the `_process_message` path used to require
  3480. # `_last_ams_cmd_time > 0` before resetting the counter, so this would
  3481. # have silently been ignored.
  3482. mqtt_client._process_message(
  3483. {
  3484. "print": {
  3485. "command": "ams_filament_setting",
  3486. "sequence_id": "0",
  3487. "result": "success",
  3488. "reason": "success",
  3489. }
  3490. }
  3491. )
  3492. # Counter MUST be reset — the response proves the channel is alive.
  3493. assert mqtt_client._ams_cmd_unanswered == 0, (
  3494. "Late ams_filament_setting response must reset the unanswered "
  3495. "counter even when the watchdog already zeroed _last_ams_cmd_time. "
  3496. "If this assertion fails the #1164 regression is back: a single "
  3497. "sluggish response will leave the counter armed and cause a "
  3498. "spurious force_reconnect on the next slow response."
  3499. )
  3500. # Now even if a future command times out, the counter starts fresh
  3501. # and a single timeout doesn't trip the 2x reconnect threshold.
  3502. mqtt_client._last_ams_cmd_time = time.monotonic() - 11.0
  3503. mqtt_client._update_state({"gcode_state": "IDLE"})
  3504. assert mqtt_client._ams_cmd_unanswered == 1
  3505. assert mqtt_client.state.connected is True # no force reconnect
  3506. def test_on_connect_resets_tracking(self, mqtt_client):
  3507. """_on_connect resets zombie tracking fields."""
  3508. import time
  3509. mqtt_client._last_ams_cmd_time = time.monotonic()
  3510. mqtt_client._ams_cmd_unanswered = 5
  3511. # subscribe() must return (result, mid) tuple
  3512. mqtt_client._client.subscribe.return_value = (0, 1)
  3513. mqtt_client._on_connect(mqtt_client._client, None, None, 0)
  3514. assert mqtt_client._last_ams_cmd_time == 0.0
  3515. assert mqtt_client._ams_cmd_unanswered == 0
  3516. def test_no_check_when_no_command_pending(self, mqtt_client):
  3517. """If no command was published, push_status does not trigger detection."""
  3518. assert mqtt_client._last_ams_cmd_time == 0.0
  3519. mqtt_client._update_state({"gcode_state": "IDLE"})
  3520. assert mqtt_client._ams_cmd_unanswered == 0
  3521. def test_no_timeout_within_window(self, mqtt_client):
  3522. """A command published <10s ago should not trigger a timeout."""
  3523. import time
  3524. mqtt_client._last_ams_cmd_time = time.monotonic() - 5.0
  3525. mqtt_client._update_state({"gcode_state": "IDLE"})
  3526. assert mqtt_client._ams_cmd_unanswered == 0
  3527. assert mqtt_client._last_ams_cmd_time > 0 # still pending
  3528. class TestHMSUserActionFiltering:
  3529. """HMS short codes the printer firmware emits during user-cancel sequences
  3530. must not appear in state.hms_errors — they're status echoes, not faults,
  3531. and shouldn't drive the printer card's "X problem" badge or red pip."""
  3532. @pytest.fixture
  3533. def mqtt_client(self):
  3534. from backend.app.services.bambu_mqtt import BambuMQTTClient
  3535. return BambuMQTTClient(
  3536. ip_address="192.168.1.100",
  3537. serial_number="TEST_HMS",
  3538. access_code="12345678",
  3539. )
  3540. def test_task_cancelled_echo_0300_400c_filtered(self, mqtt_client):
  3541. """0300_400C ("The task was canceled.") is the user-cancel echo that was
  3542. leaving the printer card stuck on "1 problem" after every stop."""
  3543. mqtt_client._update_state({"hms": [{"attr": 0x03000300, "code": 0x400C}]})
  3544. assert mqtt_client.state.hms_errors == []
  3545. def test_printing_cancelled_echo_0500_400e_filtered(self, mqtt_client):
  3546. """0500_400E ("Printing was cancelled.") — the corresponding nozzle-module
  3547. echo that the backend notification path was already suppressing for the
  3548. same reason."""
  3549. mqtt_client._update_state({"hms": [{"attr": 0x05000300, "code": 0x400E}]})
  3550. assert mqtt_client.state.hms_errors == []
  3551. def test_real_layer_shift_still_passes_through(self, mqtt_client):
  3552. """0300_4057 (Z-axis step loss) is a real fault and must NOT be filtered."""
  3553. mqtt_client._update_state({"hms": [{"attr": 0x03000100, "code": 0x4057}]})
  3554. assert len(mqtt_client.state.hms_errors) == 1
  3555. assert mqtt_client.state.hms_errors[0].code == "0x4057"
  3556. def test_filter_only_drops_user_action_codes_keeps_concurrent_real_faults(self, mqtt_client):
  3557. """When the user cancels mid-fault, the firmware sends the real fault HMS
  3558. alongside the cancel echo. Drop only the echo, keep the real fault."""
  3559. mqtt_client._update_state(
  3560. {
  3561. "hms": [
  3562. {"attr": 0x03000300, "code": 0x400C}, # cancel echo — drop
  3563. {"attr": 0x07FF0200, "code": 0x8011}, # filament runout — keep
  3564. ]
  3565. }
  3566. )
  3567. codes = [e.code for e in mqtt_client.state.hms_errors]
  3568. assert "0x8011" in codes
  3569. assert "0x400c" not in codes
  3570. assert len(mqtt_client.state.hms_errors) == 1
  3571. def test_print_error_path_also_filters_cancel_echo(self, mqtt_client):
  3572. """`print_error` is a second route that appends into state.hms_errors. The
  3573. same user-action codes (e.g. 0500_400E "Printing was cancelled") must be
  3574. filtered there too — otherwise the printer card stays on "1 problem"
  3575. when the firmware reports the cancel via print_error rather than hms[]."""
  3576. mqtt_client._update_state({"print_error": 0x0500_400E})
  3577. assert mqtt_client.state.hms_errors == []
  3578. def test_print_error_path_passes_real_errors_through(self, mqtt_client):
  3579. """Real print_error codes still reach state.hms_errors."""
  3580. mqtt_client._update_state({"print_error": 0x0500_8061})
  3581. assert len(mqtt_client.state.hms_errors) == 1
  3582. assert mqtt_client.state.hms_errors[0].code == "0x8061"
  3583. class TestForceReconnectRouting:
  3584. """#1136 — force_reconnect_stale_session routes between hard-reset (full
  3585. paho-client teardown, wipes the QoS 1 queue) and socket-close (the legacy
  3586. behaviour, safe to call from paho's own network thread). The routing
  3587. decision is based on whether an asyncio loop is running: hard-reset
  3588. requires loop_stop() which would deadlock if called from inside the
  3589. network thread itself."""
  3590. @pytest.fixture
  3591. def mqtt_client(self):
  3592. from unittest.mock import MagicMock
  3593. from backend.app.services.bambu_mqtt import BambuMQTTClient
  3594. client = BambuMQTTClient(
  3595. ip_address="192.168.1.100",
  3596. serial_number="TEST_HARD_RESET",
  3597. access_code="12345678",
  3598. )
  3599. client.state.connected = True
  3600. client._client = MagicMock()
  3601. return client
  3602. def test_routing_falls_back_to_socket_close_without_running_loop(self, mqtt_client):
  3603. """Sync caller → no asyncio loop → socket-close path (legacy behaviour
  3604. preserved for paho-thread callers like zombie detection)."""
  3605. mqtt_client.force_reconnect_stale_session("test")
  3606. mqtt_client._client.socket().close.assert_called()
  3607. # Old client is NOT torn down on this path; same-instance reconnect
  3608. # via paho's auto-reconnect handles it.
  3609. assert mqtt_client._client is not None
  3610. def test_routing_uses_hard_reset_when_loop_is_running(self, mqtt_client):
  3611. """Async caller → loop available → hard-reset path wipes the queue."""
  3612. import asyncio
  3613. original = mqtt_client._client
  3614. # Stub connect() so the rebuild doesn't open a real socket.
  3615. mqtt_client.connect = lambda loop=None: None
  3616. async def _trigger():
  3617. mqtt_client.force_reconnect_stale_session("test")
  3618. asyncio.run(_trigger())
  3619. original.disconnect.assert_called()
  3620. original.loop_stop.assert_called()
  3621. # connect() stub didn't repopulate _client, so it's None — the contract
  3622. # in production is that connect() builds a fresh mqtt.Client here.
  3623. assert mqtt_client._client is None
  3624. def test_marks_state_disconnected_and_broadcasts(self, mqtt_client):
  3625. """Both routing paths must broadcast the disconnected state once."""
  3626. broadcasts: list[bool] = []
  3627. mqtt_client.on_state_change = lambda s: broadcasts.append(s.connected)
  3628. mqtt_client.force_reconnect_stale_session("test")
  3629. assert mqtt_client.state.connected is False
  3630. assert mqtt_client._stale_reconnecting is True
  3631. assert broadcasts == [False]
  3632. class TestHardResetClientDirect:
  3633. """Lower-level coverage of `_hard_reset_client` itself — the helper called
  3634. by the routing layer when a full paho-client teardown is safe. These tests
  3635. drive the helper directly so they don't depend on the routing decision."""
  3636. @pytest.fixture
  3637. def mqtt_client(self):
  3638. from unittest.mock import MagicMock
  3639. from backend.app.services.bambu_mqtt import BambuMQTTClient
  3640. client = BambuMQTTClient(
  3641. ip_address="192.168.1.100",
  3642. serial_number="TEST_HARD_DIRECT",
  3643. access_code="12345678",
  3644. )
  3645. client.state.connected = True
  3646. client._client = MagicMock()
  3647. # Stub connect() so the rebuild doesn't open a real socket.
  3648. client.connect = lambda loop=None: None
  3649. return client
  3650. def test_disconnects_and_stops_old_client(self, mqtt_client):
  3651. """Old paho client must receive DISCONNECT (broker drops session) +
  3652. loop_stop (network thread exits, taking its QoS 1 queue with it)."""
  3653. original = mqtt_client._client
  3654. mqtt_client._hard_reset_client()
  3655. original.disconnect.assert_called()
  3656. original.loop_stop.assert_called()
  3657. def test_clears_client_reference(self, mqtt_client):
  3658. """Old reference must go to None so subsequent code can't accidentally
  3659. publish through the dying client."""
  3660. mqtt_client._hard_reset_client()
  3661. assert mqtt_client._client is None
  3662. def test_swallows_disconnect_exception(self, mqtt_client):
  3663. """A failing disconnect() (e.g. paho already in error state) must not
  3664. propagate — the await chain in background_dispatch.py would otherwise
  3665. raise instead of moving on, and a single broken client could brick
  3666. every future dispatch."""
  3667. original = mqtt_client._client
  3668. original.disconnect.side_effect = RuntimeError("boom")
  3669. # No exception escapes the call (test would fail if it did).
  3670. mqtt_client._hard_reset_client()
  3671. # loop_stop is still attempted after the disconnect failure.
  3672. original.loop_stop.assert_called()
  3673. assert mqtt_client._client is None
  3674. class TestStartPrintRecordsDispatchedPlate:
  3675. """Tests for the dispatched-plate record set by start_print() — used by the
  3676. /cover route to pick the right thumbnail when the printer's gcode_file
  3677. echo doesn't include the plate path (#1166).
  3678. Some firmware versions (P1S 01.10.00.00) only put the .3mf filename in
  3679. print.gcode_file, so the regex falls back to plate 1 and the printer card
  3680. shows the wrong plate's thumbnail. Recording what we dispatched at the
  3681. publish site lets resolve_plate_id() return the right plate without
  3682. needing to introspect the 3MF.
  3683. """
  3684. @pytest.fixture
  3685. def mqtt_client(self):
  3686. from unittest.mock import MagicMock
  3687. from backend.app.services.bambu_mqtt import BambuMQTTClient
  3688. client = BambuMQTTClient(
  3689. ip_address="192.168.1.100",
  3690. serial_number="TEST123",
  3691. access_code="12345678",
  3692. )
  3693. client._client = MagicMock()
  3694. client.state.connected = True
  3695. return client
  3696. def test_dispatched_plate_recorded_after_start_print(self, mqtt_client):
  3697. # Default state has no dispatched plate.
  3698. assert mqtt_client.state.dispatched_plate_id is None
  3699. assert mqtt_client.state.dispatched_subtask is None
  3700. mqtt_client.start_print("Luigi.3mf", plate_id=2)
  3701. # The subtask_name we record matches the one we send (and the printer
  3702. # reflects back via MQTT), so resolve_plate_id() can validate the
  3703. # match downstream.
  3704. assert mqtt_client.state.dispatched_plate_id == 2
  3705. assert mqtt_client.state.dispatched_subtask == "Luigi"
  3706. def test_dispatched_plate_default_is_one(self, mqtt_client):
  3707. # When start_print is called without plate_id (legacy/single-plate
  3708. # flow), we still record plate=1 — the contract is that dispatched_*
  3709. # describes the active dispatch.
  3710. mqtt_client.start_print("Single.3mf")
  3711. assert mqtt_client.state.dispatched_plate_id == 1
  3712. assert mqtt_client.state.dispatched_subtask == "Single"
  3713. def test_dispatched_plate_overwritten_by_subsequent_dispatch(self, mqtt_client):
  3714. # Each dispatch replaces the prior record so we can never serve a
  3715. # stale plate from an older print.
  3716. mqtt_client.start_print("First.3mf", plate_id=4)
  3717. mqtt_client.start_print("Second.3mf", plate_id=2)
  3718. assert mqtt_client.state.dispatched_plate_id == 2
  3719. assert mqtt_client.state.dispatched_subtask == "Second"
  3720. def test_dispatched_plate_not_recorded_when_publish_skipped(self, mqtt_client):
  3721. # If start_print early-returns because we're not connected, no record
  3722. # should land — otherwise the next print's /cover call would believe
  3723. # a phantom dispatch happened.
  3724. mqtt_client.state.connected = False
  3725. result = mqtt_client.start_print("Phantom.3mf", plate_id=3)
  3726. assert result is False
  3727. assert mqtt_client.state.dispatched_plate_id is None
  3728. assert mqtt_client.state.dispatched_subtask is None
  3729. class TestFilamentTrackSwitchDetection:
  3730. """Tests for Filament Track Switch (FTS) accessory detection (#1162).
  3731. The FTS is an accessory that sits between an AMS and the printer's
  3732. extruders, dynamically routing any slot to either nozzle. When installed,
  3733. each AMS unit reports info bits 8-11 = 0xE (uninitialized) since slots are
  3734. no longer tied to a specific extruder. Detection comes from the presence of
  3735. the print.device.fila_switch object in MQTT push_status.
  3736. """
  3737. @pytest.fixture
  3738. def mqtt_client(self):
  3739. from backend.app.services.bambu_mqtt import BambuMQTTClient
  3740. return BambuMQTTClient(
  3741. ip_address="192.168.1.100",
  3742. serial_number="TEST123",
  3743. access_code="12345678",
  3744. )
  3745. def test_fts_default_not_installed(self, mqtt_client):
  3746. """Without any MQTT data, fila_switch.installed must be False so the
  3747. frontend keeps applying the per-extruder filter on regular dual-nozzle
  3748. printers."""
  3749. assert mqtt_client.state.fila_switch.installed is False
  3750. assert mqtt_client.state.fila_switch.in_slots == []
  3751. assert mqtt_client.state.fila_switch.out_extruders == []
  3752. def test_fts_detected_from_device_fila_switch(self, mqtt_client):
  3753. """A push_status with print.device.fila_switch present must mark FTS
  3754. installed and capture its routing arrays. Mirrors the user's MQTT
  3755. bundle in #1162."""
  3756. data = {
  3757. "gcode_state": "RUNNING",
  3758. "device": {
  3759. "fila_switch": {
  3760. "in": [-1, 2],
  3761. "info": 2,
  3762. "out": [0, 1],
  3763. "stat": 0,
  3764. }
  3765. },
  3766. }
  3767. mqtt_client._update_state(data)
  3768. fs = mqtt_client.state.fila_switch
  3769. assert fs.installed is True
  3770. assert fs.in_slots == [-1, 2]
  3771. assert fs.out_extruders == [0, 1]
  3772. assert fs.stat == 0
  3773. assert fs.info == 2
  3774. def test_fts_absent_when_no_fila_switch_field(self, mqtt_client):
  3775. """A push_status that has device.* but no fila_switch must leave
  3776. fila_switch.installed = False — only that specific field flips it on."""
  3777. data = {
  3778. "gcode_state": "IDLE",
  3779. "device": {"extruder": {"state": 0}},
  3780. }
  3781. mqtt_client._update_state(data)
  3782. assert mqtt_client.state.fila_switch.installed is False
  3783. def test_fts_handles_missing_in_out_arrays(self, mqtt_client):
  3784. """If the firmware sends fila_switch with missing or non-list in/out,
  3785. we must still mark it installed (presence is the signal) and default
  3786. the arrays to empty lists rather than crashing."""
  3787. data = {
  3788. "gcode_state": "IDLE",
  3789. "device": {"fila_switch": {"stat": 0, "info": 0}},
  3790. }
  3791. mqtt_client._update_state(data)
  3792. fs = mqtt_client.state.fila_switch
  3793. assert fs.installed is True
  3794. assert fs.in_slots == []
  3795. assert fs.out_extruders == []
  3796. class TestAmsLoadFilamentEncoding:
  3797. """Per-target ams_change_filament command encoding (#891)."""
  3798. @pytest.fixture
  3799. def mqtt_client(self):
  3800. from unittest.mock import MagicMock
  3801. from backend.app.services.bambu_mqtt import BambuMQTTClient
  3802. client = BambuMQTTClient(
  3803. ip_address="192.168.1.100",
  3804. serial_number="TEST123",
  3805. access_code="12345678",
  3806. )
  3807. # Pretend the MQTT layer is connected so the publish path is reached.
  3808. client._client = MagicMock()
  3809. client.state.connected = True
  3810. return client
  3811. @staticmethod
  3812. def _published(client) -> dict:
  3813. """Return the JSON of the most recent publish() call."""
  3814. last_call = client._client.publish.call_args_list[-1]
  3815. topic, payload, *_ = last_call.args
  3816. return json.loads(payload)
  3817. def test_ams_slot_uses_local_index_and_minus_one_temps(self, mqtt_client):
  3818. """tray_id=5 → ams_id=1, slot_id=1, target=5, curr/tar=-1."""
  3819. assert mqtt_client.ams_load_filament(5) is True
  3820. cmd = self._published(mqtt_client)["print"]
  3821. assert cmd["command"] == "ams_change_filament"
  3822. assert cmd["ams_id"] == 1
  3823. assert cmd["slot_id"] == 1
  3824. assert cmd["target"] == 5
  3825. assert cmd["curr_temp"] == -1
  3826. assert cmd["tar_temp"] == -1
  3827. def test_external_left_keeps_legacy_encoding(self, mqtt_client):
  3828. """tray_id=254 → ams_id=255, slot_id=254, target=254, curr/tar=-1.
  3829. This is the original capture from a single-extruder printer; preserved
  3830. verbatim so existing single-external setups don't regress.
  3831. """
  3832. assert mqtt_client.ams_load_filament(254) is True
  3833. cmd = self._published(mqtt_client)["print"]
  3834. assert cmd["ams_id"] == 255
  3835. assert cmd["slot_id"] == 254
  3836. assert cmd["target"] == 254
  3837. assert cmd["curr_temp"] == -1
  3838. assert cmd["tar_temp"] == -1
  3839. def test_external_right_uses_extruder_index_and_actual_temp(self, mqtt_client):
  3840. """tray_id=255 → captured BambuStudio shape on dual-nozzle H2D:
  3841. ams_id=255, slot_id=0 (right extruder), target=255, curr/tar = right
  3842. nozzle temp.
  3843. """
  3844. # Simulate a heated right nozzle.
  3845. mqtt_client.state.temperatures["nozzle_2"] = 215.0
  3846. assert mqtt_client.ams_load_filament(255) is True
  3847. cmd = self._published(mqtt_client)["print"]
  3848. assert cmd["ams_id"] == 255
  3849. assert cmd["slot_id"] == 0
  3850. assert cmd["target"] == 255
  3851. assert cmd["curr_temp"] == 215
  3852. assert cmd["tar_temp"] == 215
  3853. def test_external_right_falls_back_when_nozzle_cold(self, mqtt_client):
  3854. """If the right nozzle reports < 180 °C, fall back to a sane default
  3855. so the printer accepts the command rather than rejecting it on a
  3856. nonsensical temperature.
  3857. """
  3858. mqtt_client.state.temperatures["nozzle_2"] = 25.0
  3859. assert mqtt_client.ams_load_filament(255) is True
  3860. cmd = self._published(mqtt_client)["print"]
  3861. assert cmd["curr_temp"] == 215
  3862. assert cmd["tar_temp"] == 215
  3863. def test_returns_false_when_disconnected(self, mqtt_client):
  3864. """Disconnected client must not publish anything."""
  3865. mqtt_client.state.connected = False
  3866. assert mqtt_client.ams_load_filament(0) is False
  3867. mqtt_client._client.publish.assert_not_called()