Skip to content

Commit 30ea2d0

Browse files
committed
add chat tests
1 parent 4325136 commit 30ea2d0

File tree

54 files changed

+83371
-60
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+83371
-60
lines changed

tests/Assistants/AssistantsTests.cs

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using NUnit.Framework;
1+
using Microsoft.ClientModel.TestFramework;
2+
using NUnit.Framework;
23
using NUnit.Framework.Internal;
34
using OpenAI.Assistants;
45
using OpenAI.Files;
5-
using OpenAI.Tests.Utility;
66
using OpenAI.VectorStores;
77
using System;
88
using System.ClientModel;
@@ -19,10 +19,8 @@ namespace OpenAI.Tests.Assistants;
1919

2020
#pragma warning disable OPENAI001
2121

22-
[TestFixture(true)]
23-
[TestFixture(false)]
2422
[Category("Assistants")]
25-
public class AssistantsTests : SyncAsyncTestBase
23+
public class AssistantsTests : ClientTestBase
2624
{
2725
private readonly List<Assistant> _assistantsToDelete = [];
2826
private readonly List<AssistantThread> _threadsToDelete = [];
@@ -599,11 +597,10 @@ public async Task FunctionToolsWork()
599597
Assert.That(messages[0].Content[0].Text.ToLowerInvariant(), Does.Contain("tacos"));
600598
}
601599

600+
[AsyncOnly]
602601
[Test]
603602
public async Task StreamingRunWorksAsync()
604603
{
605-
AssertAsyncOnly();
606-
607604
AssistantClient client = GetTestClient();
608605
Assistant assistant = await client.CreateAssistantAsync("gpt-4o-mini");
609606
Validate(assistant);
@@ -649,11 +646,10 @@ AsyncCollectionResult<StreamingUpdate> streamingResult
649646
Print(">>> Done <<<");
650647
}
651648

649+
[SyncOnly]
652650
[Test]
653651
public void StreamingRunWorks()
654652
{
655-
AssertSyncOnly();
656-
657653
AssistantClient client = GetTestClient();
658654
Assistant assistant = client.CreateAssistant("gpt-4o-mini");
659655
Validate(assistant);
@@ -699,11 +695,10 @@ CollectionResult<StreamingUpdate> streamingResult
699695
Print(">>> Done <<<");
700696
}
701697

698+
[AsyncOnly]
702699
[TestCase]
703700
public async Task StreamingToolCallAsync()
704701
{
705-
AssertAsyncOnly();
706-
707702
AssistantClient client = GetTestClient();
708703
FunctionToolDefinition getWeatherTool = new("get_current_weather")
709704
{
@@ -762,11 +757,10 @@ public async Task StreamingToolCallAsync()
762757
} while (run?.Status.IsTerminal == false);
763758
}
764759

760+
[SyncOnly]
765761
[TestCase]
766762
public void StreamingToolCall()
767763
{
768-
AssertSyncOnly();
769-
770764
AssistantClient client = GetTestClient();
771765
FunctionToolDefinition getWeatherTool = new("get_current_weather")
772766
{
@@ -1028,11 +1022,10 @@ This file describes the favorite foods of several people.
10281022
});
10291023
}
10301024

1025+
[SyncOnly]
10311026
[Test]
10321027
public void FileSearchStreamingWorksSync()
10331028
{
1034-
AssertSyncOnly();
1035-
10361029
const string fileContent = """
10371030
The favorite food of several people:
10381031
- Summanus Ferdinand: tacos
@@ -1119,11 +1112,10 @@ public void FileSearchStreamingWorksSync()
11191112
Assert.That(message, Does.Contain("cake"));
11201113
}
11211114

1115+
[AsyncOnly]
11221116
[Test]
11231117
public async Task FileSearchStreamingWorksAsync()
11241118
{
1125-
AssertAsyncOnly();
1126-
11271119
const string fileContent = """
11281120
The favorite food of several people:
11291121
- Summanus Ferdinand: tacos
@@ -1212,11 +1204,10 @@ public async Task FileSearchStreamingWorksAsync()
12121204
Assert.That(message, Does.Contain("cake"));
12131205
}
12141206

