openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.6.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/Audio/TranslationMockTests.cs

161lines · modecode

1using System;
2using System.ClientModel;
3using System.IO;
4using System.Linq;
5using System.Threading;
6using System.Threading.Tasks;
7using Microsoft.ClientModel.TestFramework;
8using Microsoft.ClientModel.TestFramework.Mocks;
9using NUnit.Framework;
10using OpenAI.Audio;
11
12namespace OpenAI.Tests.Audio;
13
14[Parallelizable(ParallelScope.All)]
15[Category("Audio")]
16[Category("Smoke")]
17public partial class TranslationMockTests : ClientTestBase
18{
19 private static readonly ApiKeyCredential s_fakeCredential = new ApiKeyCredential("key");
20
21 public TranslationMockTests(bool isAsync)
22 : base(isAsync)
23 {
24 }
25
26 public enum AudioSourceKind
27 {
28 UsingStream,
29 UsingFilePath
30 }
31
32 [Test]
33 [TestCase(AudioSourceKind.UsingStream)]
34 [TestCase(AudioSourceKind.UsingFilePath)]
35 public async Task TranslateAudioDeserializesLanguage(AudioSourceKind audioSourceKind)
36 {
37 OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """
38 {
39 "language": "la"
40 }
41 """);
42 AudioTranslation translation = await InvokeTranslateAudioSyncOrAsync(clientOptions, audioSourceKind);
43
44 Assert.That(translation.Language, Is.EqualTo("la"));
45 }
46
47 [TestCase(AudioSourceKind.UsingStream)]
48 [TestCase(AudioSourceKind.UsingFilePath)]
49 public async Task TranslateAudioDeserializesDuration(AudioSourceKind audioSourceKind)
50 {
51 OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """
52 {
53 "duration": 185
54 }
55 """);
56 AudioTranslation translation = await InvokeTranslateAudioSyncOrAsync(clientOptions, audioSourceKind);
57
58 Assert.That(translation.Duration, Is.EqualTo(TimeSpan.FromSeconds(185)));
59 }
60
61 [Test]
62 [TestCase(AudioSourceKind.UsingStream)]
63 [TestCase(AudioSourceKind.UsingFilePath)]
64 public async Task TranslateAudioDeserializesText(AudioSourceKind audioSourceKind)
65 {
66 OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """
67 {
68 "text": "The quick brown fox got lost."
69 }
70 """);
71 AudioTranslation translation = await InvokeTranslateAudioSyncOrAsync(clientOptions, audioSourceKind);
72
73 Assert.That(translation.Text, Is.EqualTo("The quick brown fox got lost."));
74 }
75
76 [Test]
77 [TestCase(AudioSourceKind.UsingStream)]
78 [TestCase(AudioSourceKind.UsingFilePath)]
79 public async Task TranslateAudioDeserializesSegment(AudioSourceKind audioSourceKind)
80 {
81 OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """
82 {
83 "segments": [
84 {
85 "id": 15,
86 "seek": 50,
87 "start": 2.5,
88 "end": 7.5,
89 "text": "The quick brown fox got lost.",
90 "tokens": [
91 255, 305, 678
92 ],
93 "temperature": 0.8,
94 "avg_logprob": -0.3,
95 "compression_ratio": 1.5,
96 "no_speech_prob": 0.2
97 }
98 ]
99 }
100 """);
101 AudioTranslation translation = await InvokeTranslateAudioSyncOrAsync(clientOptions, audioSourceKind);
102 TranscribedSegment segment = translation.Segments.Single();
103
104 Assert.That(segment.Id, Is.EqualTo(15));
105 Assert.That(segment.SeekOffset, Is.EqualTo(50));
106 Assert.That(segment.StartTime, Is.EqualTo(TimeSpan.FromSeconds(2.5)));
107 Assert.That(segment.EndTime, Is.EqualTo(TimeSpan.FromSeconds(7.5)));
108 Assert.That(segment.Text, Is.EqualTo("The quick brown fox got lost."));
109 Assert.That(segment.TokenIds.Span.SequenceEqual([255, 305, 678]));
110 Assert.That(segment.Temperature, Is.EqualTo(0.8f));
111 Assert.That(segment.AverageLogProbability, Is.EqualTo(-0.3f));
112 Assert.That(segment.CompressionRatio, Is.EqualTo(1.5f));
113 Assert.That(segment.NoSpeechProbability, Is.EqualTo(0.2f));
114 }
115
116 [Test]
117 public void TranslateAudioFromStreamRespectsTheCancellationToken()
118 {
119 AudioClient client = CreateProxyFromClient(new AudioClient("model", s_fakeCredential));
120 using Stream stream = new MemoryStream();
121 using CancellationTokenSource cancellationSource = new();
122 cancellationSource.Cancel();
123
124 Assert.That(async () => await client.TranslateAudioAsync(stream, "filename", cancellationToken: cancellationSource.Token),
125 Throws.InstanceOf<OperationCanceledException>());
126 }
127
128 private OpenAIClientOptions GetClientOptionsWithMockResponse(int status, string content = null)
129 {
130 MockPipelineResponse response = new MockPipelineResponse(status).WithContent(content ?? "{}");
131
132 return new OpenAIClientOptions()
133 {
134 Transport = new MockPipelineTransport(_ => response)
135 {
136 ExpectSyncPipeline = !IsAsync
137 }
138 };
139 }
140
141 private async ValueTask<AudioTranslation> InvokeTranslateAudioSyncOrAsync(OpenAIClientOptions clientOptions, AudioSourceKind audioSourceKind)
142 {
143 AudioClient client = CreateProxyFromClient(new AudioClient("model", s_fakeCredential, clientOptions));
144 string filename = "audio_french.wav";
145 string path = Path.Combine("Assets", filename);
146
147 if (audioSourceKind == AudioSourceKind.UsingStream)
148 {
149 using FileStream audio = File.OpenRead(path);
150
151 return await client.TranslateAudioAsync(audio, filename);
152 }
153 else if (audioSourceKind == AudioSourceKind.UsingFilePath)
154 {
155 return await client.TranslateAudioAsync(path);
156 }
157
158 Assert.Fail("Invalid source kind.");
159 return null;
160 }
161}