| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176 |
- import { describe, expect, it, vi } from "vitest";
- import { mkdtemp, readFile, rm, writeFile, mkdir } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import type { TerminalCapabilities } from "../../src/tui/platform/terminal-capabilities.js";
- import {
- TuiAltScreen,
- VStack,
- type Terminal,
- } from "../../src/tui/engine/public.js";
- import { createTuiApp } from "../../src/tui/app.js";
- import type {
- TuiModel,
- TuiQuestionnaireRequest,
- TuiQueuedMessage,
- TuiRuntimeEvent,
- TuiRuntime,
- TuiSessionForkPort,
- TuiStreamEvent,
- } from "../../src/runtime/port.js";
- import {
- normalizeTuiRuntimeEvent,
- type RawTuiRuntimeEvent,
- } from "../../src/runtime/event-normalizer.js";
- import type { SendMessageReq } from "@mavis/local-runtime-v2/cli-service";
- import type { TuiObservability } from "../../src/observability/local-observability.js";
- import type { McodeAuthProgress } from "../../src/auth/application.js";
- import { formatTuiShortcut } from "../../src/tui/shell/shortcut-labels.js";
- import { createTuiHostKeybindings } from "../../src/tui/shell/keybindings.js";
- import { stripAnsi } from "../../src/tui/rendering/text.js";
- import {
- TuiDraftRecovery,
- TuiDraftRecoveryError,
- } from "../../src/tui/features/composer/draft-recovery.js";
- import { composerText } from "../../src/tui/features/composer/copy.js";
- import type {
- McodeBusinessEvent,
- McodeBusinessTelemetry,
- } from "../../src/analytics/business-telemetry.js";
- import { VirtualTerminalScreen } from "../helpers/virtual-terminal.js";
- import { TuiFailure } from "../../src/failure.js";
- const runtimeEvent = (event: RawTuiRuntimeEvent): TuiRuntimeEvent =>
- normalizeTuiRuntimeEvent(event);
- function planReviewEventRequest(id: string) {
- return {
- schemaVersion: 2,
- id,
- mode: "plan",
- modePayload: {
- planReview: {
- markdown: "# Frozen plan\n\nImplement and verify the change.",
- path: "/history/session-1/artifacts/plan.md",
- },
- },
- presentation: {
- replaceComposer: true,
- showProgress: true,
- allowBackNavigation: true,
- },
- steps: [
- {
- id: "plan-review",
- question: "Approve?",
- selectionMode: "single",
- options: [{ id: "approve", label: "Approve" }],
- allowOther: true,
- required: true,
- },
- ],
- } as const;
- }
- function planEntryEventRequest(id: string) {
- return {
- schemaVersion: 2,
- id,
- mode: "plan",
- presentation: {
- replaceComposer: true,
- showProgress: true,
- allowBackNavigation: true,
- },
- steps: [
- {
- id: "plan-enter",
- question: "Enter Plan Mode for this task?",
- selectionMode: "single",
- options: [
- { id: "confirm", label: "Confirm" },
- { id: "decline", label: "Decline" },
- ],
- allowOther: true,
- otherPlaceholder: "Others...",
- required: true,
- },
- ],
- } as const;
- }
- function createObservability(): TuiObservability {
- return {
- recordStartup: vi.fn(),
- recordAccess: vi.fn(),
- recordEventStream: vi.fn(),
- recordTerminal: vi.fn(),
- recordProcessStop: vi.fn(),
- observeRender: vi.fn(),
- snapshot: vi.fn(() => ({
- filePath: "/tmp/mcode-observability.jsonl",
- eventCounts: {
- startup: 0,
- runtimeAccess: 0,
- eventStream: 0,
- terminal: 0,
- },
- accessSources: {},
- reconnects: 0,
- render: {
- frames: 0,
- averageDurationMs: 0,
- maxDurationMs: 0,
- p95DurationMs: 0,
- },
- })),
- flush: vi.fn(async () => undefined),
- };
- }
- class FakeTerminal implements Terminal {
- columns = 80;
- rows = 18;
- kittyProtocolActive = false;
- started = false;
- stopped = false;
- startCount = 0;
- stopCount = 0;
- drainCount = 0;
- title = "";
- titleUpdates: string[] = [];
- mouseTracking = false;
- mouseTrackingUpdates: boolean[] = [];
- writes: string[] = [];
- input?: (data: string) => void;
- start(onInput: (data: string) => void): void {
- this.started = true;
- this.stopped = false;
- this.startCount += 1;
- this.input = onInput;
- }
- stop(): void {
- this.stopped = true;
- this.stopCount += 1;
- }
- drainInput(): Promise<void> {
- this.drainCount += 1;
- return Promise.resolve();
- }
- write(data: string): void {
- this.writes.push(data);
- }
- moveBy(): void {}
- hideCursor(): void {}
- showCursor(): void {}
- clearLine(): void {}
- clearFromCursor(): void {}
- clearScreen(): void {}
- setProgress(): void {}
- setMouseTracking(enabled: boolean): void {
- this.mouseTracking = enabled;
- this.mouseTrackingUpdates.push(enabled);
- }
- setTitle(title: string): void {
- this.title = title;
- this.titleUpdates.push(title);
- }
- }
- function renderTerminalViewport(
- app: ReturnType<typeof createTuiApp>,
- terminal: FakeTerminal,
- ): string {
- app.tui.renderNow();
- const screen = new VirtualTerminalScreen(terminal.columns, terminal.rows);
- try {
- for (const write of terminal.writes) screen.feed(write);
- return screen.viewportText();
- } finally {
- screen.dispose();
- }
- }
- function createRuntime(): TuiRuntime {
- const abortSession = vi.fn(async () => true);
- let currentGoal: Awaited<ReturnType<TuiRuntime["getGoal"]>>;
- let createdSession:
- | {
- sessionId: string;
- title: string;
- workspaceDir: string;
- interactionMode?: "plan";
- }
- | undefined;
- const runtime: TuiRuntime = {
- createSession: vi.fn(async () => {
- createdSession = {
- sessionId: "session-1",
- title: "New session",
- workspaceDir: "/workspace",
- };
- return { ...createdSession };
- }),
- listSessions: vi.fn(async () => []),
- listSessionPage: vi.fn(async () => ({
- sessions: [],
- hasMore: false,
- })),
- getSession: vi.fn(async (sessionId: string) =>
- createdSession?.sessionId === sessionId
- ? { ...createdSession }
- : {
- sessionId,
- title: "Existing session",
- workspaceDir: "/workspace",
- },
- ),
- getMessages: vi.fn(async () => []),
- listMessagePage: vi.fn(async () => ({ messages: [], hasMore: false })),
- getSessionForkOptions: vi.fn(async () => ({
- canFork: true,
- worktreeVisible: false,
- worktreeEligible: false,
- })),
- forkSession: vi.fn(async () => ({
- session: {
- sessionId: "session-fork",
- parentSessionId: "session-1",
- title: "New session (fork 1)",
- workspaceDir: "/workspace",
- },
- })),
- getWorkspaceGitMetadata: vi.fn(async () => ({
- isGitRepo: false,
- branch: "",
- detached: false,
- isWorktree: false,
- })),
- renameSession: vi.fn(async (sessionId: string, title: string) => ({
- sessionId,
- title,
- workspaceDir: "/workspace",
- })),
- archiveSession: vi.fn(async () => undefined),
- listSessionInputSummaries: vi.fn(async () => []),
- getSessionRewindPreview: vi.fn(async () => ({ turns: [] })),
- rewindSession: vi.fn(async () => ({ rewound: true })),
- editSessionMessage: vi.fn(async () => ({
- rewound: true,
- turnId: "turn-edit-1",
- userMessageId: "msg-user-v1-edited",
- })),
- isGoalEnabled: vi.fn(() => true),
- getGoal: vi.fn(async () => currentGoal),
- createGoal: vi.fn(async ({ sessionId, objective, attachments }) => {
- currentGoal = {
- goalId: "goal-1",
- sessionId,
- objective,
- status: "active",
- createdAt: 10,
- updatedAt: 10,
- tokensUsed: 0,
- turnsUsed: 0,
- timeUsedSeconds: 0,
- tokenBudget: null,
- statusReason: null,
- hasKickoffAttachments: Boolean(attachments?.length),
- };
- return currentGoal;
- }),
- patchGoal: vi.fn(async (_sessionId, patch) => {
- if (!currentGoal) throw new Error("Goal not found");
- currentGoal = {
- ...currentGoal,
- ...patch,
- updatedAt: currentGoal.updatedAt + 1,
- };
- return currentGoal;
- }),
- clearGoal: vi.fn(async () => {
- const existed = Boolean(currentGoal);
- currentGoal = undefined;
- return existed;
- }),
- getInstructionSources: vi.fn(async () => []),
- getRuntimeDiagnostics: vi.fn(async () => ({
- status: "ok",
- surface: "cli-standalone",
- runtimeMode: "clean",
- runtimeOwnerKind: "cli",
- runtimeOwnerId: "minimax-code",
- dataDir: "/home/dev/.minimax",
- configPath: "/home/dev/.minimax/config.yaml",
- configPresent: true,
- authCachePresent: true,
- defaultModel: "minimax/MiniMax-M2.7",
- providerId: "minimax",
- modelId: "MiniMax-M2.7",
- authMode: "managed-login",
- authModeSource: "explicit",
- managedTokenPresent: true,
- apiKeyPresent: false,
- customProviderCount: 0,
- warnings: [],
- })),
- prepareFeedback: vi.fn(async ({ description, sessionId }) => ({
- schemaVersion: 1 as const,
- draftId: "feedback-draft-1",
- description,
- diagnostics: [
- { label: "Client", value: "mcode 0.1.0" },
- { label: "Runtime", value: "clean · cli" },
- { label: "Session", value: sessionId ?? "not included" },
- ],
- included: ["redacted feedback description"],
- excluded: ["credentials and tokens", "conversation prompts and messages"],
- expiresAtMs: Date.now() + 60_000,
- })),
- submitFeedback: vi.fn(async () => ({
- schemaVersion: 1 as const,
- ticketId: "ticket-1",
- status: "processing" as const,
- createdAtMs: Date.now(),
- })),
- cancelFeedback: vi.fn(async () => true),
- runDailyCheckin: vi.fn(async () => ({
- status: "claimed" as const,
- dayNo: 2,
- points: 20,
- expireAtMs: 1_800_000_000_000,
- panel: {
- scene: 2,
- days: Array.from({ length: 7 }, (_, index) => ({
- day_no: index + 1,
- points: (index + 1) * 10,
- status: index <= 1 ? 3 : 1,
- is_today: index === 1,
- })),
- },
- })),
- getAccountStatus: vi.fn(async () => ({
- status: "ready",
- defaultModel: "minimax/MiniMax-M2.7",
- providerId: "minimax",
- modelId: "MiniMax-M2.7",
- authMode: "managed-login",
- managedTokenPresent: true,
- warnings: [],
- })),
- listProviderPresets: vi.fn(async () => []),
- getCodexOAuthStatus: vi.fn(async () => ({
- state: "hidden" as const,
- providerId: "openai-codex" as const,
- })),
- startCodexOAuthLogin: vi.fn(async () => ({
- state: "hidden" as const,
- providerId: "openai-codex" as const,
- })),
- getPermissionMode: vi.fn(async () => "auto" as const),
- setPermissionMode: vi.fn(async (mode) => mode),
- getPlanModeCapabilities: vi.fn(async () => ({ entryEnabled: true })),
- getPendingQuestionnaire: vi.fn(async () => undefined),
- getLatestPlanReview: vi.fn(async () => undefined),
- replyQuestionnaire: vi.fn(async () => true),
- dismissQuestionnaire: vi.fn(async () => true),
- listPendingPermissions: vi.fn(async () => []),
- replyPermission: vi.fn(async () => true),
- listModels: vi.fn(async () => [
- {
- providerId: "minimax",
- modelId: "MiniMax-M2.7",
- displayName: "MiniMax M2.7",
- providerKind: "minimax-managed",
- selected: true,
- },
- ]),
- selectModel: vi.fn(async () => true),
- getSessionUsage: vi.fn(async () => ({
- summary: {
- inputTokens: 100,
- outputTokens: 20,
- totalTokens: 120,
- turns: 1,
- },
- rows: [],
- })),
- requestCompaction: vi.fn(async () => ({
- success: true,
- sessionId: "session-1",
- compactionId: "compact-1",
- messagesBefore: 12,
- messagesAfter: 4,
- tokensBefore: 4000,
- tokensAfter: 1200,
- })),
- getContextSnapshot: vi.fn(async () => ({ status: "empty" as const })),
- getActiveRun: vi.fn(async (sessionId: string) => ({
- schemaVersion: 1 as const,
- sessionId,
- state: "idle" as const,
- actions: { steer: false },
- })),
- getDelegationSnapshot: vi.fn(async (rootSessionId: string) => ({
- schemaVersion: 1 as const,
- rootSessionId,
- members: [],
- })),
- stopDelegation: vi.fn(async (rootSessionId: string) => ({
- schemaVersion: 1 as const,
- rootSessionId,
- rootStopped: await abortSession({ id: rootSessionId }),
- stoppedSessionIds: [],
- activeSessionIds: [],
- failedSessionIds: [],
- })),
- steer: vi.fn(async (input) => ({
- turnId: input.requestedTurnId ?? "turn-steer",
- mode: input.requestedTurnId
- ? ("steered" as const)
- : ("activated" as const),
- ...(input.requestedTurnId
- ? {}
- : {
- completion:
- (async function* completion(): AsyncGenerator<TuiStreamEvent> {
- yield { type: "done", turnId: "turn-steer" };
- })(),
- }),
- })),
- listSkills: vi.fn(async () => ({
- skills: [
- {
- name: "docs",
- displayName: "Docs",
- description: "Read project docs",
- enabled: true,
- sourceKind: "builtin",
- },
- ],
- hasMore: false,
- })),
- inspectProjectMcp: vi.fn(async () => undefined),
- listMcpServers: vi.fn(async () => [
- {
- name: "browser",
- enabled: true,
- transport: "stdio",
- description: "Browser tools",
- configJson: '{"env":{"TOKEN":"hidden"}}',
- },
- ]),
- listQueuedMessages: vi.fn(async () => []),
- getQueueSnapshot: vi.fn(async (sessionId: string) => {
- const items = await runtime.listQueuedMessages(sessionId);
- return {
- items,
- paused: items.some((item) => item.status === "paused"),
- pendingCount: items.length,
- };
- }),
- continueQueue: vi.fn(async () => undefined),
- steerQueuedMessage: vi.fn(async (_sessionId: string, itemId: string) => ({
- queueItemId: itemId,
- turnId: "turn-steered-queue",
- })),
- enqueueMessage: vi.fn(async () => ({
- itemId: "queue-1",
- status: "queued",
- position: 1,
- })),
- updateQueuedMessageContent: vi.fn(async () => undefined),
- deleteQueuedMessage: vi.fn(async () => undefined),
- watchEvents: vi.fn(async function* watchEvents(signal) {
- await new Promise<void>((resolve) => {
- signal.addEventListener("abort", resolve, { once: true });
- });
- for (const event of [] as TuiRuntimeEvent[]) yield event;
- }),
- watchSessionTurn: vi.fn(async function* watchSessionTurn() {}),
- sendMessage: vi.fn(
- async function* sendMessage(request): AsyncGenerator<TuiStreamEvent> {
- if (createdSession?.sessionId === request.id) {
- if (request.clientIntent === "plan-entry")
- createdSession.interactionMode = "plan";
- if (request.clientIntent === "plan-exit")
- delete createdSession.interactionMode;
- }
- yield { type: "delta", content: "Hello from the Agent" };
- yield { type: "done" };
- },
- ),
- abortSession,
- };
- return runtime;
- }
- describe("createTuiApp", () => {
- it.each(["regular", "fullscreen"] as const)(
- "configures /statusline from terminal input and reopens the saved selection in %s mode",
- async (tuiMode) => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const persistStatusLineItems = vi.fn(async () => undefined);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "test",
- workspaceDir: "/workspace",
- tuiMode,
- statusLineItems: ["current-dir"],
- persistStatusLineItems,
- });
- try {
- app.start();
- await app.ready;
- app.editor.setText("/statusline");
- terminal.input?.("\r");
- await vi.waitFor(() => expect(app.interaction.isActive()).toBe(true));
- expect(stripAnsi(app.interaction.render(80).join("\n"))).toContain(
- "[x] current-dir",
- );
- terminal.input?.(" ");
- terminal.input?.("\r");
- await vi.waitFor(() => expect(app.interaction.isActive()).toBe(false));
- expect(persistStatusLineItems).toHaveBeenCalledWith([]);
- app.editor.setText("/statusline");
- terminal.input?.("\r");
- await vi.waitFor(() => expect(app.interaction.isActive()).toBe(true));
- expect(stripAnsi(app.interaction.render(80).join("\n"))).toContain(
- "[ ] current-dir",
- );
- terminal.input?.("\x1b");
- expect(app.interaction.isActive()).toBe(false);
- expect(persistStatusLineItems).toHaveBeenCalledOnce();
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- } finally {
- await app.stop();
- }
- },
- );
- it.each([
- ["regular", false],
- ["regular", true],
- ["fullscreen", false],
- ["fullscreen", true],
- ] as const)(
- "runs ls unchanged in %s mode after empty-prefix Tab: %s",
- async (tuiMode, pressTab) => {
- const directory = await mkdtemp(join(tmpdir(), "tui-ls-enter-"));
- await mkdir(join(directory, "aaa-directory"));
- await writeFile(join(directory, "root-only.txt"), "root");
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: directory,
- tuiMode,
- });
- try {
- app.start();
- await app.ready;
- app.editor.setText("!ls");
- terminal.input?.(" ");
- expect(stripAnsi(app.editor.render(100).join("\n"))).not.toContain(
- "aaa-directory/",
- );
- if (pressTab) {
- terminal.input?.("\t");
- await vi.waitFor(() =>
- expect(stripAnsi(app.editor.render(100).join("\n"))).toContain(
- "aaa-directory/",
- ),
- );
- }
- expect(app.editor.getText()).toBe("!ls ");
- terminal.input?.("\r");
- await vi.waitFor(
- () =>
- expect(
- app.transcript.snapshot().find((cell) => cell.kind === "shell")
- ?.status,
- ).toBe("succeeded"),
- { timeout: 5000 },
- );
- expect(
- app.transcript.snapshot().find((cell) => cell.kind === "shell"),
- ).toMatchObject({
- title: "! ls",
- content: expect.stringContaining("root-only.txt"),
- });
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- } finally {
- await app.stop();
- await rm(directory, { recursive: true, force: true });
- }
- },
- );
- it.each(["regular", "fullscreen"] as const)(
- "completes a shell path from terminal input in %s mode before executing",
- async (tuiMode) => {
- const directory = await mkdtemp(
- join(tmpdir(), "tui-shell-completion-app-"),
- );
- await writeFile(
- join(directory, "report file.txt"),
- "completed-shell-output",
- );
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: directory,
- tuiMode,
- });
- try {
- app.start();
- await app.ready;
- app.editor.setText(
- process.platform === "win32" ? "!Get-Content " : "!cat ",
- );
- for (const char of "rep") terminal.input?.(char);
- expect(app.editor.getText()).toBe(
- process.platform === "win32" ? "!Get-Content rep" : "!cat rep",
- );
- terminal.input?.("\t");
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe(
- process.platform === "win32"
- ? "!Get-Content report` file.txt "
- : "!cat report\\ file.txt ",
- ),
- );
- expect(
- app.transcript
- .snapshot()
- .some((cell) => cell.id.startsWith("local:bash:")),
- ).toBe(false);
- terminal.input?.("\r");
- await vi.waitFor(
- () =>
- expect(
- app.transcript
- .snapshot()
- .some(
- (cell) =>
- cell.id.startsWith("local:bash:") &&
- cell.status === "succeeded" &&
- cell.content.includes("completed-shell-output"),
- ),
- ).toBe(true),
- { timeout: 5000 },
- );
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- } finally {
- await app.stop();
- await rm(directory, { recursive: true, force: true });
- }
- },
- );
- it("runs an editor ! command locally and carries its result into the next model message", async () => {
- const directory = await mkdtemp(join(tmpdir(), "tui-bash-app-"));
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: directory,
- });
- try {
- await app.ready;
- app.editor.setText(
- process.platform === "win32"
- ? "!Write-Output shell-result"
- : "!printf shell-result",
- );
- app.editor.submit();
- await vi.waitFor(
- () =>
- expect(
- app.transcript
- .snapshot()
- .some(
- (cell) =>
- cell.id.startsWith("local:bash:") &&
- cell.status === "succeeded",
- ),
- ).toBe(true),
- { timeout: 5000 },
- );
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- expect(
- app.transcript
- .snapshot()
- .find((cell) => cell.id.startsWith("local:bash:"))?.content,
- ).toContain("shell-result");
- expect(stripAnsi(app.tui.render(100).join("\n"))).toContain(
- "shell-result",
- );
- await app.submit("Explain the output");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- content: expect.stringContaining("shell-result"),
- }),
- expect.anything(),
- );
- } finally {
- await app.stop();
- await rm(directory, { recursive: true, force: true });
- }
- });
- it("shows shell output before the command exits and drains it when the app stops", async () => {
- const directory = await mkdtemp(join(tmpdir(), "tui-bash-stream-"));
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: directory,
- });
- try {
- await app.ready;
- const running = app.submit(
- process.platform === "win32"
- ? "!Write-Output streamed-result; Start-Sleep -Seconds 30"
- : "!printf streamed-result; sleep 30",
- );
- await vi.waitFor(
- () =>
- expect(
- app.transcript
- .snapshot()
- .some(
- (cell) =>
- cell.status === "running" &&
- cell.content.includes("streamed-result"),
- ),
- ).toBe(true),
- { timeout: 5000 },
- );
- expect(stripAnsi(app.tui.render(100).join("\n"))).toContain(
- "streamed-result",
- );
- await app.stop();
- await running;
- expect(
- app.transcript
- .snapshot()
- .find((cell) => cell.id.startsWith("local:bash:"))?.status,
- ).toBe("cancelled");
- } finally {
- await app.stop();
- await rm(directory, { recursive: true, force: true });
- }
- });
- it("continues a restored paused Queue from its manager without sending a new message", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let paused = true;
- vi.mocked(runtime.listQueuedMessages).mockImplementation(async () => [
- {
- itemId: "paused-follow-up",
- sessionId: "session-1",
- status: paused ? "paused" : "queued",
- content: "Existing queued work",
- },
- ]);
- vi.mocked(runtime.continueQueue).mockImplementation(async () => {
- paused = false;
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "test",
- workspaceDir: "/workspace",
- productFeatures: { queue: true },
- });
- app.start();
- try {
- await app.ready;
- await app.openSession("session-1");
- await app.submit("/queue");
- expect(app.tui.render(120).join("\n")).toContain("c continue queue");
- terminal.input?.("c");
- await vi.waitFor(() =>
- expect(runtime.continueQueue).toHaveBeenCalledWith("session-1"),
- );
- await vi.waitFor(() =>
- expect(app.tui.render(120).join("\n")).toContain("Queue continued."),
- );
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- expect(runtime.enqueueMessage).not.toHaveBeenCalled();
- expect(runtime.deleteQueuedMessage).not.toHaveBeenCalled();
- } finally {
- await app.stop();
- }
- });
- it.each(["keep", "clear", "cancel-enter", "cancel-escape"] as const)(
- "recovers a restored paused Queue when sending a message: %s",
- async (decision) => {
- const dataDir = await mkdtemp(join(tmpdir(), "tui-paused-queue-draft-"));
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let items: TuiQueuedMessage[] = [
- {
- itemId: "paused-follow-up",
- sessionId: "session-1",
- status: "paused",
- content: "Existing queued work",
- },
- ];
- let emitQueueChange:
- | ((event: TuiRuntimeEvent | undefined) => void)
- | undefined;
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* (signal) {
- const event = await new Promise<TuiRuntimeEvent | undefined>(
- (resolve) => {
- emitQueueChange = resolve;
- signal.addEventListener("abort", () => resolve(undefined), {
- once: true,
- });
- },
- );
- if (!event || signal.aborted) return;
- yield event;
- if (!signal.aborted) {
- await new Promise<void>((resolve) => {
- signal.addEventListener("abort", () => resolve(), { once: true });
- });
- }
- },
- );
- vi.mocked(runtime.listQueuedMessages).mockImplementation(async () => [
- ...items,
- ]);
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* (request, _signal, options) {
- if (!options?.pausedQueueAction) {
- throw new TuiFailure("runtime", "Queue paused", {
- code: "local_session_queue_paused",
- });
- }
- items =
- options.pausedQueueAction === "clear"
- ? []
- : items.map((item) => ({ ...item, status: "queued" }));
- emitQueueChange?.({
- type: "session.queue.updated",
- sessionId: "session-1",
- source: "runtime-v2",
- timestampMs: Date.now(),
- });
- yield { type: "done", turnId: request.turnId };
- },
- );
- const appOptions = {
- runtime,
- terminal,
- version: "test",
- dataDir,
- workspaceDir: "/workspace",
- productFeatures: { queue: true },
- };
- let app = createTuiApp(appOptions);
- app.start();
- try {
- await app.ready;
- await app.openSession("session-1");
- await vi.waitFor(() => expect(emitQueueChange).toBeTypeOf("function"));
- await vi.waitFor(() =>
- expect(app.tui.render(120).join("\n")).toContain(
- "Existing queued work",
- ),
- );
- terminal.input?.("Resume safely");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.tui.render(120).join("\n")).toContain("Keep my draft"),
- );
- const cancelled = decision.startsWith("cancel");
- if (decision === "cancel-escape") terminal.input?.("\u001b");
- else {
- if (decision === "clear") terminal.input?.("\u001b[B");
- if (decision === "cancel-enter") {
- terminal.input?.("\u001b[B");
- terminal.input?.("\u001b[B");
- }
- terminal.input?.("\r");
- }
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toBeUndefined(),
- );
- expect(runtime.enqueueMessage).not.toHaveBeenCalled();
- if (cancelled) {
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("Resume safely"),
- );
- expect(runtime.sendMessage).toHaveBeenCalledOnce();
- expect(runtime.continueQueue).not.toHaveBeenCalled();
- expect(runtime.deleteQueuedMessage).not.toHaveBeenCalled();
- expect(
- app.transcript.snapshot().filter((cell) => cell.kind === "user"),
- ).toEqual([]);
- expect(
- app.transcript.snapshot().filter((cell) => cell.kind === "error"),
- ).toEqual([]);
- expect(app.tui.render(120).join("\n")).toContain("Queue paused");
- expect(app.tui.render(120).join("\n")).not.toContain("Keep my draft");
- await vi.waitFor(async () => {
- const recovered = await new TuiDraftRecovery({
- dataDir,
- workspaceDir: "/workspace",
- sessionKey: "session-1",
- }).load();
- expect(recovered?.editor.text).toBe("Resume safely");
- expect(recovered?.retrySubmissions ?? []).toEqual([]);
- });
- app.editor.setText("Edited draft");
- await app.stop();
- app = createTuiApp(appOptions);
- app.start();
- await app.ready;
- await app.openSession("session-1");
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("Edited draft"),
- );
- expect(runtime.sendMessage).toHaveBeenCalledOnce();
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.tui.render(120).join("\n")).toContain("Keep my draft"),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledTimes(3),
- );
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toBeUndefined(),
- );
- expect(
- vi.mocked(runtime.sendMessage).mock.calls[2]?.[0],
- ).toMatchObject({
- content: "Edited draft",
- });
- expect(vi.mocked(runtime.sendMessage).mock.calls[2]?.[2]).toEqual({
- pausedQueueAction: "keep",
- });
- await vi.waitFor(() =>
- expect(app.tui.render(120).join("\n")).not.toContain(
- "Queue paused",
- ),
- );
- expect(app.editor.getText()).toBe("");
- expect(app.tui.render(120).join("\n")).toContain(
- "Existing queued work",
- );
- } else {
- expect(runtime.sendMessage).toHaveBeenCalledTimes(2);
- expect(
- vi.mocked(runtime.sendMessage).mock.calls[1]?.[0],
- ).toMatchObject({
- content: "Resume safely",
- });
- expect(vi.mocked(runtime.sendMessage).mock.calls[1]?.[2]).toEqual({
- pausedQueueAction: decision,
- });
- await vi.waitFor(() =>
- expect(app.tui.render(120).join("\n")).not.toContain(
- "Queue paused",
- ),
- );
- const rendered = app.tui.render(120).join("\n");
- if (decision === "clear")
- expect(rendered).not.toContain("Existing queued work");
- else expect(rendered).toContain("Existing queued work");
- }
- } finally {
- await app.stop();
- await rm(dataDir, { recursive: true, force: true });
- }
- },
- );
- it.each([
- ["plan-entry", "keep"],
- ["plan-entry", "clear"],
- ["plan-entry", "cancel"],
- ["plan-entry", "busy"],
- ["plan-exit", "keep"],
- ["plan-exit", "clear"],
- ["plan-exit", "cancel"],
- ["plan-exit", "busy"],
- ] as const)(
- "recovers a paused Queue with %s and decision %s",
- async (clientIntent, decision) => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let interactionMode: "plan" | undefined =
- clientIntent === "plan-exit" ? "plan" : undefined;
- let rejectRecovery = decision === "busy";
- vi.mocked(runtime.getSession).mockImplementation(async () => ({
- sessionId: "session-1",
- interactionMode,
- }));
- vi.mocked(runtime.listQueuedMessages).mockResolvedValue([
- {
- itemId: "paused-work",
- sessionId: "session-1",
- status: "paused",
- content: "Existing work",
- },
- ]);
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* (request, _signal, options) {
- if (!options?.pausedQueueAction || rejectRecovery) {
- throw new TuiFailure("runtime", "Admission rejected", {
- code: options?.pausedQueueAction
- ? "local_session_busy"
- : "local_session_queue_paused",
- });
- }
- interactionMode =
- request.clientIntent === "plan-entry" ? "plan" : undefined;
- yield { type: "done", turnId: request.turnId };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "test",
- workspaceDir: "/workspace",
- productFeatures: { queue: true },
- });
- app.start();
- try {
- await app.ready;
- await app.openSession("session-1");
- await app.submit(
- clientIntent === "plan-entry" ? "/plan on" : "/plan off",
- );
- terminal.input?.("Change mode and continue");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.tui.render(120).join("\n")).toContain("Keep my draft"),
- );
- if (decision === "clear" || decision === "cancel")
- terminal.input?.("\u001b[B");
- if (decision === "cancel") terminal.input?.("\u001b[B");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toBeUndefined(),
- );
- expect(runtime.enqueueMessage).not.toHaveBeenCalled();
- if (decision === "cancel" || decision === "busy") {
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("Change mode and continue"),
- );
- expect(interactionMode === "plan").toBe(clientIntent === "plan-exit");
- rejectRecovery = false;
- app.editor.setText("Edited mode instruction");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.tui.render(120).join("\n")).toContain("Keep my draft"),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toBeUndefined(),
- );
- }
- expect(runtime.sendMessage).toHaveBeenLastCalledWith(
- expect.objectContaining({ clientIntent }),
- expect.any(AbortSignal),
- { pausedQueueAction: decision === "clear" ? "clear" : "keep" },
- );
- expect(interactionMode === "plan").toBe(clientIntent === "plan-entry");
- expect(app.editor.getText()).toBe("");
- } finally {
- await app.stop();
- }
- },
- );
- it("shows the MCode prompt in an empty Composer and hides it after input", async () => {
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- try {
- app.start();
- await app.ready;
- expect(stripAnsi(app.tui.render(80).join("\n"))).toContain(
- "Ask Mcode to do anything",
- );
- app.editor.handleInput("R");
- expect(stripAnsi(app.tui.render(80).join("\n"))).not.toContain(
- "Ask Mcode to do anything",
- );
- expect(app.editor.getText()).toBe("R");
- } finally {
- await app.stop();
- }
- });
- it("copies the last Assistant reply to the client clipboard over SSH", async () => {
- vi.stubEnv("SSH_CONNECTION", "10.0.0.1 50100 10.0.0.2 22");
- vi.stubEnv("SSH_TTY", "/dev/pts/1");
- Object.defineProperty(process.stdin, "isTTY", {
- configurable: true,
- value: true,
- });
- Object.defineProperty(process.stdout, "isTTY", {
- configurable: true,
- value: true,
- });
- const terminal = new FakeTerminal();
- let app: ReturnType<typeof createTuiApp> | undefined;
- try {
- app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Say hello");
- await app.submit("/copy");
- expect(terminal.writes).toContain(
- "\u001B]52;c;SGVsbG8gZnJvbSB0aGUgQWdlbnQ=\u0007",
- );
- } finally {
- await app?.stop();
- Reflect.deleteProperty(process.stdin, "isTTY");
- Reflect.deleteProperty(process.stdout, "isTTY");
- vi.unstubAllEnvs();
- }
- });
- it("runs /goal through Runtime ownership and renders the active Goal above the Composer", async () => {
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/goal Ship TUI Goal support");
- expect(runtime.createSession).toHaveBeenCalledOnce();
- expect(runtime.createGoal).toHaveBeenCalledWith({
- sessionId: "session-1",
- objective: "Ship TUI Goal support",
- });
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- expect(app.surfaceHost.render(100).join("\n")).toContain("Goal · Active");
- expect(app.surfaceHost.render(100).join("\n")).toContain(
- "Ship TUI Goal support",
- );
- await app.submit("/goal pause");
- expect(runtime.patchGoal).toHaveBeenCalledWith("session-1", {
- status: "paused",
- });
- expect(app.surfaceHost.render(100).join("\n")).toContain("Goal · Paused");
- await app.stop();
- });
- it("wires complete chat and autocomplete business events into the TUI surface", async () => {
- const events: McodeBusinessEvent[] = [];
- const businessTelemetry: McodeBusinessTelemetry = {
- track: (event, properties) =>
- events.push({ event, properties } as McodeBusinessEvent),
- flush: async () => undefined,
- };
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace/agent-archon",
- businessTelemetry,
- });
- await app.ready;
- await app.submit("First prompt");
- await app.submit("Second prompt");
- for (const character of "/he") app.editor.handleInput(character);
- await vi.waitFor(() =>
- expect(
- events.some((item) => item.event === "slash_command_menu_view"),
- ).toBe(true),
- );
- app.editor.handleInput("\r");
- expect(events.filter((item) => item.event === "chat_send")).toEqual([
- {
- event: "chat_send",
- properties: {
- chat_type: "chat",
- is_first_message: 1,
- is_attachment: "text",
- },
- },
- {
- event: "chat_send",
- properties: {
- chat_type: "chat",
- is_first_message: 0,
- is_attachment: "text",
- },
- },
- ]);
- expect(events).toContainEqual({
- event: "slash_command_click",
- properties: {
- chat_type: "chat",
- command_type: "other",
- },
- });
- await app.stop();
- });
- it("releases the terminal on suspend and reconciles Runtime state after resume", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const requestProcessSuspend = vi.fn();
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- await new Promise<void>((resolve) => {
- signal.addEventListener("abort", resolve, { once: true });
- });
- yield* [];
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- requestProcessSuspend,
- });
- app.start();
- await app.ready;
- await app.openSession("session-1");
- await vi.waitFor(() => expect(runtime.watchEvents).toHaveBeenCalledOnce());
- terminal.input?.("\x1a");
- expect(requestProcessSuspend).toHaveBeenCalledOnce();
- await app.suspend();
- expect(terminal.stopCount).toBe(1);
- expect(app.controller.snapshot().status).not.toBe("error");
- await app.resume();
- expect(terminal.startCount).toBe(2);
- await vi.waitFor(() =>
- expect(runtime.watchEvents).toHaveBeenCalledTimes(2),
- );
- expect(runtime.getActiveRun).toHaveBeenCalled();
- await app.stop();
- });
- it.each(["regular", "fullscreen"] as const)(
- "restores %s Composer input after Ctrl+G returns from the external editor",
- async (tuiMode) => {
- const terminal = new FakeTerminal();
- const stopTerminal = vi.spyOn(terminal, "stop");
- const startTerminal = vi.spyOn(terminal, "start");
- const editDraftInExternalEditor = vi.fn(
- async () => "edited in the external editor",
- );
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- tuiMode,
- externalEditorCommand: "code --wait",
- editDraftInExternalEditor,
- });
- app.start();
- await app.ready;
- const longPaste = Array.from(
- { length: 12 },
- (_, index) => `line ${String(index + 1)}`,
- ).join("\n");
- terminal.input?.(`\x1b[200~${longPaste}\x1b[201~`);
- expect(app.editor.getText()).toBe("[paste #1 +12 lines]");
- terminal.input?.("\x07");
- await vi.waitFor(() =>
- expect(editDraftInExternalEditor).toHaveBeenCalledOnce(),
- );
- expect(editDraftInExternalEditor).toHaveBeenCalledWith({
- command: "code --wait",
- draft: longPaste,
- cwd: "/workspace",
- });
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("edited in the external editor"),
- );
- expect(stopTerminal).toHaveBeenCalledOnce();
- expect(startTerminal).toHaveBeenCalledTimes(2);
- terminal.input?.("!");
- expect(app.editor.getText()).toBe("edited in the external editor!");
- await app.stop();
- },
- );
- it("loads Runtime-owned Git context into the status rail without blocking startup", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.getWorkspaceGitMetadata).mockResolvedValue({
- isGitRepo: true,
- branch: "feat/status-context",
- detached: false,
- isWorktree: true,
- });
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/Users/dev/github/agent-archon",
- homeDir: "/Users/dev",
- });
- await app.ready;
- await vi.waitFor(() => {
- expect(runtime.getWorkspaceGitMetadata).toHaveBeenCalledWith(
- "/Users/dev/github/agent-archon",
- );
- const rendered = app.tui.render(120).join("\n");
- expect(rendered).toContain("~/github/agent-archon");
- expect(rendered).toContain("⎇ feat/status-context · worktree");
- });
- await app.stop();
- });
- it("keeps Pi clear-on-shrink disabled for the inline product by default", async () => {
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- expect(app.tui.getClearOnShrink()).toBe(false);
- await app.ready;
- await app.stop();
- });
- it("drains Pi terminal input before restoring the terminal on shutdown", async () => {
- const terminal = new FakeTerminal();
- const drainInput = vi.spyOn(terminal, "drainInput");
- const stopTerminal = vi.spyOn(terminal, "stop");
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.stop();
- expect(drainInput).toHaveBeenCalledWith(1000);
- expect(stopTerminal).toHaveBeenCalledOnce();
- expect(drainInput.mock.invocationCallOrder[0]).toBeLessThan(
- stopTerminal.mock.invocationCallOrder[0] ?? Number.POSITIVE_INFINITY,
- );
- });
- it("shows the Runtime initialization error text when the Session catalog is unavailable", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockRejectedValue(
- new Error("Unable to connect to the Runtime on port 1234"),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- expect(app.controller.snapshot()).toMatchObject({
- status: "error",
- error:
- "Couldn't load sessions: Unable to connect to the Runtime on port 1234. Retry.",
- sessions: [],
- });
- expect(app.tui.render(80).join("\n")).toContain(
- "Error · Couldn't load sessions:",
- );
- await app.stop();
- });
- it("shows an available update as a dedicated startup notice", async () => {
- const terminal = new FakeTerminal();
- const checkForUpdate = vi.fn(async () => ({ latestVersion: "1.2.4" }));
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "1.2.3",
- workspaceDir: "/workspace",
- checkForUpdate,
- });
- app.start();
- await app.ready;
- await vi.waitFor(() => {
- expect(terminal.writes.join("")).toContain(
- "A new version of MCode is available",
- );
- });
- const rendered = terminal.writes.join("");
- expect(checkForUpdate).toHaveBeenCalledTimes(1);
- expect(rendered).toContain("Run '/update' to install MCode 1.2.4");
- await app.submit("Start working");
- expect(app.getSurface()).toBe("conversation");
- expect(app.tui.render(80).join("\n")).not.toContain(
- "A new version of MCode is available",
- );
- await app.submit("/new");
- expect(app.getSurface()).toBe("welcome");
- expect(app.tui.render(80).join("\n")).toContain(
- "A new version of MCode is available",
- );
- await app.stop();
- });
- it("keeps the Welcome handoff out of the primary hint and submits it on Ctrl+U", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSkills).mockResolvedValue({
- skills: [
- {
- name: "resume-codex",
- displayName: "Resume Codex",
- description: "Continue a Codex session",
- enabled: true,
- sourceKind: "builtin",
- },
- ],
- hasMore: false,
- });
- const sessionId = "01a02373-25ae-76a1-880c-9253ba939cdc";
- const findRecentCodexSession = vi.fn(async () => ({
- sessionId,
- updatedAtMs: Date.now() - 60_000,
- source: "cli" as const,
- }));
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- findRecentCodexSession,
- });
- app.start();
- await app.ready;
- await vi.waitFor(() =>
- expect(findRecentCodexSession).toHaveBeenCalledOnce(),
- );
- const welcome = app.tui.render(100).join("\n");
- expect(welcome).toContain("Start");
- expect(welcome).not.toContain("Coming from Codex?");
- terminal.input?.("\x15");
- await vi.waitFor(() => expect(runtime.sendMessage).toHaveBeenCalledOnce());
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- id: "session-1",
- content: `/resume-codex ${sessionId}`,
- }),
- expect.any(AbortSignal),
- );
- expect(app.tui.render(100).join("\n")).not.toContain("Coming from Codex?");
- await app.stop();
- });
- it("invalidates an in-flight Codex handoff scan after the user starts typing", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSkills).mockResolvedValue({
- skills: [
- {
- name: "resume-codex",
- displayName: "Resume Codex",
- description: "Continue a Codex session",
- enabled: true,
- sourceKind: "builtin",
- },
- ],
- hasMore: false,
- });
- let resolveRecent:
- | ((session: {
- sessionId: string;
- updatedAtMs: number;
- source: "cli";
- }) => void)
- | undefined;
- const findRecentCodexSession = vi.fn(
- async () =>
- await new Promise<{
- sessionId: string;
- updatedAtMs: number;
- source: "cli";
- }>((resolve) => {
- resolveRecent = resolve;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- findRecentCodexSession,
- });
- app.start();
- terminal.input?.("x");
- await vi.waitFor(() =>
- expect(findRecentCodexSession).toHaveBeenCalledOnce(),
- );
- resolveRecent?.({
- sessionId: "01a02373-25ae-76a1-880c-9253ba939cdc",
- updatedAtMs: Date.now(),
- source: "cli",
- });
- await Promise.resolve();
- await Promise.resolve();
- expect(app.editor.getText()).toBe("x");
- expect(app.tui.render(100).join("\n")).not.toContain("Coming from Codex?");
- await app.stop();
- });
- it("does not inspect Codex storage when the resume Skill is unavailable", async () => {
- const runtime = createRuntime();
- const findRecentCodexSession = vi.fn(async () => ({
- sessionId: "01a02373-25ae-76a1-880c-9253ba939cdc",
- updatedAtMs: Date.now(),
- source: "cli" as const,
- }));
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- findRecentCodexSession,
- });
- app.start();
- await app.ready;
- await vi.waitFor(() => expect(runtime.listSkills).toHaveBeenCalledOnce());
- await Promise.resolve();
- expect(findRecentCodexSession).not.toHaveBeenCalled();
- expect(app.tui.render(100).join("\n")).not.toContain("Coming from Codex?");
- await app.stop();
- });
- it.each(["unsupported result", "blank version", "rejected check"] as const)(
- "keeps a %s startup update result out of the notice and transcript",
- async (scenario) => {
- const terminal = new FakeTerminal();
- const checkForUpdate = vi.fn(async () => {
- if (scenario === "rejected check")
- throw new Error("registry unavailable");
- if (scenario === "unsupported result") return undefined;
- return { latestVersion: " " };
- });
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "1.2.3",
- workspaceDir: "/workspace",
- checkForUpdate,
- });
- app.start();
- await app.ready;
- await vi.waitFor(() => expect(checkForUpdate).toHaveBeenCalledOnce());
- await app.stop();
- expect(terminal.writes.join("")).not.toContain(
- "A new version of MCode is available",
- );
- expect(
- app.transcript.snapshot().filter((cell) => cell.kind === "error"),
- ).toEqual([]);
- },
- );
- it("routes /update through the in-process update confirmation flow", async () => {
- const plan = {
- kind: "available" as const,
- source: "managed-installer" as const,
- currentVersion: "1.2.3",
- latestVersion: "1.2.4",
- channel: "stable" as const,
- };
- const inspectUpdate = vi.fn(async () => plan);
- const applyUpdate = vi.fn(async () => ({
- applied: true,
- message:
- "MCode 1.2.4 is installed. Restart running MCode sessions to use it.",
- }));
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "1.2.3",
- workspaceDir: "/workspace",
- inspectUpdate,
- applyUpdate,
- });
- await app.ready;
- await app.submit("/update");
- expect(inspectUpdate).toHaveBeenCalledOnce();
- expect(app.interaction.current()?.render(80).join("\n")).toContain(
- "MCode update",
- );
- app.interaction.current()?.handleInput?.("\r");
- await vi.waitFor(() =>
- expect(applyUpdate).toHaveBeenCalledWith(
- plan,
- expect.objectContaining({
- onOutput: expect.any(Function),
- }),
- ),
- );
- await vi.waitFor(() =>
- expect(
- app.transcript
- .snapshot()
- .some((cell) => cell.content.includes("MCode update completed")),
- ).toBe(true),
- );
- await app.stop();
- });
- it("leaves the current TUI after /update stages an npm-prefix replacement", async () => {
- const plan = {
- kind: "package-manager" as const,
- source: "npm-prefix" as const,
- currentVersion: "1.2.3",
- latestVersion: "1.2.4",
- packageTag: "latest" as const,
- command: {
- executable: "npm",
- args: ["install"],
- display: "npm install",
- },
- };
- const requestRestart = vi.fn();
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "1.2.3",
- workspaceDir: "/workspace",
- inspectUpdate: async () => plan,
- applyUpdate: async () => ({
- applied: true,
- restartRequired: true,
- message: "MCode 1.2.4 is staged safely.",
- }),
- requestRestart,
- });
- app.start();
- await app.ready;
- await app.submit("/update");
- app.interaction.current()?.handleInput?.("\r");
- await vi.waitFor(() => expect(requestRestart).toHaveBeenCalledOnce());
- await app.stopped;
- });
- it("rechecks local and Runtime work before applying an update", async () => {
- const runtime = createRuntime();
- const applyUpdate = vi.fn(async () => ({
- applied: true,
- message: "updated",
- }));
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "1.2.3",
- workspaceDir: "/workspace",
- inspectUpdate: async () => ({
- kind: "available",
- source: "managed-installer",
- currentVersion: "1.2.3",
- latestVersion: "1.2.4",
- channel: "stable",
- }),
- applyUpdate,
- });
- await app.ready;
- await app.openSession("session-1");
- vi.mocked(runtime.getActiveRun).mockResolvedValue({
- schemaVersion: 1,
- sessionId: "session-1",
- state: "running",
- turnId: "turn-active",
- actions: { steer: true },
- });
- await app.submit("/update");
- app.interaction.current()?.handleInput?.("\r");
- await vi.waitFor(() =>
- expect(
- app.transcript
- .snapshot()
- .some((cell) =>
- cell.content.includes(
- "MCode update failed: Finish or stop the active response before updating.",
- ),
- ),
- ).toBe(true),
- );
- expect(applyUpdate).not.toHaveBeenCalled();
- await app.stop();
- });
- it("fails the update gate closed when Runtime state cannot be verified", async () => {
- const runtime = createRuntime();
- const applyUpdate = vi.fn(async () => ({
- applied: true,
- message: "updated",
- }));
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "1.2.3",
- workspaceDir: "/workspace",
- inspectUpdate: async () => ({
- kind: "available",
- source: "managed-installer",
- currentVersion: "1.2.3",
- latestVersion: "1.2.4",
- channel: "stable",
- }),
- applyUpdate,
- });
- await app.ready;
- await app.openSession("session-1");
- vi.mocked(runtime.getActiveRun).mockRejectedValue(
- new Error("Runtime unavailable"),
- );
- await app.submit("/update");
- app.interaction.current()?.handleInput?.("\r");
- await vi.waitFor(() =>
- expect(
- app.transcript
- .snapshot()
- .some((cell) =>
- cell.content.includes("Unable to verify that this Session is idle"),
- ),
- ).toBe(true),
- );
- expect(applyUpdate).not.toHaveBeenCalled();
- await app.stop();
- });
- it("does not expose the TUI before base hydration has settled", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let releaseSessions: (() => void) | undefined;
- vi.mocked(runtime.listSessions).mockImplementation(
- async () =>
- await new Promise((resolve) => {
- releaseSessions = () => resolve([]);
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- let settled = false;
- void app.ready.then(() => {
- settled = true;
- });
- await Promise.resolve();
- expect(settled).toBe(false);
- releaseSessions?.();
- await app.ready;
- app.start();
- const rendered = app.tui.render(80).join("\n");
- expect(rendered).not.toContain("Starting MiniMax Code");
- expect(rendered).not.toContain("Loading session");
- await app.stop();
- });
- it("renders server startup in the welcome composer header until hydration settles", async () => {
- const runtime = createRuntime();
- let releaseSessions: (() => void) | undefined;
- vi.mocked(runtime.listSessions).mockImplementation(
- async () =>
- await new Promise((resolve) => {
- releaseSessions = () => resolve([]);
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- try {
- app.setStartupStatus("⠋ Starting server...");
- app.start();
- const starting = app.tui.render(80).join("\n");
- expect(starting).toContain("⠋ Starting server...");
- expect(starting).not.toContain("Start · @ file · / autocomplete");
- expect(starting).not.toContain("Loading session");
- releaseSessions?.();
- await app.ready;
- app.setStartupStatus(undefined);
- expect(app.tui.render(80).join("\n")).toContain(
- "Start · @ file · / autocomplete",
- );
- } finally {
- releaseSessions?.();
- await app.stop();
- }
- });
- it("waits for account hydration before allowing the first message", async () => {
- const runtime = createRuntime();
- let resolveAccount!: (
- account: Awaited<ReturnType<typeof runtime.getAccountStatus>>,
- ) => void;
- vi.mocked(runtime.getAccountStatus).mockReturnValue(
- new Promise((resolve) => {
- resolveAccount = resolve;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- try {
- const readiness = await Promise.race([
- app.ready.then(() => "ready" as const),
- new Promise<"blocked">((resolve) =>
- setTimeout(() => resolve("blocked"), 100),
- ),
- ]);
- expect(readiness).toBe("blocked");
- expect(runtime.createSession).not.toHaveBeenCalled();
- resolveAccount({
- status: "ready",
- defaultModel: "minimax/MiniMax-M2.7",
- managedTokenPresent: true,
- warnings: [],
- });
- await app.ready;
- const submission = app.submit("Send after account hydration");
- await submission;
- expect(runtime.createSession).toHaveBeenCalledWith({
- workspaceDir: "/workspace",
- });
- expect(app.transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- kind: "user",
- content: "Send after account hydration",
- }),
- expect.objectContaining({
- kind: "assistant",
- content: "Hello from the Agent",
- }),
- ]),
- );
- } finally {
- resolveAccount({
- status: "ready",
- defaultModel: "minimax/MiniMax-M2.7",
- managedTokenPresent: true,
- warnings: [],
- });
- await app.ready;
- await app.stop();
- }
- });
- it("does not wait for Token Plan membership hydration before App readiness", async () => {
- const runtime = createRuntime();
- const baseAccount = {
- status: "ready" as const,
- defaultModel: "minimax/MiniMax-M2.7",
- providerId: "minimax",
- modelId: "MiniMax-M2.7",
- authMode: "managed-login",
- managedTokenPresent: true,
- modelSource: "token-plan" as const,
- warnings: [],
- };
- const hydratedAccount = {
- ...baseAccount,
- tokenPlanQuotaState: "available" as const,
- tokenPlanQuota: {
- fiveHour: { remainingPercent: 90, unlimited: false },
- weekly: { remainingPercent: 80, unlimited: false },
- },
- };
- let resolveMembership!: (account: typeof hydratedAccount) => void;
- vi.mocked(runtime.getAccountStatus).mockImplementation(
- async (_sessionId, options) => {
- if (options?.includeMembership === true) {
- return await new Promise((resolve) => {
- resolveMembership = resolve;
- });
- }
- return baseAccount;
- },
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- try {
- await app.ready;
- expect(runtime.getAccountStatus).toHaveBeenCalledWith(undefined, {
- includeMembership: false,
- });
- expect(runtime.getAccountStatus).toHaveBeenCalledWith(undefined, {
- includeMembership: true,
- });
- expect(app.controller.snapshot().account).toMatchObject(baseAccount);
- resolveMembership(hydratedAccount);
- await vi.waitFor(() =>
- expect(app.controller.snapshot().account).toMatchObject({
- tokenPlanQuotaState: "available",
- }),
- );
- } finally {
- resolveMembership(hydratedAccount);
- await app.stop();
- }
- });
- it("waits for permission mode hydration before reporting App readiness", async () => {
- const runtime = createRuntime();
- let resolvePermissionMode!: (mode: "auto") => void;
- vi.mocked(runtime.getPermissionMode).mockReturnValue(
- new Promise((resolve) => {
- resolvePermissionMode = resolve;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- const readiness = Promise.race([
- app.ready.then(() => "ready" as const),
- new Promise<"blocked">((resolve) =>
- setTimeout(() => resolve("blocked"), 100),
- ),
- ]);
- await expect(readiness).resolves.toBe("blocked");
- resolvePermissionMode("auto");
- await app.ready;
- await app.stop();
- });
- it("accepts modeled account and optional permission degradation at the ready boundary", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.getAccountStatus).mockRejectedValue(
- new Error("account offline"),
- );
- vi.mocked(runtime.getPermissionMode).mockRejectedValue(
- new Error("config unavailable"),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await expect(app.ready).resolves.toBeUndefined();
- expect(app.controller.snapshot().account).toEqual({
- status: "unknown",
- warnings: [],
- });
- await app.stop();
- });
- it("hydrates the selected model display name into the status rail on startup", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.getAccountStatus).mockResolvedValue({
- status: "ready",
- defaultModel: "custom_provider:innerTest/MiniMax-M3",
- providerId: "custom_provider:innerTest",
- modelId: "MiniMax-M3",
- authMode: "byok",
- managedTokenPresent: true,
- warnings: [],
- });
- vi.mocked(runtime.listModels).mockResolvedValue([
- {
- providerId: "custom_provider:innerTest",
- modelId: "MiniMax-M3",
- displayName: "m3.05",
- selected: true,
- },
- ]);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await vi.waitFor(() =>
- expect(app.tui.render(120).join("\n")).toContain("✦ m3.05"),
- );
- await app.stop();
- });
- it("restores a managed-login failure to the Composer", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.getAccountStatus).mockResolvedValue({
- status: "ready",
- defaultModel: "minimax/MiniMax-M2.7",
- authMode: "managed-login",
- modelSource: "token-plan",
- managedTokenPresent: false,
- warnings: [],
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).toContain("Sign in with /login"),
- );
- terminal.input?.("Keep this until login");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("Keep this until login"),
- );
- expect(app.tui.render(80).join("\n")).toContain("Keep this until login");
- expect(app.transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- kind: "warning",
- content:
- "Sign in to MiniMax to use Agent features. Run /login, then retry.",
- }),
- ]),
- );
- expect(runtime.createSession).not.toHaveBeenCalled();
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- await app.stop();
- });
- it("renders an Enter submission before the asynchronous login preflight settles", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- let resolveAccount:
- | ((
- account: Awaited<ReturnType<typeof runtime.getAccountStatus>>,
- ) => void)
- | undefined;
- vi.mocked(runtime.getAccountStatus).mockImplementationOnce(
- () =>
- new Promise((resolve) => {
- resolveAccount = resolve;
- }),
- );
- const writesBeforeSubmission = terminal.writes.length;
- terminal.input?.("Show this immediately");
- terminal.input?.("\r");
- expect(app.editor.getText()).toBe("");
- expect(
- app.transcript.snapshot().filter((cell) => cell.kind === "user"),
- ).toEqual([
- expect.objectContaining({
- status: "pending",
- content: "Show this immediately",
- }),
- ]);
- await vi.waitFor(() =>
- expect(terminal.writes.length).toBeGreaterThan(writesBeforeSubmission),
- );
- expect(runtime.createSession).not.toHaveBeenCalled();
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- await vi.waitFor(() => expect(resolveAccount).toBeTypeOf("function"));
- resolveAccount?.({
- status: "ready",
- defaultModel: "minimax/MiniMax-M2.7",
- providerId: "minimax",
- modelId: "MiniMax-M2.7",
- authMode: "managed-login",
- managedTokenPresent: true,
- warnings: [],
- });
- await vi.waitFor(() => expect(runtime.sendMessage).toHaveBeenCalledOnce());
- await app.stop();
- });
- it("keeps a login-blocked submission editable after current-region sign-in", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let authenticated = false;
- vi.mocked(runtime.getAccountStatus).mockImplementation(async () =>
- authenticated
- ? {
- status: "ready",
- defaultModel: "minimax/MiniMax-M2.7",
- providerId: "minimax",
- modelId: "MiniMax-M2.7",
- authMode: "managed-login",
- modelSource: "token-plan",
- managedTokenPresent: true,
- warnings: [],
- }
- : {
- status: "needs-login",
- authMode: "managed-login",
- modelSource: "token-plan",
- managedTokenPresent: false,
- warnings: [],
- },
- );
- const auth = {
- login: vi.fn(async () => {
- authenticated = true;
- return {
- state: "authenticated" as const,
- message: "Signed in with MiniMax.",
- };
- }),
- logout: vi.fn(async () => ({
- state: "signed-out" as const,
- message: "Signed out of MiniMax.",
- })),
- };
- const app = createTuiApp({
- runtime,
- auth,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- terminal.input?.("Send this after login");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("Send this after login"),
- );
- await app.submit("/login");
- app.interaction.current()?.handleInput("\r");
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- app.editor.handleInput("\r");
- await vi.waitFor(() => expect(runtime.sendMessage).toHaveBeenCalledOnce());
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- id: "session-1",
- content: "Send this after login",
- turnId: expect.any(String),
- }),
- expect.any(AbortSignal),
- );
- expect(app.transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- kind: "user",
- content: "Send this after login",
- }),
- ]),
- );
- await app.stop();
- });
- it("runs /checkin immediately for a signed-in MiniMax account", async () => {
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/checkin");
- expect(runtime.runDailyCheckin).toHaveBeenCalledOnce();
- expect(app.transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- kind: "final-summary",
- content: expect.stringContaining(
- "Checked in: Cycle day 2, +20 Credits",
- ),
- }),
- ]),
- );
- await app.stop();
- });
- it("shows a generic /checkin failure without backend details", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.runDailyCheckin).mockRejectedValue(
- new Error("internal signin shard rejected request"),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/checkin");
- const transcript = app.transcript
- .snapshot()
- .map((cell) => cell.content)
- .join("\n");
- expect(transcript).toContain("Couldn't complete daily check-in.");
- expect(transcript).not.toContain("internal signin shard");
- await app.stop();
- });
- it("does not open login when /checkin cannot verify account status", async () => {
- const runtime = createRuntime();
- const auth = {
- login: vi.fn(),
- logout: vi.fn(async () => ({
- state: "signed-out" as const,
- message: "Signed out of MiniMax.",
- })),
- };
- const app = createTuiApp({
- runtime,
- auth,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- vi.mocked(runtime.getAccountStatus).mockRejectedValue(
- new Error("status unavailable"),
- );
- await app.submit("/checkin");
- expect(app.interaction.current()).toBeUndefined();
- expect(auth.login).not.toHaveBeenCalled();
- expect(runtime.runDailyCheckin).not.toHaveBeenCalled();
- await app.stop();
- });
- it("starts MiniMax login and resumes /checkin once after authentication", async () => {
- const runtime = createRuntime();
- let authenticated = false;
- vi.mocked(runtime.getAccountStatus).mockImplementation(async () => ({
- status: authenticated ? "ready" : "needs-login",
- authMode: "managed-login",
- modelSource: "byok",
- managedTokenPresent: authenticated,
- warnings: [],
- }));
- const auth = {
- login: vi.fn(async () => {
- authenticated = true;
- return {
- state: "authenticated" as const,
- message: "Signed in with MiniMax.",
- };
- }),
- logout: vi.fn(async () => ({
- state: "signed-out" as const,
- message: "Signed out of MiniMax.",
- })),
- };
- const app = createTuiApp({
- runtime,
- auth,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/checkin");
- expect(runtime.runDailyCheckin).not.toHaveBeenCalled();
- app.interaction.current()?.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.runDailyCheckin).toHaveBeenCalledOnce(),
- );
- expect(auth.login).toHaveBeenCalledWith(expect.any(Function), "cn");
- await app.stop();
- });
- it("does not resume /checkin when the login picker is cancelled", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.getAccountStatus).mockResolvedValue({
- status: "needs-login",
- authMode: "managed-login",
- modelSource: "byok",
- managedTokenPresent: false,
- warnings: [],
- });
- const auth = {
- login: vi.fn(),
- logout: vi.fn(async () => ({
- state: "signed-out" as const,
- message: "Signed out of MiniMax.",
- })),
- };
- const app = createTuiApp({
- runtime,
- auth,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/checkin");
- app.interaction.current()?.handleInput("\u001b");
- expect(auth.login).not.toHaveBeenCalled();
- expect(runtime.runDailyCheckin).not.toHaveBeenCalled();
- await app.stop();
- });
- it("does not resume /checkin when browser login fails", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.getAccountStatus).mockResolvedValue({
- status: "needs-login",
- managedTokenPresent: false,
- warnings: [],
- });
- const auth = {
- login: vi.fn(async () => {
- throw new Error("browser login failed");
- }),
- logout: vi.fn(async () => ({
- state: "signed-out" as const,
- message: "Signed out of MiniMax.",
- })),
- };
- const app = createTuiApp({
- runtime,
- auth,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/checkin");
- app.interaction.current()?.handleInput("\r");
- await vi.waitFor(() => expect(auth.login).toHaveBeenCalledOnce());
- expect(runtime.runDailyCheckin).not.toHaveBeenCalled();
- await app.stop();
- });
- it("carries /checkin through a cross-region login restart", async () => {
- const runtime = createRuntime();
- const requestRestart = vi.fn();
- vi.mocked(runtime.getAccountStatus).mockResolvedValue({
- status: "needs-login",
- managedTokenPresent: false,
- warnings: [],
- });
- const auth = {
- login: vi.fn(async () => ({
- state: "authenticated" as const,
- restartRequired: true,
- message: "Signed in with MiniMax Global.",
- })),
- logout: vi.fn(async () => ({
- state: "signed-out" as const,
- message: "Signed out of MiniMax.",
- })),
- };
- const app = createTuiApp({
- runtime,
- auth,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- requestRestart,
- });
- app.start();
- await app.ready;
- await app.submit("/checkin");
- app.interaction.current()?.handleInput("\u001b[B");
- app.interaction.current()?.handleInput("\r");
- await vi.waitFor(() =>
- expect(requestRestart).toHaveBeenCalledWith("en", "/checkin"),
- );
- expect(runtime.runDailyCheckin).not.toHaveBeenCalled();
- await app.stopped;
- });
- it("restarts after cross-region login while keeping the blocked submission", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const requestRestart = vi.fn();
- const notifyAuthContextChanged = vi.fn(async () => undefined);
- vi.mocked(runtime.getAccountStatus).mockResolvedValue({
- status: "needs-login",
- authMode: "managed-login",
- modelSource: "token-plan",
- managedTokenPresent: false,
- warnings: [],
- });
- const auth = {
- login: vi.fn(async () => ({
- state: "authenticated" as const,
- restartRequired: true,
- message: "Signed in with MiniMax Global.",
- })),
- logout: vi.fn(async () => ({
- state: "signed-out" as const,
- message: "Signed out of MiniMax.",
- })),
- };
- const app = createTuiApp({
- runtime,
- auth,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- requestRestart,
- notifyAuthContextChanged,
- });
- app.start();
- await app.ready;
- terminal.input?.("Keep this through restart");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("Keep this through restart"),
- );
- await app.submit("/login");
- app.interaction.current()?.handleInput("\u001b[B");
- app.interaction.current()?.handleInput("\r");
- await vi.waitFor(() => expect(auth.login).toHaveBeenCalledOnce());
- expect(auth.login).toHaveBeenCalledWith(expect.any(Function), "en");
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- await vi.waitFor(() => expect(requestRestart).toHaveBeenCalledOnce());
- expect(requestRestart).toHaveBeenCalledWith("en");
- expect(notifyAuthContextChanged).not.toHaveBeenCalled();
- await app.stopped;
- expect(app.editor.getText()).toBe("Keep this through restart");
- });
- it("offers MiniMax login without blocking an unauthenticated builtin BYOK Turn", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.getAccountStatus).mockResolvedValue({
- status: "ready",
- defaultModel: "minimax/MiniMax-M3",
- providerId: "minimax",
- authMode: "managed-login",
- modelSource: "byok",
- managedTokenPresent: false,
- warnings: [],
- });
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).toContain("Sign in with /login"),
- );
- await app.submit("BYOK still runs");
- expect(runtime.createSession).toHaveBeenCalledWith({
- workspaceDir: "/workspace",
- });
- expect(app.transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({ kind: "user", content: "BYOK still runs" }),
- expect.objectContaining({
- kind: "assistant",
- content: "Hello from the Agent",
- }),
- ]),
- );
- await app.stop();
- });
- it("starts a terminal-native workbench and submits messages through the Agent runtime", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- homeDir: "/home/dev",
- });
- app.start();
- await app.ready;
- expect(app.getSurface()).toBe("welcome");
- await app.submit("Say hello");
- expect(app.getSurface()).toBe("conversation");
- const conversation = app.tui.render(80).join("\n");
- expect(conversation).toContain("Tips for getting started");
- expect(conversation).toContain("Say hello");
- expect(conversation).toContain("Hello from the Agent");
- expect(terminal.started).toBe(true);
- expect(terminal.title).toBe("Minimax Code");
- expect(runtime.createSession).toHaveBeenCalledWith({
- workspaceDir: "/workspace",
- });
- expect(app.transcript.snapshot()).toEqual([
- expect.objectContaining({ kind: "user", content: "Say hello" }),
- expect.objectContaining({
- kind: "assistant",
- status: "succeeded",
- content: "Hello from the Agent",
- }),
- expect.objectContaining({ kind: "turn-duration", status: "succeeded" }),
- ]);
- await app.stop();
- expect(terminal.stopped).toBe(true);
- });
- it("syncs the generated Session title to the terminal without duplicate writes", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let sessionTitle = "New session";
- let emitEvent: ((event: TuiRuntimeEvent) => void) | undefined;
- vi.mocked(runtime.listSessions).mockImplementation(async () => [
- {
- sessionId: "session-1",
- title: sessionTitle,
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- const event = await new Promise<TuiRuntimeEvent | undefined>(
- (resolve) => {
- emitEvent = resolve;
- signal.addEventListener("abort", () => resolve(undefined), {
- once: true,
- });
- },
- );
- if (event) yield event;
- await new Promise<void>((resolve) => {
- signal.addEventListener("abort", resolve, { once: true });
- });
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Review uncommitted work");
- await vi.waitFor(() => expect(emitEvent).toBeTypeOf("function"));
- sessionTitle = "Review uncommitted work";
- emitEvent?.(
- runtimeEvent({
- type: "session.title_updated",
- timestamp: Date.now(),
- source: "runtime",
- payload: {
- sessionId: "session-1",
- title: sessionTitle,
- },
- }),
- );
- await vi.waitFor(() => expect(terminal.title).toBe(sessionTitle));
- await app.submit("/status");
- expect(terminal.titleUpdates).toEqual(["Minimax Code", sessionTitle]);
- await app.stop();
- });
- it("keeps chat state alive while a regular feature overlay owns the viewport", async () => {
- const terminal = new FakeTerminal();
- terminal.rows = 6;
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- const featureInput = vi.fn();
- const featureContent = {
- render: () => ["feature-header", "feature-body"],
- invalidate: () => undefined,
- };
- const feature = {
- id: "sessions",
- layoutRoot: new VStack([{ component: featureContent }]),
- render: featureContent.render,
- invalidate: () => undefined,
- handleInput: featureInput,
- };
- app.start();
- await app.ready;
- const screen = new VirtualTerminalScreen(terminal.columns, terminal.rows);
- let writeIndex = 0;
- const flushWrites = (): void => {
- for (; writeIndex < terminal.writes.length; writeIndex += 1) {
- screen.feed(terminal.writes[writeIndex] ?? "");
- }
- };
- flushWrites();
- app.editor.setText("preserved draft");
- const handle = app.surfaceHost.pushFeature({ screen: feature });
- app.tui.renderNow();
- flushWrites();
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "sessions",
- });
- expect(app.surfaceHost.render(80).join("\n")).toContain("preserved draft");
- expect(screen.viewportText()).toContain("feature-header");
- expect(screen.viewportText()).toContain("feature-body");
- expect(app.tui.mode).toBe("regular");
- expect(terminal.startCount).toBe(1);
- expect(terminal.stopCount).toBe(0);
- terminal.input?.("j");
- expect(featureInput).toHaveBeenCalledWith("j");
- expect(handle.close()).toBe(true);
- app.tui.renderNow();
- flushWrites();
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "chat",
- id: "chat",
- });
- expect(app.editor.getText()).toBe("preserved draft");
- expect(app.surfaceHost.render(80).join("\n")).toContain("preserved draft");
- expect(screen.viewportText()).toContain("preserved draft");
- expect(screen.viewportText()).not.toContain("feature-header");
- await app.stop();
- screen.dispose();
- });
- it("switches /settings through explicit choices and closes after applying", async () => {
- const terminal = new FakeTerminal();
- const persistTuiMode = vi.fn();
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.2.0",
- workspaceDir: "/workspace",
- tuiMode: "regular",
- persistTuiMode,
- });
- app.start();
- await app.ready;
- expect(app.surfaceHost.getChatMode()).toBe("regular");
- expect(app.tui.mode).toBe("regular");
- await app.submit("/settings");
- const fullscreenSettings = app.interaction.current();
- expect(app.tui.getFocusedComponent()).toBe(fullscreenSettings);
- expect(app.surfaceHost.render(80).join("\n")).toContain(
- "TUI mode · Regular",
- );
- terminal.input?.("\u001b[B");
- terminal.input?.("\r");
- expect(app.surfaceHost.getChatMode()).toBe("fullscreen");
- expect(app.tui.mode).toBe("fullscreen");
- expect(app.interaction.isActive()).toBe(false);
- expect(app.tui.getFocusedComponent()).toBe(app.editor);
- expect(persistTuiMode).toHaveBeenLastCalledWith("fullscreen");
- expect(app.editor.focused).toBe(true);
- terminal.input?.("/");
- await vi.waitFor(() =>
- expect(app.editor.render(60).join("\n")).toContain(
- "Show available commands",
- ),
- );
- terminal.input?.("\u001b");
- app.editor.setText("");
- await app.submit("/settings");
- const regularSettings = app.interaction.current();
- expect(app.tui.getFocusedComponent()).toBe(regularSettings);
- expect(app.surfaceHost.render(80).join("\n")).toContain(
- "TUI mode · Fullscreen",
- );
- terminal.input?.("\u001b[A");
- terminal.input?.("\r");
- expect(app.surfaceHost.getChatMode()).toBe("regular");
- expect(app.tui.mode).toBe("regular");
- expect(app.interaction.isActive()).toBe(false);
- expect(app.tui.getFocusedComponent()).toBe(app.editor);
- expect(persistTuiMode).toHaveBeenLastCalledWith("regular");
- await app.stop();
- });
- it("keeps the Pi Editor and Slash Command autocomplete active in fullscreen chat", async () => {
- const terminal = new FakeTerminal();
- terminal.rows = 8;
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.2.0",
- workspaceDir: "/workspace",
- tuiMode: "fullscreen",
- });
- app.start();
- await app.ready;
- terminal.input?.("/");
- await vi.waitFor(() =>
- expect(app.editor.render(60).join("\n")).toContain(
- "Show available commands",
- ),
- );
- expect(app.editor.getText()).toBe("/");
- terminal.input?.("\u001B[B");
- expect(app.editor.getText()).toBe("/");
- terminal.input?.("\u001B");
- expect(app.editor.getText()).toBe("/");
- expect(app.editor.focused).toBe(true);
- await app.stop();
- });
- it("renders regular-mode Slash Command autocomplete on the first keypress and every update", async () => {
- const terminal = new FakeTerminal();
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.2.0",
- workspaceDir: "/workspace",
- tuiMode: "regular",
- });
- app.start();
- await app.ready;
- const screen = new VirtualTerminalScreen(terminal.columns, terminal.rows);
- let writeIndex = 0;
- const flushWrites = (): void => {
- for (; writeIndex < terminal.writes.length; writeIndex += 1) {
- screen.feed(terminal.writes[writeIndex] ?? "");
- }
- };
- flushWrites();
- terminal.input?.("/");
- await vi.waitFor(() => {
- flushWrites();
- expect(screen.viewportText()).toContain("Show available commands");
- });
- expect(screen.viewportText()).toContain("/");
- terminal.input?.("m");
- await vi.waitFor(() => {
- flushWrites();
- expect(screen.viewportText()).toContain("Choose a model");
- });
- expect(screen.viewportText()).toContain("/m");
- screen.dispose();
- await app.stop();
- });
- it("resumes fullscreen tail following when submitting from scrolled history", async () => {
- const terminal = new FakeTerminal();
- terminal.rows = 6;
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.2.0",
- workspaceDir: "/workspace",
- tuiMode: "fullscreen",
- });
- app.start();
- await app.ready;
- for (let index = 1; index <= 4; index += 1) {
- await app.submit(`Prompt ${String(index)}`);
- }
- app.tui.renderNow();
- terminal.input?.("\u001B[5~");
- expect((app.tui as TuiAltScreen).isFollowingOutput).toBe(false);
- await app.submit("Latest prompt");
- expect((app.tui as TuiAltScreen).isFollowingOutput).toBe(true);
- await app.stop();
- });
- it("stops the terminal without waiting for a stalled Desktop event stream", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents() {
- await new Promise<void>(() => undefined);
- yield runtimeEvent({
- type: "unreachable",
- timestamp: 0,
- source: "test",
- payload: {},
- });
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const stopping = app.stop();
- await vi.waitFor(() => expect(terminal.stopped).toBe(true));
- await stopping;
- });
- it("restores the terminal before a stalled remote turn abort finishes", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishRemoteAbort: ((value: boolean) => void) | undefined;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage(_req: SendMessageReq, signal?: AbortSignal) {
- await new Promise<void>((resolve) => {
- signal?.addEventListener("abort", () => resolve(), { once: true });
- });
- yield { type: "done" };
- },
- );
- vi.mocked(runtime.abortSession).mockImplementation(
- async () =>
- await new Promise<boolean>((resolve) => {
- finishRemoteAbort = resolve;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const submitting = app.submit("Keep running");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().status).toBe("running"),
- );
- const stopping = app.stop();
- await vi.waitFor(() => expect(terminal.stopped).toBe(true));
- finishRemoteAbort?.(true);
- await Promise.all([submitting, stopping]);
- });
- it("stops the active turn before leaving the UI", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishTurn: (() => void) | undefined;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- yield {
- type: "delta",
- turnId: "turn-detached",
- role: "assistant",
- content: "Still running",
- };
- await new Promise<void>((resolve) => {
- finishTurn = resolve;
- });
- yield { type: "done", turnId: "turn-detached" };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const submitting = app.submit("Keep running after the UI leaves");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().status).toBe("running"),
- );
- await app.leaveUi();
- expect(terminal.stopped).toBe(true);
- expect(runtime.abortSession).toHaveBeenCalledWith(
- expect.objectContaining({ id: "session-1" }),
- );
- finishTurn?.();
- await submitting;
- });
- it("resumes a launcher-selected Session and exposes its ID for the exit hint", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- expect(app.controller.snapshot().session?.sessionId).toBeUndefined();
- await app.openSession("session-existing");
- expect(runtime.getSession).toHaveBeenCalledWith("session-existing");
- expect(runtime.getMessages).toHaveBeenCalledWith("session-existing");
- expect(app.controller.snapshot().session?.sessionId).toBe(
- "session-existing",
- );
- await app.leaveUi();
- });
- it("reports a launcher continue hydration failure in the Transcript", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-existing",
- title: "Existing session",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.getSession).mockRejectedValue(
- new Error("Session hydration failed"),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await expect(app.continueLatestSession()).rejects.toThrow(
- "Session hydration failed",
- );
- expect(app.transcript.snapshot().at(-1)).toMatchObject({
- kind: "error",
- content:
- "Couldn't continue the latest conversation: Session hydration failed. Retry after the connection recovers.",
- });
- await app.leaveUi();
- });
- it("can disable queue UI and preserve the next draft while a turn is running", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishTool: (() => void) | undefined;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- yield {
- type: "delta",
- turnId: "turn-running-tool",
- toolCalls: [
- {
- id: "read-1",
- name: "read",
- status: "running",
- input: { path: "src/cart/price.ts" },
- },
- ],
- };
- await new Promise<void>((resolve) => {
- finishTool = resolve;
- });
- yield { type: "done", turnId: "turn-running-tool" };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- productFeatures: { queue: false },
- });
- await app.ready;
- const submission = app.submit("Inspect the pricing logic");
- await vi.waitFor(() => {
- const rendered = app.tui.render(80).join("\n");
- expect(rendered).toContain("Reading (src/cart/price.ts)");
- expect(rendered).toContain("Running");
- expect(rendered).not.toContain("Running · 0s");
- expect(rendered).not.toContain("Running · Read");
- expect(rendered).not.toContain("MCode · Running");
- expect(rendered).toContain("Ctrl+O details");
- expect(rendered).toContain("Esc stop");
- expect(rendered).not.toContain("Enter run next");
- expect(rendered).not.toContain("Message · Enter send");
- expect(rendered).not.toContain("Follow-up");
- expect(rendered).not.toContain("waiting");
- });
- await app.submit("Review the tests next");
- expect(runtime.enqueueMessage).not.toHaveBeenCalled();
- expect(app.editor.getText()).toBe("Review the tests next");
- expect(app.tui.render(80).join("\n")).toContain("Esc to interrupt");
- finishTool?.();
- await submission;
- });
- it("refreshes the running queue hint from the same bindings as the hotkeys panel", async () => {
- const keybindings = createTuiHostKeybindings({
- platform: "linux",
- suspendSupported: true,
- });
- const runtime = createRuntime();
- let finishTurn: (() => void) | undefined;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- yield {
- type: "delta",
- turnId: "turn-custom-queue",
- thinking: "Inspecting the repository",
- };
- await new Promise<void>((resolve) => {
- finishTurn = resolve;
- });
- yield { type: "done", turnId: "turn-custom-queue" };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- keybindings: keybindings.registry,
- });
- await app.ready;
- keybindings.manager.setUserBindings({ "run.queue-draft": "ctrl+enter" });
- const submission = app.submit("Inspect the repository");
- try {
- await vi.waitFor(() => {
- const rendered = stripAnsi(app.tui.render(100).join("\n"));
- expect(rendered).toContain("Running");
- expect(rendered).toContain("Ctrl+Enter queue");
- expect(rendered).not.toContain("Alt+Enter queue");
- expect(
- keybindings.registry
- .helpRows()
- .find((row) => row.ids.includes("run.queue-draft"))?.keys,
- ).toBe("Ctrl+Enter");
- });
- } finally {
- finishTurn?.();
- await submission;
- await app.stop();
- }
- });
- it("shows running Thinking in chat and opens the full Transcript with /transcript", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishThinking: (() => void) | undefined;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- yield {
- type: "delta",
- turnId: "turn-running-thinking",
- thinking: "Inspecting the repository",
- };
- await new Promise<void>((resolve) => {
- finishThinking = resolve;
- });
- yield { type: "done", turnId: "turn-running-thinking" };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const submission = app.submit("Inspect the repository");
- const countThinkingBodyRows = (rendered: string): number =>
- rendered
- .split("\n")
- .filter(
- (line) =>
- line.includes("Inspecting the repository") &&
- !line.includes("MCode"),
- ).length;
- let expandedThinkingRows = 0;
- try {
- await vi.waitFor(() => {
- const thinking = app.transcript
- .snapshot()
- .find((cell) => cell.kind === "thinking");
- expect(thinking?.content).toBe("Inspecting the repository");
- expect(thinking?.expanded).toBeUndefined();
- const rendered = app.tui.render(80).join("\n");
- expect(rendered).toContain("Running");
- expect(rendered).not.toContain("Running · 0s");
- expect(rendered).toContain(`${formatTuiShortcut("alt+enter")} queue`);
- expect(rendered).toContain("Enter steer");
- expect(rendered).not.toContain(`${formatTuiShortcut("ctrl+x")} steer`);
- expect(rendered).toContain("Ctrl+O details");
- expect(rendered).toContain("Esc stop");
- expect(rendered).toContain("Thinking…");
- expect(rendered).not.toContain("MCode ·");
- expect(rendered).not.toContain("Thinking · Inspecting the repository");
- expandedThinkingRows = countThinkingBodyRows(rendered);
- expect(expandedThinkingRows).toBeGreaterThan(0);
- });
- await app.submit("/transcript");
- await vi.waitFor(() => {
- const rendered = renderTerminalViewport(app, terminal);
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "transcript",
- });
- expect(rendered).toContain("Transcript");
- expect(rendered).not.toContain("Inspecting the repository");
- expect(countThinkingBodyRows(rendered)).toBeLessThan(
- expandedThinkingRows,
- );
- });
- terminal.input?.("\x14");
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "transcript",
- });
- finishThinking?.();
- finishThinking = undefined;
- await submission;
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "transcript",
- });
- expect(app.controller.snapshot().activeTurnId).toBeUndefined();
- expect(
- app.transcript.snapshot().find((cell) => cell.kind === "thinking"),
- ).toMatchObject({
- status: "succeeded",
- });
- terminal.input?.("\u001B");
- await vi.waitFor(() => {
- const rendered = app.tui.render(80).join("\n");
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "chat",
- id: "chat",
- });
- expect(rendered).toContain("Message · Enter send");
- expect(rendered).not.toContain("Running");
- });
- } finally {
- finishThinking?.();
- await submission;
- await app.stop();
- }
- });
- it("toggles hidden and full inline Tool output with Ctrl+O", async () => {
- const terminal = new FakeTerminal();
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- app.transcript.upsert({
- id: "tool-detail-toggle",
- kind: "tool",
- status: "succeeded",
- title: "bash",
- content: '{"command":"pnpm test"}',
- detail: [
- "result line 1",
- "result line 2",
- "result line 3",
- "result line 4",
- ].join("\n"),
- createdAtMs: 1,
- });
- expect(app.tui.render(80).join("\n")).not.toContain("result line 1");
- expect(app.tui.render(80).join("\n")).not.toContain("result line 4");
- terminal.input?.("\x0f");
- const detailed = app.tui.render(80).join("\n");
- expect(detailed).toContain("result line 1");
- expect(detailed).toContain("result line 3");
- expect(detailed).toContain("result line 4");
- expect(detailed).not.toContain("Details expanded");
- terminal.input?.("\x0f");
- const compact = app.tui.render(80).join("\n");
- expect(compact).not.toContain("result line 1");
- expect(compact).not.toContain("result line 3");
- expect(compact).not.toContain("result line 4");
- expect(compact).not.toContain("Details collapsed");
- await app.stop();
- });
- it("uses Ctrl+T for tasks while Ctrl+O remains scoped to transcript details", async () => {
- const terminal = new FakeTerminal();
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- app.controller.applyRuntimeTurnEvent("turn-todo", {
- type: "generic",
- eventType: "todo_updated",
- turnId: "turn-todo",
- data: {
- todos: [
- { content: "Inspect Desktop behavior", status: "completed" },
- { content: "Polish the task rail", status: "in_progress" },
- { content: "Run focused tests", status: "pending" },
- { content: "Review narrow layouts", status: "pending" },
- { content: "Update user documentation", status: "pending" },
- { content: "Publish the test package", status: "pending" },
- ],
- },
- });
- const compact = app.tui.render(80).join("\n");
- expect(compact).toContain("✓ Inspect Desktop behavior");
- expect(compact).toContain("● Polish the task rail");
- expect(compact).toContain("Ctrl+T expand");
- expect(compact).not.toContain("Publish the test package");
- expect(compact).not.toContain("todo_updated");
- terminal.input?.("\x0f");
- const transcriptDetailed = app.tui.render(80).join("\n");
- expect(transcriptDetailed).toContain("Ctrl+T expand");
- expect(transcriptDetailed).not.toContain("Publish the test package");
- terminal.input?.("\x14");
- const tasksExpanded = app.tui.render(80).join("\n");
- expect(tasksExpanded).toContain(
- "Todo list 1/6 · 5 remaining · Ctrl+T compact",
- );
- expect(tasksExpanded).toContain("Publish the test package");
- terminal.input?.("\x0f");
- expect(app.tui.render(80).join("\n")).toContain("Publish the test package");
- app.controller.applyRuntimeTurnEvent("turn-todo", {
- type: "generic",
- eventType: "todo_updated",
- turnId: "turn-todo",
- data: {
- todos: [
- { content: "Inspect Desktop behavior", status: "completed" },
- { content: "Polish the task rail", status: "completed" },
- { content: "Run focused tests", status: "completed" },
- { content: "Review narrow layouts", status: "completed" },
- { content: "Update user documentation", status: "completed" },
- { content: "Publish the test package", status: "completed" },
- ],
- },
- });
- expect(app.tui.render(80).join("\n")).toContain(
- "✓ Todo list 6/6 completed",
- );
- await app.stop();
- });
- it("keeps settled Thinking hidden until Ctrl+O requests transcript details", async () => {
- const terminal = new FakeTerminal();
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- app.transcript.upsert({
- id: "thinking-detail-toggle",
- kind: "thinking",
- status: "succeeded",
- content: "Private settled reasoning",
- durationMs: 3_200,
- createdAtMs: 1,
- updatedAtMs: 3_201,
- });
- const compact = app.tui.render(80).join("\n");
- expect(compact).toContain("Thought for 3.2s");
- expect(compact).not.toContain("Private settled reasoning");
- terminal.input?.("\x0f");
- expect(app.tui.render(80).join("\n")).toContain(
- "Private settled reasoning",
- );
- terminal.input?.("\x0f");
- expect(app.tui.render(80).join("\n")).not.toContain(
- "Private settled reasoning",
- );
- await app.stop();
- });
- it("returns from the conversation surface to Welcome after /new", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.submit("Start a conversation");
- expect(app.getSurface()).toBe("conversation");
- await app.submit("/new");
- expect(app.getSurface()).toBe("welcome");
- expect(app.transcript.snapshot()).toEqual([]);
- });
- it("clears into a fresh conversation while keeping the previous session resumable", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.submit("Keep this conversation");
- expect(app.controller.snapshot().session?.sessionId).toBe("session-1");
- await app.submit("/clear");
- expect(app.getSurface()).toBe("welcome");
- expect(app.transcript.snapshot()).toEqual([]);
- expect(app.controller.snapshot()).toMatchObject({
- status: "idle",
- session: undefined,
- sessions: [expect.objectContaining({ sessionId: "session-1" })],
- });
- expect(runtime.archiveSession).not.toHaveBeenCalled();
- });
- it("opens read-only Help above the Composer without using a TUI overlay", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- const showOverlay = vi.spyOn(app.tui, "showOverlay");
- app.start();
- await app.ready;
- const modelRequestsBeforeHelp = vi.mocked(runtime.listModels).mock.calls
- .length;
- const activeRunRequestsBeforeHelp = vi.mocked(runtime.getActiveRun).mock
- .calls.length;
- await app.submit("/help");
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- expect(app.transcript.snapshot()).toEqual([]);
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- expect(terminal.mouseTracking).toBe(false);
- terminal.input?.("m");
- terminal.input?.("\r");
- expect(app.tui.hasOverlay()).toBe(false);
- await vi.waitFor(() => expect(app.interaction.isActive()).toBe(false));
- expect(app.editor.getText()).toBe("");
- expect(runtime.listModels).toHaveBeenCalledTimes(modelRequestsBeforeHelp);
- expect(runtime.getActiveRun).toHaveBeenCalledTimes(
- activeRunRequestsBeforeHelp,
- );
- expect(showOverlay).not.toHaveBeenCalled();
- await app.stop();
- });
- it("lets Help use the available terminal height instead of capping it at twelve rows", async () => {
- const terminal = new FakeTerminal();
- terminal.rows = 30;
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.submit("/help");
- expect(app.interaction.current()?.render?.(terminal.columns)).toHaveLength(
- 24,
- );
- terminal.rows = 18;
- expect(app.interaction.current()?.render?.(terminal.columns)).toHaveLength(
- 12,
- );
- await app.stop();
- });
- it("keeps steer executable and exposes it in slash-command discovery", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.getActiveRun).mockImplementation(async (sessionId) => ({
- schemaVersion: 1,
- sessionId,
- state: "running",
- turnId: "turn-active",
- actions: { steer: true },
- }));
- vi.mocked(runtime.steer).mockResolvedValue({
- turnId: "turn-active",
- mode: "steered",
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.submit("Create session");
- await app.submit("/help");
- const helpPanel = app.interaction.current();
- expect(helpPanel).toBeDefined();
- expect(app.tui.hasOverlay()).toBe(false);
- const helpPages: string[] = [];
- for (let page = 0; page < 20; page += 1) {
- const current = helpPanel?.render?.(100).join("\n") ?? "";
- if (current === helpPages.at(-1)) break;
- helpPages.push(current);
- helpPanel?.handleInput?.("\x1b[6~");
- }
- const help = helpPages.join("\n");
- expect(help).toContain("/steer <message>");
- expect(help).not.toContain("/send-now");
- expect(help).not.toContain("/retry");
- helpPanel?.handleInput?.("\x1b");
- await app.submit("/steer focus on the failing test");
- expect(runtime.steer).toHaveBeenCalledWith(
- expect.objectContaining({
- sessionId: "session-1",
- source: "api",
- message: { content: "focus on the failing test" },
- producerId: "mcode",
- idempotencyKey: expect.stringMatching(/^run_action_/u),
- }),
- );
- await app.stop();
- });
- it("refreshes Runtime steer capability before Enter submits the active Draft", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishTurn: (() => void) | undefined;
- let consumeSteer: (() => void) | undefined;
- const steerConsumptionReady = new Promise<void>((resolve) => {
- consumeSteer = resolve;
- });
- let steerAvailable = false;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- yield {
- type: "delta",
- turnId: "turn-active",
- content: "Working on it",
- };
- await steerConsumptionReady;
- yield {
- type: "message",
- message: {
- id: "message-steer",
- turnId: "turn-active",
- role: "user",
- content: "focus on the failing test",
- },
- };
- await new Promise<void>((resolve) => {
- finishTurn = resolve;
- });
- yield { type: "done", turnId: "turn-active" };
- },
- );
- vi.mocked(runtime.getActiveRun).mockImplementation(async (sessionId) =>
- steerAvailable
- ? {
- schemaVersion: 1,
- sessionId,
- state: "running",
- turnId: "turn-active",
- actions: { steer: true },
- }
- : {
- schemaVersion: 1,
- sessionId,
- state: "idle",
- actions: { steer: false },
- },
- );
- vi.mocked(runtime.steer).mockResolvedValue({
- turnId: "turn-active",
- mode: "steered",
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const submission = app.submit("Inspect the failure");
- try {
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).toContain("Running"),
- );
- steerAvailable = true;
- terminal.input?.("focus on the failing test");
- terminal.input?.("\r");
- expect(app.editor.getText()).toBe("");
- await vi.waitFor(() =>
- expect(runtime.steer).toHaveBeenCalledWith(
- expect.objectContaining({
- sessionId: "session-1",
- source: "api",
- message: { content: "focus on the failing test" },
- producerId: "mcode",
- idempotencyKey: expect.stringMatching(/^run_action_/u),
- requestedTurnId: "turn-active",
- }),
- ),
- );
- expect(app.transcript.snapshot().at(-1)).toMatchObject({
- kind: "user",
- status: "pending",
- content: "focus on the failing test",
- userPresentation: "pending-steer",
- });
- expect(stripAnsi(app.tui.render(80).join("\n"))).toContain(
- "↳ Next · focus on the failing test",
- );
- consumeSteer?.();
- await vi.waitFor(() =>
- expect(
- app.transcript.snapshot().filter((cell) => cell.kind === "user"),
- ).toEqual([
- expect.objectContaining({ content: "Inspect the failure" }),
- expect.objectContaining({
- status: "succeeded",
- content: "focus on the failing test",
- sourceMessageId: "message-steer",
- userPresentation: undefined,
- }),
- ]),
- );
- const rendered = app.tui.render(80).join("\n");
- expect(rendered).toContain("Inspect the failure");
- expect(rendered).toContain("focus on the failing test");
- expect(stripAnsi(rendered)).not.toContain("↳ Next ·");
- expect(app.editor.getText()).toBe("");
- } finally {
- consumeSteer?.();
- await vi.waitFor(() => expect(finishTurn).toEqual(expect.any(Function)));
- finishTurn?.();
- await submission;
- await app.stop();
- }
- });
- it("queues with Alt+Enter, restores with Option+Up, then steers with Enter", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const queuedItems: TuiQueuedMessage[] = [];
- let finishTurn: (() => void) | undefined;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- yield {
- type: "delta",
- turnId: "turn-active",
- content: "Working on it",
- };
- await new Promise<void>((resolve) => {
- finishTurn = resolve;
- });
- yield { type: "done", turnId: "turn-active" };
- },
- );
- vi.mocked(runtime.enqueueMessage).mockImplementation(
- async (sessionId, content) => {
- const item: TuiQueuedMessage = {
- itemId: "queue-guidance",
- sessionId,
- status: "queued",
- source: "api",
- content,
- };
- queuedItems.push(item);
- return {
- itemId: item.itemId,
- status: item.status,
- position: queuedItems.length,
- };
- },
- );
- vi.mocked(runtime.listQueuedMessages).mockImplementation(async () => [
- ...queuedItems,
- ]);
- vi.mocked(runtime.deleteQueuedMessage).mockImplementation(
- async (_sessionId, itemId) => {
- const index = queuedItems.findIndex((item) => item.itemId === itemId);
- return index >= 0 ? queuedItems.splice(index, 1)[0] : undefined;
- },
- );
- vi.mocked(runtime.getActiveRun).mockImplementation(async (sessionId) => ({
- schemaVersion: 1,
- sessionId,
- state: "running",
- turnId: "turn-active",
- actions: { steer: true },
- }));
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const submission = app.submit("Inspect the failure");
- try {
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).toContain("Running"),
- );
- terminal.input?.("Use the preview branch directly");
- terminal.input?.("\x1b\r");
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).toContain(
- "Use the preview branch directly",
- ),
- );
- await vi.waitFor(() =>
- expect(runtime.listQueuedMessages).toHaveBeenCalledWith("session-1"),
- );
- expect(app.editor.getText()).toBe("");
- terminal.input?.("\x1b[1;3A");
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("Use the preview branch directly"),
- );
- expect(runtime.deleteQueuedMessage).toHaveBeenCalledWith(
- "session-1",
- "queue-guidance",
- );
- expect(runtime.steerQueuedMessage).not.toHaveBeenCalled();
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).not.toContain("Queued ·"),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.steer).toHaveBeenCalledWith(
- expect.objectContaining({
- sessionId: "session-1",
- message: { content: "Use the preview branch directly" },
- requestedTurnId: "turn-active",
- }),
- ),
- );
- expect(app.editor.getText()).toBe("");
- } finally {
- finishTurn?.();
- await submission;
- await app.stop();
- }
- });
- it("renders /context from the Runtime-owned persisted context usage", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.getContextSnapshot).mockResolvedValue({
- status: "live",
- model: { provider: "minimax", id: "MiniMax-M3", contextWindow: 10_000 },
- contextUsage: {
- contextWindowTokens: 10_000,
- usedTokens: 3_000,
- totalCountSource: "PROVIDER_USAGE_ANCHORED",
- components: [
- { kind: "SYSTEM_PROMPT", tokens: 1_000 },
- { kind: "MEMORY", tokens: 0 },
- { kind: "TOOLS", tokens: 0 },
- { kind: "SKILLS", tokens: 0 },
- { kind: "MESSAGES", tokens: 2_000 },
- { kind: "OTHER", tokens: 0 },
- ],
- },
- });
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.submit("Create session");
- const transcriptBeforeContext = app.transcript.snapshot();
- await app.submit("/context");
- expect(app.transcript.snapshot()).toEqual(transcriptBeforeContext);
- expect(app.interaction.current()).toBeDefined();
- const rendered = app.tui.render(80).join("\n");
- expect(rendered).toContain("Context");
- expect(rendered).toContain("3k/10k tokens (30% used)");
- expect(rendered).toContain("Usage by category");
- expect(rendered).toContain("Messages");
- expect(rendered).toMatch(/[⛁⛀⛶]/u);
- app.interaction.current()?.handleInput?.("\x1b");
- expect(app.interaction.current()).toBeUndefined();
- expect(app.transcript.snapshot()).toEqual(transcriptBeforeContext);
- });
- it("routes /init through the Runtime-owned init Skill and the normal Agent turn", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSkills).mockResolvedValue({
- skills: [
- {
- name: "init",
- displayName: "Init",
- description: "Analyze the codebase and generate AGENTS.md",
- enabled: true,
- sourceKind: "builtin",
- },
- ],
- hasMore: false,
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await vi.waitFor(() => expect(runtime.listSkills).toHaveBeenCalled());
- await app.submit("/init");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({ id: "session-1", content: "/init" }),
- expect.any(AbortSignal),
- );
- });
- it("forwards literal slash-prefixed prompts without invoking local commands", async () => {
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- const prompts = [
- "/commands",
- "/diff",
- "/tool",
- "/tools",
- "/resumex",
- "/renamex Renamed by a typo",
- "/clearx",
- "/tmp/sample-photo.JPG 用 read 工具直接读这个图片,告诉我是什么",
- "/not-a-command explain what this text means",
- "/context 这样还是会显示上下文信息",
- ];
- try {
- app.start();
- await app.ready;
- for (const prompt of prompts) {
- await app.submit(prompt);
- expect(runtime.sendMessage).toHaveBeenLastCalledWith(
- expect.objectContaining({ id: "session-1", content: prompt }),
- expect.any(AbortSignal),
- );
- expect(app.interaction.current()).toBeUndefined();
- expect(app.tui.hasOverlay()).toBe(false);
- }
- expect(runtime.sendMessage).toHaveBeenCalledTimes(prompts.length);
- expect(runtime.renameSession).not.toHaveBeenCalled();
- // Context metrics refresh independently; the overlay assertions above verify no local command ran.
- expect(app.transcript.snapshot()).not.toContainEqual(
- expect.objectContaining({ content: "Unknown command. Use /help." }),
- );
- } finally {
- await app.stop();
- }
- });
- it("keeps contextual commands local and submits other legacy names as prompts", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.submit("/queue");
- expect(runtime.listQueuedMessages).not.toHaveBeenCalled();
- expect(app.transcript.snapshot()).toEqual([]);
- expect(app.tui.render(80).join("\n")).toContain(
- "No messages are waiting and no response is currently running.",
- );
- await app.submit("/queue-clear");
- expect(runtime.deleteQueuedMessage).not.toHaveBeenCalled();
- await app.submit("/send-now do this first");
- await app.submit("/retry");
- expect(
- vi
- .mocked(runtime.sendMessage)
- .mock.calls.map(([request]) => request.content),
- ).toEqual(["/queue-clear", "/send-now do this first"]);
- expect(app.tui.render(80).join("\n")).toContain(
- "There is no failed response to retry in this Session.",
- );
- });
- it("resends the failed user submission through /retry instead of sending the command as text", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let attempt = 0;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- attempt += 1;
- if (attempt === 1) {
- yield { type: "error", message: "terminated" };
- return;
- }
- yield { type: "delta", content: "Recovered response" };
- yield { type: "done" };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.submit("Explain this failure");
- expect(app.tui.render(100).join("\n")).toContain(
- "The model connection ended before the response was completed.",
- );
- expect(app.tui.render(100).join("\n")).toContain(
- "Run /retry to resend your last message.",
- );
- await app.submit("/retry");
- expect(
- vi
- .mocked(runtime.sendMessage)
- .mock.calls.map(([request]) => request.content),
- ).toEqual(["Explain this failure", "Explain this failure"]);
- expect(
- vi
- .mocked(runtime.sendMessage)
- .mock.calls.some(([request]) => request.content === "/retry"),
- ).toBe(false);
- expect(app.transcript.snapshot()).toContainEqual(
- expect.objectContaining({
- kind: "assistant",
- content: "Recovered response",
- }),
- );
- await app.stop();
- });
- it("uses Runtime retry-continuation when a terminal failure has no local submission snapshot", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const restoredSession = {
- sessionId: "session-1",
- title: "Restored session",
- workspaceDir: "/workspace",
- status: "error",
- errorMessage: "terminated",
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([restoredSession]);
- vi.mocked(runtime.getSession).mockResolvedValue(restoredSession);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-1");
- expect(app.tui.render(100).join("\n")).toContain(
- "The model connection ended before the response was completed.",
- );
- await app.submit("/retry");
- expect(runtime.sendMessage).toHaveBeenLastCalledWith(
- expect.objectContaining({
- id: "session-1",
- content: "",
- clientIntent: "retry-continuation",
- }),
- expect.any(AbortSignal),
- );
- await app.stop();
- });
- it("reports service authentication failures without assuming the user needs to sign in", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- yield { type: "error", message: "authentication failed" };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.submit("Use the protected model");
- expect(app.tui.render(100).join("\n")).toContain(
- "The service rejected request authentication.",
- );
- expect(app.tui.render(100).join("\n")).not.toContain("Run /login");
- expect(app.tui.render(100).join("\n")).not.toContain("Run /retry");
- await app.submit("/retry");
- expect(runtime.sendMessage).toHaveBeenCalledOnce();
- await app.stop();
- });
- it("cycles the shared permission mode with Alt+M without repeating the shortcut", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- expect(app.tui.render(80).join("\n")).toContain("Auto");
- expect(app.tui.render(80).join("\n")).not.toContain("Shift+Tab");
- expect(app.tui.render(80).join("\n")).not.toContain(
- "Shift+Tab switch mode",
- );
- terminal.input?.("\x1bm");
- await vi.waitFor(() => {
- expect(runtime.setPermissionMode).toHaveBeenCalledWith(
- "bypassPermissions",
- );
- const rendered = app.tui.render(80).join("\n");
- expect(rendered).toContain("Full access");
- expect(rendered).not.toContain("Shift+Tab");
- expect(rendered.match(/Full access/g)).toHaveLength(1);
- expect(rendered).not.toContain("Desktop permissions:");
- });
- await app.stop();
- });
- it("projects Token Plan warnings into the status rail", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.getAccountStatus).mockResolvedValue({
- status: "ready",
- defaultModel: "minimax/MiniMax-M2.7",
- providerId: "minimax",
- modelId: "MiniMax-M2.7",
- authMode: "managed-login",
- managedTokenPresent: true,
- modelSource: "token-plan",
- tokenPlanQuotaState: "available",
- tokenPlanQuota: {
- fiveHour: { remainingPercent: 30, unlimited: false },
- weekly: { remainingPercent: 64, unlimited: false },
- },
- warnings: [],
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Measure this turn");
- await vi.waitFor(() => {
- const rendered = app.tui.render(120).join("\n");
- expect(rendered).toContain("5h 30%");
- expect(rendered).not.toContain("Week 64%");
- });
- await app.stop();
- });
- it("keeps Token Plan subscription state out of the status rail when no quota exists", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.getAccountStatus).mockResolvedValue({
- status: "ready",
- defaultModel: "minimax/MiniMax-M3",
- providerId: "minimax",
- modelId: "MiniMax-M3",
- authMode: "managed-login",
- managedTokenPresent: true,
- modelSource: "token-plan",
- tokenPlanQuotaState: "not-subscribed",
- warnings: [],
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Measure this turn");
- await vi.waitFor(() => {
- const rendered = app.tui.render(120).join("\n");
- expect(rendered).toContain("✦ MiniMax M2.7");
- expect(rendered).not.toMatch(/Context 5%|Quota|Plan|5h|Week/u);
- });
- await app.stop();
- });
- it("keeps the current permission mode when an Alt+M update fails", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.setPermissionMode).mockRejectedValueOnce(
- new Error("config is read-only"),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- terminal.input?.("\x1bm");
- await vi.waitFor(() => {
- expect(app.tui.render(80).join("\n")).toContain("Auto");
- expect(app.tui.render(80).join("\n")).not.toContain("FULL");
- expect(app.transcript.snapshot().at(-1)).toMatchObject({
- kind: "warning",
- content:
- "Permission mode was not changed: config is read-only. Retry /permission or Alt+M.",
- });
- });
- await app.stop();
- });
- it("arms Plan with Shift+Tab and sends the one-shot Runtime intent", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- terminal.input?.("\x1b[Z");
- expect(app.tui.render(100).join("\n")).toContain("PLAN next");
- expect(app.tui.render(100).join("\n")).toContain(
- "Plan Mode · next message enters Plan",
- );
- terminal.input?.("计");
- expect(app.tui.render(100).join("\n")).toContain(
- "Plan Mode · next message enters Plan",
- );
- expect(app.tui.render(32).join("\n")).toContain("Plan Mode");
- terminal.input?.("\x7f");
- expect(app.editor.getText()).toBe("");
- expect(app.tui.render(100).join("\n")).toContain(
- "Plan Mode · next message enters Plan",
- );
- terminal.input?.("计");
- terminal.input?.("划");
- expect(app.editor.getText()).toBe("计划");
- expect(app.tui.render(100).join("\n")).toContain("PLAN next");
- expect(app.tui.render(100).join("\n")).toContain(
- "Plan Mode · next message enters Plan",
- );
- terminal.input?.("\r");
- await vi.waitFor(() => expect(runtime.sendMessage).toHaveBeenCalledOnce());
- await app.controller.whenIdle();
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- content: "计划",
- clientIntent: "plan-entry",
- }),
- expect.any(AbortSignal),
- );
- expect(app.tui.render(100).join("\n")).toContain("PLAN");
- expect(app.tui.render(100).join("\n")).toContain("Auto");
- await app.stop();
- });
- it("keeps ordinary messages in Plan and exits only with an explicit one-shot intent", async () => {
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/plan on");
- await app.submit("Draft the implementation plan");
- await app.submit("Refine the verification section");
- expect(runtime.sendMessage).toHaveBeenNthCalledWith(
- 2,
- expect.not.objectContaining({ clientIntent: expect.anything() }),
- expect.any(AbortSignal),
- );
- expect(app.tui.render(100).join("\n")).toContain(
- "/workspace │ PLAN │ Auto",
- );
- await app.submit("/plan off");
- await app.submit("Implement the approved direction");
- expect(runtime.sendMessage).toHaveBeenLastCalledWith(
- expect.objectContaining({ clientIntent: "plan-exit" }),
- expect.any(AbortSignal),
- );
- expect(app.tui.render(100).join("\n")).not.toContain("PLAN");
- await app.stop();
- });
- it("keeps the one-shot Plan intent armed when direct admission rejects it", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.sendMessage).mockImplementationOnce(
- async function* rejectedAdmission() {
- yield* [] as TuiStreamEvent[];
- throw Object.assign(new Error("Plan entry is disabled"), {
- code: "local_plan_entry_disabled",
- });
- },
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/plan on");
- await app.submit("Plan this after the feature is enabled");
- expect(app.tui.render(100).join("\n")).toContain("PLAN next");
- await app.submit("Retry the same Plan intent");
- expect(runtime.sendMessage).toHaveBeenLastCalledWith(
- expect.objectContaining({ clientIntent: "plan-entry" }),
- expect.any(AbortSignal),
- );
- await app.stop();
- });
- it("settles a Plan transition from authoritative Session metadata when the stream is empty", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.sendMessage).mockImplementationOnce(
- async function* emptyAcceptedStream() {
- yield* [] as TuiStreamEvent[];
- },
- );
- vi.mocked(runtime.getSession).mockResolvedValue({
- sessionId: "session-1",
- workspace: "/workspace",
- interactionMode: "plan",
- });
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/plan on");
- await app.submit("Plan without emitting a stream event");
- expect(runtime.getSession).toHaveBeenCalledWith("session-1");
- expect(app.tui.render(100).join("\n")).toContain(
- "/workspace │ PLAN │ Auto",
- );
- expect(app.tui.render(100).join("\n")).not.toContain("PLAN next");
- await app.stop();
- });
- it("clears accepted Plan entry while authoritative metadata refresh is unavailable", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.getSession).mockRejectedValue(
- new Error("metadata temporarily unavailable"),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/plan on");
- await app.submit("OK");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({ content: "OK", clientIntent: "plan-entry" }),
- expect.any(AbortSignal),
- );
- expect(app.tui.render(100).join("\n")).not.toContain("/workspace │ PLAN");
- expect(app.tui.render(100).join("\n")).not.toContain("DEFAULT next");
- await app.stop();
- });
- it("preserves Plan intent through direct busy fallback into the Runtime Queue", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.listModels).mockResolvedValue([
- {
- providerId: "minimax",
- modelId: "MiniMax-M2.7",
- selected: true,
- variant: "",
- },
- ]);
- vi.mocked(runtime.sendMessage).mockImplementationOnce(
- async function* busyAdmission() {
- yield* [] as TuiStreamEvent[];
- throw Object.assign(new Error("Session is busy"), {
- code: "local_session_busy",
- });
- },
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/plan on");
- await app.submit("Queue this Plan request");
- expect(runtime.enqueueMessage).toHaveBeenCalledWith(
- "session-1",
- "Queue this Plan request",
- {
- attachments: [],
- clientIntent: "plan-entry",
- model: { providerId: "minimax", modelId: "MiniMax-M2.7", variant: "" },
- },
- );
- expect(app.tui.render(100).join("\n")).not.toContain("PLAN queued");
- await app.stop();
- });
- it("binds a one-shot Plan intent to only the first concurrent submission", async () => {
- let releaseFirstEvent: (() => void) | undefined;
- const firstEventGate = new Promise<void>((resolve) => {
- releaseFirstEvent = resolve;
- });
- const runtime = createRuntime();
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* delayedAdmission() {
- await firstEventGate;
- yield { type: "delta", content: "Planning" };
- yield { type: "done" };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/plan on");
- const first = app.submit("Plan the implementation");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledTimes(1),
- );
- await app.submit("Queue a follow-up while admission is quiet");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({ clientIntent: "plan-entry" }),
- expect.any(AbortSignal),
- );
- expect(runtime.enqueueMessage).toHaveBeenCalledWith(
- "session-1",
- "Queue a follow-up while admission is quiet",
- {
- attachments: [],
- model: { providerId: "minimax", modelId: "MiniMax-M2.7" },
- },
- );
- releaseFirstEvent?.();
- await first;
- await app.stop();
- });
- it("shows the Desktop-aligned confirmation when the Agent calls EnterPlanMode", async () => {
- let emitEvent: ((event: TuiRuntimeEvent) => void) | undefined;
- const runtime = createRuntime();
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- const event = await new Promise<TuiRuntimeEvent | undefined>(
- (resolve) => {
- emitEvent = resolve;
- signal.addEventListener("abort", () => resolve(undefined), {
- once: true,
- });
- },
- );
- if (event) yield event;
- await new Promise<void>((resolve) => {
- signal.addEventListener("abort", resolve, { once: true });
- });
- },
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Plan a complex change");
- await vi.waitFor(() => expect(emitEvent).toBeTypeOf("function"));
- const askEvent = runtimeEvent({
- type: "questionnaire.ask",
- timestamp: Date.now(),
- source: "runtime",
- payload: {
- sessionId: "session-1",
- agentName: "mavis",
- request: planEntryEventRequest("plan-entry-agent-tool"),
- },
- });
- expect(askEvent).toMatchObject({
- type: "questionnaire.ask",
- sessionId: "session-1",
- request: { id: "plan-entry-agent-tool", mode: "plan" },
- });
- emitEvent?.(askEvent);
- await vi.waitFor(() => expect(app.interaction.isActive()).toBe(true));
- const panel = app.interaction.current();
- const rendered = panel?.render?.(100).join("\n") ?? "";
- expect(rendered).toContain("Use Plan mode?");
- expect(rendered).toContain(
- "Plan mode structures complex tasks before execution.",
- );
- expect(rendered).toContain("Continue with plan");
- expect(rendered).toContain("Deny");
- panel?.handleInput?.("\r");
- await vi.waitFor(() =>
- expect(runtime.replyQuestionnaire).toHaveBeenCalledWith(
- "mavis",
- "plan-entry-agent-tool",
- [
- {
- stepId: "plan-enter",
- selectedOptionIds: ["confirm"],
- selectedOther: false,
- },
- ],
- ),
- );
- await app.stop();
- });
- it("keeps a pending Plan exit armed when steer guides the current Plan Turn", async () => {
- let finishTurn: (() => void) | undefined;
- let interactionMode: "plan" | undefined;
- const runtime = createRuntime();
- vi.mocked(runtime.getSession).mockImplementation(async () => ({
- sessionId: "session-1",
- workspace: "/workspace",
- ...(interactionMode ? { interactionMode } : {}),
- }));
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* activePlanTurn() {
- interactionMode = "plan";
- yield { type: "delta", content: "Drafting the plan" };
- await new Promise<void>((resolve) => {
- finishTurn = resolve;
- });
- yield { type: "done" };
- },
- );
- vi.mocked(runtime.getActiveRun).mockImplementation(async (sessionId) => ({
- schemaVersion: 1,
- sessionId,
- state: "running",
- turnId: "turn-plan",
- actions: { steer: true },
- }));
- vi.mocked(runtime.steer).mockResolvedValue({
- turnId: "turn-plan",
- mode: "steered",
- });
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/plan on");
- const planTurn = app.submit("Draft a plan");
- await vi.waitFor(() =>
- expect(app.tui.render(100).join("\n")).toContain(
- "/workspace │ PLAN │ Auto",
- ),
- );
- await app.submit("/plan off");
- await app.submit("/steer include rollback checks");
- expect(runtime.steer).toHaveBeenCalledWith(
- expect.objectContaining({
- sessionId: "session-1",
- message: { content: "include rollback checks" },
- }),
- );
- expect(app.tui.render(100).join("\n")).toContain("DEFAULT next");
- finishTurn?.();
- await planTurn;
- await app.stop();
- });
- it("does not arm a mode transition while a Plan Review is pending", async () => {
- let emitEvent: ((event: TuiRuntimeEvent) => void) | undefined;
- const runtime = createRuntime();
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- const event = await new Promise<TuiRuntimeEvent | undefined>(
- (resolve) => {
- emitEvent = resolve;
- signal.addEventListener("abort", () => resolve(undefined), {
- once: true,
- });
- },
- );
- if (event) yield event;
- await new Promise<void>((resolve) => {
- signal.addEventListener("abort", resolve, { once: true });
- });
- },
- );
- const terminal = new FakeTerminal();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/plan on");
- await app.submit("Draft a plan");
- await vi.waitFor(() => expect(emitEvent).toBeTypeOf("function"));
- emitEvent?.(
- runtimeEvent({
- type: "questionnaire.ask",
- timestamp: Date.now(),
- source: "runtime",
- payload: {
- sessionId: "session-1",
- agentName: "mavis",
- request: planReviewEventRequest("plan-review-mode-guard"),
- },
- }),
- );
- await vi.waitFor(() => expect(app.interaction.isActive()).toBe(true));
- // The compact panel owns the decision; the frozen plan is projected to the Transcript.
- expect(app.interaction.current()?.render?.(100).join("\n")).toContain(
- "Plan complete. What would you like to do?",
- );
- expect(app.tui.render(100).join("\n")).toContain("Frozen plan");
- terminal.input?.("\x1b[Z");
- expect(app.interaction.isActive()).toBe(true);
- expect(app.tui.render(100).join("\n")).toContain(
- "/workspace │ PLAN │ Auto",
- );
- expect(app.tui.render(100).join("\n")).not.toContain("DEFAULT next");
- await app.submit("/plan off");
- expect(app.interaction.isActive()).toBe(true);
- expect(app.tui.render(100).join("\n")).toContain(
- "/workspace │ PLAN │ Auto",
- );
- expect(app.tui.render(100).join("\n")).not.toContain("DEFAULT next");
- expect(runtime.sendMessage).toHaveBeenCalledTimes(1);
- await app.submit("/steer bypass the review");
- expect(app.interaction.isActive()).toBe(true);
- expect(runtime.steer).not.toHaveBeenCalled();
- app.interaction.current()?.handleInput?.("\u001b");
- await vi.waitFor(() =>
- expect(runtime.replyQuestionnaire).toHaveBeenCalledWith(
- "mavis",
- "plan-review-mode-guard",
- [expect.objectContaining({ skipped: true })],
- ),
- );
- await vi.waitFor(() => expect(app.interaction.isActive()).toBe(false));
- expect(app.tui.render(100).join("\n")).toContain(
- "/workspace │ PLAN │ Auto",
- );
- await app.stop();
- });
- it("streams Plan feedback and replaces it with the revised Plan Review", async () => {
- let emitReview: ((event: TuiRuntimeEvent) => void) | undefined;
- let releaseContinuation: (() => void) | undefined;
- let finishReply: ((value: boolean) => void) | undefined;
- const runtime = createRuntime();
- vi.mocked(runtime.replyQuestionnaire).mockImplementation(
- async () =>
- await new Promise<boolean>((resolve) => {
- finishReply = resolve;
- }),
- );
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- const review = await new Promise<TuiRuntimeEvent | undefined>(
- (resolve) => {
- emitReview = resolve;
- signal.addEventListener("abort", () => resolve(undefined), {
- once: true,
- });
- },
- );
- if (!review || signal.aborted) return;
- yield review;
- await new Promise<void>((resolve) => {
- releaseContinuation = resolve;
- signal.addEventListener("abort", resolve, { once: true });
- });
- if (signal.aborted) return;
- const turnId = "turn-runtime-plan-feedback";
- yield runtimeEvent({
- type: "session.start",
- timestamp: Date.now(),
- source: "runtime",
- payload: { sessionId: "session-1", turnId },
- });
- vi.mocked(runtime.getMessages).mockResolvedValue([
- {
- id: "assistant-revised-plan",
- role: "assistant",
- content: "I revised the plan with rollback coverage.",
- },
- ]);
- yield runtimeEvent({
- type: "questionnaire.ask",
- timestamp: Date.now(),
- source: "runtime",
- payload: {
- sessionId: "session-1",
- agentName: "mavis",
- request: {
- ...planReviewEventRequest("plan-review-feedback-revised"),
- modePayload: {
- planReview: {
- markdown:
- "# Revised frozen plan\n\nIncludes rollback coverage.",
- path: "/history/session-1/artifacts/plan.md",
- },
- },
- },
- },
- });
- yield runtimeEvent({
- type: "session.finish",
- timestamp: Date.now(),
- source: "runtime",
- payload: { sessionId: "session-1", turnId },
- });
- finishReply?.(true);
- await new Promise<void>((resolve) => {
- signal.addEventListener("abort", resolve, { once: true });
- });
- },
- );
- const terminal = new FakeTerminal();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/plan on");
- await app.submit("Draft a plan");
- await vi.waitFor(() => expect(emitReview).toBeTypeOf("function"));
- emitReview?.(
- runtimeEvent({
- type: "questionnaire.ask",
- timestamp: Date.now(),
- source: "runtime",
- payload: {
- sessionId: "session-1",
- agentName: "mavis",
- request: planReviewEventRequest("plan-review-feedback-live"),
- },
- }),
- );
- await vi.waitFor(() => expect(app.interaction.isActive()).toBe(true));
- terminal.input?.("\u001b[B");
- terminal.input?.("\u001b[B");
- terminal.input?.("\r");
- for (const character of "Add rollback coverage")
- terminal.input?.(character);
- terminal.input?.("\r");
- await vi.waitFor(() => expect(finishReply).toBeTypeOf("function"));
- releaseContinuation?.();
- await vi.waitFor(() =>
- expect(
- app.transcript
- .snapshot()
- .map((cell) => cell.content)
- .join("\n"),
- ).toContain("I revised the plan with rollback coverage."),
- );
- expect(runtime.watchSessionTurn).toHaveBeenCalledWith(
- "session-1",
- "turn-runtime-plan-feedback",
- expect.any(AbortSignal),
- );
- await vi.waitFor(() => expect(app.interaction.isActive()).toBe(true));
- // The revised plan is projected to the Transcript; the panel stays the compact decision.
- expect(app.interaction.current()?.render?.(100).join("\n")).toContain(
- "Plan complete. What would you like to do?",
- );
- expect(app.tui.render(100).join("\n")).toContain("Revised frozen plan");
- expect(app.tui.render(100).join("\n")).not.toContain(
- "Message · Enter send",
- );
- await app.stop();
- });
- it("supports explicit /plan and /permission mode commands", async () => {
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/plan on");
- expect(app.tui.render(100).join("\n")).toContain("PLAN next");
- await app.submit("/permission full");
- expect(runtime.setPermissionMode).toHaveBeenCalledWith("bypassPermissions");
- expect(app.tui.render(100).join("\n")).toContain("Full access");
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- await app.submit("Plan with Full access");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({ clientIntent: "plan-entry" }),
- expect.any(AbortSignal),
- );
- expect(app.tui.render(100).join("\n")).toContain("PLAN");
- expect(app.tui.render(100).join("\n")).toContain("Full access");
- await app.stop();
- });
- it("opens a permission mode selector for bare /permission", async () => {
- const runtime = createRuntime();
- const terminal = new FakeTerminal();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/permission");
- expect(app.interaction.isActive()).toBe(true);
- const rendered = app.interaction.current()?.render?.(100).join("\n") ?? "";
- expect(rendered).toContain("╭─ Permission");
- expect(rendered).toContain("Current · Auto");
- expect(rendered).toContain("ASK");
- expect(rendered).toContain("› AUTO");
- expect(rendered).toContain("FULL");
- expect(runtime.setPermissionMode).not.toHaveBeenCalled();
- terminal.input?.("\u001b[B");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.setPermissionMode).toHaveBeenCalledWith(
- "bypassPermissions",
- ),
- );
- expect(app.interaction.isActive()).toBe(false);
- expect(app.tui.render(100).join("\n")).toContain("Full access");
- await app.submit("/permission status");
- expect(app.interaction.isActive()).toBe(false);
- expect(
- app.transcript
- .snapshot()
- .map((cell) => cell.content)
- .join("\n"),
- ).toContain("Permission mode: Full access.");
- await app.stop();
- });
- it("opens the latest settled Plan as a read-only full snapshot", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.getLatestPlanReview).mockResolvedValue(
- planReviewEventRequest("plan-review-settled"),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.openSession("session-1");
- await app.submit("/plan view");
- expect(runtime.getLatestPlanReview).toHaveBeenCalledWith(
- "mavis",
- "session-1",
- );
- const rendered = app.interaction.current()?.render?.(100).join("\n") ?? "";
- expect(rendered).toContain("Frozen plan");
- expect(rendered).toContain("/history/session-1/artifacts/plan.md");
- expect(rendered).not.toContain("Agree and start implementation");
- app.interaction.current()?.handleInput?.("\u001b");
- expect(app.interaction.isActive()).toBe(false);
- await app.stop();
- });
- it("does not open a settled Plan after the active Session changes", async () => {
- let resolveReview:
- | ((request: TuiQuestionnaireRequest | undefined) => void)
- | undefined;
- const runtime = createRuntime();
- vi.mocked(runtime.getLatestPlanReview).mockImplementation(
- async () =>
- await new Promise<TuiQuestionnaireRequest | undefined>((resolve) => {
- resolveReview = resolve;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.openSession("session-1");
- const viewing = app.submit("/plan view");
- await vi.waitFor(() => expect(resolveReview).toBeTypeOf("function"));
- await app.openSession("session-2");
- resolveReview?.(planReviewEventRequest("stale-plan-review"));
- await viewing;
- expect(app.interaction.isActive()).toBe(false);
- expect(app.tui.render(100).join("\n")).not.toContain("Frozen plan");
- await app.stop();
- });
- it("blocks Plan entry but still allows Plan exit when the Runtime capability is disabled", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.getPlanModeCapabilities).mockResolvedValue({
- entryEnabled: false,
- });
- const terminal = new FakeTerminal();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.openSession("session-1");
- terminal.input?.("\x1b[Z");
- await app.submit("/plan on");
- expect(app.tui.render(100).join("\n")).not.toContain("PLAN next");
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- vi.mocked(runtime.getSession).mockResolvedValue({
- sessionId: "session-1",
- workspace: "/workspace",
- interactionMode: "plan",
- });
- await app.controller.refreshSessionMetadata("session-1");
- terminal.input?.("\x1b[Z");
- expect(app.tui.render(100).join("\n")).toContain("DEFAULT next");
- await app.stop();
- });
- it("fails closed when the Runtime Plan capability cannot be loaded", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.getPlanModeCapabilities).mockRejectedValue(
- new Error("Runtime unavailable"),
- );
- const terminal = new FakeTerminal();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- terminal.input?.("\x1b[Z");
- await app.submit("/plan on");
- expect(app.tui.render(100).join("\n")).not.toContain("PLAN next");
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- await app.stop();
- });
- it("submits the hidden Runtime engineering log command name as an ordinary prompt", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- runtimeLogDirectory: "/home/dev/.minimax/v2/observability/logs",
- });
- await app.submit("/logs");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({ id: "session-1", content: "/logs" }),
- expect.any(AbortSignal),
- );
- });
- it("exposes sanitized checks and submits the hidden /config name as ordinary text", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listPendingPermissions).mockResolvedValue([
- {
- requestId: "permission-1",
- sessionId: "session-1",
- toolName: "bash",
- toolDescription: "Run tests",
- },
- ]);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.submit("/doctor");
- const doctorOutput = app.transcript.snapshot().at(-1)?.content ?? "";
- expect(app.interaction.current()).toBeUndefined();
- await app.submit("/config");
- expect(app.interaction.current()).toBeUndefined();
- await app.submit("/skills docs");
- const skillsPanel = app.interaction.current();
- const skillsOutput = skillsPanel?.render(80).join("\n") ?? "";
- expect(app.tui.render(80).join("\n")).not.toContain("Message · Enter");
- skillsPanel?.handleInput?.("\x1b");
- await app.submit("/mcp browser");
- const mcpPanel = app.interaction.current();
- const mcpOutput = mcpPanel?.render(80).join("\n") ?? "";
- expect(app.tui.render(80).join("\n")).not.toContain("Message · Enter");
- mcpPanel?.handleInput?.("\x1b");
- await app.submit("/permissions");
- const output = app.transcript
- .snapshot()
- .map((cell) => cell.content)
- .join("\n");
- expect(runtime.getRuntimeDiagnostics).toHaveBeenCalledTimes(1);
- expect(runtime.listSkills).toHaveBeenCalledWith("mavis", "docs");
- expect(runtime.listMcpServers).toHaveBeenCalledWith("browser", "session-1");
- expect(runtime.listPendingPermissions).toHaveBeenCalled();
- expect(doctorOutput).toContain("Configuration check");
- expect(doctorOutput).toContain("Status: Valid");
- expect(doctorOutput).not.toContain("/doctor");
- expect(output).toContain("Configuration check");
- expect(output).not.toContain("Effective configuration");
- expect(skillsOutput).toContain("Skills");
- expect(skillsOutput).toContain("Built-in · 1");
- expect(skillsOutput).toContain("Docs");
- expect(skillsOutput).toContain("● available · builtin");
- expect(mcpOutput).toContain("MCP servers");
- expect(mcpOutput).toContain("browser");
- expect(mcpOutput).toContain("◆ configured · stdio");
- expect(output).toContain("bash · Run tests");
- expect(output).not.toContain("TOKEN");
- expect(runtime.sendMessage).toHaveBeenCalledTimes(1);
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({ id: "session-1", content: "/config" }),
- expect.any(AbortSignal),
- );
- });
- it("previews /feedback without uploading until Enter confirms the Runtime draft", async () => {
- const terminal = new FakeTerminal();
- terminal.rows = 30;
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/feedback Upload failed token=****");
- expect(runtime.prepareFeedback).toHaveBeenCalledWith({
- description: "Upload failed token=****",
- });
- expect(runtime.submitFeedback).not.toHaveBeenCalled();
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- const panel = app.interaction.current();
- expect(panel?.render(80).join("\n")).toContain("Review feedback");
- panel?.handleInput?.("d");
- expect(panel?.render(80).join("\n")).toContain("Client mcode 0.1.0");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.submitFeedback).toHaveBeenCalledWith(
- "feedback-draft-1",
- expect.objectContaining({ onPhase: expect.any(Function) }),
- ),
- );
- expect(panel?.render(80).join("\n")).toContain("ticket-1");
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- terminal.input?.("\x1b");
- expect(app.interaction.isActive()).toBe(false);
- await app.stop();
- });
- it("cancels /feedback without uploading and keeps missing descriptions out of the Agent", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/feedback");
- expect(app.transcript.snapshot().at(-1)).toMatchObject({
- kind: "final-summary",
- content: "Usage: /feedback <message>",
- });
- expect(runtime.prepareFeedback).not.toHaveBeenCalled();
- await app.submit("/feedback Cancel this report");
- terminal.input?.("\x1b");
- await vi.waitFor(() =>
- expect(runtime.cancelFeedback).toHaveBeenCalledWith("feedback-draft-1"),
- );
- expect(runtime.submitFeedback).not.toHaveBeenCalled();
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- await app.stop();
- });
- it("keeps the status report local and submits the hidden /config name as text", async () => {
- const terminal = new FakeTerminal();
- terminal.rows = 24;
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const transcriptBeforeStatus = app.transcript.snapshot();
- await app.submit("/status");
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- expect(app.transcript.snapshot()).toEqual(transcriptBeforeStatus);
- const statusOutput = app.tui.render(80).join("\n");
- expect(statusOutput).toContain("MCode status");
- expect(statusOutput).toContain("Model");
- expect(statusOutput).not.toContain("/status");
- expect(statusOutput).toContain("/workspace");
- expect(statusOutput).toMatch(/Branch:\s+Not a Git repository/u);
- expect(statusOutput).toContain("Not applicable");
- expect(statusOutput).toContain("Session");
- expect(statusOutput).toContain("Permissions");
- expect(statusOutput).toContain("Auto");
- app.interaction.current()?.handleInput?.("\x1b");
- expect(app.interaction.isActive()).toBe(false);
- expect(app.transcript.snapshot()).toEqual(transcriptBeforeStatus);
- await app.submit("/config");
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(false);
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({ id: "session-1", content: "/config" }),
- expect.any(AbortSignal),
- );
- expect(runtime.getRuntimeDiagnostics).not.toHaveBeenCalled();
- await app.stop();
- });
- it("commits the first frame immediately when the Pi renderer starts", async () => {
- const terminal = new FakeTerminal();
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- let firstFrameCommitted = false;
- void app.firstFrame.then(() => {
- firstFrameCommitted = true;
- });
- app.start();
- await app.firstFrame;
- expect(terminal.started).toBe(true);
- expect(firstFrameCommitted).toBe(true);
- expect(terminal.writes.join("")).toContain("Tips for getting started");
- await app.stop();
- });
- it("chooses the account region before routing login through the auth application", async () => {
- const runtime = createRuntime();
- const openedTargets: string[] = [];
- const logoutUrl =
- "https://agent.minimax.io/auth/logout?logout_redirect_uri=https%3A%2F%2Fagent.minimax.io";
- let authenticated = false;
- vi.mocked(runtime.getAccountStatus).mockImplementation(async () =>
- authenticated
- ? {
- status: "ready",
- defaultModel: "minimax/MiniMax-M2.7",
- providerId: "minimax",
- modelId: "MiniMax-M2.7",
- authMode: "managed-login",
- managedTokenPresent: true,
- warnings: [],
- }
- : {
- status: "needs-login",
- authMode: "managed-login",
- managedTokenPresent: false,
- warnings: [],
- },
- );
- const auth = {
- login: vi.fn(
- async (
- onProgress?: (progress: McodeAuthProgress) => void,
- _region?: "cn" | "en",
- ) => {
- authenticated = true;
- onProgress?.({
- state: "device-authorization",
- userCode: "ABCD-EFGH",
- verificationUri: "https://account.example.test/device",
- expiresInSec: 300,
- });
- return {
- state: "authenticated" as const,
- message:
- _region === "en"
- ? "Signed in with MiniMax Global. Restart MCode to use this account region."
- : "Signed in with MiniMax.",
- };
- },
- ),
- logout: vi.fn(async () => {
- authenticated = false;
- return {
- state: "signed-out" as const,
- message: "Signed out of MiniMax.",
- logoutUrl,
- };
- }),
- };
- const notifyAuthContextChanged = vi.fn(async () => {
- throw new Error("Hook lifecycle cleanup failed");
- });
- const app = createTuiApp({
- runtime,
- auth,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- openExternalTarget: async (target) => {
- openedTargets.push(target);
- throw new Error("no desktop session");
- },
- notifyAuthContextChanged,
- });
- app.start();
- await app.ready;
- expect(app.tui.render(80).join("\n")).toContain("Sign in with /login");
- await app.submit("/login");
- expect(auth.login).not.toHaveBeenCalled();
- expect(app.interaction.render(80).join("\n")).toContain("China (CN)");
- expect(app.interaction.render(80).join("\n")).toContain("Global");
- app.interaction.current()?.handleInput("\u001b[B");
- app.interaction.current()?.handleInput("\r");
- await vi.waitFor(() => expect(auth.login).toHaveBeenCalledOnce());
- await vi.waitFor(() =>
- expect(app.controller.snapshot().account?.managedTokenPresent).toBe(true),
- );
- expect(app.getSurface()).toBe("conversation");
- expect(app.tui.render(80).join("\n")).not.toContain("Sign in with /login");
- expect(app.tui.render(80).join("\n")).not.toContain("Login required");
- await app.submit("/logout");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().account?.managedTokenPresent).toBe(
- false,
- ),
- );
- expect(app.tui.render(80).join("\n")).toContain("Sign in with /login");
- expect(auth.login).toHaveBeenCalledWith(expect.any(Function), "en");
- expect(openedTargets).toEqual([
- "https://account.example.test/device?client_surface=tui&download_source=mcode-internal",
- logoutUrl,
- ]);
- await vi.waitFor(() =>
- expect(app.transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- kind: "warning",
- content:
- "Couldn't open the default browser. Open the authorization URL above manually.",
- }),
- expect.objectContaining({
- kind: "warning",
- content:
- "Couldn't open the default browser. Open the sign-out URL above manually.",
- }),
- ]),
- ),
- );
- expect(app.transcript.snapshot().slice(-6)).toMatchObject([
- {
- kind: "final-summary",
- content:
- "Complete MiniMax login in your browser:\nhttps://account.example.test/device?client_surface=tui&download_source=mcode-internal\nCode: ABCD-EFGH",
- },
- {
- kind: "warning",
- content:
- "Couldn't open the default browser. Open the authorization URL above manually.",
- },
- {
- kind: "final-summary",
- content:
- "Signed in with MiniMax Global. Restart MCode to use this account region.",
- },
- {
- kind: "final-summary",
- content: "Signed out of MiniMax.",
- },
- {
- kind: "final-summary",
- content: `Finish signing out in your browser:\n${logoutUrl}`,
- },
- {
- kind: "warning",
- content:
- "Couldn't open the default browser. Open the sign-out URL above manually.",
- },
- ]);
- expect(auth.login).toHaveBeenCalledOnce();
- expect(auth.logout).toHaveBeenCalledOnce();
- expect(notifyAuthContextChanged.mock.calls).toEqual([
- ["authenticated"],
- ["logged_out"],
- ]);
- expect(
- app.transcript
- .snapshot()
- .map((cell) => cell.content)
- .join("\n"),
- ).not.toContain("Sign-in wasn't completed");
- expect(
- app.transcript
- .snapshot()
- .map((cell) => cell.content)
- .join("\n"),
- ).not.toContain("Couldn't sign out");
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- await app.stop();
- });
- it("clears Runtime auth context when the shared account is already signed out", async () => {
- const notifyAuthContextChanged = vi.fn(async () => undefined);
- const logoutUrl =
- "https://agent.minimax.cn/auth/logout?logout_redirect_uri=https%3A%2F%2Fagent.minimax.cn";
- const openExternalTarget = vi.fn(() => new Promise<void>(() => {}));
- const app = createTuiApp({
- runtime: createRuntime(),
- auth: {
- login: vi.fn(),
- logout: vi.fn(async () => ({
- state: "already-signed-out" as const,
- message: "Already signed out of MiniMax.",
- logoutUrl,
- })),
- },
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- notifyAuthContextChanged,
- openExternalTarget,
- });
- app.start();
- await app.ready;
- await app.submit("/logout");
- expect(notifyAuthContextChanged).toHaveBeenCalledOnce();
- expect(notifyAuthContextChanged).toHaveBeenCalledWith("logged_out");
- expect(openExternalTarget).toHaveBeenCalledWith(logoutUrl);
- expect(app.transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- content: `Finish signing out in your browser:\n${logoutUrl}`,
- }),
- ]),
- );
- await app.stop();
- });
- it("refreshes Runtime auth context when login renews an existing account", async () => {
- const runtime = createRuntime();
- let authenticated = false;
- vi.mocked(runtime.getAccountStatus).mockImplementation(async () =>
- authenticated
- ? {
- status: "ready",
- defaultModel: "minimax/MiniMax-M2.7",
- providerId: "minimax",
- modelId: "MiniMax-M2.7",
- authMode: "managed-login",
- managedTokenPresent: true,
- warnings: [],
- }
- : {
- status: "needs-login",
- authMode: "managed-login",
- managedTokenPresent: false,
- warnings: [],
- },
- );
- const notifyAuthContextChanged = vi.fn(async () => undefined);
- const auth = {
- login: vi.fn(async () => {
- authenticated = true;
- return {
- state: "already-authenticated" as const,
- message: "Already signed in with MiniMax.",
- };
- }),
- logout: vi.fn(async () => ({
- state: "signed-out" as const,
- message: "Signed out of MiniMax.",
- })),
- };
- const app = createTuiApp({
- runtime,
- auth,
- notifyAuthContextChanged,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- expect(app.tui.render(80).join("\n")).toContain("Sign in with /login");
- await app.submit("/login");
- app.interaction.current()?.handleInput("\u001b[B");
- app.interaction.current()?.handleInput("\r");
- await vi.waitFor(() => expect(auth.login).toHaveBeenCalledOnce());
- expect(notifyAuthContextChanged).toHaveBeenCalledOnce();
- expect(notifyAuthContextChanged).toHaveBeenCalledWith("authenticated");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().account?.managedTokenPresent).toBe(true),
- );
- expect(app.getSurface()).toBe("conversation");
- expect(app.tui.render(80).join("\n")).not.toContain("Sign in with /login");
- expect(app.tui.render(80).join("\n")).not.toContain("Login required");
- await app.stop();
- });
- it("exits immediately through /quit without aborting the shared Runtime turn", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const abortSession = vi.spyOn(runtime, "abortSession");
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/quit");
- await app.stopped;
- expect(terminal.stopped).toBe(true);
- expect(abortSession).not.toHaveBeenCalled();
- });
- it("does not restore /quit after reopening the exited Session", async () => {
- const dataDir = await mkdtemp(join(tmpdir(), "mcode-quit-draft-"));
- try {
- const first = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- dataDir,
- workspaceDir: "/workspace",
- });
- await first.ready;
- await first.openSession("session-1");
- first.editor.setText("/quit");
- first.editor.handleInput("\r");
- await first.stopped;
- const restored = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- dataDir,
- workspaceDir: "/workspace",
- });
- await restored.ready;
- await restored.openSession("session-1");
- expect(restored.editor.getText()).toBe("");
- await restored.stop();
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it("keeps warning status details visible until the local panel closes", async () => {
- const terminal = new FakeTerminal();
- terminal.rows = 40;
- const runtime = createRuntime();
- vi.mocked(runtime.getAccountStatus).mockResolvedValue({
- status: "warning",
- defaultModel: "minimax/MiniMax-M2.7",
- providerId: "minimax",
- modelId: "MiniMax-M2.7",
- authMode: "managed-login",
- managedTokenPresent: true,
- warnings: ["Desktop login should be refreshed soon."],
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const transcriptBeforeStatus = app.transcript.snapshot();
- await app.submit("/status");
- const rendered = app.tui.render(80).join("\n");
- expect(app.interaction.current()).toBeDefined();
- expect(app.transcript.snapshot()).toEqual(transcriptBeforeStatus);
- expect(rendered).toContain("Needs attention");
- expect(rendered).toContain("Connected with warnings");
- expect(rendered).toContain("Model");
- expect(rendered).toContain("Desktop login should be refreshed");
- expect(rendered).toContain("soon.");
- app.interaction.current()?.handleInput?.("\x1b");
- expect(app.interaction.current()).toBeUndefined();
- expect(app.transcript.snapshot()).toEqual(transcriptBeforeStatus);
- expect(app.tui.render(80).join("\n")).not.toContain(
- "Desktop login should be refreshed",
- );
- await app.stop();
- });
- it("reports doctor failures and submits the hidden /config name as text", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.getRuntimeDiagnostics).mockRejectedValue(
- new Error("Diagnostics unavailable"),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.submit("/doctor");
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.transcript.snapshot().at(-1)).toMatchObject({
- kind: "error",
- content:
- "Couldn't load configuration check: Diagnostics unavailable. Retry.",
- });
- await app.submit("/config");
- expect(app.tui.hasOverlay()).toBe(false);
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({ id: "session-1", content: "/config" }),
- expect.any(AbortSignal),
- );
- expect(runtime.getRuntimeDiagnostics).toHaveBeenCalledTimes(1);
- });
- it("lets an async Session card replace a local status panel", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let resolveSessionPage:
- | ((page: {
- sessions: Array<{
- sessionId: string;
- title: string;
- workspaceDir: string;
- updatedAt: number;
- }>;
- hasMore: boolean;
- }) => void)
- | undefined;
- vi.mocked(runtime.listSessionPage).mockImplementationOnce(
- async () =>
- await new Promise((resolve) => {
- resolveSessionPage = resolve;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- const transcriptBeforeStatus = app.transcript.snapshot();
- const sessionCard = app.submit("/sessions");
- await vi.waitFor(() =>
- expect(runtime.listSessionPage).toHaveBeenCalledOnce(),
- );
- await app.submit("/status");
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- expect(app.transcript.snapshot()).toEqual(transcriptBeforeStatus);
- expect(app.tui.render(80).join("\n")).toContain("MCode status");
- resolveSessionPage?.({
- sessions: [
- {
- sessionId: "session-race",
- title: "Session opened after status",
- workspaceDir: "/workspace",
- updatedAt: Date.now(),
- },
- ],
- hasMore: false,
- });
- await sessionCard;
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- app.interaction.current()?.handleInput?.("\x03");
- expect(runtime.getSession).not.toHaveBeenCalled();
- expect(app.interaction.isActive()).toBe(false);
- expect(app.tui.hasOverlay()).toBe(false);
- await app.stop();
- });
- it("keeps unsupported standalone MCP inspection concise and recoverable", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.listMcpServers).mockRejectedValue(
- Object.assign(new Error("DesktopService implementation detail"), {
- status: 501,
- code: "NOT_IMPLEMENTED",
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.submit("/mcp");
- expect(app.transcript.snapshot().at(-1)).toMatchObject({
- kind: "warning",
- content:
- "MCP status is not available in this build. Configured MCP tools remain available.",
- });
- });
- it("registers enabled Skills as discoverable slash commands without shadowing built-ins", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSkills).mockResolvedValue({
- skills: [
- {
- name: "docs",
- displayName: "Docs",
- description: "Read project docs",
- enabled: true,
- sourceKind: "builtin",
- },
- {
- name: "disabled-skill",
- enabled: false,
- },
- {
- name: "help",
- enabled: true,
- },
- ...Array.from({ length: 14 }, (_, index) => ({
- name: `skill-${index}`,
- description: `Generated skill ${index}`,
- enabled: true,
- })),
- ],
- hasMore: false,
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- app.editor.setText("/docs summarize this repository");
- expect(stripAnsi(app.tui.render(80).join("\n"))).toContain(
- "Skill · instructions · Enter invoke",
- );
- app.editor.setText("/context");
- expect(stripAnsi(app.tui.render(80).join("\n"))).toContain(
- "Command · Enter run",
- );
- app.editor.setText("/context explain this repository");
- expect(stripAnsi(app.tui.render(80).join("\n"))).toContain(
- "Prompt · Enter send",
- );
- app.editor.setText("/stop");
- expect(stripAnsi(app.tui.render(80).join("\n"))).toContain(
- "Command · Enter run",
- );
- app.editor.setText("/config");
- expect(stripAnsi(app.tui.render(80).join("\n"))).toContain(
- "Prompt · Enter send",
- );
- app.editor.setText("");
- await app.submit("/help");
- expect(app.transcript.snapshot()).toEqual([]);
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- for (const character of "docs") terminal.input?.(character);
- terminal.input?.("\r");
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(false);
- expect(app.editor.getText()).toBe("");
- await app.submit("/docs summarize this repository");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- id: "session-1",
- content: "/docs summarize this repository",
- }),
- expect.any(AbortSignal),
- );
- vi.mocked(runtime.sendMessage).mockClear();
- await app.submit("/disabled-skill");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({ id: "session-1", content: "/disabled-skill" }),
- expect.any(AbortSignal),
- );
- await app.stop();
- });
- it("deduplicates configured Skills case-insensitively and reserves built-in names", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSkills).mockResolvedValue({
- skills: [
- {
- name: "HELP",
- description: "A Skill that must not shadow /help",
- enabled: true,
- },
- {
- name: "Docs",
- description: "Read project docs",
- enabled: true,
- },
- {
- name: "docs",
- description: "Duplicate project docs command",
- enabled: true,
- },
- ],
- hasMore: false,
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/help");
- const helpPanel = app.interaction.current();
- expect(helpPanel).toBeDefined();
- helpPanel?.handleInput?.("\x1b");
- await app.submit("/help [arguments]");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- id: "session-1",
- content: "/help [arguments]",
- }),
- expect.any(AbortSignal),
- );
- vi.mocked(runtime.sendMessage).mockClear();
- await app.submit("/docs explain this repository");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- id: "session-1",
- content: "/docs explain this repository",
- }),
- expect.any(AbortSignal),
- );
- await app.stop();
- });
- it("uses public Runtime owners for Session inspection commands", async () => {
- const terminal = new FakeTerminal();
- terminal.rows = 40;
- const runtime = createRuntime();
- vi.mocked(runtime.getAccountStatus).mockResolvedValue({
- status: "ready",
- defaultModel: "minimax/MiniMax-M3",
- providerId: "minimax-cn-coding-plan",
- authMode: "managed-login",
- managedTokenPresent: true,
- modelSource: "token-plan",
- tokenPlanQuotaState: "available",
- tokenPlanSummary: { tier: "Ultra Plan", creditBalance: "0" },
- tokenPlanQuota: {
- fiveHour: { remainingPercent: 100, unlimited: false },
- weekly: { remainingPercent: 99, unlimited: false },
- video: { remainingCount: 5, totalCount: 5, unlimited: false },
- },
- warnings: [],
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.submit("Create a session");
- vi.mocked(runtime.sendMessage).mockClear();
- vi.mocked(runtime.getSessionUsage).mockClear();
- vi.mocked(runtime.getContextSnapshot).mockResolvedValueOnce({
- status: "live",
- contextUsage: {
- contextWindowTokens: 10_000,
- usedTokens: 3_000,
- totalCountSource: "PROVIDER_USAGE_ANCHORED",
- components: [],
- },
- });
- const transcriptBeforeUsage = app.transcript.snapshot();
- await app.submit("/usage");
- const usageOutput = app.tui.render(80).join("\n");
- expect(app.interaction.current()).toBeDefined();
- expect(app.transcript.snapshot()).toEqual(transcriptBeforeUsage);
- expect(usageOutput).toContain("Usage");
- expect(usageOutput).toContain("120 total");
- expect(usageOutput).toContain("30% used · 70% free");
- expect(usageOutput).toContain("Ultra Plan");
- expect(usageOutput).toContain("100% left");
- app.interaction.current()?.handleInput?.("\x1b");
- expect(app.interaction.current()).toBeUndefined();
- expect(app.transcript.snapshot()).toEqual(transcriptBeforeUsage);
- await app.submit("/compact keep test evidence");
- expect(runtime.getSessionUsage).toHaveBeenCalledWith("session-1");
- expect(runtime.getContextSnapshot).toHaveBeenCalledWith("session-1");
- expect(runtime.getAccountStatus).toHaveBeenCalledWith("session-1", {
- includeMembership: true,
- forceRefresh: true,
- });
- expect(runtime.requestCompaction).toHaveBeenCalledWith(
- "session-1",
- "mavis",
- "keep test evidence",
- );
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- const output = app.transcript
- .snapshot()
- .map((cell) => cell.content)
- .join("\n");
- expect(output).not.toContain("Session usage");
- expect(output).not.toMatch(
- /Turns|Total tokens|Cost|Reasoning|Cache|Runtime|source|compaction|snapshot/i,
- );
- expect(output).not.toContain("Compaction completed");
- expect(output).not.toContain("4,000 → 1,200 tokens");
- expect(output).not.toContain("compact-1");
- });
- it("shows compaction as live activity until the Runtime request settles", async () => {
- const runtime = createRuntime();
- let resolveCompaction:
- | ((result: {
- success: true;
- sessionId: string;
- compactionId: string;
- }) => void)
- | undefined;
- vi.mocked(runtime.requestCompaction).mockImplementationOnce(
- async () =>
- await new Promise((resolve) => {
- resolveCompaction = resolve;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.submit("Create a session");
- const pending = app.submit("/compact keep decisions");
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).toContain("Compacting context"),
- );
- expect(app.tui.render(80).join("\n")).not.toContain("Esc to interrupt");
- resolveCompaction?.({
- success: true,
- sessionId: "session-1",
- compactionId: "compact-1",
- });
- await pending;
- expect(app.tui.render(80).join("\n")).not.toContain("Compacting context");
- });
- it("treats Runtime NOTHING_TO_COMPACT as a recoverable Session state", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.requestCompaction).mockRejectedValue(
- Object.assign(new Error("HTTP 400 implementation detail"), {
- status: 400,
- code: "NOTHING_TO_COMPACT",
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.submit("Create a session");
- await app.submit("/compact");
- expect(app.transcript.snapshot().at(-1)).toMatchObject({
- kind: "final-summary",
- content: "No compaction is needed for this conversation yet.",
- });
- });
- it("shows account usage before a Session exists and keeps Session-only inspection recoverable", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.getAccountStatus).mockResolvedValue({
- status: "ready",
- authMode: "byok",
- managedTokenPresent: false,
- modelSource: "byok",
- tokenPlanQuotaState: "available",
- tokenPlanSummary: { tier: "Pro Plan" },
- tokenPlanQuota: {
- fiveHour: { remainingPercent: 75, unlimited: false },
- weekly: { remainingPercent: 50, unlimited: false },
- },
- warnings: [],
- });
- const terminal = new FakeTerminal();
- terminal.rows = 40;
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.submit("/usage");
- const usageOutput = app.tui.render(80).join("\n");
- expect(app.interaction.current()).toBeDefined();
- expect(app.transcript.snapshot()).toEqual([]);
- expect(usageOutput).toContain("Usage");
- expect(usageOutput).toContain("Account only");
- expect(usageOutput).toContain("Pro Plan");
- expect(usageOutput).toContain("75% left");
- app.interaction.current()?.handleInput?.("\x1b");
- expect(app.interaction.current()).toBeUndefined();
- expect(app.transcript.snapshot()).toEqual([]);
- await app.submit("/compact");
- expect(runtime.getSessionUsage).not.toHaveBeenCalled();
- expect(runtime.getContextSnapshot).not.toHaveBeenCalled();
- expect(runtime.getAccountStatus).toHaveBeenCalledWith(undefined, {
- includeMembership: true,
- forceRefresh: true,
- });
- expect(app.transcript.snapshot()).toEqual([]);
- expect(app.tui.render(80).join("\n")).toContain(
- "Start or resume a Session before compacting it.",
- );
- });
- it("keeps prompt history and long bracketed paste intact in the Composer", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- terminal.input?.("first prompt");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledTimes(1),
- );
- terminal.input?.("second prompt");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledTimes(2),
- );
- terminal.input?.("\x1b[A");
- expect(app.editor.getText()).toBe("second prompt");
- terminal.input?.("\x1b[A");
- expect(app.editor.getText()).toBe("first prompt");
- terminal.input?.("\x1b[B");
- expect(app.editor.getText()).toBe("second prompt");
- app.editor.setText("");
- const pasted = `Explain this payload:\n${"x".repeat(1_200)}`;
- terminal.input?.(`\x1b[200~${pasted}\x1b[201~`);
- expect(app.editor.getText()).toContain("[paste #");
- expect(app.editor.getExpandedText()).toBe(pasted);
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledTimes(3),
- );
- expect(runtime.sendMessage).toHaveBeenLastCalledWith(
- expect.objectContaining({ id: "session-1", content: pasted }),
- expect.any(AbortSignal),
- );
- await app.stop();
- });
- it("trusts bracketed paste and submits dense typed input without a false newline", async () => {
- const terminal = new FakeTerminal();
- Object.defineProperty(terminal, "capabilities", {
- value: {
- terminalId: "unknown",
- platform: "linux",
- isTTY: true,
- transport: "local",
- multiplexer: "none",
- color: true,
- colorLevel: 3,
- } satisfies TerminalCapabilities,
- });
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- for (const character of "abcdefgh") terminal.input?.(character);
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledTimes(1),
- );
- expect(app.editor.getText()).toBe("");
- await app.stop();
- });
- it("projects an otty image paste as an inline editor placeholder", async () => {
- const terminal = new FakeTerminal();
- const resolveAttachment = vi.fn(async () => ({
- type: "image" as const,
- filePath: "/tmp/otty-paste/image-123.png",
- fileName: "image-123.png",
- mimeType: "image/png",
- sizeBytes: 1_024,
- }));
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- resolveAttachment,
- });
- app.start();
- await app.ready;
- terminal.input?.("\x1b[200~/tmp/otty-paste/image-123.png\x1b[201~");
- await vi.waitFor(() => expect(resolveAttachment).toHaveBeenCalledTimes(1));
- const rendered = app.tui.render(80).join("\n");
- expect(app.editor.getText()).toBe("[Image #1] ");
- expect(rendered).toContain("[Image #1]");
- expect(rendered).not.toContain("/tmp/otty-paste/");
- await app.stop();
- });
- it.each(["regular", "fullscreen"] as const)(
- "keeps image preview controls separate from Enter submission in %s mode",
- async (tuiMode) => {
- const terminal = new FakeTerminal();
- terminal.rows = 40;
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- tuiMode,
- version: "0.1.0",
- workspaceDir: "/workspace",
- resolveAttachment: async () => ({
- type: "image",
- filePath: "/tmp/image-preview-missing.png",
- fileName: "preview.png",
- mimeType: "image/png",
- sizeBytes: 32768,
- }),
- });
- app.start();
- await app.ready;
- terminal.input?.("\x1b[200~/tmp/image-preview-missing.png\x1b[201~");
- await vi.waitFor(() =>
- expect(app.editor.getAttachmentPreview()?.id).toBe(
- "/tmp/image-preview-missing.png",
- ),
- );
- expect(app.editor.getText()).toBe("[Image #1] ");
- expect(app.tui.render(80).join("\n")).toContain("preview.png");
- expect(app.tui.render(80).join("\n")).toContain("32 KB");
- expect(app.tui.render(80).join("\n")).not.toMatch(/Option\+I|Alt\+I/);
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- terminal.input?.("\x1b");
- expect(app.editor.getAttachmentPreview()).toBeUndefined();
- terminal.input?.("\x1b[D");
- expect(app.editor.getAttachmentPreview()?.id).toBe(
- "/tmp/image-preview-missing.png",
- );
- terminal.input?.("\r");
- await vi.waitFor(() => expect(runtime.sendMessage).toHaveBeenCalled());
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- content: "",
- attachments: [
- expect.objectContaining({
- local: { filePath: "/tmp/image-preview-missing.png" },
- }),
- ],
- }),
- expect.any(AbortSignal),
- );
- expect(app.editor.getAttachmentPreview()).toBeUndefined();
- await app.stop();
- },
- );
- it("projects a pasted absolute WeChat image path as a short inline placeholder", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const wechatImagePath =
- "D:\\Yingyong\\social\\wechat\\xwechat_files\\wxid_demo\\temp\\RWTemp\\2026-08\\3336519cd4d5351f4bc5a5a68c7fea75.png";
- const resolveAttachment = vi.fn(async () => ({
- type: "image" as const,
- filePath: wechatImagePath,
- fileName: "3336519cd4d5351f4bc5a5a68c7fea75.png",
- mimeType: "image/png",
- sizeBytes: 1_024,
- }));
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "D:\\workspace",
- resolveAttachment,
- });
- app.start();
- await app.ready;
- terminal.input?.(`\x1b[200~'${wechatImagePath}'\x1b[201~`);
- await vi.waitFor(() => expect(resolveAttachment).toHaveBeenCalledOnce());
- expect(resolveAttachment).toHaveBeenCalledWith(`'${wechatImagePath}'`, {
- source: "terminal-paste",
- workspaceDir: "D:\\workspace",
- });
- expect(app.editor.getText()).toBe("[Image #1] ");
- expect(app.tui.render(80).join("\n")).not.toContain("xwechat_files");
- await app.submit("Inspect this image");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- content: "Inspect this image",
- attachments: [
- expect.objectContaining({
- local: { filePath: wechatImagePath },
- }),
- ],
- }),
- expect.any(AbortSignal),
- );
- await app.stop();
- });
- it("keeps an image placeholder visible before a long CJK prompt", async () => {
- const terminal = new FakeTerminal();
- const resolveAttachment = vi.fn(async () => ({
- type: "image" as const,
- filePath: "/tmp/otty-paste/logo.png",
- fileName: "logo.png",
- mimeType: "image/png",
- sizeBytes: 1_024,
- }));
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- resolveAttachment,
- });
- app.start();
- await app.ready;
- terminal.input?.("\x1b[200~/tmp/otty-paste/logo.png\x1b[201~");
- await vi.waitFor(() => expect(resolveAttachment).toHaveBeenCalledTimes(1));
- app.editor.insertTextAtCursor(
- "logo 不太对还是,还出现了奇怪的--; logo你自己截取把:",
- );
- const rendered = app.tui.render(80).join("\n");
- expect(rendered).toContain("[Image #1]");
- await app.stop();
- });
- it("removes an inline image placeholder and its attachment with Backspace", async () => {
- const terminal = new FakeTerminal();
- const resolveAttachment = vi.fn(async () => ({
- type: "image" as const,
- filePath: "/tmp/otty-paste/image-123.png",
- fileName: "image-123.png",
- mimeType: "image/png",
- sizeBytes: 1_024,
- }));
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- resolveAttachment,
- });
- app.start();
- await app.ready;
- app.editor.setText("first line\nsecond line");
- terminal.input?.("\x1b[200~/tmp/otty-paste/image-123.png\x1b[201~");
- await vi.waitFor(() => expect(resolveAttachment).toHaveBeenCalledTimes(1));
- expect(app.editor.getText()).toBe("first line\nsecond line [Image #1] ");
- expect(app.tui.render(80).join("\n")).toContain("[Image #1]");
- terminal.input?.("\x7f");
- await vi.waitFor(() =>
- expect(app.editor.getText().trimEnd()).toBe("first line\nsecond line"),
- );
- expect(app.tui.render(80).join("\n")).not.toContain("[Image #1]");
- await app.stop();
- });
- it("resumes Desktop history, supports rename, and executes the files-unchanged fork flow", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-existing",
- title: "Existing session",
- workspaceDir: "/workspace",
- updatedAt: 200,
- },
- ]);
- const history = [
- {
- id: "msg-user-v1-fork-source",
- turnId: "turn-history",
- role: "user" as const,
- content: "Try the original approach",
- timestamp: 90,
- },
- {
- id: "assistant-fork-source",
- turnId: "turn-history",
- role: "assistant" as const,
- content: "Recovered answer",
- timestamp: 100,
- actions: { fork: true },
- },
- ];
- const forkRuntime = runtime as TuiRuntime & TuiSessionForkPort;
- vi.mocked(runtime.getMessages).mockResolvedValue(history);
- vi.mocked(runtime.listMessagePage).mockResolvedValue({
- messages: history,
- hasMore: false,
- });
- vi.mocked(runtime.getSession).mockImplementation(async (sessionId) =>
- sessionId === "session-fork"
- ? {
- sessionId,
- parentSessionId: "session-existing",
- title: "Existing session (fork 1)",
- workspaceDir: "/workspace",
- }
- : {
- sessionId,
- title: "Existing session",
- workspaceDir: "/workspace",
- },
- );
- vi.mocked(forkRuntime.getSessionForkOptions).mockResolvedValue({
- canFork: true,
- suggestedTitle: "Existing session (fork 1)",
- worktreeVisible: false,
- worktreeEligible: false,
- });
- vi.mocked(forkRuntime.forkSession).mockResolvedValue({
- session: {
- sessionId: "session-fork",
- parentSessionId: "session-existing",
- title: "Existing session (fork 1)",
- workspaceDir: "/workspace",
- },
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/sessions");
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- app.interaction.current()?.handleInput?.("\x1b");
- expect(app.interaction.isActive()).toBe(false);
- await app.submit("/resume 1");
- expect(app.getSurface()).toBe("conversation");
- vi.mocked(runtime.renameSession).mockClear();
- await app.submit("/rename");
- expect(runtime.renameSession).not.toHaveBeenCalled();
- expect(app.interaction.isActive()).toBe(true);
- expect(app.tui.render(100).join("\n")).toContain(
- "Sessions / Rename session",
- );
- app.interaction.current()?.handleInput?.("\x1b");
- expect(app.tui.render(100).join("\n")).toContain("Sessions");
- app.interaction.current()?.handleInput?.("\x1b");
- expect(app.interaction.isActive()).toBe(false);
- await app.submit("/rename Renamed session");
- vi.mocked(runtime.createSession).mockClear();
- await app.submit("/fork");
- await vi.waitFor(() =>
- expect(runtime.listSessionInputSummaries).toHaveBeenCalledWith(
- "session-existing",
- {
- limit: 100,
- },
- ),
- );
- // The new fork flow owns the surface with the history picker, not the
- // generic transcript panel.
- expect(renderTerminalViewport(app, terminal)).toContain(
- "Fork from a previous message",
- );
- expect(renderTerminalViewport(app, terminal)).not.toContain("Transcript");
- // Esc cancels without calling the fork Runtime API.
- const beforeForkCalls = vi.mocked(runtime.forkSession).mock.calls.length;
- app.surfaceHost.popFeature();
- expect(vi.mocked(runtime.forkSession).mock.calls.length).toBe(
- beforeForkCalls,
- );
- const transcriptBeforeStatus = app.transcript.snapshot();
- await app.submit("/status");
- expect(runtime.getSession).toHaveBeenCalledWith("session-existing");
- expect(app.interaction.current()).toBeDefined();
- expect(app.transcript.snapshot()).toEqual(transcriptBeforeStatus);
- expect(app.tui.render(100).join("\n")).toContain("MCode status");
- expect(runtime.renameSession).toHaveBeenCalledWith(
- "session-existing",
- "Renamed session",
- );
- expect(runtime.createSession).not.toHaveBeenCalled();
- expect(app.transcript.snapshot()).not.toEqual(
- expect.arrayContaining([
- expect.objectContaining({ content: "Unknown command. Use /help." }),
- ]),
- );
- app.interaction.current()?.handleInput?.("\x1b");
- await app.stop();
- });
- it.each(["regular", "fullscreen"] as const)(
- "presents /history in %s mode and returns to chat on cancel",
- async (tuiMode) => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-history",
- assistantMessageId: "msg-assistant-history",
- contentHead: "Inspect the Session UI",
- timestamp: 1_700_000_000_000,
- fileChangeCount: 1,
- },
- ]);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- tuiMode,
- });
- app.start();
- await app.ready;
- await app.submit("Seed the session");
- await app.submit("/history");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-history:explorer",
- }),
- );
- expect(renderTerminalViewport(app, terminal)).toContain(
- "Inspect the Session UI",
- );
- expect(renderTerminalViewport(app, terminal)).toContain("Search:");
- expect(renderTerminalViewport(app, terminal)).toContain("Enter actions");
- terminal.input?.("\u001b");
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "chat",
- id: "chat",
- });
- await app.stop();
- },
- );
- it("forks with the assistant boundary and starts a new operation after a terminal failure", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-1",
- assistantMessageId: "msg-assistant-1",
- contentHead: "Investigate flaky login tests",
- timestamp: 1_700_000_000_000,
- fileChangeCount: 2,
- },
- {
- userMessageId: "msg-user-2",
- assistantMessageId: "msg-assistant-2",
- contentHead: "Draft a new CLI handler",
- timestamp: 1_699_999_000_000,
- fileChangeCount: 0,
- },
- ]);
- vi.mocked(runtime.getSessionForkOptions).mockResolvedValue({
- canFork: true,
- worktreeVisible: false,
- worktreeEligible: false,
- });
- vi.mocked(runtime.forkSession).mockRejectedValueOnce(
- Object.assign(new Error("persisted fork recovery failed"), {
- key: "FORK_RECOVERY_FAILED",
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- // Submit a message so the controller has an active session id.
- await app.submit("Seed the session");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().session?.sessionId).toBe("session-1"),
- );
- const sourceSessionId = app.controller.snapshot().session?.sessionId;
- expect(sourceSessionId).toBe("session-1");
- const transcriptBefore = app.transcript.snapshot();
- vi.mocked(runtime.createSession).mockClear();
- vi.mocked(runtime.forkSession).mockClear();
- vi.mocked(runtime.getSessionForkOptions).mockClear();
- vi.mocked(runtime.listSessionInputSummaries).mockClear();
- await app.submit("/fork");
- // 1. /fork loads input summaries, not the generic transcript.
- await vi.waitFor(() =>
- expect(runtime.listSessionInputSummaries).toHaveBeenCalledWith(
- sourceSessionId,
- {
- limit: 100,
- },
- ),
- );
- expect(renderTerminalViewport(app, terminal)).toContain(
- "Fork from a previous message",
- );
- expect(renderTerminalViewport(app, terminal)).not.toContain("Transcript");
- // 2. The newest summary (last entry) is selected by default; confirm with Enter.
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:history",
- }),
- );
- terminal.input?.("\r");
- // The history selection only fetches options; it must not mutate yet.
- await vi.waitFor(() =>
- expect(runtime.getSessionForkOptions).toHaveBeenCalledWith(
- sourceSessionId,
- "msg-assistant-2",
- ),
- );
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:fork-confirm",
- }),
- );
- expect(runtime.forkSession).not.toHaveBeenCalled();
- expect(renderTerminalViewport(app, terminal)).toContain("Confirm fork");
- // 3. Explicit confirmation performs the mutation.
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.forkSession).toHaveBeenCalledWith(
- expect.objectContaining({
- sessionId: sourceSessionId,
- assistantMessageId: "msg-assistant-2",
- useSuggestedTitle: true,
- createIsolatedWorktree: false,
- }),
- ),
- );
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:fork-confirm",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.forkSession).toHaveBeenCalledTimes(2),
- );
- const forkCalls = vi.mocked(runtime.forkSession).mock.calls;
- const operationIds = forkCalls.map(([input]) => input.clientRequestId);
- expect(operationIds[0]).toBeTruthy();
- expect(operationIds[1]).toBeTruthy();
- expect(operationIds[0]).not.toBe(operationIds[1]);
- // 5. Activation moves the controller to the child session id.
- await vi.waitFor(() =>
- expect(app.controller.snapshot().session?.sessionId).toBe("session-fork"),
- );
- expect(runtime.createSession).not.toHaveBeenCalled();
- // The user message that seeded the source session was already projected
- // into the transcript before the fork ran; the fork flow must not write
- // any new user-typed cell while the picker is open or the mutation runs.
- expect(
- transcriptBefore.filter((cell) => cell.kind === "user").length,
- ).toBeGreaterThan(0);
- });
- it("does not call forkSession when /fork is cancelled, unavailable, or fails", async () => {
- const scenarios: ReadonlyArray<{
- readonly label: string;
- readonly setup: (runtime: ReturnType<typeof createRuntime>) => void;
- readonly interact: (terminal: FakeTerminal) => void | Promise<void>;
- /** When true, expect forkSession to have been called exactly once. */
- readonly expectForkCall: boolean;
- readonly confirmAfterSelection?: boolean;
- }> = [
- {
- label: "esc cancels the picker without invoking fork",
- setup: (runtime) => {
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-1",
- assistantMessageId: "msg-assistant-1",
- contentHead: "Investigate flaky login tests",
- timestamp: 1_700_000_000_000,
- fileChangeCount: 0,
- },
- ]);
- },
- interact: (terminal) => {
- terminal.input?.("\u001b");
- },
- expectForkCall: false,
- },
- {
- label: "unavailable fork surfaces a warning without invoking fork",
- setup: (runtime) => {
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-1",
- assistantMessageId: "msg-assistant-1",
- contentHead: "Investigate flaky login tests",
- timestamp: 1_700_000_000_000,
- fileChangeCount: 0,
- },
- ]);
- vi.mocked(runtime.getSessionForkOptions).mockResolvedValue({
- canFork: false,
- unavailableReason: "No persisted user prompts yet.",
- worktreeVisible: false,
- worktreeEligible: false,
- });
- },
- interact: (terminal) => {
- terminal.input?.("\r");
- },
- expectForkCall: false,
- },
- {
- label:
- "forkSession failure preserves the source session and transcript",
- setup: (runtime) => {
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-1",
- assistantMessageId: "msg-assistant-1",
- contentHead: "Investigate flaky login tests",
- timestamp: 1_700_000_000_000,
- fileChangeCount: 0,
- },
- ]);
- vi.mocked(runtime.getSessionForkOptions).mockResolvedValue({
- canFork: true,
- worktreeVisible: false,
- worktreeEligible: false,
- });
- vi.mocked(runtime.forkSession).mockRejectedValue(
- new Error("Runtime offline."),
- );
- },
- interact: (terminal) => {
- terminal.input?.("\r");
- },
- expectForkCall: true,
- confirmAfterSelection: true,
- },
- ];
- for (const scenario of scenarios) {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- scenario.setup(runtime);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed the session");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().session?.sessionId).toBe("session-1"),
- );
- const sourceSessionId = app.controller.snapshot().session?.sessionId;
- const transcriptBefore = app.transcript.snapshot();
- await app.submit("/fork");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:history",
- }),
- );
- await scenario.interact(terminal);
- if (scenario.expectForkCall || scenario.label.includes("unavailable")) {
- await vi.waitFor(() =>
- expect(runtime.getSessionForkOptions).toHaveBeenCalled(),
- );
- }
- expect(runtime.forkSession).not.toHaveBeenCalled();
- if (scenario.confirmAfterSelection) {
- terminal.input?.("\r");
- }
- // Give the async fork pipeline a chance to settle.
- await new Promise((resolve) => setTimeout(resolve, 40));
- if (scenario.expectForkCall) {
- expect(
- vi.mocked(runtime.forkSession).mock.calls.length,
- `${scenario.label} should call forkSession once`,
- ).toBe(1);
- } else {
- expect(
- vi.mocked(runtime.forkSession).mock.calls.length,
- `${scenario.label} should not call forkSession`,
- ).toBe(0);
- }
- expect(
- app.controller.snapshot().session?.sessionId,
- `${scenario.label} must not switch sessions`,
- ).toBe(sourceSessionId);
- expect(
- app.transcript.snapshot().filter((cell) => cell.kind === "user").length,
- `${scenario.label} must preserve the source transcript`,
- ).toBe(transcriptBefore.filter((cell) => cell.kind === "user").length);
- await app.stop();
- }
- });
- it("does not call forkSession when the source session changes between summary and selection", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-1",
- assistantMessageId: "msg-assistant-1",
- contentHead: "Investigate flaky login tests",
- timestamp: 1_700_000_000_000,
- fileChangeCount: 0,
- },
- ]);
- vi.mocked(runtime.getSessionForkOptions).mockResolvedValue({
- canFork: true,
- worktreeVisible: false,
- worktreeEligible: false,
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed the session");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().session?.sessionId).toBe("session-1"),
- );
- await app.submit("/fork");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:history",
- }),
- );
- // Simulate the source session changing while the picker is open.
- const otherSession = {
- sessionId: "session-other",
- title: "Other session",
- workspaceDir: "/workspace",
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([otherSession]);
- vi.mocked(runtime.getSession).mockImplementation(async (sessionId) =>
- sessionId === otherSession.sessionId
- ? otherSession
- : { sessionId, workspaceDir: "/workspace" },
- );
- await app.openSession(otherSession.sessionId);
- expect(app.controller.snapshot().session?.sessionId).toBe(
- otherSession.sessionId,
- );
- // Confirming the picker now must not call forkSession.
- terminal.input?.("\r");
- await new Promise((resolve) => setTimeout(resolve, 20));
- expect(vi.mocked(runtime.forkSession)).not.toHaveBeenCalled();
- expect(vi.mocked(runtime.getSessionForkOptions).mock.calls.length).toBe(0);
- });
- it("preserves the /fork command draft and skips mutation when a turn is running", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishTurn: (() => void) | undefined;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- await new Promise<void>((resolve) => {
- finishTurn = resolve;
- });
- yield { type: "done" };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const submission = app.submit("Seed the session");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().session?.sessionId).toBe("session-1"),
- );
- await vi.waitFor(() =>
- expect(vi.mocked(runtime.sendMessage)).toHaveBeenCalled(),
- );
- const summariesBefore = vi.mocked(runtime.listSessionInputSummaries).mock
- .calls.length;
- const forkOptionsBefore = vi.mocked(runtime.getSessionForkOptions).mock
- .calls.length;
- const forkBefore = vi.mocked(runtime.forkSession).mock.calls.length;
- // While the turn is still running, /fork should preserve the draft and
- // skip every runtime mutation method.
- app.editor.setText("/fork");
- await app.submit("/fork");
- expect(app.editor.getText()).toBe("/fork");
- expect(vi.mocked(runtime.listSessionInputSummaries).mock.calls.length).toBe(
- summariesBefore,
- );
- expect(vi.mocked(runtime.getSessionForkOptions).mock.calls.length).toBe(
- forkOptionsBefore,
- );
- expect(vi.mocked(runtime.forkSession).mock.calls.length).toBe(forkBefore);
- // The fork surface should not have been pushed either.
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "chat",
- id: "chat",
- });
- finishTurn?.();
- await submission;
- await app.stop();
- });
- it.each([
- ["conversation", []],
- ["conversation_and_files", ["\u001b[B"]],
- ] as const)(
- "runs /rewind with explicit %s scope and stable operation id",
- async (scope, navigation) => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-rewind",
- contentHead: "Undo this point",
- timestamp: 1_700_000_000_000,
- fileChangeCount: 2,
- },
- ]);
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({
- turns: [
- {
- turnId: "turn-1",
- files: [
- { filePath: "src/ready.ts", action: "modified", skipped: false },
- { filePath: "src/skipped.ts", action: "deleted", skipped: true },
- ],
- },
- ],
- });
- vi.mocked(runtime.rewindSession).mockResolvedValue(
- scope === "conversation"
- ? { rewound: true }
- : {
- rewound: true,
- turnDiffRewind: {
- status: "rewound",
- revertedTurnIds: ["turn-1"],
- },
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed rewind session");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().session?.sessionId).toBe("session-1"),
- );
- expect(
- app.transcript.snapshot().some((cell) => cell.kind === "turn-duration"),
- ).toBe(true);
- vi.mocked(runtime.getMessages).mockResolvedValue([]);
- await app.submit("/rewind");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:history",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.getSessionRewindPreview).toHaveBeenCalledWith({
- sessionId: "session-1",
- userMessageId: "msg-user-rewind",
- }),
- );
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-preview",
- }),
- );
- expect(renderTerminalViewport(app, terminal)).toContain("src/ready.ts");
- expect(renderTerminalViewport(app, terminal)).toContain("src/skipped.ts");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:scope",
- }),
- );
- for (const key of navigation) terminal.input?.(key);
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-confirm",
- }),
- );
- expect(runtime.rewindSession).not.toHaveBeenCalled();
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.rewindSession).toHaveBeenCalledTimes(1),
- );
- const input = vi.mocked(runtime.rewindSession).mock.calls[0]?.[0];
- expect(input).toEqual({
- sessionId: "session-1",
- userMessageId: "msg-user-rewind",
- clientRequestId: expect.stringMatching(/^tui-rewind_/),
- ...(scope === "conversation_and_files" ? { rewindTurnDiff: true } : {}),
- });
- expect(input?.clientRequestId).toMatch(/^tui-rewind_/);
- await vi.waitFor(() => expect(runtime.getMessages).toHaveBeenCalled());
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "chat",
- id: "chat",
- }),
- );
- expect(
- app.transcript
- .snapshot()
- .some((cell) => cell.content === "Rewind completed."),
- ).toBe(false);
- expect(app.transcript.snapshot()).toEqual([]);
- await app.stop();
- },
- );
- it("edits only the latest user turn, hides that turn while editing, and adds no completion cell", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-older",
- assistantMessageId: "msg-assistant-older",
- contentHead: "Older prompt",
- timestamp: 1,
- fileChangeCount: 0,
- },
- {
- userMessageId: "msg-user-latest",
- assistantMessageId: "msg-assistant-latest",
- contentHead: "Latest prompt",
- timestamp: 2,
- fileChangeCount: 0,
- },
- ]);
- vi.mocked(runtime.listMessagePage).mockResolvedValue({
- messages: [
- {
- id: "msg-user-older",
- role: "user",
- content: "Older prompt",
- timestamp: 1,
- },
- {
- id: "msg-user-latest",
- role: "user",
- content: "Latest prompt",
- timestamp: 2,
- },
- ],
- hasMore: false,
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed edit session");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().session?.sessionId).toBe("session-1"),
- );
- app.transcript.upsert({
- id: "older-user-cell",
- kind: "user",
- status: "succeeded",
- content: "Older prompt",
- sourceMessageId: "msg-user-older",
- turnId: "turn-older",
- createdAtMs: 1,
- });
- app.transcript.upsert({
- id: "latest-user-cell",
- kind: "user",
- status: "succeeded",
- content: "Latest prompt",
- sourceMessageId: "msg-user-latest",
- turnId: "turn-latest",
- createdAtMs: 2,
- });
- app.transcript.upsert({
- id: "latest-assistant-cell",
- kind: "assistant",
- status: "succeeded",
- content: "Latest response",
- sourceMessageId: "msg-assistant-latest",
- turnId: "turn-latest",
- createdAtMs: 3,
- });
- await app.submit("/edit");
- await vi.waitFor(() =>
- expect(app.editor.getExpandedText()).toBe("Latest prompt"),
- );
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "chat",
- id: "chat",
- });
- const editingFrame = app.tui.render(100).join("\n");
- expect(editingFrame).toContain("Older prompt");
- expect(editingFrame).not.toContain("Latest response");
- const historyRefreshCount = vi.mocked(runtime.getMessages).mock.calls
- .length;
- app.editor.setText("Edited latest prompt");
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.editSessionMessage).toHaveBeenCalledTimes(1),
- );
- await vi.waitFor(() =>
- expect(vi.mocked(runtime.getMessages).mock.calls.length).toBeGreaterThan(
- historyRefreshCount,
- ),
- );
- expect(
- app.transcript
- .snapshot()
- .some(
- (cell) =>
- cell.content ===
- "Edited message submitted; regenerating from this point.",
- ),
- ).toBe(false);
- expect(runtime.editSessionMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- userMessageId: "msg-user-latest",
- content: "Edited latest prompt",
- }),
- );
- await app.stop();
- });
- it("restores the hidden latest turn when Edit is cancelled", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-latest",
- assistantMessageId: "msg-assistant-latest",
- contentHead: "Latest prompt",
- timestamp: 2,
- fileChangeCount: 0,
- },
- ]);
- vi.mocked(runtime.listMessagePage).mockResolvedValue({
- messages: [
- {
- id: "msg-user-latest",
- role: "user",
- content: "Latest prompt",
- timestamp: 2,
- },
- ],
- hasMore: false,
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed edit session");
- app.transcript.upsert({
- id: "latest-user-cell",
- kind: "user",
- status: "succeeded",
- content: "Latest prompt",
- sourceMessageId: "msg-user-latest",
- turnId: "turn-latest",
- createdAtMs: 2,
- });
- app.transcript.upsert({
- id: "latest-assistant-cell",
- kind: "assistant",
- status: "succeeded",
- content: "Latest response restored on cancel",
- sourceMessageId: "msg-assistant-latest",
- turnId: "turn-latest",
- createdAtMs: 3,
- });
- await app.submit("/edit");
- await vi.waitFor(() =>
- expect(app.editor.getExpandedText()).toBe("Latest prompt"),
- );
- expect(app.tui.render(100).join("\n")).not.toContain(
- "Latest response restored on cancel",
- );
- terminal.input?.("\u001b");
- await vi.waitFor(() => expect(app.editor.getExpandedText()).toBe(""));
- expect(app.tui.render(100).join("\n")).toContain(
- "Latest response restored on cancel",
- );
- expect(runtime.editSessionMessage).not.toHaveBeenCalled();
- await app.stop();
- });
- it.each([
- { reference: "filePath", action: "cancel" },
- { reference: "assetId", action: "send" },
- { reference: "assetId", action: "delete" },
- { reference: "filePath", action: "undo" },
- { reference: "assetId", action: "add" },
- { reference: "filePath", action: "clear-restore" },
- { reference: "filePath", action: "cached" },
- ] as const)(
- "restores editable image chips after double Escape and retry ($reference / $action)",
- async ({ reference, action }) => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-latest",
- assistantMessageId: "msg-assistant-latest",
- contentHead: "Latest prompt from shortcut",
- timestamp: 2,
- fileChangeCount: 0,
- },
- ]);
- vi.mocked(runtime.listMessagePage).mockResolvedValue({
- messages: [
- {
- id: "msg-user-latest",
- role: "user",
- content: "Latest prompt from shortcut",
- attachments: [
- {
- type: "image",
- fileName: "edit-reference.png",
- mimeType: "image/png",
- [reference]:
- reference === "filePath"
- ? "/workspace/edit-reference.png"
- : "asset-edit-image",
- },
- ],
- timestamp: 2,
- },
- ],
- hasMore: false,
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- resolveAttachment: async (path) => ({
- type: "image",
- filePath: path,
- fileName: path.endsWith("new.png") ? "new.png" : "edit-reference.png",
- mimeType: "image/png",
- sizeBytes: 123,
- }),
- });
- app.start();
- await app.ready;
- let restoredText = "Latest prompt from shortcut [Image #1] ";
- if (action === "cached") {
- terminal.input?.("\x1b[200~/workspace/edit-reference.png\x1b[201~");
- await vi.waitFor(() =>
- expect(app.editor.getText()).toContain("[Image #1]"),
- );
- app.editor.handleInput("Latest prompt from shortcut");
- restoredText = app.editor.getText();
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledTimes(1),
- );
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toBeUndefined(),
- );
- } else {
- await app.submit("Seed edit session");
- }
- terminal.input?.("\u001b");
- terminal.input?.("\u001b");
- await vi.waitFor(() =>
- expect(app.editor.getExpandedText()).toBe(restoredText),
- );
- expect(runtime.listSessionInputSummaries).toHaveBeenCalledWith(
- "session-1",
- { limit: 100 },
- );
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "chat",
- id: "chat",
- });
- expect(stripAnsi(app.tui.render(100).join("\n"))).toContain("[Image #1]");
- vi.mocked(runtime.editSessionMessage).mockRejectedValueOnce(
- new Error("Temporary failure"),
- );
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.editSessionMessage).toHaveBeenCalledTimes(1),
- );
- await vi.waitFor(() =>
- expect(app.editor.getExpandedText()).toBe(restoredText),
- );
- expect(stripAnsi(app.tui.render(100).join("\n"))).toContain("[Image #1]");
- expect(runtime.editSessionMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- content: "Latest prompt from shortcut",
- attachments: [
- expect.objectContaining({
- fileName: "edit-reference.png",
- [reference]:
- reference === "filePath"
- ? "/workspace/edit-reference.png"
- : "asset-edit-image",
- }),
- ],
- }),
- );
- if (action === "delete" || action === "undo") {
- app.editor.handleInput("\x05");
- app.editor.handleInput("\x7f");
- await vi.waitFor(() =>
- expect(app.editor.getText()).not.toContain("[Image #1]"),
- );
- if (action === "undo") {
- app.editor.handleInput("\x1f");
- expect(app.editor.getText()).toContain("[Image #1]");
- }
- }
- if (action === "add") {
- terminal.input?.("\x1b[200~/workspace/new.png\x1b[201~");
- await vi.waitFor(() =>
- expect(app.editor.getText()).toContain("[Image #2]"),
- );
- expect(app.editor.captureDraft().attachmentPlaceholders).toHaveLength(
- 2,
- );
- }
- if (action === "clear-restore") {
- terminal.input?.("\x03");
- expect(app.editor.getText()).toBe("");
- terminal.input?.("\x1f");
- expect(app.editor.getText()).toContain("[Image #1]");
- }
- if (action === "cancel") {
- terminal.input?.("\u001b");
- } else {
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.editSessionMessage).toHaveBeenCalledTimes(2),
- );
- const sent = vi.mocked(runtime.editSessionMessage).mock.calls[1]?.[0];
- expect(sent?.content).toBe("Latest prompt from shortcut");
- expect(sent?.attachments ?? []).toHaveLength(
- action === "delete" ? 0 : action === "add" ? 2 : 1,
- );
- if (action !== "delete")
- expect(sent?.attachments?.[0]?.fileName).toBe("edit-reference.png");
- }
- await vi.waitFor(() =>
- expect(stripAnsi(app.tui.render(100).join("\n"))).not.toContain(
- "[Image #1]",
- ),
- );
- await app.stop();
- },
- );
- it("edits the latest committed user turn through the desktop-aligned mutation contract", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-v1-target",
- assistantMessageId: "msg-assistant-v1-target",
- contentHead: "Original prompt head",
- timestamp: 1_700_000_000_000,
- fileChangeCount: 1,
- },
- ]);
- vi.mocked(runtime.listMessagePage).mockResolvedValue({
- messages: [
- {
- id: "msg-user-v1-target",
- role: "user",
- content: "Original complete prompt",
- attachments: [
- {
- type: "file",
- fileName: "spec.md",
- mimeType: "text/markdown",
- sizeBytes: 42,
- filePath: "/workspace/spec.md",
- },
- ],
- timestamp: 1_700_000_000_000,
- },
- ],
- hasMore: false,
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed edit session");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().session?.sessionId).toBe("session-1"),
- );
- await app.submit("/edit");
- await vi.waitFor(() =>
- expect(app.editor.getExpandedText()).toBe(
- "Original complete prompt [File #1] ",
- ),
- );
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "chat",
- id: "chat",
- });
- app.editor.setText("Updated prompt");
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.editSessionMessage).toHaveBeenCalledTimes(1),
- );
- expect(runtime.editSessionMessage).toHaveBeenCalledWith({
- sessionId: "session-1",
- userMessageId: "msg-user-v1-target",
- clientRequestId: expect.stringMatching(/^tui-edit_/u),
- content: "Updated prompt",
- attachments: [
- {
- type: "file",
- fileName: "spec.md",
- mimeType: "text/markdown",
- sizeBytes: 42,
- filePath: "/workspace/spec.md",
- },
- ],
- rewindTurnDiff: true,
- });
- await app.stop();
- });
- it("keeps hidden chat context out of the editor and restores it for Edit transport", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const transportContent = [
- "<user-provided-context>",
- "Referenced context",
- "</user-provided-context>",
- "",
- "Original visible prompt",
- ].join("\n");
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-v1-context",
- assistantMessageId: "msg-assistant-v1-context",
- contentHead: "Original visible prompt",
- timestamp: 1_700_000_000_000,
- fileChangeCount: 0,
- },
- ]);
- vi.mocked(runtime.listMessagePage).mockResolvedValue({
- messages: [
- {
- id: "msg-user-v1-context",
- role: "user",
- content: transportContent,
- timestamp: 1_700_000_000_000,
- },
- ],
- hasMore: false,
- });
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({ turns: [] });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed edit session");
- await app.submit("/edit");
- await vi.waitFor(() =>
- expect(app.editor.getExpandedText()).toBe("Original visible prompt"),
- );
- expect(app.editor.getExpandedText()).not.toContain("user-provided-context");
- app.editor.setText("Updated visible prompt");
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.editSessionMessage).toHaveBeenCalledTimes(1),
- );
- expect(runtime.editSessionMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- content: [
- "<user-provided-context>",
- "Referenced context",
- "</user-provided-context>",
- "",
- "Updated visible prompt",
- ].join("\n"),
- }),
- );
- await app.stop();
- });
- it("preserves hidden context and attachments when Edit recovery requires a new message", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const transportContent = [
- "<mavis-chat-context>",
- "Referenced context",
- "</mavis-chat-context>",
- "",
- "Original visible prompt",
- ].join("\n");
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-v1-recovery",
- assistantMessageId: "msg-assistant-v1-recovery",
- contentHead: "Original visible prompt",
- timestamp: 1_700_000_000_000,
- fileChangeCount: 0,
- },
- ]);
- vi.mocked(runtime.listMessagePage).mockResolvedValue({
- messages: [
- {
- id: "msg-user-v1-recovery",
- role: "user",
- content: transportContent,
- attachments: [
- {
- type: "file",
- fileName: "context.md",
- mimeType: "text/markdown",
- sizeBytes: 42,
- assetId: "asset-context-md",
- },
- ],
- timestamp: 1_700_000_000_000,
- },
- ],
- hasMore: false,
- });
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({ turns: [] });
- vi.mocked(runtime.editSessionMessage).mockRejectedValueOnce(
- Object.assign(new Error("history already rewound"), {
- key: "EDIT_RESTART_NEEDS_RESUBMIT",
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed edit session");
- const sendCountBeforeEdit = vi.mocked(runtime.sendMessage).mock.calls
- .length;
- await app.submit("/edit");
- await vi.waitFor(() =>
- expect(app.editor.getExpandedText()).toBe(
- "Original visible prompt [File #1] ",
- ),
- );
- app.editor.setText("/help should remain message text");
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.editSessionMessage).toHaveBeenCalledTimes(1),
- );
- await vi.waitFor(() =>
- expect(app.editor.getExpandedText()).toBe(
- "/help should remain message text [File #1] ",
- ),
- );
- expect(app.editor.getExpandedText()).not.toContain("mavis-chat-context");
- expect(runtime.sendMessage).toHaveBeenCalledTimes(sendCountBeforeEdit);
- let releaseAdmission!: () => void;
- vi.mocked(runtime.getAccountStatus).mockImplementationOnce(
- async () =>
- await new Promise((resolve) => {
- releaseAdmission = () =>
- resolve({
- status: "ready",
- defaultModel: "minimax/MiniMax-M2.7",
- providerId: "minimax",
- modelId: "MiniMax-M2.7",
- authMode: "managed-login",
- managedTokenPresent: true,
- warnings: [],
- });
- }),
- );
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toBeDefined(),
- );
- terminal.input?.("\u001b");
- releaseAdmission();
- await vi.waitFor(() =>
- expect(app.editor.getExpandedText()).toBe(
- "/help should remain message text [File #1] ",
- ),
- );
- expect(runtime.sendMessage).toHaveBeenCalledTimes(sendCountBeforeEdit);
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledTimes(
- sendCountBeforeEdit + 1,
- ),
- );
- expect(vi.mocked(runtime.sendMessage).mock.calls.at(-1)?.[0]).toMatchObject(
- {
- content: [
- "<mavis-chat-context>",
- "Referenced context",
- "</mavis-chat-context>",
- "",
- "/help should remain message text",
- ].join("\n"),
- attachments: [
- {
- meta: {
- attachmentType: "file",
- fileName: "context.md",
- mimeType: "text/markdown",
- sizeBytes: 42,
- },
- local: { assetId: "asset-context-md" },
- },
- ],
- },
- );
- expect(runtime.editSessionMessage).toHaveBeenCalledTimes(1);
- expect(
- app.transcript
- .snapshot()
- .filter((cell) => cell.kind === "user")
- .map((cell) => cell.content)
- .join("\n"),
- ).toContain("/help should remain message text");
- expect(
- app.transcript
- .snapshot()
- .filter((cell) => cell.kind === "user")
- .map((cell) => cell.content)
- .join("\n"),
- ).not.toContain("mavis-chat-context");
- await app.stop();
- });
- it("allows an attachment-only committed message to enter Edit and gain a body", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-v1-attachment-only",
- assistantMessageId: "msg-assistant-v1-attachment-only",
- timestamp: 1_700_000_000_000,
- fileChangeCount: 0,
- },
- ]);
- vi.mocked(runtime.listMessagePage).mockResolvedValue({
- messages: [
- {
- id: "msg-user-v1-attachment-only",
- role: "user",
- content: "",
- attachments: [
- {
- type: "image",
- fileName: "reference.png",
- mimeType: "image/png",
- filePath: "/workspace/reference.png",
- },
- ],
- timestamp: 1_700_000_000_000,
- },
- ],
- hasMore: false,
- });
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({ turns: [] });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed edit session");
- const sendCountBeforeEdit = vi.mocked(runtime.sendMessage).mock.calls
- .length;
- await app.submit("/edit");
- await vi.waitFor(() =>
- expect(app.tui.render(100).join("\n")).toContain(
- "Editing the selected message",
- ),
- );
- expect(app.editor.getExpandedText()).toBe("[Image #1] ");
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "chat",
- id: "chat",
- });
- app.editor.setText("Describe this reference");
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.editSessionMessage).toHaveBeenCalledTimes(1),
- );
- expect(runtime.editSessionMessage).toHaveBeenCalledWith({
- sessionId: "session-1",
- userMessageId: "msg-user-v1-attachment-only",
- clientRequestId: expect.stringMatching(/^tui-edit_/u),
- content: "Describe this reference",
- attachments: [
- {
- type: "image",
- fileName: "reference.png",
- mimeType: "image/png",
- filePath: "/workspace/reference.png",
- },
- ],
- rewindTurnDiff: true,
- });
- expect(runtime.sendMessage).toHaveBeenCalledTimes(sendCountBeforeEdit);
- await app.stop();
- });
- it("keeps a second composer submission while an edit request is still in flight", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-v1-target",
- assistantMessageId: "msg-assistant-v1-target",
- contentHead: "Original prompt",
- timestamp: 1,
- fileChangeCount: 0,
- },
- ]);
- vi.mocked(runtime.listMessagePage).mockResolvedValue({
- messages: [
- {
- id: "msg-user-v1-target",
- role: "user",
- content: "Original prompt",
- timestamp: 1,
- },
- ],
- hasMore: false,
- });
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({ turns: [] });
- let resolveEdit!: (value: {
- rewound: boolean;
- turnId: string;
- userMessageId: string;
- }) => void;
- vi.mocked(runtime.editSessionMessage).mockImplementation(
- () =>
- new Promise((resolve) => {
- resolveEdit = resolve;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed edit session");
- const sendCountBeforeEdit = vi.mocked(runtime.sendMessage).mock.calls
- .length;
- await app.submit("/edit");
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("Original prompt"),
- );
- app.editor.setText("First edited prompt");
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.editSessionMessage).toHaveBeenCalledTimes(1),
- );
- app.editor.setText("Keep this until edit settles");
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("Keep this until edit settles"),
- );
- expect(runtime.sendMessage).toHaveBeenCalledTimes(sendCountBeforeEdit);
- resolveEdit({
- rewound: true,
- turnId: "turn-edit-1",
- userMessageId: "msg-user-v1-edited",
- });
- await app.stop();
- });
- it("preserves the edit and retries the same operation when submission fails after rewind", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- {
- userMessageId: "msg-user-v1-target",
- assistantMessageId: "msg-assistant-v1-target",
- contentHead: "Original prompt",
- timestamp: 1,
- fileChangeCount: 0,
- },
- ]);
- vi.mocked(runtime.listMessagePage).mockResolvedValue({
- messages: [
- {
- id: "msg-user-v1-target",
- role: "user",
- content: "Original prompt",
- timestamp: 1,
- },
- ],
- hasMore: false,
- });
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({ turns: [] });
- vi.mocked(runtime.editSessionMessage)
- .mockRejectedValueOnce(
- Object.assign(new Error("submit failed"), {
- key: "EDIT_SUBMIT_FAILED_AFTER_REWIND",
- }),
- )
- .mockResolvedValueOnce({
- rewound: true,
- turnId: "turn-edit-retry",
- userMessageId: "msg-user-v1-edited",
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed edit session");
- await app.submit("/edit");
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("Original prompt"),
- );
- app.editor.setText("Preserve this edited prompt");
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.editSessionMessage).toHaveBeenCalledTimes(1),
- );
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("Preserve this edited prompt"),
- );
- expect(
- app.transcript
- .snapshot()
- .some((cell) => cell.content.includes("History was rewound")),
- ).toBe(true);
- const firstOperationId = vi.mocked(runtime.editSessionMessage).mock
- .calls[0]?.[0].clientRequestId;
- const sendCount = vi.mocked(runtime.sendMessage).mock.calls.length;
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.editSessionMessage).toHaveBeenCalledTimes(2),
- );
- expect(runtime.editSessionMessage).toHaveBeenLastCalledWith(
- expect.objectContaining({ clientRequestId: firstOperationId }),
- );
- expect(runtime.sendMessage).toHaveBeenCalledTimes(sendCount);
- await app.stop();
- });
- it("cancels /rewind preview and ignores duplicate scope submission", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- { userMessageId: "msg-user-rewind", timestamp: 1, fileChangeCount: 1 },
- ]);
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({
- turns: [
- {
- turnId: "turn-1",
- files: [{ filePath: "a.ts", action: "modified", skipped: false }],
- },
- ],
- });
- let resolveRewind!: (value: { rewound: boolean }) => void;
- vi.mocked(runtime.rewindSession).mockImplementation(
- () =>
- new Promise((resolve) => {
- resolveRewind = resolve;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed rewind session");
- await app.submit("/rewind");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:history",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-preview",
- }),
- );
- terminal.input?.("\u001b");
- expect(runtime.rewindSession).not.toHaveBeenCalled();
- await app.submit("/rewind");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:history",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-preview",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:scope",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-confirm",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.rewindSession).toHaveBeenCalledTimes(1),
- );
- terminal.input?.("\u001b");
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-confirm",
- });
- resolveRewind({ rewound: true });
- await app.stop();
- });
- it("invalidates /rewind when the active session switches before mutation", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- { userMessageId: "msg-user-rewind", timestamp: 1, fileChangeCount: 0 },
- ]);
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({ turns: [] });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed rewind session");
- await app.submit("/rewind");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:history",
- }),
- );
- const other = {
- sessionId: "session-other",
- title: "Other",
- workspaceDir: "/workspace",
- };
- vi.mocked(runtime.getSession).mockResolvedValue(other);
- await app.openSession(other.sessionId);
- terminal.input?.("\r");
- await new Promise((resolve) => setTimeout(resolve, 20));
- expect(runtime.getSessionRewindPreview).not.toHaveBeenCalled();
- expect(runtime.rewindSession).not.toHaveBeenCalled();
- expect(app.controller.snapshot().session?.sessionId).toBe(other.sessionId);
- await app.stop();
- });
- it("retries a failed file rewind with the same operation id", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- { userMessageId: "msg-user-rewind", timestamp: 1, fileChangeCount: 1 },
- ]);
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({
- turns: [
- {
- turnId: "turn-1",
- files: [{ filePath: "a.ts", action: "modified", skipped: false }],
- },
- ],
- });
- vi.mocked(runtime.rewindSession)
- .mockRejectedValueOnce(new Error("temporary conflict"))
- .mockResolvedValueOnce({
- rewound: true,
- turnDiffRewind: { status: "rewound", revertedTurnIds: ["turn-1"] },
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed rewind session");
- await app.submit("/rewind");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:history",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-preview",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:scope",
- }),
- );
- terminal.input?.("\u001b[B");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-confirm",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.rewindSession).toHaveBeenCalledTimes(1),
- );
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-confirm",
- });
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.rewindSession).toHaveBeenCalledTimes(2),
- );
- const ids = vi
- .mocked(runtime.rewindSession)
- .mock.calls.map(([input]) => input.clientRequestId);
- expect(ids[0]).toBe(ids[1]);
- await app.stop();
- });
- it.each([
- [
- "terminal busy failure",
- "CONVERSATION_MUTATION_BUSY",
- "Stop the running response and try again.",
- false,
- ],
- [
- "pending display publication",
- "REWIND_DISPLAY_COMMIT_FAILED",
- "retry with the same operation id",
- true,
- ],
- ] as const)(
- "keeps a failed conversation rewind open and retries %s with the correct operation id",
- async (_label, errorCode, expectedCopy, reuseOperationId) => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- { userMessageId: "msg-user-rewind", timestamp: 1, fileChangeCount: 0 },
- ]);
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({
- turns: [],
- });
- vi.mocked(runtime.rewindSession)
- .mockRejectedValueOnce(
- Object.assign(new Error("rewind failed"), { key: errorCode }),
- )
- .mockResolvedValueOnce({ rewound: true });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed rewind session");
- await app.submit("/rewind");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:history",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-preview",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:scope",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-confirm",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.rewindSession).toHaveBeenCalledTimes(1),
- );
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-confirm",
- });
- expect(renderTerminalViewport(app, terminal)).toContain(expectedCopy);
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.rewindSession).toHaveBeenCalledTimes(2),
- );
- const ids = vi
- .mocked(runtime.rewindSession)
- .mock.calls.map(([input]) => input.clientRequestId);
- if (reuseOperationId) expect(ids[0]).toBe(ids[1]);
- else expect(ids[0]).not.toBe(ids[1]);
- await app.stop();
- },
- );
- it.each([
- [
- "failed-after-rewind",
- {
- status: "failed-after-rewind",
- revertedTurnIds: ["turn-1"],
- errorCode: "conflict",
- },
- ],
- ["empty-reverted-ids", { status: "rewound", revertedTurnIds: [] }],
- [
- "unknown-status",
- { status: "future-runtime-status", revertedTurnIds: ["turn-1"] },
- ],
- ] as const)(
- "does not report %s as a successful file rewind",
- async (_label, turnDiffRewind) => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- { userMessageId: "msg-user-rewind", timestamp: 1, fileChangeCount: 1 },
- ]);
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({
- turns: [
- {
- turnId: "turn-1",
- files: [{ filePath: "a.ts", action: "modified", skipped: false }],
- },
- ],
- });
- vi.mocked(runtime.rewindSession).mockResolvedValue({
- rewound: true,
- turnDiffRewind,
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed rewind session");
- await app.submit("/rewind");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:history",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-preview",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:scope",
- }),
- );
- terminal.input?.("\u001b[B");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-confirm",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.rewindSession).toHaveBeenCalledTimes(1),
- );
- await vi.waitFor(() =>
- expect(app.transcript.snapshot().at(-1)?.content).toMatch(
- /did not complete|No file changes/,
- ),
- );
- expect(app.transcript.snapshot().at(-1)?.content).not.toContain(
- "Files rewound; conversation history was kept.",
- );
- await app.stop();
- },
- );
- it.each([
- [
- "MESSAGE_BOUNDARY_INVALID",
- "This conversation point is no longer available.",
- ],
- [
- "CONVERSATION_MUTATION_REQUEST_CONFLICT",
- "This action was already submitted or became stale.",
- ],
- ["CONVERSATION_MUTATION_BUSY", "Stop the running response and try again."],
- ] as const)(
- "maps rewind Runtime error %s to stable copy",
- async (code, expected) => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- { userMessageId: "msg-user-rewind", timestamp: 1, fileChangeCount: 1 },
- ]);
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({
- turns: [
- {
- turnId: "turn-1",
- files: [{ filePath: "a.ts", action: "modified", skipped: false }],
- },
- ],
- });
- vi.mocked(runtime.rewindSession).mockRejectedValue(
- Object.assign(new Error("raw prompt token=secret"), { key: code }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed rewind session");
- await app.submit("/rewind");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:history",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-preview",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:scope",
- }),
- );
- terminal.input?.("\u001b[B");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-confirm",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(
- app.transcript.snapshot().some((cell) => cell.content === expected),
- ),
- );
- expect(
- app.transcript
- .snapshot()
- .some((cell) => cell.content.includes("raw prompt token")),
- ).toBe(false);
- await app.stop();
- },
- );
- it("reports refresh failure without unhandled rejection after conversation-and-files rewind", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- { userMessageId: "msg-user-rewind", timestamp: 1, fileChangeCount: 1 },
- ]);
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({
- turns: [
- {
- turnId: "turn-1",
- files: [{ filePath: "a.ts", action: "modified", skipped: false }],
- },
- ],
- });
- vi.mocked(runtime.rewindSession).mockResolvedValue({
- rewound: true,
- turnDiffRewind: { status: "rewound", revertedTurnIds: ["turn-1"] },
- });
- vi.mocked(runtime.getMessages).mockRejectedValueOnce(
- new Error("refresh unavailable"),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed rewind session");
- await app.submit("/rewind");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:history",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-preview",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:scope",
- }),
- );
- terminal.input?.("\u001b[B");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-confirm",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(
- app.transcript
- .snapshot()
- .some((cell) => cell.content.includes("could not be refreshed")),
- ),
- );
- await app.stop();
- });
- it("does not apply a pending rewind refresh result after switching sessions", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionInputSummaries).mockResolvedValue([
- { userMessageId: "msg-user-rewind", timestamp: 1, fileChangeCount: 1 },
- ]);
- vi.mocked(runtime.getSessionRewindPreview).mockResolvedValue({
- turns: [
- {
- turnId: "turn-1",
- files: [{ filePath: "a.ts", action: "modified", skipped: false }],
- },
- ],
- });
- vi.mocked(runtime.rewindSession).mockResolvedValue({
- rewound: true,
- turnDiffRewind: { status: "rewound", revertedTurnIds: ["turn-1"] },
- });
- let resolveRefresh!: (messages: readonly TuiMessage[]) => void;
- const refreshPromise = new Promise<readonly TuiMessage[]>((resolve) => {
- resolveRefresh = resolve;
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Seed rewind session");
- vi.mocked(runtime.getMessages).mockImplementationOnce(() => refreshPromise);
- await app.submit("/rewind");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:history",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-preview",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:scope",
- }),
- );
- terminal.input?.("\u001b[B");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "session-mutation:rewind-confirm",
- }),
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.rewindSession).toHaveBeenCalledTimes(1),
- );
- const other = {
- sessionId: "session-other",
- title: "Other",
- workspaceDir: "/workspace",
- };
- vi.mocked(runtime.getSession).mockResolvedValue(other);
- vi.mocked(runtime.getMessages).mockResolvedValue([]);
- const open = app.openSession(other.sessionId);
- resolveRefresh([
- {
- id: "stale-message",
- role: "assistant",
- content: "stale",
- timestamp: 3,
- },
- ]);
- await open;
- expect(app.controller.snapshot().session?.sessionId).toBe(other.sessionId);
- expect(
- app.transcript
- .snapshot()
- .some((cell) => cell.content.includes("Files rewound")),
- ).toBe(false);
- await app.stop();
- });
- it("redirects a hidden task child to its parent before rendering", async () => {
- const runtime = createRuntime();
- const parent = {
- sessionId: "session-parent",
- title: "CLI integration",
- workspaceDir: "/workspace",
- };
- const child = {
- sessionId: "session-child",
- parentSessionId: parent.sessionId,
- title: "Review recent CLI changes",
- workspaceDir: "/workspace",
- sessionType: "branch",
- sessionKind: "task",
- visibility: "hidden",
- purpose: "local-task:turn-1:tool-1",
- agentName: "verifier",
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([parent]);
- vi.mocked(runtime.getSession).mockImplementation(async (sessionId) =>
- sessionId === child.sessionId ? child : parent,
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.openSession(child.sessionId);
- expect(runtime.getSession).toHaveBeenLastCalledWith(parent.sessionId);
- expect(app.controller.snapshot().session?.sessionId).toBe(parent.sessionId);
- expect(app.tui.render(140).join("\n")).not.toContain("Sub: verifier");
- expect(app.transcript.snapshot().at(-1)?.content).toBe(
- "Sub-agent Sessions are internal. Opened the parent Session instead.",
- );
- await app.stop();
- });
- it("shows compact Agent Team status without exposing child details or commands", async () => {
- const terminal = new FakeTerminal();
- terminal.rows = 24;
- const runtime = createRuntime();
- const parent = {
- sessionId: "session-parent",
- title: "Agent Team parent",
- workspaceDir: "/workspace",
- };
- const child = {
- sessionId: "session-child",
- parentSessionId: parent.sessionId,
- title: "Verify the TUI",
- workspaceDir: "/workspace",
- sessionType: "branch" as const,
- sessionKind: "task",
- visibility: "hidden" as const,
- purpose: "local-task:turn-1:tool-1",
- agentName: "verifier",
- status: "finished",
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([parent]);
- vi.mocked(runtime.getSession).mockImplementation(async (sessionId) =>
- sessionId === child.sessionId ? child : parent,
- );
- vi.mocked(runtime.getDelegationSnapshot).mockResolvedValue({
- schemaVersion: 1,
- rootSessionId: parent.sessionId,
- members: [
- {
- sessionId: child.sessionId,
- parentSessionId: parent.sessionId,
- agentName: child.agentName,
- task: child.title,
- status: "running",
- createdAtMs: 100,
- updatedAtMs: 200,
- },
- ],
- });
- vi.mocked(runtime.getMessages).mockImplementation(async (sessionId) =>
- sessionId === child.sessionId
- ? [
- {
- id: "child-answer",
- turnId: "turn-child",
- role: "assistant",
- content: "Child verification result",
- timestamp: 200,
- },
- ]
- : [],
- );
- vi.mocked(runtime.getActiveRun).mockImplementation(async (sessionId) => ({
- schemaVersion: 1,
- sessionId,
- state: sessionId === child.sessionId ? "running" : "idle",
- ...(sessionId === child.sessionId ? { turnId: "turn-child" } : {}),
- actions: { steer: false },
- }));
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.openSession(parent.sessionId);
- app.start();
- const summary = app.tui.render(100).join("\n");
- expect(summary).toContain("Tasks · 1 agent active · /tasks details");
- expect(summary).not.toContain("verifier");
- expect(summary).not.toContain("/agents");
- expect(
- app.transcript.snapshot().some((cell) => cell.kind === "agent-team"),
- ).toBe(false);
- await app.submit("/agents");
- await app.submit("/team");
- expect(app.controller.snapshot().session?.sessionId).toBe(parent.sessionId);
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(false);
- expect(app.tui.render(100).join("\n")).not.toContain(
- "Child verification result",
- );
- expect(
- vi
- .mocked(runtime.sendMessage)
- .mock.calls.map(([request]) => request.content),
- ).toEqual(["/agents", "/team"]);
- await app.stop();
- });
- it("allocates enough standard-terminal space for ten session choices", async () => {
- const terminal = new FakeTerminal();
- terminal.rows = 24;
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionPage).mockResolvedValue({
- sessions: Array.from({ length: 12 }, (_, index) => ({
- sessionId: `session-${String(index + 1).padStart(2, "0")}`,
- title: `Session ${String(index + 1).padStart(2, "0")}`,
- workspaceDir: "/workspace",
- updatedAt: 12 - index,
- })),
- hasMore: false,
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.submit("/sessions");
- const lines = app.interaction.current()?.render(80) ?? [];
- const rendered = lines.join("\n");
- expect(lines.length).toBeLessThanOrEqual(terminal.rows);
- expect(rendered).toContain("Session 10");
- expect(rendered).not.toContain("Session 11");
- });
- it("switches, restores, and resumes archived Desktop sessions from the session manager", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let archived = true;
- const activeSession = {
- sessionId: "session-active",
- title: "Active session",
- workspaceDir: "/workspace",
- updatedAt: 200,
- };
- const archivedSession = {
- sessionId: "session-archived",
- title: "Archived session",
- workspaceDir: "/workspace",
- updatedAt: 100,
- archived: true,
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([activeSession]);
- vi.mocked(runtime.listSessionPage).mockResolvedValue({
- sessions: [activeSession, archivedSession],
- hasMore: false,
- });
- vi.mocked(runtime.getSession).mockImplementation(async (sessionId) => ({
- ...(sessionId === archivedSession.sessionId
- ? archivedSession
- : activeSession),
- archived: sessionId === archivedSession.sessionId ? archived : false,
- }));
- vi.mocked(runtime.archiveSession).mockImplementation(
- async (_sessionId, value) => {
- archived = value;
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/sessions");
- expect(runtime.listSessionPage).toHaveBeenCalledWith({
- allAgents: true,
- workspaceDir: "/workspace",
- limit: 50,
- includeArchived: true,
- includeHidden: true,
- });
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- terminal.input?.("\t");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.archiveSession).toHaveBeenCalledWith(
- "session-archived",
- false,
- ),
- );
- await vi.waitFor(() =>
- expect(runtime.getMessages).toHaveBeenCalledWith("session-archived"),
- );
- expect(app.getSurface()).toBe("conversation");
- expect(app.controller.snapshot().session?.sessionId).toBe(
- "session-archived",
- );
- expect(app.tui.hasOverlay()).toBe(false);
- await app.stop();
- });
- it("uses the resume alias to search every Desktop session page", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionPage)
- .mockResolvedValueOnce({
- sessions: [
- {
- sessionId: "session-page-1",
- title: "First page",
- workspaceDir: "/workspace",
- updatedAt: 200,
- },
- ],
- hasMore: true,
- nextCursor: "session-page-1",
- })
- .mockResolvedValueOnce({
- sessions: [
- {
- sessionId: "session-page-2",
- title: "Second page",
- workspaceDir: "/workspace",
- updatedAt: 100,
- },
- ],
- hasMore: false,
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/resume Second page");
- await vi.waitFor(() =>
- expect(runtime.listSessionPage).toHaveBeenNthCalledWith(2, {
- allAgents: true,
- workspaceDir: "/workspace",
- limit: 50,
- cursor: "session-page-1",
- includeArchived: true,
- includeHidden: true,
- }),
- );
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- expect(app.interaction.current()?.render(100).join("\n")).toContain(
- "Second page",
- );
- await app.stop();
- });
- it("queries the current workspace first and reloads the global catalog on Ctrl+A", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessionPage).mockImplementation(
- async (input = {}) => {
- if (input.cursor === "all-page-2") {
- return {
- sessions: [
- {
- sessionId: "session-other-older",
- title: "Older session elsewhere",
- workspaceDir: "/other-workspace",
- updatedAt: 50,
- },
- ],
- hasMore: false,
- };
- }
- return input.workspaceDir
- ? {
- sessions: [
- {
- sessionId: "session-workspace-old",
- title: "Older workspace session",
- workspaceDir: "/workspace",
- updatedAt: 100,
- },
- ],
- hasMore: false,
- }
- : {
- sessions: [
- {
- sessionId: "session-other-new",
- title: "Newer session elsewhere",
- workspaceDir: "/other-workspace",
- updatedAt: 200,
- },
- {
- sessionId: "session-workspace-old",
- title: "Older workspace session",
- workspaceDir: "/workspace",
- updatedAt: 100,
- },
- ],
- hasMore: true,
- nextCursor: "all-page-2",
- };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/sessions");
- expect(runtime.listSessionPage).toHaveBeenNthCalledWith(1, {
- allAgents: true,
- workspaceDir: "/workspace",
- limit: 50,
- includeArchived: true,
- includeHidden: true,
- });
- expect(app.interaction.current()?.render(100).join("\n")).toContain(
- "Older workspace session",
- );
- terminal.input?.("\x01");
- await vi.waitFor(() =>
- expect(runtime.listSessionPage).toHaveBeenNthCalledWith(2, {
- allAgents: true,
- limit: 50,
- includeArchived: true,
- includeHidden: true,
- }),
- );
- await vi.waitFor(() =>
- expect(app.interaction.current()?.render(100).join("\n")).toContain(
- "Newer session elsewhere",
- ),
- );
- expect(app.interaction.current()?.render(100).join("\n")).toContain(
- "All sessions",
- );
- terminal.input?.("\x0c");
- await vi.waitFor(() =>
- expect(runtime.listSessionPage).toHaveBeenNthCalledWith(3, {
- allAgents: true,
- limit: 50,
- cursor: "all-page-2",
- includeArchived: true,
- includeHidden: true,
- }),
- );
- await vi.waitFor(() =>
- expect(app.interaction.current()?.render(100).join("\n")).toContain(
- "Older session elsewhere",
- ),
- );
- await app.stop();
- });
- it("shows account status and resolves pending permission and questionnaire decisions", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-existing",
- title: "Existing session",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.listPendingPermissions).mockResolvedValue([
- {
- requestId: "permission-1",
- sessionId: "session-existing",
- agentName: "mavis",
- toolName: "bash",
- reason: "Run tests",
- allowAlwaysSupported: true,
- },
- ]);
- vi.mocked(runtime.getPendingQuestionnaire).mockResolvedValue({
- id: "question-1",
- title: "Choose",
- steps: [
- {
- id: "step-1",
- question: "Proceed?",
- selectionMode: "single",
- options: [
- { id: "yes", label: "Yes" },
- { id: "no", label: "No" },
- ],
- },
- ],
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.submit("/resume session-existing");
- expect(app.transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({ kind: "permission", status: "blocked" }),
- expect.objectContaining({ kind: "question", status: "blocked" }),
- ]),
- );
- expect(app.transcript.get("permission:permission-1")).toMatchObject({
- title: "bash",
- content: "Run tests",
- });
- expect(
- app.transcript.get("permission:permission-1")?.content,
- ).not.toContain("decision panel");
- expect(app.transcript.get("permission:permission-1")?.content).not.toMatch(
- /\/(?:allow|always|deny)\b/u,
- );
- expect(app.transcript.get("question:question-1")?.content).toBe("");
- await app.submit("/q1 1");
- expect(runtime.replyQuestionnaire).not.toHaveBeenCalled();
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- expect(terminal.mouseTracking).toBe(false);
- await app.submit("/allow");
- await app.submit("/q1 1");
- await app.submit("/status");
- expect(runtime.replyPermission).toHaveBeenCalledWith(
- "mavis",
- "permission-1",
- "allowOnce",
- );
- expect(runtime.replyQuestionnaire).toHaveBeenCalledWith(
- "mavis",
- "question-1",
- [
- {
- stepId: "step-1",
- selectedOptionIds: ["yes"],
- selectedOther: false,
- },
- ],
- );
- expect(terminal.mouseTracking).toBe(false);
- expect(app.transcript.get("question:question-1")).toMatchObject({
- status: "resolved",
- title: "Answers sent",
- content: "Proceed? Yes",
- });
- expect(app.interaction.current()).toBeDefined();
- expect(app.tui.render(80).join("\n")).toContain("MCode status");
- expect(app.transcript.snapshot()).not.toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- kind: "inspection",
- inspection: expect.objectContaining({
- visualization: { kind: "status" },
- }),
- }),
- ]),
- );
- });
- it("does not let a stale permission response hide a newer decision surface", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-race",
- title: "Decision race",
- workspaceDir: "/workspace",
- },
- ]);
- const firstPermission = {
- requestId: "permission-first",
- sessionId: "session-race",
- agentName: "mavis",
- toolName: "bash",
- reason: "First request",
- allowAlwaysSupported: true,
- };
- const secondPermission = {
- requestId: "permission-second",
- sessionId: "session-race",
- agentName: "mavis",
- toolName: "write_file",
- reason: "Newer request",
- allowAlwaysSupported: false,
- };
- vi.mocked(runtime.listPendingPermissions).mockResolvedValue([
- firstPermission,
- ]);
- let finishReply: ((value: boolean) => void) | undefined;
- vi.mocked(runtime.replyPermission).mockImplementation(
- async () =>
- await new Promise<boolean>((resolve) => {
- finishReply = resolve;
- }),
- );
- let emitEvent: ((event: TuiRuntimeEvent) => void) | undefined;
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- const event = await new Promise<TuiRuntimeEvent | undefined>(
- (resolve) => {
- emitEvent = resolve;
- signal.addEventListener("abort", () => resolve(undefined), {
- once: true,
- });
- },
- );
- if (event) yield event;
- await new Promise<void>((resolve) => {
- signal.addEventListener("abort", resolve, { once: true });
- });
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-race");
- const resolving = app.submit("/allow");
- await vi.waitFor(() => expect(runtime.replyPermission).toHaveBeenCalled());
- await vi.waitFor(() => expect(emitEvent).toBeTypeOf("function"));
- emitEvent?.(
- runtimeEvent({
- type: "permission.ask",
- timestamp: Date.now(),
- source: "test",
- payload: secondPermission,
- }),
- );
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).toContain("Newer request"),
- );
- finishReply?.(true);
- await resolving;
- const rendered = app.tui.render(80).join("\n");
- expect(rendered).toContain("Write file");
- expect(rendered).toContain("Newer request");
- expect(runtime.replyPermission).toHaveBeenCalledTimes(1);
- await app.stop();
- });
- it("closes a permission that another surface resolved before the reply arrived", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const permission = {
- requestId: "permission-stale",
- sessionId: "session-permission-stale",
- agentName: "mavis",
- toolName: "bash",
- reason: "Run tests",
- allowAlwaysSupported: true,
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-permission-stale",
- title: "Permission race",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.listPendingPermissions)
- .mockResolvedValueOnce([permission])
- .mockResolvedValue([]);
- vi.mocked(runtime.replyPermission).mockResolvedValue(false);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.submit("/resume session-permission-stale");
- await app.submit("/allow");
- expect(app.transcript.get("permission:permission-stale")).toMatchObject({
- status: "resolved",
- detail: "Resolved on another surface.",
- });
- expect(app.interaction.isActive()).toBe(false);
- });
- it("keeps an option-only questionnaire pending after an invalid slash answer", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-questionnaire",
- title: "Questionnaire session",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.getPendingQuestionnaire).mockResolvedValue({
- id: "question-options-only",
- title: "Choose",
- steps: [
- {
- id: "channel",
- question: "Choose a release channel",
- selectionMode: "single",
- options: [
- { id: "preview", label: "Preview" },
- { id: "stable", label: "Stable" },
- ],
- allowOther: false,
- },
- ],
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.submit("/resume session-questionnaire");
- await app.submit("/q1 nightly");
- expect(runtime.replyQuestionnaire).not.toHaveBeenCalled();
- expect(app.transcript.get("question:question-options-only")).toMatchObject({
- status: "blocked",
- });
- expect(app.transcript.snapshot().at(-1)).toMatchObject({
- kind: "warning",
- content:
- "/q1 accepts only listed options. Use an option number, ID, or label.",
- });
- await app.submit("/q1 1");
- expect(runtime.replyQuestionnaire).toHaveBeenCalledWith(
- "mavis",
- "question-options-only",
- [
- {
- stepId: "channel",
- selectedOptionIds: ["preview"],
- selectedOther: false,
- },
- ],
- );
- });
- it("shows the first AskUser answer immediately while later questions remain pending", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-multi-question",
- title: "Multi question",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.getPendingQuestionnaire).mockResolvedValue({
- id: "question-multi-step",
- title: "Preferences",
- steps: [
- {
- id: "animal",
- question: "Favorite animal?",
- selectionMode: "single",
- options: [
- { id: "cat", label: "Cat" },
- { id: "dog", label: "Dog" },
- ],
- },
- {
- id: "color",
- question: "Favorite color?",
- selectionMode: "single",
- options: [
- { id: "blue", label: "Blue" },
- { id: "green", label: "Green" },
- ],
- },
- ],
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- app.start();
- await app.submit("/resume session-multi-question");
- const picker = app.interaction.current();
- expect(picker).toBeDefined();
- terminal.input?.("1");
- expect(runtime.replyQuestionnaire).not.toHaveBeenCalled();
- expect(app.interaction.current()).toBe(picker);
- expect(app.transcript.get("question:question-multi-step")).toMatchObject({
- status: "blocked",
- content: "Favorite animal? Cat",
- detail: "1 of 2 answered",
- });
- const rendered = app.tui.render(80).join("\n");
- expect(rendered).toContain("Answers so far");
- expect(rendered).toContain("Favorite animal? Cat");
- expect(rendered).toContain("Favorite color?");
- terminal.input?.("\u001B[B");
- expect(app.tui.render(80).join("\n")).toContain("› 2 Green");
- terminal.input?.("\r");
- await vi.waitFor(() => {
- expect(runtime.replyQuestionnaire).toHaveBeenCalledWith(
- "mavis",
- "question-multi-step",
- [
- {
- stepId: "animal",
- selectedOptionIds: ["cat"],
- selectedOther: false,
- },
- {
- stepId: "color",
- selectedOptionIds: ["green"],
- selectedOther: false,
- },
- ],
- );
- });
- await app.stop();
- });
- it("sends the selected answer immediately and refreshes the Agent continuation", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-review",
- title: "Questionnaire review",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.getPendingQuestionnaire).mockResolvedValue({
- id: "question-review",
- title: "Release setup",
- steps: [
- {
- id: "channel",
- question: "Choose a channel",
- selectionMode: "single",
- options: [
- { id: "preview", label: "Preview" },
- { id: "stable", label: "Stable" },
- ],
- },
- ],
- });
- const previousMessages = [
- {
- id: "previous-question-answer",
- role: "user" as const,
- content: [
- "<questionnaire-response>",
- " <requestId>question-previous</requestId>",
- " <answers />",
- "</questionnaire-response>",
- "",
- "Q: Choose a channel",
- "A: Preview",
- ].join("\n"),
- },
- ];
- vi.mocked(runtime.getMessages).mockResolvedValue(previousMessages);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- let releaseReply: ((result: boolean) => void) | undefined;
- let releaseContinuation: (() => void) | undefined;
- vi.mocked(runtime.replyQuestionnaire).mockImplementation(
- async () =>
- await new Promise<boolean>((resolve) => {
- releaseReply = resolve;
- }),
- );
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- await new Promise<void>((resolve) => {
- releaseContinuation = resolve;
- signal.addEventListener("abort", resolve, { once: true });
- });
- if (signal.aborted) return;
- yield runtimeEvent({
- type: "session.start",
- timestamp: Date.now(),
- source: "runtime",
- payload: {
- sessionId: "session-review",
- turnId: "turn-questionnaire-continuation",
- },
- });
- yield runtimeEvent({
- type: "session.finish",
- timestamp: Date.now(),
- source: "runtime",
- payload: {
- sessionId: "session-review",
- turnId: "turn-questionnaire-continuation",
- },
- });
- },
- );
- app.start();
- await app.ready;
- await app.submit("/resume session-review");
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- terminal.input?.("2");
- await vi.waitFor(() =>
- expect(runtime.replyQuestionnaire).toHaveBeenCalledWith(
- "mavis",
- "question-review",
- [
- {
- stepId: "channel",
- selectedOptionIds: ["stable"],
- selectedOther: false,
- },
- ],
- ),
- );
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.transcript.get("question:question-review")).toMatchObject({
- status: "pending",
- detail: "Sending your answer…",
- });
- releaseReply?.(true);
- await vi.waitFor(() =>
- expect(app.transcript.get("question:question-review")).toMatchObject({
- status: "resolved",
- detail: "Answer sent · MCode is continuing…",
- }),
- );
- vi.mocked(runtime.getMessages).mockResolvedValue([
- ...previousMessages,
- {
- id: "assistant-after-question",
- role: "assistant",
- content: "Agent continued after your answer.",
- },
- ]);
- releaseContinuation?.();
- await vi.waitFor(() =>
- expect(
- app.transcript
- .snapshot()
- .map((cell) => cell.content)
- .join("\n"),
- ).toContain("Agent continued after your answer."),
- );
- expect(app.transcript.get("question:question-review")).toMatchObject({
- kind: "question",
- status: "resolved",
- title: "Answers sent",
- content: "Choose a channel Stable",
- detail: undefined,
- });
- expect(
- app.transcript
- .snapshot()
- .filter((cell) => cell.kind === "question")
- .map((cell) => cell.content),
- ).toEqual(["Choose a channel Preview", "Choose a channel Stable"]);
- expect(app.tui.render(80).join("\n")).not.toContain("Thinking · Message");
- expect(app.tui.render(80).join("\n")).not.toContain("Esc to interrupt");
- await app.stop();
- });
- it("redraws after a delayed questionnaire reply resolves", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-delayed-reply",
- title: "Delayed questionnaire reply",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.getPendingQuestionnaire).mockResolvedValue({
- id: "question-delayed-reply",
- steps: [
- {
- id: "channel",
- question: "Choose a channel",
- selectionMode: "single",
- options: [{ id: "preview", label: "Preview" }],
- },
- ],
- });
- let finishReply: ((result: boolean) => void) | undefined;
- vi.mocked(runtime.replyQuestionnaire).mockImplementation(
- async () =>
- await new Promise<boolean>((resolve) => {
- finishReply = resolve;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-delayed-reply");
- terminal.input?.("1");
- await vi.waitFor(() =>
- expect(runtime.replyQuestionnaire).toHaveBeenCalledTimes(1),
- );
- expect(app.tui.hasOverlay()).toBe(false);
- const writesBeforeResolution = terminal.writes.length;
- finishReply?.(true);
- await vi.waitFor(() =>
- expect(
- app.transcript.get("question:question-delayed-reply"),
- ).toMatchObject({
- status: "resolved",
- detail: "Answer sent · MCode is continuing…",
- }),
- );
- await vi.waitFor(() =>
- expect(terminal.writes.length).toBeGreaterThan(writesBeforeResolution),
- );
- await app.stop();
- });
- it("keeps the questionnaire interactive when the Runtime cannot start its continuation", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-retry-questionnaire",
- title: "Retry questionnaire",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.getPendingQuestionnaire).mockResolvedValue({
- id: "question-retry",
- title: "Choose a channel",
- steps: [
- {
- id: "channel",
- question: "Choose a channel",
- selectionMode: "single",
- options: [{ id: "preview", label: "Preview" }],
- },
- ],
- });
- vi.mocked(runtime.replyQuestionnaire).mockRejectedValue(
- new Error(
- "Questionnaire answer was saved, but the continuation could not start.",
- ),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- app.start();
- await app.ready;
- await app.submit("/resume session-retry-questionnaire");
- terminal.input?.("1");
- await vi.waitFor(() =>
- expect(app.transcript.get("question:question-retry")).toMatchObject({
- status: "blocked",
- title: "Choose a channel",
- detail: "Answer could not be sent · retry",
- }),
- );
- expect(app.interaction.isActive()).toBe(true);
- expect(app.transcript.snapshot().at(-1)).toMatchObject({
- kind: "error",
- content:
- "Couldn't send your answers: Questionnaire answer was saved, but the continuation could not start. Retry. The question remains open.",
- });
- await app.stop();
- });
- it("redraws after a delayed questionnaire dismissal resolves", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-delayed-dismiss",
- title: "Delayed questionnaire dismissal",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.getPendingQuestionnaire).mockResolvedValue({
- id: "question-delayed-dismiss",
- steps: [{ id: "confirmation", question: "Continue?" }],
- });
- let finishDismiss: ((result: boolean) => void) | undefined;
- vi.mocked(runtime.dismissQuestionnaire).mockImplementation(
- async () =>
- await new Promise<boolean>((resolve) => {
- finishDismiss = resolve;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-delayed-dismiss");
- const dismissing = app.submit("/ask-cancel");
- await vi.waitFor(() =>
- expect(runtime.dismissQuestionnaire).toHaveBeenCalledTimes(1),
- );
- const writesBeforeResolution = terminal.writes.length;
- finishDismiss?.(true);
- await dismissing;
- expect(
- app.transcript.get("question:question-delayed-dismiss"),
- ).toMatchObject({
- status: "resolved",
- title: "Question dismissed",
- content: "",
- detail: undefined,
- });
- await vi.waitFor(() =>
- expect(terminal.writes.length).toBeGreaterThan(writesBeforeResolution),
- );
- await app.stop();
- });
- it.each([
- { keyName: "Escape", input: "\x1b" },
- { keyName: "Ctrl-C", input: "\x03" },
- ])(
- "keeps AskUser focused while $keyName dismisses the pending questionnaire",
- async ({ input }) => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-escape-dismiss",
- title: "Escape questionnaire dismissal",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.getPendingQuestionnaire).mockResolvedValue({
- id: "question-escape-dismiss",
- steps: [
- {
- id: "channel",
- question: "Choose a channel",
- selectionMode: "single",
- options: [{ id: "preview", label: "Preview" }],
- },
- ],
- });
- let finishDismiss: ((result: boolean) => void) | undefined;
- vi.mocked(runtime.dismissQuestionnaire).mockImplementation(
- async () =>
- await new Promise<boolean>((resolve) => {
- finishDismiss = resolve;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-escape-dismiss");
- terminal.input?.(input);
- if (input === "\x1b") {
- expect(app.interaction.current()?.render(80).join("\n")).toContain(
- "Cancel this question?",
- );
- terminal.input?.("\r");
- }
- await vi.waitFor(() =>
- expect(runtime.dismissQuestionnaire).toHaveBeenCalledWith(
- "mavis",
- "question-escape-dismiss",
- ),
- );
- expect(app.interaction.isActive()).toBe(true);
- expect(app.interaction.current()?.render(80).join("\n")).toContain(
- "Closing question",
- );
- expect(
- app.state.snapshot().sessions.get("session-escape-dismiss")?.attention
- .question,
- ).toBe(true);
- finishDismiss?.(true);
- await vi.waitFor(() => expect(app.interaction.isActive()).toBe(false));
- expect(
- app.transcript.get("question:question-escape-dismiss"),
- ).toMatchObject({
- status: "resolved",
- title: "Question dismissed",
- });
- expect(
- app.state.snapshot().sessions.get("session-escape-dismiss")?.attention
- .question,
- ).toBe(false);
- expect(app.tui.render(80).join("\n")).not.toContain("Reply required");
- await app.stop();
- },
- );
- it("denies a pending permission explicitly and releases the blocked interaction", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-pending",
- title: "Pending permission",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.listPendingPermissions).mockResolvedValue([
- {
- requestId: "permission-pending",
- sessionId: "session-pending",
- agentName: "mavis",
- toolName: "bash",
- toolInput: "pnpm test",
- ruleContents: ["Bash(pnpm test:*)"],
- allowAlwaysSupported: true,
- },
- ]);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-pending");
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- terminal.input?.("3");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.replyPermission).toHaveBeenCalledWith(
- "mavis",
- "permission-pending",
- "deny",
- ),
- );
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(false);
- expect(app.transcript.get("permission:permission-pending")).toMatchObject({
- status: "resolved",
- detail: "Denied · bash",
- });
- expect(app.tui.render(80).join("\n")).not.toContain("Approval required");
- await app.stop();
- });
- it("resolves a regular inline permission without clearing the terminal viewport", async () => {
- const terminal = new FakeTerminal();
- terminal.rows = 8;
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-permission-render",
- title: "Permission render",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.listPendingPermissions).mockResolvedValue([
- {
- requestId: "permission-render",
- sessionId: "session-permission-render",
- agentName: "mavis",
- toolName: "bash",
- toolInput: "pnpm test",
- reason: "Run focused tests",
- allowAlwaysSupported: true,
- },
- ]);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.2.6",
- workspaceDir: "/workspace",
- tuiMode: "regular",
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-permission-render");
- await vi.waitFor(() => expect(app.interaction.isActive()).toBe(true));
- app.tui.renderNow();
- terminal.writes.length = 0;
- terminal.input?.("\r");
- await vi.waitFor(() => expect(app.interaction.isActive()).toBe(false));
- await vi.waitFor(() => expect(terminal.writes.length).toBeGreaterThan(0));
- expect(runtime.replyPermission).toHaveBeenCalledWith(
- "mavis",
- "permission-render",
- "allowOnce",
- );
- expect(app.transcript.get("permission:permission-render")).toMatchObject({
- status: "resolved",
- detail: "Allowed for this conversation · bash",
- });
- expect(terminal.writes.join("")).not.toContain("\x1b[2J");
- expect(terminal.writes.join("")).not.toContain("\x1b[3J");
- await app.stop();
- });
- it("keeps a submission retryable when a pending decision blocks admission", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-draft-blocked",
- title: "Pending decision",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.listPendingPermissions).mockResolvedValue([
- {
- requestId: "permission-draft-blocked",
- sessionId: "session-draft-blocked",
- agentName: "mavis",
- toolName: "bash",
- allowAlwaysSupported: true,
- },
- ]);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-draft-blocked");
- app.editor.setText("Keep this draft for later");
- app.editor.handleInput("\r");
- await vi.waitFor(() => expect(app.editor.getText()).toBe(""));
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- expect(app.tui.render(80).join("\n")).toContain("Approval required");
- expect(app.tui.render(80).join("\n")).not.toContain("Message · Enter");
- await app.stop();
- });
- it("keeps a pending questionnaire when Runtime rejects dismissal", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-existing",
- title: "Existing session",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.getPendingQuestionnaire).mockResolvedValue({
- id: "question-rejected",
- steps: [{ id: "step-1", question: "Proceed?" }],
- });
- vi.mocked(runtime.dismissQuestionnaire).mockResolvedValue(false);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.submit("/resume session-existing");
- await app.submit("/ask-cancel");
- expect(app.transcript.get("question:question-rejected")).toMatchObject({
- status: "blocked",
- });
- expect(app.transcript.snapshot().at(-1)).toMatchObject({
- kind: "error",
- content: "Couldn't dismiss this question. It remains open; retry.",
- });
- });
- it("restores the questionnaire panel when an Escape dismissal request fails", async () => {
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-dismiss-retry",
- title: "Dismiss retry",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.getPendingQuestionnaire).mockResolvedValue({
- id: "question-dismiss-retry",
- steps: [
- {
- id: "step-1",
- question: "Proceed?",
- selectionMode: "single",
- options: [{ id: "yes", label: "Yes" }],
- },
- ],
- });
- vi.mocked(runtime.dismissQuestionnaire).mockRejectedValue(
- new Error("connection lost"),
- );
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.submit("/resume session-dismiss-retry");
- app.interaction.current()?.handleInput("\u001b");
- app.interaction.current()?.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.dismissQuestionnaire).toHaveBeenCalledOnce(),
- );
- await vi.waitFor(() => {
- const panel = app.interaction.current();
- expect(panel).toBeDefined();
- expect(panel?.render(80).join("\n")).toContain("Ask");
- });
- expect(app.transcript.get("question:question-dismiss-retry")).toMatchObject(
- {
- status: "blocked",
- },
- );
- });
- it("settles a questionnaire dismissal that already reached a terminal Runtime state", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-terminal-dismiss",
- title: "Terminal dismissal",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.getPendingQuestionnaire).mockResolvedValue({
- id: "question-terminal-dismiss",
- steps: [{ id: "step-1", question: "Proceed?" }],
- });
- vi.mocked(runtime.dismissQuestionnaire).mockRejectedValue(
- Object.assign(new Error("already superseded"), {
- status: 410,
- code: "QUESTIONNAIRE_NOT_PENDING",
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.submit("/resume session-terminal-dismiss");
- await app.submit("/ask-cancel");
- expect(
- app.transcript.get("question:question-terminal-dismiss"),
- ).toMatchObject({
- status: "resolved",
- title: "Question closed",
- detail: "Question is no longer pending.",
- });
- expect(app.interaction.isActive()).toBe(false);
- });
- it("projects live Desktop permission events into the transcript", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents() {
- yield runtimeEvent({
- type: "permission.ask",
- timestamp: 100,
- source: "runtime",
- payload: {
- requestId: "permission-live",
- sessionId: "session-1",
- agentName: "mavis",
- toolName: "bash",
- toolDescription: "Run a shell command",
- reason: "Run checks",
- ruleContents: ["Bash(pnpm test:*)"],
- allowAlwaysSupported: true,
- },
- });
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.submit("Start the current session");
- await app.submit("/transcript");
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "transcript",
- });
- app.start();
- await vi.waitFor(() =>
- expect(app.transcript.get("permission:permission-live")).toMatchObject({
- kind: "permission",
- status: "blocked",
- content: expect.stringContaining("Run checks"),
- }),
- );
- expect(app.transcript.get("permission:permission-live")).toMatchObject({
- title: "bash",
- content: "Run checks",
- });
- expect(
- app.transcript.get("permission:permission-live")?.content,
- ).not.toContain("Rules:");
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "chat",
- id: "chat",
- });
- await app.submit("/allow");
- expect(app.surfaceHost.getActiveSurface()).toEqual({
- kind: "feature",
- id: "transcript",
- });
- await app.stop();
- });
- it("ignores Desktop interaction events until their session is active", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let resolveEventsDelivered: (() => void) | undefined;
- const eventsDelivered = new Promise<void>((resolve) => {
- resolveEventsDelivered = resolve;
- });
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- yield runtimeEvent({
- type: "permission.ask",
- timestamp: 100,
- source: "runtime",
- payload: {
- requestId: "permission-other-session",
- sessionId: "session-other",
- agentName: "mavis",
- toolName: "bash",
- },
- });
- yield runtimeEvent({
- type: "questionnaire.ask",
- timestamp: 101,
- source: "runtime",
- payload: {
- sessionId: "session-other",
- agentName: "mavis",
- request: {
- id: "question-other-session",
- steps: [
- { id: "step-1", question: "Proceed?", selectionMode: "single" },
- ],
- },
- },
- });
- resolveEventsDelivered?.();
- await new Promise<void>((resolve) => {
- signal.addEventListener("abort", () => resolve(), { once: true });
- });
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await eventsDelivered;
- expect(
- app.transcript.get("permission:permission-other-session"),
- ).toBeUndefined();
- expect(
- app.transcript.get("question:question-other-session"),
- ).toBeUndefined();
- expect(
- app.state.snapshot().sessions.get("session-other")?.interactions
- .permission?.requestId,
- ).toBe("permission-other-session");
- expect(
- app.state.snapshot().sessions.get("session-other")?.interactions
- .questionnaire?.request.id,
- ).toBe("question-other-session");
- await app.stop();
- });
- it("surfaces only descendant permissions while keeping explicit Deny separate from Stop", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const rootSession = {
- sessionId: "session-root",
- title: "Root session",
- workspaceDir: "/workspace",
- status: "started",
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([rootSession]);
- vi.mocked(runtime.getSession).mockResolvedValue(rootSession);
- vi.mocked(runtime.getDelegationSnapshot).mockResolvedValue({
- schemaVersion: 1,
- rootSessionId: "session-root",
- members: [
- {
- sessionId: "session-child",
- parentSessionId: "session-root",
- agentName: "worker",
- task: "Delegate nested work",
- status: "running",
- },
- {
- sessionId: "session-grandchild",
- parentSessionId: "session-child",
- agentName: "verifier",
- task: "Verify the result",
- status: "running",
- },
- ],
- });
- vi.mocked(runtime.getActiveRun).mockImplementation(
- async (sessionId: string) => ({
- schemaVersion: 1,
- sessionId,
- state:
- sessionId === rootSession.sessionId ? "decision-blocked" : "running",
- turnId:
- sessionId === rootSession.sessionId
- ? "turn-root"
- : `turn-${sessionId}`,
- actions: { steer: false },
- }),
- );
- const deniedPermissionIds = new Set<string>();
- vi.mocked(runtime.replyPermission).mockImplementation(
- async (_agentName, requestId) => {
- deniedPermissionIds.add(requestId);
- return true;
- },
- );
- vi.mocked(runtime.listPendingPermissions).mockImplementation(async () =>
- deniedPermissionIds.has("permission-grandchild")
- ? [
- {
- requestId: "permission-unrelated-next",
- sessionId: "session-unrelated",
- agentName: "worker",
- toolName: "bash",
- reason: "Must remain hidden",
- },
- {
- requestId: deniedPermissionIds.has("permission-child-next")
- ? "permission-child-stop"
- : "permission-child-next",
- sessionId: "session-child",
- agentName: "worker",
- toolName: "read",
- reason: deniedPermissionIds.has("permission-child-next")
- ? "Wait for an explicit Stop"
- : "Inspect the delegated output",
- },
- ]
- : [],
- );
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- yield runtimeEvent({
- type: "permission.ask",
- timestamp: 100,
- source: "runtime",
- payload: {
- requestId: "permission-unrelated",
- sessionId: "session-unrelated",
- agentName: "worker",
- toolName: "bash",
- reason: "Must stay hidden",
- },
- });
- yield runtimeEvent({
- type: "permission.ask",
- timestamp: 101,
- source: "runtime",
- payload: {
- requestId: "permission-grandchild",
- sessionId: "session-grandchild",
- agentName: "verifier",
- toolName: "write",
- reason: "Update the verification report",
- allowAlwaysSupported: true,
- },
- });
- await new Promise<void>((resolve) => {
- signal.addEventListener("abort", resolve, { once: true });
- });
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.submit("/resume session-root");
- vi.mocked(runtime.abortSession).mockClear();
- vi.mocked(runtime.stopDelegation).mockClear();
- app.start();
- await vi.waitFor(() =>
- expect(
- app.transcript.get("permission:permission-grandchild"),
- ).toMatchObject({
- kind: "permission",
- status: "blocked",
- }),
- );
- expect(
- app.transcript.get("permission:permission-unrelated"),
- ).toBeUndefined();
- expect(app.interaction.current()?.render(80).join("\n")).toContain(
- "Update the verification report",
- );
- app.interaction.current()?.handleInput?.("3");
- app.interaction.current()?.handleInput?.("\r");
- await vi.waitFor(() =>
- expect(runtime.replyPermission).toHaveBeenCalledWith(
- "verifier",
- "permission-grandchild",
- "deny",
- ),
- );
- expect(runtime.stopDelegation).not.toHaveBeenCalled();
- expect(runtime.abortSession).not.toHaveBeenCalled();
- await vi.waitFor(() =>
- expect(app.interaction.current()?.render(80).join("\n")).toContain(
- "Inspect the delegated output",
- ),
- );
- expect(
- app.transcript.get("permission:permission-unrelated-next"),
- ).toBeUndefined();
- terminal.input?.("\x1b");
- await vi.waitFor(() =>
- expect(runtime.replyPermission).toHaveBeenCalledWith(
- "worker",
- "permission-child-next",
- "deny",
- ),
- );
- expect(runtime.abortSession).not.toHaveBeenCalled();
- expect(runtime.stopDelegation).not.toHaveBeenCalled();
- await vi.waitFor(() =>
- expect(app.interaction.current()?.render(80).join("\n")).toContain(
- "Wait for an explicit Stop",
- ),
- );
- terminal.input?.("\x03");
- await vi.waitFor(() =>
- expect(runtime.abortSession).toHaveBeenCalledWith({
- id: rootSession.sessionId,
- turnId: "turn-root",
- reason: "user_stop",
- }),
- );
- expect(runtime.stopDelegation).toHaveBeenCalledOnce();
- expect(runtime.stopDelegation).toHaveBeenCalledWith(rootSession.sessionId);
- expect(runtime.replyPermission).not.toHaveBeenCalledWith(
- "worker",
- "permission-child-stop",
- "deny",
- );
- await app.stop();
- });
- it("recovers a descendant permission from the Runtime pending list without leaking unrelated requests", async () => {
- const runtime = createRuntime();
- const rootSession = {
- sessionId: "session-recovery-root",
- title: "Recovery root",
- workspaceDir: "/workspace",
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([rootSession]);
- vi.mocked(runtime.getSession).mockResolvedValue(rootSession);
- vi.mocked(runtime.getDelegationSnapshot).mockResolvedValue({
- schemaVersion: 1,
- rootSessionId: rootSession.sessionId,
- members: [
- {
- sessionId: "session-recovery-child",
- parentSessionId: rootSession.sessionId,
- agentName: "worker",
- task: "Resume delegated work",
- status: "running",
- },
- ],
- });
- vi.mocked(runtime.listPendingPermissions).mockResolvedValue([
- {
- requestId: "permission-recovery-unrelated",
- sessionId: "session-other-root",
- agentName: "worker",
- toolName: "bash",
- reason: "Do not show this request",
- },
- {
- requestId: "permission-recovery-child",
- sessionId: "session-recovery-child",
- agentName: "worker",
- toolName: "write",
- reason: "Resume the child request",
- },
- ]);
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.submit("/resume session-recovery-root");
- expect(
- app.transcript.get("permission:permission-recovery-child"),
- ).toMatchObject({
- kind: "permission",
- status: "blocked",
- });
- expect(
- app.transcript.get("permission:permission-recovery-unrelated"),
- ).toBeUndefined();
- expect(app.interaction.current()?.render(80).join("\n")).toContain(
- "Resume the child request",
- );
- await app.stop();
- });
- it("reconnects the Runtime event stream after a transient disconnect", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const observability = createObservability();
- let attempt = 0;
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents() {
- attempt += 1;
- if (attempt === 1) throw new Error("connection reset");
- yield runtimeEvent({
- type: "permission.ask",
- timestamp: 101,
- source: "runtime",
- payload: {
- requestId: "permission-after-reconnect",
- sessionId: "session-1",
- toolName: "read",
- },
- });
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- observability,
- });
- await app.ready;
- await app.submit("Start the current session");
- app.start();
- await vi.waitFor(
- () =>
- expect(
- app.transcript.get("permission:permission-after-reconnect"),
- ).toMatchObject({
- status: "blocked",
- }),
- { timeout: 1500 },
- );
- expect(runtime.watchEvents).toHaveBeenCalledTimes(2);
- expect(observability.recordEventStream).toHaveBeenCalledWith(
- expect.objectContaining({
- state: "retry-scheduled",
- attempt: 1,
- retryDelayMs: 250,
- errorKind: "Error",
- }),
- );
- expect(observability.recordEventStream).toHaveBeenCalledWith(
- expect.objectContaining({
- state: "reconnected",
- attempt: 1,
- downtimeMs: expect.any(Number),
- }),
- );
- expect(app.transcript.snapshot()).not.toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- kind: "warning",
- content: expect.stringContaining("Runtime event stream"),
- }),
- ]),
- );
- await app.stop();
- });
- it("sends pasted local attachments and restores failed sends to the Composer", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const readClipboardImage = vi.fn(async () => ({
- attachment: {
- type: "image" as const,
- filePath: "/tmp/minimax-code-clipboard/diagram.png",
- fileName: "diagram.png",
- mimeType: "image/png",
- sizeBytes: 2048,
- },
- dispose: vi.fn(async () => undefined),
- }));
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- readClipboardImage,
- readClipboardText: async () => null,
- });
- app.start();
- await app.ready;
- terminal.input?.("\u001bv");
- await vi.waitFor(() => expect(readClipboardImage).toHaveBeenCalledTimes(1));
- await app.submit("Inspect the diagram");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- id: "session-1",
- content: "Inspect the diagram",
- attachments: [
- expect.objectContaining({
- local: { filePath: "/tmp/minimax-code-clipboard/diagram.png" },
- meta: expect.objectContaining({ mimeType: "image/png" }),
- }),
- ],
- }),
- expect.any(AbortSignal),
- );
- expect(app.tui.render(80).join("\n")).not.toContain("[Image #1]");
- vi.mocked(runtime.sendMessage).mockImplementationOnce(
- async function* sendMessage() {
- yield { type: "error", message: "provider unavailable" };
- },
- );
- terminal.input?.("\x16");
- await vi.waitFor(() => expect(readClipboardImage).toHaveBeenCalledTimes(2));
- app.editor.setText("Try again");
- app.editor.handleInput("\r");
- await vi.waitFor(() => expect(app.editor.getText()).toContain("Try again"));
- expect(app.tui.render(80).join("\n")).toContain(
- "Error The model provider is temporarily unavailable.",
- );
- expect(app.tui.render(80).join("\n")).not.toContain("Request failed");
- expect(app.editor.getText()).toContain("[Image #1]");
- expect(app.tui.render(80).join("\n")).not.toContain(
- "Message kept under Failed",
- );
- await app.submit("/retry");
- expect(runtime.sendMessage).toHaveBeenLastCalledWith(
- expect.objectContaining({
- content: "Try again",
- attachments: [
- expect.objectContaining({
- local: { filePath: "/tmp/minimax-code-clipboard/diagram.png" },
- }),
- ],
- }),
- expect.any(AbortSignal),
- );
- await app.submit("A fresh turn");
- expect(app.tui.render(80).join("\n")).not.toContain("Couldn't send");
- await app.stop();
- });
- it("restores a content-review retraction to the Composer without a failed Queue item", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const busEvents: TuiRuntimeEvent[] = [];
- let wakeBus: (() => void) | undefined;
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- while (!signal.aborted) {
- if (busEvents.length === 0) {
- await new Promise<void>((resolve) => {
- wakeBus = resolve;
- signal.addEventListener("abort", resolve, { once: true });
- });
- }
- const event = busEvents.shift();
- if (event) yield event;
- }
- },
- );
- let releaseTurn: (() => void) | undefined;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- await new Promise<void>((resolve) => {
- releaseTurn = resolve;
- });
- yield { type: "done" };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- terminal.input?.("review this response");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toBeTruthy(),
- );
- busEvents.push(
- runtimeEvent({
- type: "content.retry.exceeded",
- timestamp: 100,
- source: "runtime-v2",
- payload: { sessionId: "session-1", variant: "content" },
- }),
- runtimeEvent({
- type: "message.rewind",
- timestamp: 101,
- source: "runtime-v2",
- payload: { sessionId: "session-1", contextReset: true },
- }),
- );
- wakeBus?.();
- releaseTurn?.();
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("review this response"),
- );
- const rendered = app.tui.render(100).join("\n");
- expect(rendered).toContain(
- "Content review withdrew this response. Rephrase your request, then retry.",
- );
- expect(rendered).not.toContain("Message kept under Failed");
- await app.submit("/queue");
- expect(app.interaction.current()).toBeUndefined();
- await app.stop();
- });
- it("sends editable @directory/ input as ordinary text", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.submit("Review @src/");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- { id: "session-1", content: "Review @src/", turnId: expect.any(String) },
- expect.any(AbortSignal),
- );
- await expect(app.submit("Review @generated/")).resolves.toBeUndefined();
- expect(runtime.sendMessage).toHaveBeenCalledTimes(2);
- });
- it("pastes a clipboard image without replacing the text draft and cleans it after send", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const dispose = vi.fn(async () => undefined);
- const readClipboardImage = vi.fn(async () => ({
- attachment: {
- type: "image" as const,
- filePath: "/tmp/minimax-code-clipboard/clipboard-1.png",
- fileName: "clipboard-1.png",
- mimeType: "image/png",
- sizeBytes: 4096,
- },
- dispose,
- }));
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- readClipboardImage,
- readClipboardText: async () => null,
- });
- app.start();
- await app.ready;
- app.editor.setText("Keep this draft");
- terminal.input?.("\u001B[118;9u");
- terminal.input?.("\u001B[118;9:3u");
- await vi.waitFor(() => expect(readClipboardImage).toHaveBeenCalledOnce());
- await vi.waitFor(() =>
- expect(app.editor.getText()).toContain("[Image #1]"),
- );
- expect(app.editor.getText()).toBe("Keep this draft [Image #1] ");
- expect(app.tui.render(100).join("\n")).not.toContain(
- "A clipboard media paste is already in progress.",
- );
- await app.submit("Keep this draft");
- expect(runtime.sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({
- id: "session-1",
- content: "Keep this draft",
- attachments: [
- expect.objectContaining({
- local: { filePath: "/tmp/minimax-code-clipboard/clipboard-1.png" },
- meta: expect.objectContaining({ mimeType: "image/png" }),
- }),
- ],
- }),
- expect.any(AbortSignal),
- );
- expect(dispose).toHaveBeenCalledOnce();
- await app.stop();
- });
- it("retains existing draft state when clipboard read or send fails", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const dispose = vi.fn(async () => undefined);
- const readClipboardImage = vi
- .fn()
- .mockResolvedValueOnce({
- attachment: {
- type: "image" as const,
- filePath: "/tmp/minimax-code-clipboard/clipboard-2.png",
- fileName: "clipboard-2.png",
- mimeType: "image/png",
- sizeBytes: 1024,
- },
- dispose,
- })
- .mockRejectedValueOnce(new Error("clipboard has no image"));
- vi.mocked(runtime.sendMessage).mockImplementationOnce(
- async function* sendMessage() {
- yield { type: "error", message: "provider unavailable" };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- readClipboardImage,
- readClipboardText: async () => null,
- });
- app.start();
- await app.ready;
- app.editor.setText("Do not clear me");
- terminal.input?.("\x16");
- await vi.waitFor(() => expect(readClipboardImage).toHaveBeenCalledTimes(1));
- terminal.input?.("\x16");
- await vi.waitFor(() => expect(readClipboardImage).toHaveBeenCalledTimes(2));
- expect(app.editor.getText()).toBe("Do not clear me [Image #1] ");
- await app.submit("Try the image");
- expect(dispose).not.toHaveBeenCalled();
- expect(app.tui.render(80).join("\n")).toContain("[Image #1]");
- terminal.input?.("\x7f");
- expect(app.tui.render(80).join("\n")).not.toContain("[Image #1]");
- expect(dispose).not.toHaveBeenCalled();
- await app.stop();
- expect(dispose).toHaveBeenCalledOnce();
- });
- it("cancels an in-flight clipboard read with Ctrl+C and keeps the draft", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let observedSignal: AbortSignal | undefined;
- const readClipboardImage = vi.fn(
- async (signal?: AbortSignal) =>
- await new Promise<never>((_resolve, reject) => {
- observedSignal = signal;
- signal?.addEventListener(
- "abort",
- () => reject(new Error("cancelled")),
- { once: true },
- );
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- readClipboardImage,
- readClipboardText: async () => null,
- });
- app.start();
- await app.ready;
- app.editor.setText("Draft survives");
- terminal.input?.("\x16");
- await vi.waitFor(() => expect(observedSignal).toBeDefined());
- terminal.input?.("\x03");
- await vi.waitFor(() => expect(observedSignal?.aborted).toBe(true));
- expect(app.editor.getText()).toBe("Draft survives");
- await app.stop();
- });
- it("keeps queued clipboard files alive until the Runtime reports a terminal queue state", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const dispose = vi.fn(async () => undefined);
- let finishTurn: (() => void) | undefined;
- let deliverQueueEvent: ((event: TuiRuntimeEvent) => void) | undefined;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- await new Promise<void>((resolve) => {
- finishTurn = resolve;
- });
- yield { type: "done" };
- },
- );
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents() {
- const event = await new Promise<TuiRuntimeEvent>((resolve) => {
- deliverQueueEvent = resolve;
- });
- yield event;
- },
- );
- const readClipboardImage = vi.fn(async () => ({
- attachment: {
- type: "image" as const,
- filePath: "/tmp/minimax-code-clipboard/clipboard-queue.png",
- fileName: "clipboard-queue.png",
- mimeType: "image/png",
- sizeBytes: 2048,
- },
- dispose,
- }));
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- productFeatures: { queue: true },
- readClipboardImage,
- });
- app.start();
- await app.ready;
- const first = app.submit("First turn");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().status).toBe("running"),
- );
- terminal.input?.("\x16");
- await vi.waitFor(() => expect(readClipboardImage).toHaveBeenCalledOnce());
- await app.submit("Queued with image");
- expect(runtime.enqueueMessage).toHaveBeenCalledWith(
- "session-1",
- "Queued with image",
- expect.objectContaining({
- attachments: [
- expect.objectContaining({ fileName: "clipboard-queue.png" }),
- ],
- }),
- );
- expect(dispose).not.toHaveBeenCalled();
- deliverQueueEvent?.(
- runtimeEvent({
- type: "session.queue.updated",
- timestamp: Date.now(),
- source: "runtime",
- payload: {
- sessionId: "session-1",
- itemId: "queue-1",
- status: "completed",
- queuedCount: 0,
- },
- }),
- );
- await vi.waitFor(() => expect(dispose).toHaveBeenCalledOnce());
- finishTurn?.();
- await first;
- await app.stop();
- });
- it("opens the Desktop model picker and applies the selected session model", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listModels).mockResolvedValue([
- {
- providerId: "minimax",
- modelId: "MiniMax-M3",
- displayName: "MiniMax M3",
- variant: "thinking",
- thinkingConfig: { mode: "switchable", defaultValue: "true" },
- providerKind: "minimax-managed",
- selected: true,
- },
- ]);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("Create a session");
- await app.submit("/model");
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- terminal.input?.("\x1b");
- expect(app.tui.hasOverlay()).toBe(false);
- await app.submit("/model");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.selectModel).toHaveBeenCalledWith(
- expect.objectContaining({
- providerId: "minimax",
- modelId: "MiniMax-M3",
- variant: "thinking",
- }),
- "session-1",
- ),
- );
- await vi.waitFor(() => {
- const rendered = app.tui.render(120).join("\n");
- expect(rendered).toContain("✦ MiniMax M3");
- expect(rendered).toContain("Thinking On");
- expect(rendered).not.toContain("MiniMax M3#thinking");
- });
- await app.stop();
- });
- it("queues a thinking-on model with the configured effort even when the Session stored none", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishTurn: (() => void) | undefined;
- vi.mocked(runtime.listModels).mockResolvedValue([
- {
- providerId: "minimax",
- modelId: "deep-reasoner-1",
- displayName: "deep-reasoner-1",
- selected: true,
- variant: "thinking",
- thinkingConfig: { mode: "switchable", defaultValue: "true" },
- effortOptions: ["xhigh"],
- },
- ]);
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- await new Promise<void>((resolve) => {
- finishTurn = resolve;
- });
- yield { type: "done" };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- productFeatures: { queue: true },
- });
- app.start();
- await app.ready;
- const first = app.submit("First turn");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().status).toBe("running"),
- );
- await app.submit("Queued turn");
- // The Session echo carries no stored effort, so the per-Turn selection has to
- // resolve the configured level. Sending only `variant: thinking` makes Runtime
- // drop the effort and the provider apply its own default.
- expect(runtime.enqueueMessage).toHaveBeenCalledWith(
- "session-1",
- "Queued turn",
- expect.objectContaining({
- model: expect.objectContaining({
- providerId: "minimax",
- modelId: "deep-reasoner-1",
- variant: "thinking",
- thinking: { effort: "xhigh" },
- }),
- }),
- );
- finishTurn?.();
- await first;
- await app.stop();
- });
- it("applies a welcome-screen effort to the new Session before sending its first message", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listModels).mockResolvedValue([
- {
- providerId: "custom_provider:byok",
- modelId: "byok-large-5",
- displayName: "BYOK Large 5",
- selected: true,
- variant: "",
- thinkingConfig: { mode: "switchable", defaultValue: "false" },
- effortOptions: ["low", "medium", "high"],
- },
- ]);
- let releaseDefaultSelection: ((selected: boolean) => void) | undefined;
- let releaseSessionSelection: ((selected: boolean) => void) | undefined;
- vi.mocked(runtime.selectModel)
- .mockImplementationOnce(
- async () =>
- await new Promise<boolean>((resolve) => {
- releaseDefaultSelection = resolve;
- }),
- )
- .mockImplementationOnce(
- async () =>
- await new Promise<boolean>((resolve) => {
- releaseSessionSelection = resolve;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/model");
- terminal.input?.("\u001b[C");
- terminal.input?.("\r");
- const firstTurn = app.submit("First turn");
- await vi.waitFor(() =>
- expect(runtime.selectModel).toHaveBeenNthCalledWith(
- 1,
- expect.objectContaining({ thinking: { effort: "high" }, variant: "" }),
- undefined,
- ),
- );
- expect(runtime.selectModel).toHaveBeenCalledOnce();
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- releaseDefaultSelection?.(true);
- await vi.waitFor(() =>
- expect(runtime.selectModel).toHaveBeenNthCalledWith(
- 2,
- expect.objectContaining({ thinking: { effort: "high" }, variant: "" }),
- "session-1",
- ),
- );
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- releaseSessionSelection?.(true);
- await firstTurn;
- expect(runtime.sendMessage).toHaveBeenCalledOnce();
- expect(runtime.selectModel.mock.invocationCallOrder[1]).toBeLessThan(
- runtime.sendMessage.mock.invocationCallOrder[0] ??
- Number.POSITIVE_INFINITY,
- );
- await app.stop();
- });
- it("does not send the first message when Runtime rejects its pending effort", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listModels).mockResolvedValue([
- {
- providerId: "custom_provider:byok",
- modelId: "byok-large-5",
- displayName: "BYOK Large 5",
- selected: true,
- effortOptions: ["low", "medium", "high"],
- },
- ]);
- vi.mocked(runtime.selectModel)
- .mockResolvedValueOnce(true)
- .mockResolvedValueOnce(false);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/model");
- terminal.input?.("\r");
- await vi.waitFor(() => expect(runtime.selectModel).toHaveBeenCalledOnce());
- await app.submit("First turn");
- expect(runtime.selectModel).toHaveBeenCalledTimes(2);
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- await app.stop();
- });
- it("lets Escape cancel the first turn while Desktop is creating its session", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishSessionCreation:
- | ((session: { sessionId: string; workspaceDir: string }) => void)
- | undefined;
- vi.mocked(runtime.createSession).mockImplementation(
- async () =>
- await new Promise<{ sessionId: string; workspaceDir: string }>(
- (resolve) => {
- finishSessionCreation = resolve;
- },
- ),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const submitting = app.submit("Wait for the session");
- await vi.waitFor(() =>
- expect(app.controller.snapshot()).toMatchObject({
- status: "starting",
- activeTurnId: expect.any(String),
- }),
- );
- expect(app.tui.render(80).join("\n")).toContain("Loading");
- expect(app.tui.render(80).join("\n")).not.toContain("Loading · 0s");
- expect(app.tui.render(80).join("\n")).not.toContain("MCode ·");
- terminal.input?.("\x1b");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toBeUndefined(),
- );
- finishSessionCreation?.({
- sessionId: "session-created",
- workspaceDir: "/workspace",
- });
- await submitting;
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- expect(app.transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({ kind: "assistant", status: "cancelled" }),
- ]),
- );
- await app.stop();
- });
- it("restores a submission attempted while the previous cancellation fence is active", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let releaseCancelledStream: (() => void) | undefined;
- let sendCount = 0;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage(_req: SendMessageReq, signal?: AbortSignal) {
- sendCount += 1;
- if (sendCount === 1) {
- await new Promise<void>((resolve) => {
- signal?.addEventListener("abort", resolve, { once: true });
- });
- await new Promise<void>((resolve) => {
- releaseCancelledStream = resolve;
- });
- } else {
- yield { type: "delta", content: "Second response" };
- }
- yield { type: "done" };
- },
- );
- vi.mocked(runtime.abortSession).mockImplementation(async () => {
- setTimeout(() => releaseCancelledStream?.(), 10);
- return true;
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- terminal.input?.("First request");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().status).toBe("running"),
- );
- terminal.input?.("\x1b");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().cancelling).toBe(true),
- );
- terminal.input?.("Second request");
- terminal.input?.("\r");
- await vi.waitFor(() => expect(app.editor.getText()).toBe("Second request"));
- expect(runtime.sendMessage).toHaveBeenCalledTimes(1);
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toBeUndefined(),
- );
- await vi.waitFor(() =>
- expect(app.controller.snapshot().retiringTurnId).toBeUndefined(),
- );
- expect(app.controller.snapshot().cancelling).toBe(false);
- expect(app.tui.render(80).join("\n")).not.toContain(
- "Stopping the current response",
- );
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledTimes(2),
- );
- expect(runtime.sendMessage).toHaveBeenCalledTimes(2);
- expect(runtime.enqueueMessage).not.toHaveBeenCalled();
- expect(app.transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({ kind: "assistant", status: "cancelled" }),
- ]),
- );
- await app.stop();
- });
- it("restores a failed submission before text typed while cancellation is retiring", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let releaseRuntimeOwnership: (() => void) | undefined;
- const runtimeOwnershipReleased = new Promise<void>((resolve) => {
- releaseRuntimeOwnership = resolve;
- });
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage(_req: SendMessageReq, signal?: AbortSignal) {
- await new Promise<void>((resolve) => {
- signal?.addEventListener("abort", resolve, { once: true });
- });
- await runtimeOwnershipReleased;
- yield { type: "done" };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- terminal.input?.("First request");
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().status).toBe("running"),
- );
- terminal.input?.("\x1b");
- await vi.waitFor(
- () => expect(app.controller.snapshot().retiringTurnId).toBeTruthy(),
- {
- timeout: 2_000,
- },
- );
- expect(app.controller.snapshot()).toMatchObject({
- status: "idle",
- activeTurnId: undefined,
- cancelling: false,
- });
- terminal.input?.("Second request");
- terminal.input?.("\r");
- terminal.input?.("extra detail");
- await vi.waitFor(() => expect(app.editor.getText()).toBe("extra detail"));
- expect(runtime.sendMessage).toHaveBeenCalledTimes(1);
- await vi.waitFor(() =>
- expect(app.tui.render(160).join("\n")).toContain("Second request"),
- );
- expect(app.editor.getText()).toBe("Second request\nextra detail");
- releaseRuntimeOwnership?.();
- await vi.waitFor(() =>
- expect(app.controller.snapshot().retiringTurnId).toBeUndefined(),
- );
- expect(app.tui.render(160).join("\n")).not.toContain(
- "Runtime is still stopping; your draft is preserved",
- );
- await app.stop();
- });
- it("aborts session creation when the TUI stops during its first turn", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishSessionCreation:
- | ((session: { sessionId: string; workspaceDir: string }) => void)
- | undefined;
- vi.mocked(runtime.createSession).mockImplementation(
- async () =>
- await new Promise<{ sessionId: string; workspaceDir: string }>(
- (resolve) => {
- finishSessionCreation = resolve;
- },
- ),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const submitting = app.submit("Wait for the session");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toEqual(
- expect.any(String),
- ),
- );
- const stopping = app.stop();
- finishSessionCreation?.({
- sessionId: "session-created",
- workspaceDir: "/workspace",
- });
- await Promise.all([submitting, stopping]);
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- expect(app.transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({ kind: "assistant", status: "cancelled" }),
- ]),
- );
- expect(terminal.stopped).toBe(true);
- });
- it.each(["minimax", "custom_provider:work"])(
- "hydrates the Runtime-selected startup model for %s",
- async (providerId) => {
- const runtime = createRuntime();
- vi.mocked(runtime.listModels).mockResolvedValue([
- { providerId, modelId: "MiniMax-M3", selected: true },
- ]);
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- try {
- await app.ready;
- expect(runtime.listModels).toHaveBeenCalled();
- expect(runtime.selectModel).not.toHaveBeenCalled();
- await app.submit("/model");
- expect(runtime.selectModel).not.toHaveBeenCalled();
- } finally {
- await app.stop();
- }
- },
- );
- it("offers provider onboarding when Desktop has no models", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listModels).mockResolvedValue([]);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.submit("/model unavailable");
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- const rendered = app.tui.render(120).join("\n");
- expect(rendered).toContain("No matching models");
- expect(rendered).toContain("Add 3rd-party provider");
- terminal.input?.("\x1b");
- await app.stop();
- });
- it("keeps the TUI usable when the Runtime model catalog fails to load", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- vi.mocked(runtime.listModels).mockRejectedValue(
- new Error("catalog unavailable"),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await expect(app.submit("/model")).resolves.toBeUndefined();
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.transcript.snapshot().at(-1)).toMatchObject({
- kind: "warning",
- content: "Couldn't load models: catalog unavailable. Retry /model.",
- });
- });
- it("shows a follow-up as queued while Runtime admission is pending", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishFirstTurn: (() => void) | undefined;
- let finishEnqueue: (() => void) | undefined;
- let admitted = false;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- await new Promise<void>((resolve) => {
- finishFirstTurn = resolve;
- });
- yield { type: "done" };
- },
- );
- vi.mocked(runtime.enqueueMessage).mockImplementation(async () => {
- await new Promise<void>((resolve) => {
- finishEnqueue = resolve;
- });
- admitted = true;
- return { itemId: "queue-pending-1", status: "queued", position: 1 };
- });
- vi.mocked(runtime.listQueuedMessages).mockImplementation(async () =>
- admitted
- ? [
- {
- itemId: "queue-pending-1",
- sessionId: "session-1",
- status: "queued",
- content: "Second turn",
- },
- ]
- : [],
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const first = app.submit("First turn");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toEqual(
- expect.any(String),
- ),
- );
- terminal.input?.("Second turn");
- terminal.input?.("\x1b\r");
- await vi.waitFor(() =>
- expect(runtime.enqueueMessage).toHaveBeenCalledOnce(),
- );
- try {
- expect(
- app.transcript
- .snapshot()
- .find((cell) => cell.content.includes("Second turn")),
- ).toBeUndefined();
- const rendered = app.tui.render(100).join("\n");
- expect(rendered).not.toContain("Adding to Queue");
- expect(rendered).toContain("Next · after current response");
- expect(rendered).toContain("Second turn");
- finishEnqueue?.();
- await vi.waitFor(() => {
- const confirmed = app.tui.render(100).join("\n");
- expect(confirmed).not.toContain("Adding to Queue");
- expect(confirmed).toContain("Next · after current response");
- expect(confirmed).toContain("Second turn");
- });
- } finally {
- finishEnqueue?.();
- finishFirstTurn?.();
- await first;
- await app.stop();
- }
- });
- it("removes the local Queue admission and restores the Draft when Runtime rejects it", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishFirstTurn: (() => void) | undefined;
- let rejectEnqueue: ((error: Error) => void) | undefined;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- await new Promise<void>((resolve) => {
- finishFirstTurn = resolve;
- });
- yield { type: "done" };
- },
- );
- vi.mocked(runtime.enqueueMessage).mockImplementation(
- async () =>
- await new Promise<never>((_resolve, reject) => {
- rejectEnqueue = reject;
- }),
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const first = app.submit("First turn");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toEqual(
- expect.any(String),
- ),
- );
- terminal.input?.("Keep this Draft");
- terminal.input?.("\x1b\r");
- await vi.waitFor(() =>
- expect(app.tui.render(100).join("\n")).toContain(
- "Next · after current response",
- ),
- );
- await vi.waitFor(() =>
- expect(runtime.enqueueMessage).toHaveBeenCalledOnce(),
- );
- rejectEnqueue?.(new Error("queue unavailable"));
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("Keep this Draft"),
- );
- const rendered = app.tui.render(100).join("\n");
- expect(rendered).not.toContain("Next · after current response");
- expect(rendered).toContain("Couldn't add this message to the Queue");
- finishFirstTurn?.();
- await first;
- await app.stop();
- });
- it("shows every rapid follow-up as queued while Runtime admissions are serialized", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishFirstTurn: (() => void) | undefined;
- const releaseAdmissions: Array<() => void> = [];
- const queued: TuiQueuedMessage[] = [];
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- await new Promise<void>((resolve) => {
- finishFirstTurn = resolve;
- });
- yield { type: "done" };
- },
- );
- vi.mocked(runtime.enqueueMessage).mockImplementation(
- async (sessionId, content) => {
- await new Promise<void>((resolve) => releaseAdmissions.push(resolve));
- const item = {
- itemId: `queue-rapid-${String(queued.length + 1)}`,
- sessionId,
- status: "queued",
- content,
- } satisfies TuiQueuedMessage;
- queued.push(item);
- return {
- itemId: item.itemId,
- status: item.status,
- position: queued.length,
- };
- },
- );
- vi.mocked(runtime.listQueuedMessages).mockImplementation(async () => [
- ...queued,
- ]);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- const first = app.submit("First turn");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toEqual(
- expect.any(String),
- ),
- );
- terminal.input?.("Second turn");
- terminal.input?.("\x1b\r");
- terminal.input?.("Third turn");
- terminal.input?.("\x1b\r");
- await vi.waitFor(() => {
- const rendered = app.tui.render(100).join("\n");
- expect(rendered).not.toContain("Adding");
- expect(rendered).toContain("Queue · 2 next");
- expect(rendered).toContain("Second turn");
- expect(rendered).toContain("Third turn");
- });
- expect(
- app.transcript
- .snapshot()
- .filter(
- (cell) =>
- cell.content.includes("Second turn") ||
- cell.content.includes("Third turn"),
- ),
- ).toEqual([]);
- await vi.waitFor(() =>
- expect(runtime.enqueueMessage).toHaveBeenCalledOnce(),
- );
- releaseAdmissions[0]?.();
- await vi.waitFor(() =>
- expect(runtime.enqueueMessage).toHaveBeenCalledTimes(2),
- );
- const partiallyConfirmed = app.tui.render(100).join("\n");
- expect(partiallyConfirmed).toContain("Queue · 2 next");
- expect(partiallyConfirmed.indexOf("Second turn")).toBeLessThan(
- partiallyConfirmed.indexOf("Third turn"),
- );
- releaseAdmissions[1]?.();
- await vi.waitFor(() =>
- expect(app.tui.render(100).join("\n")).toContain("Queue · 2 next"),
- );
- finishFirstTurn?.();
- await first;
- await app.stop();
- });
- it("submits follow-ups to the runtime-owned queue and never drains them in the CLI", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishFirstTurn: (() => void) | undefined;
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage(req: SendMessageReq) {
- const content = req.content ?? "";
- if (content === "First turn") {
- await new Promise<void>((resolve) => {
- finishFirstTurn = resolve;
- });
- }
- yield { type: "delta", content: `Reply to ${content || "attachment"}` };
- yield { type: "done" };
- },
- );
- const attachment = {
- type: "file" as const,
- filePath: "/workspace/notes.md",
- fileName: "notes.md",
- mimeType: "text/markdown",
- sizeBytes: 32,
- };
- const queued: Array<{
- itemId: string;
- sessionId: string;
- status: string;
- content: string;
- }> = [];
- vi.mocked(runtime.enqueueMessage).mockImplementation(
- async (sessionId, content) => {
- const item = {
- itemId: `queue-${queued.length + 1}`,
- sessionId,
- status: "queued",
- content,
- };
- queued.push(item);
- return {
- itemId: item.itemId,
- status: item.status,
- position: queued.length,
- };
- },
- );
- vi.mocked(runtime.listQueuedMessages).mockImplementation(async () => [
- ...queued,
- ]);
- vi.mocked(runtime.deleteQueuedMessage).mockImplementation(
- async (_sessionId, itemId) => {
- const index = queued.findIndex((item) => item.itemId === itemId);
- if (index < 0) return undefined;
- return queued.splice(index, 1)[0];
- },
- );
- const readClipboardImage = vi.fn(async () => ({
- attachment,
- dispose: vi.fn(async () => undefined),
- }));
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- readClipboardImage,
- });
- app.start();
- await app.ready;
- const first = app.submit("First turn");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toEqual(
- expect.any(String),
- ),
- );
- terminal.input?.("\x16");
- await vi.waitFor(() => expect(readClipboardImage).toHaveBeenCalledOnce());
- await app.submit("Second turn");
- await app.submit("Third turn");
- expect(runtime.sendMessage).toHaveBeenCalledTimes(1);
- expect(runtime.enqueueMessage).toHaveBeenNthCalledWith(
- 1,
- "session-1",
- "Second turn",
- {
- attachments: [attachment],
- model: { providerId: "minimax", modelId: "MiniMax-M2.7" },
- },
- );
- expect(runtime.enqueueMessage).toHaveBeenNthCalledWith(
- 2,
- "session-1",
- "Third turn",
- {
- attachments: [],
- model: { providerId: "minimax", modelId: "MiniMax-M2.7" },
- },
- );
- expect(app.transcript.get("queue:queue-1")).toBeUndefined();
- expect(app.transcript.get("queue:queue-2")).toBeUndefined();
- const queuedUi = app.tui.render(80).join("\n");
- expect(queuedUi).toContain("Queue · 2 next");
- expect(queuedUi).toContain("after current response");
- expect(queuedUi).toContain("Second turn");
- expect(queuedUi).toContain("Third turn");
- expect(queuedUi).toContain("/queue manage");
- await app.submit("/queue");
- expect(app.interaction.render(80).join("\n")).toContain("Queue");
- expect(app.interaction.render(80).join("\n")).toContain("2 next");
- expect(app.interaction.render(80).join("\n")).toContain("› 2. Third turn");
- expect(app.editor.getText()).toBe("");
- terminal.input?.("d");
- expect(app.interaction.render(80).join("\n")).toContain(
- "Remove this next message?",
- );
- terminal.input?.("\r");
- await vi.waitFor(() =>
- expect(runtime.deleteQueuedMessage).toHaveBeenCalledWith(
- "session-1",
- "queue-2",
- ),
- );
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).not.toContain("Third turn"),
- );
- expect(app.tui.render(80).join("\n")).toContain("Second turn");
- terminal.input?.("\x1b");
- finishFirstTurn?.();
- await first;
- expect(
- vi.mocked(runtime.sendMessage).mock.calls.map((call) => call[0].content),
- ).toEqual(["First turn"]);
- expect(app.tui.render(80).join("\n")).toContain(
- "Next · after current response",
- );
- expect(app.tui.render(80).join("\n")).toContain("Second turn");
- expect(app.tui.render(80).join("\n")).not.toContain("Sending next");
- await app.stop();
- });
- it("uses the Runtime queue when a resumed started session is active before queue/model hydration finishes", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let releaseQueue: ((items: TuiQueuedMessage[]) => void) | undefined;
- const slowQueue = new Promise<TuiQueuedMessage[]>((resolve) => {
- releaseQueue = resolve;
- });
- let releaseModels: ((models: TuiModel[]) => void) | undefined;
- const slowModels = new Promise<TuiModel[]>((resolve) => {
- releaseModels = resolve;
- });
- const session = {
- sessionId: "session-running",
- title: "Running session",
- workspaceDir: "/workspace",
- status: "started",
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([session]);
- vi.mocked(runtime.getSession).mockResolvedValue(session);
- vi.mocked(runtime.getActiveRun).mockResolvedValue({
- schemaVersion: 1,
- sessionId: "session-running",
- state: "running",
- turnId: "turn-running",
- actions: { steer: true },
- });
- vi.mocked(runtime.listQueuedMessages).mockImplementation(
- async () => slowQueue,
- );
- vi.mocked(runtime.listModels).mockImplementation(async (sessionId) =>
- sessionId ? slowModels : [],
- );
- vi.mocked(runtime.enqueueMessage).mockResolvedValue({
- itemId: "queue-running-1",
- status: "queued",
- position: 1,
- });
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- productFeatures: { queue: true },
- });
- await app.ready;
- const resuming = app.submit("/resume session-running");
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).toContain("Loading"),
- );
- expect(app.tui.render(80).join("\n")).not.toContain("Loading · 0s");
- const followUp = app.submit("Send this through the Runtime queue");
- await vi.waitFor(() =>
- expect(runtime.enqueueMessage).toHaveBeenCalledWith(
- "session-running",
- "Send this through the Runtime queue",
- { attachments: [] },
- ),
- );
- expect(runtime.sendMessage).not.toHaveBeenCalled();
- releaseQueue?.([]);
- releaseModels?.([]);
- await Promise.all([resuming, followUp]);
- await app.stop();
- });
- it("does not recover a terminal session as an active turn", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const session = {
- sessionId: "session-finished",
- title: "Finished session",
- workspaceDir: "/workspace",
- status: "finished",
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([session]);
- vi.mocked(runtime.getSession).mockResolvedValue(session);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- await app.submit("/resume session-finished");
- expect(app.tui.render(80).join("\n")).not.toContain("Esc to interrupt");
- expect(app.tui.render(80).join("\n")).toContain("Message · Enter send");
- await app.stop();
- });
- it("does not recover a stale started Session when Runtime is already idle", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const session = {
- sessionId: "session-stale",
- title: "Stale session",
- workspaceDir: "/workspace",
- status: "started",
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([session]);
- vi.mocked(runtime.getSession).mockResolvedValue(session);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-stale");
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).not.toContain("Loading"),
- );
- const rendered = app.tui.render(80).join("\n");
- expect(rendered).not.toContain("Thinking");
- expect(rendered).not.toContain("Stopping response");
- expect(rendered).not.toContain("Stopping the current response");
- expect(rendered).toContain("Message · Enter send");
- await app.stop();
- });
- it("stops a recovered Runtime turn with /stop and Escape through abortSession", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const session = {
- sessionId: "session-running",
- title: "Running session",
- workspaceDir: "/workspace",
- status: "started",
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([session]);
- vi.mocked(runtime.getSession).mockResolvedValue(session);
- vi.mocked(runtime.getActiveRun).mockResolvedValue({
- schemaVersion: 1,
- sessionId: "session-running",
- state: "running",
- turnId: "turn-running",
- actions: { steer: true },
- });
- vi.mocked(runtime.abortSession).mockResolvedValue(true);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-running");
- await app.submit("/stop");
- expect(runtime.abortSession).toHaveBeenCalledWith(
- expect.objectContaining({ id: "session-running" }),
- );
- expect(app.tui.render(80).join("\n")).not.toContain("Esc to interrupt");
- vi.mocked(runtime.abortSession).mockClear();
- await app.submit("/resume session-running");
- terminal.input?.("\x1b");
- await vi.waitFor(() =>
- expect(runtime.abortSession).toHaveBeenCalledWith(
- expect.objectContaining({ id: "session-running" }),
- ),
- );
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).not.toContain("Esc to interrupt"),
- );
- expect(app.tui.render(80).join("\n")).not.toMatch(
- /Stopped after|Interrupted after/u,
- );
- await app.stop();
- });
- it.each([
- [
- "rejects the abort",
- async () => false,
- "Runtime did not confirm that the active response stopped.",
- ],
- [
- "fails the abort request",
- async () => Promise.reject(new Error("connection lost")),
- "Couldn't stop the current response:",
- ],
- ])(
- "clears the recovered foreground projection and reports when Runtime %s",
- async (_label, abort, expectedError) => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const session = {
- sessionId: "session-running",
- title: "Running session",
- workspaceDir: "/workspace",
- status: "started",
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([session]);
- vi.mocked(runtime.getSession).mockResolvedValue(session);
- vi.mocked(runtime.getActiveRun).mockResolvedValue({
- schemaVersion: 1,
- sessionId: "session-running",
- state: "running",
- turnId: "turn-running",
- actions: { steer: false },
- });
- vi.mocked(runtime.abortSession).mockImplementation(abort);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-running");
- terminal.input?.("\x1b");
- await vi.waitFor(() =>
- expect(runtime.abortSession).toHaveBeenCalledWith(
- expect.objectContaining({ id: "session-running" }),
- ),
- );
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).not.toContain("Esc to interrupt"),
- );
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).toContain(expectedError),
- );
- await app.stop();
- },
- );
- it("keeps Request failed visible when Runtime still has queued follow-ups", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let finishTurn: (() => void) | undefined;
- const queued = [
- {
- itemId: "queue-after-error",
- sessionId: "session-1",
- status: "queued",
- content: "Continue after failure",
- },
- ];
- vi.mocked(runtime.listQueuedMessages).mockResolvedValue(queued);
- vi.mocked(runtime.enqueueMessage).mockResolvedValue({
- itemId: "queue-after-error",
- status: "queued",
- position: 1,
- });
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- await new Promise<void>((resolve) => {
- finishTurn = resolve;
- });
- yield { type: "error", message: "provider unavailable" };
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- productFeatures: { queue: true },
- });
- await app.ready;
- const first = app.submit("First turn");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toEqual(
- expect.any(String),
- ),
- );
- await app.submit("Continue after failure");
- finishTurn?.();
- await first;
- expect(app.tui.render(80).join("\n")).toContain(
- "Error The model provider is temporarily unavailable.",
- );
- expect(app.tui.render(80).join("\n")).not.toContain("Request failed");
- await app.stop();
- });
- it("converges a recovered running state after the Runtime emits a terminal event", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const session = {
- sessionId: "session-recovered",
- title: "Recovered session",
- workspaceDir: "/workspace",
- status: "started",
- };
- const busEvents: TuiRuntimeEvent[] = [];
- let wakeBus: (() => void) | undefined;
- const waitForNextBusEvent = (signal: AbortSignal) =>
- new Promise<void>((resolve) => {
- wakeBus = resolve;
- signal.addEventListener("abort", resolve, { once: true });
- });
- vi.mocked(runtime.listSessions).mockResolvedValue([session]);
- vi.mocked(runtime.getSession).mockResolvedValue(session);
- let activeRecoveredTurn = true;
- vi.mocked(runtime.getActiveRun).mockImplementation(async () =>
- activeRecoveredTurn
- ? {
- schemaVersion: 1,
- sessionId: "session-recovered",
- state: "running",
- turnId: "turn-recovered",
- actions: { steer: true },
- }
- : {
- schemaVersion: 1,
- sessionId: "session-recovered",
- state: "terminal",
- turnId: "turn-recovered",
- actions: { steer: false },
- },
- );
- let durableMessages: Awaited<ReturnType<TuiRuntime["getMessages"]>> = [];
- vi.mocked(runtime.getMessages).mockImplementation(
- async () => durableMessages,
- );
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- while (!signal.aborted) {
- if (busEvents.length === 0) await waitForNextBusEvent(signal);
- if (signal.aborted) return;
- const event = busEvents.shift();
- if (event) yield event;
- }
- },
- );
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-recovered");
- expect(app.tui.render(80).join("\n")).toContain("Loading");
- expect(app.tui.render(80).join("\n")).not.toContain("Loading · 0s");
- await vi.waitFor(() =>
- expect(runtime.watchSessionTurn).toHaveBeenCalledWith(
- "session-recovered",
- "turn-recovered",
- expect.any(AbortSignal),
- ),
- );
- activeRecoveredTurn = false;
- const historyCallsBeforeTerminal = vi.mocked(runtime.getMessages).mock.calls
- .length;
- durableMessages = [
- {
- id: "message-recovered-assistant",
- turnId: "turn-recovered",
- role: "assistant",
- content: "Recovered answer",
- timestamp: 101,
- },
- ];
- busEvents.push(
- runtimeEvent({
- type: "session.finish",
- timestamp: 101,
- source: "runtime",
- payload: {
- sessionId: "session-recovered",
- turnId: "turn-recovered",
- status: "finished",
- },
- }),
- );
- wakeBus?.();
- wakeBus = undefined;
- await vi.waitFor(() =>
- expect(vi.mocked(runtime.getMessages).mock.calls.length).toBeGreaterThan(
- historyCallsBeforeTerminal,
- ),
- );
- await vi.waitFor(() => {
- const rendered = app.tui.render(80).join("\n");
- expect(rendered).not.toContain("Esc to interrupt");
- expect(app.transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- kind: "assistant",
- content: "Recovered answer",
- }),
- ]),
- );
- });
- await app.stop();
- });
- it("clears a recovered decision block when switching to a session without a pending decision", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- let controllerSessionId = "";
- const firstSession = {
- sessionId: "session-blocked",
- title: "Blocked session",
- workspaceDir: "/workspace",
- status: "started",
- };
- const secondSession = {
- sessionId: "session-clear",
- title: "Clear session",
- workspaceDir: "/workspace",
- status: "finished",
- };
- vi.mocked(runtime.listSessions).mockResolvedValue([
- firstSession,
- secondSession,
- ]);
- vi.mocked(runtime.getSession).mockImplementation(async (sessionId) =>
- sessionId === firstSession.sessionId ? firstSession : secondSession,
- );
- vi.mocked(runtime.listPendingPermissions).mockImplementation(async () =>
- controllerSessionId === firstSession.sessionId
- ? [
- {
- requestId: "permission-blocked",
- sessionId: firstSession.sessionId,
- agentName: "mavis",
- toolName: "bash",
- reason: "Run checks",
- allowAlwaysSupported: true,
- },
- ]
- : [],
- );
- vi.mocked(runtime.getPendingQuestionnaire).mockResolvedValue(undefined);
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- controllerSessionId = firstSession.sessionId;
- await app.submit("/resume session-blocked");
- expect(app.tui.hasOverlay()).toBe(false);
- expect(app.interaction.isActive()).toBe(true);
- expect(app.tui.render(80).join("\n")).toContain("Approval required");
- controllerSessionId = secondSession.sessionId;
- await app.submit("/resume session-clear");
- expect(app.tui.hasOverlay()).toBe(false);
- const rendered = app.tui.render(80).join("\n");
- expect(rendered).not.toContain("Approval required");
- expect(rendered).not.toContain("Reply required");
- await app.stop();
- });
- it("moves a sending follow-up from the Composer area into the user transcript", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const busEvents: TuiRuntimeEvent[] = [];
- let wakeBus: (() => void) | undefined;
- const waitForNextBusEvent = (signal: AbortSignal) =>
- new Promise<void>((resolve) => {
- wakeBus = resolve;
- signal.addEventListener("abort", resolve, { once: true });
- });
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-existing",
- title: "Existing",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.listQueuedMessages).mockResolvedValue([
- {
- itemId: "queue-with-attachment",
- sessionId: "session-existing",
- status: "queued",
- content: "Review the attached plan",
- attachments: [
- {
- meta: {
- attachmentType: "file",
- fileName: "plan.md",
- mimeType: "text/markdown",
- sizeBytes: 42,
- },
- local: { filePath: "/workspace/plan.md" },
- },
- ],
- },
- {
- itemId: "queue-that-fails",
- sessionId: "session-existing",
- status: "queued",
- content: "Run the failing follow-up",
- },
- ]);
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- while (!signal.aborted) {
- if (busEvents.length === 0) await waitForNextBusEvent(signal);
- if (signal.aborted) return;
- const event = busEvents.shift();
- if (event) yield event;
- }
- },
- );
- const emitQueueStatus = (
- itemId: string,
- status: string,
- failedReason?: string,
- ) => {
- busEvents.push(
- runtimeEvent({
- type: "session.queue.updated",
- timestamp: Date.now(),
- source: "runtime",
- payload: {
- sessionId: "session-existing",
- itemId,
- status,
- queuedCount: status === "queued" ? 1 : 0,
- ...(failedReason ? { failedReason } : {}),
- },
- }),
- );
- wakeBus?.();
- wakeBus = undefined;
- };
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- productFeatures: { queue: true },
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-existing");
- expect(app.tui.render(80).join("\n")).toContain("Queue · 2 next");
- expect(app.tui.render(80).join("\n")).toContain("Review the attached plan");
- expect(app.transcript.get("queue:queue-with-attachment")).toBeUndefined();
- emitQueueStatus("queue-with-attachment", "running");
- await vi.waitFor(() =>
- expect(app.transcript.get("queue:queue-with-attachment")).toMatchObject({
- kind: "user",
- status: "succeeded",
- content: expect.stringContaining("Review the attached plan"),
- }),
- );
- expect(app.tui.render(80).join("\n")).not.toContain("SENDING");
- expect(app.tui.render(80).join("\n")).toContain(
- "Next · after current response",
- );
- expect(app.tui.render(80).join("\n")).toContain(
- "Run the failing follow-up",
- );
- expect(
- app.transcript.get("queue:queue-with-attachment")?.attachments,
- ).toEqual([
- expect.objectContaining({
- fileName: "plan.md",
- mimeType: "text/markdown",
- sizeBytes: 42,
- }),
- ]);
- emitQueueStatus("queue-with-attachment", "completed");
- await vi.waitFor(() =>
- expect(app.transcript.get("queue:queue-with-attachment")).toMatchObject({
- kind: "user",
- status: "succeeded",
- content: expect.stringContaining("Review the attached plan"),
- }),
- );
- emitQueueStatus("queue-that-fails", "running");
- await vi.waitFor(() =>
- expect(app.transcript.get("queue:queue-that-fails")).toMatchObject({
- kind: "user",
- status: "succeeded",
- }),
- );
- emitQueueStatus(
- "queue-that-fails",
- "admission-rejected",
- "provider unavailable",
- );
- await vi.waitFor(() =>
- expect(app.editor.getText()).toBe("Run the failing follow-up"),
- );
- expect(app.tui.render(80).join("\n")).toContain(
- "Error Message was not added to the Queue: provider unavailable.",
- );
- expect(app.transcript.get("queue:queue-that-fails")).toMatchObject({
- kind: "user",
- status: "succeeded",
- });
- expect(app.tui.render(80).join("\n")).toContain(
- "Run the failing follow-up",
- );
- await app.stop();
- });
- it("refreshes history when Desktop Runtime finishes an auto-drained queued turn", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const busEvents: TuiRuntimeEvent[] = [];
- let wakeBus: (() => void) | undefined;
- const waitForNextBusEvent = (signal: AbortSignal) =>
- new Promise<void>((resolve) => {
- wakeBus = resolve;
- signal.addEventListener("abort", resolve, { once: true });
- });
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-existing",
- title: "Existing",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.getMessages)
- .mockResolvedValueOnce([])
- .mockResolvedValue([
- {
- id: "message-user-queued",
- turnId: "turn-queued",
- role: "user",
- content: "Runtime follow-up",
- timestamp: 100,
- },
- {
- id: "message-assistant-queued",
- turnId: "turn-queued",
- role: "assistant",
- content: "Runtime follow-up answer",
- timestamp: 101,
- },
- ]);
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- while (!signal.aborted) {
- if (busEvents.length === 0) {
- await waitForNextBusEvent(signal);
- }
- if (signal.aborted) return;
- const event = busEvents.shift();
- if (event) yield event;
- }
- },
- );
- const emit = (event: RawTuiRuntimeEvent) => {
- busEvents.push(runtimeEvent(event));
- wakeBus?.();
- wakeBus = undefined;
- };
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- app.start();
- await app.ready;
- await app.submit("/resume session-existing");
- emit({
- type: "session.start",
- timestamp: 100,
- source: "runtime",
- payload: {
- sessionId: "session-existing",
- turnId: "turn-queued",
- source: "queued-drain",
- queueItemIds: ["queue-1"],
- },
- });
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).toContain("Loading"),
- );
- expect(app.tui.render(80).join("\n")).not.toContain("Loading · 0s");
- expect(runtime.watchSessionTurn).toHaveBeenCalledWith(
- "session-existing",
- "turn-queued",
- expect.any(AbortSignal),
- );
- emit({
- type: "session.finish",
- timestamp: 101,
- source: "runtime",
- payload: {
- sessionId: "session-existing",
- turnId: "turn-queued",
- status: "finished",
- },
- });
- await vi.waitFor(() =>
- expect(app.transcript.snapshot()).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- kind: "assistant",
- content: "Runtime follow-up answer",
- }),
- ]),
- ),
- );
- const historyProjectionCalls = vi
- .mocked(runtime.getMessages)
- .mock.calls.filter(
- ([sessionId, limit]) =>
- sessionId === "session-existing" && limit === undefined,
- );
- expect(historyProjectionCalls).toHaveLength(2);
- await app.stop();
- });
- it("writes one structured result when an auto-drained queued turn finishes", async () => {
- const dataDir = await mkdtemp(join(tmpdir(), "mcode-queued-result-"));
- const resultPath = join(dataDir, "results.jsonl");
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const busEvents: TuiRuntimeEvent[] = [];
- let wakeBus: (() => void) | undefined;
- let durableMessages: Awaited<ReturnType<TuiRuntime["getMessages"]>> = [];
- vi.mocked(runtime.listSessions).mockResolvedValue([
- {
- sessionId: "session-existing",
- title: "Existing",
- workspaceDir: "/workspace",
- },
- ]);
- vi.mocked(runtime.getMessages).mockImplementation(
- async () => durableMessages,
- );
- const waitForBusEvent = (signal: AbortSignal) =>
- new Promise<void>((resolve) => {
- wakeBus = resolve;
- signal.addEventListener("abort", resolve, { once: true });
- });
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- while (!signal.aborted) {
- if (busEvents.length === 0) await waitForBusEvent(signal);
- if (signal.aborted) return;
- const event = busEvents.shift();
- if (event) yield event;
- }
- },
- );
- const emit = (event: RawTuiRuntimeEvent) => {
- busEvents.push(runtimeEvent(event));
- wakeBus?.();
- wakeBus = undefined;
- };
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- statusLineItems: ["build-mode"],
- automationResultPath: resultPath,
- });
- try {
- app.start();
- await app.ready;
- await app.submit("/resume session-existing");
- emit({
- type: "session.start",
- timestamp: 100,
- source: "runtime",
- payload: {
- sessionId: "session-existing",
- turnId: "turn-queued-result",
- source: "queued-drain",
- queueItemIds: ["queue-result"],
- },
- });
- await vi.waitFor(() =>
- expect(runtime.watchSessionTurn).toHaveBeenCalledWith(
- "session-existing",
- "turn-queued-result",
- expect.any(AbortSignal),
- ),
- );
- durableMessages = [
- {
- id: "message-user-result",
- turnId: "turn-queued-result",
- role: "user",
- content: "Queued request",
- timestamp: 101,
- },
- {
- id: "message-assistant-result",
- turnId: "turn-queued-result",
- role: "assistant",
- content: "Queued structured answer",
- timestamp: 102,
- },
- ];
- emit({
- type: "session.finish",
- timestamp: 103,
- source: "runtime",
- payload: {
- sessionId: "session-existing",
- turnId: "turn-queued-result",
- status: "finished",
- },
- });
- await vi.waitFor(async () => {
- const records = (await readFile(resultPath, "utf8"))
- .trim()
- .split("\n")
- .map(JSON.parse);
- expect(records).toEqual([
- expect.objectContaining({
- type: "exec.result",
- sessionId: "session-existing",
- turnId: "turn-queued-result",
- status: "succeeded",
- answer: "Queued structured answer",
- }),
- ]);
- });
- expect(app.controller.snapshot().lastSettledTurn).toEqual({
- sessionId: "session-existing",
- turnId: "turn-queued-result",
- status: "succeeded",
- });
- } finally {
- await app.stop();
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it("settles an auto-drained follow-up once and replaces waiting state with run duration", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const busEvents: TuiRuntimeEvent[] = [];
- const queuedItems: TuiQueuedMessage[] = [];
- let wakeBus: (() => void) | undefined;
- let finishFirstTurn: (() => void) | undefined;
- let finishQueuedStream: (() => void) | undefined;
- const queuedStreamClosed = vi.fn();
- let durableMessages: Awaited<ReturnType<TuiRuntime["getMessages"]>> = [];
- const waitForNextBusEvent = (signal: AbortSignal) =>
- new Promise<void>((resolve) => {
- wakeBus = resolve;
- signal.addEventListener("abort", resolve, { once: true });
- });
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage() {
- await new Promise<void>((resolve) => {
- finishFirstTurn = resolve;
- });
- yield { type: "delta", content: "First answer" };
- yield { type: "done" };
- },
- );
- vi.mocked(runtime.watchSessionTurn).mockImplementation(
- async function* watchSessionTurn() {
- try {
- await new Promise<void>((resolve) => {
- finishQueuedStream = resolve;
- });
- yield { type: "delta", content: "Queued answer" };
- yield { type: "done" };
- } finally {
- queuedStreamClosed();
- }
- },
- );
- vi.mocked(runtime.enqueueMessage).mockImplementation(
- async (sessionId, content) => {
- const item: TuiQueuedMessage = {
- itemId: "queue-b",
- sessionId,
- status: "queued",
- content,
- };
- queuedItems.push(item);
- return { itemId: item.itemId, status: item.status, position: 1 };
- },
- );
- vi.mocked(runtime.listQueuedMessages).mockImplementation(async () => [
- ...queuedItems,
- ]);
- vi.mocked(runtime.getMessages).mockImplementation(
- async () => durableMessages,
- );
- vi.mocked(runtime.watchEvents).mockImplementation(
- async function* watchEvents(signal) {
- while (!signal.aborted) {
- if (busEvents.length === 0) await waitForNextBusEvent(signal);
- if (signal.aborted) return;
- const event = busEvents.shift();
- if (event) yield event;
- }
- },
- );
- const emit = (event: RawTuiRuntimeEvent) => {
- busEvents.push(runtimeEvent(event));
- wakeBus?.();
- wakeBus = undefined;
- };
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- productFeatures: { queue: true },
- });
- app.start();
- await app.ready;
- const first = app.submit("A");
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toBeDefined(),
- );
- await app.submit("B");
- expect(app.tui.render(80).join("\n")).toContain(
- "Next · after current response",
- );
- expect(app.tui.render(80).join("\n")).toContain("B");
- finishFirstTurn?.();
- await first;
- const queuedItem = queuedItems[0];
- expect(queuedItem).toBeDefined();
- if (!queuedItem) throw new Error("Expected B to be queued.");
- queuedItems[0] = { ...queuedItem, status: "running" };
- emit({
- type: "session.queue.updated",
- timestamp: 200,
- source: "runtime",
- payload: {
- sessionId: "session-1",
- itemId: "queue-b",
- status: "running",
- queuedCount: 0,
- },
- });
- emit({
- type: "session.start",
- timestamp: 201,
- source: "runtime",
- payload: {
- sessionId: "session-1",
- turnId: "turn_queue_b",
- source: "queued-drain",
- queueItemIds: ["queue-b"],
- },
- });
- await vi.waitFor(() =>
- expect(app.tui.render(80).join("\n")).toContain("Loading"),
- );
- expect(app.tui.render(80).join("\n")).not.toContain("Loading · 0s");
- queuedItems.length = 0;
- durableMessages = [
- {
- id: "history-user-b",
- turnId: "turn_queue_b",
- role: "user",
- content: "B",
- timestamp: 202,
- },
- {
- id: "history-assistant-b",
- turnId: "turn_queue_b",
- role: "assistant",
- content: "Queued answer",
- timestamp: 203,
- },
- ];
- finishQueuedStream?.();
- await vi.waitFor(() => expect(queuedStreamClosed).toHaveBeenCalledOnce());
- emit({
- type: "session.finish",
- timestamp: 3_201,
- source: "runtime",
- payload: {
- sessionId: "session-1",
- turnId: "turn_queue_b",
- status: "finished",
- },
- });
- await vi.waitFor(() =>
- expect(
- app.transcript
- .snapshot()
- .filter((cell) => cell.content === "Queued answer"),
- ).toHaveLength(1),
- );
- await vi.waitFor(() =>
- expect(app.transcript.get("turn-duration:turn_queue_b")).toMatchObject({
- status: "succeeded",
- durationMs: 3_000,
- }),
- );
- const settledUi = app.tui.render(80).join("\n");
- expect(settledUi).toContain("Completed in 3s");
- expect(settledUi.indexOf("Completed in 3s")).toBeGreaterThan(
- settledUi.indexOf("Queued answer"),
- );
- expect(settledUi).not.toContain("message waiting");
- expect(settledUi).not.toContain("Next · after current response");
- await app.stop();
- });
- it("records submitted drafts in editor history", async () => {
- const terminal = new FakeTerminal();
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal,
- version: "0.1.0",
- workspaceDir: "/workspace",
- });
- await app.ready;
- app.editor.setText("Remember this draft");
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledTimes(1),
- );
- app.editor.handleInput("\u001b[A");
- expect(app.editor.getText()).toBe("Remember this draft");
- });
- it("continues sending and switching sessions when draft cleanup fails", async () => {
- const dataDir = await mkdtemp(join(tmpdir(), "mcode-app-draft-warning-"));
- const runtime = createRuntime();
- const app = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- dataDir,
- workspaceDir: "/workspace",
- });
- const flush = vi.spyOn(TuiDraftRecovery.prototype, "flush");
- try {
- await app.ready;
- flush.mockRejectedValue(
- new TuiDraftRecoveryError("cleanup", new Error("deletion denied")),
- );
- app.editor.setText("hello");
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(
- app.transcript.snapshot().filter((cell) => cell.kind === "warning"),
- ).toHaveLength(1),
- );
- const warnings = app.transcript
- .snapshot()
- .filter((cell) => cell.kind === "warning");
- expect(warnings[0]?.content).toContain(
- composerText("draftCleanupNextStep"),
- );
- expect(warnings[0]?.content).not.toContain(
- composerText("draftRecoveryUnavailable"),
- );
- expect(warnings[0]?.content).toContain("deletion denied");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledOnce(),
- );
- await vi.waitFor(() =>
- expect(app.controller.snapshot().activeTurnId).toBeUndefined(),
- );
- await expect(app.openSession("session-2")).resolves.toBeUndefined();
- expect(app.controller.snapshot().session?.sessionId).toBe("session-2");
- app.editor.setText("after switching");
- app.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledTimes(2),
- );
- await expect(app.stop()).resolves.toBeUndefined();
- } finally {
- await app.stop();
- flush.mockRestore();
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it("does not read or restore a persisted Draft during TUI startup", async () => {
- const dataDir = await mkdtemp(join(tmpdir(), "mcode-app-draft-"));
- try {
- const first = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- dataDir,
- workspaceDir: "/workspace/recovery",
- });
- await first.ready;
- first.editor.setText("/exit");
- await first.stop();
- const loadDraft = vi.spyOn(TuiDraftRecovery.prototype, "load");
- const restored = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- dataDir,
- workspaceDir: "/workspace/recovery",
- });
- await restored.ready;
- expect(loadDraft).not.toHaveBeenCalled();
- expect(restored.editor.getText()).toBe("");
- expect(restored.tui.render(80).join("\n")).not.toContain(
- "Draft restored",
- );
- loadDraft.mockRestore();
- restored.editor.setText("");
- await restored.stop();
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it("does not resume a persisted Draft during startup when login recovery is enabled", async () => {
- const dataDir = await mkdtemp(join(tmpdir(), "mcode-login-restart-draft-"));
- try {
- const first = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- dataDir,
- workspaceDir: "/workspace/login-recovery",
- });
- await first.ready;
- first.editor.setText("send after Global login");
- await first.stop();
- const restored = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- dataDir,
- workspaceDir: "/workspace/login-recovery",
- resumeDraftAfterLogin: true,
- });
- await restored.ready;
- restored.start();
- expect(restored.editor.getText()).toBe("");
- expect(restored.getSurface()).toBe("welcome");
- expect(restored.tui.render(80).join("\n")).toContain(
- "Tips for getting started",
- );
- await restored.stop();
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it("keeps the submitted Draft recoverable when shutdown wins before Runtime admission", async () => {
- const dataDir = await mkdtemp(join(tmpdir(), "mcode-app-pending-draft-"));
- try {
- const runtime = createRuntime();
- vi.spyOn(runtime, "sendMessage").mockImplementation(
- async function* sendMessage(
- _req: SendMessageReq,
- signal?: AbortSignal,
- ) {
- yield* [];
- await new Promise<void>((_resolve, reject) => {
- signal?.addEventListener(
- "abort",
- () => reject(new Error("admission aborted")),
- {
- once: true,
- },
- );
- });
- throw new Error("unreachable");
- },
- );
- const first = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- dataDir,
- workspaceDir: "/workspace/pending-recovery",
- });
- await first.ready;
- first.editor.setText("recover me until Runtime accepts");
- await new Promise((resolve) => setTimeout(resolve, 300));
- first.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledOnce(),
- );
- await first.stop();
- const restored = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- dataDir,
- workspaceDir: "/workspace/pending-recovery",
- });
- await restored.ready;
- await restored.openSession("session-1");
- expect(restored.editor.getText()).toBe(
- "recover me until Runtime accepts",
- );
- const rendered = restored.tui.render(80).join("\n");
- expect(rendered).toContain("recover me until Runtime accepts");
- expect(rendered).not.toContain("Message kept under Failed");
- await restored.stop();
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it("does not restore a submitted Draft after Runtime accepts the turn", async () => {
- const dataDir = await mkdtemp(join(tmpdir(), "mcode-app-accepted-draft-"));
- try {
- const runtime = createRuntime();
- vi.mocked(runtime.sendMessage).mockImplementation(
- async function* sendMessage(
- _req: SendMessageReq,
- signal?: AbortSignal,
- ) {
- await new Promise<void>((resolve) => {
- signal?.addEventListener("abort", resolve, { once: true });
- });
- yield { type: "done" };
- },
- );
- const first = createTuiApp({
- runtime,
- terminal: new FakeTerminal(),
- version: "0.1.0",
- dataDir,
- workspaceDir: "/workspace/accepted-recovery",
- });
- await first.ready;
- first.editor.setText("already accepted by Runtime");
- await new Promise((resolve) => setTimeout(resolve, 300));
- first.editor.handleInput("\r");
- await vi.waitFor(() =>
- expect(runtime.sendMessage).toHaveBeenCalledOnce(),
- );
- await first.stop();
- const restored = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- dataDir,
- workspaceDir: "/workspace/accepted-recovery",
- });
- await restored.ready;
- expect(restored.editor.getText()).toBe("");
- await restored.stop();
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it("serializes rapid Session switches without mixing their Drafts", async () => {
- const dataDir = await mkdtemp(join(tmpdir(), "mcode-app-draft-"));
- try {
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- dataDir,
- workspaceDir: "/workspace/recovery",
- });
- await app.ready;
- await app.openSession("session-a");
- app.editor.setText("Draft for Session A");
- await app.openSession("session-b");
- app.editor.setText("Draft for Session B");
- await Promise.all([
- app.openSession("session-a"),
- app.openSession("session-b"),
- ]);
- expect(app.controller.snapshot().session?.sessionId).toBe("session-b");
- expect(app.editor.getText()).toBe("Draft for Session B");
- await app.openSession("session-a");
- expect(app.editor.getText()).toBe("Draft for Session A");
- await app.openSession("session-b");
- expect(app.editor.getText()).toBe("Draft for Session B");
- await app.stop();
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it("moves to the new-session Draft namespace after archiving the current Session", async () => {
- const dataDir = await mkdtemp(join(tmpdir(), "mcode-app-draft-"));
- try {
- const app = createTuiApp({
- runtime: createRuntime(),
- terminal: new FakeTerminal(),
- version: "0.1.0",
- dataDir,
- workspaceDir: "/workspace/recovery",
- });
- await app.ready;
- await app.openSession("session-a");
- app.editor.setText("Archived Session Draft");
- await app.submit("/archive");
- app.editor.setText("Fresh Session Draft");
- await app.openSession("session-a");
- expect(app.editor.getText()).toBe("Archived Session Draft");
- await app.submit("/new");
- expect(app.editor.getText()).toBe("Fresh Session Draft");
- await app.stop();
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- });
|