|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package genai |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "log" |
| 21 | + "net/http" |
| 22 | + "net/http/httptest" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "cloud.google.com/go/auth" |
| 26 | +) |
| 27 | + |
| 28 | +func TestChatsUnitTest(t *testing.T) { |
| 29 | + ctx := context.Background() |
| 30 | + t.Run("TestServer", func(t *testing.T) { |
| 31 | + t.Parallel() |
| 32 | + if isDisabledTest(t) { |
| 33 | + t.Skip("Skip: disabled test") |
| 34 | + } |
| 35 | + // Create a test server |
| 36 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 37 | + w.WriteHeader(http.StatusOK) |
| 38 | + fmt.Fprintln(w, `{ |
| 39 | + "candidates": [ |
| 40 | + { |
| 41 | + "content": { |
| 42 | + "role": "model", |
| 43 | + "parts": [ |
| 44 | + { |
| 45 | + "text": "1 + 2 = 3" |
| 46 | + } |
| 47 | + ] |
| 48 | + }, |
| 49 | + "finishReason": "STOP", |
| 50 | + "avgLogprobs": -0.6608115907699342 |
| 51 | + } |
| 52 | + ] |
| 53 | + } |
| 54 | + `) |
| 55 | + })) |
| 56 | + defer ts.Close() |
| 57 | + |
| 58 | + t.Logf("Using test server: %s", ts.URL) |
| 59 | + cc := &ClientConfig{ |
| 60 | + HTTPOptions: HTTPOptions{ |
| 61 | + BaseURL: ts.URL, |
| 62 | + }, |
| 63 | + HTTPClient: ts.Client(), |
| 64 | + Credentials: &auth.Credentials{}, |
| 65 | + } |
| 66 | + ac := &apiClient{clientConfig: cc} |
| 67 | + client := &Client{ |
| 68 | + clientConfig: *cc, |
| 69 | + Chats: &Chats{apiClient: ac}, |
| 70 | + } |
| 71 | + |
| 72 | + // Create a new Chat. |
| 73 | + var config *GenerateContentConfig = &GenerateContentConfig{Temperature: Ptr[float32](0.5)} |
| 74 | + chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", config, nil) |
| 75 | + if err != nil { |
| 76 | + log.Fatal(err) |
| 77 | + } |
| 78 | + |
| 79 | + part := Part{Text: "What is 1 + 2?"} |
| 80 | + |
| 81 | + result, err := chat.SendMessage(ctx, part) |
| 82 | + if err != nil { |
| 83 | + log.Fatal(err) |
| 84 | + } |
| 85 | + if result.Text() == "" { |
| 86 | + t.Errorf("Response text should not be empty") |
| 87 | + } |
| 88 | + }) |
| 89 | + |
| 90 | +} |
| 91 | + |
| 92 | +func TestChatsText(t *testing.T) { |
| 93 | + if *mode != apiMode { |
| 94 | + t.Skip("Skip. This test is only in the API mode") |
| 95 | + } |
| 96 | + ctx := context.Background() |
| 97 | + for _, backend := range backends { |
| 98 | + t.Run(backend.name, func(t *testing.T) { |
| 99 | + t.Parallel() |
| 100 | + if isDisabledTest(t) { |
| 101 | + t.Skip("Skip: disabled test") |
| 102 | + } |
| 103 | + client, err := NewClient(ctx, &ClientConfig{Backend: backend.Backend}) |
| 104 | + if err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + // Create a new Chat. |
| 108 | + var config *GenerateContentConfig = &GenerateContentConfig{Temperature: Ptr[float32](0.5)} |
| 109 | + chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", config, nil) |
| 110 | + if err != nil { |
| 111 | + log.Fatal(err) |
| 112 | + } |
| 113 | + |
| 114 | + part := Part{Text: "What is 1 + 2?"} |
| 115 | + |
| 116 | + result, err := chat.SendMessage(ctx, part) |
| 117 | + if err != nil { |
| 118 | + log.Fatal(err) |
| 119 | + } |
| 120 | + if result.Text() == "" { |
| 121 | + t.Errorf("Response text should not be empty") |
| 122 | + } |
| 123 | + }) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestChatsParts(t *testing.T) { |
| 128 | + if *mode != apiMode { |
| 129 | + t.Skip("Skip. This test is only in the API mode") |
| 130 | + } |
| 131 | + ctx := context.Background() |
| 132 | + for _, backend := range backends { |
| 133 | + t.Run(backend.name, func(t *testing.T) { |
| 134 | + t.Parallel() |
| 135 | + if isDisabledTest(t) { |
| 136 | + t.Skip("Skip: disabled test") |
| 137 | + } |
| 138 | + client, err := NewClient(ctx, &ClientConfig{Backend: backend.Backend}) |
| 139 | + if err != nil { |
| 140 | + t.Fatal(err) |
| 141 | + } |
| 142 | + // Create a new Chat. |
| 143 | + var config *GenerateContentConfig = &GenerateContentConfig{Temperature: Ptr[float32](0.5)} |
| 144 | + chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", config, nil) |
| 145 | + if err != nil { |
| 146 | + log.Fatal(err) |
| 147 | + } |
| 148 | + |
| 149 | + parts := make([]Part, 2) |
| 150 | + parts[0] = Part{Text: "What is "} |
| 151 | + parts[1] = Part{Text: "1 + 2?"} |
| 152 | + |
| 153 | + // Send chat message. |
| 154 | + result, err := chat.SendMessage(ctx, parts...) |
| 155 | + if err != nil { |
| 156 | + log.Fatal(err) |
| 157 | + } |
| 158 | + if result.Text() == "" { |
| 159 | + t.Errorf("Response text should not be empty") |
| 160 | + } |
| 161 | + }) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func TestChats2Messages(t *testing.T) { |
| 166 | + if *mode != apiMode { |
| 167 | + t.Skip("Skip. This test is only in the API mode") |
| 168 | + } |
| 169 | + ctx := context.Background() |
| 170 | + for _, backend := range backends { |
| 171 | + t.Run(backend.name, func(t *testing.T) { |
| 172 | + t.Parallel() |
| 173 | + if isDisabledTest(t) { |
| 174 | + t.Skip("Skip: disabled test") |
| 175 | + } |
| 176 | + client, err := NewClient(ctx, &ClientConfig{Backend: backend.Backend}) |
| 177 | + if err != nil { |
| 178 | + t.Fatal(err) |
| 179 | + } |
| 180 | + // Create a new Chat. |
| 181 | + var config *GenerateContentConfig = &GenerateContentConfig{Temperature: Ptr[float32](0.5)} |
| 182 | + chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", config, nil) |
| 183 | + if err != nil { |
| 184 | + log.Fatal(err) |
| 185 | + } |
| 186 | + |
| 187 | + // Send first chat message. |
| 188 | + part := Part{Text: "What is 1 + 2?"} |
| 189 | + |
| 190 | + result, err := chat.SendMessage(ctx, part) |
| 191 | + if err != nil { |
| 192 | + log.Fatal(err) |
| 193 | + } |
| 194 | + if result.Text() == "" { |
| 195 | + t.Errorf("Response text should not be empty") |
| 196 | + } |
| 197 | + |
| 198 | + // Send second chat message. |
| 199 | + part = Part{Text: "Add 1 to the previous result."} |
| 200 | + result, err = chat.SendMessage(ctx, part) |
| 201 | + if err != nil { |
| 202 | + log.Fatal(err) |
| 203 | + } |
| 204 | + if result.Text() == "" { |
| 205 | + t.Errorf("Response text should not be empty") |
| 206 | + } |
| 207 | + }) |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +func TestChatsHistory(t *testing.T) { |
| 212 | + if *mode != apiMode { |
| 213 | + t.Skip("Skip. This test is only in the API mode") |
| 214 | + } |
| 215 | + ctx := context.Background() |
| 216 | + for _, backend := range backends { |
| 217 | + t.Run(backend.name, func(t *testing.T) { |
| 218 | + t.Parallel() |
| 219 | + if isDisabledTest(t) { |
| 220 | + t.Skip("Skip: disabled test") |
| 221 | + } |
| 222 | + client, err := NewClient(ctx, &ClientConfig{Backend: backend.Backend}) |
| 223 | + if err != nil { |
| 224 | + t.Fatal(err) |
| 225 | + } |
| 226 | + // Create a new Chat with handwritten history. |
| 227 | + var config *GenerateContentConfig = &GenerateContentConfig{Temperature: Ptr[float32](0.5)} |
| 228 | + history := []*Content{ |
| 229 | + &Content{ |
| 230 | + Role: "user", |
| 231 | + Parts: []*Part{ |
| 232 | + &Part{Text: "What is 1 + 2?"}, |
| 233 | + }, |
| 234 | + }, |
| 235 | + &Content{ |
| 236 | + Role: "model", |
| 237 | + Parts: []*Part{ |
| 238 | + &Part{Text: "3"}, |
| 239 | + }, |
| 240 | + }, |
| 241 | + } |
| 242 | + chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", config, history) |
| 243 | + if err != nil { |
| 244 | + log.Fatal(err) |
| 245 | + } |
| 246 | + |
| 247 | + // Send chat message. |
| 248 | + part := Part{Text: "Add 1 to the previous result."} |
| 249 | + result, err := chat.SendMessage(ctx, part) |
| 250 | + if err != nil { |
| 251 | + log.Fatal(err) |
| 252 | + } |
| 253 | + if result.Text() == "" { |
| 254 | + t.Errorf("Response text should not be empty") |
| 255 | + } |
| 256 | + }) |
| 257 | + } |
| 258 | +} |
0 commit comments