Übungssätze mit Azure-Stimme abspielbar machen

This commit is contained in:
Eugenius Naumenko
2026-10-14 01:51:38 +00:00
parent 9e6347400a
commit 16da8cad1d
6 changed files with 194 additions and 4 deletions
@@ -0,0 +1,80 @@
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
}
func (s *audioSpeechService) Synthesize(_ context.Context, locale, voice, text string) ([]byte, string, error) {
s.locale, s.voice, s.text = locale, voice, text
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." {
t.Fatalf("unexpected synthesis input: user %q, locale %q, voice %q, text %q", lookup.requestedUserID, speech.locale, speech.voice, 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)
}
}
+27
View File
@@ -61,6 +61,7 @@ func (s *Server) Routes() http.Handler {
mux.Handle("GET /dashboard", s.requireAuth(http.HandlerFunc(s.dashboard)))
mux.Handle("GET /progress", s.requireAuth(http.HandlerFunc(s.progress)))
mux.Handle("POST /lessons/next", s.requireAuth(http.HandlerFunc(s.nextLesson)))
mux.Handle("GET /practice/exercises/{id}/audio", s.requireAuth(http.HandlerFunc(s.exerciseAudio)))
mux.Handle("POST /practice/attempts", s.requireAuth(http.HandlerFunc(s.createAttempt)))
mux.Handle("POST /conversation/start", s.requireAuth(http.HandlerFunc(s.startConversation)))
mux.Handle("POST /conversation/{id}/turn", s.requireAuth(http.HandlerFunc(s.conversationTurn)))
@@ -299,6 +300,32 @@ func (s *Server) nextAgentExercise(ctx context.Context, user domain.User, profil
return lesson, exercise, err
}
func (s *Server) exerciseAudio(w http.ResponseWriter, r *http.Request) {
exercise, err := s.store.GetExercise(r.Context(), r.PathValue("id"), currentUserID(r))
if err != nil {
writeError(w, http.StatusNotFound, err)
return
}
language, err := s.store.GetLanguage(r.Context(), exercise.TargetLanguage)
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
audio, mime, err := s.speech.Synthesize(r.Context(), language.AzureLocale, language.DefaultTTSVoice, exercise.TargetText)
if err != nil {
writeError(w, http.StatusBadGateway, err)
return
}
if len(audio) == 0 {
writeError(w, http.StatusServiceUnavailable, errors.New("audio is unavailable"))
return
}
w.Header().Set("Content-Type", mime)
w.Header().Set("Cache-Control", "private, no-store")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(audio)
}
func (s *Server) createAttempt(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(24 << 20); err != nil {
writeError(w, http.StatusBadRequest, err)