|
1 | 1 | package openai
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "strings" |
4 | 6 | "testing"
|
5 | 7 |
|
6 | 8 | "github.com/danielmiessler/fabric/chat"
|
@@ -218,3 +220,121 @@ func TestAddImageGenerationToolWithDynamicFormat(t *testing.T) {
|
218 | 220 | })
|
219 | 221 | }
|
220 | 222 | }
|
| 223 | + |
| 224 | +func TestSupportsImageGeneration(t *testing.T) { |
| 225 | + tests := []struct { |
| 226 | + name string |
| 227 | + model string |
| 228 | + expected bool |
| 229 | + }{ |
| 230 | + { |
| 231 | + name: "gpt-4o supports image generation", |
| 232 | + model: "gpt-4o", |
| 233 | + expected: true, |
| 234 | + }, |
| 235 | + { |
| 236 | + name: "gpt-4o-mini supports image generation", |
| 237 | + model: "gpt-4o-mini", |
| 238 | + expected: true, |
| 239 | + }, |
| 240 | + { |
| 241 | + name: "gpt-4.1 supports image generation", |
| 242 | + model: "gpt-4.1", |
| 243 | + expected: true, |
| 244 | + }, |
| 245 | + { |
| 246 | + name: "gpt-4.1-mini supports image generation", |
| 247 | + model: "gpt-4.1-mini", |
| 248 | + expected: true, |
| 249 | + }, |
| 250 | + { |
| 251 | + name: "gpt-4.1-nano supports image generation", |
| 252 | + model: "gpt-4.1-nano", |
| 253 | + expected: true, |
| 254 | + }, |
| 255 | + { |
| 256 | + name: "o3 supports image generation", |
| 257 | + model: "o3", |
| 258 | + expected: true, |
| 259 | + }, |
| 260 | + { |
| 261 | + name: "o1 does not support image generation", |
| 262 | + model: "o1", |
| 263 | + expected: false, |
| 264 | + }, |
| 265 | + { |
| 266 | + name: "o1-mini does not support image generation", |
| 267 | + model: "o1-mini", |
| 268 | + expected: false, |
| 269 | + }, |
| 270 | + { |
| 271 | + name: "o3-mini does not support image generation", |
| 272 | + model: "o3-mini", |
| 273 | + expected: false, |
| 274 | + }, |
| 275 | + { |
| 276 | + name: "gpt-4 does not support image generation", |
| 277 | + model: "gpt-4", |
| 278 | + expected: false, |
| 279 | + }, |
| 280 | + { |
| 281 | + name: "gpt-3.5-turbo does not support image generation", |
| 282 | + model: "gpt-3.5-turbo", |
| 283 | + expected: false, |
| 284 | + }, |
| 285 | + { |
| 286 | + name: "empty model does not support image generation", |
| 287 | + model: "", |
| 288 | + expected: false, |
| 289 | + }, |
| 290 | + } |
| 291 | + |
| 292 | + for _, tt := range tests { |
| 293 | + t.Run(tt.name, func(t *testing.T) { |
| 294 | + result := supportsImageGeneration(tt.model) |
| 295 | + assert.Equal(t, tt.expected, result) |
| 296 | + }) |
| 297 | + } |
| 298 | +} |
| 299 | + |
| 300 | +func TestModelValidationLogic(t *testing.T) { |
| 301 | + t.Run("Unsupported model with image file should return validation error", func(t *testing.T) { |
| 302 | + opts := &common.ChatOptions{ |
| 303 | + Model: "o1-mini", |
| 304 | + ImageFile: "/tmp/output.png", |
| 305 | + } |
| 306 | + |
| 307 | + // Test the validation logic directly |
| 308 | + if opts.ImageFile != "" && !supportsImageGeneration(opts.Model) { |
| 309 | + err := fmt.Errorf("model '%s' does not support image generation. Supported models: %s", opts.Model, strings.Join(ImageGenerationSupportedModels, ", ")) |
| 310 | + |
| 311 | + assert.Contains(t, err.Error(), "does not support image generation") |
| 312 | + assert.Contains(t, err.Error(), "o1-mini") |
| 313 | + assert.Contains(t, err.Error(), "Supported models:") |
| 314 | + } else { |
| 315 | + t.Error("Expected validation to trigger") |
| 316 | + } |
| 317 | + }) |
| 318 | + |
| 319 | + t.Run("Supported model with image file should not trigger validation", func(t *testing.T) { |
| 320 | + opts := &common.ChatOptions{ |
| 321 | + Model: "gpt-4o", |
| 322 | + ImageFile: "/tmp/output.png", |
| 323 | + } |
| 324 | + |
| 325 | + // Test the validation logic directly |
| 326 | + shouldFail := opts.ImageFile != "" && !supportsImageGeneration(opts.Model) |
| 327 | + assert.False(t, shouldFail, "Validation should not trigger for supported model") |
| 328 | + }) |
| 329 | + |
| 330 | + t.Run("Unsupported model without image file should not trigger validation", func(t *testing.T) { |
| 331 | + opts := &common.ChatOptions{ |
| 332 | + Model: "o1-mini", |
| 333 | + ImageFile: "", // No image file |
| 334 | + } |
| 335 | + |
| 336 | + // Test the validation logic directly |
| 337 | + shouldFail := opts.ImageFile != "" && !supportsImageGeneration(opts.Model) |
| 338 | + assert.False(t, shouldFail, "Validation should not trigger when no image file is specified") |
| 339 | + }) |
| 340 | +} |
0 commit comments