Public Access
112 lines
3.6 KiB
C#
112 lines
3.6 KiB
C#
using MeetingAssistant.Recording;
|
|
using MeetingAssistant.Transcription;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace MeetingAssistant.Tests;
|
|
|
|
public sealed class AzureSpeechStreamingTranscriptionProviderTests
|
|
{
|
|
[Fact]
|
|
public async Task TranscribeFailsClearlyWhenKeyIsMissing()
|
|
{
|
|
var provider = new AzureSpeechStreamingTranscriptionProvider(
|
|
Options.Create(new MeetingAssistantOptions
|
|
{
|
|
AzureSpeech = new AzureSpeechOptions
|
|
{
|
|
Region = "germanywestcentral",
|
|
KeyEnv = $"MEETING_ASSISTANT_MISSING_AZURE_KEY_{Guid.NewGuid():N}"
|
|
}
|
|
}),
|
|
NullLogger<AzureSpeechStreamingTranscriptionProvider>.Instance);
|
|
|
|
var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
|
|
{
|
|
await foreach (var _ in provider.TranscribeAsync(ReadSingleChunk(), SpeechRecognitionPipelineOptions.Default, CancellationToken.None))
|
|
{
|
|
}
|
|
});
|
|
|
|
Assert.Contains("Azure Speech key is not configured", exception.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void AutoDetectLanguagesAreUsedOnlyWhenLanguageIsAuto()
|
|
{
|
|
Assert.Equal(
|
|
["de-DE", "en-US"],
|
|
AzureSpeechStreamingTranscriptionProvider.GetAutoDetectLanguages(new AzureSpeechOptions
|
|
{
|
|
Language = "auto",
|
|
AutoDetectLanguages = ["de-DE", "en-US", "de-DE", " "]
|
|
}));
|
|
|
|
Assert.Empty(AzureSpeechStreamingTranscriptionProvider.GetAutoDetectLanguages(new AzureSpeechOptions
|
|
{
|
|
Language = "de-DE",
|
|
AutoDetectLanguages = ["de-DE", "en-US"]
|
|
}));
|
|
}
|
|
|
|
[Fact]
|
|
public void NormalizeDictationWordsTrimsAndDeduplicatesPhrases()
|
|
{
|
|
Assert.Equal(
|
|
["PBI", "Product Backlog Item"],
|
|
AzureSpeechStreamingTranscriptionProvider.NormalizeDictationWords([
|
|
" PBI ",
|
|
"pbi",
|
|
"",
|
|
"Product Backlog Item"
|
|
]));
|
|
}
|
|
|
|
[Fact]
|
|
public void ResolveKeyFallsBackToUserEnvironment()
|
|
{
|
|
var keyEnv = $"MEETING_ASSISTANT_AZURE_KEY_{Guid.NewGuid():N}";
|
|
try
|
|
{
|
|
Environment.SetEnvironmentVariable(keyEnv, "user-key", EnvironmentVariableTarget.User);
|
|
|
|
Assert.Equal("user-key", AzureSpeechStreamingTranscriptionProvider.ResolveKey(new AzureSpeechOptions
|
|
{
|
|
KeyEnv = keyEnv
|
|
}));
|
|
}
|
|
finally
|
|
{
|
|
Environment.SetEnvironmentVariable(keyEnv, null, EnvironmentVariableTarget.User);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void EndpointDefaultsToUniversalV2SpeechEndpointForRegion()
|
|
{
|
|
Assert.Equal(
|
|
new Uri("wss://germanywestcentral.stt.speech.microsoft.com/speech/universal/v2"),
|
|
AzureSpeechStreamingTranscriptionProvider.GetEndpoint(new AzureSpeechOptions
|
|
{
|
|
Endpoint = "",
|
|
Region = "germanywestcentral"
|
|
}));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(null, "Continuous")]
|
|
[InlineData("", "Continuous")]
|
|
[InlineData("continuous", "Continuous")]
|
|
[InlineData("AtStart", "AtStart")]
|
|
public void LanguageIdModeDefaultsToContinuous(string? configured, string expected)
|
|
{
|
|
Assert.Equal(expected, AzureSpeechStreamingTranscriptionProvider.NormalizeLanguageIdMode(configured));
|
|
}
|
|
|
|
private static async IAsyncEnumerable<AudioChunk> ReadSingleChunk()
|
|
{
|
|
await Task.Yield();
|
|
yield return new AudioChunk(new byte[320], 16000, 1);
|
|
}
|
|
}
|