package httpapi import ( "context" "net/http" "net/http/httptest" "testing" "speech-learning-api/internal/domain" "speech-learning-api/internal/services" "speech-learning-api/internal/store" ) type audioExerciseStore struct { DataStore requestedUserID string lookupErr error } func (s *audioExerciseStore) GetExercise(_ context.Context, _, userID string) (domain.Exercise, error) { s.requestedUserID = userID if s.lookupErr != nil { return domain.Exercise{}, s.lookupErr } return domain.Exercise{ID: "ex_1", TargetLanguage: "en-US", TargetText: "Please pass the water."}, nil } func (s *audioExerciseStore) GetLanguage(_ context.Context, _ string) (domain.LanguageProfile, error) { return domain.LanguageProfile{AzureLocale: "en-US", DefaultTTSVoice: "en-US-JennyNeural"}, nil } type audioSpeechService struct { services.SpeechService locale string voice string text string mode string word string } func (s *audioSpeechService) SynthesizeExercise(_ context.Context, locale, voice, text, mode, word string) ([]byte, string, error) { s.locale, s.voice, s.text = locale, voice, text s.mode, s.word = mode, word return []byte("mp3-data"), "audio/mpeg", nil } func TestExerciseAudioUsesStoredSentenceAndLanguageVoice(t *testing.T) { lookup := &audioExerciseStore{} speech := &audioSpeechService{} server := &Server{store: lookup, speech: speech} req := httptest.NewRequest(http.MethodGet, "/practice/exercises/ex_1/audio", nil) req.SetPathValue("id", "ex_1") req = req.WithContext(context.WithValue(req.Context(), userIDKey, "usr_current")) recorder := httptest.NewRecorder() server.exerciseAudio(recorder, req) if recorder.Code != http.StatusOK || recorder.Body.String() != "mp3-data" { t.Fatalf("status = %d, body = %q", recorder.Code, recorder.Body.String()) } if recorder.Header().Get("Content-Type") != "audio/mpeg" { t.Fatalf("content type = %q", recorder.Header().Get("Content-Type")) } if lookup.requestedUserID != "usr_current" || speech.locale != "en-US" || speech.voice != "en-US-JennyNeural" || speech.text != "Please pass the water." || speech.mode != "normal" { t.Fatalf("unexpected synthesis input: user %q, locale %q, voice %q, text %q", lookup.requestedUserID, speech.locale, speech.voice, speech.text) } } func TestExerciseAudioSlowAndFocusModes(t *testing.T) { for _, test := range []struct { name string url string mode string word string }{ {"slow", "/practice/exercises/ex_1/audio?mode=slow", "slow", ""}, {"focus", "/practice/exercises/ex_1/audio?mode=focus&word=water", "focus", "water"}, {"focus punctuation", "/practice/exercises/ex_1/audio?mode=focus&word=water.", "focus", "water"}, } { t.Run(test.name, func(t *testing.T) { speech := &audioSpeechService{} server := &Server{store: &audioExerciseStore{}, speech: speech} req := httptest.NewRequest(http.MethodGet, test.url, nil) req.SetPathValue("id", "ex_1") req = req.WithContext(context.WithValue(req.Context(), userIDKey, "usr_current")) recorder := httptest.NewRecorder() server.exerciseAudio(recorder, req) if recorder.Code != http.StatusOK || speech.mode != test.mode || speech.word != test.word { t.Fatalf("status = %d, mode = %q, word = %q", recorder.Code, speech.mode, speech.word) } }) } } func TestExerciseAudioRejectsWordsOutsideSentence(t *testing.T) { speech := &audioSpeechService{} server := &Server{store: &audioExerciseStore{}, speech: speech} req := httptest.NewRequest(http.MethodGet, "/practice/exercises/ex_1/audio?mode=focus&word=unrelated", nil) req.SetPathValue("id", "ex_1") req = req.WithContext(context.WithValue(req.Context(), userIDKey, "usr_current")) recorder := httptest.NewRecorder() server.exerciseAudio(recorder, req) if recorder.Code != http.StatusBadRequest || speech.text != "" { t.Fatalf("status = %d, synthesis text = %q", recorder.Code, speech.text) } } func TestExerciseAudioRejectsInaccessibleExercise(t *testing.T) { lookup := &audioExerciseStore{lookupErr: store.ErrNotFound} speech := &audioSpeechService{} server := &Server{store: lookup, speech: speech} req := httptest.NewRequest(http.MethodGet, "/practice/exercises/ex_other/audio", nil) req.SetPathValue("id", "ex_other") req = req.WithContext(context.WithValue(req.Context(), userIDKey, "usr_current")) recorder := httptest.NewRecorder() server.exerciseAudio(recorder, req) if recorder.Code != http.StatusNotFound || lookup.requestedUserID != "usr_current" || speech.text != "" { t.Fatalf("status = %d, synthesis text = %q", recorder.Code, speech.text) } }