package services import ( "context" "encoding/json" "fmt" "net/http" "net/http/httptest" "regexp" "strings" "testing" "speech-learning-api/internal/config" "speech-learning-api/internal/domain" ) func pronunciationFixture() domain.GeneratedExercise { return domain.GeneratedExercise{ExerciseType: "repeat_sentence", TargetLanguage: "en-US", CEFRLevel: "A1", ReferenceText: "I eat an apple.", TTSText: "I eat an apple.", DisplayPrompt: "Sprich den Satz nach.", FocusPoints: []string{"apple"}, SuccessCriteria: []string{"Sprich alle Wörter deutlich."}, ReasonForChoice: "Allgemeine A1-Übung ohne belegte Schwächen."} } func TestPronunciationSchemaControlCharacterPattern(t *testing.T) { a := NewAIService(nil, config.Config{}) var schema struct { Properties map[string]struct { Pattern string `json:"pattern"` } `json:"properties"` } if err := json.Unmarshal([]byte(a.Schemas["next_exercise"]), &schema); err != nil { t.Fatal(err) } pattern, err := regexp.Compile(schema.Properties["reason_for_choice"].Pattern) if err != nil { t.Fatal(err) } for _, text := range []string{"Kurze A1-Übung.", "cat /k/", "这是猫。"} { if !pattern.MatchString(text) { t.Fatalf("valid text rejected: %q", text) } } for _, text := range []string{"", "Zeile\nZeile", "Tab\tWort", "Wort\x7f"} { if pattern.MatchString(text) { t.Fatalf("invalid text accepted: %q", text) } } } func TestPronunciationPostgresCorrectsSubstitutedTrainingWord(t *testing.T) { for _, corrected := range []bool{false, true} { t.Run(fmt.Sprint(corrected), func(t *testing.T) { a, _, user := setupAI(t) ctx := context.Background() if _, err := a.Repo.SaveProfile(ctx, user, aiTestProfile("next_exercise")); err != nil { t.Fatal(err) } if err := a.Repo.SaveBudget(ctx, user, 10000000, 0); err != nil { t.Fatal(err) } calls := 0 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { calls++ var body struct { Messages []struct { Content string `json:"content"` } `json:"messages"` } json.NewDecoder(r.Body).Decode(&body) var message struct { Input struct { Words []string `json:"required_practice_words"` Error string `json:"technical_error"` } `json:"input"` } json.Unmarshal([]byte(body.Messages[1].Content), &message) if strings.Join(message.Input.Words, ",") != "cat" { t.Error("training target not sent") } if calls == 2 && !strings.Contains(message.Input.Error, `required practice word "cat"`) { t.Error("missing focused correction", message.Input.Error) } ex := pronunciationFixture() ex.ReferenceText = "The dog sits on the rug." ex.FocusPoints = []string{"dog", "sits"} if calls == 2 && corrected { ex.ReferenceText = "My cat is very small." ex.FocusPoints = []string{"cat"} } ex.TTSText = ex.ReferenceText json.NewEncoder(w).Encode(aiCompletion(ex, true)) })) defer server.Close() a.URLs["mistral"] = server.URL ex, err := a.NextExercise(ctx, domain.GeneratedExerciseRequest{UserID: user, TargetLanguage: "en-US", CEFRLevel: "A1", Progress: domain.ProgressSummary{RecentAttempts: []domain.Attempt{{TargetLanguage: "en-US", WordScores: []domain.WordScore{{Word: "cat", Accuracy: 29}}}}}}) if calls != 2 { t.Fatalf("retry count %d", calls) } if corrected { if err != nil || ex.Source != "ai" { t.Fatal("corrected exercise rejected", err) } } else if err == nil || !strings.Contains(err.Error(), `required practice word "cat"`) { t.Fatal("wrong training accepted", err) } }) } } func TestPronunciationReasonValidationExplainsExactViolation(t *testing.T) { input := domain.GeneratedExerciseRequest{TargetLanguage: "en-US", CEFRLevel: "A1"} for _, tc := range []struct{ reason, want string }{ {"", "reason_for_choice is required"}, {strings.Repeat("ü", 301), "reason_for_choice exceeds maximum length of 300 characters"}, {"Ein Satz.\nNoch ein Satz.", "reason_for_choice must be a single line"}, } { ex := pronunciationFixture() ex.ReasonForChoice = tc.reason if got := generatedExerciseValidationError(ex, input); !strings.Contains(got, tc.want) { t.Fatalf("wrong diagnosis: %s", got) } } ex := pronunciationFixture() ex.ReasonForChoice = strings.Repeat("ü", 300) if got := generatedExerciseValidationError(ex, input); got != "" { t.Fatal("valid Unicode boundary rejected", got) } } func TestPronunciationPostgresRepairsExactFieldContract(t *testing.T) { for _, nativeSchema := range []bool{false, true} { t.Run(map[bool]string{false: "json_prompt", true: "native_schema"}[nativeSchema], func(t *testing.T) { a, _, user := setupAI(t) ctx := context.Background() p := aiTestProfile("next_exercise") p.JSONSchema = nativeSchema if _, err := a.Repo.SaveProfile(ctx, user, p); err != nil { t.Fatal(err) } if err := a.Repo.SaveBudget(ctx, user, 10000000, 0); err != nil { t.Fatal(err) } calls := 0 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { calls++ var body struct { Messages []struct { Content string `json:"content"` } `json:"messages"` Format map[string]any `json:"response_format"` } if err := json.NewDecoder(r.Body).Decode(&body); err != nil { t.Error(err) } if nativeSchema { if body.Format["type"] != "json_schema" { t.Error("native contract not sent") } } else if !strings.Contains(body.Messages[0].Content, `"maxLength": 300`) { t.Error("field limits absent from prompt") } if calls == 2 && !strings.Contains(body.Messages[1].Content, "reason_for_choice exceeds maximum length of 300 characters") { t.Error("retry lost exact correction") } ex := pronunciationFixture() if calls == 1 { ex.ReasonForChoice = strings.Repeat("x", 301) } json.NewEncoder(w).Encode(aiCompletion(ex, true)) })) defer server.Close() a.URLs["mistral"] = server.URL ex, err := a.NextExercise(ctx, domain.GeneratedExerciseRequest{UserID: user, TargetLanguage: "en-US", CEFRLevel: "A1", UILanguage: "de-DE"}) if err != nil || ex.Source != "ai" || calls != 2 { t.Fatalf("correction failed: %+v %v calls=%d", ex, err, calls) } usage, err := a.Repo.Usage(ctx, domain.AIUsageFilter{Action: "next_exercise"}) if err != nil || usage.Total != 2 { t.Fatalf("retry accounting lost: %v %v", usage, err) } }) } }