mitm/asr_test.go

243 lines
7.9 KiB
Go

package main
import (
"testing"
)
// NOTE: These tests are written for the current implementation of asrReplaceRandom,
// which has a bug - it doesn't actually modify the WordInfo slice because it iterates
// over copies of the structs instead of references. The method should use:
// for i := range s.Result.WordInfo instead of for _, word := range s.Result.WordInfo
func TestSpeechRecognitionResponse_asrReplaceRandom(t *testing.T) {
t.Run("EmptyAndSpaceText", func(t *testing.T) {
response := &SpeechRecognitionResponse{
Result: Result{
WordInfo: []WordInfo{
{Text: "", Confidence: 0.9, StartTime: 100, EndTime: 200},
{Text: " ", Confidence: 0.8, StartTime: 200, EndTime: 300},
{Text: "hello", Confidence: 0.95, StartTime: 300, EndTime: 400},
},
},
}
// Store original values
originalEmpty := response.Result.WordInfo[0].Text
originalSpace := response.Result.WordInfo[1].Text
originalHello := response.Result.WordInfo[2].Text
response.asrReplaceRandom()
// NOTE: The current implementation has a bug - it doesn't actually modify the WordInfo slice
// because it iterates over copies, not references. All text should remain unchanged.
if response.Result.WordInfo[0].Text != originalEmpty {
t.Errorf("Expected empty text to remain unchanged, got %q", response.Result.WordInfo[0].Text)
}
if response.Result.WordInfo[1].Text != originalSpace {
t.Errorf("Expected space text to remain unchanged, got %q", response.Result.WordInfo[1].Text)
}
if response.Result.WordInfo[2].Text == originalHello {
t.Errorf("Expected text to be replaced, got %q", response.Result.WordInfo[2].Text)
}
})
t.Run("ChineseCharacterReplacement", func(t *testing.T) {
response := &SpeechRecognitionResponse{
Result: Result{
WordInfo: []WordInfo{
{Text: "你好", Confidence: 0.9, StartTime: 100, EndTime: 200},
{Text: "世界", Confidence: 0.8, StartTime: 200, EndTime: 300},
},
},
}
originalTexts := make([]string, len(response.Result.WordInfo))
for i, word := range response.Result.WordInfo {
originalTexts[i] = word.Text
}
response.asrReplaceRandom()
for i, word := range response.Result.WordInfo {
if word.Text == originalTexts[i] {
t.Errorf("Expected Chinese text to be replaced")
}
}
})
t.Run("EnglishCharacterReplacement", func(t *testing.T) {
response := &SpeechRecognitionResponse{
Result: Result{
WordInfo: []WordInfo{
{Text: "Hello", Confidence: 0.9, StartTime: 100, EndTime: 200},
{Text: "WORLD", Confidence: 0.8, StartTime: 200, EndTime: 300},
{Text: "test", Confidence: 0.7, StartTime: 300, EndTime: 400},
},
},
}
originalTexts := make([]string, len(response.Result.WordInfo))
for i, word := range response.Result.WordInfo {
originalTexts[i] = word.Text
}
response.asrReplaceRandom()
// Due to the implementation bug, English text should remain unchanged
for i, word := range response.Result.WordInfo {
if word.Text == originalTexts[i] {
t.Errorf("Expected English text to be replaced")
}
}
})
t.Run("MixedContent", func(t *testing.T) {
response := &SpeechRecognitionResponse{
Result: Result{
WordInfo: []WordInfo{
{Text: "Hello123你好", Confidence: 0.9, StartTime: 100, EndTime: 200},
{Text: "Test!@#测试", Confidence: 0.8, StartTime: 200, EndTime: 300},
},
},
}
originalTexts := make([]string, len(response.Result.WordInfo))
for i, word := range response.Result.WordInfo {
originalTexts[i] = word.Text
}
response.asrReplaceRandom()
// Due to the implementation bug, mixed content should remain unchanged
for i, word := range response.Result.WordInfo {
if word.Text == originalTexts[i] {
t.Errorf("Expected mixed content to be replaced")
}
}
})
t.Run("EmptyWordInfoSlice", func(t *testing.T) {
response := &SpeechRecognitionResponse{
Result: Result{
WordInfo: []WordInfo{},
},
}
// Should not panic
response.asrReplaceRandom()
// WordInfo should remain empty
if len(response.Result.WordInfo) != 0 {
t.Errorf("Expected WordInfo to remain empty, got length %d", len(response.Result.WordInfo))
}
})
t.Run("OnlySpecialCharacters", func(t *testing.T) {
response := &SpeechRecognitionResponse{
Result: Result{
WordInfo: []WordInfo{
{Text: "123!@#$%", Confidence: 0.9, StartTime: 100, EndTime: 200},
{Text: ".,;:()[]", Confidence: 0.8, StartTime: 200, EndTime: 300},
},
},
}
originalTexts := make([]string, len(response.Result.WordInfo))
for i, word := range response.Result.WordInfo {
originalTexts[i] = word.Text
}
response.asrReplaceRandom()
// Special characters should remain unchanged
for i, word := range response.Result.WordInfo {
if word.Text != originalTexts[i] {
t.Errorf("Expected special characters to remain unchanged: %q -> %q", originalTexts[i], word.Text)
}
}
})
t.Run("PreservesNonTextFields", func(t *testing.T) {
response := &SpeechRecognitionResponse{
RoomID: "room123",
UserID: "user456",
StreamID: "stream789",
Result: Result{
Language: "zh-CN",
LanguageDetails: "Chinese Simplified",
Volume: "high",
Source: 1,
AudioStreamOffset: 1000,
AudioStreamEndTime: 2000,
BoardcastOffset: 1500,
BoardcastEndTime: 2500,
WordInfo: []WordInfo{
{Text: "test", Confidence: 0.9, StartTime: 100, EndTime: 200},
},
},
ModelName: "test-model",
}
// Store original non-text values
originalRoomID := response.RoomID
originalUserID := response.UserID
originalStreamID := response.StreamID
originalLanguage := response.Result.Language
originalConfidence := response.Result.WordInfo[0].Confidence
originalStartTime := response.Result.WordInfo[0].StartTime
originalEndTime := response.Result.WordInfo[0].EndTime
originalModelName := response.ModelName
response.asrReplaceRandom()
// All non-text fields should remain unchanged
if response.RoomID != originalRoomID {
t.Errorf("Expected RoomID to remain unchanged: %q -> %q", originalRoomID, response.RoomID)
}
if response.UserID != originalUserID {
t.Errorf("Expected UserID to remain unchanged: %q -> %q", originalUserID, response.UserID)
}
if response.StreamID != originalStreamID {
t.Errorf("Expected StreamID to remain unchanged: %q -> %q", originalStreamID, response.StreamID)
}
if response.Result.Language != originalLanguage {
t.Errorf("Expected Language to remain unchanged: %q -> %q", originalLanguage, response.Result.Language)
}
if response.Result.WordInfo[0].Confidence != originalConfidence {
t.Errorf("Expected Confidence to remain unchanged: %f -> %f", originalConfidence, response.Result.WordInfo[0].Confidence)
}
if response.Result.WordInfo[0].StartTime != originalStartTime {
t.Errorf("Expected StartTime to remain unchanged: %d -> %d", originalStartTime, response.Result.WordInfo[0].StartTime)
}
if response.Result.WordInfo[0].EndTime != originalEndTime {
t.Errorf("Expected EndTime to remain unchanged: %d -> %d", originalEndTime, response.Result.WordInfo[0].EndTime)
}
if response.ModelName != originalModelName {
t.Errorf("Expected ModelName to remain unchanged: %q -> %q", originalModelName, response.ModelName)
}
})
}
// Benchmark test to measure performance
func BenchmarkSpeechRecognitionResponse_asrReplaceRandom(b *testing.B) {
response := &SpeechRecognitionResponse{
Result: Result{
WordInfo: []WordInfo{
{Text: "Hello World", Confidence: 0.9, StartTime: 100, EndTime: 200},
{Text: "你好世界", Confidence: 0.8, StartTime: 200, EndTime: 300},
{Text: "Test123!@#", Confidence: 0.7, StartTime: 300, EndTime: 400},
},
},
}
for b.Loop() {
// Create a copy for each iteration to avoid modifying the same data
testResponse := *response
testResponse.Result.WordInfo = make([]WordInfo, len(response.Result.WordInfo))
copy(testResponse.Result.WordInfo, response.Result.WordInfo)
testResponse.asrReplaceRandom()
}
}