openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Images/ImagesMockTests.cs
587lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Images; |
| 3 | using OpenAI.Tests.Utility; |
| 4 | using System; |
| 5 | using System.ClientModel; |
| 6 | using System.IO; |
| 7 | using System.Linq; |
| 8 | using System.Threading; |
| 9 | using System.Threading.Tasks; |
| 10 | |
| 11 | namespace OpenAI.Tests.Images; |
| 12 | |
| 13 | [TestFixture(true)] |
| 14 | [TestFixture(false)] |
| 15 | [Parallelizable(ParallelScope.All)] |
| 16 | [Category("Images")] |
| 17 | [Category("Smoke")] |
| 18 | public class ImagesMockTests : SyncAsyncTestBase |
| 19 | { |
| 20 | private static readonly ApiKeyCredential s_fakeCredential = new ApiKeyCredential("key"); |
| 21 | |
| 22 | public ImagesMockTests(bool isAsync) |
| 23 | : base(isAsync) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | public enum ImageSourceKind |
| 28 | { |
| 29 | UsingStream, |
| 30 | UsingFilePath |
| 31 | } |
| 32 | |
| 33 | private static Array s_imageSourceKindSource = Enum.GetValues(typeof(ImageSourceKind)); |
| 34 | |
| 35 | [Test] |
| 36 | public async Task GenerateImageDeserializesRevisedPrompt() |
| 37 | { |
| 38 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 39 | { |
| 40 | "data": [ |
| 41 | { |
| 42 | "revised_prompt": "new prompt" |
| 43 | } |
| 44 | ] |
| 45 | } |
| 46 | """); |
| 47 | ImageClient client = new ImageClient("model", s_fakeCredential, clientOptions); |
| 48 | |
| 49 | GeneratedImage image = IsAsync |
| 50 | ? await client.GenerateImageAsync("prompt") |
| 51 | : client.GenerateImage("prompt"); |
| 52 | |
| 53 | Assert.That(image.RevisedPrompt, Is.EqualTo("new prompt")); |
| 54 | } |
| 55 | |
| 56 | [Test] |
| 57 | public void GenerateImageRespectsTheCancellationToken() |
| 58 | { |
| 59 | ImageClient client = new ImageClient("model", s_fakeCredential); |
| 60 | using CancellationTokenSource cancellationSource = new(); |
| 61 | cancellationSource.Cancel(); |
| 62 | |
| 63 | if (IsAsync) |
| 64 | { |
| 65 | Assert.That(async () => await client.GenerateImageAsync("prompt", cancellationToken: cancellationSource.Token), |
| 66 | Throws.InstanceOf<OperationCanceledException>()); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | Assert.That(() => client.GenerateImage("prompt", cancellationToken: cancellationSource.Token), |
| 71 | Throws.InstanceOf<OperationCanceledException>()); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | [Test] |
| 76 | public async Task GenerateImagesDeserializesCreatedAt() |
| 77 | { |
| 78 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 79 | { |
| 80 | "created": 1704096000, |
| 81 | "data": [] |
| 82 | } |
| 83 | """); |
| 84 | ImageClient client = new ImageClient("model", s_fakeCredential, clientOptions); |
| 85 | |
| 86 | GeneratedImageCollection images = IsAsync |
| 87 | ? await client.GenerateImagesAsync("prompt", 2) |
| 88 | : client.GenerateImages("prompt", 2); |
| 89 | |
| 90 | Assert.That(images.CreatedAt.ToUnixTimeSeconds(), Is.EqualTo(1704096000)); |
| 91 | } |
| 92 | |
| 93 | [Test] |
| 94 | public async Task GenerateImagesDeserializesRevisedPrompt() |
| 95 | { |
| 96 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 97 | { |
| 98 | "data": [ |
| 99 | { |
| 100 | "revised_prompt": "new prompt" |
| 101 | } |
| 102 | ] |
| 103 | } |
| 104 | """); |
| 105 | ImageClient client = new ImageClient("model", s_fakeCredential, clientOptions); |
| 106 | |
| 107 | GeneratedImageCollection images = IsAsync |
| 108 | ? await client.GenerateImagesAsync("prompt", 2) |
| 109 | : client.GenerateImages("prompt", 2); |
| 110 | GeneratedImage image = images.Single(); |
| 111 | |
| 112 | Assert.That(image.RevisedPrompt, Is.EqualTo("new prompt")); |
| 113 | } |
| 114 | |
| 115 | [Test] |
| 116 | public void GenerateImagesRespectsTheCancellationToken() |
| 117 | { |
| 118 | ImageClient client = new ImageClient("model", s_fakeCredential); |
| 119 | using CancellationTokenSource cancellationSource = new(); |
| 120 | cancellationSource.Cancel(); |
| 121 | |
| 122 | if (IsAsync) |
| 123 | { |
| 124 | Assert.That(async () => await client.GenerateImagesAsync("prompt", 2, cancellationToken: cancellationSource.Token), |
| 125 | Throws.InstanceOf<OperationCanceledException>()); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | Assert.That(() => client.GenerateImages("prompt", 2, cancellationToken: cancellationSource.Token), |
| 130 | Throws.InstanceOf<OperationCanceledException>()); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | [Test] |
| 135 | [TestCaseSource(nameof(s_imageSourceKindSource))] |
| 136 | public async Task GenerateImageEditDeserializesRevisedPrompt(ImageSourceKind imageSourceKind) |
| 137 | { |
| 138 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 139 | { |
| 140 | "data": [ |
| 141 | { |
| 142 | "revised_prompt": "new prompt" |
| 143 | } |
| 144 | ] |
| 145 | } |
| 146 | """); |
| 147 | ImageClient client = new ImageClient("model", s_fakeCredential, clientOptions); |
| 148 | string maskFilename = "images_empty_room_with_mask.png"; |
| 149 | string maskImagePath = Path.Combine("Assets", maskFilename); |
| 150 | GeneratedImage image = null; |
| 151 | |
| 152 | if (imageSourceKind == ImageSourceKind.UsingStream) |
| 153 | { |
| 154 | using Stream stream = new MemoryStream(); |
| 155 | |
| 156 | image = IsAsync |
| 157 | ? await client.GenerateImageEditAsync(stream, "filename", "prompt") |
| 158 | : client.GenerateImageEdit(stream, "filename", "prompt"); |
| 159 | } |
| 160 | else if (imageSourceKind == ImageSourceKind.UsingFilePath) |
| 161 | { |
| 162 | image = IsAsync |
| 163 | ? await client.GenerateImageEditAsync(maskImagePath, "prompt") |
| 164 | : client.GenerateImageEdit(maskImagePath, "prompt"); |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | Assert.Fail("Invalid source kind."); |
| 169 | } |
| 170 | |
| 171 | Assert.That(image.RevisedPrompt, Is.EqualTo("new prompt")); |
| 172 | } |
| 173 | |
| 174 | [Test] |
| 175 | public void GenerateImageEditFromStreamRespectsTheCancellationToken() |
| 176 | { |
| 177 | ImageClient client = new ImageClient("model", s_fakeCredential); |
| 178 | using Stream stream = new MemoryStream(); |
| 179 | using CancellationTokenSource cancellationSource = new(); |
| 180 | cancellationSource.Cancel(); |
| 181 | |
| 182 | if (IsAsync) |
| 183 | { |
| 184 | Assert.That(async () => await client.GenerateImageEditAsync(stream, "filename", "prompt", cancellationToken: cancellationSource.Token), |
| 185 | Throws.InstanceOf<OperationCanceledException>()); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | Assert.That(() => client.GenerateImageEdit(stream, "filename", "prompt", cancellationToken: cancellationSource.Token), |
| 190 | Throws.InstanceOf<OperationCanceledException>()); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | [Test] |
| 195 | [TestCaseSource(nameof(s_imageSourceKindSource))] |
| 196 | public async Task GenerateImageEditWithMaskDeserializesRevisedPrompt(ImageSourceKind imageSourceKind) |
| 197 | { |
| 198 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 199 | { |
| 200 | "data": [ |
| 201 | { |
| 202 | "revised_prompt": "new prompt" |
| 203 | } |
| 204 | ] |
| 205 | } |
| 206 | """); |
| 207 | ImageClient client = new ImageClient("model", s_fakeCredential, clientOptions); |
| 208 | string originalImageFilename = "images_empty_room.png"; |
| 209 | string maskFilename = "images_empty_room_with_mask.png"; |
| 210 | string originalImagePath = Path.Combine("Assets", originalImageFilename); |
| 211 | string maskImagePath = Path.Combine("Assets", maskFilename); |
| 212 | GeneratedImage image = null; |
| 213 | |
| 214 | if (imageSourceKind == ImageSourceKind.UsingStream) |
| 215 | { |
| 216 | using Stream stream = new MemoryStream(); |
| 217 | using Stream mask = new MemoryStream(); |
| 218 | |
| 219 | image = IsAsync |
| 220 | ? await client.GenerateImageEditAsync(stream, "filename", "prompt", mask, "maskFilename") |
| 221 | : client.GenerateImageEdit(stream, "filename", "prompt", mask, "maskFilename"); |
| 222 | } |
| 223 | else if (imageSourceKind == ImageSourceKind.UsingFilePath) |
| 224 | { |
| 225 | image = IsAsync |
| 226 | ? await client.GenerateImageEditAsync(originalImagePath, "prompt", maskImagePath) |
| 227 | : client.GenerateImageEdit(originalImagePath, "prompt", maskImagePath); |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | Assert.Fail("Invalid source kind."); |
| 232 | } |
| 233 | |
| 234 | Assert.That(image.RevisedPrompt, Is.EqualTo("new prompt")); |
| 235 | } |
| 236 | |
| 237 | [Test] |
| 238 | public void GenerateImageEditFromStreamWithMaskRespectsTheCancellationToken() |
| 239 | { |
| 240 | ImageClient client = new ImageClient("model", s_fakeCredential); |
| 241 | using Stream stream = new MemoryStream(); |
| 242 | using CancellationTokenSource cancellationSource = new(); |
| 243 | cancellationSource.Cancel(); |
| 244 | |
| 245 | if (IsAsync) |
| 246 | { |
| 247 | Assert.That(async () => await client.GenerateImageEditAsync(stream, "filename", "prompt", stream, "maskFilename", cancellationToken: cancellationSource.Token), |
| 248 | Throws.InstanceOf<OperationCanceledException>()); |
| 249 | } |
| 250 | else |
| 251 | { |
| 252 | Assert.That(() => client.GenerateImageEdit(stream, "filename", "prompt", stream, "maskFilename", cancellationToken: cancellationSource.Token), |
| 253 | Throws.InstanceOf<OperationCanceledException>()); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | [Test] |
| 258 | [TestCaseSource(nameof(s_imageSourceKindSource))] |
| 259 | public async Task GenerateImageEditsDeserializesCreatedAt(ImageSourceKind imageSourceKind) |
| 260 | { |
| 261 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 262 | { |
| 263 | "created": 1704096000, |
| 264 | "data": [] |
| 265 | } |
| 266 | """); |
| 267 | ImageClient client = new ImageClient("model", s_fakeCredential, clientOptions); |
| 268 | string maskFilename = "images_empty_room_with_mask.png"; |
| 269 | string maskImagePath = Path.Combine("Assets", maskFilename); |
| 270 | GeneratedImageCollection images = null; |
| 271 | |
| 272 | if (imageSourceKind == ImageSourceKind.UsingStream) |
| 273 | { |
| 274 | using Stream stream = new MemoryStream(); |
| 275 | |
| 276 | images = IsAsync |
| 277 | ? await client.GenerateImageEditsAsync(stream, "filename", "prompt", 2) |
| 278 | : client.GenerateImageEdits(stream, "filename", "prompt", 2); |
| 279 | } |
| 280 | else if (imageSourceKind == ImageSourceKind.UsingFilePath) |
| 281 | { |
| 282 | images = IsAsync |
| 283 | ? await client.GenerateImageEditsAsync(maskImagePath, "prompt", 2) |
| 284 | : client.GenerateImageEdits(maskImagePath, "prompt", 2); |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | Assert.Fail("Invalid source kind."); |
| 289 | } |
| 290 | |
| 291 | Assert.That(images.CreatedAt.ToUnixTimeSeconds(), Is.EqualTo(1704096000)); |
| 292 | } |
| 293 | |
| 294 | [Test] |
| 295 | [TestCaseSource(nameof(s_imageSourceKindSource))] |
| 296 | public async Task GenerateImageEditsDeserializesRevisedPrompt(ImageSourceKind imageSourceKind) |
| 297 | { |
| 298 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 299 | { |
| 300 | "data": [ |
| 301 | { |
| 302 | "revised_prompt": "new prompt" |
| 303 | } |
| 304 | ] |
| 305 | } |
| 306 | """); |
| 307 | ImageClient client = new ImageClient("model", s_fakeCredential, clientOptions); |
| 308 | string maskFilename = "images_empty_room_with_mask.png"; |
| 309 | string maskImagePath = Path.Combine("Assets", maskFilename); |
| 310 | GeneratedImageCollection images = null; |
| 311 | |
| 312 | if (imageSourceKind == ImageSourceKind.UsingStream) |
| 313 | { |
| 314 | using Stream stream = new MemoryStream(); |
| 315 | |
| 316 | images = IsAsync |
| 317 | ? await client.GenerateImageEditsAsync(stream, "filename", "prompt", 2) |
| 318 | : client.GenerateImageEdits(stream, "filename", "prompt", 2); |
| 319 | } |
| 320 | else if (imageSourceKind == ImageSourceKind.UsingFilePath) |
| 321 | { |
| 322 | images = IsAsync |
| 323 | ? await client.GenerateImageEditsAsync(maskImagePath, "prompt", 2) |
| 324 | : client.GenerateImageEdits(maskImagePath, "prompt", 2); |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | Assert.Fail("Invalid source kind."); |
| 329 | } |
| 330 | |
| 331 | GeneratedImage image = images.Single(); |
| 332 | Assert.That(image.RevisedPrompt, Is.EqualTo("new prompt")); |
| 333 | } |
| 334 | |
| 335 | [Test] |
| 336 | public void GenerateImageEditsFromStreamRespectsTheCancellationToken() |
| 337 | { |
| 338 | ImageClient client = new ImageClient("model", s_fakeCredential); |
| 339 | using Stream stream = new MemoryStream(); |
| 340 | using CancellationTokenSource cancellationSource = new(); |
| 341 | cancellationSource.Cancel(); |
| 342 | |
| 343 | if (IsAsync) |
| 344 | { |
| 345 | Assert.That(async () => await client.GenerateImageEditsAsync(stream, "filename", "prompt", 2, cancellationToken: cancellationSource.Token), |
| 346 | Throws.InstanceOf<OperationCanceledException>()); |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | Assert.That(() => client.GenerateImageEdits(stream, "filename", "prompt", 2, cancellationToken: cancellationSource.Token), |
| 351 | Throws.InstanceOf<OperationCanceledException>()); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | [Test] |
| 356 | [TestCaseSource(nameof(s_imageSourceKindSource))] |
| 357 | public async Task GenerateImageEditsWithMaskDeserializesRevisedPrompt(ImageSourceKind imageSourceKind) |
| 358 | { |
| 359 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 360 | { |
| 361 | "data": [ |
| 362 | { |
| 363 | "revised_prompt": "new prompt" |
| 364 | } |
| 365 | ] |
| 366 | } |
| 367 | """); |
| 368 | ImageClient client = new ImageClient("model", s_fakeCredential, clientOptions); |
| 369 | string originalImageFilename = "images_empty_room.png"; |
| 370 | string maskFilename = "images_empty_room_with_mask.png"; |
| 371 | string originalImagePath = Path.Combine("Assets", originalImageFilename); |
| 372 | string maskImagePath = Path.Combine("Assets", maskFilename); |
| 373 | GeneratedImageCollection images = null; |
| 374 | |
| 375 | if (imageSourceKind == ImageSourceKind.UsingStream) |
| 376 | { |
| 377 | using Stream stream = new MemoryStream(); |
| 378 | using Stream mask = new MemoryStream(); |
| 379 | |
| 380 | images = IsAsync |
| 381 | ? await client.GenerateImageEditsAsync(stream, "filename", "prompt", mask, "maskFilename", 2) |
| 382 | : client.GenerateImageEdits(stream, "filename", "prompt", mask, "maskFilename", 2); |
| 383 | } |
| 384 | else if (imageSourceKind == ImageSourceKind.UsingFilePath) |
| 385 | { |
| 386 | images = IsAsync |
| 387 | ? await client.GenerateImageEditsAsync(originalImagePath, "prompt", maskImagePath, 2) |
| 388 | : client.GenerateImageEdits(originalImagePath, "prompt", maskImagePath, 2); |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | Assert.Fail("Invalid source kind."); |
| 393 | } |
| 394 | |
| 395 | GeneratedImage image = images.Single(); |
| 396 | Assert.That(image.RevisedPrompt, Is.EqualTo("new prompt")); |
| 397 | } |
| 398 | |
| 399 | [Test] |
| 400 | public void GenerateImageEditsFromStreamWithMaskRespectsTheCancellationToken() |
| 401 | { |
| 402 | ImageClient client = new ImageClient("model", s_fakeCredential); |
| 403 | using Stream stream = new MemoryStream(); |
| 404 | using CancellationTokenSource cancellationSource = new(); |
| 405 | cancellationSource.Cancel(); |
| 406 | |
| 407 | if (IsAsync) |
| 408 | { |
| 409 | Assert.That(async () => await client.GenerateImageEditsAsync(stream, "filename", "prompt", stream, "maskFilename", 2, cancellationToken: cancellationSource.Token), |
| 410 | Throws.InstanceOf<OperationCanceledException>()); |
| 411 | } |
| 412 | else |
| 413 | { |
| 414 | Assert.That(() => client.GenerateImageEdits(stream, "filename", "prompt", stream, "maskFilename", 2, cancellationToken: cancellationSource.Token), |
| 415 | Throws.InstanceOf<OperationCanceledException>()); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | [Test] |
| 420 | [TestCaseSource(nameof(s_imageSourceKindSource))] |
| 421 | public async Task GenerateImageVariationDeserializesRevisedPrompt(ImageSourceKind imageSourceKind) |
| 422 | { |
| 423 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 424 | { |
| 425 | "data": [ |
| 426 | { |
| 427 | "revised_prompt": "new prompt" |
| 428 | } |
| 429 | ] |
| 430 | } |
| 431 | """); |
| 432 | ImageClient client = new ImageClient("model", s_fakeCredential, clientOptions); |
| 433 | string imageFilename = "images_dog_and_cat.png"; |
| 434 | string imagePath = Path.Combine("Assets", imageFilename); |
| 435 | GeneratedImage image = null; |
| 436 | |
| 437 | if (imageSourceKind == ImageSourceKind.UsingStream) |
| 438 | { |
| 439 | using Stream stream = new MemoryStream(); |
| 440 | |
| 441 | image = IsAsync |
| 442 | ? await client.GenerateImageVariationAsync(stream, "filename") |
| 443 | : client.GenerateImageVariation(stream, "filename"); |
| 444 | } |
| 445 | else if (imageSourceKind == ImageSourceKind.UsingFilePath) |
| 446 | { |
| 447 | image = IsAsync |
| 448 | ? await client.GenerateImageVariationAsync(imagePath) |
| 449 | : client.GenerateImageVariation(imagePath); |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | Assert.Fail("Invalid source kind."); |
| 454 | } |
| 455 | |
| 456 | Assert.That(image.RevisedPrompt, Is.EqualTo("new prompt")); |
| 457 | } |
| 458 | |
| 459 | [Test] |
| 460 | public void GenerateImageVariationFromStreamRespectsTheCancellationToken() |
| 461 | { |
| 462 | ImageClient client = new ImageClient("model", s_fakeCredential); |
| 463 | using Stream stream = new MemoryStream(); |
| 464 | using CancellationTokenSource cancellationSource = new(); |
| 465 | cancellationSource.Cancel(); |
| 466 | |
| 467 | if (IsAsync) |
| 468 | { |
| 469 | Assert.That(async () => await client.GenerateImageVariationAsync(stream, "filename", cancellationToken: cancellationSource.Token), |
| 470 | Throws.InstanceOf<OperationCanceledException>()); |
| 471 | } |
| 472 | else |
| 473 | { |
| 474 | Assert.That(() => client.GenerateImageVariation(stream, "filename", cancellationToken: cancellationSource.Token), |
| 475 | Throws.InstanceOf<OperationCanceledException>()); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | [Test] |
| 480 | [TestCaseSource(nameof(s_imageSourceKindSource))] |
| 481 | public async Task GenerateImageVariationsDeserializesCreatedAt(ImageSourceKind imageSourceKind) |
| 482 | { |
| 483 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 484 | { |
| 485 | "created": 1704096000, |
| 486 | "data": [] |
| 487 | } |
| 488 | """); |
| 489 | ImageClient client = new ImageClient("model", s_fakeCredential, clientOptions); |
| 490 | string imageFilename = "images_dog_and_cat.png"; |
| 491 | string imagePath = Path.Combine("Assets", imageFilename); |
| 492 | GeneratedImageCollection images = null; |
| 493 | |
| 494 | if (imageSourceKind == ImageSourceKind.UsingStream) |
| 495 | { |
| 496 | using Stream stream = new MemoryStream(); |
| 497 | |
| 498 | images = IsAsync |
| 499 | ? await client.GenerateImageVariationsAsync(stream, "filename", 2) |
| 500 | : client.GenerateImageVariations(stream, "filename", 2); |
| 501 | } |
| 502 | else if (imageSourceKind == ImageSourceKind.UsingFilePath) |
| 503 | { |
| 504 | images = IsAsync |
| 505 | ? await client.GenerateImageVariationsAsync(imagePath, 2) |
| 506 | : client.GenerateImageVariations(imagePath, 2); |
| 507 | } |
| 508 | else |
| 509 | { |
| 510 | Assert.Fail("Invalid source kind."); |
| 511 | } |
| 512 | |
| 513 | Assert.That(images.CreatedAt.ToUnixTimeSeconds(), Is.EqualTo(1704096000)); |
| 514 | } |
| 515 | |
| 516 | [Test] |
| 517 | [TestCaseSource(nameof(s_imageSourceKindSource))] |
| 518 | public async Task GenerateImageVariationsDeserializesRevisedPrompt(ImageSourceKind imageSourceKind) |
| 519 | { |
| 520 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 521 | { |
| 522 | "data": [ |
| 523 | { |
| 524 | "revised_prompt": "new prompt" |
| 525 | } |
| 526 | ] |
| 527 | } |
| 528 | """); |
| 529 | ImageClient client = new ImageClient("model", s_fakeCredential, clientOptions); |
| 530 | string imageFilename = "images_dog_and_cat.png"; |
| 531 | string imagePath = Path.Combine("Assets", imageFilename); |
| 532 | GeneratedImageCollection images = null; |
| 533 | |
| 534 | if (imageSourceKind == ImageSourceKind.UsingStream) |
| 535 | { |
| 536 | using Stream stream = new MemoryStream(); |
| 537 | |
| 538 | images = IsAsync |
| 539 | ? await client.GenerateImageVariationsAsync(stream, "filename", 2) |
| 540 | : client.GenerateImageVariations(stream, "filename", 2); |
| 541 | } |
| 542 | else if (imageSourceKind == ImageSourceKind.UsingFilePath) |
| 543 | { |
| 544 | images = IsAsync |
| 545 | ? await client.GenerateImageVariationsAsync(imagePath, 2) |
| 546 | : client.GenerateImageVariations(imagePath, 2); |
| 547 | } |
| 548 | else |
| 549 | { |
| 550 | Assert.Fail("Invalid source kind."); |
| 551 | } |
| 552 | |
| 553 | GeneratedImage image = images.Single(); |
| 554 | Assert.That(image.RevisedPrompt, Is.EqualTo("new prompt")); |
| 555 | } |
| 556 | |
| 557 | [Test] |
| 558 | public void GenerateImageVariationsFromStreamRespectsTheCancellationToken() |
| 559 | { |
| 560 | ImageClient client = new ImageClient("model", s_fakeCredential); |
| 561 | using Stream stream = new MemoryStream(); |
| 562 | using CancellationTokenSource cancellationSource = new(); |
| 563 | cancellationSource.Cancel(); |
| 564 | |
| 565 | if (IsAsync) |
| 566 | { |
| 567 | Assert.That(async () => await client.GenerateImageVariationsAsync(stream, "filename", 2, cancellationToken: cancellationSource.Token), |
| 568 | Throws.InstanceOf<OperationCanceledException>()); |
| 569 | } |
| 570 | else |
| 571 | { |
| 572 | Assert.That(() => client.GenerateImageVariations(stream, "filename", 2, cancellationToken: cancellationSource.Token), |
| 573 | Throws.InstanceOf<OperationCanceledException>()); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | private OpenAIClientOptions GetClientOptionsWithMockResponse(int status, string content) |
| 578 | { |
| 579 | MockPipelineResponse response = new MockPipelineResponse(status); |
| 580 | response.SetContent(content); |
| 581 | |
| 582 | return new OpenAIClientOptions() |
| 583 | { |
| 584 | Transport = new MockPipelineTransport(response) |
| 585 | }; |
| 586 | } |
| 587 | } |