1515package genai
1616
1717import (
18- "flag"
1918 "fmt"
20- "strings"
2119 "testing"
2220 "time"
2321
22+ "github.com/google/go-cmp/cmp"
23+ "github.com/google/go-cmp/cmp/cmpopts"
2424 "google.golang.org/genai"
2525)
2626
27- const (
28- apiMode = "api" // API mode runs the tests in any environment where the tests can hit the actual service.
29- unitMode = "unit" // Unit mode runs the test in the github actions using the mocked service (this is the default).
30- )
31-
32- var mode = flag .String ("mode" , unitMode , "Test mode" )
33-
34- func waitForAgentEngineOperation (tb testing.TB , name string , done bool , c * Client ) any {
35- tb .Helper ()
36- var res any
37- for ! done {
38- tb .Logf ("Waiting for operation to complete: [%s]\n " , name )
39- time .Sleep (5 * time .Second )
40- op , err := c .AgentEngines .getAgentOperation (tb .Context (), name , nil )
41- if err != nil {
42- tb .Fatalf ("getAgentOperation failed, err: %v" , err )
43- }
44- done = op .Done
45- res = op
46- }
47- return res
48- }
49-
50- func newTestClient (tb testing.TB ) * Client {
51- tb .Helper ()
52- client , err := NewGenAIClient (tb .Context (), & genai.ClientConfig {
53- Backend : genai .BackendVertexAI ,
54- })
55- if err != nil {
56- tb .Fatal (err )
57- }
58- return client
59- }
60-
61- func getResourceNameFromOperation (operationName string ) string {
62- i := strings .Index (operationName , "/operations/" )
63- return operationName [:i ]
64- }
65-
66- func createAgentEngineAndWait (t testing.TB , tt testing.TB , client * Client , config * CreateAgentEngineConfig ) * ReasoningEngine {
67- tt .Helper ()
68- if config == nil {
69- config = & CreateAgentEngineConfig {
70- DisplayName : tt .Name (),
71- Description : "You can remove this agent engine if it is older than 10 minutes. It must be an orphan AE." ,
72- }
73- }
74- if config .DisplayName == "" {
75- config .DisplayName = tt .Name ()
76- }
77- if config .Description == "" {
78- config .Description = "You can remove this agent engine if it is older than 10 minutes. It must be an orphan AE."
79- }
80- createOp , err := client .AgentEngines .create (tt .Context (), config )
81- if err != nil {
82- tt .Fatalf ("create() failed unexpectedly: %v" , err )
83- }
84- tt .Cleanup (cleanupAgentEngine (t , client , getResourceNameFromOperation (createOp .Name )))
85- createOp = waitForAgentEngineOperation (tt , createOp .Name , createOp .Done , client ).(* AgentEngineOperation )
86- return createOp .Response
87- }
88-
89- func cleanupAgentEngine (t testing.TB , client * Client , name string ) func () {
90- return func () {
91- t .Logf ("Cleaning up AgentEngine: %s" , name )
92- deleteOp , err := client .AgentEngines .delete (t .Context (), name , genai .Ptr (true ), nil )
93- if err != nil {
94- t .Logf ("cleanup() failed, err: %v" , err )
95- } else {
96- waitForAgentEngineOperation (t , deleteOp .Name , deleteOp .Done , client )
97- }
98- }
99- }
100-
10127func TestAgentEngines (t * testing.T ) {
10228 if * mode != apiMode {
10329 t .Skipf ("Skipping %s. We only tun these in the api mode." , t .Name ())
@@ -163,13 +89,14 @@ func TestAgentEngines(t *testing.T) {
16389 ctx := tt .Context ()
16490 client := newTestClient (t )
16591 re := createAgentEngineAndWait (t , tt , client , nil )
166-
16792 deleteOp , err := client .AgentEngines .delete (t .Context (), re .Name , nil , nil )
16893 if err != nil {
16994 tt .Fatalf ("delete() failed unexpectedly: %v" , err )
17095 }
171- waitForAgentEngineOperation (t , deleteOp .Name , deleteOp .Done , client )
172-
96+ operation := func () (* AgentEngineOperation , error ) {
97+ return client .AgentEngines .getAgentOperation (tt .Context (), deleteOp .Name , nil )
98+ }
99+ waitForOperation (t , operation )
173100 got , err := client .AgentEngines .get (ctx , re .Name , nil )
174101 if err == nil {
175102 t .Errorf ("delete() didn't remove the reasoning engine, want error(NOT_FOUND), got: %v" , got )
@@ -179,11 +106,23 @@ func TestAgentEngines(t *testing.T) {
179106 t .Run ("Get" , func (tt * testing.T ) {
180107 ctx := tt .Context ()
181108 client := newTestClient (tt )
182- re := createAgentEngineAndWait (t , tt , client , nil )
183- _ , err := client .AgentEngines .get (ctx , re .Name , nil )
109+ want := & ReasoningEngine {
110+ DisplayName : tt .Name (),
111+ Description : "You can remove this agent engine if it is older than 10 minutes. It must be an orphan AE." ,
112+ }
113+ config := & CreateAgentEngineConfig {
114+ DisplayName : want .DisplayName ,
115+ Description : want .Description ,
116+ }
117+ re := createAgentEngineAndWait (t , tt , client , config )
118+ got , err := client .AgentEngines .get (ctx , re .Name , nil )
184119 if err != nil {
185120 tt .Errorf ("get() failed unexpectedly: %v" , err )
186121 }
122+ if diff := cmp .Diff (got , want , cmpopts .IgnoreFields (ReasoningEngine {}, "CreateTime" , "Spec" , "UpdateTime" , "Name" )); diff != "" {
123+ tt .Errorf ("create() and get() had diff (-got +want): %v" , diff )
124+ }
125+
187126 })
188127
189128 t .Run ("List" , func (tt * testing.T ) {
@@ -210,11 +149,10 @@ func TestAgentEngines(t *testing.T) {
210149 if err != nil {
211150 tt .Fatalf ("update() failed unexpectedly: %v" , err )
212151 }
213- waitForAgentEngineOperation (tt , op .Name , op .Done , client )
214- updated , err := client .AgentEngines .get (ctx , re .Name , nil )
215- if err != nil {
216- tt .Errorf ("get() failed unexpectedly: %v" , err )
152+ operation := func () (* AgentEngineOperation , error ) {
153+ return client .AgentEngines .getAgentOperation (tt .Context (), op .Name , nil )
217154 }
155+ updated := waitForOperation (tt , operation ).Response
218156 if got := updated .DisplayName ; got != want {
219157 tt .Errorf ("update() returned DisplayName %v, want %v" , got , want )
220158 }
0 commit comments