Skip to content

Commit 847e73a

Browse files
committed
test: add image serialization coverage
1 parent 05a102e commit 847e73a

3 files changed

Lines changed: 98 additions & 4 deletions

File tree

src/tests/OpenAI.IntegrationTests/Tests.Chat.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ public async Task ChatWithVision(CustomProvider customProvider)
130130
var pair = GetAuthorizedChatApi(customProvider, model: customProvider switch
131131
{
132132
CustomProvider.Together => "meta-llama/Llama-Vision-Free",
133-
CustomProvider.Groq => "llama-3.2-11b-vision-preview",
133+
CustomProvider.Groq => Environment.GetEnvironmentVariable("GROQ_VISION_MODEL") ??
134+
"meta-llama/llama-4-scout-17b-16e-instruct",
134135
_ => null,
135136
});
136137
using var api = pair.Api;

src/tests/OpenAI.IntegrationTests/Tests.Helpers.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ internal static (OpenAiClient Api, string Model) GetAuthorizedChatApi(CustomProv
5353
return (CustomProviders.Groq(apiKey:
5454
Environment.GetEnvironmentVariable("GROQ_API_KEY") ??
5555
throw new AssertInconclusiveException("GROQ_API_KEY environment variable is not found.")),
56-
model ?? "llama3-8b-8192");
56+
model ??
57+
Environment.GetEnvironmentVariable("GROQ_CHAT_MODEL") ??
58+
"llama-3.3-70b-versatile");
5759
}
5860
if (customProvider == CustomProvider.DeepSeek)
5961
{
@@ -118,7 +120,9 @@ internal static (OpenAiClient Api, string Model) GetAuthorizedChatApi(CustomProv
118120
return (CustomProviders.Hyperbolic(apiKey:
119121
Environment.GetEnvironmentVariable("HYPERBOLIC_API_KEY") ??
120122
throw new AssertInconclusiveException("HYPERBOLIC_API_KEY environment variable is not found.")),
121-
model ?? "meta-llama/Llama-3.2-3B-Instruct");
123+
model ??
124+
Environment.GetEnvironmentVariable("HYPERBOLIC_CHAT_MODEL") ??
125+
"meta-llama/Llama-3.3-70B-Instruct");
122126
}
123127

124128
if (customProvider == CustomProvider.Ollama)
@@ -149,7 +153,9 @@ internal static (OpenAiClient Api, string Model) GetAuthorizedChatApi(CustomProv
149153
return (CustomProviders.XAi(apiKey:
150154
Environment.GetEnvironmentVariable("XAI_API_KEY") ??
151155
throw new AssertInconclusiveException("XAI_API_KEY environment variable is not found.")),
152-
model ?? "grok-beta");
156+
model ??
157+
Environment.GetEnvironmentVariable("XAI_CHAT_MODEL") ??
158+
"grok-3-mini");
153159
}
154160
if (customProvider == CustomProvider.Cohere)
155161
{
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System.Net;
2+
using System.Text;
3+
using System.Text.Json;
4+
5+
namespace tryAGI.OpenAI.IntegrationTests;
6+
7+
public partial class Tests
8+
{
9+
[TestMethod]
10+
[DataRow(CreateImageRequestQuality.Medium, "medium")]
11+
[DataRow(CreateImageRequestQuality.High, "high")]
12+
[DataRow(CreateImageRequestQuality.Auto, "auto")]
13+
public async Task CreateImage_SerializesGptImageQualityValues(
14+
CreateImageRequestQuality quality,
15+
string expectedWireValue)
16+
{
17+
string? capturedBody = null;
18+
19+
using var httpClient = new HttpClient(new ScriptedHttpMessageHandler(async (request, cancellationToken) =>
20+
{
21+
request.Method.Should().Be(HttpMethod.Post);
22+
request.RequestUri.Should().Be(new Uri("https://api.openai.com/v1/images/generations"));
23+
request.Headers.Authorization?.Scheme.Should().Be("Bearer");
24+
request.Headers.Authorization?.Parameter.Should().Be("test-api-key");
25+
26+
capturedBody = await request.Content!.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
27+
28+
return new HttpResponseMessage(HttpStatusCode.OK)
29+
{
30+
Content = new StringContent(
31+
"""
32+
{
33+
"created": 1740000000,
34+
"data": [
35+
{
36+
"b64_json": "iVBORw0KGgo="
37+
}
38+
],
39+
"quality": "medium",
40+
"size": "1024x1024",
41+
"output_format": "png"
42+
}
43+
""",
44+
Encoding.UTF8,
45+
"application/json")
46+
};
47+
}))
48+
{
49+
BaseAddress = new Uri(OpenAiClient.DefaultBaseUrl)
50+
};
51+
52+
using var api = new OpenAiClient(
53+
"test-api-key",
54+
httpClient: httpClient,
55+
disposeHttpClient: false);
56+
57+
var response = await api.Images.CreateImageAsync(
58+
prompt: "Generate a test image",
59+
model: CreateImageRequestModel.GptImage15,
60+
n: 1,
61+
quality: quality,
62+
size: CreateImageRequestSize.x1024x1024,
63+
outputFormat: CreateImageRequestOutputFormat.Png);
64+
65+
capturedBody.Should().NotBeNullOrWhiteSpace();
66+
var requestJson = JsonDocument.Parse(capturedBody!);
67+
var root = requestJson.RootElement;
68+
69+
root.GetProperty("model").GetString().Should().Be("gpt-image-1.5");
70+
root.GetProperty("quality").GetString().Should().Be(expectedWireValue);
71+
root.GetProperty("size").GetString().Should().Be("1024x1024");
72+
root.GetProperty("output_format").GetString().Should().Be("png");
73+
capturedBody.Should().NotContain("\"standard\"");
74+
capturedBody.Should().NotContain("\"hd\"");
75+
76+
response.Data.Should().HaveCount(1);
77+
response.Data![0].B64Json.Should().Be("iVBORw0KGgo=");
78+
}
79+
80+
private sealed class ScriptedHttpMessageHandler(
81+
Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handler)
82+
: HttpMessageHandler
83+
{
84+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
85+
=> handler(request, cancellationToken);
86+
}
87+
}

0 commit comments

Comments
 (0)