-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathConversationProtocolTests.cs
99 lines (88 loc) · 3.72 KB
/
ConversationProtocolTests.cs
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
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using NUnit.Framework;
using OpenAI.Chat;
using OpenAI.RealtimeConversation;
using OpenAI.Tests.Telemetry;
using OpenAI.Tests.Utility;
using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using static OpenAI.Tests.Telemetry.TestMeterListener;
using static OpenAI.Tests.TestHelpers;
namespace OpenAI.Tests.Conversation;
#pragma warning disable OPENAI002
[TestFixture(true)]
[TestFixture(false)]
public class ConversationProtocolTests : ConversationTestFixtureBase
{
public ConversationProtocolTests(bool isAsync) : base(isAsync)
{ }
[Test]
public async Task ProtocolCanConfigureSession()
{
RealtimeConversationClient client = GetTestClient();
using RealtimeConversationSession session = await client.StartConversationSessionAsync(CancellationToken);
BinaryData configureSessionCommand = BinaryData.FromString("""
{
"type": "session.update",
"session": {
"turn_detection": null
}
}
""");
await session.SendCommandAsync(configureSessionCommand, CancellationOptions);
List<JsonNode> receivedCommands = [];
await foreach (ConversationUpdate update in session.ReceiveUpdatesAsync(CancellationToken))
{
BinaryData rawContentBytes = update.GetRawContent();
JsonNode jsonNode = JsonNode.Parse(rawContentBytes);
string updateType = jsonNode["type"]?.GetValue<string>();
Assert.That(updateType, Is.Not.Null.And.Not.Empty);
receivedCommands.Add(jsonNode);
if (updateType == "error")
{
Assert.Fail($"Error encountered: {rawContentBytes.ToString()}");
}
else if (updateType == "session.created")
{
BinaryData createResponseCommand = BinaryData.FromString("""
{
"type": "response.create",
"response": {
"max_output_tokens": null
}
}
""");
await session.SendCommandAsync(createResponseCommand, CancellationOptions);
}
else if (updateType == "response.done")
{
break;
}
}
List<JsonNode> NodesOfType(string type) => receivedCommands.Where(command => command["type"].GetValue<string>() == type).ToList();
Assert.That(NodesOfType("session.created"), Has.Count.EqualTo(1));
Assert.That(NodesOfType("session.updated"), Has.Count.EqualTo(1));
Assert.That(NodesOfType("response.created"), Has.Count.EqualTo(1));
Assert.That(NodesOfType("response.done"), Has.Count.EqualTo(1));
Assert.That(NodesOfType("response.output_item.added"), Has.Count.EqualTo(1));
Assert.That(NodesOfType("conversation.item.created"), Has.Count.EqualTo(1));
Assert.That(NodesOfType("response.content_part.added"), Has.Count.EqualTo(1));
Assert.That(NodesOfType("response.audio_transcript.delta"), Has.Count.GreaterThan(0));
Assert.That(NodesOfType("response.audio.delta"), Has.Count.GreaterThan(0));
Assert.That(NodesOfType("response.audio_transcript.done"), Has.Count.EqualTo(1));
Assert.That(NodesOfType("response.content_part.done"), Has.Count.EqualTo(1));
Assert.That(NodesOfType("response.output_item.done"), Has.Count.EqualTo(1));
}
}