1+ /*
2+ * Copyright 2025 Google LLC
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * https://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ using System ;
18+ using System . Collections . Generic ;
19+ using System . Linq ;
20+ using System . Threading . Tasks ;
21+
22+ using Google . GenAI ;
23+ using Google . GenAI . Types ;
24+
25+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
26+
27+ using TestServerSdk ;
28+
29+ [ TestClass ]
30+ public class ListModelsTest
31+ {
32+ private static TestServerProcess ? _server ;
33+ private Client vertexClient ;
34+ private Client geminiClient ;
35+ public TestContext TestContext { get ; set ; }
36+
37+ [ ClassInitialize ]
38+ public static void ClassInit ( TestContext _ )
39+ {
40+ _server = TestServer . StartTestServer ( ) ;
41+ }
42+
43+ [ ClassCleanup ]
44+ public static void ClassCleanup ( )
45+ {
46+ TestServer . StopTestServer ( _server ) ;
47+ }
48+
49+ [ TestInitialize ]
50+ public void TestInit ( )
51+ {
52+ // Test server specific setup.
53+ if ( _server == null )
54+ {
55+ throw new InvalidOperationException ( "Test server is not initialized." ) ;
56+ }
57+ var geminiClientHttpOptions = new HttpOptions
58+ {
59+ Headers = new Dictionary < string , string > { { "Test-Name" ,
60+ $ "{ GetType ( ) . Name } .{ TestContext . TestName } " } } ,
61+ BaseUrl = "http://localhost:1453"
62+ } ;
63+ var vertexClientHttpOptions = new HttpOptions
64+ {
65+ Headers = new Dictionary < string , string > { { "Test-Name" ,
66+ $ "{ GetType ( ) . Name } .{ TestContext . TestName } " } } ,
67+ BaseUrl = "http://localhost:1454"
68+ } ;
69+
70+ // Common setup for both clients.
71+ string project = System . Environment . GetEnvironmentVariable ( "GOOGLE_CLOUD_PROJECT" ) ;
72+ string location =
73+ System . Environment . GetEnvironmentVariable ( "GOOGLE_CLOUD_LOCATION" ) ?? "us-central1" ;
74+ string apiKey = System . Environment . GetEnvironmentVariable ( "GOOGLE_API_KEY" ) ;
75+ vertexClient = new Client ( project : project , location : location , vertexAI : true ,
76+ credential : TestServer . GetCredentialForTestMode ( ) ,
77+ httpOptions : vertexClientHttpOptions ) ;
78+ geminiClient =
79+ new Client ( apiKey : apiKey , vertexAI : false , httpOptions : geminiClientHttpOptions ) ;
80+ }
81+
82+ [ TestMethod ]
83+ public async Task ListTunedModelsVertex ( )
84+ {
85+ var config = new ListModelsConfig { QueryBase = false , PageSize = 3 } ;
86+ var pager = await vertexClient . Models . ListAsync ( config ) ;
87+ Assert . IsNotNull ( pager ) ;
88+
89+ int count = 0 ;
90+ await foreach ( var model in pager )
91+ {
92+ count ++ ;
93+ if ( count >= 4 ) break ;
94+ }
95+ Assert . IsTrue ( count >= 0 ) ;
96+ }
97+
98+ [ TestMethod ]
99+ public async Task ListTunedModelsGemini ( )
100+ {
101+ var config = new ListModelsConfig { QueryBase = false , PageSize = 3 } ;
102+ var pager = await geminiClient . Models . ListAsync ( config ) ;
103+ Assert . IsNotNull ( pager ) ;
104+
105+ int count = 0 ;
106+ await foreach ( var model in pager )
107+ {
108+ count ++ ;
109+ if ( count >= 4 ) break ;
110+ }
111+ Assert . IsTrue ( count >= 0 ) ;
112+ }
113+
114+ [ TestMethod ]
115+ public async Task ListBaseModelsVertex ( )
116+ {
117+ // Page size set to avoid long output
118+ var config = new ListModelsConfig { PageSize = 5 } ;
119+ var pager = await vertexClient . Models . ListAsync ( config ) ;
120+ Assert . IsNotNull ( pager ) ;
121+
122+ int count = 0 ;
123+ await foreach ( var model in pager )
124+ {
125+ count ++ ;
126+ if ( count >= 10 ) break ;
127+ }
128+ Assert . IsTrue ( count == 10 ) ;
129+ }
130+
131+ [ TestMethod ]
132+ public async Task ListBaseModelsGemini ( )
133+ {
134+ var pager = await geminiClient . Models . ListAsync ( ) ;
135+ Assert . IsNotNull ( pager ) ;
136+
137+ int count = 0 ;
138+ await foreach ( var model in pager )
139+ {
140+ count ++ ;
141+ if ( count >= 4 ) break ;
142+ }
143+ Assert . IsTrue ( count >= 0 ) ;
144+ }
145+
146+ [ TestMethod ]
147+ public async Task ListBaseModelsWithConfigVertex ( )
148+ {
149+ var config = new ListModelsConfig { QueryBase = true , PageSize = 10 } ;
150+ var pager = await vertexClient . Models . ListAsync ( config ) ;
151+ Assert . IsNotNull ( pager ) ;
152+
153+ int count = 0 ;
154+ await foreach ( var model in pager )
155+ {
156+ count ++ ;
157+ if ( count >= 4 ) break ;
158+ }
159+ Assert . IsTrue ( count >= 0 ) ;
160+ }
161+
162+ [ TestMethod ]
163+ public async Task ListBaseModelsWithConfigGemini ( )
164+ {
165+ var config = new ListModelsConfig { QueryBase = true , PageSize = 5 } ;
166+ var pager = await geminiClient . Models . ListAsync ( config ) ;
167+ Assert . IsNotNull ( pager ) ;
168+
169+ int count = 0 ;
170+ await foreach ( var model in pager )
171+ {
172+ count ++ ;
173+ if ( count >= 4 ) break ;
174+ }
175+ Assert . IsTrue ( count >= 0 ) ;
176+ }
177+ }
0 commit comments