1207+
[AsyncOnly]
12151208
[Test]
12161209
public async Task Pagination_CanEnumerateAssistantsAsync()
12171210
{
1218-
AssertAsyncOnly();
1219-
12201211
const int TestAssistantCount = 10;
12211212

12221213
AssistantClient client = GetTestClient();
@@ -1257,11 +1248,10 @@ public async Task Pagination_CanEnumerateAssistantsAsync()
12571248
Assert.That(count, Is.GreaterThanOrEqualTo(TestAssistantCount));
12581249
}
12591250

1251+
[SyncOnly]
12601252
[Test]
12611253
public void Pagination_CanEnumerateAssistants()
12621254
{
1263-
AssertSyncOnly();
1264-
12651255
const int TestAssistantCount = 10;
12661256

12671257
AssistantClient client = GetTestClient();
@@ -1302,11 +1292,10 @@ public void Pagination_CanEnumerateAssistants()
13021292
Assert.That(count, Is.GreaterThanOrEqualTo(TestAssistantCount));
13031293
}
13041294

1295+
[AsyncOnly]
13051296
[Test]
13061297
public async Task Pagination_CanPageThroughAssistantCollectionAsync()
13071298
{
1308-
AssertAsyncOnly();
1309-
13101299
const int TestAssistantCount = 10;
13111300
const int TestPageSizeLimit = 2;
13121301

@@ -1360,11 +1349,10 @@ public async Task Pagination_CanPageThroughAssistantCollectionAsync()
13601349
Assert.That(pageCount, Is.GreaterThanOrEqualTo(TestAssistantCount / TestPageSizeLimit));
13611350
}
13621351

1352+
[SyncOnly]
13631353
[Test]
13641354
public void Pagination_CanPageThroughAssistantCollection()
13651355
{
1366-
AssertSyncOnly();
1367-
13681356
const int TestAssistantCount = 10;
13691357
const int TestPageSizeLimit = 2;
13701358

@@ -1428,11 +1416,10 @@ private static IEnumerable<Assistant> GetAssistantsFromPage(ClientResult page)
14281416
return els.Select(el => ModelReaderWriter.Read<Assistant>(BinaryData.FromString(el.GetRawText())));
14291417
}
14301418

1419+
[AsyncOnly]
14311420
[Test]
14321421
public async Task Pagination_CanRehydrateAssistantPageCollectionFromBytesAsync()
14331422
{
1434-
AssertAsyncOnly();
1435-
14361423
const int TestAssistantCount = 10;
14371424
const int TestPageSizeLimit = 2;
14381425

@@ -1502,11 +1489,10 @@ public async Task Pagination_CanRehydrateAssistantPageCollectionFromBytesAsync()
15021489
Assert.That(pageCount, Is.GreaterThanOrEqualTo(TestAssistantCount / TestPageSizeLimit));
15031490
}
15041491

1492+
[SyncOnly]
15051493
[Test]
15061494
public void Pagination_CanRehydrateAssistantPageCollectionFromBytes()
15071495
{
1508-
AssertSyncOnly();
1509-
15101496
const int TestAssistantCount = 10;
15111497
const int TestPageSizeLimit = 2;
15121498

@@ -1577,11 +1563,10 @@ public void Pagination_CanRehydrateAssistantPageCollectionFromBytes()
15771563
Assert.That(pageCount, Is.GreaterThanOrEqualTo(TestAssistantCount / TestPageSizeLimit));
15781564
}
15791565

