@@ -42,6 +42,118 @@ func TestOpenAIComponent_GetAvailableModels(t *testing.T) {
4242 assert .Nil (t , models )
4343 })
4444
45+ t .Run ("anonymous user can see public CSGHub models" , func (t * testing.T ) {
46+ now := time .Now ()
47+ deploys := []database.Deploy {
48+ {
49+ ID : 1 ,
50+ SvcName : "svc1" ,
51+ Type : commontypes .InferenceType ,
52+ UserID : 1 ,
53+ SecureLevel : commontypes .EndpointPublic ,
54+ Repository : & database.Repository {
55+ Name : "model1" ,
56+ Path : "model1" ,
57+ },
58+ User : & database.User {
59+ Username : "publicuser" ,
60+ UUID : "publicuser-uuid" ,
61+ },
62+ Endpoint : "endpoint1" ,
63+ Task : "text-generation" ,
64+ },
65+ {
66+ ID : 2 ,
67+ SvcName : "svc2" ,
68+ Type : commontypes .ServerlessType ,
69+ UserID : 2 ,
70+ SecureLevel : commontypes .EndpointPublic ,
71+ Repository : & database.Repository {
72+ HFPath : "hf-model2" ,
73+ },
74+ User : & database.User {
75+ Username : "serverless-owner" ,
76+ UUID : "serverless-owner-uuid" ,
77+ },
78+ Endpoint : "endpoint2" ,
79+ Task : "text-to-image" ,
80+ },
81+ }
82+ deploys [0 ].CreatedAt = now
83+ deploys [1 ].CreatedAt = now
84+
85+ mockDeployStore .EXPECT ().RunningVisibleToUser (mock .Anything , int64 (0 )).
86+ Return (deploys , nil ).Once ()
87+ mockLLMConfigStore .EXPECT ().Index (mock .Anything , 50 , 1 , mock .Anything ).
88+ Return ([]* database.LLMConfig {}, 0 , nil )
89+
90+ expectModels := []types.Model {
91+ {
92+ BaseModel : types.BaseModel {
93+ ID : "model1:svc1" ,
94+ OwnedBy : "publicuser" ,
95+ Object : "model" ,
96+ Created : deploys [0 ].CreatedAt .Unix (),
97+ Task : "text-generation" ,
98+ DisplayName : "model1" ,
99+ Public : true ,
100+ },
101+ Endpoint : "endpoint1" ,
102+ InternalModelInfo : types.InternalModelInfo {
103+ CSGHubModelID : deploys [0 ].Repository .Path ,
104+ OwnerUUID : deploys [0 ].User .UUID ,
105+ ClusterID : deploys [0 ].ClusterID ,
106+ SvcName : deploys [0 ].SvcName ,
107+ SvcType : deploys [0 ].Type ,
108+ ImageID : deploys [0 ].ImageID ,
109+ },
110+ InternalUse : true ,
111+ },
112+ {
113+ BaseModel : types.BaseModel {
114+ ID : "hf-model2:svc2" ,
115+ OwnedBy : "OpenCSG" ,
116+ Object : "model" ,
117+ Created : deploys [1 ].CreatedAt .Unix (),
118+ Task : "text-to-image" ,
119+ Public : true ,
120+ },
121+ Endpoint : "endpoint2" ,
122+ InternalModelInfo : types.InternalModelInfo {
123+ OwnerUUID : deploys [1 ].User .UUID ,
124+ ClusterID : deploys [1 ].ClusterID ,
125+ SvcName : deploys [1 ].SvcName ,
126+ SvcType : deploys [1 ].Type ,
127+ ImageID : deploys [1 ].ImageID ,
128+ },
129+ InternalUse : true ,
130+ },
131+ }
132+ var wg sync.WaitGroup
133+ wg .Add (1 )
134+ for _ , model := range expectModels {
135+ expectJson , _ := json .Marshal (model )
136+ mockCache .EXPECT ().HSet (mock .Anything , modelCacheKey , model .ID , string (expectJson )).
137+ Return (nil ).Once ()
138+ }
139+ mockCache .EXPECT ().Expire (mock .Anything , modelCacheKey , modelCacheTTL ).
140+ RunAndReturn (func (ctx context.Context , s string , d time.Duration ) error {
141+ wg .Done ()
142+ return nil
143+ }).Once ()
144+
145+ models , err := comp .GetAvailableModels (context .Background (), "" )
146+ require .NoError (t , err )
147+ require .Len (t , models , 2 )
148+ assert .Equal (t , "model1:svc1" , models [0 ].ID )
149+ assert .Equal (t , "publicuser" , models [0 ].OwnedBy )
150+ assert .True (t , models [0 ].Public )
151+ assert .Equal (t , "hf-model2:svc2" , models [1 ].ID )
152+ assert .Equal (t , "OpenCSG" , models [1 ].OwnedBy )
153+ assert .True (t , models [1 ].Public )
154+ wg .Wait ()
155+ })
156+
45157 t .Run ("successful case" , func (t * testing.T ) {
46158 user := & database.User {
47159 ID : 1 ,
0 commit comments