-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathItemContentConverterTests.cs
More file actions
200 lines (171 loc) · 7.34 KB
/
ItemContentConverterTests.cs
File metadata and controls
200 lines (171 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) Microsoft. All rights reserved.
using System;
using Microsoft.Agents.AI.Hosting.OpenAI.Responses.Converters;
using Microsoft.Agents.AI.Hosting.OpenAI.Responses.Models;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.Hosting.OpenAI.UnitTests;
/// <summary>
/// Tests for ItemContentConverter focusing on MIME type handling for image URIs.
/// </summary>
public sealed class ItemContentConverterTests
{
[Theory]
[InlineData("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==", "image/png")]
[InlineData("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlbaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD5/ooooA//2Q==", "image/jpeg")]
[InlineData("data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==", "image/gif")]
[InlineData("data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAgA0JaQAA3AA/vuUAAA=", "image/webp")]
[InlineData("data:image/bmp;base64,Qk0eAAAAAAAAABoAAAAMAAAAAQAAAAEAAAABACAAAAA=", "image/bmp")]
public void ToAIContent_DataUri_PreservesMimeType(string dataUri, string expectedMediaType)
{
// Arrange
ItemContentInputImage inputImage = new ItemContentInputImage
{
ImageUrl = dataUri
};
// Act
AIContent? result = ItemContentConverter.ToAIContent(inputImage);
// Assert
Assert.NotNull(result);
DataContent dataContent = Assert.IsType<DataContent>(result);
Assert.Equal(expectedMediaType, dataContent.MediaType);
Assert.Equal(dataUri, dataContent.Uri);
}
[Theory]
[InlineData("https://example.com/image.png", "image/png")]
[InlineData("https://example.com/photo.jpg", "image/jpeg")]
[InlineData("https://example.com/photo.jpeg", "image/jpeg")]
[InlineData("https://example.com/animation.gif", "image/gif")]
[InlineData("https://example.com/picture.bmp", "image/bmp")]
[InlineData("https://example.com/modern.webp", "image/webp")]
[InlineData("https://example.com/IMAGE.PNG", "image/png")] // Case insensitive
[InlineData("https://example.com/PHOTO.JPG", "image/jpeg")] // Case insensitive
public void ToAIContent_HttpUri_InfersMimeTypeFromExtension(string uri, string expectedMediaType)
{
// Arrange
ItemContentInputImage inputImage = new ItemContentInputImage
{
ImageUrl = uri
};
// Act
AIContent? result = ItemContentConverter.ToAIContent(inputImage);
// Assert
Assert.NotNull(result);
UriContent uriContent = Assert.IsType<UriContent>(result);
Assert.Equal(expectedMediaType, uriContent.MediaType);
Assert.Equal(uri, uriContent.Uri?.ToString());
}
[Theory]
[InlineData("https://example.com/image")]
[InlineData("https://example.com/image.unknown")]
[InlineData("https://example.com/image.txt")]
public void ToAIContent_HttpUri_UnknownExtension_UsesGenericMimeType(string uri)
{
// Arrange
ItemContentInputImage inputImage = new ItemContentInputImage
{
ImageUrl = uri
};
// Act
AIContent? result = ItemContentConverter.ToAIContent(inputImage);
// Assert
Assert.NotNull(result);
UriContent uriContent = Assert.IsType<UriContent>(result);
Assert.Equal("image/*", uriContent.MediaType);
Assert.Equal(uri, uriContent.Uri?.ToString());
}
[Fact]
public void ToAIContent_DataUriPng_CreatesDataContent()
{
// Arrange
const string DataUri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==";
ItemContentInputImage inputImage = new ItemContentInputImage
{
ImageUrl = DataUri
};
// Act
AIContent? result = ItemContentConverter.ToAIContent(inputImage);
// Assert
Assert.NotNull(result);
DataContent dataContent = Assert.IsType<DataContent>(result);
Assert.Equal("image/png", dataContent.MediaType);
Assert.Equal(DataUri, dataContent.Uri);
}
[Fact]
public void ToAIContent_HttpUriPng_CreatesUriContent()
{
// Arrange
const string Uri = "https://example.com/test.png";
ItemContentInputImage inputImage = new ItemContentInputImage
{
ImageUrl = Uri
};
// Act
AIContent? result = ItemContentConverter.ToAIContent(inputImage);
// Assert
Assert.NotNull(result);
UriContent uriContent = Assert.IsType<UriContent>(result);
Assert.Equal("image/png", uriContent.MediaType);
Assert.Equal(Uri, uriContent.Uri?.ToString());
}
[Fact]
public void ToAIContent_FileId_CreatesHostedFileContent()
{
// Arrange
const string FileId = "file-abc123";
ItemContentInputImage inputImage = new ItemContentInputImage
{
FileId = FileId
};
// Act
AIContent? result = ItemContentConverter.ToAIContent(inputImage);
// Assert
Assert.NotNull(result);
HostedFileContent hostedFile = Assert.IsType<HostedFileContent>(result);
Assert.Equal(FileId, hostedFile.FileId);
}
[Fact]
public void ToAIContent_NullImageUrl_ReturnsNull()
{
// Arrange
ItemContentInputImage inputImage = new ItemContentInputImage
{
ImageUrl = null
};
// Act
AIContent? result = ItemContentConverter.ToAIContent(inputImage);
// Assert
Assert.Null(result);
}
[Fact]
public void ToAIContent_EmptyImageUrl_ReturnsNull()
{
// Arrange
ItemContentInputImage inputImage = new ItemContentInputImage
{
ImageUrl = string.Empty
};
// Act
AIContent? result = ItemContentConverter.ToAIContent(inputImage);
// Assert
Assert.Null(result);
}
[Fact]
public void ToAIContent_PreservesImageDetail()
{
// Arrange
const string Uri = "https://example.com/test.png";
ItemContentInputImage inputImage = new ItemContentInputImage
{
ImageUrl = Uri,
Detail = "high"
};
// Act
AIContent? result = ItemContentConverter.ToAIContent(inputImage);
// Assert
Assert.NotNull(result);
UriContent uriContent = Assert.IsType<UriContent>(result);
Assert.NotNull(uriContent.AdditionalProperties);
Assert.True(uriContent.AdditionalProperties.TryGetValue("detail", out object? detail));
Assert.Equal("high", detail?.ToString());
}
}