@@ -3,6 +3,7 @@ import fs from "node:fs/promises";
33import os from "node:os" ;
44import path from "node:path" ;
55import { afterEach , beforeAll , describe , expect , it , vi } from "vitest" ;
6+ import * as jsonFiles from "../infra/json-files.js" ;
67import { withEnvAsync } from "../test-utils/env.js" ;
78import { withTempDir } from "../test-utils/temp-dir.js" ;
89import {
@@ -102,6 +103,22 @@ async function writeLocalMarketplaceFixture(params: {
102103 return writeMarketplaceManifest ( params . rootDir , params . manifest ) ;
103104}
104105
106+ async function withKnownMarketplaceRegistry < T > (
107+ marketplaces : Record < string , unknown > ,
108+ run : ( homeDir : string ) => Promise < T > ,
109+ ) : Promise < T > {
110+ return await withTempDir ( "openclaw-marketplace-known-" , async ( homeDir ) => {
111+ const openClawHome = path . join ( homeDir , "openclaw-home" ) ;
112+ const registryPath = path . join ( homeDir , ".claude" , "plugins" , "known_marketplaces.json" ) ;
113+ await fs . mkdir ( path . dirname ( registryPath ) , { recursive : true } ) ;
114+ await fs . mkdir ( openClawHome , { recursive : true } ) ;
115+ await fs . writeFile ( registryPath , JSON . stringify ( marketplaces ) ) ;
116+ return await withEnvAsync ( { HOME : homeDir , OPENCLAW_HOME : openClawHome } , async ( ) =>
117+ run ( homeDir ) ,
118+ ) ;
119+ } ) ;
120+ }
121+
105122function mockRemoteMarketplaceClone ( params : {
106123 manifest : unknown ;
107124 pluginDir ?: string ;
@@ -533,6 +550,151 @@ describe("marketplace plugins", () => {
533550 } ) ;
534551 } ) ;
535552
553+ const cyclicKnownMarketplaces = [
554+ {
555+ label : "self-referential" ,
556+ marketplace : "loop" ,
557+ marketplaces : {
558+ loop : { source : { source : "path" , path : "loop" } } ,
559+ } ,
560+ cycle : "loop -> loop" ,
561+ } ,
562+ {
563+ label : "two-marketplace" ,
564+ marketplace : "alpha" ,
565+ marketplaces : {
566+ alpha : { source : { source : "path" , path : "beta" } } ,
567+ beta : { source : { source : "path" , path : "alpha" } } ,
568+ } ,
569+ cycle : "alpha -> beta -> alpha" ,
570+ } ,
571+ ] as const ;
572+
573+ it . each ( cyclicKnownMarketplaces ) (
574+ "rejects a $label known marketplace alias cycle while listing" ,
575+ async ( { marketplace, marketplaces, cycle } ) => {
576+ await withKnownMarketplaceRegistry ( marketplaces , async ( ) => {
577+ const registryRead = vi . spyOn ( jsonFiles , "tryReadJson" ) ;
578+ for ( let read = 0 ; read <= Object . keys ( marketplaces ) . length ; read += 1 ) {
579+ registryRead . mockResolvedValueOnce ( marketplaces ) ;
580+ }
581+ // Bound the unfixed recursive loader so the regression never leaves a runaway task.
582+ registryRead . mockResolvedValueOnce ( { } ) ;
583+
584+ try {
585+ const result = await listMarketplacePlugins ( { marketplace } ) ;
586+
587+ expect ( result ) . toEqual ( {
588+ ok : false ,
589+ error : `known marketplace source cycle: ${ cycle } ` ,
590+ } ) ;
591+ expect ( registryRead ) . toHaveBeenCalledTimes ( 1 ) ;
592+ expect ( runCommandWithTimeoutMock ) . not . toHaveBeenCalled ( ) ;
593+ expect ( fetchWithSsrFGuardMock ) . not . toHaveBeenCalled ( ) ;
594+ } finally {
595+ registryRead . mockRestore ( ) ;
596+ }
597+ } ) ;
598+ } ,
599+ ) ;
600+
601+ it . each ( cyclicKnownMarketplaces ) (
602+ "rejects a $label known marketplace alias cycle before installing" ,
603+ async ( { marketplace, marketplaces, cycle } ) => {
604+ await withKnownMarketplaceRegistry ( marketplaces , async ( ) => {
605+ const registryRead = vi . spyOn ( jsonFiles , "tryReadJson" ) ;
606+ for ( let read = 0 ; read <= Object . keys ( marketplaces ) . length ; read += 1 ) {
607+ registryRead . mockResolvedValueOnce ( marketplaces ) ;
608+ }
609+ // Bound the unfixed recursive loader so the lifecycle-owning call always settles.
610+ registryRead . mockResolvedValueOnce ( { } ) ;
611+
612+ try {
613+ const result = await installPluginFromMarketplace ( {
614+ marketplace,
615+ plugin : "frontend-design" ,
616+ } ) ;
617+
618+ expect ( result ) . toEqual ( {
619+ ok : false ,
620+ error : `known marketplace source cycle: ${ cycle } ` ,
621+ } ) ;
622+ expect ( registryRead ) . toHaveBeenCalledTimes ( 1 ) ;
623+ expect ( installPluginFromPathMock ) . not . toHaveBeenCalled ( ) ;
624+ expect ( runCommandWithTimeoutMock ) . not . toHaveBeenCalled ( ) ;
625+ expect ( fetchWithSsrFGuardMock ) . not . toHaveBeenCalled ( ) ;
626+ } finally {
627+ registryRead . mockRestore ( ) ;
628+ }
629+ } ) ;
630+ } ,
631+ ) ;
632+
633+ it ( "resolves independent known marketplace alias calls from one registry snapshot each" , async ( ) => {
634+ await withKnownMarketplaceRegistry ( { } , async ( homeDir ) => {
635+ const marketplaceRoot = path . join ( homeDir , "known-marketplace" ) ;
636+ const pluginDir = path . join ( marketplaceRoot , "plugins" , "frontend-design" ) ;
637+ await writeLocalMarketplaceFixture ( {
638+ rootDir : marketplaceRoot ,
639+ pluginDir,
640+ manifest : {
641+ plugins : [ { name : "frontend-design" , source : "./plugins/frontend-design" } ] ,
642+ } ,
643+ } ) ;
644+ await fs . writeFile (
645+ path . join ( homeDir , ".claude" , "plugins" , "known_marketplaces.json" ) ,
646+ JSON . stringify ( {
647+ alpha : { source : { source : "path" , path : "beta" } } ,
648+ beta : {
649+ installLocation : marketplaceRoot ,
650+ source : { source : "path" , path : "alpha" } ,
651+ } ,
652+ } ) ,
653+ ) ;
654+ installPluginFromPathMock . mockResolvedValue ( {
655+ ok : true ,
656+ pluginId : "frontend-design" ,
657+ targetDir : "/tmp/frontend-design" ,
658+ version : "0.1.0" ,
659+ extensions : [ "index.ts" ] ,
660+ } ) ;
661+ const registryRead = vi . spyOn ( jsonFiles , "tryReadJson" ) ;
662+
663+ try {
664+ const listed = await listMarketplacePlugins ( { marketplace : "alpha" } ) ;
665+ const installed = await installPluginFromMarketplace ( {
666+ marketplace : "alpha" ,
667+ plugin : "frontend-design" ,
668+ } ) ;
669+
670+ expect ( listed ) . toMatchObject ( { ok : true , sourceLabel : "beta" } ) ;
671+ expectLocalMarketplaceInstallResult ( {
672+ result : installed ,
673+ pluginDir,
674+ marketplaceSource : "alpha" ,
675+ } ) ;
676+ expect ( registryRead ) . toHaveBeenCalledTimes ( 2 ) ;
677+ expect ( runCommandWithTimeoutMock ) . not . toHaveBeenCalled ( ) ;
678+ expect ( fetchWithSsrFGuardMock ) . not . toHaveBeenCalled ( ) ;
679+ } finally {
680+ registryRead . mockRestore ( ) ;
681+ }
682+ } ) ;
683+ } ) ;
684+
685+ it ( "preserves ordinary source fallback for an invalid known marketplace alias" , async ( ) => {
686+ await withKnownMarketplaceRegistry ( { broken : { source : { source : "path" } } } , async ( ) => {
687+ const result = await listMarketplacePlugins ( { marketplace : "broken" } ) ;
688+
689+ expect ( result ) . toEqual ( {
690+ ok : false ,
691+ error : "unsupported marketplace source: broken" ,
692+ } ) ;
693+ expect ( runCommandWithTimeoutMock ) . not . toHaveBeenCalled ( ) ;
694+ expect ( fetchWithSsrFGuardMock ) . not . toHaveBeenCalled ( ) ;
695+ } ) ;
696+ } ) ;
697+
536698 it ( "installs remote marketplace plugins from relative paths inside the cloned repo" , async ( ) => {
537699 mockRemoteMarketplaceClone ( {
538700 pluginDir : path . join ( "plugins" , "frontend-design" ) ,
0 commit comments