package pronunciation import ( "fmt" "math" "speech-learning-api/internal/domain" "testing" "time" ) var testNow = time.Date(2026, 10, 29, 12, 0, 0, 0, time.UTC) func evidence(id, task, word, symbol string, score float64, at time.Time) []domain.PronunciationObservation { return Observe(id, task, "en-US", at, domain.PronunciationResult{Provider: "azure", Locale: "en-US", Alphabet: "IPA", WordScores: []domain.WordScore{{Word: word, Accuracy: score, PhonemeScores: []domain.PhonemeScore{{Phoneme: symbol, Accuracy: score}}}}}) } func sound(t *testing.T, p domain.PronunciationProfile, symbol string) domain.PronunciationTarget { t.Helper() for _, x := range p.Targets { if x.Kind == "phoneme" && x.Symbol == symbol { return x } } t.Fatal("sound missing", symbol) return domain.PronunciationTarget{} } func TestProfileBundlesDifferentWordsAndSeparatesLanguages(t *testing.T) { obs := []domain.PronunciationObservation{} for i, w := range []string{"cat", "cup", "school"} { obs = append(obs, evidence(fmt.Sprint(i), fmt.Sprint(i), w, "k", float64(30+i*10), testNow.Add(-time.Duration(i)*time.Hour))...) } // Neither another locale nor a similar IPA symbol can change /k/. x := evidence("foreign", "foreign", "Katze", "k", 99, testNow) for i := range x { x[i].Language = "de-DE" x[i].Locale = "de-DE" } obs = append(obs, x...) obs = append(obs, evidence("other", "other", "thing", "θ", 99, testNow)...) p := Build("en-US", testNow, obs) k := sound(t, p, "k") if k.Words != 3 || k.Samples != 3 || k.State != "needs_practice" || math.Abs(k.Mean-40) > .01 { t.Fatalf("wrong grouping: %+v", k) } } func TestProfileCapsSameTaskDayAndRepeatedSound(t *testing.T) { obs := evidence("old", "task", "cook", "k", 1, testNow.Add(-time.Hour)) r := domain.PronunciationResult{Locale: "en-US", Alphabet: "IPA", WordScores: []domain.WordScore{{Word: "cook", Accuracy: 70, PhonemeScores: []domain.PhonemeScore{{Phoneme: "k", Accuracy: 20}, {Phoneme: "k", Accuracy: 80}}}}} obs = append(obs, Observe("latest", "task", "en-US", testNow, r)...) k := sound(t, Build("en-US", testNow, obs), "k") if k.Samples != 1 || k.Mean != 50 || k.State != "limited" { t.Fatalf("repeat inflated or worst-score used: %+v", k) } } func TestProfileBalancesWordsAndWeightsNewEvidence(t *testing.T) { obs := []domain.PronunciationObservation{} for i := 0; i < 10; i++ { obs = append(obs, evidence(fmt.Sprint(i), fmt.Sprint(i), "cat", "k", 100, testNow.Add(-time.Duration(i)*time.Hour))...) } obs = append(obs, evidence("cup", "cup", "cup", "k", 0, testNow)...) k := sound(t, Build("en-US", testNow, obs), "k") if math.Abs(k.Mean-50) > .00001 { t.Fatalf("frequent word drowned other context: %f", k.Mean) } obs = append(evidence("older", "older", "cat", "k", 0, testNow.AddDate(0, 0, -14)), evidence("newer", "newer", "cat", "k", 100, testNow)...) k = sound(t, Build("en-US", testNow, obs), "k") if math.Abs(k.Mean-100.0/1.5) > .01 { t.Fatalf("wrong decay: %f", k.Mean) } } func TestProfileSpacedReviewAndRelapse(t *testing.T) { obs := []domain.PronunciationObservation{} start := testNow.AddDate(0, 0, -30) for i, w := range []string{"cat", "cup", "cat"} { obs = append(obs, evidence(fmt.Sprint(i), fmt.Sprint(i), w, "k", 90, start.Add(time.Duration(i)*time.Hour))...) } p := Build("en-US", start.Add(2*time.Hour), obs) k := sound(t, p, "k") if k.State != "improved" || k.NextReview == nil || !k.NextReview.Equal(start.Add(26*time.Hour)) { t.Fatalf("improvement schedule: %+v", k) } // 1,3,7,14 day review intervals, each anchored to the actual measurement. due := *k.NextReview for i, days := range []int{3, 7, 14, 30} { obs = append(obs, evidence(fmt.Sprint("review", i), fmt.Sprint("review", i), "cup", "k", 95, due)...) k = sound(t, Build("en-US", due, obs), "k") if k.NextReview == nil || !k.NextReview.Equal(due.AddDate(0, 0, days)) { t.Fatalf("review %d: %+v", i, k) } due = *k.NextReview } if k.State != "stable" { t.Fatal("stable state not reached", k.State) } obs = append(obs, evidence("relapse", "relapse", "cup", "k", 40, due.Add(-time.Hour))...) k = sound(t, Build("en-US", due, obs), "k") if k.State == "stable" || k.NextReview != nil { t.Fatalf("relapse ignored: %+v", k) } } func TestProfileDoesNotInventPhonemesOrErasePatternWithOneSuccess(t *testing.T) { no := false r := domain.PronunciationResult{Locale: "en-US", Alphabet: "IPA", WordScores: []domain.WordScore{ {Word: "missing", Accuracy: 0, AccuracyMeasured: &no}, {Word: "omitted", Accuracy: 0, ErrorType: "Omission", PhonemeScores: []domain.PhonemeScore{{Phoneme: "x", Accuracy: 0}}}, {Word: "invalid", Accuracy: math.NaN()}, }} p := Build("en-US", testNow, Observe("a", "a", "en-US", testNow, r)) if len(p.Targets) != 1 || p.Targets[0].Kind != "omission" { t.Fatalf("bad evidence included: %+v", p.Targets) } obs := append(evidence("a", "a", "cat", "k", 20, testNow.Add(-3*time.Hour)), evidence("b", "b", "cup", "k", 25, testNow.Add(-2*time.Hour))...) obs = append(obs, evidence("c", "c", "cat", "k", 95, testNow)...) if k := sound(t, Build("en-US", testNow, obs), "k"); k.State != "needs_practice" { t.Fatalf("single success erased pattern: %+v", k) } } func TestProfileWindowAndTrend(t *testing.T) { obs := evidence("ancient", "ancient", "cat", "k", 0, testNow.AddDate(0, 0, -91)) for i := 0; i < 35; i++ { v := 40.0 if i >= 30 { v = 95 } obs = append(obs, evidence(fmt.Sprint(i), fmt.Sprint(i), "cat", "k", v, testNow.Add(time.Duration(i-35)*time.Hour))...) } k := sound(t, Build("en-US", testNow, obs), "k") if k.Samples != 30 || k.Trend != "improving" { t.Fatalf("wrong window/trend: %+v", k) } } func TestSelectionMixTransferAndNonRepetition(t *testing.T) { due := testNow.Add(-time.Hour) p := domain.PronunciationProfile{Algorithm: domain.PronunciationAlgorithm, Language: "en-US", Targets: []domain.PronunciationTarget{ {Key: "weak", Kind: "phoneme", Symbol: "k", Samples: 5, Words: 3, State: "needs_practice", Mean: 40, Priority: .9, Examples: []domain.PronunciationExample{{Word: "cat", Mean: 40}}}, {Key: "review", Kind: "phoneme", Symbol: "s", Samples: 5, Words: 3, State: "improved", Mean: 90, Priority: .1, ReviewDue: true, NextReview: &due, Examples: []domain.PronunciationExample{{Word: "sits", Mean: 90}}}, }} history := []domain.PronunciationPlan{} counts := map[string]int{} for i := 0; i < 20; i++ { h := Select(p, history, testNow) counts[h.Category]++ history = append([]domain.PronunciationPlan{h}, history...) } if counts["targeted"] != 12 || counts["review"] != 5 || counts["explore"] != 3 { t.Fatalf("wrong mix: %v", counts) } transfer := Select(p, []domain.PronunciationPlan{{Category: "targeted"}, {Category: "targeted"}, {Category: "targeted"}, {Category: "targeted"}, {Category: "review"}}, testNow) if transfer.Mode != "transfer" || len(transfer.RequiredWords) != 0 || transfer.TransferStatus != "unconfirmed" { t.Fatalf("no transfer: %+v", transfer) } h := domain.PronunciationPlan{Target: &p.Targets[0], Category: "targeted"} next := Select(p, []domain.PronunciationPlan{h, h}, testNow) if next.Target != nil && next.Target.Key == "weak" { t.Fatal("same sound three times") } } func TestSuccessfulWordsResolveOmissionsWithoutInventingPhonemes(t *testing.T) { absent := domain.PronunciationResult{Locale: "en-US", Alphabet: "IPA", WordScores: []domain.WordScore{{Word: "cat", ErrorType: "Omission", Accuracy: 0}}} obs := Observe("old", "task", "en-US", testNow.Add(-time.Hour), absent) obs = append(obs, evidence("new", "task", "cat", "k", 95, testNow)...) p := Build("en-US", testNow, obs) for _, target := range p.Targets { if target.Kind == "omission" { t.Fatal("same-day correction left perpetual omission", target) } } }