1566+
[AsyncOnly]
15801567
[Test]
15811568
public async Task Pagination_CanRehydrateAssistantPageCollectionFromPageTokenAsync()
15821569
{
1583-
AssertAsyncOnly();
1584-
15851570
const int TestAssistantCount = 10;
15861571
const int TestPageSizeLimit = 2;
15871572

@@ -1661,11 +1646,10 @@ public async Task Pagination_CanRehydrateAssistantPageCollectionFromPageTokenAsy
16611646
Assert.That(pageCount, Is.GreaterThanOrEqualTo(TestAssistantCount / TestPageSizeLimit));
16621647
}
16631648

1649+
[SyncOnly]
16641650
[Test]
16651651
public void Pagination_CanRehydrateAssistantPageCollectionFromPageToken()
16661652
{
1667-
AssertSyncOnly();
1668-
16691653
const int TestAssistantCount = 10;
16701654
const int TestPageSizeLimit = 2;
16711655

@@ -1744,11 +1728,10 @@ public void Pagination_CanRehydrateAssistantPageCollectionFromPageToken()
17441728
Assert.That(pageCount, Is.GreaterThanOrEqualTo(TestAssistantCount / TestPageSizeLimit));
17451729
}
17461730

1731+
[AsyncOnly]
17471732
[Test]
17481733
public async Task Pagination_CanRehydrateRunStepPageCollectionFromBytesAsync()
17491734
{
1750-
AssertAsyncOnly();
1751-
17521735
AssistantClient client = GetTestClient();
17531736
Assistant assistant = client.CreateAssistant("gpt-4o", new AssistantCreationOptions()
17541737
{
@@ -1824,14 +1807,13 @@ public async Task Pagination_CanRehydrateRunStepPageCollectionFromBytesAsync()
18241807
.ToListAsync();
18251808
}
18261809

1827-
CollectionAssert.AreEqual(runSteps, rehydratedRunSteps);
1810+
Assert.That(rehydratedRunSteps, Is.EqualTo(runSteps).AsCollection);
18281811
}
18291812

1813+
[SyncOnly]
18301814
[Test]
18311815
public async Task Pagination_CanRehydrateRunStepPageCollectionFromBytes()
18321816
{
1833-
AssertSyncOnly();
1834-
18351817
AssistantClient client = GetTestClient();
18361818
Assistant assistant = client.CreateAssistant("gpt-4o", new AssistantCreationOptions()
18371819
{
@@ -1907,7 +1889,7 @@ public async Task Pagination_CanRehydrateRunStepPageCollectionFromBytes()
19071889
.ToList();
19081890
}
19091891

1910-
CollectionAssert.AreEqual(runSteps, rehydratedRunSteps);
1892+
Assert.That(rehydratedRunSteps, Is.EqualTo(runSteps).AsCollection);
19111893
}
19121894

19131895
[Test]

tests/Chat/ChatTests.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace OpenAI.Tests.Chat;
2424
[Category("Chat")]
2525
public class ChatTests : OpenAIRecordedTestBase
2626
{
27-
public ChatTests(bool isAsync) : base(isAsync, RecordedTestMode.Record)
27+
public ChatTests(bool isAsync) : base(isAsync)
2828
{
2929
TestTimeoutInSeconds = 30;
3030
}
@@ -786,28 +786,31 @@ public async Task WebSearchWorks()
786786
[Test]
787787
public async Task FileIdContentWorks()
788788
{
789-
OpenAIFileClient fileClient = GetProxiedOpenAIClient<OpenAIFileClient>(TestScenario.Files);
790-
OpenAIFile testInputFile = await fileClient.UploadFileAsync(
791-
Path.Combine("Assets", "files_travis_favorite_food.pdf"),
792-
FileUploadPurpose.UserData);
793-
Validate(testInputFile);
794-
795-
ChatMessageContentPart fileIdContentPart
796-
= ChatMessageContentPart.CreateFilePart(testInputFile.Id);
797-
Assert.That(fileIdContentPart.FileId, Is.EqualTo(testInputFile.Id));
798-
Assert.That(fileIdContentPart.FileBytes, Is.Null);
799-
Assert.That(fileIdContentPart.FileBytesMediaType, Is.Null);
800-
Assert.That(fileIdContentPart.Filename, Is.Null);
801-
802-
ChatClient client = GetTestClient();
803-
ChatCompletion completion = await client.CompleteChatAsync(
804-
[
805-
ChatMessage.CreateUserMessage(
789+
using (Recording.DisableRequestBodyRecording()) // Temp while multipart support in the test proxy is in progress
790+
{
791+
OpenAIFileClient fileClient = GetProxiedOpenAIClient<OpenAIFileClient>(TestScenario.Files);
792+
OpenAIFile testInputFile = await fileClient.UploadFileAsync(
793+
Path.Combine("Assets", "files_travis_favorite_food.pdf"),
794+
FileUploadPurpose.UserData);
795+
Validate(testInputFile);
796+
797+
ChatMessageContentPart fileIdContentPart
798+
= ChatMessageContentPart.CreateFilePart(testInputFile.Id);
799+
Assert.That(fileIdContentPart.FileId, Is.EqualTo(testInputFile.Id));
800+
Assert.That(fileIdContentPart.FileBytes, Is.Null);
801+
Assert.That(fileIdContentPart.FileBytesMediaType, Is.Null);
802+
Assert.That(fileIdContentPart.Filename, Is.Null);
803+
804+
ChatClient client = GetTestClient();
805+
ChatCompletion completion = await client.CompleteChatAsync(
806+
[
807+
ChatMessage.CreateUserMessage(
806808
"Based on the following, what food should I order for whom?",
807809
fileIdContentPart)
808-
]);
809-
Assert.That(completion?.Content, Is.Not.Null.And.Not.Empty);
810-
Assert.That(completion.Content[0].Text?.ToLower(), Does.Contain("pizza"));
810+
]);
811+
Assert.That(completion?.Content, Is.Not.Null.And.Not.Empty);
812+
Assert.That(completion.Content[0].Text?.ToLower(), Does.Contain("pizza"));
813+
}
811814
}
812815

813816
[Test]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"Entries": [
3+
{
4+
"RequestUri": "https://api.openai.com/v1/chat/completions",
5+
"RequestMethod": "POST",
6+
"RequestHeaders": {
7+
"Accept": "application/json",
8+
"Authorization": "Sanitized",
9+
"Content-Length": "110",
10+
"Content-Type": "application/json",
11+
"User-Agent": "OpenAI/2.4.0 (.NET 9.0.9; Microsoft Windows 10.0.26100)"
12+
},
13+
"RequestBody": {
14+
"messages": [
15+
{
16+
"role": "user",
17+
"content": "Uh oh, this isn't going to work with that key"
18+
}
19+
],
20+
"model": "gpt-4o-mini"
21+
},
22+
"StatusCode": 401,
23+
"ResponseHeaders": {
24+
"Alt-Svc": "h3=\":443\"",
25+
"cf-cache-status": "DYNAMIC",
26+
"CF-RAY": "9801cd4e0d1098e5-SEA",
27+
"Connection": "keep-alive",
28+
"Content-Length": "288",
29+
"Content-Type": "application/json; charset=utf-8",
30+
"Date": "Tue, 16 Sep 2025 16:39:48 GMT",
31+
"Server": "cloudflare",
32+
"Strict-Transport-Security": "max-age=31536000; includeSubDomains; preload",
33+
"Vary": "Origin",
34+
"X-Content-Type-Options": "nosniff",
35+
"x-envoy-upstream-service-time": "2",
36+
"x-openai-proxy-wasm": "v0.1",
37+
"X-Request-ID": "Sanitized"
38+
},
39+
"ResponseBody": {
40+
"error": {
41+
"message": "Incorrect API key provided: not-a-re**************************ized. You can find your API key at https://platform.openai.com/account/api-keys.",
42+
"type": "invalid_request_error",
43+
"param": null,
44+
"code": "invalid_api_key"
45+
}
46+
}
47+
}
48+
],
49+
"Variables": {}
50+
}

0 commit comments

Comments
 (0)