-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathConversationTestFixtureBase.cs
67 lines (59 loc) · 2.39 KB
/
ConversationTestFixtureBase.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
using NUnit.Framework;
using OpenAI.RealtimeConversation;
using OpenAI.Tests.Utility;
using System;
using System.ClientModel.Primitives;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
using static OpenAI.Tests.TestHelpers;
namespace OpenAI.Tests.Conversation;
#pragma warning disable OPENAI002
[Parallelizable(ParallelScope.All)]
[Category("Conversation")]
public class ConversationTestFixtureBase : SyncAsyncTestBase
{
public CancellationTokenSource CancellationTokenSource { get; }
public CancellationToken CancellationToken => CancellationTokenSource?.Token ?? default;
public RequestOptions CancellationOptions => new() { CancellationToken = CancellationToken };
public ConversationTestFixtureBase(bool isAsync) : base(isAsync)
{
CancellationTokenSource = new();
if (!Debugger.IsAttached)
{
CancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(15));
}
}
public static RealtimeConversationClient GetTestClient()
{
RealtimeConversationClient client = GetTestClient<RealtimeConversationClient>(TestScenario.RealtimeConversation);
client.OnSendingCommand += (_, data) => PrintMessageData(data, "> ");
client.OnReceivingCommand += (_, data) => PrintMessageData(data, " < ");
return client;
}
public static void PrintMessageData(BinaryData data, string prefix = "")
{
JsonNode jsonNode = JsonNode.Parse(data.ToString());
foreach ((string labelKey, string labelValue, string dataKey) in new (string, string, string)[]
{
("type", "input_audio_buffer.append", "audio"),
("type", "audio", "data"),
("event", "add_user_audio", "data"),
("type", "response.audio.delta", "delta")
})
{
if (jsonNode[labelKey]?.GetValue<string>() == labelValue)
{
string rawBase64Data = jsonNode[dataKey]?.GetValue<string>();
if (rawBase64Data is not null)
{
byte[] base64Data = rawBase64Data == null ? [] : Convert.FromBase64String(rawBase64Data);
jsonNode[dataKey] = $"<{base64Data.Length} bytes>";
}
}
}
string rawMessage = jsonNode.ToJsonString(new JsonSerializerOptions());
Console.WriteLine($"{prefix}{rawMessage}");
}
}