| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974 |
- import assert from "node:assert/strict";
- import { spawn } from "node:child_process";
- import { once } from "node:events";
- import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
- import { homedir, tmpdir } from "node:os";
- import { PassThrough } from "node:stream";
- import { join } from "node:path";
- import { setTimeout as delay } from "node:timers/promises";
- import test from "node:test";
- import { fileURLToPath } from "node:url";
- import { ZCODE_OFFICIAL_PLUGIN_MARKETPLACE, ZCODE_PLUGIN_HOST_COMMAND } from "@zcode/contracts";
- import {
- ZCODE_CUA_OFFICIAL_PLUGIN_ID,
- ZCODE_CUA_NODE_REPL_HOST_ENV_KEY,
- ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY,
- ZCODE_PLUGIN_ID_ENV_KEY,
- ZCODE_RUNTIME_ENV_KEY,
- readZCodeToolEnvPassthroughEnv,
- resetCapturedZCodeCuaBrokerCredentialsForTest,
- sanitizeZCodeRuntimeEnvInPlace,
- type ModelSelection,
- type ZCodeModelOption,
- } from "@zcode/shared";
- import type { RunContext } from "@zcode/shared-types";
- import type { TuiOptions } from "@zcode/tui";
- import type { ZCodeAppOptions } from "@zcode/bootstrap";
- import { run } from "../src/run.js";
- import { createTuiSubmitPrompt } from "../src/tui-prompt-handler.js";
- import {
- buildManualSkillPrompt,
- createCommandCenter,
- parseSlashCommand,
- } from "../src/command-center.js";
- import type { CommandCenterTarget } from "../src/command-center.js";
- import { expandCliCustomCommandPrompt } from "../src/custom-command-expand.js";
- import { findDotenv, loadCliDotenv, prepareCliRuntimeEnv } from "../src/env.js";
- import { runPluginsCommand } from "../src/plugins-command.js";
- import { runPluginHostCommand } from "../src/plugin-host-command.js";
- import { CLI_PROCESS_NAME, setCliProcessTitle } from "../src/process-name.js";
- import { loadCliPlaywrightChromium } from "../src/sea-playwright-runtime.js";
- import { interceptKnownRuntimeWarnings } from "../src/runtime-warnings.js";
- import { installStderrConsoleBoundary } from "../src/protocol-console.js";
- import { interceptTuiStderr, isTuiInvocation } from "../src/tui-stderr.js";
- import { resolveWorkspaceGitBranch } from "../src/tui-workspace-git.js";
- import { createWorkspacePathSuggestionProvider } from "../src/tui-workspace-paths.js";
- import type { RunDependencies } from "../src/run.js";
- import { scheduleCliExitWatchdog, type CliShutdownProcess } from "../src/shutdown.js";
- type CapturedWriteStream = NodeJS.WriteStream & {
- output: () => string;
- };
- const createWriteStream = (isTTY = false): CapturedWriteStream => {
- let output = "";
- return {
- isTTY,
- output: () => output,
- write: (
- chunk: string | Uint8Array,
- encodingOrCallback?: BufferEncoding | ((err?: Error | null) => void),
- callback?: (err?: Error | null) => void,
- ): boolean => {
- output += typeof chunk === "string" ? chunk : chunk.toString();
- const writeCallback =
- typeof encodingOrCallback === "function" ? encodingOrCallback : callback;
- writeCallback?.();
- return true;
- },
- } as CapturedWriteStream;
- };
- const createContext = (
- argv: string[],
- ): RunContext & {
- stderr: CapturedWriteStream;
- stdout: CapturedWriteStream;
- } => ({
- argv,
- stderr: createWriteStream(),
- stdin: {
- isTTY: false,
- } as NodeJS.ReadStream,
- stdout: createWriteStream(),
- });
- const withTimeout = async <T>(promise: Promise<T>, timeoutMs: number, message: string) => {
- let timeout: NodeJS.Timeout | undefined;
- try {
- return await Promise.race([
- promise,
- new Promise<never>((_resolve, reject) => {
- timeout = setTimeout(() => reject(new Error(message)), timeoutMs);
- timeout.unref?.();
- }),
- ]);
- } finally {
- if (timeout) clearTimeout(timeout);
- }
- };
- const fakeProjection = {
- contextUsed: 0,
- contextWindow: 0,
- status: "idle",
- totalTokenCount: 0,
- turnCount: 1,
- };
- /** 协议 Model Option 测试夹具;TUI 登录态由 App 模型目录中是否存在可选模型推导。 */
- const createTestModelOption = (
- providerId: string,
- modelId: string,
- {
- defaultLevel = "medium",
- label = modelId,
- levels = ["low", "medium", "high"],
- }: { defaultLevel?: string; label?: string; levels?: readonly string[] } = {},
- ): ZCodeModelOption => ({
- ref: { providerId, modelId },
- label,
- providerLabel: providerId,
- reasoning: {
- levels: levels.map((value) => ({ value, label: value })),
- defaultLevel,
- },
- properties: {
- inputFormat: {
- supportsText: true,
- supportsImage: false,
- supportsVideo: false,
- supportsAudio: false,
- supportsPdf: false,
- },
- outputFormat: { supportsText: true },
- },
- });
- /** 至少一个未禁用的模型,表示 Provider/账号已可用,普通 prompt 不会被登录提示拦截。 */
- const selectableTestModels = (): ZCodeModelOption[] => [
- createTestModelOption("openai", "gpt-test"),
- ];
- /**
- * 真实 runTui 先绘制启动屏,再调用 loadStartupOptions 并把结果合并进 TUI 选项;
- * 测试替身按同一顺序模拟启动,断言 TUI 最终拿到的初始模型、主题、命令等数据。
- */
- const loadTuiStartup = async (options: TuiOptions): Promise<TuiOptions> => ({
- ...options,
- ...(await options.loadStartupOptions?.()),
- });
- const createPromptDeps = (): RunDependencies => ({
- createModelAdapter: () => ({}) as never,
- createZCodeApp: () =>
- ({
- getModel: () => "openai/gpt-test",
- getLocale: () => "en-US",
- getThoughtLevel: () => "medium",
- listModels: selectableTestModels,
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [{} as never, {} as never],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- }) as never,
- loadDotenv: () => ({
- keys: [],
- loaded: false,
- }),
- startProcessProviderRegistryRuntime: async () =>
- ({
- dispose: () => {},
- runtime: { registryService: {} },
- }) as never,
- skipUserConfig: true,
- });
- test("prepares a sanitized CLI runtime env with ZCode-owned runtime keys", () => {
- const sourceEnv = {
- NODE_ENV: "development",
- HTTP_PROXY: "http://ambient-proxy:8080",
- NODE_EXTRA_CA_CERTS: "/tmp/ambient-ca.pem",
- ZCODE_HTTP_PROXY: "http://zcode-proxy:8080",
- no_proxy: ".example.test",
- npm_config_proxy: "http://npm-proxy:8080",
- };
- const env = prepareCliRuntimeEnv(sourceEnv, ["/usr/bin/node", "/workspace/bin/zcode.js"]);
- const toolEnv = readZCodeToolEnvPassthroughEnv(env);
- assert.equal(env.ZCODE_RUNTIME_ENV, "production");
- assert.equal(env.ZCODE_HTTP_PROXY, "http://zcode-proxy:8080");
- assert.deepEqual(toolEnv, {
- HTTP_PROXY: "http://ambient-proxy:8080",
- NODE_EXTRA_CA_CERTS: "/tmp/ambient-ca.pem",
- no_proxy: ".example.test",
- npm_config_proxy: "http://npm-proxy:8080",
- });
- assert.equal(env.NODE_ENV, undefined);
- assert.equal(env.HTTP_PROXY, undefined);
- assert.equal(env.NODE_EXTRA_CA_CERTS, undefined);
- assert.equal(env.no_proxy, undefined);
- assert.equal(env.npm_config_proxy, undefined);
- assert.equal(sourceEnv.NODE_ENV, "development");
- });
- test("prepares beta CLI runtime env with beta storage root", () => {
- const env = prepareCliRuntimeEnv(
- {
- ZCODE_BETA: "1",
- },
- ["/usr/bin/node", "/workspace/bin/zcode.js"],
- );
- assert.equal(env.ZCODE_STORAGE_DIR, join(homedir(), ".zcode-beta"));
- });
- test("preserves explicit storage root when beta mode is enabled", () => {
- const env = prepareCliRuntimeEnv(
- {
- ZCODE_BETA: "1",
- ZCODE_STORAGE_DIR: "/tmp/custom-zcode-home",
- },
- ["/usr/bin/node", "/workspace/bin/zcode.js"],
- );
- assert.equal(env.ZCODE_STORAGE_DIR, "/tmp/custom-zcode-home");
- });
- test("opens the TUI by default", async () => {
- const ctx = createContext([]);
- let opened = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- runTui: async (options) => {
- opened = true;
- assert.equal(options.stdin, ctx.stdin);
- // 启动屏先于运行时绘制:模型、主题、命令等启动数据改由 loadStartupOptions 在首帧后提供。
- const startup = await loadTuiStartup(options);
- assert.equal(startup.initialModel, "openai/gpt-test");
- assert.equal(startup.initialThoughtLevel, "medium");
- assert.equal(startup.theme, "auto");
- assert.equal(typeof options.listMcpServers, "function");
- assert.equal(typeof options.listWorkspacePathSuggestions, "function");
- assert.equal(typeof options.submitPrompt, "function");
- assert.equal(typeof options.writeClipboardText, "function");
- assert.ok(startup.slashCommands?.some((command) => command.name === "model"));
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(opened, true);
- assert.equal(ctx.stdout.output(), "");
- assert.equal(ctx.stderr.output(), "");
- });
- test("ignores ambient NODE_ENV when deciding TUI developer mode", async () => {
- const ctx = createContext([]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- env: {
- NODE_ENV: "development",
- },
- runTui: async (options) => {
- assert.equal(options.developerMode, false);
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- });
- test("passes developer mode, version, and workspace git branch into the TUI", async () => {
- const ctx = createContext([]);
- const workspaceDirectory = "/workspace/dev";
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- cwd: () => workspaceDirectory,
- env: {
- ZCODE_RUNTIME_ENV: "development",
- },
- resolveWorkspaceGitBranch: async (options) => {
- assert.equal(options.workspaceDirectory, workspaceDirectory);
- return "feature/sidebar";
- },
- runTui: async (options) => {
- assert.equal(options.developerMode, true);
- assert.equal(options.version, "0.0.0");
- assert.equal(options.workspaceDirectory, workspaceDirectory);
- // Git 分支解析不阻塞首帧,随 loadStartupOptions 一起返回。
- const startup = await loadTuiStartup(options);
- assert.equal(startup.workspaceGitBranch, "feature/sidebar");
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- });
- test("resolves workspace git branch from a non-shell git command", async () => {
- const branch = await resolveWorkspaceGitBranch({
- workspaceDirectory: "/workspace/project",
- runCommand: async (file, args) => {
- assert.equal(file, "git");
- assert.deepEqual(args, [
- "-C",
- "/workspace/project",
- "symbolic-ref",
- "--quiet",
- "--short",
- "HEAD",
- ]);
- return { exitCode: 0, stdout: "main\n" };
- },
- });
- assert.equal(branch, "main");
- });
- test("ignores missing workspace git branch", async () => {
- assert.equal(
- await resolveWorkspaceGitBranch({
- workspaceDirectory: "/workspace/project",
- runCommand: async () => ({ exitCode: 1, stdout: "" }),
- }),
- undefined,
- );
- assert.equal(
- await resolveWorkspaceGitBranch({
- workspaceDirectory: "/workspace/project",
- runCommand: async () => ({ exitCode: 0, stdout: "HEAD\n" }),
- }),
- undefined,
- );
- });
- test("passes configured TUI theme into the TUI", async () => {
- const ctx = createContext([]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () =>
- ({
- getLocale: () => "en-US",
- getModel: () => "openai/gpt-test",
- getTheme: () => "light",
- getThoughtLevel: () => "medium",
- runtime: {} as never,
- sessionId: "session-test",
- submitPrompt: async () => {
- throw new Error("submitPrompt should not be called");
- },
- traceId: "trace-test",
- }) as never,
- runTui: async (options) => {
- const startup = await loadTuiStartup(options);
- assert.equal(startup.theme, "light");
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- });
- test("suggests workspace paths for TUI file mentions", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-tui-paths-"));
- try {
- await mkdir(join(dir, "public"));
- await mkdir(join(dir, "node_modules"));
- await writeFile(join(dir, "public", "app.ts"), "export {}", "utf8");
- await writeFile(join(dir, "puzzle.md"), "# puzzle", "utf8");
- await writeFile(join(dir, ".hidden.md"), "# hidden", "utf8");
- const suggest = createWorkspacePathSuggestionProvider({ workspaceDirectory: dir });
- assert.deepEqual(await suggest({ token: "pu" }), {
- items: [
- { kind: "directory", path: "public/" },
- { kind: "file", path: "puzzle.md" },
- ],
- truncated: false,
- });
- assert.deepEqual(await suggest({ token: "public/" }), {
- items: [{ kind: "file", path: "public/app.ts" }],
- truncated: false,
- });
- assert.deepEqual(await suggest({ token: "../" }), {
- items: [],
- truncated: false,
- });
- assert.equal(
- (await suggest({ token: "" })).items.some((item) => item.path === ".hidden.md"),
- false,
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- test("enables session title generation for TUI sessions", async () => {
- const ctx = createContext(["tui"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.deepEqual(options?.runtimeConfig?.titleGeneration, {});
- assert.equal(options?.runtimeConfig?.memory?.extractionEnabled, undefined);
- return {
- getLocale: () => "en-US",
- getModel: () => "openai/gpt-test",
- getThoughtLevel: () => "medium",
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- runTui: async () => 0,
- });
- assert.equal(exitCode, 0);
- });
- test("passes --disallowedTools into TUI runtime config", async () => {
- const ctx = createContext(["tui", "--disallowedTools", "Bash", "Edit,web_search"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.deepEqual(options?.runtimeConfig?.toolDisallowlist, ["Bash", "Edit", "WebSearch"]);
- return {
- getLocale: () => "en-US",
- getModel: () => "openai/gpt-test",
- getThoughtLevel: () => "medium",
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- runTui: async () => 0,
- });
- assert.equal(exitCode, 0);
- });
- test("passes force MCS into TUI runtime config", async () => {
- const ctx = createContext(["tui", "--force-mcs"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.deepEqual((options?.runtimeConfig as any)?.midConversationSystem, {
- mode: "force",
- });
- return {
- getLocale: () => "en-US",
- getModel: () => "openai/gpt-test",
- getThoughtLevel: () => "medium",
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- runTui: async () => 0,
- });
- assert.equal(exitCode, 0);
- });
- test("does not enable session title generation for headless prompts", async () => {
- const ctx = createContext(["--prompt", "hello"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.equal(options?.runtimeConfig?.titleGeneration, undefined);
- assert.equal(options?.standaloneSessionControl, true);
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- close: async () => {},
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello\n");
- });
- test("disables automatic Memory Extraction for headless prompts", async () => {
- const ctx = createContext(["-p", "hello Memory"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.deepEqual(options?.runtimeConfig?.memory, {
- extractionEnabled: false,
- });
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- close: async () => {},
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello Memory\n");
- });
- test("injects process provider endpoint routing into headless prompts", async () => {
- const ctx = createContext(["--prompt", "route me"]);
- const providerEndpointRoutingPort = {
- async resolve(url: string) {
- return { routed: false, url };
- },
- };
- let routingFactoryCalls = 0;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createProviderEndpointRoutingPort: (options) => {
- routingFactoryCalls += 1;
- assert.equal(options.workingDirectory, "/tmp/zcode-routing-headless");
- return providerEndpointRoutingPort;
- },
- createZCodeApp: (options) => {
- assert.equal(options?.providerEndpointRoutingPort, providerEndpointRoutingPort);
- return {
- close: async () => {},
- runtime: {} as never,
- sessionId: "session-routing-headless",
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-routing-headless" as never,
- turnId: "turn-routing-headless" as never,
- }),
- traceId: "trace-routing-headless",
- } as never;
- },
- cwd: () => "/tmp/zcode-routing-headless",
- });
- assert.equal(exitCode, 0);
- assert.equal(routingFactoryCalls, 1);
- assert.equal(ctx.stdout.output(), "route me\n");
- });
- test("borrows one process provider registry for a headless prompt and disposes it", async () => {
- const ctx = createContext(["--prompt", "registry me"]);
- const providerRegistry = {} as never;
- const providerRuntimeHeadersPort = {} as never;
- let runtimeStarts = 0;
- let runtimeDisposals = 0;
- let selectionRepositoryDisposals = 0;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- skipUserConfig: false,
- userConfigPath: "/tmp/zcode-legacy-cli.json",
- startProcessProviderRegistryRuntime: async (_env, options) => {
- runtimeStarts += 1;
- // 刷新回调是当前 CLI 装配的一部分,仍验证唯一 Runtime 和旧配置迁移路径。
- assert.equal(typeof options?.standalone?.onBuiltinRefreshError, "function");
- assert.equal(typeof options?.standalone?.onBuiltinRefreshResult, "function");
- assert.deepEqual(
- {
- standalone: {
- legacyCliUserConfigFilePath: options?.standalone?.legacyCliUserConfigFilePath,
- },
- },
- {
- standalone: {
- legacyCliUserConfigFilePath: "/tmp/zcode-legacy-cli.json",
- },
- },
- );
- return {
- accountSource: {} as never,
- dispose: () => {
- selectionRepositoryDisposals += 1;
- runtimeDisposals += 1;
- },
- configuredDefaultModelSelection: { providerId: "provider-a", modelId: "model-a" },
- modelSelectionConfigRepository: {
- dispose: () => {
- selectionRepositoryDisposals += 1;
- },
- read: async () => ({ providerId: "provider-a", modelId: "model-a" }),
- },
- providerRuntimeHeadersPort,
- snapshot: {} as never,
- runtime: {
- dispose: () => {
- runtimeDisposals += 1;
- },
- registryService: providerRegistry,
- },
- } as never;
- },
- createZCodeApp: (options) => {
- assert.equal(options?.providerRegistry, providerRegistry);
- assert.equal(options?.providerRuntimeHeadersPort, providerRuntimeHeadersPort);
- assert.deepEqual(options?.configuredDefaultModelSelection, {
- providerId: "provider-a",
- modelId: "model-a",
- });
- return {
- close: async () => {},
- runtime: {} as never,
- sessionId: "session-registry-headless",
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-registry-headless" as never,
- turnId: "turn-registry-headless" as never,
- }),
- traceId: "trace-registry-headless",
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(runtimeStarts, 1);
- assert.equal(runtimeDisposals, 1);
- assert.equal(selectionRepositoryDisposals, 1);
- });
- test("shows a non-blocking login notice when no coding plan key is configured", async () => {
- const ctx = createContext([]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- // 登录态改由 Registry 的可选模型推导:未配置 Coding Plan 时模型目录只有被禁用的选项。
- createZCodeApp: () =>
- ({
- getLocale: () => "en-US",
- getModel: () => undefined,
- listModels: () => [
- {
- ...createTestModelOption("zai", "glm-5.1"),
- disabledReason: "Sign in to Z.AI Coding Plan first.",
- },
- ],
- runtime: {} as never,
- sessionId: "session-test",
- submitPrompt: async () => {
- throw new Error("submitPrompt should not be called");
- },
- traceId: "trace-test",
- }) as never,
- runTui: async (options) => {
- const startup = await loadTuiStartup(options);
- assert.equal(startup.loginRequired, true);
- assert.equal(startup.initialResult, undefined);
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- });
- test("honors configured locale before app creation when login is required", async () => {
- const tempRoot = await mkdtemp(join(tmpdir(), "zcode-cli-prelogin-locale-"));
- const configPath = join(tempRoot, "config.json");
- await writeFile(configPath, JSON.stringify({ ui: { locale: "zh-CN" } }));
- try {
- const ctx = createContext([]);
- let appCreated = false;
- const exitCode = await run(ctx, {
- cwd: () => tempRoot,
- env: {},
- // 登录态改由 App 的可选模型推导;App 在启动屏首帧之后才创建,这里模拟没有可选模型。
- createZCodeApp: () => {
- appCreated = true;
- return {
- listModels: () => [],
- runtime: {} as never,
- sessionId: "session-prelogin",
- submitPrompt: async () => {
- throw new Error("submitPrompt should not be called");
- },
- traceId: "trace-prelogin",
- } as never;
- },
- listCustomCommands: async () =>
- ({
- commands: [],
- diagnostics: [],
- totalDiscovered: 0,
- }) as never,
- loadDotenv: () => ({
- keys: [],
- loaded: false,
- }),
- startProcessProviderRegistryRuntime: async () =>
- ({
- dispose: () => {},
- runtime: { registryService: {} },
- }) as never,
- runTui: async (options) => {
- // 启动屏在 App 创建前就按用户配置的语言渲染。
- assert.equal(options.locale, "zh-CN");
- assert.equal(appCreated, false);
- const startup = await loadTuiStartup(options);
- assert.equal(startup.loginRequired, true);
- assert.equal(startup.locale, "zh-CN");
- const pickerResult = await options.submitPrompt("/login", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(pickerResult.response, "选择 Coding Plan 提供商的配置方式。");
- assert.equal(pickerResult.selection?.title, "配置 Coding Plan");
- assert.equal(pickerResult.selection?.prompt, "选择登录或 API key 配置方式。");
- return 0;
- },
- userConfigPath: configPath,
- });
- assert.equal(exitCode, 0);
- } finally {
- await rm(tempRoot, { force: true, recursive: true });
- }
- });
- test("passes detected auto locale into the app and TUI", async () => {
- const ctx = createContext(["--locale", "auto"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.equal(options?.uiLocale, "auto");
- assert.equal(options?.uiDetectedLocale, "zh-CN");
- return {
- getLocale: () => "zh-CN",
- getModel: () => "openai/gpt-test",
- getThoughtLevel: () => "medium",
- runtime: {} as never,
- sessionId: "session-test",
- submitPrompt: async () => {
- throw new Error("submitPrompt should not be called");
- },
- traceId: "trace-test",
- } as never;
- },
- env: {
- LANG: "zh_CN.UTF-8",
- },
- runTui: async (options) => {
- assert.equal(options.locale, "zh-CN");
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- });
- test("TUI locale command updates later app creation locale", async () => {
- const ctx = createContext(["--locale", "en-US"]);
- const createdLocales: Array<unknown> = [];
- let activeLocale: "en-US" | "zh-CN" = "en-US";
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- createdLocales.push(options?.uiLocale);
- return {
- getLocale: () => activeLocale,
- getModel: () => "openai/gpt-test",
- getThoughtLevel: () => "medium",
- runtime: {} as never,
- sessionId: `session-${createdLocales.length}`,
- setLocale: async (locale: "auto" | "en-US" | "zh-CN") => {
- activeLocale = locale === "zh-CN" ? "zh-CN" : "en-US";
- return {
- configPath: "/tmp/zcode/config.json",
- locale: activeLocale,
- previousLocale: "en-US",
- requestedLocale: locale,
- traceId: "trace-test" as never,
- };
- },
- submitPrompt: async () => {
- throw new Error("submitPrompt should not be called");
- },
- traceId: `trace-${createdLocales.length}`,
- } as never;
- },
- runTui: async (options) => {
- // 真实 TUI 首帧后即创建初始 App;/locale 由该 App 持久化,不会落到默认用户配置文件。
- await loadTuiStartup(options);
- const switched = await options.submitPrompt("/locale zh-CN", {
- abortSignal: new AbortController().signal,
- });
- const created = await options.submitPrompt("/new", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(switched.locale, "zh-CN");
- assert.equal(created.locale, "zh-CN");
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.deepEqual(createdLocales, ["en-US", "zh-CN"]);
- });
- test("prints localized CLI help with explicit locale", async () => {
- const ctx = createContext(["--help", "--locale", "zh-CN"]);
- const exitCode = await run(ctx, createPromptDeps());
- assert.equal(exitCode, 0);
- assert.match(ctx.stdout.output(), /用法:/);
- assert.match(ctx.stdout.output(), /--locale <locale>/);
- });
- test("prints localized CLI help with auto locale detection", async () => {
- const ctx = createContext(["--help", "--locale", "auto"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- env: {
- LC_ALL: "zh_CN.UTF-8",
- },
- });
- assert.equal(exitCode, 0);
- assert.match(ctx.stdout.output(), /用法:/);
- });
- test("passes custom command suggestions into the TUI", async () => {
- const ctx = createContext([]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- listCustomCommands: async () =>
- ({
- commands: [
- {
- allowedTools: [],
- argumentHint: "[scope]",
- description: "Review custom changes",
- disableNonInteractive: false,
- frontmatterKeys: [],
- name: "review",
- path: "/workspace/.zcode/commands/review.md",
- rootPath: "/workspace/.zcode/commands",
- scope: "project",
- source: "zcode",
- },
- ],
- diagnostics: [],
- totalDiscovered: 1,
- }) as never,
- runTui: async (options) => {
- // 自定义命令扫描与首帧并行,随 loadStartupOptions 返回。
- const startup = await loadTuiStartup(options);
- assert.ok(startup.slashCommands?.some((command) => command.name === "review"));
- assert.ok(startup.slashCommands?.some((command) => command.usage === "/review [scope]"));
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- });
- test("command center expands custom commands before submitting to runtime", async () => {
- let submittedPrompt = "";
- const commandCenter = createCommandCenter({
- getApp: async () =>
- ({
- getModel: () => "openai/gpt-test",
- getThoughtLevel: () => "medium",
- sessionId: "session-custom",
- traceId: "trace-custom",
- submitPrompt: async (prompt: string) => {
- submittedPrompt = prompt;
- return {
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-custom" as never,
- turnId: "turn-custom" as never,
- };
- },
- }) as never,
- getMode: () => "build",
- loadCustomCommand: async () =>
- ({
- content: "Review $1 and $ARGUMENTS.",
- metadata: {
- description: "Review custom changes",
- name: "review",
- path: "/workspace/.zcode/commands/review.md",
- scope: "project",
- source: "zcode",
- },
- }) as never,
- resumeApp: async () => {
- throw new Error("resume should not be called");
- },
- });
- const result = await commandCenter("/review auth high", {});
- assert.match(result.response, /Run custom command \/review/);
- assert.match(submittedPrompt, /Review auth and auth high/);
- });
- test("zcode commands list prints custom commands", async () => {
- const ctx = createContext(["commands"]);
- const logger = {} as NonNullable<RunDependencies["logger"]>;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- cwd: () => "/workspace/project",
- logger,
- listCustomCommands: async (options) => {
- assert.equal(options.logger, logger);
- assert.equal(options.workingDirectory, "/workspace/project");
- return {
- commands: [
- {
- allowedTools: [],
- argumentHint: "[scope]",
- description: "Review custom changes",
- disableNonInteractive: false,
- frontmatterKeys: [],
- name: "review",
- path: "/workspace/project/.zcode/commands/review.md",
- rootPath: "/workspace/project/.zcode/commands",
- scope: "project",
- source: "zcode",
- },
- ],
- diagnostics: [],
- totalDiscovered: 1,
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.match(ctx.stdout.output(), /Custom commands \(1\)/);
- assert.match(ctx.stdout.output(), /\/review \[scope\]/);
- });
- test("zcode login runs OAuth flow and prints the authorization URL", async () => {
- const ctx = createContext(["login", "--no-browser"]);
- let sawNoBrowser = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- loginZCodeCli: async (options) => {
- sawNoBrowser = options?.noBrowser === true;
- await options?.onAuthorizeUrl?.({
- authorize_url: "https://chat.z.ai/oauth/authorize?state=state",
- expires_at: 1735000000,
- flow_id: "flow-1",
- poll_interval_sec: 2,
- poll_token: "poll-token",
- });
- return {
- configPath: "/home/test/.zcode/v2/model-selection.json",
- credentialsPath: "/home/test/.zcode/v2/credentials.json",
- model: "zai/glm-5.1",
- providerId: "zai",
- user: {
- user_id: "u_1",
- email: "alice@example.com",
- name: "Alice",
- },
- };
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(sawNoBrowser, true);
- assert.match(ctx.stdout.output(), /Open this URL to sign in/);
- assert.match(ctx.stdout.output(), /https:\/\/chat\.z\.ai\/oauth\/authorize/);
- assert.match(ctx.stdout.output(), /Login successful as Alice/);
- assert.match(
- ctx.stdout.output(),
- /Model selection: \/home\/test\/\.zcode\/v2\/model-selection\.json/,
- );
- });
- test("zcode logout clears shared OAuth credentials", async () => {
- const ctx = createContext(["logout"]);
- let logoutCalled = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- logoutZCodeCli: async () => {
- logoutCalled = true;
- return {
- credentialsPath: "/home/test/.zcode/v2/credentials.json",
- };
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(logoutCalled, true);
- assert.match(ctx.stdout.output(), /Logged out from Coding Plan accounts/);
- });
- test("parses login and logout slash commands", () => {
- assert.deepEqual(parseSlashCommand("/login"), {
- args: "",
- name: "login",
- rawName: "login",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/login zai-coding-plan"), {
- args: "zai-coding-plan",
- name: "login",
- rawName: "login",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/logout"), {
- args: "",
- name: "logout",
- rawName: "logout",
- type: "known",
- });
- });
- test("command center handles login and logout locally", async () => {
- let loginCalled = false;
- let loginAbortSignal: AbortSignal | undefined;
- let apiKeyCalled = false;
- let logoutCalled = false;
- const emittedEvents: Array<{
- payload?: unknown;
- sessionId?: unknown;
- traceId?: unknown;
- type?: string;
- }> = [];
- const commandCenter = createCommandCenter({
- // 授权链接消息归属当前活动会话(TUI 首帧后已创建 App),但登录流程本身不向模型提交 prompt。
- getApp: async () =>
- ({
- sessionId: "session-login",
- traceId: "trace-login",
- submitPrompt: async () => {
- throw new Error("login must not submit a model prompt");
- },
- }) as never,
- login: async (options) => {
- loginCalled = true;
- loginAbortSignal = options?.abortSignal;
- await options?.onAuthorizeUrl?.({
- authorize_url: "https://chat.z.ai/oauth/authorize?state=state",
- expires_at: 1735000000,
- flow_id: "flow-1",
- poll_interval_sec: 2,
- });
- return {
- configPath: "/home/test/.zcode/v2/model-selection.json",
- credentialsPath: "/home/test/.zcode/v2/credentials.json",
- model: "zai/glm-5.1",
- providerId: "zai",
- user: {
- user_id: "u_1",
- email: "alice@example.com",
- name: "Alice",
- },
- };
- },
- configureApiKey: async (options) => {
- apiKeyCalled = options.providerId === "bigmodel" && options.apiKey === "manual-key";
- return {
- configPath: "/home/test/.zcode/v2/model-selection.json",
- model: "bigmodel/glm-5.1",
- providerId: "bigmodel",
- };
- },
- logout: async () => {
- logoutCalled = true;
- return {
- credentialsPath: "/home/test/.zcode/v2/credentials.json",
- };
- },
- resumeApp: async () => {
- throw new Error("resume should not be called");
- },
- });
- const pickerResult = await commandCenter("/login", {});
- const loginAbort = new AbortController();
- const loginResult = await commandCenter("/login zai-coding-plan", {
- abortSignal: loginAbort.signal,
- onEvent: async (event) => {
- emittedEvents.push(event);
- },
- } as never);
- const apiKeyResult = await commandCenter("/login bigmodel-coding-plan-api-key manual-key", {});
- const logoutResult = await commandCenter("/logout", {});
- assert.equal(pickerResult.selection?.items.length, 4);
- assert.equal(pickerResult.selection?.filterable, false);
- assert.equal(
- pickerResult.selection?.items[0]?.pending?.primary,
- "Waiting for Z.AI authorization",
- );
- assert.equal(pickerResult.selection?.items[2]?.input?.primary, "Enter Z.AI Coding Plan API Key");
- assert.equal(pickerResult.selection?.items[2]?.input?.mask, true);
- assert.equal(loginAbortSignal, loginAbort.signal);
- assert.equal(loginCalled, true);
- assert.equal(apiKeyCalled, true);
- assert.equal(logoutCalled, true);
- assert.equal(emittedEvents[0]?.type, "assistant_message");
- assert.equal(emittedEvents[0]?.sessionId, "session-login");
- assert.equal(emittedEvents[0]?.traceId, "trace-login");
- assert.match(
- String((emittedEvents[0]?.payload as { content?: string } | undefined)?.content),
- /https:\/\/chat\.z\.ai\/oauth\/authorize/,
- );
- assert.match(loginResult.response, /Configured Z\.AI Coding Plan as Alice/);
- assert.match(
- loginResult.response,
- /Model selection: \/home\/test\/\.zcode\/v2\/model-selection\.json/,
- );
- assert.match(apiKeyResult.response, /Configured BigModel Coding Plan/);
- assert.match(logoutResult.response, /Logged out from Coding Plan accounts/);
- });
- test("command center localizes the Coding Plan setup picker", async () => {
- const commandCenter = createCommandCenter({
- getApp: async () => {
- throw new Error("app should not be created");
- },
- getLocale: () => "zh-CN",
- resumeApp: async () => {
- throw new Error("resume should not be called");
- },
- });
- const result = await commandCenter("/login", {});
- assert.equal(result.response, "选择 Coding Plan 提供商的配置方式。");
- assert.equal(result.selection?.title, "配置 Coding Plan");
- assert.equal(result.selection?.prompt, "选择登录或 API key 配置方式。");
- assert.equal(result.selection?.filterable, false);
- assert.match(result.selection?.items[0]?.secondary ?? "", /打开浏览器登录/);
- assert.equal(result.selection?.items[2]?.input?.primary, "输入 Z.AI Coding Plan API Key");
- assert.equal(result.selection?.items[2]?.input?.placeholder, "粘贴 API key");
- });
- test("command center keeps local commands usable before login", async () => {
- let appCreated = false;
- const commandCenter = createCommandCenter({
- getApp: async () => {
- appCreated = true;
- throw new Error("app should not be created before login");
- },
- getLocale: () => "en-US",
- // 登录判定改为“是否存在可选模型”,由注入的检查器回答,命令中心自身不创建 App。
- hasSelectableModels: async () => false,
- resumeApp: async () => {
- throw new Error("resume should not be called");
- },
- setLocale: async (locale) => ({
- locale: locale === "zh-CN" ? "zh-CN" : "en-US",
- requestedLocale: locale,
- }),
- });
- const promptResult = await commandCenter("hello model", {});
- const localeResult = await commandCenter("/locale zh-CN", {});
- assert.equal(promptResult.loginRequired, true);
- assert.match(promptResult.response, /No available models/);
- assert.equal(localeResult.locale, "zh-CN");
- assert.match(localeResult.response, /Locale switched to zh-CN/);
- assert.equal(appCreated, false);
- const zhCommandCenter = createCommandCenter({
- getApp: async () => {
- throw new Error("app should not be created before login");
- },
- getLocale: () => "zh-CN",
- hasSelectableModels: async () => false,
- });
- const zhPromptResult = await zhCommandCenter("hello model", {});
- assert.equal(zhPromptResult.loginRequired, true);
- assert.match(zhPromptResult.response, /没有可用模型/);
- });
- const reviewCustomCommand = {
- content: "Review $ARGUMENTS.",
- metadata: {
- description: "Review custom changes",
- name: "review",
- path: "/workspace/.zcode/commands/review.md",
- scope: "project",
- source: "zcode",
- },
- } as never;
- test("headless prompt submits a resolvable custom command as raw text", async () => {
- // Bug 回归钉:这条过去早退进 command-center(响应 "Run custom command /review …"),
- // 于是只挂在普通 prompt 路径上的 headless dwf 结算等待被整体跳过。现在它必须落到普通
- // prompt 路径,并把**原文**交给 app——展开是 facade 的 customCommandPromptResolver 干的。
- const ctx = createContext(["--prompt", "/review auth"]);
- const base = createPromptDeps();
- const loadedNames: string[] = [];
- let submitted: unknown;
- const exitCode = await run(ctx, {
- ...base,
- createZCodeApp: (options) => {
- const app = base.createZCodeApp?.(options) as {
- submitPrompt: (prompt: unknown, submitOptions?: unknown) => Promise<unknown>;
- };
- return {
- ...app,
- submitPrompt: async (prompt: unknown, submitOptions?: unknown) => {
- submitted = prompt;
- return await app.submitPrompt(prompt, submitOptions);
- },
- } as never;
- },
- loadCustomCommand: async (options) => {
- loadedNames.push(options.name);
- return reviewCustomCommand;
- },
- isReservedSlashCommandName: () => false,
- });
- assert.equal(exitCode, 0);
- // 原文,未展开:CLI 不再本地展开自定义命令。
- assert.equal(submitted, "/review auth");
- assert.deepEqual(loadedNames, ["review"]);
- // command-center 的包装文案彻底不在这条路径上了。
- assert.equal(ctx.stdout.output().includes("Run custom command"), false);
- });
- test("headless prompt keeps a facade-reserved custom command name on the command-center path", async () => {
- // `compress` 是唯一一个 CLI 解析成 unknown、而 facade 的 resolver 又因保留名拒绝展开的
- // 名字。若探测把它判成"可解析",它就会以字面文本 `/compress …` 被当普通 prompt 提交给
- // 模型——静默走错路。这条刻意**不注入**保留名判据,用真实 bootstrap 的那份清单,这样
- // 两侧是否真的同源就是可证的(清单长出新成员时这条会跟着变)。
- const ctx = createContext(["--prompt", "/compress tighten it"]);
- const base = createPromptDeps();
- let submitted: unknown;
- const exitCode = await run(ctx, {
- ...base,
- createZCodeApp: (options) => {
- const app = base.createZCodeApp?.(options) as {
- submitPrompt: (prompt: unknown, submitOptions?: unknown) => Promise<unknown>;
- };
- return {
- ...app,
- submitPrompt: async (prompt: unknown, submitOptions?: unknown) => {
- submitted = prompt;
- return await app.submitPrompt(prompt, submitOptions);
- },
- } as never;
- },
- loadCustomCommand: async () =>
- ({
- content: "Compress $ARGUMENTS.",
- metadata: {
- description: "Compress things",
- name: "compress",
- path: "/workspace/.zcode/commands/compress.md",
- scope: "project",
- source: "zcode",
- },
- }) as never,
- });
- assert.equal(exitCode, 0);
- // command-center 在本地展开了它(今天的行为,逐字保留)。
- assert.match(ctx.stdout.output(), /Run custom command \/compress\./);
- assert.match(ctx.stdout.output(), /Compress tighten it/);
- // 关键:绝不能把原文丢给模型当普通 prompt。
- assert.notEqual(submitted, "/compress tighten it");
- });
- test("TUI custom command expansion hands shell blocks to the model like the app path", () => {
- const { prompt } = expandCliCustomCommandPrompt({
- args: "go !`touch arg-marker`",
- command: {
- content: [
- "Run !`git status`, then report.",
- "```!",
- '"${CLAUDE_PLUGIN_ROOT}/s.sh" "$CLAUDE_PROJECT_DIR" $ARGUMENTS',
- "```",
- ].join("\n"),
- metadata: {
- name: "probe",
- plugin: { dataPath: "/data/p", id: "p@market", name: "p", rootPath: "/plugins/p" },
- scope: "user",
- source: "plugin",
- },
- },
- });
- assert.match(prompt, /the \/probe command file \(user\/plugin\), not from ZCode\./);
- // 代码块结束后原文另起一行;插件变量换成真实路径,工作区变量留给 Bash 环境;参数原样进入。
- assert.ok(prompt.includes("```bash\ngit status\n```\n, then report."));
- assert.ok(
- prompt.includes('```bash\n"/plugins/p/s.sh" "$CLAUDE_PROJECT_DIR" go !`touch arg-marker`\n```'),
- );
- });
- test("headless prompt keeps the Unknown command response for an unresolvable slash name", async () => {
- const ctx = createContext(["--prompt", "/nope please"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- listCustomCommands: async () =>
- ({
- commands: [reviewCustomCommand.metadata],
- diagnostics: [],
- totalDiscovered: 1,
- }) as never,
- loadCustomCommand: async () => {
- throw new Error("Custom command not found: nope");
- },
- isReservedSlashCommandName: () => false,
- });
- assert.equal(exitCode, 0);
- assert.match(ctx.stdout.output(), /Unknown command: \/nope\./);
- assert.match(ctx.stdout.output(), /Available commands: .*\/review/);
- });
- test("headless prompt propagates a custom command load failure that is not a miss", async () => {
- // 只有 /not found/i 算"不可解析"。读盘失败必须冒泡成进程错误,不能被一句
- // "Unknown command" 盖掉真正的原因。
- const ctx = createContext(["--prompt", "/review auth"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- loadCustomCommand: async () => {
- throw new Error("EACCES: permission denied, open '/workspace/.zcode/commands/review.md'");
- },
- isReservedSlashCommandName: () => false,
- });
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /EACCES: permission denied/);
- assert.equal(ctx.stdout.output().includes("Unknown command"), false);
- });
- test("wires TUI input recall skip into the active app", async () => {
- const ctx = createContext(["tui"]);
- const recalledSkips: Array<number | undefined> = [];
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () =>
- ({
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- recallPreviousInputHistory: async (skip?: number) => {
- recalledSkips.push(skip);
- return { text: skip === 1 ? "older project prompt" : "previous project prompt" };
- },
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- }) as never,
- runTui: async (options) => {
- const previous = await options.recallPreviousInput?.();
- const older = await options.recallPreviousInput?.(1);
- assert.equal(previous?.text, "previous project prompt");
- assert.equal(older?.text, "older project prompt");
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.deepEqual(recalledSkips, [undefined, 1]);
- });
- test("wires TUI slash command history recording into the active app", async () => {
- const ctx = createContext(["tui"]);
- const recorded: Array<{ input: unknown; kind: unknown }> = [];
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () =>
- ({
- getModel: () => "openai/gpt-main",
- listModels: () => [],
- recordInputHistory: async (input: unknown, kind?: unknown) => {
- recorded.push({ input, kind });
- return null;
- },
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- recallPreviousInputHistory: async () => null,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- }) as never,
- runTui: async (options) => {
- // 活动 App 在启动屏首帧后创建;此后的 slash 命令写入该 App 的输入历史。
- await loadTuiStartup(options);
- await options.submitPrompt("/help", {
- abortSignal: new AbortController().signal,
- });
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.deepEqual(recorded, [{ input: "/help", kind: "slash_command" }]);
- });
- test("TUI bare skill command lists skills when startup metadata is unavailable", async () => {
- const ctx = createContext([]);
- let appCreationAttempts = 0;
- let listed = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () => {
- appCreationAttempts += 1;
- throw new Error("app is unavailable");
- },
- cwd: () => "/workspace/project",
- listSkills: async (options) => {
- listed = true;
- assert.equal(options.workingDirectory, "/workspace/project");
- return {
- diagnostics: [],
- skills: [
- {
- description: "Use for demo tasks.",
- directory: "/workspace/project/.zcode/skills/demo",
- frontmatterKeys: ["name", "description"],
- name: "demo-skill",
- path: "/workspace/project/.zcode/skills/demo/SKILL.md",
- rootPath: "/workspace/project/.zcode/skills",
- safeToAutoLoad: true,
- scope: "project",
- source: "zcode",
- },
- ],
- totalDiscovered: 1,
- } as never;
- },
- runTui: async (options) => {
- // 启动元数据在首帧后加载;App 创建失败时降级为空元数据,TUI 仍可进入。
- const startup = await loadTuiStartup(options);
- assert.equal(startup.initialModel, undefined);
- const result = await options.submitPrompt("/skill", {
- abortSignal: new AbortController().signal,
- });
- assert.match(result.response, /Available skills \(1\)/);
- assert.match(result.response, /demo-skill \(project\/zcode\)/);
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(appCreationAttempts, 1);
- assert.equal(listed, true);
- assert.equal(ctx.stderr.output(), "");
- });
- test("command center handles goal commands locally", async () => {
- let submittedPrompt: string | undefined;
- let target: CommandCenterTarget | null = null;
- const commandCenter = createCommandCenter({
- getApp: async () =>
- ({
- sessionId: "session-target",
- traceId: "trace-target",
- readTarget: async () => target,
- setTarget: async ({ objective, status = "active" }) => {
- target = {
- objective,
- sessionID: "session-target",
- status,
- targetID: "target-1",
- time: { created: 1, updated: 1 },
- };
- return target;
- },
- updateTargetStatus: async (status) => {
- if (!target) return null;
- target = { ...target, status, time: { ...target.time, updated: 2 } };
- return target;
- },
- clearTarget: async () => {
- const hadTarget = target !== null;
- target = null;
- return hadTarget;
- },
- submitPrompt: async (prompt: string) => {
- submittedPrompt = prompt;
- return {
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-target" as never,
- turnId: "turn-target" as never,
- };
- },
- resume: async () => ({
- appliedMessageCount: 0,
- directory: "/tmp",
- interruptedToolCount: 0,
- messageCount: 0,
- partCount: 0,
- }),
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not be called");
- },
- });
- const set = await commandCenter("/goal Ship target MVP", {});
- const paused = await commandCenter("/goal pause", {});
- const summary = await commandCenter("/goal", {});
- const cleared = await commandCenter("/goal clear", {});
- assert.match(set.response, /Goal active/);
- assert.match(set.response, /Ship target MVP/);
- assert.match(paused.response, /Goal paused/);
- assert.match(summary.response, /Goal paused/);
- assert.match(cleared.response, /Goal cleared/);
- assert.equal(submittedPrompt, undefined);
- });
- test("command center starts target continuation after setting a target", async () => {
- let continued = false;
- const commandCenter = createCommandCenter({
- getApp: async () =>
- ({
- sessionId: "session-target-continue",
- traceId: "trace-target-continue",
- readTarget: async () => null,
- setTarget: async ({ objective, status = "active" }) => ({
- objective,
- sessionID: "session-target-continue",
- status,
- targetID: "target-continue",
- time: { created: 1, updated: 1 },
- }),
- updateTargetStatus: async () => null,
- clearTarget: async () => false,
- continueActiveTarget: async () => {
- continued = true;
- return {
- projection: fakeProjection as never,
- response: "continued target",
- traceId: "trace-target-continue" as never,
- turnId: "turn-target-continue" as never,
- };
- },
- resume: async () => ({
- appliedMessageCount: 0,
- directory: "/tmp",
- interruptedToolCount: 0,
- messageCount: 0,
- partCount: 0,
- }),
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not be called");
- },
- });
- const result = await commandCenter("/goal Ship target MVP", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(continued, true);
- assert.equal(result.response, "continued target");
- assert.equal(result.traceId, "trace-target-continue");
- });
- test("command center explains skipped goal continuation in plan mode", async () => {
- let continued = false;
- const commandCenter = createCommandCenter({
- getApp: async () =>
- ({
- sessionId: "session-target-plan",
- traceId: "trace-target-plan",
- readTarget: async () => null,
- setTarget: async ({ objective, status = "active" }) => ({
- objective,
- sessionID: "session-target-plan",
- status,
- targetID: "target-plan",
- time: { created: 1, updated: 1 },
- }),
- updateTargetStatus: async () => null,
- clearTarget: async () => false,
- continueActiveTarget: async () => {
- continued = true;
- return null;
- },
- resume: async () => ({
- appliedMessageCount: 0,
- directory: "/tmp",
- interruptedToolCount: 0,
- messageCount: 0,
- partCount: 0,
- }),
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- }) as never,
- getMode: () => "plan",
- resumeApp: async () => {
- throw new Error("resume should not be called");
- },
- });
- const result = await commandCenter("/goal Optimize performance", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(continued, true);
- assert.match(result.response, /Goal active/);
- assert.match(result.response, /Optimize performance/);
- assert.match(result.response, /Plan mode 下已记录 goal,但不会自动继续。/);
- });
- test("command center asks before replacing an existing goal", async () => {
- let setCalled = false;
- const existing: CommandCenterTarget = {
- objective: "Existing target",
- sessionID: "session-target-replace",
- status: "active",
- targetID: "target-existing",
- time: { created: 1, updated: 1 },
- };
- const commandCenter = createCommandCenter({
- getApp: async () =>
- ({
- sessionId: "session-target-replace",
- traceId: "trace-target-replace",
- readTarget: async () => existing,
- setTarget: async () => {
- setCalled = true;
- return existing;
- },
- updateTargetStatus: async () => existing,
- clearTarget: async () => true,
- resume: async () => ({
- appliedMessageCount: 0,
- directory: "/tmp",
- interruptedToolCount: 0,
- messageCount: 0,
- partCount: 0,
- }),
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not be called");
- },
- });
- const result = await commandCenter("/goal New target", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(setCalled, false);
- assert.equal(result.selection?.title, "Replace Goal");
- assert.equal(result.selection?.items[0]?.command, "/goal replace New target");
- });
- test("TUI startup migration failures return before entering fullscreen UI", async () => {
- const ctx = createContext(["tui"]);
- let mainUiMounted = false;
- const migrationError = new Error(
- "SQLite migration checksum mismatch for 0001_base_session_store",
- );
- migrationError.name = "SqliteSessionMigrationError";
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () => {
- throw migrationError;
- },
- // 新启动流程先绘制启动屏,再在 loadStartupOptions 中创建运行时;真实 runTui 在启动数据
- // 加载失败时销毁 renderer 并把错误抛回 CLI,因此主界面不会挂载,错误在终端恢复后输出。
- runTui: async (options) => {
- await options.loadStartupOptions?.();
- mainUiMounted = true;
- return 0;
- },
- });
- assert.equal(exitCode, 1);
- assert.equal(mainUiMounted, false);
- assert.match(ctx.stderr.output(), /SQLite migration checksum mismatch/);
- assert.equal(ctx.stdout.output(), "");
- });
- test("detects TUI invocations before loading the runtime", () => {
- assert.equal(isTuiInvocation([]), true);
- assert.equal(isTuiInvocation(["tui"]), true);
- assert.equal(isTuiInvocation(["--mode", "build"]), true);
- assert.equal(isTuiInvocation(["--resume", "session-test", "tui"]), true);
- assert.equal(isTuiInvocation(["acp"]), false);
- assert.equal(isTuiInvocation(["--help"]), false);
- assert.equal(isTuiInvocation(["tui", "--help"]), false);
- assert.equal(isTuiInvocation(["--prompt", "hello"]), false);
- assert.equal(isTuiInvocation(["missing"]), false);
- assert.equal(isTuiInvocation(["--wat"]), false);
- });
- test("buffers TUI stderr and drops it on clean restore", () => {
- const stderr = createWriteStream();
- const interceptor = interceptTuiStderr(stderr);
- stderr.write("runtime warning\n");
- assert.equal(stderr.output(), "");
- assert.equal(interceptor.bufferedOutput, "runtime warning\n");
- interceptor.restore();
- assert.equal(stderr.output(), "");
- });
- test("flushes buffered TUI stderr when startup fails", async () => {
- const stderr = createWriteStream();
- const interceptor = interceptTuiStderr(stderr);
- let callbackCalled = false;
- await new Promise<void>((resolve) => {
- stderr.write(Buffer.from("startup failed\n"), () => {
- callbackCalled = true;
- resolve();
- });
- });
- assert.equal(callbackCalled, true);
- assert.equal(stderr.output(), "");
- interceptor.restore({ flush: true });
- assert.equal(stderr.output(), "startup failed\n");
- });
- test("lets controlled TUI errors bypass raw stderr interception", () => {
- const stderr = createWriteStream();
- const interceptor = interceptTuiStderr(stderr);
- interceptor.passthrough.write("TUI requires an interactive terminal.\n");
- stderr.write("runtime warning\n");
- assert.equal(stderr.output(), "TUI requires an interactive terminal.\n");
- assert.equal(interceptor.bufferedOutput, "runtime warning\n");
- interceptor.restore();
- });
- test("filters the known SQLite experimental warning from stderr", () => {
- const stderr = createWriteStream();
- const interceptor = interceptKnownRuntimeWarnings(stderr);
- stderr.write(
- "(node:59658) ExperimentalWarning: SQLite is an experimental feature and might change at any time\n",
- );
- stderr.write(
- "(Use `zcode-darwin-arm64 --trace-warnings ...` to show where the warning was created)\n",
- );
- stderr.write("real error\n");
- assert.equal(stderr.output(), "real error\n");
- assert.equal(interceptor.suppressedCount, 2);
- interceptor.restore();
- });
- test("filters the known module.register deprecation warning from stderr", () => {
- const stderr = createWriteStream();
- const interceptor = interceptKnownRuntimeWarnings(stderr);
- stderr.write(
- "(node:69362) [DEP0205] DeprecationWarning: `module.register()` is deprecated. Use `module.registerHooks()` instead.\n",
- );
- stderr.write("(Use `node --trace-deprecation ...` to show where the warning was created)\n");
- stderr.write("help text stderr\n");
- assert.equal(stderr.output(), "help text stderr\n");
- assert.equal(interceptor.suppressedCount, 2);
- interceptor.restore();
- });
- test("filters the known insecure Node TLS environment warning from stderr", () => {
- const stderr = createWriteStream();
- const interceptor = interceptKnownRuntimeWarnings(stderr);
- stderr.write(
- "(node:65726) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.\n",
- );
- stderr.write("provider error remains visible\n");
- assert.equal(stderr.output(), "provider error remains visible\n");
- assert.equal(interceptor.suppressedCount, 1);
- interceptor.restore();
- });
- test("filters the known AI SDK Anthropic thinking budget warnings from stderr", () => {
- const stderr = createWriteStream();
- const interceptor = interceptKnownRuntimeWarnings(stderr);
- stderr.write(
- "AI SDK Warning System: To turn off warning logging, set the AI_SDK_LOG_WARNINGS global to false.\n",
- );
- stderr.write(
- 'AI SDK Warning (anthropic.messages / glm-5.1): The feature "extended thinking" is used in a compatibility mode. thinking budget is required when thinking is enabled. using default budget of 1024 tokens.\n',
- );
- stderr.write("AI SDK Warning (openai.responses / gpt-test): unrelated warning\n");
- stderr.write("runtime error remains visible\n");
- assert.equal(
- stderr.output(),
- "AI SDK Warning (openai.responses / gpt-test): unrelated warning\nruntime error remains visible\n",
- );
- assert.equal(interceptor.suppressedCount, 2);
- interceptor.restore();
- });
- test("keeps unrelated stderr while filtering runtime warnings", () => {
- const stderr = createWriteStream();
- const interceptor = interceptKnownRuntimeWarnings(stderr);
- stderr.write("(node:59658) ExperimentalWarning: Something else happened\n");
- stderr.write(
- "(node:59658) ExperimentalWarning: SQLite is an experimental feature and might change at any time\n",
- );
- stderr.write("still important\n");
- assert.equal(
- stderr.output(),
- "(node:59658) ExperimentalWarning: Something else happened\nstill important\n",
- );
- assert.equal(interceptor.suppressedCount, 1);
- interceptor.restore();
- });
- test("wires TUI approval requests into the runtime permission broker", async () => {
- const ctx = createContext(["tui"]);
- let approvalSeen = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- return {
- listModels: selectableTestModels,
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async () => {
- const permission = await options?.permissionBroker?.requestPermission(
- createPermissionRequest(),
- {
- signal: new AbortController().signal,
- },
- );
- return {
- events: [],
- projection: fakeProjection as never,
- response: `permission ${permission?.decision}`,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- };
- },
- } as never;
- },
- runTui: async (options) => {
- // TUI 挂载时登记一次审批入口;提交不再携带单次回调。
- const unregister = options.registerPermissionRequester!(async (request) => {
- approvalSeen = true;
- assert.equal(request.toolName, "Bash");
- return {
- decision: "allow",
- reason: "approved by tui test",
- };
- });
- const result = await options.submitPrompt("needs approval", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(result.response, "permission allow");
- unregister();
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(approvalSeen, true);
- });
- test("wires TUI sendInput approval requests into the runtime permission broker", async () => {
- const ctx = createContext(["tui"]);
- let approvalSeen = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- sendInput: async () => {
- const permission = await options?.permissionBroker?.requestPermission(
- createPermissionRequest(),
- {
- signal: new AbortController().signal,
- },
- );
- return {
- kind: "started_turn",
- result: {
- events: [],
- projection: fakeProjection as never,
- response: `permission ${permission?.decision}`,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- },
- };
- },
- submitPrompt: async () => {
- throw new Error("sendInput should handle this prompt");
- },
- } as never;
- },
- runTui: async (options) => {
- const unregister = options.registerPermissionRequester!(async (request) => {
- approvalSeen = true;
- assert.equal(request.toolName, "Bash");
- return {
- decision: "allow",
- reason: "approved by tui sendInput test",
- };
- });
- const result = await options.sendInput!("needs approval while busy", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(result.kind, "started_turn");
- assert.equal(result.result.response, "permission allow");
- unregister();
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(approvalSeen, true);
- });
- // 回归:审批入口原先只在单次提交内有效,回合之外的询问(后台会话、通知驱动的回合)被直接拒绝。
- test("routes approvals raised outside any submit to the mounted TUI and fails closed after unmount", async () => {
- const ctx = createContext(["tui"]);
- let broker: NonNullable<ZCodeAppOptions["permissionBroker"]> | undefined;
- const seen: string[] = [];
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- broker = options?.permissionBroker;
- return {
- listModels: selectableTestModels,
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async () => ({
- events: [],
- projection: fakeProjection as never,
- response: "created",
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- runTui: async (options) => {
- const background = { ...(createPermissionRequest() as object), sessionId: "sess_background" };
- const first = options.registerPermissionRequester!(async (request) => {
- seen.push(`first:${request.sessionId}`);
- return { decision: "deny", reason: "first" };
- });
- // 首次提交建出 App;提交结束后再询问,模拟后台会话在发起者回合之外请求审批。
- await options.submitPrompt("create background work", {
- abortSignal: new AbortController().signal,
- });
- const outside = await broker!.requestPermission(background as never);
- assert.equal(outside.decision, "deny");
- assert.equal(outside.reason, "first");
- // 重新挂载:新入口先登记,旧入口的迟到注销不能把它清掉。
- const second = options.registerPermissionRequester!(async (request) => {
- seen.push(`second:${request.sessionId}`);
- return { decision: "allow", reason: "second" };
- });
- first();
- assert.equal((await broker!.requestPermission(background as never)).decision, "allow");
- second();
- const closed = await broker!.requestPermission(background as never);
- assert.equal(closed.decision, "deny");
- assert.match(closed.reason ?? "", /No interactive approval handler/);
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.deepEqual(seen, ["first:sess_background", "second:sess_background"]);
- });
- test("wires TUI mode display and switching into runtime config", async () => {
- const ctx = createContext(["--mode", "plan", "tui"]);
- let appMode: "plan" | "build" | "edit" | "yolo" | "auto" = "build";
- const runtimeModeUpdates: string[] = [];
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- // 启动屏首帧后按 CLI --mode 创建 App;此后的模式切换都作用于活动 App。
- assert.equal(options?.runtimeConfig?.mode, "plan");
- appMode = options?.runtimeConfig?.mode ?? "build";
- return {
- getMode: () => appMode,
- listModels: selectableTestModels,
- sessionId: "session-test",
- setMode: async (mode: typeof appMode) => {
- runtimeModeUpdates.push(mode);
- const previousMode = appMode;
- appMode = mode;
- return {
- mode: appMode,
- previousMode,
- traceId: "trace-test",
- };
- },
- traceId: "trace-test",
- runtime: {
- updateConfig: (patch: { mode?: typeof appMode }) => {
- if (patch.mode) {
- runtimeModeUpdates.push(patch.mode);
- appMode = patch.mode;
- }
- },
- } as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- runTui: async (options) => {
- const startup = await loadTuiStartup(options);
- assert.equal(startup.initialMode, "plan");
- const firstSwitch = await options.submitPrompt("/mode yolo", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(firstSwitch.mode, "yolo");
- const promptResult = await options.submitPrompt("hello mode", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptResult.mode, "yolo");
- const liveSwitch = await options.submitPrompt("/mode build", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(liveSwitch.mode, "build");
- const shortcutSwitch = await options.setMode!("plan");
- assert.equal(shortcutSwitch.mode, "plan");
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.deepEqual(runtimeModeUpdates, ["yolo", "build", "plan"]);
- });
- test("wires TUI edit mode switching into runtime config", async () => {
- const ctx = createContext(["--mode", "build", "tui"]);
- let appMode: "plan" | "build" | "edit" | "yolo" | "auto" = "build";
- const runtimeModeUpdates: string[] = [];
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- // 启动屏首帧后按 CLI --mode 创建 App;edit 切换经活动 App 的 runtime config 生效。
- assert.equal(options?.runtimeConfig?.mode, "build");
- appMode = options?.runtimeConfig?.mode ?? "build";
- return {
- getMode: () => appMode,
- listModels: selectableTestModels,
- sessionId: "session-edit-test",
- traceId: "trace-edit-test",
- runtime: {
- updateConfig: (patch: { mode?: typeof appMode }) => {
- if (patch.mode) {
- runtimeModeUpdates.push(patch.mode);
- appMode = patch.mode;
- }
- },
- } as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-edit-test" as never,
- turnId: "turn-edit-test" as never,
- }),
- } as never;
- },
- runTui: async (options) => {
- const startup = await loadTuiStartup(options);
- assert.equal(startup.initialMode, "build");
- const liveSwitch = await options.submitPrompt("/mode edit", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(liveSwitch.mode, "edit");
- const promptResult = await options.submitPrompt("hello edit mode", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptResult.mode, "edit");
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.deepEqual(runtimeModeUpdates, ["edit"]);
- });
- test("wires TUI model command into app server state", async () => {
- const ctx = createContext(["tui"]);
- // 模型目录改为协议 Model Option(ref + 推理档位),/model 切换提交完整的结构化选择。
- const modelOptions = [
- createTestModelOption("openai", "gpt-main", {
- defaultLevel: "high",
- label: "Main",
- levels: ["low", "high"],
- }),
- createTestModelOption("openai", "gpt-lite", {
- defaultLevel: "low",
- label: "Lite",
- levels: ["low", "high"],
- }),
- ];
- let current: ModelSelection = {
- providerId: "openai",
- modelId: "gpt-main",
- options: { reasoningLevel: "high" },
- };
- const currentModelId = () => `${current.providerId}/${current.modelId}`;
- const requestedSelections: unknown[] = [];
- const savedDefaults: ModelSelection[] = [];
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- startProcessProviderRegistryRuntime: async () =>
- ({
- dispose: () => {},
- modelSelectionConfigRepository: {
- dispose: () => {},
- read: async () => undefined,
- saveConfiguredDefault: async (selection: ModelSelection) => {
- savedDefaults.push(selection);
- },
- },
- runtime: { registryService: {} },
- }) as never,
- createZCodeApp: () => {
- return {
- getCurrentModelOption: () =>
- modelOptions.find(
- ({ ref }) => ref.providerId === current.providerId && ref.modelId === current.modelId,
- ),
- getModel: currentModelId,
- getThoughtLevel: () => current.options?.reasoningLevel,
- listModels: () => modelOptions,
- sessionId: "session-test",
- setModel: async (selection: ModelSelection) => {
- requestedSelections.push(selection);
- const previousModel = currentModelId();
- current = selection;
- return {
- model: currentModelId(),
- previousModel,
- thoughtLevel: selection.options?.reasoningLevel,
- traceId: "trace-test",
- };
- },
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async () => ({
- events: [],
- projection: fakeProjection as never,
- response: `using ${currentModelId()}`,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- runTui: async (options) => {
- const startup = await loadTuiStartup(options);
- assert.equal(startup.initialModel, "openai/gpt-main");
- assert.equal(startup.initialThoughtLevel, "high");
- assert.deepEqual(startup.modelOptions, modelOptions);
- const listed = await options.submitPrompt("/model", {
- abortSignal: new AbortController().signal,
- });
- assert.match(listed.response, /Current model: openai\/gpt-main/);
- assert.equal(listed.model, "openai/gpt-main");
- assert.equal(listed.thoughtLevel, "high");
- const switched = await options.submitPrompt("/model openai/gpt-lite", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(switched.response, "Model switched to openai/gpt-lite (low).");
- assert.equal(switched.model, "openai/gpt-lite");
- assert.equal(switched.thoughtLevel, "low");
- const promptResult = await options.submitPrompt("hello model", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptResult.response, "using openai/gpt-lite");
- assert.equal(promptResult.model, "openai/gpt-lite");
- assert.equal(promptResult.thoughtLevel, "low");
- return 0;
- },
- });
- const expectedSelection = {
- providerId: "openai",
- modelId: "gpt-lite",
- options: { reasoningLevel: "low" },
- };
- assert.equal(exitCode, 0);
- assert.deepEqual(requestedSelections, [expectedSelection]);
- // 用户主动切换成功后,完整选择同步保存为新会话默认值。
- assert.deepEqual(savedDefaults, [expectedSelection]);
- });
- test("wires TUI effort command into app server state", async () => {
- const ctx = createContext(["tui"]);
- let thoughtLevel = "medium";
- const requestedLevels: string[] = [];
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () => {
- return {
- getModel: () => "openai/gpt-main",
- getThoughtLevel: () => thoughtLevel,
- listModels: () => [createTestModelOption("openai", "gpt-main", { label: "Main" })],
- listThoughtLevels: () => ["low", "medium", "high"],
- sessionId: "session-test",
- setThoughtLevel: async (level: string) => {
- requestedLevels.push(level);
- const previousThoughtLevel = thoughtLevel;
- thoughtLevel = level;
- return {
- previousThoughtLevel,
- thoughtLevel,
- traceId: "trace-test",
- };
- },
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async () => ({
- events: [],
- projection: fakeProjection as never,
- response: `effort ${thoughtLevel}`,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- runTui: async (options) => {
- const startup = await loadTuiStartup(options);
- assert.equal(startup.initialThoughtLevel, "medium");
- assert.deepEqual(startup.effortOptions, [
- { id: "low", label: "low" },
- { id: "medium", label: "medium" },
- { id: "high", label: "high" },
- ]);
- const listed = await options.submitPrompt("/effort", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(listed.selection, undefined);
- assert.match(listed.response, /Current reasoning effort: medium/);
- assert.match(listed.response, /- high/);
- assert.equal(listed.thoughtLevel, "medium");
- const switched = await options.submitPrompt("/variant high", {
- abortSignal: new AbortController().signal,
- });
- assert.match(switched.response, /Reasoning effort switched to high/);
- assert.deepEqual(switched.effortOptions, startup.effortOptions);
- assert.equal(switched.thoughtLevel, "high");
- const promptResult = await options.submitPrompt("hello effort", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptResult.response, "effort high");
- assert.equal(promptResult.thoughtLevel, "high");
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.deepEqual(requestedLevels, ["high"]);
- });
- test("wires TUI fork command into a new active app", async () => {
- const ctx = createContext(["tui"]);
- const createdSessionIds: Array<string | undefined> = [];
- let forkTarget: string | undefined;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- const appSessionId = options?.sessionId ?? "sess_parent";
- createdSessionIds.push(options?.sessionId);
- return {
- forkFromCheckpoint: async (forkOptions?: { targetCheckpointId?: string }) => {
- forkTarget = forkOptions?.targetCheckpointId;
- return {
- checkpoint: {} as never,
- copiedMessageCount: 1,
- forkedSessionId: "sess_child",
- restoredFiles: [],
- response: "Forked session sess_child",
- };
- },
- listModels: selectableTestModels,
- sessionId: appSessionId,
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: `${appSessionId}:${prompt}`,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- runTui: async (options) => {
- const forked = await options.submitPrompt("/fork checkpoint_1", {
- abortSignal: new AbortController().signal,
- });
- assert.match(forked.response, /Switched to forked session sess_child/);
- const promptResult = await options.submitPrompt("hello fork", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptResult.response, "sess_child:hello fork");
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(forkTarget, "checkpoint_1");
- assert.deepEqual(createdSessionIds, [undefined, "sess_child"]);
- });
- test("wires TUI new command into a fresh active app", async () => {
- const ctx = createContext(["tui"]);
- const createdSessionIds: Array<string | undefined> = [];
- const closedSessionIds: string[] = [];
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.equal(options?.standaloneSessionControl, true);
- const appSessionId = options?.sessionId ?? `sess_new_${createdSessionIds.length}`;
- createdSessionIds.push(options?.sessionId);
- return {
- listModels: selectableTestModels,
- sessionId: appSessionId,
- traceId: `trace_${appSessionId}`,
- runtime: {} as never,
- close: async () => {
- closedSessionIds.push(appSessionId);
- },
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: `${appSessionId}:${prompt}`,
- traceId: `trace_${appSessionId}` as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- runTui: async (options) => {
- const initial = await options.submitPrompt("hello first", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(initial.response, "sess_new_0:hello first");
- const created = await options.submitPrompt("/new", {
- abortSignal: new AbortController().signal,
- });
- assert.match(created.response, /Started new session sess_new_1/);
- assert.equal(created.sessionId, "sess_new_1");
- assert.equal(created.resetSessionProjection, true);
- const promptResult = await options.submitPrompt("hello new", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptResult.response, "sess_new_1:hello new");
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.deepEqual(createdSessionIds, [undefined, undefined]);
- assert.deepEqual(closedSessionIds, ["sess_new_0", "sess_new_1"]);
- });
- test("keeps shared telemetry alive across TUI session replacement and shuts it down once", async () => {
- const ctx = createContext(["tui"]);
- let telemetryPrepareCount = 0;
- let telemetryShutdownCount = 0;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- prepareZCodeTelemetryEnv: async (env) => {
- telemetryPrepareCount += 1;
- return env;
- },
- shutdownZCodeTelemetry: async () => {
- telemetryShutdownCount += 1;
- },
- runTui: async (options) => {
- await options.submitPrompt("hello first", {
- abortSignal: new AbortController().signal,
- });
- await options.submitPrompt("/new", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(telemetryShutdownCount, 0);
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(telemetryPrepareCount, 2);
- assert.equal(telemetryShutdownCount, 1);
- });
- test("reuses provider endpoint routing when TUI replaces its active session", async () => {
- const ctx = createContext(["tui"]);
- const providerEndpointRoutingPort = {
- async resolve(url: string) {
- return { routed: false, url };
- },
- };
- let routingFactoryCalls = 0;
- let createdApps = 0;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createProviderEndpointRoutingPort: () => {
- routingFactoryCalls += 1;
- return providerEndpointRoutingPort;
- },
- createZCodeApp: (options) => {
- assert.equal(options?.providerEndpointRoutingPort, providerEndpointRoutingPort);
- createdApps += 1;
- return {
- close: async () => {},
- runtime: {} as never,
- sessionId: `session-routing-tui-${createdApps}`,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: `trace-routing-tui-${createdApps}` as never,
- turnId: "turn-routing-tui" as never,
- }),
- traceId: `trace-routing-tui-${createdApps}`,
- } as never;
- },
- runTui: async (options) => {
- await options.submitPrompt("first", {
- abortSignal: new AbortController().signal,
- });
- await options.submitPrompt("/new", {
- abortSignal: new AbortController().signal,
- });
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(createdApps, 2);
- assert.equal(routingFactoryCalls, 1);
- });
- test("closes the active TUI app on process shutdown signals", async () => {
- const ctx = createContext(["tui"]);
- const shutdownProcess = createTestShutdownProcess("darwin");
- const exitDeferred = createDeferred<number>();
- let closeCount = 0;
- let telemetryShutdownCount = 0;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- prepareZCodeTelemetryEnv: async (env) => env,
- shutdownZCodeTelemetry: async () => {
- telemetryShutdownCount += 1;
- },
- createZCodeApp: () =>
- ({
- close: async () => {
- closeCount += 1;
- },
- getModel: () => "openai/gpt-test",
- getThoughtLevel: () => "medium",
- runtime: {} as never,
- sessionId: "session-test",
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- traceId: "trace-test",
- }) as never,
- exitProcess: (code) => {
- exitDeferred.resolve(code);
- },
- runTui: async (options) => {
- await options.submitPrompt("hello", {
- abortSignal: new AbortController().signal,
- });
- shutdownProcess.emitSignal("SIGHUP");
- assert.equal(await exitDeferred.promise, 129);
- return 0;
- },
- shutdownProcess,
- });
- assert.equal(exitCode, 0);
- assert.equal(closeCount, 1);
- assert.equal(telemetryShutdownCount, 1);
- });
- test("parses built-in slash commands outside the TUI", () => {
- assert.deepEqual(parseSlashCommand("hello"), null);
- assert.deepEqual(parseSlashCommand("/compact keep files"), {
- args: "keep files",
- name: "compact",
- rawName: "compact",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/expert migrate auth"), {
- args: "migrate auth",
- name: "expert",
- rawName: "expert",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/fork latest"), {
- args: "latest",
- name: "fork",
- rawName: "fork",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/help model"), {
- args: "model",
- name: "help",
- rawName: "help",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/init include scripts"), {
- args: "include scripts",
- name: "init",
- rawName: "init",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/workflow review the auth module"), {
- args: "review the auth module",
- name: "workflow",
- rawName: "workflow",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/continue"), {
- args: "",
- name: "resume",
- rawName: "continue",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/clear"), {
- args: "",
- name: "new",
- rawName: "clear",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/mode yolo"), {
- args: "yolo",
- name: "mode",
- rawName: "mode",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/locale zh-CN"), {
- args: "zh-CN",
- name: "locale",
- rawName: "locale",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/language auto"), {
- args: "auto",
- name: "locale",
- rawName: "language",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/mcp list"), {
- args: "list",
- name: "mcp",
- rawName: "mcp",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/plugins enable ios-simulator"), {
- args: "enable ios-simulator",
- name: "plugins",
- rawName: "plugins",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/plugin"), {
- args: "",
- name: "plugins",
- rawName: "plugin",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/model lite"), {
- args: "lite",
- name: "model",
- rawName: "model",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/effort high"), {
- args: "high",
- name: "effort",
- rawName: "effort",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/variant max"), {
- args: "max",
- name: "effort",
- rawName: "variant",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/new"), {
- args: "",
- name: "new",
- rawName: "new",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/rewind latest"), {
- args: "latest",
- name: "rewind",
- rawName: "rewind",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/skill"), {
- args: "",
- name: "skill",
- rawName: "skill",
- skillName: "",
- task: "",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/skill demo-skill update docs"), {
- args: "demo-skill update docs",
- name: "skill",
- rawName: "skill",
- skillName: "demo-skill",
- task: "update docs",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/goal Ship target MVP"), {
- args: "Ship target MVP",
- name: "goal",
- rawName: "goal",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/target Ship target MVP"), {
- args: "Ship target MVP",
- name: "goal",
- rawName: "target",
- type: "known",
- });
- assert.deepEqual(parseSlashCommand("/wat"), {
- args: "",
- rawName: "wat",
- type: "unknown",
- });
- });
- test("command center returns slash help without creating an app", async () => {
- const submitPrompt = createCommandCenter({
- getApp: async () => {
- throw new Error("app should not be created");
- },
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const allCommands = await submitPrompt("/help", {
- abortSignal: new AbortController().signal,
- });
- const clearCommand = await submitPrompt("/help /clear", {
- abortSignal: new AbortController().signal,
- });
- const effortCommand = await submitPrompt("/help /variant", {
- abortSignal: new AbortController().signal,
- });
- const modelCommand = await submitPrompt("/help /model", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(allCommands.mode, "build");
- assert.match(allCommands.response, /Slash commands:/);
- assert.match(allCommands.response, /\/help \[command\]/);
- assert.match(allCommands.response, /\/compact \[instructions\]/);
- assert.match(allCommands.response, /\/init \[notes\]/);
- assert.match(allCommands.response, /\/workflow \[what the workflow should accomplish\]/);
- assert.match(allCommands.response, /\/expert \[status\|resume\|stop\|<task>\]/);
- assert.match(clearCommand.response, /^\/new$/m);
- assert.match(clearCommand.response, /Aliases: \/clear/);
- assert.match(clearCommand.response, /Start a fresh session in the TUI\./);
- assert.match(effortCommand.response, /^\/effort \[list\|<level>\]/);
- assert.match(effortCommand.response, /Aliases: \/variant/);
- assert.match(effortCommand.response, /reasoning effort/);
- // /model main|lite 别名已随结构化模型选择移除,只接受 provider/model。
- assert.match(modelCommand.response, /^\/model \[list\|provider\/model\]/);
- assert.match(modelCommand.response, /Show or switch the current session model\./);
- });
- test("command center records accepted slash commands through the history port", async () => {
- const recorded: Array<{ input: unknown; kind: unknown }> = [];
- const submitPrompt = createCommandCenter({
- getApp: async () => {
- throw new Error("app should not be created");
- },
- recordInputHistory: async (input, kind) => {
- recorded.push({ input, kind });
- },
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- await submitPrompt("/help /model", {
- abortSignal: new AbortController().signal,
- });
- await submitPrompt("/login zai-coding-plan-api-key secret-token", {
- abortSignal: new AbortController().signal,
- });
- assert.deepEqual(recorded, [{ input: "/help /model", kind: "slash_command" }]);
- });
- test("command center passes attachments through for normal prompts", async () => {
- let submitted: unknown;
- const promptInput = {
- text: "describe [image #1]",
- attachments: [
- {
- content: "data:image/png;base64,aW1hZ2U=",
- path: "[image #1]",
- type: "image" as const,
- },
- ],
- };
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async (prompt: unknown) => {
- submitted = prompt;
- return {
- response: "done",
- };
- },
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt(promptInput, {
- abortSignal: new AbortController().signal,
- });
- assert.equal(result.response, "done");
- assert.deepEqual(submitted, promptInput);
- });
- test("command center rejects attachments on slash commands", async () => {
- const submitPrompt = createCommandCenter({
- getApp: async () => {
- throw new Error("app should not be created");
- },
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt(
- {
- text: "/help [image #1]",
- attachments: [
- {
- content: "data:image/png;base64,aW1hZ2U=",
- path: "[image #1]",
- type: "image",
- },
- ],
- },
- {
- abortSignal: new AbortController().signal,
- },
- );
- assert.equal(result.mode, "build");
- assert.equal(result.response, "Image attachments are only supported for normal prompts.");
- });
- test("command center starts expert workflow in yolo mode", async () => {
- let requestedMode = "";
- let workflowTask = "";
- let promptSubmitted = false;
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- runExpertWorkflow: async (input: { task: string }) => {
- workflowTask = input.task;
- return {
- response: "Expert workflow wf_1 completed.",
- runId: "wf_1",
- status: "completed",
- traceId: "trace-workflow",
- };
- },
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- promptSubmitted = true;
- return {
- response: "prompt",
- };
- },
- }) as never,
- getMode: () => (requestedMode || "build") as never,
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- setMode: async (mode) => {
- requestedMode = mode;
- return mode;
- },
- });
- const result = await submitPrompt("/expert migrate auth", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptSubmitted, false);
- assert.equal(requestedMode, "yolo");
- assert.equal(workflowTask, "migrate auth");
- assert.equal(result.traceId, "trace-workflow");
- assert.match(result.response, /wf_1 completed/);
- });
- test("command center shows expert workflow status without starting a prompt", async () => {
- let statusRunId: string | undefined;
- let workflowStarted = false;
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- expertWorkflowStatus: async (options?: { runId?: string }) => {
- statusRunId = options?.runId;
- return {
- response: "Expert workflow wf_1\nStatus: running",
- runId: "wf_1",
- status: "running",
- };
- },
- runExpertWorkflow: async () => {
- workflowStarted = true;
- return {
- response: "started",
- };
- },
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/expert status wf_1", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(workflowStarted, false);
- assert.equal(statusRunId, "wf_1");
- assert.match(result.response, /Status: running/);
- });
- test("command center reports MCP status without sending a model prompt", async () => {
- let promptSubmitted = false;
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- listMcpServers: async () => ({
- local: {
- status: "connected",
- toolCount: 2,
- transport: "stdio",
- updatedAt: "now",
- },
- remote: {
- error: "Connection timed out\n\nline two of stderr\nline three",
- status: "failed",
- toolCount: 0,
- transport: "http",
- updatedAt: "now",
- },
- }),
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- promptSubmitted = true;
- return {
- response: "prompt",
- };
- },
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/mcp list", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptSubmitted, false);
- assert.match(result.response, /MCP servers:/);
- assert.match(result.response, /local: connected via stdio; 2 tools/);
- assert.match(result.response, /remote: failed via http; 0 tools \(Connection timed out\)/);
- assert.doesNotMatch(result.response, /line two/);
- });
- test("command center opens a plugin toggle picker", async () => {
- let promptSubmitted = false;
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- listPlugins: async () => ({
- commandRoots: [],
- diagnostics: [],
- mcpServers: {},
- plugins: [
- {
- commandRootCount: 1,
- dataPath: "/tmp/zcode/plugins/data/ios-simulator@zcode-plugins-official",
- enabled: false,
- id: "ios-simulator@zcode-plugins-official",
- manifestPath: "/plugins/ios/.zcode-plugin/plugin.json",
- marketplace: "zcode-plugins-official",
- mcpServerNames: ["ios-simulator"],
- name: "ios-simulator",
- rootPath: "/plugins/ios",
- skillCount: 1,
- skillRootCount: 1,
- source: "official",
- version: "0.1.0",
- },
- ],
- skillRoots: [],
- }),
- sessionId: "sess_active",
- traceId: "trace-active",
- submitPrompt: async () => {
- promptSubmitted = true;
- return {
- response: "prompt",
- };
- },
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/plugins", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptSubmitted, false);
- assert.equal(result.selection?.title, "Plugins");
- assert.equal(result.selection?.placement, "composer");
- assert.equal(result.selection?.items[0]?.primary, "○ Ios Simulator");
- assert.equal(
- result.selection?.items[0]?.command,
- "/plugins enable ios-simulator@zcode-plugins-official",
- );
- });
- test("command center resolves plugin labels from listings by stable plugin id", async () => {
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- listPlugins: async () => ({
- commandRoots: [],
- diagnostics: [],
- mcpServers: {},
- pluginListingsById: {
- "computer-use@zcode-plugins-official": {
- displayName: "Computer Use",
- displayNameI18n: { "zh-CN": "电脑控制" },
- },
- "zcode-cua@custom-marketplace": {
- displayName: "Custom Computer Use",
- displayNameI18n: { "zh-CN": "自定义电脑控制" },
- },
- },
- plugins: [
- {
- commandRootCount: 0,
- dataPath: "/tmp/zcode/plugins/data/computer-use@zcode-plugins-official",
- enabled: true,
- id: "computer-use@zcode-plugins-official",
- manifestPath: "/plugins/cua/.zcode-plugin/plugin.json",
- marketplace: "zcode-plugins-official",
- mcpServerNames: [],
- name: "computer-use",
- rootPath: "/plugins/cua",
- skillCount: 0,
- skillRootCount: 0,
- source: "official",
- },
- {
- commandRootCount: 0,
- dataPath: "/tmp/zcode/plugins/data/zcode-cua@custom-marketplace",
- enabled: false,
- id: "zcode-cua@custom-marketplace",
- manifestPath: "/plugins/custom-cua/.zcode-plugin/plugin.json",
- marketplace: "custom-marketplace",
- mcpServerNames: [],
- name: "zcode-cua",
- rootPath: "/plugins/custom-cua",
- skillCount: 0,
- skillRootCount: 0,
- source: "cache",
- },
- {
- commandRootCount: 0,
- dataPath: "/tmp/zcode/plugins/data/zcode-cua@unlisted-marketplace",
- enabled: false,
- id: "zcode-cua@unlisted-marketplace",
- manifestPath: "/plugins/unlisted-cua/.zcode-plugin/plugin.json",
- marketplace: "unlisted-marketplace",
- mcpServerNames: [],
- name: "zcode-cua",
- rootPath: "/plugins/unlisted-cua",
- skillCount: 0,
- skillRootCount: 0,
- source: "cache",
- },
- ],
- skillRoots: [],
- }),
- sessionId: "sess_active",
- traceId: "trace-active",
- submitPrompt: async () => ({ response: "prompt" }),
- }) as never,
- getLocale: () => "zh-CN",
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/plugins", {
- abortSignal: new AbortController().signal,
- });
- const items = result.selection?.items ?? [];
- assert.equal(
- items.find((item) => item.id === "computer-use@zcode-plugins-official")?.primary,
- "✓ 电脑控制",
- );
- assert.equal(
- items.find((item) => item.id === "zcode-cua@custom-marketplace")?.primary,
- "○ 自定义电脑控制",
- );
- assert.equal(
- items.find((item) => item.id === "zcode-cua@unlisted-marketplace")?.primary,
- "○ ZCode Cua",
- );
- });
- test("command center toggles plugins and refreshes the picker", async () => {
- let enabled = false;
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- listPlugins: async () => ({
- commandRoots: [],
- diagnostics: [],
- mcpServers: {},
- plugins: [
- {
- commandRootCount: 1,
- dataPath: "/tmp/zcode/plugins/data/ios-simulator@zcode-plugins-official",
- enabled,
- id: "ios-simulator@zcode-plugins-official",
- manifestPath: "/plugins/ios/.zcode-plugin/plugin.json",
- marketplace: "zcode-plugins-official",
- mcpServerNames: ["ios-simulator"],
- name: "ios-simulator",
- rootPath: "/plugins/ios",
- skillCount: 1,
- skillRootCount: 1,
- source: "official",
- version: "0.1.0",
- },
- ],
- skillRoots: [],
- }),
- sessionId: "sess_active",
- setPluginEnabled: async (plugin, nextEnabled) => {
- assert.equal(plugin, "ios-simulator@zcode-plugins-official");
- enabled = nextEnabled;
- return {
- enabled,
- path: "/home/test/.zcode/cli/config.json",
- plugin: {
- commandRootCount: 1,
- dataPath: "/tmp/zcode/plugins/data/ios-simulator@zcode-plugins-official",
- enabled,
- id: "ios-simulator@zcode-plugins-official",
- manifestPath: "/plugins/ios/.zcode-plugin/plugin.json",
- marketplace: "zcode-plugins-official",
- mcpServerNames: ["ios-simulator"],
- name: "ios-simulator",
- rootPath: "/plugins/ios",
- skillCount: 1,
- skillRootCount: 1,
- source: "official",
- version: "0.1.0",
- },
- };
- },
- traceId: "trace-active",
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/plugins enable ios-simulator@zcode-plugins-official", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(enabled, true);
- assert.match(result.response, /Enabled plugin ios-simulator@zcode-plugins-official/);
- assert.equal(result.selection?.items[0]?.primary, "✓ Ios Simulator");
- assert.equal(
- result.selection?.items[0]?.command,
- "/plugins disable ios-simulator@zcode-plugins-official",
- );
- });
- test("command center requires --force before uninstalling a plugin", async () => {
- let uninstalled = false;
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- listPlugins: async () => ({
- commandRoots: [],
- diagnostics: [],
- mcpServers: {},
- plugins: [],
- skillRoots: [],
- }),
- sessionId: "sess_active",
- traceId: "trace-active",
- uninstallPlugin: async () => {
- uninstalled = true;
- return { removed: { id: "hello@market", name: "hello" } };
- },
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/plugins uninstall hello@market", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(uninstalled, false);
- assert.match(result.response, /Re-run with: \/plugins uninstall hello@market --force/);
- });
- test("command center uninstalls a plugin when --force is provided", async () => {
- let uninstalledPlugin = "";
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- listPlugins: async () => ({
- commandRoots: [],
- diagnostics: [],
- mcpServers: {},
- plugins: [],
- skillRoots: [],
- }),
- sessionId: "sess_active",
- traceId: "trace-active",
- uninstallPlugin: async (plugin: string) => {
- uninstalledPlugin = plugin;
- return { removed: { id: plugin, name: "hello" } };
- },
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/plugins uninstall hello@market --force", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(uninstalledPlugin, "hello@market");
- assert.match(result.response, /Uninstalled plugin hello@market/);
- });
- test("command center dispatches compact through the active app", async () => {
- let submitted = "";
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async (prompt: string) => {
- submitted = prompt;
- return {
- response: "Compacted",
- traceId: "trace-compact",
- };
- },
- }) as never,
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/compact keep recent edits", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(submitted, "/compact keep recent edits");
- assert.equal(result.response, "Compacted");
- assert.equal(result.traceId, "trace-compact");
- });
- test("command center dispatches init through the active app", async () => {
- let submitted = "";
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async (prompt: string) => {
- submitted = prompt;
- return {
- response: "Initialized",
- traceId: "trace-init",
- };
- },
- }) as never,
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/init include repo scripts", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(submitted, "/init include repo scripts");
- assert.equal(result.response, "Initialized");
- assert.equal(result.traceId, "trace-init");
- });
- test("command center dispatches workflow through the active app as a prompt", async () => {
- // 与 /init 同款:TUI 只识别名字,原文交给 app.submitPrompt,由 bootstrap 的 builtin resolver
- // 展开;没有显式分支会落到 resume 兜底,/workflow 就再也到不了模型。
- let submitted = "";
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async (prompt: string) => {
- submitted = prompt;
- return {
- response: "Workflow drafted",
- traceId: "trace-workflow",
- };
- },
- }) as never,
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/workflow review the auth module", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(submitted, "/workflow review the auth module");
- assert.equal(result.response, "Workflow drafted");
- assert.equal(result.traceId, "trace-workflow");
- });
- test("command center dispatches rewind through the active app", async () => {
- let submitted = "";
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async (prompt: string) => {
- submitted = prompt;
- return {
- response: "Rewound",
- traceId: "trace-rewind",
- };
- },
- }) as never,
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/rewind latest", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(submitted, "/rewind latest");
- assert.equal(result.response, "Rewound");
- assert.equal(result.traceId, "trace-rewind");
- });
- test("command center opens a checkpoint picker for bare rewind", async () => {
- let promptSubmitted = false;
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- listCheckpoints: async () => [
- {
- checkpointId: "checkpoint_2",
- createdAt: new Date("2026-05-05T10:00:00Z"),
- fileCount: 2,
- messageId: "msg_2",
- preview: "update tests",
- scope: "workspace",
- },
- ],
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- promptSubmitted = true;
- return {
- response: "prompt",
- };
- },
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/rewind", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptSubmitted, false);
- assert.equal(result.selection?.title, "Rewind To Checkpoint");
- assert.equal(result.selection?.placement, "composer");
- assert.equal(result.selection?.items[0]?.command, "/rewind checkpoint_2");
- assert.match(result.selection?.items[0]?.primary ?? "", /update tests/);
- });
- test("command center forks and switches the active app", async () => {
- let forkTarget: string | undefined;
- const submitPrompt = createCommandCenter({
- forkApp: async (targetCheckpointId) => {
- forkTarget = targetCheckpointId;
- return {
- forkedSessionId: "sess_child",
- response: "Forked session sess_child",
- };
- },
- getApp: async () =>
- ({
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- }) as never,
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/fork checkpoint_1", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(forkTarget, "checkpoint_1");
- assert.equal(result.response, "Forked session sess_child");
- });
- test("command center opens a checkpoint picker for bare fork", async () => {
- let forkCalled = false;
- const submitPrompt = createCommandCenter({
- forkApp: async () => {
- forkCalled = true;
- throw new Error("fork should not run");
- },
- getApp: async () =>
- ({
- listCheckpoints: async () => [
- {
- checkpointId: "checkpoint_1",
- createdAt: 1777975200000,
- fileCount: 1,
- messageId: "msg_1",
- preview: "write file",
- scope: "workspace",
- },
- ],
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- }) as never,
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/fork", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(forkCalled, false);
- assert.equal(result.selection?.title, "Fork From Checkpoint");
- assert.equal(result.selection?.placement, undefined);
- assert.equal(result.selection?.items[0]?.command, "/fork checkpoint_1");
- });
- test("command center starts a new session without sending a model prompt", async () => {
- let promptSubmitted = false;
- let newAppCreated = false;
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- promptSubmitted = true;
- return {
- response: "prompt",
- };
- },
- }) as never,
- getMode: () => "build",
- newApp: async () => {
- newAppCreated = true;
- return {
- sessionId: "sess_new",
- traceId: "trace-new",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- } as never;
- },
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/new", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptSubmitted, false);
- assert.equal(newAppCreated, true);
- assert.equal(result.sessionId, "sess_new");
- assert.equal(result.traceId, "trace-new");
- assert.equal(result.resetSessionProjection, true);
- assert.match(result.response, /Started new session sess_new/);
- });
- test("command center treats clear as a new-session alias", async () => {
- let promptSubmitted = false;
- let newAppCreated = false;
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- promptSubmitted = true;
- return {
- response: "prompt",
- };
- },
- }) as never,
- getMode: () => "build",
- newApp: async () => {
- newAppCreated = true;
- return {
- sessionId: "sess_clear",
- traceId: "trace-clear",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- } as never;
- },
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/clear", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptSubmitted, false);
- assert.equal(newAppCreated, true);
- assert.equal(result.sessionId, "sess_clear");
- assert.equal(result.traceId, "trace-clear");
- assert.equal(result.resetSessionProjection, true);
- assert.match(result.response, /Started new session sess_clear/);
- });
- test("command center dispatches skill through the active app", async () => {
- let submitted = "";
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async (prompt: string) => {
- submitted = prompt;
- return {
- response: "Skill task done",
- traceId: "trace-skill",
- };
- },
- }) as never,
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/skill demo-skill update docs", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(submitted, buildManualSkillPrompt("demo-skill", "update docs"));
- assert.equal(result.response, "Skill task done");
- assert.equal(result.traceId, "trace-skill");
- });
- test("command center reports and switches mode without sending a model prompt", async () => {
- let mode: "plan" | "build" | "edit" | "yolo" | "auto" = "build";
- let promptSubmitted = false;
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- sessionId: "sess_active",
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- promptSubmitted = true;
- return {
- response: "prompt",
- };
- },
- }) as never,
- getMode: () => mode,
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- setMode: async (nextMode) => {
- mode = nextMode;
- return mode;
- },
- });
- const current = await submitPrompt("/mode", {
- abortSignal: new AbortController().signal,
- });
- const switched = await submitPrompt("/mode plan", {
- abortSignal: new AbortController().signal,
- });
- const editSwitched = await submitPrompt("/mode edit", {
- abortSignal: new AbortController().signal,
- });
- const rejected = await submitPrompt("/mode auto", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptSubmitted, false);
- assert.equal(current.mode, "build");
- assert.match(current.response, /Current mode: build/);
- assert.equal(current.selection, undefined);
- assert.equal(switched.mode, "plan");
- assert.match(switched.response, /Mode switched to plan/);
- assert.equal(editSwitched.mode, "edit");
- assert.match(editSwitched.response, /Mode switched to edit/);
- assert.equal(rejected.mode, "edit");
- assert.match(rejected.response, /Unsupported mode: auto/);
- });
- test("command center reports and switches model without sending a model prompt", async () => {
- let model = "openai/gpt-main";
- let promptSubmitted = false;
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- getModel: () => model,
- listModels: () => [
- createTestModelOption("openai", "gpt-main", { label: "Main" }),
- createTestModelOption("openai", "gpt-lite", { label: "Lite" }),
- ],
- sessionId: "sess_active",
- setModel: async (selection: ModelSelection) => {
- const previousModel = model;
- model = `${selection.providerId}/${selection.modelId}`;
- return {
- model,
- previousModel,
- };
- },
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- promptSubmitted = true;
- return {
- response: "prompt",
- };
- },
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const current = await submitPrompt("/model", {
- abortSignal: new AbortController().signal,
- });
- // /model lite 这类别名已移除,切换使用目录中的 provider/model。
- const switched = await submitPrompt("/model openai/gpt-lite", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptSubmitted, false);
- assert.match(current.response, /Current model: openai\/gpt-main/);
- assert.match(current.response, /- openai\/gpt-lite \(Lite; openai\)/);
- assert.match(switched.response, /Model switched to openai\/gpt-lite \(medium\)/);
- });
- test("command center reports and switches effort without sending a model prompt", async () => {
- let thoughtLevel = "medium";
- let promptSubmitted = false;
- const requestedLevels: string[] = [];
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- getThoughtLevel: () => thoughtLevel,
- listThoughtLevels: () => ["low", "medium", "high"],
- sessionId: "sess_active",
- setThoughtLevel: async (level: string) => {
- requestedLevels.push(level);
- const previousThoughtLevel = thoughtLevel;
- thoughtLevel = level;
- return {
- previousThoughtLevel,
- thoughtLevel,
- };
- },
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- promptSubmitted = true;
- return {
- response: "prompt",
- };
- },
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const current = await submitPrompt("/effort", {
- abortSignal: new AbortController().signal,
- });
- const listed = await submitPrompt("/effort list", {
- abortSignal: new AbortController().signal,
- });
- const switched = await submitPrompt("/variant HIGH", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptSubmitted, false);
- assert.equal(current.selection, undefined);
- assert.match(current.response, /Current reasoning effort: medium/);
- assert.match(current.response, /- medium \(current\)/);
- assert.deepEqual(current.effortOptions, [
- { id: "low", label: "low" },
- { id: "medium", label: "medium" },
- { id: "high", label: "high" },
- ]);
- assert.match(listed.response, /Current reasoning effort: medium/);
- assert.match(listed.response, /- high/);
- assert.match(switched.response, /Reasoning effort switched to high/);
- assert.equal(switched.thoughtLevel, "high");
- assert.deepEqual(requestedLevels, ["high"]);
- });
- test("command center reports and switches locale without sending a model prompt", async () => {
- let locale = "en-US" as const | "zh-CN";
- let promptSubmitted = false;
- const submitPrompt = createCommandCenter({
- getApp: async () =>
- ({
- getLocale: () => locale,
- sessionId: "sess_active",
- setLocale: async (requestedLocale: "auto" | "en-US" | "zh-CN") => {
- const previousLocale = locale;
- locale = requestedLocale === "zh-CN" ? "zh-CN" : "en-US";
- return {
- configPath: "/tmp/zcode/config.json",
- locale,
- previousLocale,
- requestedLocale,
- };
- },
- traceId: "trace-active",
- resume: async () => {
- throw new Error("resume should not run");
- },
- submitPrompt: async () => {
- promptSubmitted = true;
- return {
- response: "prompt",
- };
- },
- }) as never,
- getMode: () => "build",
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const current = await submitPrompt("/locale", {
- abortSignal: new AbortController().signal,
- });
- const switched = await submitPrompt("/locale zh-CN", {
- abortSignal: new AbortController().signal,
- });
- const rejected = await submitPrompt("/locale fr-FR", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(promptSubmitted, false);
- assert.equal(current.locale, "en-US");
- assert.match(current.response, /Current locale: en-US/);
- assert.equal(switched.locale, "zh-CN");
- assert.match(switched.response, /Locale switched to zh-CN/);
- assert.match(switched.response, /Config: \/tmp\/zcode\/config\.json/);
- assert.equal(rejected.locale, "zh-CN");
- assert.match(rejected.response, /Unsupported locale: fr-FR/);
- });
- test("command center lists skills without sending a model prompt", async () => {
- let listed = false;
- const submitPrompt = createCommandCenter({
- getApp: async () => {
- throw new Error("app should not be created");
- },
- getMode: () => "build",
- listSkills: async () => {
- listed = true;
- return {
- skills: [
- {
- description: "Use for demo tasks.",
- name: "demo-skill",
- path: "/workspace/project/.zcode/skills/demo/SKILL.md",
- scope: "project",
- source: "zcode",
- whenToUse: "When demo files change.",
- },
- ],
- totalDiscovered: 1,
- };
- },
- resumeApp: async () => {
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/skill", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(listed, true);
- assert.equal(result.mode, "build");
- assert.match(result.response, /Available skills \(1\)/);
- assert.match(result.response, /demo-skill \(project\/zcode\)/);
- assert.match(result.response, /Use for demo tasks\. When demo files change\./);
- assert.match(result.response, /\/workspace\/project\/\.zcode\/skills\/demo\/SKILL\.md/);
- assert.match(result.response, /Use \/skill <name> \[task\] to load one\./);
- });
- test("command center resumes the latest session", async () => {
- let requestedSessionId: string | undefined = "unset";
- const events: string[] = [];
- const submitPrompt = createCommandCenter({
- getApp: async () => {
- throw new Error("default app should not run");
- },
- resumeApp: async (sessionId) => {
- requestedSessionId = sessionId;
- return {
- sessionId: "sess_latest",
- traceId: "trace-app",
- loadSessionTranscript: async () => [
- {
- content: "previous user prompt",
- role: "user",
- },
- {
- content: "previous agent answer",
- role: "agent",
- },
- ],
- resume: async (options) => {
- await options?.onEvent?.(
- createRuntimeEvent("session_resumed", {
- directory: "/tmp/project",
- interruptedToolCount: 0,
- messageCount: 3,
- partCount: 5,
- }),
- );
- return {
- appliedMessageCount: 3,
- directory: "/tmp/project",
- interruptedToolCount: 0,
- messageCount: 3,
- partCount: 5,
- traceId: "trace-resume",
- };
- },
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- };
- },
- });
- const result = await submitPrompt("/resume", {
- abortSignal: new AbortController().signal,
- onEvent: (event) => {
- events.push(event.type);
- },
- });
- assert.equal(requestedSessionId, undefined);
- assert.match(result.response, /Resumed session sess_latest/);
- assert.match(result.response, /Messages: 3\/3/);
- assert.equal(result.resetSessionProjection, true);
- assert.deepEqual(result.restoredMessages, [
- {
- content: "previous user prompt",
- role: "user",
- },
- {
- content: "previous agent answer",
- role: "agent",
- },
- ]);
- assert.equal(result.traceId, "trace-resume");
- assert.deepEqual(events, ["session_resumed"]);
- });
- test("command center opens a session picker for bare resume when sessions are available", async () => {
- let resumeCalled = false;
- const submitPrompt = createCommandCenter({
- getApp: async () => {
- throw new Error("default app should not run");
- },
- getMode: () => "build",
- listSessions: async () => [
- {
- directory: "/workspace/project",
- id: "sess_two",
- title: "Second session",
- updatedAt: new Date("2026-05-05T12:00:00Z"),
- },
- {
- directory: "/workspace/project",
- id: "sess_one",
- parentId: "sess_parent",
- title: "Forked session",
- updatedAt: 1777971600000,
- },
- ],
- resumeApp: async () => {
- resumeCalled = true;
- throw new Error("resume should not run");
- },
- });
- const result = await submitPrompt("/resume", {
- abortSignal: new AbortController().signal,
- });
- assert.equal(resumeCalled, false);
- assert.equal(result.mode, "build");
- assert.equal(result.selection?.title, "Resume Session");
- assert.equal(result.selection?.placement, "composer");
- assert.equal(result.selection?.items[0]?.command, "/resume sess_two");
- assert.match(result.selection?.items[1]?.meta ?? "", /fork of sess_parent/);
- });
- test("prints help with help flag", async () => {
- const ctx = createContext(["--help"]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 0);
- assert.match(ctx.stdout.output(), /With no command, zcode opens the full-screen TUI\./);
- assert.match(ctx.stdout.output(), /Commands:/);
- assert.match(ctx.stdout.output(), /--force-mcs/);
- assert.match(ctx.stdout.output(), /--surface <surface>/);
- assert.match(ctx.stdout.output(), /--resume <sessionId>/);
- assert.match(ctx.stdout.output(), /sessionId \(sess_\.\.\.\)/);
- assert.equal(ctx.stderr.output(), "");
- });
- test("prints version with version flag", async () => {
- const ctx = createContext(["--version"]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 0);
- assert.match(ctx.stdout.output().trim(), /^\d+\.\d+\.\d+|0\.0\.0$/);
- assert.equal(ctx.stderr.output(), "");
- });
- test("prints version with version command", async () => {
- const ctx = createContext(["version"]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 0);
- assert.match(ctx.stdout.output().trim(), /^\d+\.\d+\.\d+|0\.0\.0$/);
- assert.equal(ctx.stderr.output(), "");
- });
- test("rejects removed hello command", async () => {
- const ctx = createContext(["hello"]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /Unknown command: hello/);
- assert.equal(ctx.stdout.output(), "");
- });
- test("zcode plugins list prints installed plugins", async () => {
- const ctx = createContext(["plugins", "list", "--verbose"]);
- const logger = {} as NonNullable<RunDependencies["logger"]>;
- const exitCode = await run(ctx, {
- cwd: () => "/workspace/project",
- logger,
- listPlugins: (options) => {
- assert.equal(options.logger, logger);
- assert.equal(options.workingDirectory, "/workspace/project");
- return {
- commandRoots: [],
- diagnostics: [
- {
- code: "plugin_unsupported_component",
- message: "Plugin component is ignored in P0: hooks",
- pluginId: "ios-dev@inline",
- severity: "warning",
- },
- ],
- mcpServers: {},
- plugins: [
- {
- commandRootCount: 1,
- dataPath: "/tmp/zcode/plugins/data/ios-dev@inline",
- enabled: true,
- id: "ios-dev@inline",
- manifestPath: "/plugins/ios/.zcode-plugin/plugin.json",
- marketplace: "inline",
- mcpServerNames: ["ios-simulator"],
- name: "ios-dev",
- rootPath: "/plugins/ios",
- skillCount: 1,
- skillRootCount: 1,
- source: "inline",
- },
- ],
- skillRoots: [],
- } as never;
- },
- });
- const output = ctx.stdout.output();
- assert.equal(exitCode, 0);
- assert.match(output, /Plugins \(1\)/);
- assert.match(output, /ios-dev@inline \[enabled\]/);
- assert.match(output, /mcp: ios-simulator/);
- assert.match(output, /Diagnostics \(1\)/);
- });
- test("official plugin host runs a seeded official plugin server main", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-cli-plugin-host-"));
- const pluginRoot = join(
- root,
- "cache",
- ZCODE_OFFICIAL_PLUGIN_MARKETPLACE,
- "ios-simulator",
- "0.1.0",
- );
- const serverPath = join(pluginRoot, "dist", "mcp", "server.js");
- const outputPath = join(root, "host-output.json");
- const originalOutputEnv = process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT;
- try {
- await mkdir(join(pluginRoot, "dist", "mcp"), { recursive: true });
- await writeFile(
- join(pluginRoot, ".zcode-plugin-seed.json"),
- JSON.stringify({
- marketplace: ZCODE_OFFICIAL_PLUGIN_MARKETPLACE,
- plugin: "ios-simulator",
- pluginVersion: "0.1.0",
- version: 1,
- }),
- );
- await writeFile(join(pluginRoot, "package.json"), JSON.stringify({ type: "module" }));
- await writeFile(
- serverPath,
- `import { writeFile } from "node:fs/promises";
- export async function main() {
- await writeFile(process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT, JSON.stringify({ argv: process.argv }));
- }
- `,
- );
- process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT = outputPath;
- const ctx = createContext([ZCODE_PLUGIN_HOST_COMMAND, serverPath]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "");
- assert.equal(ctx.stderr.output(), "");
- const payload = JSON.parse(await readFile(outputPath, "utf8"));
- assert.equal(payload.argv[0], process.execPath);
- assert.equal(payload.argv[1], serverPath);
- } finally {
- if (originalOutputEnv === undefined) {
- delete process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT;
- } else {
- process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT = originalOutputEnv;
- }
- await rm(root, { force: true, recursive: true });
- }
- });
- test("retired argv-based CUA host fails closed without the shared node_repl marker", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-cli-cua-plugin-host-mismatch-"));
- const serverPath = join(root, "server.js");
- const outputPath = join(root, "host-output.json");
- const keys = [
- "ZCODE_CUA_PERMISSION_BROKER_SOCKET",
- "ZCODE_CUA_PERMISSION_BROKER_TOKEN",
- "ZCODE_PLUGIN_HOST_TEST_OUTPUT",
- ZCODE_PLUGIN_ID_ENV_KEY,
- ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY,
- ZCODE_CUA_NODE_REPL_HOST_ENV_KEY,
- ] as const;
- const originalEnv = Object.fromEntries(keys.map((key) => [key, process.env[key]]));
- try {
- await writeFile(join(root, "package.json"), JSON.stringify({ type: "module" }));
- await writeFile(
- serverPath,
- `import { writeFile } from "node:fs/promises";
- export async function main() {
- await writeFile(process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT, JSON.stringify({
- token: process.env.ZCODE_CUA_PERMISSION_BROKER_TOKEN,
- }));
- }
- `,
- );
- process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT = outputPath;
- process.env[ZCODE_PLUGIN_ID_ENV_KEY] = ZCODE_CUA_OFFICIAL_PLUGIN_ID;
- process.env.ZCODE_CUA_PERMISSION_BROKER_SOCKET = "/tmp/cua-broker.sock";
- process.env.ZCODE_CUA_PERMISSION_BROKER_TOKEN = "scoped-secret-token";
- process.env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY] = "official-authority";
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- sanitizeZCodeRuntimeEnvInPlace(process.env);
- assert.equal(process.env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY], undefined);
- const ctx = createContext([ZCODE_PLUGIN_HOST_COMMAND, serverPath]);
- const exitCode = await runPluginHostCommand(ctx, [
- serverPath,
- "--permission-broker-socket",
- "/tmp/attacker.sock",
- ]);
- assert.equal(exitCode, 1);
- assert.match(
- ctx.stderr.output(),
- /Captured ZCode CUA broker credentials may only launch the trusted shared node_repl host/,
- );
- await assert.rejects(readFile(outputPath, "utf8"), { code: "ENOENT" });
- } finally {
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- for (const key of keys) {
- const value = originalEnv[key];
- if (value === undefined) delete process.env[key];
- else process.env[key] = value;
- }
- await rm(root, { force: true, recursive: true });
- }
- });
- test("shared node_repl host restores the captured CUA broker credentials", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-cli-cua-node-repl-host-"));
- const serverPath = join(root, "server.js");
- const outputPath = join(root, "host-output.json");
- const keys = [
- "ZCODE_CUA_PERMISSION_BROKER_SOCKET",
- "ZCODE_CUA_PERMISSION_BROKER_TOKEN",
- "ZCODE_PLUGIN_HOST_TEST_OUTPUT",
- ZCODE_PLUGIN_ID_ENV_KEY,
- ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY,
- ZCODE_CUA_NODE_REPL_HOST_ENV_KEY,
- ] as const;
- const originalEnv = Object.fromEntries(keys.map((key) => [key, process.env[key]]));
- try {
- await writeFile(join(root, "package.json"), JSON.stringify({ type: "module" }));
- await writeFile(
- serverPath,
- `import { writeFile } from "node:fs/promises";
- export async function main() {
- await writeFile(process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT, JSON.stringify({
- socket: process.env.ZCODE_CUA_PERMISSION_BROKER_SOCKET,
- token: process.env.ZCODE_CUA_PERMISSION_BROKER_TOKEN,
- }));
- }
- `,
- );
- process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT = outputPath;
- process.env[ZCODE_PLUGIN_ID_ENV_KEY] = ZCODE_CUA_OFFICIAL_PLUGIN_ID;
- process.env[ZCODE_CUA_NODE_REPL_HOST_ENV_KEY] = "1";
- process.env.ZCODE_CUA_PERMISSION_BROKER_SOCKET = "/tmp/cua-broker.sock";
- process.env.ZCODE_CUA_PERMISSION_BROKER_TOKEN = "scoped-secret-token";
- process.env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY] = "official-authority";
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- sanitizeZCodeRuntimeEnvInPlace(process.env);
- const ctx = createContext([ZCODE_PLUGIN_HOST_COMMAND, serverPath]);
- assert.equal(await runPluginHostCommand(ctx, [serverPath]), 0);
- // identity 模式:恢复给宿主的只有 socket(配 authority),没有口令。环境里那个遗留
- // token 是历史键,sanitize 会连它一起剔除,因此宿主根本看不到它 —— 否则旧版 Helper
- // (认 bearer token 的那些)就能被同 agent 内的其它子进程直接驱动。
- // 逐字段断言:JSON.stringify 会把值为 undefined 的键整个省掉,深比较对不上。
- const hostEnv = JSON.parse(await readFile(outputPath, "utf8")) as {
- socket?: string;
- token?: string;
- };
- assert.equal(hostEnv.socket, "/tmp/cua-broker.sock");
- assert.equal(hostEnv.token, undefined);
- // 宿主只在运行期间短暂恢复,跑完父进程 env 必须回到已剔除状态。
- assert.equal(process.env.ZCODE_CUA_PERMISSION_BROKER_SOCKET, undefined);
- assert.equal(process.env.ZCODE_CUA_PERMISSION_BROKER_TOKEN, undefined);
- } finally {
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- for (const key of keys) {
- const value = originalEnv[key];
- if (value === undefined) delete process.env[key];
- else process.env[key] = value;
- }
- await rm(root, { force: true, recursive: true });
- }
- });
- test("shared node_repl host launches with identity-mode credentials (socket + authority, no token)", async () => {
- // M1 身份模式(darwin):Helper 不签发 token;M7 懒启动:authority 由 spawn 铸造。
- // 2026-09-10 回归:守卫曾要求 token,导致 node_repl 宿主启动即退出、工具面为空。
- const root = await mkdtemp(join(tmpdir(), "zcode-cli-cua-node-repl-identity-"));
- const serverPath = join(root, "server.js");
- const outputPath = join(root, "host-output.json");
- const keys = [
- "ZCODE_CUA_PERMISSION_BROKER_SOCKET",
- "ZCODE_CUA_PERMISSION_BROKER_TOKEN",
- "ZCODE_PLUGIN_HOST_TEST_OUTPUT",
- ZCODE_PLUGIN_ID_ENV_KEY,
- ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY,
- ZCODE_CUA_NODE_REPL_HOST_ENV_KEY,
- ] as const;
- const originalEnv = Object.fromEntries(keys.map((key) => [key, process.env[key]]));
- try {
- await writeFile(join(root, "package.json"), JSON.stringify({ type: "module" }));
- await writeFile(
- serverPath,
- `import { writeFile } from "node:fs/promises";
- export async function main() {
- await writeFile(process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT, JSON.stringify({
- socket: process.env.ZCODE_CUA_PERMISSION_BROKER_SOCKET,
- token: process.env.ZCODE_CUA_PERMISSION_BROKER_TOKEN ?? null,
- }));
- }
- `,
- );
- process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT = outputPath;
- process.env[ZCODE_PLUGIN_ID_ENV_KEY] = ZCODE_CUA_OFFICIAL_PLUGIN_ID;
- process.env[ZCODE_CUA_NODE_REPL_HOST_ENV_KEY] = "1";
- process.env.ZCODE_CUA_PERMISSION_BROKER_SOCKET = "/tmp/cua-broker.sock";
- delete process.env.ZCODE_CUA_PERMISSION_BROKER_TOKEN;
- process.env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY] = "official-authority";
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- sanitizeZCodeRuntimeEnvInPlace(process.env);
- const ctx = createContext([ZCODE_PLUGIN_HOST_COMMAND, serverPath]);
- assert.equal(await runPluginHostCommand(ctx, [serverPath]), 0);
- assert.deepEqual(JSON.parse(await readFile(outputPath, "utf8")), {
- socket: "/tmp/cua-broker.sock",
- token: null,
- });
- } finally {
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- for (const key of keys) {
- const value = originalEnv[key];
- if (value === undefined) delete process.env[key];
- else process.env[key] = value;
- }
- await rm(root, { force: true, recursive: true });
- }
- });
- test("plugin host refuses non-official code while product broker credentials are captured", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-cli-non-cua-plugin-host-"));
- const serverPath = join(root, "server.js");
- const outputPath = join(root, "host-output.json");
- const keys = [
- "ZCODE_CUA_PERMISSION_BROKER_SOCKET",
- "ZCODE_CUA_PERMISSION_BROKER_TOKEN",
- "ZCODE_PLUGIN_HOST_TEST_OUTPUT",
- ZCODE_PLUGIN_ID_ENV_KEY,
- ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY,
- ZCODE_CUA_NODE_REPL_HOST_ENV_KEY,
- ] as const;
- const originalEnv = Object.fromEntries(keys.map((key) => [key, process.env[key]]));
- try {
- await writeFile(join(root, "package.json"), JSON.stringify({ type: "module" }));
- await writeFile(
- serverPath,
- `import { writeFile } from "node:fs/promises";
- export async function main() {
- await writeFile(process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT, String(Boolean(process.env.ZCODE_CUA_PERMISSION_BROKER_TOKEN)));
- }
- `,
- );
- process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT = outputPath;
- process.env[ZCODE_PLUGIN_ID_ENV_KEY] = "zcode-cua@third-party";
- process.env.ZCODE_CUA_PERMISSION_BROKER_SOCKET = "/tmp/cua-broker.sock";
- process.env.ZCODE_CUA_PERMISSION_BROKER_TOKEN = "must-not-leak";
- process.env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY] = "captured-authority";
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- sanitizeZCodeRuntimeEnvInPlace(process.env);
- assert.equal(process.env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY], undefined);
- const ctx = createContext([
- ZCODE_PLUGIN_HOST_COMMAND,
- serverPath,
- "--permission-broker-socket",
- "/tmp/cua-broker.sock",
- ]);
- assert.equal(await run(ctx), 1);
- assert.match(
- ctx.stderr.output(),
- /Captured ZCode CUA broker credentials may only launch the trusted shared node_repl host/,
- );
- await assert.rejects(readFile(outputPath, "utf8"), { code: "ENOENT" });
- } finally {
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- for (const key of keys) {
- const value = originalEnv[key];
- if (value === undefined) delete process.env[key];
- else process.env[key] = value;
- }
- await rm(root, { force: true, recursive: true });
- }
- });
- test("plugin host can run an explicitly configured third-party module", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-cli-plugin-host-third-party-"));
- const serverPath = join(root, "third-party", "server.js");
- const outputPath = join(root, "third-party-output.json");
- const originalOutputEnv = process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT;
- try {
- await mkdir(join(root, "third-party"), { recursive: true });
- await writeFile(join(root, "third-party", "package.json"), JSON.stringify({ type: "module" }));
- await writeFile(
- serverPath,
- `import { writeFile } from "node:fs/promises";
- export async function main() {
- await writeFile(process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT, JSON.stringify({ argv: process.argv }));
- }
- `,
- );
- process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT = outputPath;
- const ctx = createContext([ZCODE_PLUGIN_HOST_COMMAND, serverPath, "--", "hello"]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "");
- assert.equal(ctx.stderr.output(), "");
- const payload = JSON.parse(await readFile(outputPath, "utf8"));
- assert.deepEqual(payload.argv, [process.execPath, serverPath, "--", "hello"]);
- } finally {
- if (originalOutputEnv === undefined) {
- delete process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT;
- } else {
- process.env.ZCODE_PLUGIN_HOST_TEST_OUTPUT = originalOutputEnv;
- }
- await rm(root, { force: true, recursive: true });
- }
- });
- test("plugin host stays alive past the one-shot CLI watchdog deadline", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-cli-plugin-host-lifetime-"));
- const serverPath = join(root, "server.mjs");
- const cliEntryPath = fileURLToPath(new URL("../src/main.ts", import.meta.url));
- const tsxLoaderPath = fileURLToPath(import.meta.resolve("tsx"));
- let child: ReturnType<typeof spawn> | undefined;
- try {
- await writeFile(
- serverPath,
- `export async function main() {
- process.stdin.resume();
- process.stdout.write("plugin-host-ready\\n");
- }
- `,
- );
- child = spawn(
- process.execPath,
- ["--import", tsxLoaderPath, cliEntryPath, ZCODE_PLUGIN_HOST_COMMAND, serverPath],
- {
- env: process.env,
- stdio: ["pipe", "pipe", "pipe"],
- },
- );
- child.stdout?.setEncoding("utf8");
- await withTimeout(
- new Promise<void>((resolve, reject) => {
- let stdout = "";
- const cleanup = () => {
- child?.stdout?.off("data", onData);
- child?.off("exit", onExit);
- };
- const onData = (chunk: string) => {
- stdout += chunk;
- if (!stdout.includes("plugin-host-ready")) return;
- cleanup();
- resolve();
- };
- const onExit = (code: number | null, signal: NodeJS.Signals | null) => {
- cleanup();
- reject(new Error(`plugin host exited before ready: code=${code}, signal=${signal}`));
- };
- child?.stdout?.on("data", onData);
- child?.once("exit", onExit);
- }),
- 5_000,
- "plugin host did not become ready",
- );
- await delay(1_250);
- assert.equal(child.exitCode, null);
- child.stdin?.end();
- const [exitCode, signal] = (await withTimeout(
- once(child, "exit"),
- 5_000,
- "plugin host did not exit after stdin closed",
- )) as [number | null, NodeJS.Signals | null];
- assert.equal(exitCode, 0);
- assert.equal(signal, null);
- } finally {
- if (child && child.exitCode === null && child.signalCode === null) {
- child.kill("SIGKILL");
- await once(child, "exit");
- }
- await rm(root, { force: true, recursive: true });
- }
- });
- test("failed plugin host still exits when initialization leaves stdin active", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-cli-plugin-host-failure-"));
- const serverPath = join(root, "server.mjs");
- const cliEntryPath = fileURLToPath(new URL("../src/main.ts", import.meta.url));
- const tsxLoaderPath = fileURLToPath(import.meta.resolve("tsx"));
- let child: ReturnType<typeof spawn> | undefined;
- let stderr = "";
- try {
- await writeFile(
- serverPath,
- `export async function main() {
- process.stdin.resume();
- throw new Error("expected initialization failure");
- }
- `,
- );
- child = spawn(
- process.execPath,
- ["--import", tsxLoaderPath, cliEntryPath, ZCODE_PLUGIN_HOST_COMMAND, serverPath],
- {
- env: process.env,
- stdio: ["pipe", "pipe", "pipe"],
- },
- );
- child.stderr?.setEncoding("utf8");
- child.stderr?.on("data", (chunk: string) => {
- stderr += chunk;
- });
- const [exitCode, signal] = (await withTimeout(
- once(child, "exit"),
- 5_000,
- "failed plugin host did not exit",
- )) as [number | null, NodeJS.Signals | null];
- assert.equal(exitCode, 1);
- assert.equal(signal, null);
- assert.match(stderr, /Plugin host failed: expected initialization failure/u);
- } finally {
- if (child && child.exitCode === null && child.signalCode === null) {
- child.kill("SIGKILL");
- await once(child, "exit");
- }
- await rm(root, { force: true, recursive: true });
- }
- });
- test("zcode plugins enable persists the selected plugin", async () => {
- const ctx = createContext(["plugins", "enable", "ios-dev"]);
- const exitCode = await run(ctx, {
- cwd: () => "/workspace/project",
- setPluginEnabled: async (options) => {
- assert.equal(options.enabled, true);
- assert.equal(options.plugin, "ios-dev");
- assert.equal(options.workingDirectory, "/workspace/project");
- return {
- enabled: true,
- path: "/home/test/.zcode/cli/config.json",
- plugin: {
- commandRootCount: 1,
- dataPath: "/tmp/zcode/plugins/data/ios-dev@zcode-plugins-official",
- enabled: true,
- id: "ios-dev@zcode-plugins-official",
- manifestPath: "/plugins/ios/.zcode-plugin/plugin.json",
- marketplace: "zcode-plugins-official",
- mcpServerNames: ["ios-simulator"],
- name: "ios-dev",
- rootPath: "/plugins/ios",
- skillCount: 1,
- skillRootCount: 1,
- source: "official",
- },
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.match(
- ctx.stdout.output(),
- /Enabled plugin ios-dev@zcode-plugins-official in \/home\/test\/.zcode\/cli\/config\.json/,
- );
- });
- test("prints slash help for prompt help command without creating an app", async () => {
- const ctx = createContext(["--prompt", "/help"]);
- let appCreated = false;
- const exitCode = await run(ctx, {
- createZCodeApp: () => {
- appCreated = true;
- throw new Error("app should not be created");
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(appCreated, false);
- assert.match(ctx.stdout.output(), /Slash commands:/);
- assert.match(ctx.stdout.output(), /\/help \[command\]/);
- assert.match(ctx.stdout.output(), /Use \/help <command> for details\./);
- assert.equal(ctx.stderr.output(), "");
- });
- test("lists skills in human format", async () => {
- const ctx = createContext(["skills", "list", "--verbose"]);
- const logger = {} as NonNullable<RunDependencies["logger"]>;
- const exitCode = await run(ctx, {
- cwd: () => "/workspace/project",
- logger,
- listSkills: async (options) => {
- assert.equal(options.logger, logger);
- assert.equal(options.workingDirectory, "/workspace/project");
- return {
- diagnostics: [
- {
- code: "skill_scan_failed",
- message: "Failed to scan skill directory: demo",
- path: "/workspace/project/.zcode/skills/demo/SKILL.md",
- severity: "warning",
- skillName: "demo",
- },
- ],
- skills: [
- {
- description: "Use for demo tasks.",
- directory: "/workspace/project/.zcode/skills/demo",
- frontmatterKeys: ["name", "description", "version"],
- name: "demo",
- path: "/workspace/project/.zcode/skills/demo/SKILL.md",
- rootPath: "/workspace/project/.zcode/skills",
- safeToAutoLoad: true,
- scope: "project",
- source: "zcode",
- },
- ],
- totalDiscovered: 1,
- } as never;
- },
- });
- const output = ctx.stdout.output();
- assert.equal(exitCode, 0);
- assert.match(output, /Available skills \(1\)/);
- assert.match(output, /demo \(project\/zcode\)/);
- assert.match(output, /Use for demo tasks\./);
- assert.match(output, /Diagnostics \(1\)/);
- assert.equal(ctx.stderr.output(), "");
- });
- test("lists skills for bare prompt skill command", async () => {
- const ctx = createContext(["--prompt", "/skill"]);
- let listed = false;
- let appCreated = false;
- const exitCode = await run(ctx, {
- createZCodeApp: () => {
- appCreated = true;
- throw new Error("app should not be created");
- },
- cwd: () => "/workspace/project",
- listSkills: async (options) => {
- listed = true;
- assert.equal(options.workingDirectory, "/workspace/project");
- return {
- diagnostics: [],
- skills: [
- {
- description: "Review React code.",
- directory: "/workspace/project/.zcode/skills/react",
- frontmatterKeys: ["name", "description"],
- name: "react-review",
- path: "/workspace/project/.zcode/skills/react/SKILL.md",
- rootPath: "/workspace/project/.zcode/skills",
- safeToAutoLoad: true,
- scope: "project",
- source: "zcode",
- },
- ],
- totalDiscovered: 1,
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(listed, true);
- assert.equal(appCreated, false);
- assert.match(ctx.stdout.output(), /Available skills \(1\)/);
- assert.match(ctx.stdout.output(), /react-review \(project\/zcode\)/);
- assert.equal(ctx.stderr.output(), "");
- });
- test("lists skills in JSON format", async () => {
- const ctx = createContext(["skills", "--json"]);
- const exitCode = await run(ctx, {
- cwd: () => "/workspace/project",
- listSkills: async () =>
- ({
- diagnostics: [],
- skills: [
- {
- description: "Review React code.",
- directory: "/workspace/project/.zcode/skills/react",
- frontmatterKeys: ["name", "description", "when_to_use"],
- name: "react-review",
- path: "/workspace/project/.zcode/skills/react/SKILL.md",
- rootPath: "/workspace/project/.zcode/skills",
- safeToAutoLoad: true,
- scope: "project",
- source: "zcode",
- whenToUse: "When React files change.",
- },
- ],
- totalDiscovered: 1,
- }) as never,
- });
- const payload = JSON.parse(ctx.stdout.output()) as {
- cwd: string;
- skills: Array<{ name: string; source: string; whenToUse?: string }>;
- totalDiscovered: number;
- };
- assert.equal(exitCode, 0);
- assert.equal(payload.cwd, "/workspace/project");
- assert.equal(payload.skills[0]?.name, "react-review");
- assert.equal(payload.skills[0]?.source, "zcode");
- assert.equal(payload.skills[0]?.whenToUse, "When React files change.");
- assert.equal(payload.totalDiscovered, 1);
- assert.equal(ctx.stderr.output(), "");
- });
- test("rejects unknown skills subcommands", async () => {
- const ctx = createContext(["skills", "missing"]);
- const exitCode = await run(ctx, {
- listSkills: async () => {
- throw new Error("should not list");
- },
- });
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /Unknown skills command: missing/);
- assert.match(ctx.stderr.output(), /Usage: zcode skills \[list\|inspect <name>\]/);
- assert.equal(ctx.stdout.output(), "");
- });
- test("loads nearest dotenv without overriding shell env", async () => {
- const tempRoot = await mkdtemp(join(tmpdir(), "zcode-dotenv-"));
- const nested = join(tempRoot, "project", "nested");
- try {
- await mkdir(nested, { recursive: true });
- await writeFile(
- join(tempRoot, ".env"),
- ["ZCODE_API_KEY=from-file", "ZCODE_BASE_URL=https://example.test"].join("\n"),
- );
- const env: NodeJS.ProcessEnv = {
- ZCODE_API_KEY: "from-shell",
- };
- const result = loadCliDotenv({ cwd: nested, env });
- assert.equal(findDotenv(nested), join(tempRoot, ".env"));
- assert.equal(result.loaded, true);
- assert.deepEqual(result.keys.sort(), ["ZCODE_API_KEY", "ZCODE_BASE_URL"].sort());
- assert.equal(env.ZCODE_API_KEY, "from-shell");
- assert.equal(env.ZCODE_BASE_URL, "https://example.test");
- } finally {
- await rm(tempRoot, { recursive: true, force: true });
- }
- });
- test("loads dotenv before creating the prompt app", async () => {
- const env: NodeJS.ProcessEnv = {};
- const ctx = createContext(["--prompt", "hello env"]);
- let appEnv: NodeJS.ProcessEnv | undefined;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- appEnv = options?.env;
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- env,
- loadDotenv: (options) => {
- if (options?.env) {
- options.env.ZCODE_API_KEY = "loaded-at-runtime";
- }
- return {
- keys: ["ZCODE_API_KEY"],
- loaded: true,
- path: "/tmp/project/.env",
- };
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(appEnv?.ZCODE_API_KEY, "loaded-at-runtime");
- assert.equal(ctx.stdout.output(), "hello env\n");
- assert.equal(ctx.stderr.output(), "");
- });
- test("runs a single prompt without opening the TUI", async () => {
- const ctx = createContext(["--prompt", "hello runtime"]);
- const exitCode = await run(ctx, createPromptDeps());
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello runtime\n");
- assert.equal(ctx.stderr.output(), "");
- });
- test("passes the CLI version into app creation for prompt runs", async () => {
- const ctx = createContext(["--prompt", "hello version"]);
- let appVersion: string | undefined;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- appVersion = options?.version;
- return {
- getModel: () => "openai/gpt-test",
- getThoughtLevel: () => "medium",
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(appVersion, "0.0.0");
- });
- test("passes prompt attachments into the app API", async () => {
- const ctx = createContext([
- "--prompt",
- "describe",
- "--attach",
- "screen.png",
- "--attach",
- "notes.txt",
- "--attach",
- "demo.MP4",
- ]);
- let submitted: unknown;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () =>
- ({
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: unknown) => {
- submitted = prompt;
- return {
- events: [],
- projection: fakeProjection as never,
- response: "done",
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- };
- },
- }) as never,
- });
- assert.equal(exitCode, 0);
- assert.deepEqual(submitted, {
- text: "describe",
- attachments: [
- { type: "image", path: "screen.png" },
- { type: "file", path: "notes.txt" },
- { type: "video", path: "demo.MP4" },
- ],
- });
- assert.equal(ctx.stdout.output(), "done\n");
- });
- test("rewrites skill slash commands for single prompts", async () => {
- const ctx = createContext(["--prompt", "/skill demo-skill update docs"]);
- let submitted = "";
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () =>
- ({
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => {
- submitted = prompt;
- return {
- events: [],
- projection: fakeProjection as never,
- response: "done",
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- };
- },
- }) as never,
- });
- assert.equal(exitCode, 0);
- assert.equal(submitted, buildManualSkillPrompt("demo-skill", "update docs"));
- assert.equal(ctx.stdout.output(), "done\n");
- assert.equal(ctx.stderr.output(), "");
- });
- test("rejects goal replacement flag without target", async () => {
- const ctx = createContext(["--target-replace"]);
- const exitCode = await run(ctx, createPromptDeps());
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /--target-replace requires --target/);
- assert.equal(ctx.stdout.output(), "");
- });
- test("runs goal command without prompt", async () => {
- const ctx = createContext(["--target", "Ship target MVP"]);
- let continued = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.deepEqual(options?.runtimeConfig?.memory, {
- extractionEnabled: false,
- });
- return {
- clearTarget: async () => false,
- continueActiveTarget: async () => {
- continued = true;
- return {
- projection: fakeProjection as never,
- response: "continued target",
- traceId: "trace-target-continue" as never,
- turnId: "turn-target-continue" as never,
- };
- },
- readTarget: async () => null,
- sessionId: "session-target-continue",
- setTarget: async ({ objective, status = "active" }) => ({
- objective,
- sessionID: "session-target-continue",
- status,
- targetID: "target-continue",
- time: { created: 1, updated: 1 },
- }),
- traceId: "trace-target-continue",
- runtime: {} as never,
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- updateTargetStatus: async () => null,
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(continued, true);
- assert.equal(ctx.stdout.output(), "continued target\n");
- assert.equal(ctx.stderr.output(), "");
- });
- test("passes force MCS into target runtime config", async () => {
- const ctx = createContext(["--force-mcs", "--target", "Ship target MVP"]);
- let continued = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.deepEqual((options?.runtimeConfig as any)?.midConversationSystem, {
- mode: "force",
- });
- return {
- clearTarget: async () => false,
- continueActiveTarget: async () => {
- continued = true;
- return {
- projection: fakeProjection as never,
- response: "continued target",
- traceId: "trace-target-force-mcs" as never,
- turnId: "turn-target-force-mcs" as never,
- };
- },
- readTarget: async () => null,
- sessionId: "session-target-force-mcs",
- setTarget: async ({ objective, status = "active" }) => ({
- objective,
- sessionID: "session-target-force-mcs",
- status,
- targetID: "target-force-mcs",
- time: { created: 1, updated: 1 },
- }),
- traceId: "trace-target-force-mcs",
- runtime: {} as never,
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- updateTargetStatus: async () => null,
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(continued, true);
- assert.equal(ctx.stdout.output(), "continued target\n");
- assert.equal(ctx.stderr.output(), "");
- });
- test("rejects empty target text", async () => {
- const ctx = createContext(["--target", " "]);
- const exitCode = await run(ctx, createPromptDeps());
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /--target requires non-empty text/);
- assert.equal(ctx.stdout.output(), "");
- });
- test("rejects combining target with prompt", async () => {
- const ctx = createContext(["--prompt", "hello target", "--target", "Ship target MVP"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () =>
- ({
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- }) as never,
- });
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /--target cannot be used with --prompt/);
- assert.equal(ctx.stdout.output(), "");
- });
- test("closes the app after a headless prompt", async () => {
- const ctx = createContext(["--prompt", "hello"]);
- let closeCount = 0;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () =>
- ({
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- close: async () => {
- closeCount += 1;
- },
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- }) as never,
- });
- assert.equal(exitCode, 0);
- assert.equal(closeCount, 1);
- assert.equal(ctx.stdout.output(), "hello\n");
- });
- test("shuts down prepared telemetry after a headless prompt", async () => {
- const ctx = createContext(["--prompt", "hello telemetry"]);
- let telemetryShutdownCount = 0;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- prepareZCodeTelemetryEnv: async (env) => env,
- shutdownZCodeTelemetry: async () => {
- telemetryShutdownCount += 1;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(telemetryShutdownCount, 1);
- });
- test("shuts down prepared telemetry when headless app creation fails", async () => {
- const ctx = createContext(["--prompt", "failing telemetry"]);
- let telemetryShutdownCount = 0;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () => {
- throw new Error("app creation failed");
- },
- prepareZCodeTelemetryEnv: async (env) => env,
- shutdownZCodeTelemetry: async () => {
- telemetryShutdownCount += 1;
- },
- });
- assert.equal(exitCode, 1);
- assert.equal(telemetryShutdownCount, 1);
- });
- test("aborts and closes the headless app on process shutdown signals", async () => {
- const ctx = createContext(["--prompt", "hello"]);
- const shutdownProcess = createTestShutdownProcess("linux");
- const exitDeferred = createDeferred<number>();
- const promptStarted = createDeferred<void>();
- let closeCount = 0;
- let abortSeen = false;
- const runPromise = run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () =>
- ({
- close: async () => {
- closeCount += 1;
- },
- runtime: {} as never,
- sessionId: "session-test",
- submitPrompt: async (_prompt: string, options?: { abortSignal?: AbortSignal }) => {
- promptStarted.resolve();
- return await new Promise<never>((_resolve, reject) => {
- options?.abortSignal?.addEventListener(
- "abort",
- () => {
- abortSeen = true;
- reject(new Error("aborted by signal"));
- },
- { once: true },
- );
- });
- },
- traceId: "trace-test",
- }) as never,
- exitProcess: (code) => {
- exitDeferred.resolve(code);
- },
- shutdownProcess,
- });
- await promptStarted.promise;
- shutdownProcess.emitSignal("SIGTERM");
- assert.equal(await exitDeferred.promise, 143);
- assert.equal(await runPromise, 1);
- assert.equal(abortSeen, true);
- assert.equal(closeCount, 1);
- });
- test("closes a late async app without submitting after shutdown during initialization", async () => {
- const ctx = createContext(["--prompt", "hello"]);
- const shutdownProcess = createTestShutdownProcess("linux");
- const started = createDeferred<void>();
- const release = createDeferred<void>();
- const exited = createDeferred<number>();
- let closed = 0;
- let submitted = 0;
- const running = run(ctx, {
- ...createPromptDeps(),
- shutdownProcess,
- exitProcess: (code) => exited.resolve(code),
- createZCodeApp: async () => {
- started.resolve();
- await release.promise;
- return {
- close: async () => {
- closed += 1;
- },
- submitPrompt: async () => {
- submitted += 1;
- throw new Error("must not submit");
- },
- runtime: {},
- sessionId: "late-app",
- traceId: "late-trace",
- } as never;
- },
- });
- await started.promise;
- shutdownProcess.emitSignal("SIGTERM");
- assert.equal(await exited.promise, 143);
- release.resolve();
- assert.equal(await running, 1);
- assert.equal(submitted, 0);
- assert.equal(closed, 1);
- });
- test("TUI closes an app that finishes initialization after handler shutdown", async () => {
- const started = createDeferred<void>();
- const release = createDeferred<void>();
- let closed = 0;
- const handler = createTuiSubmitPrompt(
- {
- ...createPromptDeps(),
- createZCodeApp: async () => {
- started.resolve();
- await release.promise;
- return {
- close: async () => {
- closed += 1;
- },
- getModel: () => "model",
- } as never;
- },
- },
- {},
- "test",
- );
- const pending = handler.getSessionMetadata!();
- // 立即注册 rejection observer,避免关闭与迟到返回之间产生未处理拒绝。
- const rejected = assert.rejects(pending, /closed/);
- await started.promise;
- await handler.close?.();
- release.resolve();
- await rejected;
- assert.equal(closed, 1);
- });
- test("rejects headless goal replacement without explicit target-replace", async () => {
- const ctx = createContext(["--target", "New target"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () =>
- ({
- clearTarget: async () => true,
- continueActiveTarget: async () => null,
- readTarget: async () => ({
- objective: "Existing target",
- sessionID: "session-test",
- status: "active",
- targetID: "target-existing",
- time: { created: 1, updated: 1 },
- }),
- sessionId: "session-test",
- setTarget: async () => {
- throw new Error("target should not be replaced");
- },
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- updateTargetStatus: async () => null,
- }) as never,
- });
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /A goal already exists/);
- assert.match(
- ctx.stderr.output(),
- /Headless goal commands cannot open an interactive replacement picker/,
- );
- assert.equal(ctx.stdout.output(), "");
- });
- test("replaces an existing goal for headless goal commands when target-replace is set", async () => {
- const ctx = createContext(["--target", "New target", "--target-replace"]);
- let replacedObjective = "";
- let continued = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () =>
- ({
- clearTarget: async () => true,
- continueActiveTarget: async () => {
- continued = true;
- return {
- projection: fakeProjection as never,
- response: "continued target",
- traceId: "trace-target-continue" as never,
- turnId: "turn-target-continue" as never,
- };
- },
- readTarget: async () => ({
- objective: "Existing target",
- sessionID: "session-test",
- status: "active",
- targetID: "target-existing",
- time: { created: 1, updated: 1 },
- }),
- sessionId: "session-test",
- setTarget: async (input: { objective: string; status?: string }) => {
- replacedObjective = `${input.objective}:${input.status ?? "missing"}`;
- return {
- objective: input.objective,
- sessionID: "session-test",
- status: input.status ?? "active",
- targetID: "target-replaced",
- time: { created: 1, updated: 1 },
- };
- },
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- updateTargetStatus: async () => null,
- }) as never,
- });
- assert.equal(exitCode, 0);
- assert.equal(replacedObjective, "New target:active");
- assert.equal(continued, true);
- assert.equal(ctx.stdout.output(), "continued target\n");
- assert.equal(ctx.stderr.output(), "");
- });
- test("runs slash goal commands directly in headless prompt mode", async () => {
- const ctx = createContext(["--prompt", "/goal Ship target MVP"]);
- let continued = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () =>
- ({
- clearTarget: async () => false,
- continueActiveTarget: async () => {
- continued = true;
- return {
- projection: fakeProjection as never,
- response: "continued target",
- traceId: "trace-target-continue" as never,
- turnId: "turn-target-continue" as never,
- };
- },
- readTarget: async () => null,
- sessionId: "session-target-continue",
- setTarget: async ({ objective, status = "active" }) => ({
- objective,
- sessionID: "session-target-continue",
- status,
- targetID: "target-continue",
- time: { created: 1, updated: 1 },
- }),
- traceId: "trace-target-continue",
- runtime: {} as never,
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- updateTargetStatus: async () => null,
- }) as never,
- });
- assert.equal(exitCode, 0);
- assert.equal(continued, true);
- assert.equal(ctx.stdout.output(), "continued target\n");
- assert.equal(ctx.stderr.output(), "");
- });
- test("rejects interactive goal replacement prompts in headless mode", async () => {
- const ctx = createContext(["--prompt", "/goal New target"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () =>
- ({
- clearTarget: async () => true,
- continueActiveTarget: async () => null,
- readTarget: async () => ({
- objective: "Existing target",
- sessionID: "session-target-existing",
- status: "active",
- targetID: "target-existing",
- time: { created: 1, updated: 1 },
- }),
- sessionId: "session-target-existing",
- setTarget: async () => {
- throw new Error("replace should not happen without explicit confirmation");
- },
- traceId: "trace-target-existing",
- runtime: {} as never,
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- updateTargetStatus: async () => null,
- }) as never,
- });
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /A goal already exists/);
- assert.match(
- ctx.stderr.output(),
- /Headless goal commands cannot open an interactive replacement picker/,
- );
- assert.equal(ctx.stdout.output(), "");
- });
- test("defaults headless prompts to yolo mode", async () => {
- const ctx = createContext(["--prompt", "hello default mode"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.equal(options?.runtimeConfig?.mode, "yolo");
- assert.equal(options?.runtimeConfig?.presentationSurface, "terminal");
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello default mode\n");
- });
- test("passes the Desktop presentation surface into headless runtime config", async () => {
- const ctx = createContext(["--surface", "desktop", "--prompt", "hello desktop"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.equal(options?.runtimeConfig?.presentationSurface, "zcode_desktop");
- assert.equal(options?.sourceTitle, undefined);
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello desktop\n");
- });
- test("rejects unsupported presentation surfaces", async () => {
- const ctx = createContext(["--surface", "web", "--prompt", "hello"]);
- const exitCode = await run(ctx, createPromptDeps());
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /Unsupported --surface value: web/);
- assert.equal(ctx.stdout.output(), "");
- });
- test("rejects the presentation surface option outside supported commands", async () => {
- const ctx = createContext(["doctor", "--surface", "desktop"]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 1);
- assert.match(
- ctx.stderr.output(),
- /--surface can only be used with --prompt, --target, app-server, or agent-server/,
- );
- assert.equal(ctx.stdout.output(), "");
- });
- test("passes prompt mode override into runtime config", async () => {
- const ctx = createContext(["--prompt", "hello mode", "--mode", "plan"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.equal(options?.runtimeConfig?.mode, "plan");
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello mode\n");
- });
- test("passes force MCS into headless runtime config", async () => {
- const ctx = createContext(["--force-mcs", "--prompt", "hello force mcs"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.deepEqual((options?.runtimeConfig as any)?.midConversationSystem, {
- mode: "force",
- });
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello force mcs\n");
- });
- test("passes --disallowedTools into headless runtime config", async () => {
- const ctx = createContext([
- "--prompt",
- "hello disallowed tools",
- "--disallowedTools",
- "Bash(git *) Edit,web_search",
- ]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.deepEqual(options?.runtimeConfig?.toolDisallowlist, [
- "Bash(git *)",
- "Edit",
- "WebSearch",
- ]);
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello disallowed tools\n");
- });
- test("supports the prompt short alias with --disallowedTools", async () => {
- const ctx = createContext(["-p", "hello short prompt", "--disallowedTools", "Bash"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.deepEqual(options?.runtimeConfig?.toolDisallowlist, ["Bash"]);
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello short prompt\n");
- });
- test("supports dashed disallowed tools alias before the prompt option", async () => {
- const ctx = createContext([
- "--disallowed-tools",
- "Bash",
- "Edit",
- "--prompt",
- "hello dashed disallowed tools",
- ]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.deepEqual(options?.runtimeConfig?.toolDisallowlist, ["Bash", "Edit"]);
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello dashed disallowed tools\n");
- });
- test("passes edit prompt mode override into runtime config", async () => {
- const ctx = createContext(["--prompt", "hello edit mode", "--mode", "edit"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.equal(options?.runtimeConfig?.mode, "edit");
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello edit mode\n");
- });
- test("passes the CLI cwd into prompt runtime config", async () => {
- const ctx = createContext(["--prompt", "hello cwd"]);
- const cwd = join(tmpdir(), "zcode-cli-cwd-test");
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- cwd: () => cwd,
- createZCodeApp: (options) => {
- assert.equal(options?.runtimeConfig?.workingDirectory, cwd);
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- loadDotenv: (options) => {
- assert.equal(options.cwd, cwd);
- return {
- keys: [],
- loaded: false,
- };
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello cwd\n");
- });
- test("passes explicit resume session into prompt app creation", async () => {
- const ctx = createContext(["--resume", "sess_existing", "--prompt", "hello resume"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.equal(options?.resume, true);
- assert.equal(options?.sessionId, "sess_existing");
- return {
- sessionId: "sess_existing",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello resume\n");
- });
- test("resolves latest session for continue prompts", async () => {
- const ctx = createContext(["--continue", "--prompt", "hello continue"]);
- const cwd = join(tmpdir(), "zcode-cli-continue-test");
- let resolved = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- cwd: () => cwd,
- resolveLatestSession: async (options) => {
- resolved = true;
- assert.equal(options.directory, cwd);
- return { id: "sess_latest" } as never;
- },
- createZCodeApp: (options) => {
- assert.equal(options?.resume, true);
- assert.equal(options?.sessionId, "sess_latest");
- return {
- sessionId: "sess_latest",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(resolved, true);
- assert.equal(ctx.stdout.output(), "hello continue\n");
- });
- test("rejects resume and continue together", async () => {
- const ctx = createContext(["--resume", "sess_existing", "--continue", "--prompt", "hello"]);
- const exitCode = await run(ctx, createPromptDeps());
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /--resume and --continue cannot be used together/);
- assert.equal(ctx.stdout.output(), "");
- });
- test("rejects unsupported prompt mode", async () => {
- const ctx = createContext(["--prompt", "hello mode", "--mode", "auto"]);
- const exitCode = await run(ctx, createPromptDeps());
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /Unsupported --mode value/);
- assert.equal(ctx.stdout.output(), "");
- });
- test("prints JSON for prompt command", async () => {
- const ctx = createContext(["--prompt", "hello json", "--json"]);
- const exitCode = await run(ctx, createPromptDeps());
- const payload = JSON.parse(ctx.stdout.output()) as {
- response: string;
- traceId: string;
- eventCount: number;
- projection: { turnCount: number };
- };
- assert.equal(exitCode, 0);
- assert.equal(payload.response, "hello json");
- assert.equal(payload.traceId, "trace-test");
- assert.equal(payload.eventCount, 2);
- assert.equal(payload.projection.turnCount, 1);
- });
- test("prints provider token usage in prompt JSON", async () => {
- const ctx = createContext(["--prompt", "hello usage", "--json"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () =>
- ({
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async () => ({
- events: [],
- projection: fakeProjection as never,
- response: "done",
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- usage: {
- source: "provider",
- modelRequestCount: 1,
- inputTokens: 11,
- outputTokens: 3,
- totalTokens: 14,
- cacheReadTokens: 5,
- cacheWriteTokens: 0,
- reasoningTokens: 1,
- },
- }),
- }) as never,
- });
- const payload = JSON.parse(ctx.stdout.output()) as {
- usage: { source: string; totalTokens: number; cacheReadTokens: number };
- };
- assert.equal(exitCode, 0);
- assert.equal(payload.usage.source, "provider");
- assert.equal(payload.usage.totalTokens, 14);
- assert.equal(payload.usage.cacheReadTokens, 5);
- });
- test("rejects empty prompt text", async () => {
- const ctx = createContext(["--prompt", " "]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /--prompt requires non-empty text/);
- assert.equal(ctx.stdout.output(), "");
- });
- test("prints JSON for doctor command", async () => {
- const ctx = createContext(["doctor", "--json"]);
- const originalTitle = process.title;
- try {
- setCliProcessTitle();
- const exitCode = await run(ctx);
- const payload = JSON.parse(ctx.stdout.output()) as {
- cli: { name: string; processName: string };
- packaging: { default: string; sea: string };
- runtime: { node: string; processTitle: string };
- };
- assert.equal(exitCode, 0);
- assert.equal(payload.cli.name, "zcode");
- assert.equal(payload.cli.processName, CLI_PROCESS_NAME);
- assert.equal(payload.runtime.node, process.version);
- assert.equal(payload.runtime.processTitle, CLI_PROCESS_NAME);
- assert.equal(payload.packaging.default, "node-bundle");
- assert.equal(payload.packaging.sea, "optional");
- } finally {
- process.title = originalTitle;
- }
- });
- test("prints human doctor details with verbose diagnostics", async () => {
- const ctx = createContext(["doctor", "--verbose", "--no-color"]);
- const originalTitle = process.title;
- try {
- setCliProcessTitle();
- const exitCode = await run(ctx);
- const output = ctx.stdout.output();
- assert.equal(exitCode, 0);
- assert.match(output, /zcode doctor/);
- assert.match(output, /process: zcode-cli/);
- assert.match(output, /execPath:/);
- assert.match(output, /cwd:/);
- assert.equal(output.includes("["), false);
- } finally {
- process.title = originalTitle;
- }
- });
- test("rejects force MCS outside prompt, target, or TUI commands", async () => {
- const ctx = createContext(["doctor", "--force-mcs"]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /--force-mcs can only be used with --prompt, --target, or tui/);
- assert.equal(ctx.stdout.output(), "");
- });
- test("dispatches app-server stdio command to the ZCode Protocol runner without production dotenv", async () => {
- const ctx = createContext(["app-server", "--stdio"]);
- let called = false;
- let dotenvCalls = 0;
- const exitCode = await run(ctx, {
- cwd: () => "/workspace",
- loadDotenv: () => {
- dotenvCalls += 1;
- return {
- keys: [],
- loaded: false,
- };
- },
- runZCodeProtocolAgent: async (options) => {
- called = true;
- assert.equal(options?.cwd, "/workspace");
- assert.equal(options?.input, ctx.stdin);
- assert.equal(options?.output, ctx.stdout);
- assert.equal(options?.version, "0.0.0");
- assert.equal(options?.presentationSurface, "terminal");
- assert.equal(options?.env?.[ZCODE_RUNTIME_ENV_KEY], "production");
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(called, true);
- assert.equal(dotenvCalls, 0);
- assert.equal(ctx.stderr.output(), "");
- });
- test("redirects every protocol console level to stderr and restores the global console", () => {
- const originalConsole = globalThis.console;
- const stderr = new PassThrough();
- let stderrOutput = "";
- stderr.setEncoding("utf8");
- stderr.on("data", (chunk: string) => {
- stderrOutput += chunk;
- });
- const restore = installStderrConsoleBoundary(stderr);
- try {
- console.debug("protocol debug must not reach stdout");
- console.log("protocol log must not reach stdout");
- console.info("protocol info must not reach stdout");
- console.warn("protocol warning must not reach stdout");
- console.error("protocol error must not reach stdout");
- assert.match(stderrOutput, /protocol debug must not reach stdout/u);
- assert.match(stderrOutput, /protocol log must not reach stdout/u);
- assert.match(stderrOutput, /protocol info must not reach stdout/u);
- assert.match(stderrOutput, /protocol warning must not reach stdout/u);
- assert.match(stderrOutput, /protocol error must not reach stdout/u);
- } finally {
- restore();
- stderr.destroy();
- }
- assert.equal(globalThis.console, originalConsole);
- });
- test("passes the Desktop presentation surface to the protocol app server", async () => {
- const ctx = createContext(["app-server", "--stdio", "--surface", "desktop"]);
- let called = false;
- const exitCode = await run(ctx, {
- runZCodeProtocolAgent: async (options) => {
- called = true;
- assert.equal(options?.presentationSurface, "zcode_desktop");
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(called, true);
- assert.equal(ctx.stderr.output(), "");
- });
- test("rejects force MCS before starting the protocol app server", async () => {
- const ctx = createContext(["app-server", "--stdio", "--force-mcs"]);
- let called = false;
- const exitCode = await run(ctx, {
- runZCodeProtocolAgent: async () => {
- called = true;
- },
- });
- assert.equal(exitCode, 1);
- assert.equal(called, false);
- assert.match(ctx.stderr.output(), /--force-mcs can only be used with --prompt, --target, or tui/);
- assert.equal(ctx.stdout.output(), "");
- });
- test("loads dotenv for app-server only in development runtime", async () => {
- const ctx = createContext(["app-server", "--stdio"]);
- let called = false;
- let dotenvCalls = 0;
- const exitCode = await run(ctx, {
- cwd: () => "/workspace",
- env: {
- [ZCODE_RUNTIME_ENV_KEY]: "development",
- },
- loadDotenv: (options) => {
- dotenvCalls += 1;
- assert.equal(options?.cwd, "/workspace");
- if (options?.env) {
- options.env.ZCODE_API_KEY = "loaded-for-dev-protocol";
- }
- return {
- keys: ["ZCODE_API_KEY"],
- loaded: true,
- path: "/workspace/.env",
- };
- },
- runZCodeProtocolAgent: async (options) => {
- called = true;
- assert.equal(options?.cwd, "/workspace");
- assert.equal(options?.env?.[ZCODE_RUNTIME_ENV_KEY], "development");
- assert.equal(options?.env?.ZCODE_API_KEY, "loaded-for-dev-protocol");
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(called, true);
- assert.equal(dotenvCalls, 1);
- assert.equal(ctx.stderr.output(), "");
- });
- test("rejects the removed acp command", async () => {
- const ctx = createContext(["acp"]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /Unknown command: acp/);
- assert.equal(ctx.stdout.output(), "");
- });
- test("returns an error for unknown commands", async () => {
- const ctx = createContext(["missing"]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /Unknown command: missing/);
- assert.match(ctx.stderr.output(), /Usage:/);
- assert.equal(ctx.stdout.output(), "");
- });
- const PLUGINS_TEST_OPTIONS = {
- force: false,
- json: false,
- noColor: false,
- verbose: false,
- } as const;
- test("plugins uninstall refuses on a non-TTY without --force", async () => {
- const ctx = createContext(["plugins", "uninstall", "hello@market"]);
- let called = false;
- const exitCode = await runPluginsCommand(
- ctx,
- { ...PLUGINS_TEST_OPTIONS },
- {
- cwd: () => process.cwd(),
- uninstallPlugin: async () => {
- called = true;
- return null;
- },
- },
- ["uninstall", "hello@market"],
- );
- assert.equal(called, false);
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /Refusing to uninstall hello@market without confirmation/);
- });
- test("plugins uninstall proceeds with --force and reports the removed plugin", async () => {
- const ctx = createContext(["plugins", "uninstall", "hello@market", "--force"]);
- let uninstalledId = "";
- const exitCode = await runPluginsCommand(
- ctx,
- { ...PLUGINS_TEST_OPTIONS, force: true },
- {
- cwd: () => process.cwd(),
- uninstallPlugin: async (options) => {
- uninstalledId = options.pluginId ?? "";
- return {
- id: options.pluginId ?? "",
- name: "hello",
- marketplace: "market",
- enabled: false,
- scope: "user",
- };
- },
- },
- ["uninstall", "hello@market"],
- );
- assert.equal(uninstalledId, "hello@market");
- assert.equal(exitCode, 0);
- assert.match(ctx.stdout.output(), /Uninstalled plugin hello@market/);
- });
- test("plugins uninstall reports a not-installed plugin as a non-zero no-op", async () => {
- const ctx = createContext(["plugins", "uninstall", "missing@market", "--force"]);
- const exitCode = await runPluginsCommand(
- ctx,
- { ...PLUGINS_TEST_OPTIONS, force: true },
- {
- cwd: () => process.cwd(),
- uninstallPlugin: async () => null,
- },
- ["uninstall", "missing@market"],
- );
- assert.equal(exitCode, 1);
- assert.match(ctx.stdout.output(), /Plugin not installed: missing@market/);
- });
- test("returns an error for unknown options", async () => {
- const ctx = createContext(["--wat"]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 1);
- assert.match(ctx.stderr.output(), /Unknown option/);
- assert.match(ctx.stderr.output(), /Usage:/);
- });
- test("rejects unsupported Browser Use modes and invalid CLI surfaces", async () => {
- const unsupported = createContext(["--browser-use=extension", "--prompt", "test"]);
- assert.equal(await run(unsupported, createPromptDeps()), 1);
- assert.match(unsupported.stderr.output(), /Unsupported --browser-use value/u);
- const executableOnly = createContext([
- "--browser-executable",
- process.execPath,
- "--prompt",
- "test",
- ]);
- assert.equal(await run(executableOnly, createPromptDeps()), 1);
- assert.match(executableOnly.stderr.output(), /requires --browser-use=headless/u);
- const appServer = createContext(["--browser-use=headless", "app-server", "--stdio"]);
- assert.equal(await run(appServer, createPromptDeps()), 1);
- assert.match(appServer.stderr.output(), /can only be used with --prompt, --target, or tui/u);
- });
- test("loads the pinned Playwright runtime from a standard Node CLI install", async () => {
- const playwright = await loadCliPlaywrightChromium();
- assert.equal(typeof playwright.chromium.launch, "function");
- assert.match(playwright.chromium.executablePath(), /chromium|chrome/u);
- });
- test("injects and closes the managed CDP port for headless prompt", async () => {
- const ctx = createContext(["--browser-use=headless", "--prompt", "browser smoke"]);
- const browserControlPort = {} as never;
- let runtimeClosed = 0;
- let appClosed = 0;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createManagedCdpBrowserRuntime: (options) => {
- assert.equal(options?.executablePath, undefined);
- return {
- browserControlPort,
- close: async () => {
- runtimeClosed += 1;
- },
- };
- },
- createZCodeApp: (options) => {
- assert.equal(options?.browserControlPort, browserControlPort);
- return {
- close: async () => {
- appClosed += 1;
- },
- sessionId: "session-browser-headless",
- traceId: "trace-browser-headless",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-browser-headless" as never,
- turnId: "turn-browser-headless" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(appClosed, 1);
- assert.equal(runtimeClosed, 1);
- assert.equal(ctx.stdout.output(), "browser smoke\n");
- });
- test("continues managed browser cleanup when app close never settles", async () => {
- const ctx = createContext(["--browser-use=headless", "--prompt", "browser shutdown"]);
- let appCloseCount = 0;
- let browserCloseCount = 0;
- const startedAt = Date.now();
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createManagedCdpBrowserRuntime: () => ({
- browserControlPort: {} as never,
- close: async () => {
- browserCloseCount += 1;
- },
- }),
- createZCodeApp: () =>
- ({
- close: async () => {
- appCloseCount += 1;
- await new Promise<void>(() => undefined);
- },
- runtime: {} as never,
- sessionId: "session-browser-close-timeout",
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-browser-close-timeout" as never,
- turnId: "turn-browser-close-timeout" as never,
- }),
- traceId: "trace-browser-close-timeout",
- }) as never,
- shutdownCleanupTimeoutMs: 20,
- });
- assert.equal(exitCode, 0);
- assert.equal(appCloseCount, 1);
- assert.equal(browserCloseCount, 1);
- assert.ok(Date.now() - startedAt < 200);
- });
- test("returns from a headless prompt when managed browser close never settles", async () => {
- const ctx = createContext(["--browser-use=headless", "--prompt", "browser shutdown"]);
- let appCloseCount = 0;
- let browserCloseCount = 0;
- const startedAt = Date.now();
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createManagedCdpBrowserRuntime: () => ({
- browserControlPort: {} as never,
- close: async () => {
- browserCloseCount += 1;
- await new Promise<void>(() => undefined);
- },
- }),
- createZCodeApp: () =>
- ({
- close: async () => {
- appCloseCount += 1;
- },
- runtime: {} as never,
- sessionId: "session-browser-runtime-close-timeout",
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-browser-runtime-close-timeout" as never,
- turnId: "turn-browser-runtime-close-timeout" as never,
- }),
- traceId: "trace-browser-runtime-close-timeout",
- }) as never,
- shutdownCleanupTimeoutMs: 20,
- });
- assert.equal(exitCode, 0);
- assert.equal(appCloseCount, 1);
- assert.equal(browserCloseCount, 1);
- assert.ok(Date.now() - startedAt < 200);
- });
- test("forces the existing exit code when the CLI watchdog deadline is reached", async () => {
- const exitDeferred = createDeferred<number>();
- scheduleCliExitWatchdog({
- exitCode: 7,
- exitProcess: (code) => exitDeferred.resolve(code),
- timeoutMs: 20,
- });
- assert.equal(await exitDeferred.promise, 7);
- });
- test("injects one managed CDP runtime into TUI app lifecycle and closes it", async () => {
- const ctx = createContext(["--browser-use=headless", "tui"]);
- const browserControlPort = {} as never;
- let runtimeClosed = 0;
- let appClosed = 0;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createManagedCdpBrowserRuntime: () => ({
- browserControlPort,
- close: async () => {
- runtimeClosed += 1;
- },
- }),
- createZCodeApp: (options) => {
- assert.equal(options?.browserControlPort, browserControlPort);
- return {
- close: async () => {
- appClosed += 1;
- },
- getLocale: () => "en-US",
- getModel: () => "openai/gpt-test",
- getThoughtLevel: () => "medium",
- runtime: {} as never,
- sessionId: "session-browser-tui",
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-browser-tui" as never,
- turnId: "turn-browser-tui" as never,
- }),
- traceId: "trace-browser-tui",
- } as never;
- },
- // 真实 TUI 在首帧后通过 loadStartupOptions 创建 App;浏览器运行时随该 App 注入和关闭。
- runTui: async (options) => {
- await loadTuiStartup(options);
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(appClosed, 1);
- assert.equal(runtimeClosed, 1);
- });
- test("reuses one process provider registry across TUI app replacements", async () => {
- const ctx = createContext(["tui"]);
- const providerRegistry = {} as never;
- const providerRuntimeHeadersPort = {} as never;
- let runtimeStarts = 0;
- let runtimeDisposals = 0;
- let selectionRepositoryDisposals = 0;
- let appCreations = 0;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- skipUserConfig: false,
- userConfigPath: "/tmp/zcode-legacy-tui.json",
- startProcessProviderRegistryRuntime: async (_env, options) => {
- runtimeStarts += 1;
- // 刷新回调是当前 CLI 装配的一部分,仍验证唯一 Runtime 和旧配置迁移路径。
- assert.equal(typeof options?.standalone?.onBuiltinRefreshError, "function");
- assert.equal(typeof options?.standalone?.onBuiltinRefreshResult, "function");
- assert.deepEqual(
- {
- standalone: {
- legacyCliUserConfigFilePath: options?.standalone?.legacyCliUserConfigFilePath,
- },
- },
- {
- standalone: {
- legacyCliUserConfigFilePath: "/tmp/zcode-legacy-tui.json",
- },
- },
- );
- return {
- accountSource: {} as never,
- dispose: () => {
- selectionRepositoryDisposals += 1;
- runtimeDisposals += 1;
- },
- configuredDefaultModelSelection: { providerId: "provider-a", modelId: "model-a" },
- modelSelectionConfigRepository: {
- dispose: () => {
- selectionRepositoryDisposals += 1;
- },
- read: async () => ({ providerId: "provider-a", modelId: "model-a" }),
- },
- providerRuntimeHeadersPort,
- snapshot: {} as never,
- runtime: {
- dispose: () => {
- runtimeDisposals += 1;
- },
- registryService: providerRegistry,
- },
- } as never;
- },
- createZCodeApp: (options) => {
- appCreations += 1;
- assert.equal(options?.providerRegistry, providerRegistry);
- assert.equal(options?.providerRuntimeHeadersPort, providerRuntimeHeadersPort);
- assert.deepEqual(options?.configuredDefaultModelSelection, {
- providerId: "provider-a",
- modelId: "model-a",
- });
- return {
- close: async () => {},
- getLocale: () => "en-US",
- getModel: () => "openai/gpt-test",
- getThoughtLevel: () => "medium",
- runtime: {} as never,
- sessionId: `session-registry-tui-${appCreations}`,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-registry-tui" as never,
- turnId: "turn-registry-tui" as never,
- }),
- traceId: "trace-registry-tui",
- } as never;
- },
- runTui: async (options) => {
- await loadTuiStartup(options);
- await options.submitPrompt("/new", {
- abortSignal: new AbortController().signal,
- });
- return 0;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(appCreations, 2);
- assert.equal(runtimeStarts, 1);
- assert.equal(runtimeDisposals, 1);
- assert.equal(selectionRepositoryDisposals, 1);
- });
- test("rejects an invalid explicit browser executable before creating the app", async () => {
- const ctx = createContext([
- "--browser-use=headless",
- "--browser-executable",
- "/definitely/missing/zcode-chromium",
- "--prompt",
- "browser smoke",
- ]);
- let appCreated = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: () => {
- appCreated = true;
- throw new Error("app must not be created");
- },
- });
- assert.equal(exitCode, 1);
- assert.equal(appCreated, false);
- assert.match(ctx.stderr.output(), /missing or not executable/u);
- });
- test("rejects tui when stdio is not interactive", async () => {
- const ctx = createContext(["tui"]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 1);
- // Locale can come from user config; the invariant is that non-interactive TUI exits.
- assert.match(ctx.stderr.output(), /TUI requires an interactive terminal|TUI 需要交互式终端/);
- });
- const createRuntimeEvent = (type: string, payload: Record<string, unknown>): never =>
- ({
- id: `event-${type}`,
- payload,
- sequenceNumber: 0,
- sessionId: "session-test",
- timestamp: new Date(),
- traceId: "trace-test",
- type,
- }) as never;
- const createPermissionRequest = (): never =>
- ({
- input: { command: "touch demo.txt" },
- mode: "build",
- reason: "Tool has side effects and requires approval",
- requestId: "perm_test",
- requestedAt: new Date(),
- riskLevel: "medium",
- ruleId: "mode.build.sideEffect",
- sessionId: "session-test",
- sideEffectScope: "workspace",
- toolCallId: "tool_bash",
- toolName: "Bash",
- traceId: "trace-test",
- turnId: "turn-test",
- }) as never;
- function createDeferred<T>() {
- let resolve: (value: T) => void = () => undefined;
- let reject: (reason?: unknown) => void = () => undefined;
- const promise = new Promise<T>((promiseResolve, promiseReject) => {
- resolve = promiseResolve;
- reject = promiseReject;
- });
- return { promise, reject, resolve };
- }
- function createTestShutdownProcess(platform: NodeJS.Platform): CliShutdownProcess & {
- emitSignal(signal: NodeJS.Signals): void;
- } {
- const listeners = new Map<NodeJS.Signals, Set<() => void>>();
- const target = {
- platform,
- off(signal: NodeJS.Signals, listener: () => void) {
- listeners.get(signal)?.delete(listener);
- return target;
- },
- once(signal: NodeJS.Signals, listener: () => void) {
- const signalListeners = listeners.get(signal) ?? new Set<() => void>();
- signalListeners.add(listener);
- listeners.set(signal, signalListeners);
- return target;
- },
- emitSignal(signal: NodeJS.Signals) {
- const signalListeners = Array.from(listeners.get(signal) ?? []);
- listeners.set(signal, new Set());
- for (const listener of signalListeners) {
- listener();
- }
- },
- };
- return target;
- }
- test("headless prompt reports exact Workspace Hook pretrust diagnostics without failing the task", async () => {
- const ctx = createContext(["--prompt", "hello"]);
- const deps = createPromptDeps();
- const exitCode = await run(ctx, {
- ...deps,
- createZCodeApp: () =>
- ({
- getModel: () => "openai/gpt-test",
- getLocale: () => "en-US",
- getThoughtLevel: () => "medium",
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async () => ({
- events: [
- {
- type: "hook_run_blocked",
- payload: {
- errorCode: "workspace_hooks_pending_trust",
- descriptor: { sourceKind: "project" },
- },
- },
- ],
- projection: fakeProjection as never,
- response: "ok",
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- }) as never,
- inspectWorkspaceHookTrust: async () => ({
- workspacePath: "/workspace",
- workspaceIdentity: "/workspace",
- bundleDigest: "b".repeat(64),
- reasonCode: "workspace_hooks_pending_trust",
- items: [
- {
- reviewItemId: "item-1",
- event: "SessionStart",
- matcher: null,
- displayCommand: "./start.sh",
- sourcePath: ".zcode/config.json",
- configuredEnabled: true,
- hookDeclarationDigest: "a".repeat(64),
- trustState: "pending_trust",
- },
- ],
- }),
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "ok\n");
- assert.match(ctx.stderr.output(), /Workspace Hooks skipped: workspace_hooks_pending_trust/);
- assert.match(ctx.stderr.output(), new RegExp("a{64}"));
- assert.match(ctx.stderr.output(), /zcode hooks trust review/);
- });
- test("headless rollout-off diagnostics preserve the runtime feature-disabled reason", async () => {
- const ctx = createContext(["--prompt", "hello"]);
- const deps = createPromptDeps();
- const exitCode = await run(ctx, {
- ...deps,
- createZCodeApp: () =>
- ({
- getModel: () => "openai/gpt-test",
- getLocale: () => "en-US",
- getThoughtLevel: () => "medium",
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async () => ({
- events: [
- {
- type: "hook_run_blocked",
- payload: {
- errorCode: "workspace_hooks_feature_disabled",
- descriptor: { sourceKind: "project" },
- },
- },
- ],
- projection: fakeProjection as never,
- response: "ok",
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- }) as never,
- inspectWorkspaceHookTrust: async () => ({
- workspacePath: "/workspace",
- workspaceIdentity: "/workspace",
- bundleDigest: "b".repeat(64),
- reasonCode: "workspace_hooks_pending_trust",
- items: [],
- }),
- });
- assert.equal(exitCode, 0);
- assert.match(ctx.stderr.output(), /Workspace Hooks skipped: workspace_hooks_feature_disabled/);
- });
- for (const mode of [undefined, "off", "auto", "local"] as const) {
- test(`passes ToolSearch mode ${mode ?? "default off"} into prompt runtime`, async () => {
- let observed = false;
- const ctx = createContext([...(mode ? ["--toolsearch-mode", mode] : []), "--prompt", "hello"]);
- const deps = createPromptDeps();
- const original = deps.createZCodeApp;
- const code = await run(ctx, {
- ...deps,
- createZCodeApp: (options) => {
- observed = true;
- assert.deepEqual((options?.runtimeConfig as any)?.toolSearch, { mode: mode ?? "off" });
- return original!(options);
- },
- });
- assert.equal(code, 0);
- assert.equal(observed, true);
- });
- }
- test("rejects invalid ToolSearch mode and removed enable flag before creating an app", async () => {
- for (const args of [
- ["--toolsearch-mode", "force"],
- ["--toolsearch-mode", "typo"],
- ["--enable-toolsearch"],
- ]) {
- let created = false;
- const ctx = createContext([...args, "-p", "hello"]);
- const code = await run(ctx, {
- createZCodeApp: () => {
- created = true;
- throw new Error("unreachable");
- },
- });
- assert.equal(code, 1);
- assert.equal(created, false);
- assert.match(ctx.stderr.output(), /toolsearch/);
- }
- });
- test("passes explicit Native ToolSearch into TUI runtime config", async () => {
- const ctx = createContext(["tui", "--toolsearch-mode", "native"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.deepEqual((options?.runtimeConfig as any)?.toolSearch, {
- mode: "native",
- });
- return {
- getLocale: () => "en-US",
- getModel: () => "openai/gpt-test",
- getThoughtLevel: () => "medium",
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- runTui: async () => 0,
- });
- assert.equal(exitCode, 0);
- });
- test("passes explicit Native ToolSearch into target runtime config", async () => {
- const ctx = createContext(["--toolsearch-mode", "native", "--target", "Ship target MVP"]);
- let continued = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.deepEqual((options?.runtimeConfig as any)?.toolSearch, {
- mode: "native",
- });
- return {
- clearTarget: async () => false,
- continueActiveTarget: async () => {
- continued = true;
- return {
- projection: fakeProjection as never,
- response: "continued target",
- traceId: "trace-target-toolsearch" as never,
- turnId: "turn-target-toolsearch" as never,
- };
- },
- readTarget: async () => null,
- sessionId: "session-target-toolsearch",
- setTarget: async ({ objective, status = "active" }) => ({
- objective,
- sessionID: "session-target-toolsearch",
- status,
- targetID: "target-toolsearch",
- time: { created: 1, updated: 1 },
- }),
- traceId: "trace-target-toolsearch",
- runtime: {} as never,
- submitPrompt: async () => {
- throw new Error("prompt should not run");
- },
- updateTargetStatus: async () => null,
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(continued, true);
- assert.equal(ctx.stdout.output(), "continued target\n");
- assert.equal(ctx.stderr.output(), "");
- });
- test("passes explicit Native ToolSearch into headless runtime config", async () => {
- const ctx = createContext(["--toolsearch-mode", "native", "--prompt", "hello enable toolsearch"]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.deepEqual((options?.runtimeConfig as any)?.toolSearch, {
- mode: "native",
- });
- return {
- sessionId: "session-test",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello enable toolsearch\n");
- });
- test("rejects explicit Native ToolSearch outside prompt, target, or TUI commands", async () => {
- const ctx = createContext(["doctor", "--toolsearch-mode", "native"]);
- const exitCode = await run(ctx);
- assert.equal(exitCode, 1);
- assert.match(
- ctx.stderr.output(),
- /--toolsearch-mode can only be used with --prompt, --target, or tui/,
- );
- assert.equal(ctx.stdout.output(), "");
- });
- test("rejects explicit Native ToolSearch before starting the protocol app server", async () => {
- const ctx = createContext(["app-server", "--stdio", "--toolsearch-mode", "native"]);
- let called = false;
- const exitCode = await run(ctx, {
- runZCodeProtocolAgent: async () => {
- called = true;
- },
- });
- assert.equal(exitCode, 1);
- assert.equal(called, false);
- assert.match(
- ctx.stderr.output(),
- /--toolsearch-mode can only be used with --prompt, --target, or tui/,
- );
- assert.equal(ctx.stdout.output(), "");
- });
- test("passes explicit Native ToolSearch and explicit resume session into prompt app creation", async () => {
- const ctx = createContext([
- "--toolsearch-mode",
- "native",
- "--resume",
- "sess_existing",
- "--prompt",
- "hello resume",
- ]);
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- createZCodeApp: (options) => {
- assert.equal(options?.resume, true);
- assert.deepEqual((options?.runtimeConfig as any)?.toolSearch, { mode: "native" });
- assert.equal(options?.sessionId, "sess_existing");
- return {
- sessionId: "sess_existing",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(ctx.stdout.output(), "hello resume\n");
- });
- test("passes explicit Native ToolSearch while resolving latest session for continue prompts", async () => {
- const ctx = createContext([
- "--toolsearch-mode",
- "native",
- "--continue",
- "--prompt",
- "hello continue",
- ]);
- const cwd = join(tmpdir(), "zcode-cli-continue-test");
- let resolved = false;
- const exitCode = await run(ctx, {
- ...createPromptDeps(),
- cwd: () => cwd,
- resolveLatestSession: async (options) => {
- resolved = true;
- assert.equal(options.directory, cwd);
- return { id: "sess_latest" } as never;
- },
- createZCodeApp: (options) => {
- assert.equal(options?.resume, true);
- assert.deepEqual((options?.runtimeConfig as any)?.toolSearch, { mode: "native" });
- assert.equal(options?.sessionId, "sess_latest");
- return {
- sessionId: "sess_latest",
- traceId: "trace-test",
- runtime: {} as never,
- submitPrompt: async (prompt: string) => ({
- events: [],
- projection: fakeProjection as never,
- response: prompt,
- traceId: "trace-test" as never,
- turnId: "turn-test" as never,
- }),
- } as never;
- },
- });
- assert.equal(exitCode, 0);
- assert.equal(resolved, true);
- assert.equal(ctx.stdout.output(), "hello continue\n");
- });
- test("storage-migrate executes only the migration port and emits strict completion JSON", async () => {
- const ctx = createContext(["storage-migrate"]);
- let calls = 0;
- const result = await run(ctx, {
- migrateStorage: async () => {
- calls++;
- return { applied: ["0020"], backupSkipped: true };
- },
- runZCodeProtocolAgent: async () => {
- throw new Error("must not start runtime");
- },
- createZCodeApp: () => {
- throw new Error("must not create session");
- },
- });
- assert.equal(result, 0);
- assert.equal(calls, 1);
- assert.deepEqual(JSON.parse(ctx.stdout.output()), {
- version: 1,
- kind: "storage-migrated",
- applied: ["0020"],
- backupSkipped: true,
- });
- });
- test("storage-migrate reports failure and rejects extra flags without starting runtime", async () => {
- for (const argv of [["storage-migrate"], ["storage-migrate", "--prompt", "do not run"]]) {
- const ctx = createContext(argv);
- const result = await run(ctx, {
- migrateStorage: async () => {
- throw new Error("checksum mismatch");
- },
- });
- assert.equal(result, 1);
- assert.equal(ctx.stdout.output(), "");
- assert.ok(ctx.stderr.output().length > 0);
- }
- });
- test("storage-migrate real entrypoint honors an isolated custom database and rejects checksum drift", async () => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-cli-storage-migration-"));
- const database = join(directory, "custom storage", "sessions.sqlite");
- const cliEntry = fileURLToPath(new URL("../src/main.ts", import.meta.url));
- const tsxLoader = fileURLToPath(import.meta.resolve("tsx"));
- const invoke = async () => {
- const child = spawn(process.execPath, ["--import", tsxLoader, cliEntry, "storage-migrate"], {
- cwd: directory,
- env: {
- ...process.env,
- HOME: directory,
- USERPROFILE: directory,
- ZCODE_DATA_BASE_DIR: directory,
- ZCODE_SESSION_DB_PATH: database,
- },
- stdio: ["ignore", "pipe", "pipe"],
- });
- let stdout = "";
- let stderr = "";
- child.stdout.on("data", (data) => {
- stdout += data;
- });
- child.stderr.on("data", (data) => {
- stderr += data;
- });
- const [code] = await once(child, "close");
- return { code, stdout, stderr };
- };
- try {
- const first = await invoke();
- assert.equal(first.code, 0, first.stderr);
- const report = JSON.parse(first.stdout);
- assert.equal(report.kind, "storage-migrated");
- assert.ok(report.applied.includes("0023_session_soft_delete_outcome"));
- const second = await invoke();
- assert.equal(second.code, 0, second.stderr);
- assert.deepEqual(JSON.parse(second.stdout).applied, []);
- const { DatabaseSync } = await import("node:sqlite");
- const db = new DatabaseSync(database);
- try {
- assert.equal(db.prepare("SELECT count(*) AS total FROM session").get()?.total, 0);
- db.prepare("UPDATE schema_migration SET checksum='invalid' WHERE id=?").run(
- "0023_session_soft_delete_outcome",
- );
- } finally {
- db.close();
- }
- const failed = await invoke();
- assert.equal(failed.code, 1);
- assert.equal(failed.stdout, "");
- assert.match(failed.stderr, /checksum mismatch/i);
- } finally {
- await rm(directory, { recursive: true, force: true });
- }
- });
|