| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046 |
- import { execFile } from "node:child_process";
- import { createHash } from "node:crypto";
- import { existsSync } from "node:fs";
- import * as asyncFs from "node:fs/promises";
- import { cp, mkdir, mkdtemp, readFile, rename, rm, symlink, writeFile } from "node:fs/promises";
- import { createServer, type Server } from "node:http";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { promisify } from "node:util";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import {
- addMarketplace,
- applyClaudePluginIcons,
- createNodePluginAdapter,
- describeMarketplacePlugin,
- discoverNodePlugins,
- enrichCachedClaudeMarketplaceIcons,
- ensureDefaultPluginMarketplaces,
- installMarketplacePlugin,
- listInstalledPluginRecords,
- loadKnownMarketplacesSync,
- loadMarketplaceManifestSync,
- parsePluginIconSources,
- uninstallMarketplacePlugin,
- updateMarketplace,
- validateMarketplacePlugin,
- validateMarketplaceSource,
- writeBundledOfficialMarketplacePartition,
- writeCdnOfficialMarketplacePartition,
- } from "../src/plugins/index.js";
- import {
- activateDirectoryAtomically,
- recoverAtomicTargetSync,
- resolveAtomicReadPath,
- replaceDirectoryAtomically,
- } from "../src/plugins/atomic-directory.js";
- import { createArchiveFetchError } from "../src/plugins/source-errors.js";
- import { enumeratePluginComponents } from "../src/plugins/plugin-components.js";
- import {
- buildGitHubArchiveUrl,
- parsePublicGitHubRepositoryUrl,
- } from "../src/plugins/github-archive-source.js";
- import {
- listInstalledPluginRecordsAsync,
- buildMarketplaceGitEnv,
- } from "../src/plugins/marketplace.js";
- import { resolveZipPluginSource } from "../src/plugins/zip-source.js";
- import {
- appendPluginSourceCleanupError,
- cleanupPluginSourceBestEffort,
- PluginMaterialCache,
- } from "../src/plugins/helpers.js";
- import { createNodeCustomCommandAdapter } from "../src/commands/index.js";
- import { createNodeSkillAdapter } from "../src/skills/index.js";
- import {
- ZCODE_AGENT_CA_CERT_ENV_KEY,
- ZCODE_HTTP_PROXY_ENV_KEY,
- ZCODE_NO_PROXY_ENV_KEY,
- ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY,
- } from "@zcode/shared";
- const execFileAsync = promisify(execFile);
- vi.mock("node:fs/promises", async (importOriginal) => {
- const actual = await importOriginal<typeof import("node:fs/promises")>();
- return {
- ...actual,
- readFile: vi.fn(actual.readFile),
- readdir: vi.fn(actual.readdir),
- lstat: vi.fn(actual.lstat),
- stat: vi.fn(actual.stat),
- realpath: vi.fn(actual.realpath),
- };
- });
- describe("NodePluginAdapter", () => {
- it("shares concurrent path checks and reuses their type in Skill scanning", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-shared-path-check-"));
- const file = join(root, "SKILL.md");
- const cache = new PluginMaterialCache();
- try {
- await writeFile(file, "# first");
- const reader = cache.reader("scope");
- vi.mocked(asyncFs.stat).mockClear();
- vi.mocked(asyncFs.realpath).mockClear();
- await Promise.all(Array.from({ length: 8 }, () => reader.observe(file)));
- expect(vi.mocked(asyncFs.stat).mock.calls.filter(([path]) => path === file)).toHaveLength(1);
- expect(vi.mocked(asyncFs.realpath).mock.calls.filter(([path]) => path === file)).toHaveLength(
- 1,
- );
- await reader.observe(root);
- vi.mocked(asyncFs.stat).mockClear();
- vi.mocked(asyncFs.lstat).mockClear();
- expect(await reader.skillFiles(root)).toEqual([file]);
- expect(asyncFs.stat).not.toHaveBeenCalled();
- expect(asyncFs.lstat).toHaveBeenCalledTimes(2);
- await writeFile(file, "# changed content");
- expect(await reader.validate()).toBe(false);
- } finally {
- cache.dispose();
- await rm(root, { recursive: true, force: true });
- }
- });
- it("uses directory absence without probing each candidate and detects later creation", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-directory-absence-"));
- const cache = new PluginMaterialCache();
- try {
- const reader = cache.reader("scope");
- await reader.directory(root);
- vi.mocked(asyncFs.stat).mockClear();
- vi.mocked(asyncFs.realpath).mockClear();
- for (const name of [
- ".mcp.json",
- "hooks/hooks.json",
- ".claude-plugin/plugin.json",
- "skills",
- "agents",
- ]) {
- const path = join(root, name);
- expect(await reader.missing(path)).toBe(true);
- await expect(reader.json(path)).rejects.toMatchObject({ code: "ENOENT" });
- }
- expect(asyncFs.stat).not.toHaveBeenCalled();
- expect(asyncFs.realpath).not.toHaveBeenCalled();
- expect(await reader.validate()).toBe(true);
- expect(vi.mocked(asyncFs.stat).mock.calls.map(([path]) => path)).toEqual([root]);
- await mkdir(join(root, "hooks"));
- await writeFile(join(root, "hooks", "hooks.json"), "{}");
- expect(await reader.validate()).toBe(false);
- const next = cache.reader("scope");
- await next.directory(root);
- expect(await next.json(join(root, "hooks", "hooks.json"))).toEqual({});
- const moved = `${root}-old`;
- try {
- await rename(root, moved);
- await mkdir(root);
- expect(await next.validate()).toBe(false);
- const replaced = cache.reader("scope");
- await replaced.directory(root);
- expect(await replaced.missing(join(root, "hooks", "hooks.json"))).toBe(true);
- } finally {
- await rm(moved, { recursive: true, force: true });
- }
- } finally {
- cache.dispose();
- await rm(root, { recursive: true, force: true });
- }
- });
- it("keeps real checks for case-equivalent and nonstandard directory entries", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-directory-case-"));
- const cache = new PluginMaterialCache();
- try {
- await mkdir(join(root, "SKILLS"));
- const reader = cache.reader("scope");
- await reader.directory(root);
- vi.mocked(asyncFs.stat).mockClear();
- await reader.directoryExists(join(root, "skills"));
- await reader.missing(join(root, "CUSTOM~1"));
- expect(vi.mocked(asyncFs.stat).mock.calls.map(([path]) => path)).toEqual([
- join(root, "skills"),
- join(root, "CUSTOM~1"),
- ]);
- await mkdir(join(root, "技能"));
- const uncertain = cache.reader("scope");
- await uncertain.directory(root);
- vi.mocked(asyncFs.stat).mockClear();
- await uncertain.missing(join(root, "hooks", "hooks.json"));
- expect(vi.mocked(asyncFs.stat).mock.calls.map(([path]) => path)).toEqual([
- join(root, "hooks", "hooks.json"),
- ]);
- } finally {
- cache.dispose();
- await rm(root, { recursive: true, force: true });
- }
- });
- it("falls back to normal discovery when the plugin root cannot be listed", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-root-listing-denied-"));
- const actual = await vi.importActual<typeof import("node:fs/promises")>("node:fs/promises");
- const adapter = createNodePluginAdapter({ storageRoot: join(root, "storage") });
- try {
- await writePluginManifest(root, { name: "listing-denied", skills: ["./skill"] });
- await mkdir(join(root, "skill"));
- await writeFile(join(root, "skill", "SKILL.md"), "---\nname: kept\n---\nbody");
- vi.mocked(asyncFs.readdir).mockImplementation(async (...args) => {
- if (args[0] === root) throw Object.assign(new Error("denied"), { code: "EACCES" });
- return actual.readdir(...args);
- });
- const result = await adapter.discoverPlugins({
- config: { dirs: [root], enabled: true, enabledPlugins: {}, options: {} },
- env: {},
- storageRoot: join(root, "storage"),
- workingDirectory: root,
- });
- expect(
- result.plugins.find((plugin) => plugin.id === "listing-denied@inline")?.skillCount,
- ).toBe(1);
- expect(vi.mocked(asyncFs.readdir).mock.calls.some(([path]) => path === root)).toBe(true);
- } finally {
- vi.mocked(asyncFs.readdir).mockReset();
- adapter.dispose();
- await rm(root, { recursive: true, force: true });
- }
- });
- it.each([
- { skills: ["./entry"], expected: ["entry"] },
- { skills: "./entry", expected: ["entry"] },
- { skills: ["./entry", "./entry/child"], expected: ["child", "entry"] },
- { skills: [], expected: [] },
- { skills: "./collection", expected: ["collection-item"] },
- { skills: ["./collection", "./collection/item"], expected: ["collection-item"] },
- { skills: undefined, expected: ["default"] },
- ])("uses only declared Skill entries: $skills", async ({ skills, expected }) => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-declared-skills-"));
- const root = join(dir, "plugin");
- const storageRoot = join(dir, "storage");
- const adapter = createNodePluginAdapter({ storageRoot });
- try {
- await writePluginManifest(root, { name: "declared", skills });
- for (const [path, name] of [
- ["entry", "entry"],
- ["entry/child", "child"],
- ["collection/item", "collection-item"],
- ["skills/default", "default"],
- ]) {
- const skill = join(root, path!);
- await mkdir(skill, { recursive: true });
- await writeFile(
- join(skill, "SKILL.md"),
- `---\nname: ${name}\ndescription: demo\n---\nbody`,
- );
- }
- for (let attempt = 0; attempt < 2; attempt += 1) {
- vi.mocked(asyncFs.readdir).mockClear();
- const outcome = await adapter.discoverPlugins({
- config: { dirs: [root], enabled: true, enabledPlugins: {}, options: {} },
- env: {},
- storageRoot,
- workingDirectory: dir,
- });
- const plugin = outcome.plugins.find((item) => item.id === "declared@inline");
- expect(plugin?.skillCount).toBe(expected.length);
- expect(componentNames(plugin?.components ?? [], "skill").sort()).toEqual(expected);
- const standalone = await enumeratePluginComponents(root, { name: "declared", skills });
- expect(componentNames(standalone, "skill").sort()).toEqual(expected);
- const runtime = await createNodeSkillAdapter().discoverSkills({
- roots: outcome.skillRoots,
- workingDirectory: dir,
- });
- expect(runtime.skills.map((skill) => skill.name).sort()).toEqual(expected);
- // 声明单技能后,不应再枚举附属目录;未声明默认根也不应被补扫。
- const reads = vi.mocked(asyncFs.readdir).mock.calls.map(([path]) => String(path));
- expect(reads).not.toContain(join(root, "entry"));
- if (skills !== undefined) expect(reads).not.toContain(join(root, "skills"));
- }
- } finally {
- adapter.dispose();
- await rm(dir, { recursive: true, force: true });
- }
- });
- it("shares each Skill root scan across diagnostics, counts and component metadata", async () => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-plugin-scan-reuse-"));
- const root = join(directory, "plugin");
- await writePluginManifest(root, { name: "scan-reuse", skills: ["./skills"] });
- const files = ["first", "second"].map((name) => join(root, "skills", name, "SKILL.md"));
- for (const path of files) {
- await mkdir(join(path, ".."), { recursive: true });
- await writeFile(path, "---\nname: duplicate-name\n---\nbody");
- }
- const adapter = createNodePluginAdapter({ storageRoot: join(directory, "storage") });
- const request = {
- storageRoot: join(directory, "storage"),
- workingDirectory: directory,
- config: {
- dirs: [root],
- enabled: true,
- enabledPlugins: {},
- options: {},
- suppressedBuiltins: [],
- },
- };
- try {
- for (let query = 0; query < 2; query += 1) {
- vi.mocked(asyncFs.lstat).mockClear();
- const result = await adapter.discoverPlugins(request);
- expect(result.plugins[0]?.skillCount).toBe(2);
- expect(
- result.plugins[0]?.components.find((group) => group.kind === "skill")?.items,
- ).toEqual([{ name: "duplicate-name" }]);
- for (const file of files)
- expect(
- vi.mocked(asyncFs.lstat).mock.calls.filter(([path]) => path === file),
- ).toHaveLength(1);
- }
- } finally {
- adapter.dispose();
- await rm(directory, { recursive: true, force: true });
- }
- });
- it("avoids reads for missing materials and detects later creation", async () => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-plugin-missing-material-"));
- const path = join(directory, "optional.json");
- const parentFile = join(directory, "not-a-directory");
- await writeFile(parentFile, "file");
- const nested = join(parentFile, "child.json");
- const cache = new PluginMaterialCache();
- try {
- const reader = cache.reader("scope");
- vi.mocked(asyncFs.readFile).mockClear();
- for (let i = 0; i < 2; i += 1) {
- await expect(reader.json(path)).rejects.toMatchObject({ code: "ENOENT" });
- await expect(reader.json(nested)).rejects.toMatchObject({ code: "ENOTDIR" });
- }
- expect(
- vi.mocked(asyncFs.readFile).mock.calls.filter(([file]) => file === path || file === nested),
- ).toHaveLength(0);
- expect(await reader.validate()).toBe(true);
- await writeFile(path, '{"created":true}');
- expect(await reader.validate()).toBe(false);
- expect(await cache.reader("scope").json(path)).toEqual({ created: true });
- } finally {
- cache.dispose();
- await rm(directory, { recursive: true, force: true });
- }
- });
- it("reads component Markdown concurrently with a four-read owner limit and stable name precedence", async () => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-plugin-parallel-markdown-"));
- const root = join(directory, "plugin");
- const commandRoot = join(root, "commands");
- await mkdir(commandRoot, { recursive: true });
- for (let i = 0; i < 8; i += 1)
- await writeFile(
- join(commandRoot, `${i}.md`),
- `---\nname: ${i < 2 ? "duplicate" : `command-${i}`}\ndescription: file-${i}\n---\nbody`,
- );
- const actual = await vi.importActual<typeof import("node:fs/promises")>("node:fs/promises");
- const first = Promise.withResolvers<void>();
- const rest = Promise.withResolvers<void>();
- let active = 0;
- let peak = 0;
- let completed = 0;
- vi.mocked(asyncFs.readFile).mockImplementation(async (...args) => {
- if (!String(args[0]).startsWith(commandRoot)) return actual.readFile(...args);
- active += 1;
- peak = Math.max(peak, active);
- try {
- await (String(args[0]).endsWith("0.md") ? first.promise : rest.promise);
- return await actual.readFile(...args);
- } finally {
- active -= 1;
- completed += 1;
- }
- });
- const cache = new PluginMaterialCache();
- const reads = ["a", "b"].map((scope) =>
- enumeratePluginComponents(root, null, {
- loaded: {
- id: "parallel@inline",
- manifest: { name: "parallel" },
- manifestPath: join(root, ".zcode-plugin/plugin.json"),
- rootPath: root,
- marketplace: "inline",
- source: "inline",
- materials: cache.reader(scope),
- },
- }),
- );
- const done = Promise.all(reads);
- try {
- await vi.waitFor(() => expect(active).toBe(4));
- expect(peak).toBe(4);
- rest.resolve();
- await vi.waitFor(() => expect(completed).toBeGreaterThan(0));
- first.resolve();
- const results = await done;
- expect(peak).toBe(4);
- expect(results[0]).toEqual(results[1]);
- expect(results[0].find((group) => group.kind === "command")?.items).toEqual([
- { name: "duplicate", description: "file-0" },
- ...[2, 3, 4, 5, 6, 7].map((i) => ({ name: `command-${i}`, description: `file-${i}` })),
- ]);
- } finally {
- first.resolve();
- rest.resolve();
- await Promise.allSettled(reads);
- cache.dispose();
- vi.mocked(asyncFs.readFile).mockReset();
- await rm(directory, { recursive: true, force: true });
- }
- });
- it.each(["invalidate", "dispose"] as const)(
- "does not start queued material IO after %s",
- async (action) => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-plugin-read-limit-"));
- const paths = Array.from({ length: 9 }, (_, i) => join(directory, `${i}.json`));
- for (const path of paths) await writeFile(path, "{}");
- const actual = await vi.importActual<typeof import("node:fs/promises")>("node:fs/promises");
- const release = Promise.withResolvers<void>();
- let started = 0;
- vi.mocked(asyncFs.readFile).mockImplementation(async (...args) => {
- if (paths.includes(String(args[0]))) {
- started += 1;
- await release.promise;
- }
- return actual.readFile(...args);
- });
- const cache = new PluginMaterialCache();
- const reader = cache.reader("scope");
- const done = Promise.allSettled(paths.map((path) => reader.json(path)));
- try {
- await vi.waitFor(() => expect(started).toBe(4));
- cache[action]();
- release.resolve();
- const results = await done;
- expect(started).toBe(4);
- for (const result of results) {
- expect(result.status).toBe("rejected");
- if (result.status === "rejected")
- expect(result.reason).toMatchObject(
- action === "dispose"
- ? { name: "AbortError" }
- : { code: "ZCODE_PLUGIN_SOURCES_CHANGED" },
- );
- }
- } finally {
- release.resolve();
- await done;
- cache.dispose();
- vi.mocked(asyncFs.readFile).mockReset();
- await rm(directory, { recursive: true, force: true });
- }
- },
- );
- it("shares concurrent Skill scans without sharing mutable results or link policies", async () => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-plugin-scan-policy-"));
- const root = join(directory, "real");
- await mkdir(root);
- const file = join(root, "SKILL.md");
- await writeFile(file, "# skill");
- const link = join(directory, "linked");
- await symlink(root, link, process.platform === "win32" ? "junction" : "dir");
- const cache = new PluginMaterialCache();
- try {
- const reader = cache.reader("scope");
- vi.mocked(asyncFs.lstat).mockClear();
- const [first, second] = await Promise.all([reader.skillFiles(root), reader.skillFiles(root)]);
- expect(first).toEqual([file]);
- first.length = 0;
- expect(second).toEqual([file]);
- expect(vi.mocked(asyncFs.lstat).mock.calls.filter(([path]) => path === file)).toHaveLength(1);
- expect(await reader.skillFiles(link)).toEqual([]);
- expect(await reader.skillFiles(link, true)).toEqual([join(link, "SKILL.md")]);
- cache.invalidate();
- await expect(reader.skillFiles(root)).rejects.toMatchObject({
- code: "ZCODE_PLUGIN_SOURCES_CHANGED",
- });
- } finally {
- cache.dispose();
- await rm(directory, { recursive: true, force: true });
- }
- });
- it("releases material read slots after IO and parse failures", async () => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-plugin-failed-reads-"));
- const paths = Array.from({ length: 9 }, (_, i) => join(directory, `${i}.json`));
- for (const path of paths) await writeFile(path, path === paths[4] ? "{" : "{}");
- const actual = await vi.importActual<typeof import("node:fs/promises")>("node:fs/promises");
- vi.mocked(asyncFs.readFile).mockImplementation(async (...args) => {
- if (paths.slice(0, 4).includes(String(args[0])))
- throw Object.assign(new Error("denied"), { code: "EACCES" });
- return actual.readFile(...args);
- });
- const cache = new PluginMaterialCache();
- try {
- const reader = cache.reader("scope");
- const results = await Promise.allSettled(paths.map((path) => reader.json(path)));
- expect(results.slice(0, 5).map((result) => result.status)).toEqual(Array(5).fill("rejected"));
- expect(results.slice(5).map((result) => result.status)).toEqual(Array(4).fill("fulfilled"));
- vi.mocked(asyncFs.readFile).mockReset();
- await writeFile(paths[4]!, "{}");
- expect(await Promise.all(paths.map((path) => cache.reader("scope").json(path)))).toEqual(
- paths.map(() => ({})),
- );
- } finally {
- cache.dispose();
- vi.mocked(asyncFs.readFile).mockReset();
- await rm(directory, { recursive: true, force: true });
- }
- });
- it("reuses parsed materials across sequential queries but reprojects configuration and isolates scopes", async () => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-plugin-material-reuse-"));
- const root = join(directory, "plugin");
- const storageRoot = join(directory, "storage");
- await writePluginManifest(root, { name: "material-fixture", version: "1.0.0" });
- const skillFile = join(root, "skills", "one", "SKILL.md");
- await mkdir(join(root, "skills", "one"), { recursive: true });
- await writeFile(skillFile, "---\nname: one\ndescription: first\n---\nbody");
- const manifestPath = join(root, ".zcode-plugin", "plugin.json");
- const sources = new Set([manifestPath, skillFile]);
- const sourceReads = () =>
- vi
- .mocked(asyncFs.readFile)
- .mock.calls.filter(([path]) => typeof path === "string" && sources.has(path)).length;
- const adapter = createNodePluginAdapter({ storageRoot });
- const request = {
- storageRoot,
- workingDirectory: directory,
- workspaceIdentity: "workspace-a",
- remoteSessionId: "attachment-a",
- config: {
- dirs: [root],
- enabled: true,
- enabledPlugins: {},
- options: {},
- suppressedBuiltins: [],
- },
- };
- try {
- const first = await adapter.discoverPlugins(request);
- expect(first.plugins[0]?.name).toBe("material-fixture");
- first.plugins[0]!.name = "changed-by-caller";
- vi.mocked(asyncFs.readFile).mockClear();
- vi.mocked(asyncFs.readdir).mockClear();
- const second = await adapter.discoverPlugins({
- ...request,
- config: { ...request.config, enabledPlugins: { "material-fixture@inline": false } },
- });
- expect(second.plugins[0]?.name).toBe("material-fixture");
- expect(second.plugins[0]?.enabled).toBe(false);
- expect(sourceReads()).toBe(0);
- await adapter.discoverPlugins(request);
- expect(sourceReads()).toBe(0);
- expect(
- vi.mocked(asyncFs.readdir).mock.calls.filter(([path]) => path === join(root, "skills")),
- ).toEqual([]);
- await adapter.discoverPlugins({ ...request, workspaceIdentity: "workspace-b" });
- expect(sourceReads()).toBeGreaterThan(0);
- vi.mocked(asyncFs.readFile).mockClear();
- await adapter.discoverPlugins({ ...request, remoteSessionId: "attachment-b" });
- expect(sourceReads()).toBeGreaterThan(0);
- const before = await asyncFs.stat(manifestPath);
- await writePluginManifest(root, { name: "material-fixture", version: "2.0.0" });
- await asyncFs.utimes(manifestPath, before.atime, before.mtime);
- const refreshed = await adapter.discoverPlugins(request);
- expect(refreshed.plugins[0]?.version).toBe("2.0.0");
- await mkdir(join(root, "skills", "two"));
- await writeFile(join(root, "skills", "two", "SKILL.md"), "---\nname: two\n---\nbody");
- expect((await adapter.discoverPlugins(request)).plugins[0]?.skillCount).toBe(2);
- } finally {
- adapter.dispose();
- await rm(directory, { recursive: true, force: true });
- }
- });
- it("parses once and returns independent values while validating current file metadata", async () => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-plugin-parser-cache-"));
- const path = join(directory, "data.json");
- const cache = new PluginMaterialCache();
- const parse = vi.fn(JSON.parse);
- await writeFile(path, '{"value":1}');
- try {
- const first = await cache.reader("scope").parsed(path, "json", parse);
- first.value = 9;
- expect(await cache.reader("scope").parsed(path, "json", parse)).toEqual({ value: 1 });
- expect(parse).toHaveBeenCalledOnce();
- cache.invalidate();
- expect(await cache.reader("scope").parsed(path, "json", parse)).toEqual({ value: 1 });
- expect(parse).toHaveBeenCalledTimes(2);
- const inFlight = cache.reader("scope");
- await inFlight.json(path);
- await writeFile(path, '{"value":2}');
- expect(await inFlight.validate()).toBe(false);
- expect(await cache.reader("scope").json(path)).toEqual({ value: 2 });
- } finally {
- cache.dispose();
- await rm(directory, { recursive: true, force: true });
- }
- });
- it("shares a read without letting one cancelled query abort another", async () => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-plugin-shared-material-"));
- const root = join(directory, "plugin");
- await writePluginManifest(root, { name: "shared", version: "1.0.0" });
- const manifestPath = join(root, ".zcode-plugin", "plugin.json");
- const actual = await vi.importActual<typeof import("node:fs/promises")>("node:fs/promises");
- const entered = Promise.withResolvers<void>();
- const release = Promise.withResolvers<void>();
- let reads = 0;
- vi.mocked(asyncFs.readFile).mockImplementation(async (...args) => {
- const bytes = await actual.readFile(...args);
- if (args[0] === manifestPath) {
- reads += 1;
- entered.resolve();
- await release.promise;
- }
- return bytes;
- });
- const adapter = createNodePluginAdapter({ storageRoot: join(directory, "storage") });
- const request = {
- storageRoot: join(directory, "storage"),
- workingDirectory: directory,
- config: {
- dirs: [root],
- enabled: true,
- enabledPlugins: {},
- options: {},
- suppressedBuiltins: [],
- },
- };
- const controller = new AbortController();
- const first = adapter.discoverPlugins(request, { signal: controller.signal });
- const cancelled = expect(first).rejects.toMatchObject({ name: "AbortError" });
- await entered.promise;
- const second = adapter.discoverPlugins(request);
- try {
- controller.abort();
- await cancelled;
- release.resolve();
- expect((await second).plugins[0]?.name).toBe("shared");
- expect(reads).toBe(1);
- } finally {
- release.resolve();
- await Promise.allSettled([first, second]);
- adapter.dispose();
- vi.mocked(asyncFs.readFile).mockReset();
- await rm(directory, { recursive: true, force: true });
- }
- });
- it("discards material read before a source change and rejects new queries after dispose", async () => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-plugin-stale-material-"));
- const root = join(directory, "plugin");
- await writePluginManifest(root, { name: "changing", version: "1.0.0" });
- const manifestPath = join(root, ".zcode-plugin", "plugin.json");
- const actual = await vi.importActual<typeof import("node:fs/promises")>("node:fs/promises");
- const entered = Promise.withResolvers<void>();
- const release = Promise.withResolvers<void>();
- let firstRead = true;
- vi.mocked(asyncFs.readFile).mockImplementation(async (...args) => {
- const bytes = await actual.readFile(...args);
- if (args[0] === manifestPath && firstRead) {
- firstRead = false;
- entered.resolve();
- await release.promise;
- }
- return bytes;
- });
- const adapter = createNodePluginAdapter({ storageRoot: join(directory, "storage") });
- const request = {
- storageRoot: join(directory, "storage"),
- workingDirectory: directory,
- config: {
- dirs: [root],
- enabled: true,
- enabledPlugins: {},
- options: {},
- suppressedBuiltins: [],
- },
- };
- const pending = adapter.discoverPlugins(request);
- try {
- await entered.promise;
- await writePluginManifest(root, { name: "changing", version: "2.0.0" });
- release.resolve();
- expect((await pending).plugins[0]?.version).toBe("2.0.0");
- adapter.dispose();
- await expect(adapter.discoverPlugins(request)).rejects.toMatchObject({ name: "AbortError" });
- } finally {
- release.resolve();
- await Promise.allSettled([pending]);
- adapter.dispose();
- vi.mocked(asyncFs.readFile).mockReset();
- await rm(directory, { recursive: true, force: true });
- }
- });
- it.each(["dispose", "invalidate"] as const)(
- "bounds in-flight %s without publishing a stale result",
- async (action) => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-plugin-owner-change-"));
- const root = join(directory, "plugin");
- await writePluginManifest(root, { name: "changing", version: "1.0.0" });
- const manifestPath = join(root, ".zcode-plugin", "plugin.json");
- const actual = await vi.importActual<typeof import("node:fs/promises")>("node:fs/promises");
- const adapter = createNodePluginAdapter({ storageRoot: join(directory, "storage") });
- let reads = 0;
- vi.mocked(asyncFs.readFile).mockImplementation(async (...args) => {
- const bytes = await actual.readFile(...args);
- if (args[0] === manifestPath) {
- reads += 1;
- adapter[action]();
- }
- return bytes;
- });
- try {
- await expect(
- adapter.discoverPlugins({
- storageRoot: join(directory, "storage"),
- workingDirectory: directory,
- config: {
- dirs: [root],
- enabled: true,
- enabledPlugins: {},
- options: {},
- suppressedBuiltins: [],
- },
- }),
- ).rejects.toMatchObject(
- action === "dispose" ? { name: "AbortError" } : { code: "ZCODE_PLUGIN_SOURCES_CHANGED" },
- );
- expect(reads).toBe(action === "dispose" ? 1 : 3);
- } finally {
- adapter.dispose();
- vi.mocked(asyncFs.readFile).mockReset();
- await rm(directory, { recursive: true, force: true });
- }
- },
- );
- it("does not retain parse failures or materials above its byte budget", async () => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-plugin-material-budget-"));
- const path = join(directory, "source.json");
- const cache = new PluginMaterialCache();
- try {
- await writeFile(path, "{");
- await expect(cache.reader("scope").json(path)).rejects.toBeInstanceOf(SyntaxError);
- await writeFile(path, '{"ready":true}');
- expect(await cache.reader("scope").json(path)).toEqual({ ready: true });
- await writeFile(path, "x".repeat(4 * 1024 * 1024 + 1));
- const parse = vi.fn((contents: string) => contents.length);
- await cache.reader("scope").parsed(path, "text", parse);
- await cache.reader("scope").parsed(path, "text", parse);
- expect(parse).toHaveBeenCalledTimes(2);
- } finally {
- cache.dispose();
- await rm(directory, { recursive: true, force: true });
- }
- });
- it("waits for asynchronous manifest IO while unrelated work can run and avoids rewriting commands", async () => {
- const directory = await mkdtemp(join(tmpdir(), "zcode-plugin-async-discovery-"));
- const root = join(directory, "plugin");
- await writePluginManifest(root, {
- name: "async-fixture",
- version: "1.0.0",
- commands: { probe: { content: "# probe" } },
- });
- const manifestPath = join(root, ".zcode-plugin", "plugin.json");
- const entered = Promise.withResolvers<void>();
- const release = Promise.withResolvers<void>();
- const originalRead = (
- await vi.importActual<typeof import("node:fs/promises")>("node:fs/promises")
- ).readFile;
- let intercepted = false;
- const read = vi.spyOn(asyncFs, "readFile").mockImplementation(async (...args) => {
- if (args[0] === manifestPath) {
- intercepted = true;
- entered.resolve();
- await release.promise;
- }
- return originalRead(...args);
- });
- const request = {
- config: {
- dirs: [root],
- enabled: true,
- enabledPlugins: {},
- options: {},
- suppressedBuiltins: [],
- },
- storageRoot: join(directory, "storage"),
- workingDirectory: directory,
- };
- let settled = false;
- const pending = discoverNodePlugins(request).finally(() => {
- settled = true;
- });
- try {
- await Promise.race([entered.promise, pending]);
- expect(intercepted).toBe(true);
- await new Promise<void>((resolve) => setImmediate(resolve));
- expect(settled).toBe(false);
- release.resolve();
- const outcome = await pending;
- expect(outcome.plugins.map((plugin) => plugin.name)).toEqual(["async-fixture"]);
- const commandPath = join(outcome.commandRoots[0]!.path, "probe.md");
- const first = await asyncFs.stat(commandPath, { bigint: true });
- await discoverNodePlugins(request);
- const second = await asyncFs.stat(commandPath, { bigint: true });
- expect([second.ino, second.mtimeNs]).toEqual([first.ino, first.mtimeNs]);
- } finally {
- release.resolve();
- await pending;
- read.mockRestore();
- await rm(directory, { recursive: true, force: true });
- }
- });
- it("retains the first discovered agent source and rejects paths outside the plugin", async () => {
- const temporaryRoot = await mkdtemp(join(tmpdir(), "zcode-agent-component-source-"));
- const root = join(temporaryRoot, "plugin");
- try {
- for (const directory of ["agents", "custom", "absolute"])
- await mkdir(join(root, directory), { recursive: true });
- const outside = join(temporaryRoot, "outside");
- await mkdir(outside);
- for (const directory of [outside, join(root, "absolute")]) {
- await writeFile(
- join(directory, "forbidden.md"),
- "---\nname: forbidden\ndescription: Must not load\n---\nForbidden prompt",
- );
- }
- await writeFile(
- join(root, "agents", "first.md"),
- "---\nname: reviewer\ndescription: Default\n---\nDefault prompt",
- );
- await writeFile(
- join(root, "custom", "duplicate.md"),
- "---\nname: reviewer\ndescription: Custom\n---\nCustom prompt",
- );
- await writeFile(
- join(root, "custom", "other.md"),
- "---\nname: researcher\ndescription: Research\n---\nResearch prompt",
- );
- const groups = await enumeratePluginComponents(root, {
- name: "fixture",
- agents: ["custom", "agents", "custom", "../outside", join(root, "absolute")],
- });
- expect(groups.find((group) => group.kind === "agent")?.items).toEqual([
- { name: "reviewer", description: "Default", path: join(root, "agents", "first.md") },
- { name: "researcher", description: "Research", path: join(root, "custom", "other.md") },
- ]);
- expect(
- (
- await enumeratePluginComponents(root, {
- name: "fixture",
- agents: { reviewer: { description: "Declared" } },
- })
- ).find((group) => group.kind === "agent")?.items,
- ).toEqual([{ name: "reviewer", description: "Declared" }]);
- } finally {
- await rm(temporaryRoot, { force: true, recursive: true });
- }
- });
- afterEach(() => {
- vi.unstubAllEnvs();
- vi.unstubAllGlobals();
- });
- it("validates icon source records and only fills missing Claude marketplace icons", () => {
- const icons = parsePluginIconSources([
- { name: "figma", icon: "figma/icon.png", mimeType: "image/png", sha256: "a".repeat(64) },
- { name: "unsafe", icon: "../unsafe.png", mimeType: "image/png" },
- { name: "svg", icon: "svg/icon.svg", mimeType: "image/svg+xml" },
- ]);
- expect(icons.get("figma")).toBe(
- "https://cdn-zcode.z.ai/zcode/official-plugin/assets/figma/icon.png",
- );
- expect(icons.has("unsafe")).toBe(false);
- expect(icons.has("svg")).toBe(false);
- const enriched = applyClaudePluginIcons(
- {
- name: "claude-plugins-official",
- plugins: [
- { name: "figma", source: "./figma" },
- { name: "existing", icon: "https://example.com/existing.png", source: "./existing" },
- ],
- },
- new Map([
- ["figma", icons.get("figma")!],
- ["existing", "https://cdn.example.com/replacement.png"],
- ]),
- );
- expect(enriched.plugins).toEqual([
- {
- name: "figma",
- icon: "https://cdn-zcode.z.ai/zcode/official-plugin/assets/figma/icon.png",
- source: "./figma",
- },
- { name: "existing", icon: "https://example.com/existing.png", source: "./existing" },
- ]);
- });
- it("only classifies canonical public GitHub HTTPS repository URLs for Archive download", () => {
- expect(parsePublicGitHubRepositoryUrl("https://github.com/acme/plugins.git")).toEqual({
- owner: "acme",
- repo: "plugins",
- });
- expect(parsePublicGitHubRepositoryUrl("http://github.com/acme/plugins.git")).toBeNull();
- expect(parsePublicGitHubRepositoryUrl("https://token@github.com/acme/plugins.git")).toBeNull();
- expect(parsePublicGitHubRepositoryUrl("https://github.com/acme/plugins/tree/main")).toBeNull();
- expect(buildGitHubArchiveUrl({ owner: "acme", repo: "plugins" }, "feature/no-git")).toBe(
- "https://api.github.com/repos/acme/plugins/zipball/feature%2Fno-git",
- );
- });
- it("follows marketplace redirects through the configured system proxy without leaking headers", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-marketplace-proxy-"));
- const storageRoot = join(dir, "storage");
- const proxyRequests: Array<{ token: string | undefined; url: string }> = [];
- const proxy = createServer((request, response) => {
- const url = request.url ?? "";
- proxyRequests.push({
- token: request.headers["x-marketplace-token"],
- url,
- });
- if (url === "http://marketplace.test/marketplace.json") {
- response.writeHead(302, {
- location: "http://cdn.test/marketplace-redirect.json",
- });
- response.end();
- return;
- }
- if (url === "http://cdn.test/marketplace-redirect.json") {
- response.writeHead(307, {
- location: "http://cdn.test/marketplace.json",
- });
- response.end();
- return;
- }
- response.setHeader("content-type", "application/json");
- response.end(
- JSON.stringify({
- name: "proxy-market",
- plugins: [{ name: "proxied", source: "./proxied" }],
- }),
- );
- });
- try {
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "proxy-market",
- source: {
- source: "url",
- url: "http://marketplace.test/marketplace.json",
- headers: { "x-marketplace-token": "marketplace-secret" },
- },
- name: "proxy-market",
- addedAt: "2026-01-01T00:00:00.000Z",
- pluginCount: 0,
- },
- ]);
- vi.stubEnv("ZCODE_HTTP_PROXY", "");
- vi.stubEnv(
- ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY,
- JSON.stringify({ HTTP_PROXY: await listenServer(proxy) }),
- );
- const updated = await updateMarketplace({
- marketplace: "proxy-market",
- storageRoot,
- });
- expect(updated.map((record) => record.id)).toEqual(["proxy-market"]);
- expect(proxyRequests).toEqual([
- {
- token: "marketplace-secret",
- url: "http://marketplace.test/marketplace.json",
- },
- {
- token: undefined,
- url: "http://cdn.test/marketplace-redirect.json",
- },
- {
- token: undefined,
- url: "http://cdn.test/marketplace.json",
- },
- ]);
- expect(loadMarketplaceManifestSync(storageRoot, "proxy-market")?.plugins[0]?.name).toBe(
- "proxied",
- );
- } finally {
- await new Promise<void>((resolve, reject) =>
- proxy.close((error) => (error ? reject(error) : resolve())),
- );
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("fetches and persists optional icons while refreshing the Claude official marketplace", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-claude-icons-"));
- const storageRoot = join(dir, "storage");
- const sourceDir = join(dir, "claude-marketplace");
- try {
- await writeMarketplaceManifest(sourceDir, {
- name: "claude-plugins-official",
- plugins: [{ name: "figma", source: "./figma" }],
- });
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "zcode-plugins-official",
- source: {
- source: "settings",
- marketplace: {
- name: "zcode-plugins-official",
- plugins: [],
- raw: { name: "zcode-plugins-official", plugins: [] },
- },
- },
- name: "zcode-plugins-official",
- addedAt: "2026-01-01T00:00:00.000Z",
- pluginCount: 0,
- },
- {
- id: "claude-plugins-official",
- source: {
- source: "settings",
- marketplace: {
- name: "claude-plugins-official",
- plugins: [],
- raw: { name: "claude-plugins-official", plugins: [] },
- },
- },
- name: "claude-plugins-official",
- addedAt: "2026-01-01T00:00:00.000Z",
- pluginCount: 0,
- },
- {
- id: "claude-plugins-official",
- source: { source: "directory", path: sourceDir },
- name: "claude-plugins-official",
- addedAt: "2026-01-01T00:00:00.000Z",
- pluginCount: 1,
- },
- ]);
- vi.stubGlobal(
- "fetch",
- vi
- .fn()
- .mockResolvedValue(
- new Response(
- JSON.stringify([{ name: "figma", icon: "figma/icon.png", mimeType: "image/png" }]),
- { status: 200 },
- ),
- ),
- );
- await updateMarketplace({ marketplace: "claude-plugins-official", storageRoot });
- expect(
- loadMarketplaceManifestSync(storageRoot, "claude-plugins-official")?.raw,
- ).toMatchObject({
- plugins: [
- {
- name: "figma",
- icon: "https://cdn-zcode.z.ai/zcode/official-plugin/assets/figma/icon.png",
- },
- ],
- });
- const cached = JSON.parse(
- await readFile(join(storageRoot, "icon-sources.json"), "utf8"),
- ) as unknown;
- expect(cached).toEqual([{ name: "figma", icon: "figma/icon.png", mimeType: "image/png" }]);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("enriches an existing Claude catalog without refreshing its marketplace", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-existing-claude-icons-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(storageRoot, "marketplaces", "claude-plugins-official");
- try {
- await mkdir(marketplaceRoot, { recursive: true });
- await writeFile(
- join(marketplaceRoot, "marketplace.json"),
- JSON.stringify({
- name: "claude-plugins-official",
- plugins: [{ name: "figma", source: "./figma" }],
- }),
- );
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "claude-plugins-official",
- source: { source: "github", repo: "anthropics/claude-plugins-official" },
- name: "claude-plugins-official",
- addedAt: "2026-01-01T00:00:00.000Z",
- lastUpdated: "2026-01-02T00:00:00.000Z",
- pluginCount: 1,
- },
- ]);
- let resolveFetch!: (response: Response) => void;
- const fetchMock = vi.fn(
- () =>
- new Promise<Response>((resolve) => {
- resolveFetch = resolve;
- }),
- );
- vi.stubGlobal("fetch", fetchMock);
- let writeLockCalls = 0;
- const migration = enrichCachedClaudeMarketplaceIcons(storageRoot, async (write) => {
- writeLockCalls += 1;
- await write();
- });
- await Promise.resolve();
- expect(writeLockCalls).toBe(0);
- resolveFetch(
- new Response(
- JSON.stringify([{ name: "figma", icon: "figma/icon.png", mimeType: "image/png" }]),
- { status: 200 },
- ),
- );
- await migration;
- await enrichCachedClaudeMarketplaceIcons(storageRoot);
- expect(fetchMock).toHaveBeenCalledTimes(1);
- expect(writeLockCalls).toBe(1);
- expect(
- loadMarketplaceManifestSync(storageRoot, "claude-plugins-official")?.raw,
- ).toMatchObject({
- plugins: [
- {
- name: "figma",
- icon: "https://cdn-zcode.z.ai/zcode/official-plugin/assets/figma/icon.png",
- },
- ],
- });
- expect(
- JSON.parse(await readFile(join(storageRoot, "known_marketplaces.json"), "utf8")),
- ).toMatchObject({
- marketplaces: [{ lastUpdated: "2026-01-02T00:00:00.000Z" }],
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("uses the cached icon mapping when the optional CDN request fails", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-cached-claude-icons-"));
- const storageRoot = join(dir, "storage");
- const sourceDir = join(dir, "claude-marketplace");
- try {
- await writeMarketplaceManifest(sourceDir, {
- name: "claude-plugins-official",
- plugins: [{ name: "figma", source: "./figma" }],
- });
- await mkdir(storageRoot, { recursive: true });
- await writeFile(
- join(storageRoot, "icon-sources.json"),
- JSON.stringify([{ name: "figma", icon: "figma/icon.png", mimeType: "image/png" }]),
- );
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "claude-plugins-official",
- source: { source: "directory", path: sourceDir },
- name: "claude-plugins-official",
- addedAt: "2026-01-01T00:00:00.000Z",
- pluginCount: 1,
- },
- ]);
- vi.stubGlobal("fetch", vi.fn().mockRejectedValue(new Error("offline")));
- await updateMarketplace({ marketplace: "claude-plugins-official", storageRoot });
- expect(
- loadMarketplaceManifestSync(storageRoot, "claude-plugins-official")?.raw,
- ).toMatchObject({
- plugins: [
- {
- name: "figma",
- icon: "https://cdn-zcode.z.ai/zcode/official-plugin/assets/figma/icon.png",
- },
- ],
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("injects explicit ZCode proxy env into marketplace Git commands", () => {
- const env = buildMarketplaceGitEnv({
- [ZCODE_AGENT_CA_CERT_ENV_KEY]: "/tmp/root-ca.pem",
- [ZCODE_HTTP_PROXY_ENV_KEY]: "127.0.0.1:7890",
- [ZCODE_NO_PROXY_ENV_KEY]: "localhost,127.0.0.1",
- PATH: "/usr/bin:/bin",
- http_proxy: "http://ambient-proxy:8080",
- });
- expect(env).toMatchObject({
- ALL_PROXY: "http://127.0.0.1:7890",
- CURL_CA_BUNDLE: "/tmp/root-ca.pem",
- GIT_SSL_CAINFO: "/tmp/root-ca.pem",
- HTTPS_PROXY: "http://127.0.0.1:7890",
- HTTP_PROXY: "http://127.0.0.1:7890",
- NODE_EXTRA_CA_CERTS: "/tmp/root-ca.pem",
- NO_PROXY: "localhost,127.0.0.1",
- PATH: "/usr/bin:/bin",
- REQUESTS_CA_BUNDLE: "/tmp/root-ca.pem",
- SSL_CERT_FILE: "/tmp/root-ca.pem",
- all_proxy: "http://127.0.0.1:7890",
- http_proxy: "http://127.0.0.1:7890",
- https_proxy: "http://127.0.0.1:7890",
- no_proxy: "localhost,127.0.0.1",
- });
- });
- // Finding #4 (defense-in-depth): the lowest-level discovery adapter must drop official
- // candidates whose id is in config.suppressedBuiltins, even when their cache dirs still
- // exist on disk. scanOfficialCache walks EVERY version dir, so a stale old-version dir left
- // by an app upgrade (seeding only deletes the current version's targetRoot) used to
- // re-surface an uninstalled built-in. Suppression must not depend on physical deletion.
- it("suppresses official plugins from discovery even when stale cache version dirs exist", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-suppress-official-cache-"));
- const storageRoot = join(dir, "storage");
- const officialCacheRoot = join(storageRoot, "cache", "zcode-plugins-official");
- try {
- // Suppressed built-in seeded at TWO versions (mirrors an upgrade that left an old dir).
- for (const version of ["0.1.0", "0.2.0"]) {
- await writePluginManifest(join(officialCacheRoot, "skill-creator", version), {
- name: "skill-creator",
- version,
- });
- }
- // A different, NON-suppressed official built-in in the same cache must still appear.
- await writePluginManifest(join(officialCacheRoot, "document-skills", "0.1.0"), {
- name: "document-skills",
- version: "0.1.0",
- });
- const outcome = await discoverNodePlugins({
- config: {
- dirs: [],
- enabled: true,
- enabledPlugins: {},
- options: {},
- suppressedBuiltins: ["skill-creator@zcode-plugins-official"],
- },
- env: {},
- storageRoot,
- workingDirectory: join(dir, "workspace"),
- });
- expect(
- outcome.plugins.find((plugin) => plugin.id === "skill-creator@zcode-plugins-official"),
- ).toBeUndefined();
- expect(
- outcome.plugins.find((plugin) => plugin.id === "document-skills@zcode-plugins-official"),
- ).toMatchObject({ source: "official" });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("prefers the official marketplace cache path when stale plugin versions coexist", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-current-official-cache-"));
- const storageRoot = join(dir, "storage");
- const pluginRoot = join(storageRoot, "cache", "zcode-plugins-official", "browser-use");
- const staleRoot = join(pluginRoot, "0.2.1");
- const currentRoot = join(pluginRoot, "0.4.1");
- try {
- // 真实升级现场会保留旧目录,bundled partition 必须继续只认当前发布版本。
- await writePluginManifest(staleRoot, {
- name: "browser-use",
- version: "0.2.1",
- });
- await writePluginManifest(currentRoot, {
- name: "browser-use",
- version: "0.4.1",
- });
- await writeBundledOfficialMarketplacePartition({
- manifest: {
- name: "zcode-plugins-official",
- plugins: [
- {
- cachePath: currentRoot,
- name: "browser-use",
- source: "filesystem",
- version: "0.4.1",
- },
- ],
- },
- storageRoot,
- });
- const outcome = await discoverNodePlugins({
- config: {
- dirs: [],
- enabled: true,
- enabledPlugins: {},
- options: {},
- suppressedBuiltins: [],
- },
- env: {},
- storageRoot,
- workingDirectory: join(dir, "workspace"),
- });
- expect(
- outcome.plugins.find((plugin) => plugin.id === "browser-use@zcode-plugins-official"),
- ).toMatchObject({
- rootPath: currentRoot,
- version: "0.4.1",
- });
- expect(
- outcome.diagnostics.filter(
- (diagnostic) =>
- diagnostic.code === "plugin_duplicate_id" &&
- diagnostic.pluginId === "browser-use@zcode-plugins-official",
- ),
- ).toHaveLength(0);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- // Guard: suppression only targets official candidates. An inline/cache plugin whose id
- // collides with a suppressed string must NOT be dropped.
- it("does not suppress inline plugins that share a suppressed built-in's name", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-suppress-inline-guard-"));
- const pluginRoot = join(dir, "skill-creator-inline");
- try {
- await writePluginManifest(pluginRoot, { name: "skill-creator" });
- const outcome = await discoverNodePlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- // Same bare name, but the inline id is skill-creator@inline — different from the
- // official id, so the inline plugin must survive.
- suppressedBuiltins: ["skill-creator@zcode-plugins-official", "skill-creator@inline"],
- },
- env: {},
- storageRoot: join(dir, "storage"),
- workingDirectory: join(dir, "workspace"),
- });
- expect(outcome.plugins.find((plugin) => plugin.id === "skill-creator@inline")).toMatchObject({
- source: "inline",
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("rejects an untrusted marketplace that impersonates an official id", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-official-impersonate-"));
- const storageRoot = join(dir, "storage");
- try {
- // Both reserved ids must be protected via the untrusted (user-facing) path.
- // claude-plugins-official is a DEFAULT marketplace (always seeded as "known"),
- // so a known-state heuristic would never fire for it — the explicit trusted
- // flag is what closes that hole.
- for (const reservedId of ["zcode-plugins-official", "claude-plugins-official"]) {
- const sourceDir = join(dir, `evil-${reservedId}`);
- await writeMarketplaceManifest(sourceDir, { name: reservedId, plugins: [] });
- await expect(
- addMarketplace({ source: { source: "directory", path: sourceDir }, storageRoot }),
- ).rejects.toThrow(/official marketplace/i);
- }
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("does not clobber the local official manifest dir when rejecting an impersonator", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-official-clobber-"));
- const storageRoot = join(dir, "storage");
- const sourceDir = join(dir, "evil");
- try {
- // Seed an existing local claude-plugins-official manifest dir with a sentinel.
- const officialDir = join(storageRoot, "marketplaces", "claude-plugins-official");
- await mkdir(officialDir, { recursive: true });
- await writeFile(
- join(officialDir, "marketplace.json"),
- JSON.stringify({ name: "claude-plugins-official", plugins: [] }),
- );
- await writeFile(join(officialDir, "SENTINEL"), "real-official");
- // Attacker source whose manifest impersonates the official id.
- await writeMarketplaceManifest(sourceDir, { name: "claude-plugins-official", plugins: [] });
- await expect(
- addMarketplace({ source: { source: "directory", path: sourceDir }, storageRoot }),
- ).rejects.toThrow(/official marketplace/i);
- // The guard must run BEFORE persistence: the original dir + sentinel survive intact.
- expect(await readFile(join(officialDir, "SENTINEL"), "utf8")).toBe("real-official");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- // Review #2: a trusted refresh must NOT let a non-official known marketplace claim an official
- // id by renaming its manifest. trustedId pins the refresh to the record's own id, so an
- // official name coming from a record whose id is not that official id is rejected.
- it("records a failed trusted refresh when a non-official manifest renames to an official id", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-official-rename-attack-"));
- const storageRoot = join(dir, "storage");
- const sourceDir = join(dir, "evil-source");
- try {
- // A user-added marketplace "evil" becomes known.
- await writeMarketplaceManifest(sourceDir, { name: "evil", plugins: [] });
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "evil",
- source: { source: "directory", path: sourceDir },
- name: "evil",
- addedAt: "2026-01-01T00:00:00.000Z",
- pluginCount: 0,
- },
- ]);
- // Later the attacker renames the source manifest to impersonate the official id.
- await writeMarketplaceManifest(sourceDir, { name: "claude-plugins-official", plugins: [] });
- // The trusted internal refresh must reject the official-id claim without aborting the
- // refresh envelope; the durable failure is surfaced by overview/protocol diagnostics.
- expect(await updateMarketplace({ marketplace: "evil", storageRoot })).toEqual([]);
- expect(
- loadKnownMarketplacesSync(storageRoot).find((record) => record.id === "evil")
- ?.lastRefreshFailure,
- ).toMatchObject({
- code: "plugin_marketplace_invalid",
- message: expect.stringMatching(/official marketplace/i),
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("seeds Claude official marketplace without overwriting an existing record", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-default-marketplace-"));
- const storageRoot = join(dir, "storage");
- try {
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "claude-plugins-official",
- source: { source: "directory", path: join(dir, "custom-claude") },
- name: "claude-plugins-official",
- description: "Custom Claude mirror",
- addedAt: "2026-01-01T00:00:00.000Z",
- lastUpdated: "2026-01-02T00:00:00.000Z",
- pluginCount: 123,
- },
- ]);
- const known = await ensureDefaultPluginMarketplaces(storageRoot);
- // 默认市场现在有两个:claude-plugins-official(已有记录不被覆盖)+ 唯一 ZCode
- // 官方市场 zcode-plugins-official(缺失时补种)。
- expect(known).toHaveLength(2);
- const claude = known.find((record) => record.id === "claude-plugins-official");
- expect(claude).toMatchObject({
- description: "Custom Claude mirror",
- lastUpdated: "2026-01-02T00:00:00.000Z",
- pluginCount: 123,
- source: { source: "directory", path: join(dir, "custom-claude") },
- });
- expect(
- JSON.parse(await readFile(join(storageRoot, "known_marketplaces.json"), "utf8")),
- ).toMatchObject({
- marketplaces: [
- {
- description: "Custom Claude mirror",
- pluginCount: 123,
- },
- {
- id: "zcode-plugins-official",
- },
- ],
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("lazy-loads a known marketplace manifest before installing", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-known-marketplace-install-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const pluginRoot = join(marketplaceRoot, "plugins", "hello");
- try {
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "lazy-market",
- source: { source: "directory", path: marketplaceRoot },
- name: "lazy-market",
- addedAt: "2026-01-01T00:00:00.000Z",
- pluginCount: 0,
- },
- ]);
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "lazy-market",
- plugins: [
- {
- name: "hello",
- source: "./plugins/hello",
- },
- ],
- });
- await writePluginManifest(pluginRoot, { name: "hello", skills: "skills" }, ".claude-plugin");
- await mkdir(join(pluginRoot, "skills", "hello"), { recursive: true });
- await writeFile(join(pluginRoot, "skills", "hello", "SKILL.md"), "# hello");
- const install = await installMarketplacePlugin({
- marketplace: "lazy-market",
- name: "hello",
- storageRoot,
- });
- expect(install.closure).toEqual(["hello@lazy-market"]);
- expect(install.installed[0]?.id).toBe("hello@lazy-market");
- expect(
- JSON.parse(
- await readFile(
- join(storageRoot, "marketplaces", "lazy-market", "marketplace.json"),
- "utf8",
- ),
- ),
- ).toMatchObject({ name: "lazy-market" });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- // 扩展摘要只属于目录(docs/plugins/plugin-publishing.md §4.2):没有 plugin.json 的条目合成清单时
- // 必须剔除,否则一份不完整的扩展信息会被当成清单内容。
- it("strips the catalog extension summary when it synthesizes a manifest", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-synth-extension-summary-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const pluginRoot = join(marketplaceRoot, "plugins", "ext-entry");
- try {
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "ext-market",
- source: { source: "directory", path: marketplaceRoot },
- name: "ext-market",
- addedAt: "2026-01-01T00:00:00.000Z",
- pluginCount: 0,
- },
- ]);
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "ext-market",
- plugins: [
- {
- name: "ext-entry",
- source: "./plugins/ext-entry",
- strict: false,
- skills: "skills",
- displayName: "Ext Entry",
- componentTypes: ["extension"],
- extensionSummary: { engines: { zcode: "^1.4.0" }, grants: ["sessions.read"] },
- },
- ],
- });
- await mkdir(join(pluginRoot, "skills", "ext-entry"), { recursive: true });
- await writeFile(join(pluginRoot, "skills", "ext-entry", "SKILL.md"), "# ext");
- const install = await installMarketplacePlugin({
- marketplace: "ext-market",
- name: "ext-entry",
- storageRoot,
- });
- const installPath = install.installed[0]?.installPath;
- expect(installPath).toBeDefined();
- const manifest = JSON.parse(
- await readFile(join(installPath!, ".claude-plugin", "plugin.json"), "utf8"),
- ) as Record<string, unknown>;
- expect(manifest).toMatchObject({ name: "ext-entry", skills: "skills" });
- expect(manifest).not.toHaveProperty("extensionSummary");
- expect(manifest).not.toHaveProperty("displayName");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("loads Claude-compatible local plugin components", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-"));
- const pluginRoot = join(dir, "ios-plugin");
- try {
- await writePluginManifest(pluginRoot, {
- commands: "commands",
- hooks: "hooks/hooks.json",
- name: "ios-dev",
- skills: "skills",
- userConfig: {
- default_device: {
- default: "iPhone 16",
- type: "string",
- },
- },
- });
- await mkdir(join(pluginRoot, "skills", "ios-dev"), { recursive: true });
- await mkdir(join(pluginRoot, "skills", "pdf-dev"), { recursive: true });
- await mkdir(join(pluginRoot, "commands"), { recursive: true });
- await mkdir(join(pluginRoot, "hooks"), { recursive: true });
- await writeFile(join(pluginRoot, "skills", "ios-dev", "SKILL.md"), "# skill");
- await writeFile(join(pluginRoot, "skills", "pdf-dev", "SKILL.md"), "# skill");
- await writeFile(join(pluginRoot, "commands", "ios-dev.md"), "# command");
- await writeFile(
- join(pluginRoot, "hooks", "hooks.json"),
- JSON.stringify({
- hooks: {
- SessionStart: [
- {
- matcher: "startup",
- hooks: [
- {
- type: "command",
- command: '"${CLAUDE_PLUGIN_ROOT}/hooks/session-start"',
- },
- ],
- },
- ],
- },
- }),
- );
- await writeFile(
- join(pluginRoot, ".mcp.json"),
- JSON.stringify({
- mcpServers: {
- "ios-simulator": {
- command: "bun",
- args: ["${CLAUDE_PLUGIN_ROOT}/src/mcp/server.ts"],
- cwd: "${ZCODE_PROJECT_DIR}",
- env: {
- IOS_SIM_DEFAULT_DEVICE: "${user_config.default_device}",
- IOS_SIM_PLUGIN_DATA: "${ZCODE_PLUGIN_DATA}",
- },
- },
- },
- }),
- );
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: join(dir, "workspace"),
- });
- expect(outcome.plugins[0]?.id).toBe("ios-dev@inline");
- expect(outcome.plugins[0]).toMatchObject({
- declaredMcpServerNames: ["ios-simulator"],
- hookDetails: [
- {
- command: '"${CLAUDE_PLUGIN_ROOT}/hooks/session-start"',
- event: "SessionStart",
- matcher: "startup",
- runnable: true,
- sourcePath: join(pluginRoot, "hooks", "hooks.json"),
- type: "command",
- },
- ],
- skillCount: 2,
- skillRootCount: 1,
- });
- expect(outcome.skillRoots[0]?.path).toBe(join(pluginRoot, "skills"));
- expect(outcome.commandRoots[0]?.path).toBe(join(pluginRoot, "commands"));
- const mcpKey = "plugin:ios-dev:ios-simulator";
- expect(outcome.mcpServers[mcpKey]).toMatchObject({
- command: "bun",
- cwd: join(dir, "workspace"),
- type: "stdio",
- });
- expect(outcome.mcpServers[mcpKey]?.env).toMatchObject({
- IOS_SIM_DEFAULT_DEVICE: "iPhone 16",
- });
- expect(outcome.hooks.SessionStart?.[0]?.hooks[0]).toMatchObject({
- command: '"${CLAUDE_PLUGIN_ROOT}/hooks/session-start"',
- plugin: {
- id: "ios-dev@inline",
- rootPath: pluginRoot,
- },
- type: "command",
- });
- const disabledOutcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: { "ios-dev@inline": false },
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: join(dir, "workspace"),
- });
- expect(disabledOutcome.hooks).toEqual({});
- expect(disabledOutcome.plugins[0]).toMatchObject({
- enabled: false,
- hookDetails: [
- {
- event: "SessionStart",
- runnable: true,
- },
- ],
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("loads representative Claude marketplace plugin layouts for skills, commands, and MCP servers", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-triplet-marketplace-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const workingDirectory = join(dir, "workspace");
- const cases = claudeMarketplaceTripletFixtures();
- try {
- await mkdir(workingDirectory, { recursive: true });
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "claude-plugins-official",
- plugins: cases.map((fixture) => ({
- name: fixture.name,
- source: `./plugins/${fixture.name}`,
- })),
- });
- for (const fixture of cases) {
- await writeTripletPluginFixture(join(marketplaceRoot, "plugins", fixture.name), fixture);
- }
- // trusted: this fixture legitimately stands up the official catalog from a local dir,
- // mirroring the internal refresh path; the official-id lock only blocks untrusted adds.
- await addMarketplace({
- source: { source: "directory", path: marketplaceRoot },
- storageRoot,
- trustedId: "claude-plugins-official",
- });
- for (const fixture of cases) {
- const install = await installMarketplacePlugin({
- marketplace: "claude-plugins-official",
- name: fixture.name,
- storageRoot,
- });
- expect(install.installed[0]?.id).toBe(`${fixture.name}@claude-plugins-official`);
- }
- const outcome = await createNodePluginAdapter({ storageRoot }).discoverPlugins({
- config: {
- dirs: [],
- enabled: true,
- enabledPlugins: Object.fromEntries(
- cases.map((fixture) => [`${fixture.name}@claude-plugins-official`, true]),
- ),
- options: {},
- },
- env: {},
- storageRoot,
- workingDirectory,
- });
- expect(outcome.diagnostics.filter((diagnostic) => diagnostic.severity === "error")).toEqual(
- [],
- );
- expect(outcome.plugins.filter((plugin) => plugin.enabled)).toHaveLength(cases.length);
- expect(outcome.skillRoots).toHaveLength(cases.length);
- expect(outcome.commandRoots).toHaveLength(cases.length);
- for (const fixture of cases) {
- const plugin = outcome.plugins.find(
- (candidate) => candidate.id === `${fixture.name}@claude-plugins-official`,
- );
- expect(plugin).toMatchObject({
- commandRootCount: 1,
- declaredMcpServerNames: [fixture.mcpServerName],
- enabled: true,
- skillCount: fixture.skillCount,
- skillRootCount: 1,
- });
- // 权威 components 随 list 下发:技能名应齐全且与 skillCount 一致(修复「只有数量没有名称」)。
- const skillNames = componentNames(plugin?.components ?? [], "skill");
- expect(skillNames).toHaveLength(fixture.skillCount);
- expect(skillNames).toContain(`${fixture.name}-skill-1`);
- expect(outcome.mcpServers[`plugin:${fixture.name}:${fixture.mcpServerName}`]).toMatchObject(
- {
- command: "node",
- env: {
- PLUGIN_DATA: expect.stringContaining(`${fixture.name}@claude-plugins-official`),
- PROJECT_DIR: workingDirectory,
- },
- type: "stdio",
- },
- );
- }
- const skillOutcome = await createNodeSkillAdapter().discoverSkills({
- roots: outcome.skillRoots,
- workingDirectory,
- });
- const commandOutcome = await createNodeCustomCommandAdapter().discoverCommands({
- roots: outcome.commandRoots,
- workingDirectory,
- });
- expect(skillOutcome.diagnostics).toEqual([]);
- expect(commandOutcome.diagnostics).toEqual([]);
- expect(skillOutcome.skills).toHaveLength(
- cases.reduce((total, fixture) => total + fixture.skillCount, 0),
- );
- expect(commandOutcome.commands).toHaveLength(
- cases.reduce((total, fixture) => total + fixture.commandCount, 0),
- );
- for (const fixture of cases) {
- expect(skillOutcome.skills.map((skill) => skill.name)).toContain(`${fixture.name}-skill-1`);
- expect(commandOutcome.commands.map((command) => command.name)).toContain(
- `${fixture.commandPrefix}:cmd01`,
- );
- }
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("still projects authoritative skill component names for a DISABLED plugin", async () => {
- // 核心回归:停用插件过去走 emptyComponents() 使 skillCount=0,UI join 也拿不到名称,
- // 导致详情弹窗整个技能分组消失。现在 components 由插件根目录权威枚举,与启用态无关。
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-disabled-components-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const workingDirectory = join(dir, "workspace");
- const fixture: TripletPluginFixture = {
- commandCount: 2,
- commandPrefix: "superpowers",
- mcpServerName: "superpowers",
- name: "superpowers",
- skillCount: 14,
- };
- try {
- await mkdir(workingDirectory, { recursive: true });
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "claude-plugins-official",
- plugins: [{ name: fixture.name, source: `./plugins/${fixture.name}` }],
- });
- await writeTripletPluginFixture(join(marketplaceRoot, "plugins", fixture.name), fixture);
- await addMarketplace({
- source: { source: "directory", path: marketplaceRoot },
- storageRoot,
- trustedId: "claude-plugins-official",
- });
- await installMarketplacePlugin({
- marketplace: "claude-plugins-official",
- name: fixture.name,
- storageRoot,
- });
- const outcome = await createNodePluginAdapter({ storageRoot }).discoverPlugins({
- config: {
- dirs: [],
- enabled: true,
- // 显式停用该插件。
- enabledPlugins: { "superpowers@claude-plugins-official": false },
- options: {},
- },
- env: {},
- storageRoot,
- workingDirectory,
- });
- const plugin = outcome.plugins.find(
- (candidate) => candidate.id === "superpowers@claude-plugins-official",
- );
- expect(plugin?.enabled).toBe(false);
- // 停用:runtime 不注入技能根(skillCount=0、skillRootCount=0)……
- expect(plugin?.skillCount).toBe(0);
- expect(outcome.skillRoots).toHaveLength(0);
- // ……但 components 仍权威枚举出全部 14 个技能名,详情 UI 才能展示。
- const skillNames = componentNames(plugin?.components ?? [], "skill");
- expect(skillNames).toHaveLength(14);
- expect(skillNames).toContain("superpowers-skill-1");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("describes a bundled official plugin whose marketplace source is 'filesystem'", async () => {
- // Bugfix 回归:内置 official 插件 seed 时 source 写裸字符串 "filesystem",describe 过去把它
- // 当路径解析后抛错,市场详情页对内置插件枚举不出组件。现在按 cachePath 直接定位缓存目录。
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-filesystem-describe-"));
- const storageRoot = join(dir, "storage");
- const cacheRoot = join(storageRoot, "cache", "zcode-plugins-official", "superpowers", "5.1.0");
- const marketplaceRoot = join(storageRoot, "marketplaces", "zcode-plugins-official");
- try {
- // 在缓存目录里铺一个含 manifest + 2 个技能的内置插件。
- await writePluginManifest(cacheRoot, {
- name: "superpowers",
- version: "5.1.0",
- skills: "skills",
- });
- for (const skillName of ["brainstorming", "writing-plans"]) {
- const skillDir = join(cacheRoot, "skills", skillName);
- await mkdir(skillDir, { recursive: true });
- await writeFile(
- join(skillDir, "SKILL.md"),
- ["---", `name: ${skillName}`, `description: ${skillName} desc`, "---", "", "# body"].join(
- "\n",
- ),
- );
- }
- // seeded marketplace.json:source 为裸 "filesystem",并带 cachePath(模拟 bundled-plugins 写入)。
- await mkdir(marketplaceRoot, { recursive: true });
- await writeFile(
- join(marketplaceRoot, "marketplace.json"),
- JSON.stringify({
- name: "zcode-plugins-official",
- version: 1,
- plugins: [
- { name: "superpowers", version: "5.1.0", source: "filesystem", cachePath: cacheRoot },
- ],
- }),
- );
- const result = await describeMarketplacePlugin({
- marketplace: "zcode-plugins-official",
- name: "superpowers",
- storageRoot,
- });
- expect(result.diagnostics.filter((d) => d.severity === "error")).toEqual([]);
- expect(componentNames(result.components, "skill").toSorted()).toEqual([
- "brainstorming",
- "writing-plans",
- ]);
- expect(componentDescription(result.components, "skill", "brainstorming")).toBe(
- "brainstorming desc",
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("describes MCP and hook components from loader-compatible convention files", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-describe-components-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const pluginRoot = join(marketplaceRoot, "plugins", "airtable");
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "claude-plugins-official",
- plugins: [
- {
- name: "airtable",
- source: "./plugins/airtable",
- },
- ],
- });
- await writePluginManifest(
- pluginRoot,
- {
- hooks: {
- PreToolUse: [
- {
- hooks: [{ command: "echo pre", type: "command" }],
- },
- ],
- },
- name: "airtable",
- skills: "skills",
- },
- ".claude-plugin",
- );
- await mkdir(join(pluginRoot, "skills", "airtable-overview"), { recursive: true });
- await mkdir(join(pluginRoot, "hooks"), { recursive: true });
- await writeFile(
- join(pluginRoot, "skills", "airtable-overview", "SKILL.md"),
- [
- "---",
- "name: airtable-overview",
- // 用 `>` 折叠块标量覆盖回归:描述应折叠成完整句子,而不是只剩指示符 `>`。
- "description: >",
- " Explains the Airtable data model and how",
- " to query bases from the agent.",
- "---",
- "",
- "# Airtable overview",
- ].join("\n"),
- );
- await writeFile(
- join(pluginRoot, "hooks", "hooks.json"),
- JSON.stringify({
- hooks: {
- Stop: [
- {
- hooks: [{ command: "echo stop", type: "command" }],
- },
- ],
- },
- }),
- );
- await writeFile(
- join(pluginRoot, ".mcp.json"),
- JSON.stringify({
- airtable: {
- type: "http",
- url: "https://mcp.airtable.test/mcp",
- },
- }),
- );
- await addMarketplace({
- source: { source: "directory", path: marketplaceRoot },
- storageRoot,
- trustedId: "claude-plugins-official",
- });
- const sourceDescribe = await describeMarketplacePlugin({
- marketplace: "claude-plugins-official",
- name: "airtable",
- storageRoot,
- });
- expect(componentNames(sourceDescribe.components, "skill")).toEqual(["airtable-overview"]);
- expect(componentDescription(sourceDescribe.components, "skill", "airtable-overview")).toBe(
- "Explains the Airtable data model and how to query bases from the agent.",
- );
- expect(componentNames(sourceDescribe.components, "hook")).toEqual(["Stop", "PreToolUse"]);
- expect(componentNames(sourceDescribe.components, "mcp")).toEqual(["airtable"]);
- await installMarketplacePlugin({
- marketplace: "claude-plugins-official",
- name: "airtable",
- storageRoot,
- });
- const cacheDescribe = await describeMarketplacePlugin({
- marketplace: "claude-plugins-official",
- name: "airtable",
- storageRoot,
- });
- expect(componentNames(cacheDescribe.components, "hook")).toEqual(["Stop", "PreToolUse"]);
- expect(componentNames(cacheDescribe.components, "mcp")).toEqual(["airtable"]);
- const outcome = await createNodePluginAdapter({ storageRoot }).discoverPlugins({
- config: {
- dirs: [],
- enabled: true,
- enabledPlugins: {
- "airtable@claude-plugins-official": true,
- },
- options: {},
- },
- env: {},
- storageRoot,
- workingDirectory: dir,
- });
- expect(componentNames(cacheDescribe.components, "mcp")).toEqual(
- outcome.plugins[0]?.declaredMcpServerNames,
- );
- expect(componentNames(cacheDescribe.components, "hook")).toEqual(
- outcome.plugins[0]?.hookDetails.map((hook) => hook.event),
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("describes valid components when convention MCP config is invalid", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-describe-invalid-mcp-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const pluginRoot = join(marketplaceRoot, "plugins", "broken-mcp");
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "broken-market",
- plugins: [{ name: "broken-mcp", source: "./plugins/broken-mcp" }],
- });
- await writePluginManifest(
- pluginRoot,
- { name: "broken-mcp", skills: "skills" },
- ".claude-plugin",
- );
- await mkdir(join(pluginRoot, "skills", "still-visible"), { recursive: true });
- await writeFile(join(pluginRoot, "skills", "still-visible", "SKILL.md"), "# skill");
- await writeFile(join(pluginRoot, ".mcp.json"), "{not-json");
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- const result = await describeMarketplacePlugin({
- marketplace: "broken-market",
- name: "broken-mcp",
- storageRoot,
- });
- expect(componentNames(result.components, "skill")).toEqual(["still-visible"]);
- expect(componentNames(result.components, "mcp")).toEqual([]);
- expect(result.diagnostics).toContainEqual(
- expect.objectContaining({
- code: "plugin_mcp_read_failed",
- pluginId: "broken-mcp@broken-market",
- }),
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("adds a Claude marketplace directory and installs relative plugin sources", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-marketplace-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const pluginRoot = join(marketplaceRoot, "plugins", "hello");
- const depRoot = join(marketplaceRoot, "plugins", "dep");
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "third-party",
- plugins: [
- {
- name: "hello",
- version: "1.2.3",
- source: "./plugins/hello",
- dependencies: ["dep"],
- },
- {
- name: "dep",
- version: "0.1.0",
- source: "./plugins/dep",
- },
- ],
- });
- await writePluginManifest(
- pluginRoot,
- {
- mcpServers: {
- local: {
- command: "node",
- args: ["${CLAUDE_PLUGIN_ROOT}/server.js"],
- },
- },
- name: "hello",
- skills: "skills",
- },
- ".claude-plugin",
- );
- await mkdir(join(pluginRoot, "skills", "hello"), { recursive: true });
- await writeFile(join(pluginRoot, "skills", "hello", "SKILL.md"), "# hello");
- await writePluginManifest(depRoot, { name: "dep" }, ".claude-plugin");
- const marketplace = await addMarketplace({
- source: { source: "directory", path: marketplaceRoot },
- storageRoot,
- });
- expect(marketplace.id).toBe("third-party");
- const validation = await validateMarketplacePlugin({
- marketplace: "third-party",
- name: "hello",
- storageRoot,
- });
- expect(validation.filter((item) => item.severity === "error")).toEqual([]);
- const install = await installMarketplacePlugin({
- marketplace: "third-party",
- name: "hello",
- storageRoot,
- });
- expect(install.closure).toEqual(["dep@third-party", "hello@third-party"]);
- expect(listInstalledPluginRecords(storageRoot).map((record) => record.id)).toEqual([
- "dep@third-party",
- "hello@third-party",
- ]);
- const outcome = await createNodePluginAdapter({ storageRoot }).discoverPlugins({
- config: {
- dirs: [],
- enabled: true,
- enabledPlugins: {
- "hello@third-party": true,
- },
- options: {},
- },
- env: {},
- storageRoot,
- workingDirectory: dir,
- });
- expect(outcome.plugins.find((plugin) => plugin.id === "hello@third-party")).toMatchObject({
- enabled: true,
- source: "cache",
- });
- expect(outcome.skillRoots[0]?.path).toContain(join("hello", "1.2.3", "skills"));
- expect(outcome.mcpServers["plugin:hello:local"]).toMatchObject({
- command: "node",
- type: "stdio",
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("installs Claude marketplace url sources that point at pinned plugin subdirectories", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-marketplace-url-path-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const repoRoot = join(dir, "remote-plugin");
- const pluginRoot = join(repoRoot, "claude-plugin", "atomic-agents");
- try {
- await mkdir(repoRoot, { recursive: true });
- await execFileAsync("git", ["init"], { cwd: repoRoot });
- await writePluginManifest(
- pluginRoot,
- {
- commands: "commands",
- name: "wrong-name",
- },
- ".claude-plugin",
- );
- await execFileAsync("git", ["add", "."], { cwd: repoRoot });
- await execFileAsync(
- "git",
- [
- "-c",
- "user.name=ZCode Test",
- "-c",
- "user.email=zcode-test@example.com",
- "commit",
- "-m",
- "wrong plugin",
- ],
- { cwd: repoRoot },
- );
- await writePluginManifest(
- pluginRoot,
- {
- commands: "commands",
- name: "atomic-agents",
- },
- ".claude-plugin",
- );
- await mkdir(join(pluginRoot, "commands"), { recursive: true });
- await writeFile(join(pluginRoot, "commands", "agent.md"), "# agent");
- await execFileAsync("git", ["add", "."], { cwd: repoRoot });
- await execFileAsync(
- "git",
- [
- "-c",
- "user.name=ZCode Test",
- "-c",
- "user.email=zcode-test@example.com",
- "commit",
- "-m",
- "test plugin",
- ],
- { cwd: repoRoot },
- );
- const pinnedCommit = (
- await execFileAsync("git", ["rev-parse", "HEAD"], { cwd: repoRoot })
- ).stdout.trim();
- await writePluginManifest(
- pluginRoot,
- {
- commands: "commands",
- name: "wrong-name-after-pin",
- },
- ".claude-plugin",
- );
- await execFileAsync("git", ["add", "."], { cwd: repoRoot });
- await execFileAsync(
- "git",
- [
- "-c",
- "user.name=ZCode Test",
- "-c",
- "user.email=zcode-test@example.com",
- "commit",
- "-m",
- "wrong plugin after pin",
- ],
- { cwd: repoRoot },
- );
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "official-like-url-path",
- plugins: [
- {
- name: "atomic-agents",
- source: {
- source: "url",
- url: repoRoot,
- path: "claude-plugin/atomic-agents",
- commit: pinnedCommit,
- },
- },
- ],
- });
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- const diagnostics = await validateMarketplacePlugin({
- marketplace: "official-like-url-path",
- name: "atomic-agents",
- storageRoot,
- });
- expect(diagnostics.filter((item) => item.severity === "error")).toEqual([]);
- const install = await installMarketplacePlugin({
- marketplace: "official-like-url-path",
- name: "atomic-agents",
- storageRoot,
- });
- expect(install.installed[0]?.id).toBe("atomic-agents@official-like-url-path");
- const outcome = await createNodePluginAdapter({ storageRoot }).discoverPlugins({
- config: {
- dirs: [],
- enabled: true,
- enabledPlugins: {
- "atomic-agents@official-like-url-path": true,
- },
- options: {},
- },
- env: {},
- storageRoot,
- workingDirectory: dir,
- });
- expect(outcome.commandRoots[0]?.path).toContain(join("atomic-agents", "0.0.0", "commands"));
- const installedManifest = await readFile(
- join(
- storageRoot,
- "cache",
- "official-like-url-path",
- "atomic-agents",
- "0.0.0",
- ".claude-plugin",
- "plugin.json",
- ),
- "utf8",
- );
- expect(JSON.parse(installedManifest)).toMatchObject({ name: "atomic-agents" });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- // Bugfix(插件版本 0.0.0):git/url 源插件(如 superpowers)的 marketplace 条目通常不带 version,
- // 旧逻辑用 entry.version ?? "0.0.0" 拼缓存路径,导致 Root path 落到 .../<name>/0.0.0,而 UI 展示
- // 读插件自带 plugin.json 的真实版本(6.0.3),两者割裂。修复后缓存路径段与安装记录均取真实版本。
- it("uses the cloned plugin.json version for the cache path when the marketplace entry omits version", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-version-from-manifest-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const repoRoot = join(dir, "remote-plugin");
- try {
- await mkdir(repoRoot, { recursive: true });
- await execFileAsync("git", ["init"], { cwd: repoRoot });
- // 真实版本只存在于插件自带 manifest 里,marketplace 条目刻意不写 version。
- await writePluginManifest(
- repoRoot,
- { name: "superpowers", version: "6.0.3" },
- ".claude-plugin",
- );
- await execFileAsync("git", ["add", "."], { cwd: repoRoot });
- await execFileAsync(
- "git",
- [
- "-c",
- "user.name=ZCode Test",
- "-c",
- "user.email=zcode-test@example.com",
- "commit",
- "-m",
- "superpowers plugin",
- ],
- { cwd: repoRoot },
- );
- const pinnedCommit = (
- await execFileAsync("git", ["rev-parse", "HEAD"], { cwd: repoRoot })
- ).stdout.trim();
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "claude-plugins-official",
- plugins: [
- {
- name: "superpowers",
- source: { source: "url", url: repoRoot, sha: pinnedCommit },
- },
- ],
- });
- await addMarketplace({
- source: { source: "directory", path: marketplaceRoot },
- storageRoot,
- trustedId: "claude-plugins-official",
- });
- const install = await installMarketplacePlugin({
- marketplace: "claude-plugins-official",
- name: "superpowers",
- storageRoot,
- });
- const record = install.installed.find(
- (item) => item.id === "superpowers@claude-plugins-official",
- );
- expect(record?.version).toBe("6.0.3");
- expect(record?.installPath).toContain(join("superpowers", "6.0.3"));
- expect(record?.installPath).not.toContain(join("superpowers", "0.0.0"));
- const installedManifest = await readFile(
- join(
- storageRoot,
- "cache",
- "claude-plugins-official",
- "superpowers",
- "6.0.3",
- ".claude-plugin",
- "plugin.json",
- ),
- "utf8",
- );
- expect(JSON.parse(installedManifest)).toMatchObject({
- name: "superpowers",
- version: "6.0.3",
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it.each(["absent", "bundled", "stale"])(
- "hydrates an official %s catalog before installing CDN plugins",
- async (catalogState) => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-official-install-"));
- const storageRoot = join(dir, "storage");
- const marketplace = "zcode-plugins-official";
- const requests: string[] = [];
- const archives = new Map(
- ["remote", "dependency"].map((name) => [
- name,
- createStoredZip([
- {
- path: `${name}/.claude-plugin/plugin.json`,
- content: JSON.stringify({ name, version: "1.0.0" }),
- },
- ]),
- ]),
- );
- let status = 503;
- let baseUrl = "";
- const server = createServer((request, response) => {
- requests.push(request.url ?? "");
- response.writeHead(status);
- if (status !== 200) {
- response.end("unavailable");
- } else if (request.url === "/marketplace.json") {
- response.end(
- JSON.stringify({
- name: marketplace,
- plugins: [...archives].map(([name, archive]) => ({
- name,
- dependencies: name === "remote" ? ["dependency"] : [],
- source: {
- source: "url",
- type: "zip",
- url: `${baseUrl}/${name}.zip`,
- sha256: sha256Buffer(archive),
- },
- })),
- }),
- );
- } else {
- response.end(archives.get(request.url?.slice(1, -4) ?? ""));
- }
- });
- try {
- baseUrl = await listenLoopbackServer(server);
- await mkdir(storageRoot, { recursive: true });
- await writeFile(
- join(storageRoot, "known_marketplaces.json"),
- JSON.stringify({
- version: 1,
- marketplaces: [
- {
- id: marketplace,
- name: marketplace,
- source: { source: "url", url: `${baseUrl}/marketplace.json` },
- addedAt: "2026-01-01T00:00:00.000Z",
- pluginCount: 1,
- },
- ],
- }),
- );
- if (catalogState !== "absent") {
- await writeBundledOfficialMarketplacePartition({
- storageRoot,
- manifest: { name: marketplace, plugins: [{ name: "bundled", source: "filesystem" }] },
- });
- }
- if (catalogState === "stale") {
- await writeCdnOfficialMarketplacePartition({
- storageRoot,
- manifest: { name: marketplace, plugins: [{ name: "old-cdn", source: "filesystem" }] },
- });
- }
- const previous = loadMarketplaceManifestSync(storageRoot, marketplace);
- const install = { marketplace, name: "remote", storageRoot };
- await expect(installMarketplacePlugin(install)).rejects.toThrow(
- "Failed to fetch marketplace: 503",
- );
- expect(loadMarketplaceManifestSync(storageRoot, marketplace)).toEqual(previous);
- expect(listInstalledPluginRecords(storageRoot)).toEqual([]);
- expect(requests).toEqual(["/marketplace.json"]);
- status = 200;
- const installed = await installMarketplacePlugin(install);
- expect(installed.closure).toEqual([`dependency@${marketplace}`, `remote@${marketplace}`]);
- for (const record of installed.installed) {
- expect(
- JSON.parse(
- await readFile(join(record.installPath, ".claude-plugin", "plugin.json"), "utf8"),
- ),
- ).toMatchObject({ name: record.name, version: "1.0.0" });
- }
- if (catalogState !== "absent") {
- expect(
- loadMarketplaceManifestSync(storageRoot, marketplace)?.plugins.map(
- (entry) => entry.name,
- ),
- ).toContain("bundled");
- }
- await installMarketplacePlugin({ ...install, name: "dependency" });
- expect(requests.filter((url) => url === "/marketplace.json")).toHaveLength(2);
- await expect(installMarketplacePlugin({ ...install, name: "missing" })).rejects.toThrow(
- `Dependency not found: missing@${marketplace} required by missing@${marketplace}`,
- );
- expect(requests.filter((url) => url === "/marketplace.json")).toHaveLength(3);
- } finally {
- await closeServer(server);
- await rm(dir, { recursive: true, force: true });
- }
- },
- );
- it("installs a marketplace plugin from a real zip URL source", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-marketplace-url-zip-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- let server: Server | undefined;
- try {
- const zipBuffer = createStoredZip([
- {
- path: "cdn-plugin-1.2.3/.claude-plugin/plugin.json",
- content: JSON.stringify({
- commands: "commands",
- name: "cdn-plugin",
- version: "1.2.3",
- }),
- },
- {
- path: "cdn-plugin-1.2.3/commands/deploy.md",
- content: [
- "---",
- "description: Deploy through CDN plugin",
- "allowed-tools: Bash",
- "---",
- "",
- "Run deploy with $ARGUMENTS.",
- ].join("\n"),
- },
- ]);
- const sha256 = sha256Buffer(zipBuffer);
- const fixture = await startZipFixtureServer({ "/cdn-plugin.zip": zipBuffer });
- server = fixture.server;
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "cdn-market",
- plugins: [
- {
- name: "cdn-plugin",
- source: {
- source: "url",
- type: "zip",
- url: `${fixture.baseUrl}/cdn-plugin.zip`,
- sha256,
- },
- },
- ],
- });
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- const diagnostics = await validateMarketplacePlugin({
- marketplace: "cdn-market",
- name: "cdn-plugin",
- storageRoot,
- });
- expect(diagnostics.filter((item) => item.severity === "error")).toEqual([]);
- const sourceDescribe = await describeMarketplacePlugin({
- marketplace: "cdn-market",
- name: "cdn-plugin",
- storageRoot,
- });
- expect(componentNames(sourceDescribe.components, "command")).toEqual(["deploy"]);
- const install = await installMarketplacePlugin({
- marketplace: "cdn-market",
- name: "cdn-plugin",
- storageRoot,
- });
- const record = install.installed.find((item) => item.id === "cdn-plugin@cdn-market");
- expect(record).toMatchObject({
- marketplace: "cdn-market",
- name: "cdn-plugin",
- version: "1.2.3",
- });
- expect(record?.installPath).toContain(join("cache", "cdn-market", "cdn-plugin", "1.2.3"));
- expect(record?.source).toMatchObject({ source: "url", type: "zip", sha256 });
- const outcome = await createNodePluginAdapter({ storageRoot }).discoverPlugins({
- config: {
- dirs: [],
- enabled: true,
- enabledPlugins: {
- "cdn-plugin@cdn-market": true,
- },
- options: {},
- },
- env: {},
- storageRoot,
- workingDirectory: dir,
- });
- expect(outcome.plugins.find((plugin) => plugin.id === "cdn-plugin@cdn-market")).toMatchObject(
- {
- version: "1.2.3",
- },
- );
- expect(outcome.commandRoots[0]?.path).toContain(join("cdn-plugin", "1.2.3", "commands"));
- } finally {
- await closeServer(server);
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("does not fall back to a local plugin when an explicit URL source is invalid", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-invalid-url-source-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "invalid-url-market",
- plugins: [
- {
- name: "invalid-url-plugin",
- source: {
- source: "url",
- type: "zip",
- sha256: "0".repeat(64),
- },
- },
- ],
- });
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- const localFallbackRoot = join(
- storageRoot,
- "marketplaces",
- "invalid-url-market",
- "invalid-url-plugin",
- );
- await writePluginManifest(localFallbackRoot, {
- name: "invalid-url-plugin",
- version: "1.0.0",
- });
- await expect(
- installMarketplacePlugin({
- marketplace: "invalid-url-market",
- name: "invalid-url-plugin",
- storageRoot,
- }),
- ).rejects.toThrow(/requires a non-empty url/u);
- expect(listInstalledPluginRecords(storageRoot)).toEqual([]);
- expect(existsSync(join(storageRoot, "cache", "invalid-url-market"))).toBe(false);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("reports invalid zip source option shapes", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-invalid-zip-shape-"));
- const marketplaceRoot = join(dir, "marketplace");
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "invalid-zip-shape-market",
- plugins: [
- {
- name: "invalid-headers",
- source: {
- headers: [],
- sha256: "0".repeat(64),
- source: "url",
- type: "zip",
- url: "https://cdn.example.com/plugin.zip",
- },
- },
- {
- name: "invalid-path",
- source: {
- path: 42,
- sha256: "0".repeat(64),
- source: "url",
- type: "zip",
- url: "https://cdn.example.com/plugin.zip",
- },
- },
- {
- name: "invalid-strip-root",
- source: {
- sha256: "0".repeat(64),
- source: "url",
- stripRoot: "true",
- type: "zip",
- url: "https://cdn.example.com/plugin.zip",
- },
- },
- ],
- });
- const diagnostics = await validateMarketplaceSource({
- source: { source: "directory", path: marketplaceRoot },
- storageRoot: join(dir, "storage"),
- });
- const errors = diagnostics
- .filter((diagnostic) => diagnostic.severity === "error")
- .map((diagnostic) => diagnostic.message);
- expect(errors).toEqual(
- expect.arrayContaining([
- "Plugin zip source headers must be an object",
- "Plugin zip source path must be a string",
- "Plugin zip source stripRoot must be a boolean",
- ]),
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("retries source cleanup without replacing the primary error", async () => {
- const cleanupError = new Error("temporary directory is locked");
- const cleanup = vi.fn(async () => {
- throw cleanupError;
- });
- const returnedCleanupError = await cleanupPluginSourceBestEffort(cleanup, [0, 0, 0]);
- const primaryError = new Error("Plugin zip sha256 mismatch");
- const combinedError = appendPluginSourceCleanupError(primaryError, returnedCleanupError);
- expect(cleanup).toHaveBeenCalledTimes(3);
- expect(returnedCleanupError).toBe(cleanupError);
- expect(combinedError).toBe(primaryError);
- expect(primaryError.message).toContain("Plugin zip sha256 mismatch");
- expect(primaryError.message).toContain("plugin source cleanup also failed");
- expect(primaryError.message).toContain("temporary directory is locked");
- });
- it.each([
- { persistent: false, proxied: true },
- { persistent: true, proxied: true },
- { persistent: false, proxied: false },
- { persistent: true, proxied: false },
- ])(
- "retries a reset ZIP connection once ($persistent, proxy=$proxied)",
- async ({ persistent, proxied }) => {
- const zipBuffer = createStoredZip([
- {
- path: ".claude-plugin/plugin.json",
- content: JSON.stringify({ name: "reset-zip", version: "1.0.0" }),
- },
- ]);
- let requestCount = 0;
- const proxy = createServer((request, response) => {
- requestCount += 1;
- if (persistent || requestCount === 1) {
- request.socket.destroy();
- return;
- }
- response.writeHead(200, { "content-type": "application/zip" });
- response.end(zipBuffer);
- });
- let resolved: Awaited<ReturnType<typeof resolveZipPluginSource>> | undefined;
- try {
- const proxyBaseUrl = await listenLoopbackServer(proxy);
- vi.stubEnv(ZCODE_HTTP_PROXY_ENV_KEY, proxied ? proxyBaseUrl : "");
- vi.stubEnv(ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY, "{}");
- vi.stubEnv(ZCODE_NO_PROXY_ENV_KEY, "");
- const download = resolveZipPluginSource({
- sha256: sha256Buffer(zipBuffer),
- url: proxied ? "http://127.0.0.2/reset.zip" : `${proxyBaseUrl}/reset.zip`,
- });
- if (persistent) {
- await expect(download).rejects.toMatchObject({
- code: proxied ? "proxy_error" : "network_error",
- });
- } else {
- resolved = await download;
- expect(
- JSON.parse(await readFile(join(resolved.path, ".claude-plugin/plugin.json"), "utf8")),
- ).toEqual({ name: "reset-zip", version: "1.0.0" });
- }
- expect(requestCount).toBe(2);
- } finally {
- await resolved?.cleanup();
- await closeServer(proxy);
- }
- },
- );
- it.each(["CERT_HAS_EXPIRED", "ERR_TLS_CERT_ALTNAME_INVALID", "ENOTFOUND"])(
- "does not retry ZIP failures with code %s",
- async (code) => {
- const fetchMock = vi.fn().mockRejectedValue(
- new TypeError("fetch failed", {
- cause: Object.assign(new Error("download failed"), { code }),
- }),
- );
- vi.stubGlobal("fetch", fetchMock);
- await expect(
- resolveZipPluginSource({
- sha256: "0".repeat(64),
- url: "https://cdn.example.com/plugin.zip",
- }),
- ).rejects.toMatchObject({ code: "network_error" });
- expect(fetchMock).toHaveBeenCalledTimes(1);
- },
- );
- it("cancels ZIP download without retrying a reset connection", async () => {
- const controller = new AbortController();
- const fetchMock = vi.fn(async () => {
- controller.abort();
- throw Object.assign(new Error("connection reset"), { code: "ECONNRESET" });
- });
- vi.stubGlobal("fetch", fetchMock);
- await expect(
- resolveZipPluginSource({
- sha256: "0".repeat(64),
- url: "https://cdn.example.com/plugin.zip",
- signal: controller.signal,
- }),
- ).rejects.toMatchObject({ code: "cancelled" });
- expect(fetchMock).toHaveBeenCalledTimes(1);
- });
- it("downloads a zip through captured user proxy env", async () => {
- const zipBuffer = createStoredZip([
- {
- path: ".claude-plugin/plugin.json",
- content: JSON.stringify({ name: "proxied-zip", version: "1.0.0" }),
- },
- ]);
- const proxyRequests: string[] = [];
- const proxy = createServer((request, response) => {
- proxyRequests.push(request.url ?? "");
- response.writeHead(200, {
- "content-length": String(zipBuffer.byteLength),
- "content-type": "application/zip",
- });
- response.end(zipBuffer);
- });
- let resolved: Awaited<ReturnType<typeof resolveZipPluginSource>> | undefined;
- try {
- const proxyBaseUrl = await listenLoopbackServer(proxy);
- vi.stubEnv(ZCODE_HTTP_PROXY_ENV_KEY, "");
- vi.stubEnv(ZCODE_NO_PROXY_ENV_KEY, "");
- vi.stubEnv(ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY, JSON.stringify({ HTTP_PROXY: proxyBaseUrl }));
- const sourceUrl = "http://127.0.0.2/captured-proxy.zip";
- resolved = await resolveZipPluginSource({
- sha256: sha256Buffer(zipBuffer),
- url: sourceUrl,
- });
- expect(existsSync(join(resolved.path, ".claude-plugin", "plugin.json"))).toBe(true);
- expect(proxyRequests).toEqual([sourceUrl]);
- } finally {
- await resolved?.cleanup();
- await closeServer(proxy);
- }
- });
- it("drops custom zip headers on cross-origin redirects", async () => {
- const zipBuffer = createStoredZip([
- {
- path: ".claude-plugin/plugin.json",
- content: JSON.stringify({ name: "redirected-zip", version: "1.0.0" }),
- },
- ]);
- let originHeader: string | undefined;
- let targetHeader: string | undefined;
- const target = createServer((request, response) => {
- targetHeader = request.headers["x-zcode-channel"] as string | undefined;
- response.writeHead(200, {
- "content-length": String(zipBuffer.byteLength),
- "content-type": "application/zip",
- });
- response.end(zipBuffer);
- });
- let origin: Server | undefined;
- let resolved: Awaited<ReturnType<typeof resolveZipPluginSource>> | undefined;
- try {
- const targetBaseUrl = await listenLoopbackServer(target);
- origin = createServer((request, response) => {
- originHeader = request.headers["x-zcode-channel"] as string | undefined;
- response.writeHead(302, { location: `${targetBaseUrl}/plugin.zip` });
- response.end();
- });
- const originBaseUrl = await listenLoopbackServer(origin);
- vi.stubEnv(ZCODE_NO_PROXY_ENV_KEY, "127.0.0.1");
- resolved = await resolveZipPluginSource({
- headers: { "x-zcode-channel": "internal" },
- sha256: sha256Buffer(zipBuffer),
- url: `${originBaseUrl}/redirect.zip`,
- });
- expect(originHeader).toBe("internal");
- expect(targetHeader).toBeUndefined();
- } finally {
- await resolved?.cleanup();
- await closeServer(origin);
- await closeServer(target);
- }
- });
- it("rejects a zip redirect to non-loopback HTTP", async () => {
- const origin = createServer((_request, response) => {
- response.writeHead(302, { location: "http://example.com/plugin.zip" });
- response.end();
- });
- try {
- const originBaseUrl = await listenLoopbackServer(origin);
- vi.stubEnv(ZCODE_NO_PROXY_ENV_KEY, "127.0.0.1");
- await expect(
- resolveZipPluginSource({
- sha256: "0".repeat(64),
- url: `${originBaseUrl}/redirect.zip`,
- }),
- ).rejects.toThrow(/must be HTTPS/u);
- } finally {
- await closeServer(origin);
- }
- });
- it("rejects a zip root without a manifest before replacing existing cache", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-marketplace-url-zip-root-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const existingCacheRoot = join(
- storageRoot,
- "cache",
- "invalid-root-market",
- "invalid-root",
- "1.0.0",
- );
- let server: Server | undefined;
- try {
- const zipBuffer = createStoredZip([
- {
- path: "bundle-a/README.md",
- content: "First top-level directory.",
- },
- {
- path: "bundle-b/.claude-plugin/plugin.json",
- content: JSON.stringify({ name: "invalid-root", version: "1.0.0" }),
- },
- ]);
- const fixture = await startZipFixtureServer({ "/invalid-root.zip": zipBuffer });
- server = fixture.server;
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "invalid-root-market",
- plugins: [
- {
- name: "invalid-root",
- version: "1.0.0",
- source: {
- source: "url",
- type: "zip",
- url: `${fixture.baseUrl}/invalid-root.zip`,
- sha256: sha256Buffer(zipBuffer),
- },
- },
- ],
- });
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- await mkdir(existingCacheRoot, { recursive: true });
- await writeFile(join(existingCacheRoot, "keep.txt"), "existing cache");
- await expect(
- installMarketplacePlugin({
- marketplace: "invalid-root-market",
- name: "invalid-root",
- storageRoot,
- }),
- ).rejects.toThrow("Plugin manifest not found: invalid-root@invalid-root-market");
- expect(await readFile(join(existingCacheRoot, "keep.txt"), "utf8")).toBe("existing cache");
- expect(listInstalledPluginRecords(storageRoot)).toEqual([]);
- } finally {
- await closeServer(server);
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("rejects marketplace zip sources with mismatched sha256 without installing", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-marketplace-url-zip-sha-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- let server: Server | undefined;
- try {
- const zipBuffer = createStoredZip([
- {
- path: ".claude-plugin/plugin.json",
- content: JSON.stringify({ name: "bad-zip", version: "1.0.0" }),
- },
- ]);
- const fixture = await startZipFixtureServer({ "/bad.zip": zipBuffer });
- server = fixture.server;
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "bad-zip-market",
- plugins: [
- {
- name: "bad-zip",
- source: {
- source: "url",
- type: "zip",
- url: `${fixture.baseUrl}/bad.zip`,
- sha256: "0".repeat(64),
- },
- },
- ],
- });
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- await expect(
- installMarketplacePlugin({
- marketplace: "bad-zip-market",
- name: "bad-zip",
- storageRoot,
- }),
- ).rejects.toThrow("sha256 mismatch");
- expect(listInstalledPluginRecords(storageRoot)).toEqual([]);
- expect(existsSync(join(storageRoot, "cache", "bad-zip-market", "bad-zip"))).toBe(false);
- } finally {
- await closeServer(server);
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("rejects unsafe zip entry paths before cache materialization", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-marketplace-url-zip-slip-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- let server: Server | undefined;
- try {
- const zipBuffer = createStoredZip([
- {
- path: "../escape.txt",
- content: "escape",
- },
- {
- path: ".claude-plugin/plugin.json",
- content: JSON.stringify({ name: "zip-slip", version: "1.0.0" }),
- },
- ]);
- const fixture = await startZipFixtureServer({ "/slip.zip": zipBuffer });
- server = fixture.server;
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "zip-slip-market",
- plugins: [
- {
- name: "zip-slip",
- source: {
- source: "url",
- type: "zip",
- url: `${fixture.baseUrl}/slip.zip`,
- sha256: sha256Buffer(zipBuffer),
- },
- },
- ],
- });
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- await expect(
- installMarketplacePlugin({
- marketplace: "zip-slip-market",
- name: "zip-slip",
- storageRoot,
- }),
- ).rejects.toThrow(/Unsafe plugin zip path|invalid relative path/u);
- expect(listInstalledPluginRecords(storageRoot)).toEqual([]);
- expect(existsSync(join(storageRoot, "cache", "zip-slip-market", "zip-slip"))).toBe(false);
- } finally {
- await closeServer(server);
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("adds a public GitHub marketplace and installs its GitHub plugin without system Git", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-github-archive-no-git-"));
- const storageRoot = join(dir, "storage");
- const marketplaceArchive = createStoredZip([
- {
- path: "catalog-release/.claude-plugin/marketplace.json",
- content: JSON.stringify({
- name: "archive-market",
- plugins: [
- {
- name: "archive-plugin",
- source: {
- source: "github",
- repo: "acme/archive-plugin",
- sha: "abc123def456",
- path: "packages/archive-plugin",
- },
- },
- ],
- }),
- },
- ]);
- const pluginArchive = createStoredZip([
- {
- path: "archive-plugin-abc123/packages/archive-plugin/.claude-plugin/plugin.json",
- content: JSON.stringify({ name: "archive-plugin", version: "1.2.3" }),
- },
- {
- path: "archive-plugin-abc123/packages/archive-plugin/skills/demo/SKILL.md",
- content: "# demo",
- },
- ]);
- const requestedUrls: string[] = [];
- try {
- vi.stubEnv("PATH", join(dir, "no-system-git"));
- vi.stubEnv("HTTP_PROXY", "");
- vi.stubEnv("HTTPS_PROXY", "");
- vi.stubGlobal(
- "fetch",
- vi.fn(async (url: string | URL | Request) => {
- const value = String(url);
- requestedUrls.push(value);
- const body = value.includes("/acme/archive-plugin/") ? pluginArchive : marketplaceArchive;
- return new Response(body, {
- headers: { "content-type": "application/zip" },
- status: 200,
- });
- }),
- );
- const marketplace = await addMarketplace({
- source: { source: "github", repo: "acme/catalog", ref: "release" },
- storageRoot,
- });
- const install = await installMarketplacePlugin({
- marketplace: "archive-market",
- name: "archive-plugin",
- storageRoot,
- });
- expect(marketplace.id).toBe("archive-market");
- expect(requestedUrls).toEqual([
- "https://api.github.com/repos/acme/catalog/zipball/release",
- "https://api.github.com/repos/acme/archive-plugin/zipball/abc123def456",
- ]);
- expect(install.installed[0]).toMatchObject({
- id: "archive-plugin@archive-market",
- version: "1.2.3",
- });
- expect(
- existsSync(join(install.installed[0]?.installPath ?? "", "skills", "demo", "SKILL.md")),
- ).toBe(true);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("preserves Git sparse checkout semantics for public GitHub marketplace sources", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-github-sparse-marketplace-"));
- const storageRoot = join(dir, "storage");
- const archive = createStoredZip([
- {
- path: "catalog-main/.claude-plugin/marketplace.json",
- content: JSON.stringify({ name: "sparse-market", plugins: [] }),
- },
- ]);
- const fetchMock = vi.fn(
- async () =>
- new Response(archive, { headers: { "content-type": "application/zip" }, status: 200 }),
- );
- try {
- vi.stubEnv("PATH", join(dir, "no-system-git"));
- vi.stubEnv("HTTP_PROXY", "");
- vi.stubEnv("HTTPS_PROXY", "");
- vi.stubGlobal("fetch", fetchMock);
- await expect(
- addMarketplace({
- source: {
- source: "github",
- repo: "acme/sparse-catalog",
- sparsePaths: [".claude-plugin"],
- },
- storageRoot,
- }),
- ).rejects.toThrow(/System Git is required.*acme\/sparse-catalog.*Agent Host/u);
- expect(fetchMock).not.toHaveBeenCalled();
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("falls back to Git when a plugin subdirectory inherits LFS attributes from an ancestor", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-github-ancestor-lfs-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const archive = createStoredZip([
- {
- path: "lfs-plugin-main/packages/.gitattributes",
- content: "*.bin filter=lfs diff=lfs merge=lfs -text\n",
- },
- {
- path: "lfs-plugin-main/packages/plugin/.claude-plugin/plugin.json",
- content: JSON.stringify({ name: "lfs-plugin", version: "1.0.0" }),
- },
- {
- path: "lfs-plugin-main/packages/plugin/model.bin",
- content:
- "version https://git-lfs.github.com/spec/v1\noid sha256:0123456789abcdef\nsize 42\n",
- },
- ]);
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "ancestor-lfs-market",
- plugins: [
- {
- name: "lfs-plugin",
- source: {
- source: "github",
- repo: "acme/lfs-plugin",
- path: "packages/plugin",
- },
- },
- ],
- });
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- vi.stubEnv("PATH", join(dir, "no-system-git"));
- vi.stubEnv("HTTP_PROXY", "");
- vi.stubEnv("HTTPS_PROXY", "");
- vi.stubGlobal(
- "fetch",
- vi.fn(
- async () =>
- new Response(archive, {
- headers: { "content-type": "application/zip" },
- status: 200,
- }),
- ),
- );
- await expect(
- installMarketplacePlugin({
- marketplace: "ancestor-lfs-market",
- name: "lfs-plugin",
- storageRoot,
- }),
- ).rejects.toThrow(/System Git is required.*acme\/lfs-plugin.*Agent Host/u);
- expect(listInstalledPluginRecords(storageRoot)).toEqual([]);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it.each([
- [
- "git",
- {
- source: "git",
- url: "https://github.com/acme/source-variants.git",
- ref: "ignored-ref",
- sha: "pinned-sha",
- path: "packages/source-variant",
- },
- ],
- [
- "url:git",
- {
- source: "url",
- type: "git",
- url: "https://github.com/acme/source-variants.git",
- ref: "ignored-ref",
- commit: "pinned-sha",
- path: "packages/source-variant",
- },
- ],
- [
- "git-subdir",
- {
- source: "git-subdir",
- url: "acme/source-variants",
- ref: "ignored-ref",
- sha: "pinned-sha",
- path: "packages/source-variant",
- },
- ],
- ])(
- "materializes public GitHub %s plugin sources through the pinned Archive",
- async (_label, source) => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-github-source-variant-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const archive = createStoredZip([
- {
- path: "source-variants-pin/packages/source-variant/.claude-plugin/plugin.json",
- content: JSON.stringify({ name: "source-variant", version: "2.0.0" }),
- },
- ]);
- const fetchMock = vi.fn(
- async () =>
- new Response(archive, { headers: { "content-type": "application/zip" }, status: 200 }),
- );
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "source-variant-market",
- plugins: [{ name: "source-variant", source }],
- });
- await addMarketplace({
- source: { source: "directory", path: marketplaceRoot },
- storageRoot,
- });
- vi.stubEnv("PATH", join(dir, "no-system-git"));
- vi.stubEnv("HTTP_PROXY", "");
- vi.stubEnv("HTTPS_PROXY", "");
- vi.stubGlobal("fetch", fetchMock);
- const install = await installMarketplacePlugin({
- marketplace: "source-variant-market",
- name: "source-variant",
- storageRoot,
- });
- expect(fetchMock).toHaveBeenCalledWith(
- "https://api.github.com/repos/acme/source-variants/zipball/pinned-sha",
- expect.anything(),
- );
- expect(install.installed[0]?.version).toBe("2.0.0");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- },
- );
- it("returns an actionable error when a non-GitHub source requires unavailable system Git", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-system-git-unavailable-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "git-required-market",
- plugins: [
- {
- name: "private-plugin",
- source: {
- source: "git",
- url: "ssh://git@example.test/private/plugin.git",
- },
- },
- ],
- });
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- vi.stubEnv("PATH", join(dir, "no-system-git"));
- await expect(
- installMarketplacePlugin({
- marketplace: "git-required-market",
- name: "private-plugin",
- storageRoot,
- }),
- ).rejects.toThrow(
- /System Git is required.*ssh:\/\/example\.test\/private\/plugin\.git.*Agent Host/u,
- );
- expect(listInstalledPluginRecords(storageRoot)).toEqual([]);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("redacts Git URL credentials from unavailable and persisted refresh diagnostics", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-git-credential-redaction-"));
- const storageRoot = join(dir, "storage");
- const credentialSource = "https://private-user:private-secret@example.test/plugins.git";
- try {
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "credential-market",
- source: { source: "git", url: credentialSource },
- name: "credential-market",
- addedAt: "2026-01-01T00:00:00.000Z",
- pluginCount: 0,
- },
- ]);
- vi.stubEnv("ZCODE_GIT_BINARY", join(dir, "missing-git"));
- expect(await updateMarketplace({ marketplace: "credential-market", storageRoot })).toEqual(
- [],
- );
- const message = loadKnownMarketplacesSync(storageRoot)[0]?.lastRefreshFailure?.message ?? "";
- expect(message).toContain("https://example.test/plugins.git");
- expect(message).not.toContain("private-user");
- expect(message).not.toContain("private-secret");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("redacts credentials from nested GitHub Archive failure messages", () => {
- const error = createArchiveFetchError(
- "https://source-user:source-secret@github.com/acme/plugin.git",
- new Error(
- "request to https://cause-user:cause-secret@codeload.github.com/acme/plugin/zip failed",
- ),
- );
- expect(error.message).toContain("https://github.com/acme/plugin.git");
- expect(error.message).toContain("https://codeload.github.com/acme/plugin/zip");
- expect(error.message).not.toMatch(/source-user|source-secret|cause-user|cause-secret/u);
- expect((error.cause as Error | undefined)?.message).not.toMatch(/cause-user|cause-secret/u);
- });
- it("does not fall back to system Git for a GitHub Archive server failure", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-github-archive-network-failure-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "archive-failure-market",
- plugins: [
- {
- name: "archive-failure",
- source: { source: "git", url: "https://github.com/acme/archive-failure.git" },
- },
- ],
- });
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- vi.stubEnv("PATH", join(dir, "no-system-git"));
- vi.stubEnv("HTTP_PROXY", "");
- vi.stubEnv("HTTPS_PROXY", "");
- vi.stubGlobal(
- "fetch",
- vi.fn(async () => new Response("unavailable", { status: 503 })),
- );
- await expect(
- installMarketplacePlugin({
- marketplace: "archive-failure-market",
- name: "archive-failure",
- storageRoot,
- }),
- ).rejects.toThrow(/Failed to materialize public GitHub plugin source archive.*503/u);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("falls back from an anonymous GitHub 404 and explains when system Git is unavailable", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-github-private-fallback-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "private-github-market",
- plugins: [
- {
- name: "private-github",
- source: { source: "github", repo: "acme/private-github" },
- },
- ],
- });
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- vi.stubEnv("PATH", join(dir, "no-system-git"));
- vi.stubEnv("HTTP_PROXY", "");
- vi.stubEnv("HTTPS_PROXY", "");
- vi.stubGlobal(
- "fetch",
- vi.fn(async () => new Response("not found", { status: 404 })),
- );
- await expect(
- installMarketplacePlugin({
- marketplace: "private-github-market",
- name: "private-github",
- storageRoot,
- }),
- ).rejects.toThrow(
- /System Git is required.*https:\/\/github\.com\/acme\/private-github\.git/u,
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("isolates marketplace refresh failures and clears the persisted failure after recovery", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-marketplace-refresh-isolation-"));
- const storageRoot = join(dir, "storage");
- const goodSource = join(dir, "good-source");
- const badSource = join(dir, "bad-source");
- const oldBadSnapshot = join(storageRoot, "marketplaces", "bad-market");
- try {
- await writeMarketplaceManifest(goodSource, {
- name: "good-market",
- plugins: [{ name: "good-v2", source: "./good-v2" }],
- });
- await mkdir(oldBadSnapshot, { recursive: true });
- await writeFile(
- join(oldBadSnapshot, "marketplace.json"),
- JSON.stringify({ name: "bad-market", plugins: [{ name: "bad-v1", source: "./bad-v1" }] }),
- );
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "good-market",
- source: { source: "directory", path: goodSource },
- name: "good-market",
- addedAt: "2026-01-01T00:00:00.000Z",
- lastUpdated: "2026-01-01T00:00:00.000Z",
- pluginCount: 1,
- },
- {
- id: "bad-market",
- source: { source: "directory", path: badSource },
- name: "bad-market",
- addedAt: "2026-01-01T00:00:00.000Z",
- lastUpdated: "2026-01-01T00:00:00.000Z",
- pluginCount: 1,
- },
- ]);
- vi.stubGlobal(
- "fetch",
- vi.fn(async () => new Response("[]", { status: 200 })),
- );
- const updated = await updateMarketplace({ storageRoot });
- expect(updated.map((record) => record.id)).toContain("good-market");
- expect(loadMarketplaceManifestSync(storageRoot, "bad-market")?.plugins[0]?.name).toBe(
- "bad-v1",
- );
- expect(
- loadKnownMarketplacesSync(storageRoot).find((record) => record.id === "bad-market")
- ?.lastRefreshFailure,
- ).toMatchObject({ code: "plugin_marketplace_invalid", failedAt: expect.any(String) });
- await writeMarketplaceManifest(badSource, {
- name: "bad-market",
- plugins: [{ name: "bad-v2", source: "./bad-v2" }],
- });
- await updateMarketplace({ marketplace: "bad-market", storageRoot });
- expect(loadMarketplaceManifestSync(storageRoot, "bad-market")?.plugins[0]?.name).toBe(
- "bad-v2",
- );
- expect(
- loadKnownMarketplacesSync(storageRoot).find((record) => record.id === "bad-market")
- ?.lastRefreshFailure,
- ).toBeUndefined();
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("does not persist an operation cancellation as a marketplace refresh failure", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-marketplace-refresh-cancel-"));
- const storageRoot = join(dir, "storage");
- const controller = new AbortController();
- try {
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "cancelled-market",
- source: { source: "github", repo: "acme/cancelled-market" },
- name: "cancelled-market",
- addedAt: "2026-01-01T00:00:00.000Z",
- lastUpdated: "2026-01-01T00:00:00.000Z",
- pluginCount: 0,
- },
- ]);
- vi.stubGlobal(
- "fetch",
- vi.fn(
- (_input: RequestInfo | URL, init?: RequestInit) =>
- new Promise<Response>((_resolve, reject) => {
- const signal = init?.signal;
- if (signal?.aborted) {
- reject(signal.reason);
- return;
- }
- signal?.addEventListener("abort", () => reject(signal.reason), { once: true });
- }),
- ),
- );
- const refresh = updateMarketplace({
- marketplace: "cancelled-market",
- signal: controller.signal,
- storageRoot,
- });
- await vi.waitFor(() => expect(fetch).toHaveBeenCalled());
- controller.abort();
- await expect(refresh).rejects.toBeDefined();
- expect(
- loadKnownMarketplacesSync(storageRoot).find((record) => record.id === "cancelled-market")
- ?.lastRefreshFailure,
- ).toBeUndefined();
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("keeps the previous Claude settings snapshot when timeout fires during manifest persistence", async () => {
- vi.useFakeTimers();
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-claude-settings-timeout-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(storageRoot, "marketplaces", "claude-plugins-official");
- try {
- await mkdir(marketplaceRoot, { recursive: true });
- await writeFile(
- join(marketplaceRoot, "marketplace.json"),
- JSON.stringify({
- name: "claude-plugins-official",
- plugins: [{ name: "cached", source: "./cached" }],
- }),
- );
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "claude-plugins-official",
- source: {
- source: "settings",
- marketplace: {
- name: "claude-plugins-official",
- plugins: [],
- raw: { name: "claude-plugins-official", plugins: [] },
- },
- },
- name: "claude-plugins-official",
- addedAt: "2026-01-01T00:00:00.000Z",
- lastUpdated: "2026-01-02T00:00:00.000Z",
- pluginCount: 1,
- },
- ]);
- for (const key of [
- ZCODE_HTTP_PROXY_ENV_KEY,
- "HTTP_PROXY",
- "HTTPS_PROXY",
- "ALL_PROXY",
- "http_proxy",
- "https_proxy",
- "all_proxy",
- ZCODE_AGENT_CA_CERT_ENV_KEY,
- "NODE_EXTRA_CA_CERTS",
- ]) {
- vi.stubEnv(key, "");
- }
- vi.stubEnv(ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY, "{}");
- vi.stubGlobal(
- "fetch",
- vi.fn(async () => new Response("[]", { status: 200 })),
- );
- const freshRaw = {
- name: "claude-plugins-official",
- plugins: [{ name: "fresh", source: "./fresh" }],
- // 回归触发点:旧实现会在序列化 active manifest 时触发 deadline,但仍覆盖旧文件。
- toJSON: () => {
- vi.advanceTimersByTime(30_000);
- return {
- name: "claude-plugins-official",
- plugins: [{ name: "fresh", source: "./fresh" }],
- };
- },
- };
- await expect(
- addMarketplace({
- source: {
- source: "settings",
- marketplace: {
- name: "claude-plugins-official",
- plugins: [
- {
- name: "fresh",
- source: "./fresh",
- raw: { name: "fresh", source: "./fresh" },
- },
- ],
- raw: freshRaw,
- },
- },
- storageRoot,
- trustedId: "claude-plugins-official",
- }),
- ).rejects.toThrow("30 seconds");
- expect(
- loadMarketplaceManifestSync(storageRoot, "claude-plugins-official")?.plugins[0]?.name,
- ).toBe("cached");
- expect(
- loadKnownMarketplacesSync(storageRoot).find(
- (record) => record.id === "claude-plugins-official",
- ),
- ).toMatchObject({
- lastUpdated: "2026-01-02T00:00:00.000Z",
- pluginCount: 1,
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- vi.useRealTimers();
- }
- });
- it("rolls back a settings marketplace manifest when known state persistence fails", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-settings-known-state-failure-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(storageRoot, "marketplaces", "settings-market");
- try {
- await mkdir(marketplaceRoot, { recursive: true });
- await writeFile(
- join(marketplaceRoot, "marketplace.json"),
- JSON.stringify({
- name: "settings-market",
- plugins: [{ name: "cached", source: "./cached" }],
- }),
- );
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "settings-market",
- source: {
- source: "settings",
- marketplace: {
- name: "settings-market",
- plugins: [],
- raw: { name: "settings-market", plugins: [] },
- },
- },
- name: "settings-market",
- addedAt: "2026-01-01T00:00:00.000Z",
- lastUpdated: "2026-01-02T00:00:00.000Z",
- pluginCount: 1,
- },
- ]);
- let serializationCount = 0;
- const freshRaw = {
- name: "settings-market",
- plugins: [{ name: "fresh", source: "./fresh" }],
- toJSON: () => {
- serializationCount += 1;
- if (serializationCount === 2) {
- throw new Error("known state persistence failed");
- }
- return {
- name: "settings-market",
- plugins: [{ name: "fresh", source: "./fresh" }],
- };
- },
- };
- await expect(
- addMarketplace({
- source: {
- source: "settings",
- marketplace: {
- name: "settings-market",
- plugins: [
- {
- name: "fresh",
- source: "./fresh",
- raw: { name: "fresh", source: "./fresh" },
- },
- ],
- raw: freshRaw,
- },
- },
- storageRoot,
- }),
- ).rejects.toThrow("known state persistence failed");
- expect(loadMarketplaceManifestSync(storageRoot, "settings-market")?.plugins[0]?.name).toBe(
- "cached",
- );
- expect(
- loadKnownMarketplacesSync(storageRoot).find((record) => record.id === "settings-market"),
- ).toMatchObject({
- lastUpdated: "2026-01-02T00:00:00.000Z",
- pluginCount: 1,
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("rolls back Claude manifest and known state when timeout fires during authority persistence", async () => {
- vi.useFakeTimers();
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-claude-authority-timeout-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(storageRoot, "marketplaces", "claude-plugins-official");
- try {
- await mkdir(marketplaceRoot, { recursive: true });
- await writeFile(
- join(marketplaceRoot, "marketplace.json"),
- JSON.stringify({
- name: "claude-plugins-official",
- plugins: [{ name: "cached", source: "./cached" }],
- }),
- );
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "claude-plugins-official",
- source: {
- source: "settings",
- marketplace: {
- name: "claude-plugins-official",
- plugins: [],
- raw: { name: "claude-plugins-official", plugins: [] },
- },
- },
- name: "claude-plugins-official",
- addedAt: "2026-01-01T00:00:00.000Z",
- lastUpdated: "2026-01-02T00:00:00.000Z",
- pluginCount: 1,
- },
- ]);
- for (const key of [
- ZCODE_HTTP_PROXY_ENV_KEY,
- "HTTP_PROXY",
- "HTTPS_PROXY",
- "ALL_PROXY",
- "http_proxy",
- "https_proxy",
- "all_proxy",
- ZCODE_AGENT_CA_CERT_ENV_KEY,
- "NODE_EXTRA_CA_CERTS",
- ]) {
- vi.stubEnv(key, "");
- }
- vi.stubEnv(ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY, "{}");
- vi.stubGlobal(
- "fetch",
- vi.fn(async () => new Response("[]", { status: 200 })),
- );
- let serializationCount = 0;
- const freshRaw = {
- name: "claude-plugins-official",
- plugins: [{ name: "fresh", source: "./fresh" }],
- toJSON: () => {
- serializationCount += 1;
- // 第一次序列化 staging manifest,第二次序列化 authority state。
- if (serializationCount === 2) {
- vi.advanceTimersByTime(30_000);
- }
- return {
- name: "claude-plugins-official",
- plugins: [{ name: "fresh", source: "./fresh" }],
- };
- },
- };
- await expect(
- addMarketplace({
- source: {
- source: "settings",
- marketplace: {
- name: "claude-plugins-official",
- plugins: [
- {
- name: "fresh",
- source: "./fresh",
- raw: { name: "fresh", source: "./fresh" },
- },
- ],
- raw: freshRaw,
- },
- },
- storageRoot,
- trustedId: "claude-plugins-official",
- }),
- ).rejects.toThrow("30 seconds");
- expect(
- loadMarketplaceManifestSync(storageRoot, "claude-plugins-official")?.plugins[0]?.name,
- ).toBe("cached");
- expect(
- loadKnownMarketplacesSync(storageRoot).find(
- (record) => record.id === "claude-plugins-official",
- ),
- ).toMatchObject({
- lastUpdated: "2026-01-02T00:00:00.000Z",
- pluginCount: 1,
- });
- expect(
- existsSync(join(storageRoot, "marketplaces", ".claude-plugins-official.transaction.json")),
- ).toBe(false);
- } finally {
- await rm(dir, { force: true, recursive: true });
- vi.useRealTimers();
- }
- });
- it("fails Claude marketplace refresh after 30 seconds without blocking other markets", async () => {
- vi.useFakeTimers();
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-claude-marketplace-timeout-"));
- const storageRoot = join(dir, "storage");
- const claudeSnapshot = join(storageRoot, "marketplaces", "claude-plugins-official");
- const goodSource = join(dir, "good-source");
- try {
- await mkdir(claudeSnapshot, { recursive: true });
- await writeFile(
- join(claudeSnapshot, "marketplace.json"),
- JSON.stringify({
- name: "claude-plugins-official",
- plugins: [{ name: "cached", source: "./cached" }],
- }),
- );
- await writeMarketplaceManifest(goodSource, {
- name: "good-market",
- plugins: [{ name: "good-v2", source: "./good-v2" }],
- });
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "claude-plugins-official",
- source: { source: "github", repo: "anthropics/claude-plugins-official" },
- name: "claude-plugins-official",
- addedAt: "2026-01-01T00:00:00.000Z",
- lastUpdated: "2026-01-02T00:00:00.000Z",
- pluginCount: 1,
- },
- {
- id: "good-market",
- source: { source: "directory", path: goodSource },
- name: "good-market",
- addedAt: "2026-01-01T00:00:00.000Z",
- lastUpdated: "2026-01-01T00:00:00.000Z",
- pluginCount: 1,
- },
- {
- id: "zcode-plugins-official",
- source: {
- source: "settings",
- marketplace: {
- name: "zcode-plugins-official",
- plugins: [],
- raw: { name: "zcode-plugins-official", plugins: [] },
- },
- },
- name: "zcode-plugins-official",
- addedAt: "2026-01-01T00:00:00.000Z",
- pluginCount: 0,
- },
- ]);
- vi.stubGlobal(
- "fetch",
- vi.fn(
- (_input: RequestInfo | URL, init?: RequestInit) =>
- new Promise<Response>((_resolve, reject) => {
- const signal = init?.signal;
- if (signal?.aborted) {
- reject(signal.reason);
- return;
- }
- signal?.addEventListener("abort", () => reject(signal.reason), { once: true });
- }),
- ),
- );
- const refresh = updateMarketplace({ storageRoot });
- for (let turn = 0; turn < 10 && vi.mocked(fetch).mock.calls.length === 0; turn += 1) {
- await Promise.resolve();
- await vi.advanceTimersByTimeAsync(0);
- }
- await vi.advanceTimersByTimeAsync(30_000);
- const updated = await refresh;
- expect(updated.map((record) => record.id)).toContain("good-market");
- expect(updated.map((record) => record.id)).not.toContain("claude-plugins-official");
- expect(
- loadMarketplaceManifestSync(storageRoot, "claude-plugins-official")?.plugins[0]?.name,
- ).toBe("cached");
- expect(
- loadKnownMarketplacesSync(storageRoot).find(
- (record) => record.id === "claude-plugins-official",
- )?.lastRefreshFailure,
- ).toMatchObject({
- code: "plugin_archive_fetch_failed",
- message: expect.stringContaining("30 seconds"),
- });
- expect(loadMarketplaceManifestSync(storageRoot, "good-market")?.plugins[0]?.name).toBe(
- "good-v2",
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- vi.useRealTimers();
- }
- });
- it("keeps the active directory when atomic staging cannot copy the replacement", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-atomic-directory-"));
- const targetPath = join(dir, "active");
- try {
- await mkdir(targetPath, { recursive: true });
- await writeFile(join(targetPath, "keep.txt"), "v1");
- await expect(
- replaceDirectoryAtomically({
- sourcePath: join(dir, "missing-source"),
- targetPath,
- }),
- ).rejects.toThrow();
- expect(await readFile(join(targetPath, "keep.txt"), "utf8")).toBe("v1");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("recovers state files and marketplace directories left in the atomic commit window", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-atomic-recovery-"));
- const storageRoot = join(dir, "storage");
- const installedPath = join(storageRoot, "installed_plugins.json");
- const marketplacePath = join(storageRoot, "marketplaces", "recover-market");
- try {
- await mkdir(marketplacePath, { recursive: true });
- await writeFile(
- installedPath,
- JSON.stringify({
- version: 1,
- plugins: [
- {
- id: "recover-plugin@recover-market",
- name: "recover-plugin",
- marketplace: "recover-market",
- version: "1.0.0",
- installPath: join(storageRoot, "cache", "recover-market", "recover-plugin", "1.0.0"),
- installedAt: "2026-01-01T00:00:00.000Z",
- updatedAt: "2026-01-01T00:00:00.000Z",
- scope: "user",
- },
- ],
- }),
- );
- await writeFile(
- join(marketplacePath, "marketplace.json"),
- JSON.stringify({
- name: "recover-market",
- plugins: [{ name: "recover-plugin", source: "./recover-plugin" }],
- }),
- );
- await rename(installedPath, join(storageRoot, ".installed_plugins.json.backup"));
- await rename(marketplacePath, join(storageRoot, "marketplaces", ".recover-market.backup"));
- expect(
- (await listInstalledPluginRecordsAsync(storageRoot)).map((record) => record.id),
- ).toEqual(["recover-plugin@recover-market"]);
- expect(listInstalledPluginRecords(storageRoot).map((record) => record.id)).toEqual([
- "recover-plugin@recover-market",
- ]);
- expect(loadMarketplaceManifestSync(storageRoot, "recover-market")?.name).toBe(
- "recover-market",
- );
- expect(existsSync(installedPath)).toBe(true);
- expect(existsSync(marketplacePath)).toBe(true);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("does not let a normal read recover an active directory transaction", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-atomic-active-read-"));
- const authorityPath = join(dir, "installed_plugins.json");
- const sourcePath = join(dir, "source");
- const targetPath = join(dir, "active");
- try {
- await mkdir(sourcePath, { recursive: true });
- await mkdir(targetPath, { recursive: true });
- await writeFile(join(sourcePath, "version.txt"), "v2");
- await writeFile(join(targetPath, "version.txt"), "v1");
- await writeFile(authorityPath, JSON.stringify({ version: 1, plugins: [] }));
- const activation = await activateDirectoryAtomically({
- authorityPath,
- sourcePath,
- targetPath,
- });
- expect(
- await readFile(join(await resolveAtomicReadPath(targetPath), "version.txt"), "utf8"),
- ).toBe("v1");
- const readablePath = recoverAtomicTargetSync(targetPath);
- expect(await readFile(join(targetPath, "version.txt"), "utf8")).toBe("v2");
- expect(await readFile(join(readablePath, "version.txt"), "utf8")).toBe("v1");
- expect(existsSync(join(dir, ".active.backup"))).toBe(true);
- await activation.rollback();
- expect(await readFile(join(targetPath, "version.txt"), "utf8")).toBe("v1");
- const committedActivation = await activateDirectoryAtomically({
- authorityPath,
- sourcePath,
- targetPath,
- });
- await writeFile(
- authorityPath,
- JSON.stringify({
- version: 1,
- plugins: [{ cacheTransactionId: committedActivation.transactionId }],
- }),
- );
- expect(await resolveAtomicReadPath(targetPath)).toBe(targetPath);
- const committedReadablePath = recoverAtomicTargetSync(targetPath);
- expect(await readFile(join(committedReadablePath, "version.txt"), "utf8")).toBe("v2");
- expect(existsSync(join(dir, ".active.backup"))).toBe(true);
- await committedActivation.finalize();
- expect(existsSync(join(dir, ".active.backup"))).toBe(false);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("rejects a concurrent activation for the same target without losing its rollback snapshot", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-atomic-concurrent-target-"));
- const authorityPath = join(dir, "installed_plugins.json");
- const firstSourcePath = join(dir, "source-first");
- const secondSourcePath = join(dir, "source-second");
- const targetPath = join(dir, "active");
- let firstActivation: Awaited<ReturnType<typeof activateDirectoryAtomically>> | undefined;
- try {
- await mkdir(firstSourcePath, { recursive: true });
- await mkdir(secondSourcePath, { recursive: true });
- await mkdir(targetPath, { recursive: true });
- await writeFile(join(firstSourcePath, "version.txt"), "v2");
- await writeFile(join(secondSourcePath, "version.txt"), "v3");
- await writeFile(join(targetPath, "version.txt"), "v1");
- await writeFile(authorityPath, JSON.stringify({ version: 1, plugins: [] }));
- firstActivation = await activateDirectoryAtomically({
- authorityPath,
- sourcePath: firstSourcePath,
- targetPath,
- });
- await expect(
- activateDirectoryAtomically({
- authorityPath,
- sourcePath: secondSourcePath,
- targetPath,
- }),
- ).rejects.toThrow("already active");
- expect(
- await readFile(join(await resolveAtomicReadPath(targetPath), "version.txt"), "utf8"),
- ).toBe("v1");
- const readablePath = recoverAtomicTargetSync(targetPath);
- expect(await readFile(join(targetPath, "version.txt"), "utf8")).toBe("v2");
- expect(await readFile(join(readablePath, "version.txt"), "utf8")).toBe("v1");
- expect(existsSync(join(dir, ".active.backup"))).toBe(true);
- expect(existsSync(join(dir, ".active.transaction.json"))).toBe(true);
- await firstActivation.rollback();
- firstActivation = undefined;
- expect(await readFile(join(targetPath, "version.txt"), "utf8")).toBe("v1");
- expect(existsSync(join(dir, ".active.backup"))).toBe(false);
- expect(existsSync(join(dir, ".active.transaction.json"))).toBe(false);
- } finally {
- await firstActivation?.rollback().catch(() => {});
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("does not overwrite an atomic transaction owned by another live process", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-atomic-live-owner-"));
- const sourcePath = join(dir, "source");
- const targetPath = join(dir, "active");
- const transactionPath = join(dir, ".active.transaction.json");
- try {
- await mkdir(sourcePath, { recursive: true });
- await mkdir(targetPath, { recursive: true });
- await writeFile(join(sourcePath, "version.txt"), "v2");
- await writeFile(join(targetPath, "version.txt"), "v1");
- await writeFile(
- transactionPath,
- JSON.stringify({
- hadTarget: true,
- mode: "standalone",
- ownerId: "another-live-process",
- ownerPid: process.ppid,
- stageName: ".active.stage-other",
- transactionId: "live-transaction",
- version: 2,
- }),
- );
- await expect(
- activateDirectoryAtomically({
- sourcePath,
- targetPath,
- }),
- ).rejects.toThrow("already active");
- expect(await readFile(join(targetPath, "version.txt"), "utf8")).toBe("v1");
- expect(JSON.parse(await readFile(transactionPath, "utf8"))).toMatchObject({
- ownerId: "another-live-process",
- transactionId: "live-transaction",
- });
- expect(existsSync(join(dir, ".active.backup"))).toBe(false);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("recovers a coordinated directory according to the authority transaction generation", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-atomic-generation-"));
- const authorityPath = join(dir, "installed_plugins.json");
- const targetPath = join(dir, "active");
- const backupPath = join(dir, ".active.backup");
- const transactionPath = join(dir, ".active.transaction.json");
- const transactionId = "dead-transaction";
- const writeTransaction = async (): Promise<void> => {
- await writeFile(
- transactionPath,
- JSON.stringify({
- authorityPath,
- hadTarget: true,
- mode: "coordinated",
- ownerId: "dead-owner",
- ownerPid: 999_999_999,
- stageName: ".active.stage-dead",
- transactionId,
- version: 2,
- }),
- );
- };
- try {
- await mkdir(targetPath, { recursive: true });
- await mkdir(backupPath, { recursive: true });
- await writeFile(join(targetPath, "version.txt"), "v2");
- await writeFile(join(backupPath, "version.txt"), "v1");
- await writeFile(authorityPath, JSON.stringify({ version: 1, plugins: [] }));
- await writeTransaction();
- // 已死亡事务:读取先按权威代际恢复,再返回稳定 target;未提交 → 回滚到 backup 的 v1。
- expect(await resolveAtomicReadPath(targetPath)).toBe(targetPath);
- expect(await readFile(join(targetPath, "version.txt"), "utf8")).toBe("v1");
- expect(existsSync(backupPath)).toBe(false);
- expect(existsSync(transactionPath)).toBe(false);
- await rm(targetPath, { force: true, recursive: true });
- await mkdir(targetPath, { recursive: true });
- await mkdir(backupPath, { recursive: true });
- await writeFile(join(targetPath, "version.txt"), "v2");
- await writeFile(join(backupPath, "version.txt"), "v1");
- await writeFile(
- authorityPath,
- JSON.stringify({
- version: 1,
- plugins: [{ cacheTransactionId: transactionId }],
- }),
- );
- await writeTransaction();
- // 已提交 → 保留 target 的 v2,清理 backup。
- expect(await resolveAtomicReadPath(targetPath)).toBe(targetPath);
- expect(await readFile(join(targetPath, "version.txt"), "utf8")).toBe("v2");
- expect(existsSync(backupPath)).toBe(false);
- recoverAtomicTargetSync(targetPath);
- expect(await readFile(join(targetPath, "version.txt"), "utf8")).toBe("v2");
- expect(existsSync(backupPath)).toBe(false);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("rolls back earlier dependency cache activations when a later dependency install fails", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-install-transaction-rollback-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const rootSource = join(dir, "root-source");
- const goodSource = join(dir, "good-source");
- const badSource = join(dir, "bad-source");
- try {
- await writePluginManifest(rootSource, { name: "root", version: "1.0.0" }, ".claude-plugin");
- await writePluginManifest(goodSource, { name: "good", version: "1.0.0" }, ".claude-plugin");
- await writePluginManifest(badSource, { name: "bad", version: "1.0.0" }, ".claude-plugin");
- await writeFile(join(goodSource, "marker.txt"), "v1");
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "transaction-market",
- plugins: [
- {
- name: "root",
- dependencies: ["good", "bad"],
- source: { source: "directory", path: rootSource },
- },
- { name: "good", source: { source: "directory", path: goodSource } },
- { name: "bad", source: { source: "directory", path: badSource } },
- ],
- });
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- await installMarketplacePlugin({
- marketplace: "transaction-market",
- name: "root",
- storageRoot,
- });
- const goodRecord = listInstalledPluginRecords(storageRoot).find(
- (record) => record.id === "good@transaction-market",
- );
- expect(await readFile(join(goodRecord?.installPath ?? "", "marker.txt"), "utf8")).toBe("v1");
- await writeFile(join(goodSource, "marker.txt"), "v2");
- await rm(badSource, { force: true, recursive: true });
- await expect(
- installMarketplacePlugin({
- marketplace: "transaction-market",
- name: "root",
- storageRoot,
- }),
- ).rejects.toThrow(/does not exist/u);
- expect(await readFile(join(goodRecord?.installPath ?? "", "marker.txt"), "utf8")).toBe("v1");
- expect(
- listInstalledPluginRecords(storageRoot).find(
- (record) => record.id === "good@transaction-market",
- ),
- ).toEqual(goodRecord);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("discovers an installed cache offline without system Git or source network access", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-offline-installed-cache-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const pluginRoot = join(marketplaceRoot, "plugins", "offline-plugin");
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "offline-cache-market",
- plugins: [{ name: "offline-plugin", source: "./plugins/offline-plugin" }],
- });
- await writePluginManifest(
- pluginRoot,
- { name: "offline-plugin", skills: "skills", version: "1.0.0" },
- ".claude-plugin",
- );
- await mkdir(join(pluginRoot, "skills", "offline"), { recursive: true });
- await writeFile(join(pluginRoot, "skills", "offline", "SKILL.md"), "# offline");
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- await installMarketplacePlugin({
- marketplace: "offline-cache-market",
- name: "offline-plugin",
- storageRoot,
- });
- vi.stubEnv("PATH", join(dir, "no-system-git"));
- vi.stubGlobal(
- "fetch",
- vi.fn(async () => Promise.reject(new Error("offline"))),
- );
- const outcome = await discoverNodePlugins({
- config: {
- dirs: [],
- enabled: true,
- enabledPlugins: { "offline-plugin@offline-cache-market": true },
- options: {},
- },
- env: {},
- storageRoot,
- workingDirectory: dir,
- });
- expect(outcome.plugins).toContainEqual(
- expect.objectContaining({
- id: "offline-plugin@offline-cache-market",
- source: "cache",
- }),
- );
- expect(outcome.skillRoots).toContainEqual(
- expect.objectContaining({ path: expect.stringContaining(join("offline-plugin", "1.0.0")) }),
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("recovers a crashed cache replacement before discovery publishes runtime paths", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-crashed-cache-discovery-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const pluginRoot = join(marketplaceRoot, "plugins", "crash-plugin");
- const config = {
- dirs: [],
- enabled: true,
- enabledPlugins: { "crash-plugin@crash-market": true },
- options: {},
- };
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "crash-market",
- plugins: [{ name: "crash-plugin", source: "./plugins/crash-plugin" }],
- });
- await writePluginManifest(
- pluginRoot,
- { hooks: "hooks/hooks.json", name: "crash-plugin", skills: "skills", version: "1.0.0" },
- ".claude-plugin",
- );
- await mkdir(join(pluginRoot, "skills", "crash"), { recursive: true });
- await mkdir(join(pluginRoot, "hooks"), { recursive: true });
- await writeFile(join(pluginRoot, "skills", "crash", "SKILL.md"), "# crash");
- await writeFile(join(pluginRoot, "hooks", "session-start"), "echo start");
- await writeFile(
- join(pluginRoot, "hooks", "hooks.json"),
- JSON.stringify({
- hooks: {
- SessionStart: [
- {
- hooks: [
- { command: '"${CLAUDE_PLUGIN_ROOT}/hooks/session-start"', type: "command" },
- ],
- },
- ],
- },
- }),
- );
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- await installMarketplacePlugin({
- marketplace: "crash-market",
- name: "crash-plugin",
- storageRoot,
- });
- const [record] = listInstalledPluginRecords(storageRoot);
- const cachePath = record!.installPath!;
- const cacheParent = join(cachePath, "..");
- const backupPath = join(cacheParent, ".1.0.0.backup");
- // 模拟替换在 target→backup 之后、staging→target 之前崩溃:target 缺失,只剩 backup 和死亡 owner 的 marker。
- await rename(cachePath, backupPath);
- await writeFile(
- join(cacheParent, ".1.0.0.transaction.json"),
- JSON.stringify({
- hadTarget: true,
- mode: "standalone",
- ownerId: "dead-owner",
- ownerPid: 999_999_999,
- stageName: ".1.0.0.stage-dead",
- transactionId: "dead-transaction",
- version: 2,
- }),
- );
- const outcome = await discoverNodePlugins({
- config,
- env: {},
- storageRoot,
- workingDirectory: dir,
- });
- // 发布给会话的是稳定 target,而不是会被后续恢复改名走的 backup。
- const skillRoot = outcome.skillRoots.find((root) => root.path.includes("crash-plugin"));
- expect(skillRoot?.path).toBe(join(cachePath, "skills"));
- const hook = outcome.hooks.SessionStart?.[0]?.hooks[0];
- expect(hook).toMatchObject({ plugin: { rootPath: cachePath } });
- expect(existsSync(backupPath)).toBe(false);
- expect(existsSync(join(cacheParent, ".1.0.0.transaction.json"))).toBe(false);
- // 会话持有路径期间打开详情,不能再把会话正在用的目录移走。
- await describeMarketplacePlugin({
- marketplace: "crash-market",
- name: "crash-plugin",
- storageRoot,
- });
- expect(await readFile(join(skillRoot!.path, "crash", "SKILL.md"), "utf8")).toBe("# crash");
- expect(
- await readFile(
- join(
- (hook as { plugin: { rootPath: string } }).plugin.rootPath,
- "hooks",
- "session-start",
- ),
- "utf8",
- ),
- ).toBe("echo start");
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("keeps reading the committed generation of a live replacement during discovery", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-live-cache-discovery-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const pluginRoot = join(marketplaceRoot, "plugins", "live-plugin");
- const nextSource = join(dir, "next-source");
- let activation: Awaited<ReturnType<typeof activateDirectoryAtomically>> | undefined;
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "live-market",
- plugins: [{ name: "live-plugin", source: "./plugins/live-plugin" }],
- });
- await writePluginManifest(
- pluginRoot,
- { name: "live-plugin", skills: "skills", version: "1.0.0" },
- ".claude-plugin",
- );
- await mkdir(join(pluginRoot, "skills", "live"), { recursive: true });
- await writeFile(join(pluginRoot, "skills", "live", "SKILL.md"), "# v1");
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- await installMarketplacePlugin({
- marketplace: "live-market",
- name: "live-plugin",
- storageRoot,
- });
- const [record] = listInstalledPluginRecords(storageRoot);
- const cachePath = record!.installPath!;
- await cp(cachePath, nextSource, { recursive: true });
- await writeFile(join(nextSource, "skills", "live", "SKILL.md"), "# v2");
- // 同进程 writer 已激活但未 finalize:读者不能接管它的 backup。
- activation = await activateDirectoryAtomically({
- sourcePath: nextSource,
- targetPath: cachePath,
- });
- const outcome = await discoverNodePlugins({
- config: {
- dirs: [],
- enabled: true,
- enabledPlugins: { "live-plugin@live-market": true },
- options: {},
- },
- env: {},
- storageRoot,
- workingDirectory: dir,
- });
- const backupPath = join(cachePath, "..", ".1.0.0.backup");
- const skillRoot = outcome.skillRoots.find((root) => root.path.includes("live-plugin"));
- expect(skillRoot?.path).toBe(join(backupPath, "skills"));
- expect(existsSync(backupPath)).toBe(true);
- await activation.rollback();
- activation = undefined;
- expect(await readFile(join(cachePath, "skills", "live", "SKILL.md"), "utf8")).toBe("# v1");
- } finally {
- await activation?.rollback().catch(() => {});
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("materializes Claude command object mappings into custom command roots", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-command-metadata-"));
- const pluginRoot = join(dir, "commands-plugin");
- try {
- await writePluginManifest(pluginRoot, {
- commands: {
- "from-file": {
- argumentHint: "[topic]",
- description: "Generated from a source file",
- source: "./extra-command.md",
- },
- inline: {
- allowedTools: ["Bash", "Read"],
- content: "# Inline command\n\nRun the inline command.",
- description: "Generated from inline content",
- },
- },
- name: "commands-plugin",
- });
- await writeFile(
- join(pluginRoot, "extra-command.md"),
- "# Source command\n\nRun the source command.",
- );
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: dir,
- });
- const commands = await createNodeCustomCommandAdapter().discoverCommands({
- roots: outcome.commandRoots,
- workingDirectory: dir,
- });
- expect(commands.commands.map((command) => command.name)).toEqual(["from-file", "inline"]);
- expect(commands.commands.find((command) => command.name === "from-file")).toMatchObject({
- argumentHint: "[topic]",
- description: "Generated from a source file",
- });
- expect(commands.commands.find((command) => command.name === "inline")).toMatchObject({
- allowedTools: ["Bash", "Read"],
- description: "Generated from inline content",
- });
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("dry-runs marketplace source validation against real plugin manifests", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-marketplace-dry-run-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const pluginRoot = join(marketplaceRoot, "plugins", "risky");
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "dry-run-market",
- plugins: [
- {
- name: "risky",
- source: "./plugins/risky",
- },
- ],
- });
- await writePluginManifest(
- pluginRoot,
- {
- agents: "./agents/risky.md",
- hooks: "./hooks/hooks.json",
- mcpServers: {
- risky: {
- command: "node",
- env: {
- TOKEN: "${ZCODE_MISSING_TOKEN}",
- },
- },
- },
- name: "risky",
- userConfig: {
- api_key: {
- sensitive: true,
- type: "string",
- },
- },
- },
- ".claude-plugin",
- );
- const diagnostics = await validateMarketplaceSource({
- source: { source: "directory", path: marketplaceRoot },
- storageRoot,
- });
- expect(diagnostics).toContainEqual(
- expect.objectContaining({
- code: "plugin_variable_missing",
- pluginId: "risky@dry-run-market",
- }),
- );
- expect(listInstalledPluginRecords(storageRoot)).toEqual([]);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("defers remote plugin roots while validating marketplace sources", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-marketplace-remote-dry-run-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const localRoot = join(marketplaceRoot, "plugins", "local-risky");
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "official-like",
- plugins: [
- {
- name: "remote-git-subdir",
- source: {
- source: "git-subdir",
- url: join(dir, "missing-remote.git"),
- path: "plugins/remote-git-subdir",
- sha: "abc123",
- },
- },
- {
- name: "local-risky",
- source: "./plugins/local-risky",
- },
- ],
- });
- await writePluginManifest(
- localRoot,
- {
- hooks: "hooks/hooks.json",
- name: "local-risky",
- },
- ".claude-plugin",
- );
- const diagnostics = await validateMarketplaceSource({
- source: { source: "directory", path: marketplaceRoot },
- storageRoot,
- });
- expect(diagnostics).toContainEqual(
- expect.objectContaining({
- code: "plugin_validation_deferred",
- pluginId: "remote-git-subdir@official-like",
- severity: "warning",
- }),
- );
- expect(diagnostics).not.toContainEqual(
- expect.objectContaining({
- code: "plugin_unsupported_component",
- pluginId: "local-risky@official-like",
- }),
- );
- expect(diagnostics).not.toContainEqual(
- expect.objectContaining({
- code: "plugin_marketplace_invalid",
- pluginId: "remote-git-subdir@official-like",
- }),
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("runs Marketplace cache hooks from third-party marketplaces when enabled", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-marketplace-hooks-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const pluginRoot = join(marketplaceRoot, "plugins", "hooked");
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "hook-market",
- plugins: [
- {
- name: "hooked",
- source: "./plugins/hooked",
- },
- ],
- });
- await writePluginManifest(
- pluginRoot,
- {
- hooks: "hooks/hooks.json",
- name: "hooked",
- },
- ".claude-plugin",
- );
- await mkdir(join(pluginRoot, "hooks"), { recursive: true });
- await writeFile(
- join(pluginRoot, "hooks", "hooks.json"),
- JSON.stringify({
- hooks: {
- SessionStart: [
- {
- hooks: [{ command: "echo hooked", type: "command" }],
- },
- ],
- },
- }),
- );
- await addMarketplace({ source: { source: "directory", path: marketplaceRoot }, storageRoot });
- await installMarketplacePlugin({ marketplace: "hook-market", name: "hooked", storageRoot });
- const outcome = await createNodePluginAdapter({ storageRoot }).discoverPlugins({
- config: {
- dirs: [],
- enabled: true,
- enabledPlugins: {
- "hooked@hook-market": true,
- },
- options: {},
- },
- env: {},
- storageRoot,
- workingDirectory: dir,
- });
- // 三方 marketplace 插件 hook 现在默认放行(与内置/官方一致)。
- expect(outcome.hooks.SessionStart).toBeDefined();
- expect(outcome.plugins[0]?.hookDetails).toMatchObject([
- {
- command: "echo hooked",
- event: "SessionStart",
- runnable: true,
- type: "command",
- },
- ]);
- expect(outcome.diagnostics).not.toContainEqual(
- expect.objectContaining({
- code: "plugin_unsupported_component",
- pluginId: "hooked@hook-market",
- }),
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("runs hooks from official Claude marketplace cache plugins when enabled", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-marketplace-official-hooks-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const pluginRoot = join(marketplaceRoot, "plugins", "hookify");
- try {
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "claude-plugins-official",
- plugins: [
- {
- name: "hookify",
- source: "./plugins/hookify",
- },
- ],
- });
- await writePluginManifest(pluginRoot, { name: "hookify" }, ".claude-plugin");
- await mkdir(join(pluginRoot, "hooks"), { recursive: true });
- await writeFile(
- join(pluginRoot, "hooks", "hooks.json"),
- JSON.stringify({
- hooks: {
- PreToolUse: [
- {
- hooks: [
- {
- command: 'python3 "${CLAUDE_PLUGIN_ROOT}/hooks/pretooluse.py"',
- type: "command",
- },
- ],
- },
- ],
- },
- }),
- );
- await addMarketplace({
- source: { source: "directory", path: marketplaceRoot },
- storageRoot,
- trustedId: "claude-plugins-official",
- });
- await installMarketplacePlugin({
- marketplace: "claude-plugins-official",
- name: "hookify",
- storageRoot,
- });
- const outcome = await createNodePluginAdapter({ storageRoot }).discoverPlugins({
- config: {
- dirs: [],
- enabled: true,
- enabledPlugins: {
- "hookify@claude-plugins-official": true,
- },
- options: {},
- },
- env: {},
- storageRoot,
- workingDirectory: dir,
- });
- expect(outcome.hooks.PreToolUse?.[0]?.hooks[0]).toMatchObject({
- command: 'python3 "${CLAUDE_PLUGIN_ROOT}/hooks/pretooluse.py"',
- plugin: {
- id: "hookify@claude-plugins-official",
- rootPath: expect.stringContaining(join("hookify", "0.0.0")),
- },
- type: "command",
- });
- expect(outcome.plugins[0]?.hookDetails).toMatchObject([
- {
- command: 'python3 "${CLAUDE_PLUGIN_ROOT}/hooks/pretooluse.py"',
- event: "PreToolUse",
- runnable: true,
- type: "command",
- },
- ]);
- expect(outcome.diagnostics).not.toContainEqual(
- expect.objectContaining({
- code: "plugin_unsupported_component",
- pluginId: "hookify@claude-plugins-official",
- }),
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("disables an MCP server when a required ZCODE variable is missing", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-env-"));
- const pluginRoot = join(dir, "env-plugin");
- try {
- await writePluginManifest(pluginRoot, {
- mcpServers: {
- local: {
- command: "node",
- env: {
- TOKEN: "${ZCODE_PLUGIN_TOKEN}",
- },
- },
- },
- name: "env-plugin",
- });
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: dir,
- });
- expect(outcome.mcpServers).toEqual({});
- expect(outcome.plugins[0]?.declaredMcpServerNames).toEqual(["local"]);
- expect(outcome.diagnostics.map((diagnostic) => diagnostic.code)).toContain(
- "plugin_variable_missing",
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("expands arbitrary process environment variables only in MCP secret sinks", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-secret-env-"));
- const pluginRoot = join(dir, "secret-env-plugin");
- try {
- await writePluginManifest(pluginRoot, {
- mcpServers: {
- local: {
- command: "node",
- args: ["${TOKEN_PLAN_API_TOKEN}"],
- env: {
- AUTHORIZATION: "Bearer ${TOKEN_PLAN_API_TOKEN}",
- },
- },
- remote: {
- type: "http",
- url: "https://mcp.example.test/${TOKEN_PLAN_API_TOKEN}",
- headers: {
- Authorization: "Bearer ${TOKEN_PLAN_API_TOKEN}",
- },
- },
- },
- name: "secret-env-plugin",
- });
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {
- TOKEN_PLAN_API_TOKEN: "secret-from-process-env",
- },
- storageRoot: join(dir, "plugins"),
- workingDirectory: dir,
- });
- expect(outcome.mcpServers["plugin:secret-env-plugin:local"]).toMatchObject({
- args: ["${TOKEN_PLAN_API_TOKEN}"],
- env: {
- AUTHORIZATION: "Bearer secret-from-process-env",
- },
- });
- expect(outcome.mcpServers["plugin:secret-env-plugin:remote"]).toMatchObject({
- headers: {
- Authorization: "Bearer secret-from-process-env",
- },
- url: "https://mcp.example.test/${TOKEN_PLAN_API_TOKEN}",
- });
- expect(outcome.diagnostics).not.toContainEqual(
- expect.objectContaining({ code: "plugin_variable_missing" }),
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("disables an MCP server when an arbitrary secret environment variable is missing", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-missing-secret-env-"));
- const pluginRoot = join(dir, "missing-secret-env-plugin");
- try {
- await writePluginManifest(pluginRoot, {
- mcpServers: {
- local: {
- command: "node",
- env: {
- TOKEN: "${TOKEN_PLAN_API_TOKEN}",
- },
- },
- },
- name: "missing-secret-env-plugin",
- });
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: dir,
- });
- expect(outcome.mcpServers).toEqual({});
- expect(outcome.diagnostics).toContainEqual(
- expect.objectContaining({
- code: "plugin_variable_missing",
- message: "Missing environment variable: TOKEN_PLAN_API_TOKEN",
- }),
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("diagnoses MCP variables that require unavailable runtime contexts", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-missing-context-"));
- const pluginRoot = join(dir, "missing-context-plugin");
- try {
- await writePluginManifest(pluginRoot, {
- mcpServers: {
- skillContext: {
- command: "node",
- args: ["${CLAUDE_SKILL_DIR}/server.js"],
- },
- sessionContext: {
- command: "node",
- env: {
- SESSION_ID: "${CLAUDE_SESSION_ID}",
- },
- },
- },
- name: "missing-context-plugin",
- });
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: dir,
- });
- expect(outcome.mcpServers).toEqual({});
- expect(outcome.diagnostics).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- code: "plugin_variable_missing",
- message: "Plugin variable requires a skill context: CLAUDE_SKILL_DIR",
- }),
- expect.objectContaining({
- code: "plugin_variable_missing",
- message: "Plugin variable requires a runtime session context: CLAUDE_SESSION_ID",
- }),
- ]),
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("loads plugins that only provide a Claude manifest", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-claude-plugin-"));
- const pluginRoot = join(dir, "claude-only");
- try {
- await writePluginManifest(pluginRoot, { name: "claude-only" }, ".claude-plugin");
- await mkdir(join(pluginRoot, "skills", "claude-only"), { recursive: true });
- await writeFile(join(pluginRoot, "skills", "claude-only", "SKILL.md"), "# skill");
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: dir,
- });
- expect(outcome.plugins[0]?.id).toBe("claude-only@inline");
- expect(outcome.plugins[0]?.manifestPath).toBe(
- join(pluginRoot, ".claude-plugin", "plugin.json"),
- );
- expect(outcome.skillRoots[0]?.path).toBe(join(pluginRoot, "skills"));
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("prefers Claude manifest over Codex manifest when no ZCode manifest exists", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-codex-plugin-"));
- const pluginRoot = join(dir, "multi-manifest");
- try {
- await writePluginManifest(pluginRoot, { name: "codex-name" }, ".codex-plugin");
- await writePluginManifest(pluginRoot, { name: "claude-name" }, ".claude-plugin");
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: dir,
- });
- expect(outcome.plugins[0]?.id).toBe("claude-name@inline");
- expect(outcome.plugins[0]?.manifestPath).toBe(
- join(pluginRoot, ".claude-plugin", "plugin.json"),
- );
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("removes the record, cache, and data directory on thorough uninstall", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-uninstall-"));
- const storageRoot = join(dir, "storage");
- const marketplaceRoot = join(dir, "marketplace");
- const pluginRoot = join(marketplaceRoot, "plugins", "hello");
- try {
- await writeKnownMarketplaces(storageRoot, [
- {
- id: "uninstall-market",
- source: { source: "directory", path: marketplaceRoot },
- name: "uninstall-market",
- addedAt: "2026-01-01T00:00:00.000Z",
- pluginCount: 0,
- },
- ]);
- await writeMarketplaceManifest(marketplaceRoot, {
- name: "uninstall-market",
- plugins: [{ name: "hello", source: "./plugins/hello" }],
- });
- await writePluginManifest(pluginRoot, { name: "hello", skills: "skills" }, ".claude-plugin");
- await mkdir(join(pluginRoot, "skills", "hello"), { recursive: true });
- await writeFile(join(pluginRoot, "skills", "hello", "SKILL.md"), "# hello");
- const install = await installMarketplacePlugin({
- marketplace: "uninstall-market",
- name: "hello",
- storageRoot,
- });
- const record = install.installed[0];
- expect(record?.id).toBe("hello@uninstall-market");
- const installPath = record?.installPath ?? "";
- // 模拟运行期产生的 per-plugin data 目录(含 generated-commands)。
- const dataDir = join(storageRoot, "data", "hello@uninstall-market");
- await mkdir(join(dataDir, "generated-commands"), { recursive: true });
- await writeFile(join(dataDir, "generated-commands", "demo.md"), "# demo");
- expect(existsSync(installPath)).toBe(true);
- expect(existsSync(dataDir)).toBe(true);
- const removed = await uninstallMarketplacePlugin({
- pluginId: "hello@uninstall-market",
- removeCache: true,
- storageRoot,
- });
- expect(removed?.id).toBe("hello@uninstall-market");
- expect(listInstalledPluginRecords(storageRoot)).toHaveLength(0);
- expect(existsSync(installPath)).toBe(false);
- expect(existsSync(dataDir)).toBe(false);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("returns null and keeps state when uninstalling an unknown plugin id", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-uninstall-missing-"));
- const storageRoot = join(dir, "storage");
- try {
- const removed = await uninstallMarketplacePlugin({
- pluginId: "missing@nowhere",
- removeCache: true,
- storageRoot,
- });
- expect(removed).toBeNull();
- expect(listInstalledPluginRecords(storageRoot)).toHaveLength(0);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- // Bugfix 回归(差一层目录):Claude 插件规范里 manifest skills 数组项指向「技能目录本身」
- // (项内直接是 SKILL.md),此前 ZCode 把每项当作技能根去项下找子目录,扫描恒为空。
- it("discovers skills for plugin manifest skills array entries pointing at skill directories", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-array-skills-"));
- const pluginRoot = join(dir, "mattpocock");
- const skillEntries = ["./skills/engineering/tdd", "./skills/productivity/writing"];
- try {
- await writePluginManifest(
- pluginRoot,
- { name: "mattpocock-skills", skills: skillEntries },
- ".claude-plugin",
- );
- for (const [index, entry] of skillEntries.entries()) {
- const skillName = entry.split("/").pop() ?? "";
- const skillDir = join(pluginRoot, entry);
- await mkdir(skillDir, { recursive: true });
- await writeFile(
- join(skillDir, "SKILL.md"),
- [
- "---",
- `name: ${skillName}`,
- `description: array-entry skill ${index}`,
- "---",
- "",
- "# skill",
- ].join("\n"),
- );
- }
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: join(dir, "workspace"),
- });
- const plugin = outcome.plugins.find((item) => item.id === "mattpocock-skills@inline");
- expect(plugin).toBeDefined();
- // 显式声明后只注册声明入口,不补扫默认 skills/。
- expect(plugin).toMatchObject({ skillCount: 2, skillRootCount: 2 });
- const skillNames = componentNames(plugin?.components ?? [], "skill").sort();
- expect(skillNames).toEqual(["tdd", "writing"]);
- const skillOutcome = await createNodeSkillAdapter().discoverSkills({
- roots: outcome.skillRoots,
- workingDirectory: join(dir, "workspace"),
- });
- expect(skillOutcome.skills).toHaveLength(2);
- // 插件技能带 pluginName 别名,供会话内以 <plugin>:<skill> 引用。
- expect(skillOutcome.skills.map((skill) => skill.qualifiedName).sort()).toEqual([
- "mattpocock-skills:tdd",
- "mattpocock-skills:writing",
- ]);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- // Bugfix 回归(评审跟进):数组语义下两个声明项的叶子目录可能同名
- // (./skills/engineering/tdd 与 ./skills/productivity/tdd),组件清单此前在解析
- // frontmatter 之前按目录 basename 提前去重,会把第二个技能丢掉,与按路径去重的
- // 运行时发现结果(skillCount)不一致。必须解析 frontmatter 后按最终 name 去重。
- it("keeps same-named leaf skill directories from different declared roots in components", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-same-leaf-skills-"));
- const pluginRoot = join(dir, "same-leaf");
- const entries: Array<{ entry: string; name: string }> = [
- { entry: "./skills/engineering/tdd", name: "tdd" },
- { entry: "./skills/productivity/tdd", name: "tdd-productivity" },
- ];
- try {
- await writePluginManifest(
- pluginRoot,
- { name: "same-leaf", skills: entries.map((item) => item.entry) },
- ".claude-plugin",
- );
- for (const { entry, name } of entries) {
- const skillDir = join(pluginRoot, entry);
- await mkdir(skillDir, { recursive: true });
- await writeFile(
- join(skillDir, "SKILL.md"),
- `---\nname: ${name}\ndescription: same leaf skill\n---\n\n# ${name}`,
- );
- }
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: join(dir, "workspace"),
- });
- const plugin = outcome.plugins.find((item) => item.id === "same-leaf@inline");
- expect(plugin).toMatchObject({ skillCount: 2 });
- // 组件清单与 skillCount 口径一致:两个同名叶子目录(frontmatter name 不同)都在。
- expect(componentNames(plugin?.components ?? [], "skill").sort()).toEqual([
- "tdd",
- "tdd-productivity",
- ]);
- const skillOutcome = await createNodeSkillAdapter().discoverSkills({
- roots: outcome.skillRoots,
- workingDirectory: join(dir, "workspace"),
- });
- expect(skillOutcome.skills.map((skill) => skill.name).sort()).toEqual([
- "tdd",
- "tdd-productivity",
- ]);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- it("does not add the default root alongside a declared skill", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-overlap-skills-"));
- const pluginRoot = join(dir, "overlap");
- try {
- await writePluginManifest(
- pluginRoot,
- { name: "overlap", skills: ["./skills/foo"] },
- ".claude-plugin",
- );
- await mkdir(join(pluginRoot, "skills", "foo"), { recursive: true });
- await writeFile(
- join(pluginRoot, "skills", "foo", "SKILL.md"),
- "---\nname: foo\ndescription: overlap skill\n---\n\n# foo",
- );
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: join(dir, "workspace"),
- });
- const plugin = outcome.plugins.find((item) => item.id === "overlap@inline");
- expect(plugin).toMatchObject({ skillCount: 1, skillRootCount: 1 });
- const skillOutcome = await createNodeSkillAdapter().discoverSkills({
- roots: outcome.skillRoots,
- workingDirectory: join(dir, "workspace"),
- });
- expect(skillOutcome.skills).toHaveLength(1);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- // 信任边界回归(CR-01):manifest skills 声明是插件提供的不可信输入。
- // resolveInside(helpers.ts)在词法层面拒绝绝对路径与 ../ 穿越(rel 以 .. 开头
- // 或含 ..${sep}),越界声明在 resolveComponentRoots 落成 plugin_component_path_invalid
- // error 诊断;本用例把该边界固定,防止后续重构 scanner 时重新引入路径穿越。
- it("rejects skills paths escaping the plugin root and keeps legitimate skills working", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-path-escape-"));
- const pluginRoot = join(dir, "plugin");
- // 插件根外的诱饵技能目录;若防线失效会被扫描进结果。
- const outsideSkill = join(dir, "outside-skill");
- const absoluteOutside = join(dir, "absolute-outside");
- try {
- await writePluginManifest(
- pluginRoot,
- {
- name: "path-escape",
- skills: ["../../outside-skill", absoluteOutside, "./skills/legit"],
- },
- ".claude-plugin",
- );
- await mkdir(outsideSkill, { recursive: true });
- await mkdir(absoluteOutside, { recursive: true });
- await writeFile(join(outsideSkill, "SKILL.md"), "---\nname: outside\n---\n# x");
- await writeFile(join(absoluteOutside, "SKILL.md"), "---\nname: absolute\n---\n# x");
- await mkdir(join(pluginRoot, "skills", "legit"), { recursive: true });
- await writeFile(
- join(pluginRoot, "skills", "legit", "SKILL.md"),
- "---\nname: legit\ndescription: legitimate skill\n---\n# legit",
- );
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: join(dir, "workspace"),
- });
- // 越界声明(../ 穿越与绝对路径)被拒:error 诊断逐条可见,且不会误报成
- // plugin_skill_root_empty(resolveInside 返回 null 时告警逻辑直接跳过)。
- const invalid = outcome.diagnostics.filter(
- (diagnostic) => diagnostic.code === "plugin_component_path_invalid",
- );
- expect(invalid).toHaveLength(2);
- expect(invalid.every((diagnostic) => diagnostic.severity === "error")).toBe(true);
- expect(invalid.some((diagnostic) => diagnostic.message.includes("../../outside-skill"))).toBe(
- true,
- );
- expect(invalid.some((diagnostic) => diagnostic.message.includes(absoluteOutside))).toBe(true);
- expect(
- outcome.diagnostics.some(
- (diagnostic) =>
- diagnostic.code === "plugin_skill_root_empty" &&
- diagnostic.pluginId === "path-escape@inline",
- ),
- ).toBe(false);
- // 合法声明不受越界项影响:skillCount 只含 legit,越界技能不出现在发现结果。
- const plugin = outcome.plugins.find((item) => item.id === "path-escape@inline");
- expect(plugin).toMatchObject({ skillCount: 1 });
- const skillOutcome = await createNodeSkillAdapter().discoverSkills({
- roots: outcome.skillRoots,
- workingDirectory: join(dir, "workspace"),
- });
- expect(skillOutcome.skills.map((skill) => skill.name)).toEqual(["legit"]);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- // 信任边界回归(文件级 symlink):目录是真实目录但 SKILL.md 本身是指向插件根
- // 外文件的链接(如 skills/foo/SKILL.md -> ~/.aws/credentials),以及根自身
- // SKILL.md 是链接的形态。文件级与目录级走同一个 isSymbolicLinkSync(lstat)
- // 拒绝路径。Windows 普通权限下创建文件 symlink 会 EPERM,此时才跳过——
- // 开发者模式或管理员 CI 上本用例会真实执行,不整体按平台跳过。
- it("rejects file-level SKILL.md symlinks escaping the plugin root", async (ctx) => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-file-symlink-"));
- const pluginRoot = join(dir, "plugin");
- const outsideFile = join(dir, "outside-secret.md");
- try {
- await writePluginManifest(
- pluginRoot,
- {
- name: "file-symlink-escape",
- skills: ["./skills", "./bundle-root"],
- },
- ".claude-plugin",
- );
- await writeFile(outsideFile, "---\nname: leaked-secret\ndescription: x\n---\nleaked");
- // 形态 1:子目录是真实目录,其 SKILL.md 是指向外部的文件链接。
- // 第一个链接同时作为权限探针:EPERM(Windows 无 symlink 特权)才跳过,
- // 其余错误如实抛出;finally 保证临时目录清理。
- await mkdir(join(pluginRoot, "skills", "foo"), { recursive: true });
- try {
- await symlink(outsideFile, join(pluginRoot, "skills", "foo", "SKILL.md"));
- } catch (error) {
- if ((error as NodeJS.ErrnoException).code !== "EPERM") throw error;
- ctx.skip();
- return;
- }
- // 形态 2:声明根(bundle-root)自身目录真实,SKILL.md 是外部文件链接。
- await mkdir(join(pluginRoot, "bundle-root"), { recursive: true });
- await symlink(outsideFile, join(pluginRoot, "bundle-root", "SKILL.md"));
- // 对照组:真实文件技能正常发现。
- await mkdir(join(pluginRoot, "skills", "legit"), { recursive: true });
- await writeFile(
- join(pluginRoot, "skills", "legit", "SKILL.md"),
- "---\nname: legit\ndescription: legitimate\n---\n# legit",
- );
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: join(dir, "workspace"),
- });
- // 只有真实文件技能被计数;两个文件级链接形态都被剔除。
- const plugin = outcome.plugins.find((item) => item.id === "file-symlink-escape@inline");
- expect(plugin).toMatchObject({ skillCount: 1 });
- expect(componentNames(plugin?.components ?? [], "skill")).toEqual(["legit"]);
- const skillOutcome = await createNodeSkillAdapter().discoverSkills({
- roots: outcome.skillRoots,
- workingDirectory: join(dir, "workspace"),
- });
- expect(skillOutcome.skills.map((skill) => skill.name)).toEqual(["legit"]);
- // 泄密文件内容绝不出现在任何技能路径的加载结果里。
- expect(skillOutcome.skills.some((skill) => skill.path === outsideFile)).toBe(false);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- // 信任边界回归(目录级 symlink/junction):plugin-scope 扫描不跟随符号链接——
- // skills/evil-link -> ../../outside-skill(子目录是逃逸链接)
- // skills/linked-root -> ../../outside-skill(声明根本身是逃逸链接)
- // 两种形态都被剔除;声明根是链接时扫描为空,落入「没有任何技能」告警。
- // Windows 用 junction 创建(无需特权;lstat/Dirent 对 junction 同样报告
- // isSymbolicLink=true),CR-02 的 Windows 形态在本用例获得真实覆盖。
- it("strips symlink escapes from plugin skills and warns on escaping declared roots", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-symlink-escape-"));
- const pluginRoot = join(dir, "plugin");
- const outsideSkill = join(dir, "outside-skill");
- try {
- await writePluginManifest(
- pluginRoot,
- {
- name: "symlink-escape",
- skills: ["./skills/linked-root", "./skills"],
- },
- ".claude-plugin",
- );
- await mkdir(outsideSkill, { recursive: true });
- await writeFile(
- join(outsideSkill, "SKILL.md"),
- "---\nname: escaped-skill\ndescription: must never load\n---\n# x",
- );
- // 形态 1:声明根本身是指向插件根外的链接(skills/ 需先存在)。
- // Windows 传 junction 类型(目录 symlink 需特权,junction 不需要;
- // POSIX 忽略该参数,行为为普通 symlink)。
- await mkdir(join(pluginRoot, "skills"), { recursive: true });
- await symlinkDir(outsideSkill, join(pluginRoot, "skills", "linked-root"));
- // 形态 2:默认 skills/ 根下的子目录是指向插件根外的链接。
- await symlinkDir(outsideSkill, join(pluginRoot, "skills", "evil-link"));
- const outcome = await createNodePluginAdapter({
- storageRoot: join(dir, "plugins"),
- }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: join(dir, "workspace"),
- });
- // 逃逸技能不进计数、不进组件清单。
- const plugin = outcome.plugins.find((item) => item.id === "symlink-escape@inline");
- expect(plugin).toMatchObject({ skillCount: 0 });
- expect(componentNames(plugin?.components ?? [], "skill")).toEqual([]);
- // 声明根本身是链接 → 扫描为空,落入「没有任何技能」告警(词法路径存在,
- // 不误报 does not exist)。
- expect(
- outcome.diagnostics.some(
- (diagnostic) =>
- diagnostic.code === "plugin_skill_root_empty" &&
- diagnostic.pluginId === "symlink-escape@inline" &&
- diagnostic.message.includes("does not contain any skills") &&
- diagnostic.message.includes("skills/linked-root"),
- ),
- ).toBe(true);
- // 运行时发现同样剔除:逃逸技能不出现在 skill adapter 结果。
- const skillOutcome = await createNodeSkillAdapter().discoverSkills({
- roots: outcome.skillRoots,
- workingDirectory: join(dir, "workspace"),
- });
- expect(skillOutcome.skills.map((skill) => skill.name)).toEqual([]);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- // 去静默:manifest 显式声明的技能路径扫描为 0 个技能时必须发 warning(路径写进 message,
- // 因为协议 wire schema 不携带 path);纯默认目录为空不告警;显式声明默认目录名同样算声明。
- it("warns when a declared skill path yields no skills", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-plugin-empty-skills-"));
- async function discover(pluginRoot: string, manifest: Record<string, unknown>) {
- await writePluginManifest(pluginRoot, manifest, ".claude-plugin");
- return await createNodePluginAdapter({ storageRoot: join(dir, "plugins") }).discoverPlugins({
- config: {
- dirs: [pluginRoot],
- enabled: true,
- enabledPlugins: {},
- options: {},
- },
- env: {},
- storageRoot: join(dir, "plugins"),
- workingDirectory: join(dir, "workspace"),
- });
- }
- try {
- // 场景 1:数组声明项目录存在但无 SKILL.md(分类目录,mattpocock 复现形态)。
- const missingRoot = join(dir, "declared-missing");
- await mkdir(join(missingRoot, "skills", "engineering"), { recursive: true });
- const missingOutcome = await discover(missingRoot, {
- name: "declared-missing",
- skills: ["./skills/engineering/tdd"],
- });
- const missingWarning = missingOutcome.diagnostics.find(
- (diagnostic) => diagnostic.code === "plugin_skill_root_empty",
- );
- expect(missingWarning).toMatchObject({
- pluginId: "declared-missing@inline",
- severity: "warning",
- });
- expect(missingWarning?.message).toContain("skills/engineering/tdd");
- // 场景 2:显式声明不存在的路径。
- const absentOutcome = await discover(join(dir, "declared-absent"), {
- name: "declared-absent",
- skills: "./skills/nowhere",
- });
- expect(
- absentOutcome.diagnostics.some(
- (diagnostic) =>
- diagnostic.code === "plugin_skill_root_empty" &&
- diagnostic.message.includes("skills/nowhere"),
- ),
- ).toBe(true);
- // 场景 3:纯默认目录为空(manifest 未声明 skills)不告警,避免空插件误报。
- const silentOutcome = await discover(join(dir, "default-only"), {
- name: "default-only",
- });
- expect(
- silentOutcome.diagnostics.every(
- (diagnostic) => diagnostic.code !== "plugin_skill_root_empty",
- ),
- ).toBe(true);
- // 场景 4:显式写默认目录名 "skills" 且目录为空——默认项会在 roots 里被去重合并,
- // 但声明集合独立计算,仍须告警。
- const explicitOutcome = await discover(join(dir, "explicit-default"), {
- name: "explicit-default",
- skills: "skills",
- });
- expect(
- explicitOutcome.diagnostics.some(
- (diagnostic) =>
- diagnostic.code === "plugin_skill_root_empty" &&
- diagnostic.pluginId === "explicit-default@inline",
- ),
- ).toBe(true);
- } finally {
- await rm(dir, { force: true, recursive: true });
- }
- });
- });
- function componentNames(
- components: Awaited<ReturnType<typeof describeMarketplacePlugin>>["components"],
- kind: "agent" | "command" | "skill" | "hook" | "mcp",
- ): string[] {
- return components.find((group) => group.kind === kind)?.items.map((item) => item.name) ?? [];
- }
- function componentDescription(
- components: Awaited<ReturnType<typeof describeMarketplacePlugin>>["components"],
- kind: "agent" | "command" | "skill" | "hook" | "mcp",
- name: string,
- ): string | undefined {
- return components.find((group) => group.kind === kind)?.items.find((item) => item.name === name)
- ?.description;
- }
- async function startZipFixtureServer(
- routes: Record<string, Buffer>,
- ): Promise<{ baseUrl: string; server: Server }> {
- const server = createServer((request, response) => {
- const pathname = new URL(request.url ?? "/", "http://127.0.0.1").pathname;
- const body = routes[pathname];
- if (!body) {
- response.writeHead(404);
- response.end("not found");
- return;
- }
- response.writeHead(200, {
- "content-length": String(body.byteLength),
- "content-type": "application/zip",
- });
- response.end(body);
- });
- const baseUrl = await listenLoopbackServer(server);
- return { baseUrl, server };
- }
- async function listenLoopbackServer(server: Server): Promise<string> {
- await new Promise<void>((resolveListen) => {
- server.listen(0, "127.0.0.1", resolveListen);
- });
- const address = server.address();
- if (!address || typeof address === "string") {
- throw new Error("fixture server did not bind to a TCP port");
- }
- return `http://127.0.0.1:${address.port}`;
- }
- async function closeServer(server: Server | undefined): Promise<void> {
- if (!server || !server.listening) return;
- await new Promise<void>((resolveClose, rejectClose) => {
- server.close((error) => {
- if (error) rejectClose(error);
- else resolveClose();
- });
- });
- }
- function sha256Buffer(buffer: Buffer): string {
- return createHash("sha256").update(buffer).digest("hex");
- }
- function createStoredZip(entries: Array<{ content?: string; path: string }>): Buffer {
- const localParts: Buffer[] = [];
- const centralParts: Buffer[] = [];
- let offset = 0;
- for (const entry of entries) {
- const name = Buffer.from(entry.path);
- const data = Buffer.from(entry.content ?? "");
- const crc = crc32(data);
- const local = Buffer.alloc(30);
- local.writeUInt32LE(0x04034b50, 0);
- local.writeUInt16LE(20, 4);
- local.writeUInt16LE(0, 6);
- local.writeUInt16LE(0, 8);
- local.writeUInt16LE(0, 10);
- local.writeUInt16LE(0, 12);
- local.writeUInt32LE(crc, 14);
- local.writeUInt32LE(data.byteLength, 18);
- local.writeUInt32LE(data.byteLength, 22);
- local.writeUInt16LE(name.byteLength, 26);
- local.writeUInt16LE(0, 28);
- localParts.push(local, name, data);
- const central = Buffer.alloc(46);
- central.writeUInt32LE(0x02014b50, 0);
- central.writeUInt16LE(20, 4);
- central.writeUInt16LE(20, 6);
- central.writeUInt16LE(0, 8);
- central.writeUInt16LE(0, 10);
- central.writeUInt16LE(0, 12);
- central.writeUInt16LE(0, 14);
- central.writeUInt32LE(crc, 16);
- central.writeUInt32LE(data.byteLength, 20);
- central.writeUInt32LE(data.byteLength, 24);
- central.writeUInt16LE(name.byteLength, 28);
- central.writeUInt16LE(0, 30);
- central.writeUInt16LE(0, 32);
- central.writeUInt16LE(0, 34);
- central.writeUInt16LE(0, 36);
- central.writeUInt32LE((entry.path.endsWith("/") ? 0o040755 : 0o100644) * 0x10000, 38);
- central.writeUInt32LE(offset, 42);
- centralParts.push(central, name);
- offset += local.byteLength + name.byteLength + data.byteLength;
- }
- const centralDirectory = Buffer.concat(centralParts);
- const end = Buffer.alloc(22);
- end.writeUInt32LE(0x06054b50, 0);
- end.writeUInt16LE(0, 4);
- end.writeUInt16LE(0, 6);
- end.writeUInt16LE(entries.length, 8);
- end.writeUInt16LE(entries.length, 10);
- end.writeUInt32LE(centralDirectory.byteLength, 12);
- end.writeUInt32LE(offset, 16);
- end.writeUInt16LE(0, 20);
- return Buffer.concat([...localParts, centralDirectory, end]);
- }
- const CRC32_TABLE = Array.from({ length: 256 }, (_, index) => {
- let value = index;
- for (let bit = 0; bit < 8; bit += 1) {
- value = value & 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1;
- }
- return value >>> 0;
- });
- function crc32(buffer: Buffer): number {
- let crc = 0xffffffff;
- for (const byte of buffer) {
- crc = CRC32_TABLE[(crc ^ byte) & 0xff]! ^ (crc >>> 8);
- }
- return (crc ^ 0xffffffff) >>> 0;
- }
- /** 创建指向目录的链接:Windows 用 junction(无需特权),POSIX 忽略 type 参数。 */
- async function symlinkDir(target: string, linkPath: string): Promise<void> {
- await symlink(target, linkPath, process.platform === "win32" ? "junction" : undefined);
- }
- async function writePluginManifest(
- pluginRoot: string,
- manifest: Record<string, unknown>,
- manifestDir = ".zcode-plugin",
- ): Promise<void> {
- await mkdir(join(pluginRoot, manifestDir), { recursive: true });
- await writeFile(join(pluginRoot, manifestDir, "plugin.json"), JSON.stringify(manifest));
- }
- async function writeMarketplaceManifest(
- marketplaceRoot: string,
- manifest: Record<string, unknown>,
- ): Promise<void> {
- await mkdir(join(marketplaceRoot, ".claude-plugin"), { recursive: true });
- await writeFile(
- join(marketplaceRoot, ".claude-plugin", "marketplace.json"),
- JSON.stringify(manifest),
- );
- }
- async function writeKnownMarketplaces(
- storageRoot: string,
- marketplaces: Array<Record<string, unknown>>,
- ): Promise<void> {
- await mkdir(storageRoot, { recursive: true });
- await writeFile(
- join(storageRoot, "known_marketplaces.json"),
- JSON.stringify({ version: 1, marketplaces }),
- );
- }
- async function listenServer(server: Server): Promise<string> {
- await new Promise<void>((resolve, reject) => {
- server.once("error", reject);
- server.listen(0, "127.0.0.1", () => {
- server.removeListener("error", reject);
- resolve();
- });
- });
- const address = server.address();
- if (!address || typeof address === "string") {
- throw new Error("Test server did not expose a TCP address");
- }
- return `http://127.0.0.1:${address.port}`;
- }
- interface TripletPluginFixture {
- commandCount: number;
- commandPrefix: string;
- manifestPaths?: boolean;
- mcpFileName?: ".mcp.json" | "mcp.json";
- mcpServerName: string;
- name: string;
- skillCount: number;
- }
- function claudeMarketplaceTripletFixtures(): TripletPluginFixture[] {
- return [
- {
- commandCount: 27,
- commandPrefix: "bigdata",
- mcpServerName: "bigdata",
- name: "bigdata-com",
- skillCount: 1,
- },
- {
- commandCount: 1,
- commandPrefix: "convex",
- manifestPaths: true,
- mcpServerName: "convex",
- name: "convex",
- skillCount: 2,
- },
- {
- commandCount: 4,
- commandPrefix: "logfire",
- manifestPaths: true,
- mcpFileName: "mcp.json",
- mcpServerName: "logfire",
- name: "logfire",
- skillCount: 3,
- },
- ];
- }
- async function writeTripletPluginFixture(
- pluginRoot: string,
- fixture: TripletPluginFixture,
- ): Promise<void> {
- const mcpFileName = fixture.mcpFileName ?? ".mcp.json";
- await writePluginManifest(
- pluginRoot,
- {
- ...(fixture.manifestPaths
- ? {
- commands: "./commands/",
- mcpServers: `./${mcpFileName}`,
- skills: "./skills/",
- }
- : {}),
- name: fixture.name,
- version: "1.0.0",
- },
- ".claude-plugin",
- );
- for (let index = 1; index <= fixture.skillCount; index += 1) {
- const skillName = `${fixture.name}-skill-${index}`;
- const skillDir = join(pluginRoot, "skills", skillName);
- await mkdir(skillDir, { recursive: true });
- await writeFile(
- join(skillDir, "SKILL.md"),
- [
- "---",
- `name: ${skillName}`,
- `description: ${fixture.name} marketplace compatibility skill ${index}`,
- "---",
- "",
- `# ${skillName}`,
- "",
- "Validate plugin skill projection.",
- ].join("\n"),
- );
- }
- const commandDir = join(pluginRoot, "commands", fixture.commandPrefix);
- await mkdir(commandDir, { recursive: true });
- for (let index = 1; index <= fixture.commandCount; index += 1) {
- const commandName = `cmd${String(index).padStart(2, "0")}`;
- await writeFile(
- join(commandDir, `${commandName}.md`),
- [
- "---",
- `description: ${fixture.name} marketplace compatibility command ${index}`,
- "allowed-tools: Read, Bash",
- "---",
- "",
- `Run ${fixture.name} command ${index} with $ARGUMENTS.`,
- ].join("\n"),
- );
- }
- await writeFile(
- join(pluginRoot, mcpFileName),
- JSON.stringify({
- mcpServers: {
- [fixture.mcpServerName]: {
- args: ["${CLAUDE_PLUGIN_ROOT}/mcp/server.js"],
- command: "node",
- env: {
- PLUGIN_DATA: "${CLAUDE_PLUGIN_DATA}",
- PROJECT_DIR: "${ZCODE_PROJECT_DIR}",
- },
- },
- },
- }),
- );
- }
|