test_bambu_mqtt.py 214 KB

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