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