Adaptiven A1-Kurs und statistisches Aussprachetraining integrieren
Direkte DeepSeek- und Mistral-Anbindung mit versionierten Aktionsprofilen, Budgetreservierung, Tokenabrechnung und administrativen Qualitätsprüfungen ersetzen die Flowise-Abhängigkeit im regulären Betrieb. 840 geprüfte A1-Aufgaben mit geschützter Testauswertung und konkreten offenen Arbeitsaufträgen ergänzen den Kurs. Inhalts- und Sprachbewertung bleiben von Azure-Aussprachewerten getrennt; historische Aufgaben und Antworten bleiben erhalten. Echte Wort- und Phonemmessungen werden mit Herkunft, Aktualität und Nachweisdichte gebündelt. Go bestimmt Trainingsziele und Wiederholungen, das Modell formuliert dazu passende Nachsprechübungen. Profilansicht und Auswahlgründe werden angezeigt. Validierung: Go-Race-Tests mit lokaler PostgreSQL-Integration, go vet, Web-Build, Audit aller 840 Aufgaben und drei Browser-Smokes mit lokalen Modell-/Speech-Fixtures bestanden. Keine Produktionsänderungen oder Live-Aufrufe.
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"speech-learning-api/internal/domain"
|
||||
"speech-learning-api/internal/services"
|
||||
)
|
||||
|
||||
type courseTestStore struct {
|
||||
DataStore
|
||||
savedAnswer string
|
||||
savedUser string
|
||||
savedOK bool
|
||||
key domain.CourseTaskKey
|
||||
}
|
||||
|
||||
func (s *courseTestStore) GetPrimaryProfile(context.Context, string) (domain.LearnerProfile, error) {
|
||||
return domain.LearnerProfile{TargetLanguage: "en-US", CEFRLevel: "A1"}, nil
|
||||
}
|
||||
func (s *courseTestStore) CourseTaskKey(_ context.Context, _, _, _, _ string) (domain.CourseTaskKey, error) {
|
||||
return s.key, nil
|
||||
}
|
||||
func (s *courseTestStore) SaveCourseAttempt(_ context.Context, userID, _, answer string, correct bool) (string, error) {
|
||||
s.savedUser, s.savedAnswer, s.savedOK = userID, answer, correct
|
||||
return "cat_1", nil
|
||||
}
|
||||
func (s *courseTestStore) CourseModule(context.Context, string, string, string, string) (domain.CourseModuleDetail, error) {
|
||||
return domain.CourseModuleDetail{Module: domain.CourseModule{ID: "a1_introductions_v1"}, Tasks: []domain.CourseTask{{ID: "task_1", Kind: "choice", Prompt: "Who?", Options: []string{"Anna", "Ben"}}}}, nil
|
||||
}
|
||||
func (s *courseTestStore) GetLanguage(context.Context, string) (domain.LanguageProfile, error) {
|
||||
return domain.LanguageProfile{AzureLocale: "en-US", DefaultTTSVoice: "en-US-JennyNeural"}, nil
|
||||
}
|
||||
|
||||
type courseTestSpeech struct {
|
||||
services.SpeechService
|
||||
text string
|
||||
}
|
||||
|
||||
func (s *courseTestSpeech) Synthesize(_ context.Context, _, _, text string) ([]byte, string, error) {
|
||||
s.text = text
|
||||
return []byte("mp3"), "audio/mpeg", nil
|
||||
}
|
||||
|
||||
func courseRequest(method, path, body string) *http.Request {
|
||||
req := httptest.NewRequest(method, path, strings.NewReader(body))
|
||||
req.SetPathValue("id", "task_1")
|
||||
return req.WithContext(context.WithValue(req.Context(), userIDKey, "usr_1"))
|
||||
}
|
||||
|
||||
func TestCourseModuleDoesNotExposeAnswerOrListeningTranscript(t *testing.T) {
|
||||
s := &Server{store: &courseTestStore{}}
|
||||
w := httptest.NewRecorder()
|
||||
s.courseModule(w, courseRequest(http.MethodGet, "/course/modules/a1_introductions_v1", ""))
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if bytes.Contains(w.Body.Bytes(), []byte("accepted_answers")) || bytes.Contains(w.Body.Bytes(), []byte("solution")) || bytes.Contains(w.Body.Bytes(), []byte("audio_text")) {
|
||||
t.Fatalf("course detail leaked hidden answers: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCourseAttemptStoresBothWrongAndNormalizedCorrectAnswer(t *testing.T) {
|
||||
data := &courseTestStore{key: domain.CourseTaskKey{ID: "task_1", Kind: "short_text", AcceptedAnswers: []string{"My name is Anna"}, Solution: "My name is Anna.", Explanation: "A1 sentence"}}
|
||||
s := &Server{store: data}
|
||||
for _, test := range []struct {
|
||||
answer string
|
||||
correct bool
|
||||
}{{"Wrong", false}, {" MY NAME IS ANNA. ", true}} {
|
||||
w := httptest.NewRecorder()
|
||||
s.courseAttempt(w, courseRequest(http.MethodPost, "/course/tasks/task_1/attempts", `{"answer":`+jsonString(test.answer)+`}`))
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("status %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if data.savedUser != "usr_1" || data.savedOK != test.correct {
|
||||
t.Fatalf("saved wrong result: %#v", data)
|
||||
}
|
||||
var result domain.CourseAttemptResult
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Correct != test.correct || result.Solution == "" {
|
||||
t.Fatalf("wrong response: %#v", result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func jsonString(value string) string { encoded, _ := json.Marshal(value); return string(encoded) }
|
||||
|
||||
func TestCourseAudioOnlyUsesListeningText(t *testing.T) {
|
||||
data := &courseTestStore{key: domain.CourseTaskKey{ID: "task_1", Kind: "choice", Skill: "listening", AudioText: "My name is Anna."}}
|
||||
speech := &courseTestSpeech{}
|
||||
s := &Server{store: data, speech: speech}
|
||||
w := httptest.NewRecorder()
|
||||
s.courseAudio(w, courseRequest(http.MethodGet, "/course/tasks/task_1/audio", ""))
|
||||
if w.Code != http.StatusOK || speech.text != "My name is Anna." || w.Header().Get("Content-Type") != "audio/mpeg" {
|
||||
t.Fatalf("unexpected audio result: %d %q", w.Code, speech.text)
|
||||
}
|
||||
data.key.Skill = "reading"
|
||||
w = httptest.NewRecorder()
|
||||
s.courseAudio(w, courseRequest(http.MethodGet, "/course/tasks/task_1/audio", ""))
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Fatalf("non-listening audio status %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListeningTranscriptIsReturnedAfterSubmission(t *testing.T) {
|
||||
data := &courseTestStore{key: domain.CourseTaskKey{ID: "task_1", Kind: "choice", Skill: "listening", AudioText: "My name is Anna.", AcceptedAnswers: []string{"Anna"}, Solution: "Anna"}}
|
||||
s := &Server{store: data}
|
||||
w := httptest.NewRecorder()
|
||||
s.courseAttempt(w, courseRequest(http.MethodPost, "/course/tasks/task_1/attempts", `{"answer":"Ben"}`))
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("status %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
var result domain.CourseAttemptResult
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Transcript != "My name is Anna." || result.Correct {
|
||||
t.Fatalf("unexpected feedback: %#v", result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user