package store import ( "speech-learning-api/internal/domain" "testing" "time" ) func TestFoundationRepetitionPolicy(t *testing.T) { now := time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC) step := domain.LearningSequenceStep{Phase: "grammar", BankEntryID: "base", Alternatives: []string{"other", "third"}} candidates := []bankCandidate{{bankEntryID: "base"}, {bankEntryID: "other"}, {bankEntryID: "third"}} attempt := func(id, status string, score float64, age time.Duration) foundationAttempt { return foundationAttempt{BankID: id, Status: status, Score: score, At: now.Add(-age)} } history := func(attempts ...foundationAttempt) foundationHistory { h := foundationHistory{Attempts: map[string][]foundationAttempt{}, Assigned: map[string]bool{}} for _, a := range attempts { h.Attempts[a.BankID] = append(h.Attempts[a.BankID], a) h.Assigned[a.BankID] = true } return h } t.Run("new pattern starts with its own foundation", func(t *testing.T) { h := history(attempt("other", "graded", 100, time.Minute)) n, reuse, _ := chooseFoundation(step, candidates, h, now) if n != 0 || reuse { t.Fatal(n, reuse) } }) t.Run("a new approved example checks transfer", func(t *testing.T) { h := history(attempt("base", "graded", 100, time.Hour)) n, reuse, reason := chooseFoundation(step, candidates, h, now) if n != 1 || reuse || reason != "transfer" { t.Fatal(n, reuse, reason) } }) t.Run("assigned exam or locale variant is not new", func(t *testing.T) { h := history(attempt("base", "graded", 100, time.Hour)) h.Assigned["other"] = true n, _, _ := chooseFoundation(step, candidates, h, now) if n != 2 { t.Fatal(n) } }) t.Run("a newer mistake defeats older good scores", func(t *testing.T) { h := history(attempt("base", "graded", 0, time.Minute), attempt("base", "graded", 100, 48*time.Hour)) n, reuse, _ := chooseFoundation(step, candidates, h, now) if n != 0 || reuse { t.Fatal(n, reuse) } }) t.Run("ungraded is not successful evidence", func(t *testing.T) { h := history(attempt("base", "ungraded", 100, time.Minute), attempt("base", "graded", 100, time.Hour)) n, reuse, _ := chooseFoundation(step, candidates, h, now) if n != 0 || reuse { t.Fatal(n, reuse) } }) t.Run("old practice is refreshed", func(t *testing.T) { h := history(attempt("base", "graded", 100, 8*24*time.Hour)) n, reuse, reason := chooseFoundation(step, candidates, h, now) if n != 0 || reuse || reason != "refresh" { t.Fatal(n, reuse, reason) } }) t.Run("immediate duplicate may be omitted without a mastery claim", func(t *testing.T) { h := history(attempt("base", "graded", 100, 10*time.Minute)) h.Assigned["other"] = true h.Assigned["third"] = true _, reuse, reason := chooseFoundation(step, candidates, h, now) if !reuse || reason != "recent_practice" { t.Fatal(reuse, reason) } }) t.Run("same-day repetitions cannot manufacture evidence density", func(t *testing.T) { h := history(attempt("base", "graded", 100, time.Hour), attempt("base", "graded", 100, 2*time.Hour), attempt("base", "graded", 100, 3*time.Hour)) h.Assigned["other"] = true h.Assigned["third"] = true if h.reusable(step, now) { t.Fatal("repetition count treated as independent evidence") } }) t.Run("spaced positives reduce repetitions", func(t *testing.T) { h := history(attempt("base", "graded", 100, time.Hour), attempt("base", "graded", 100, 48*time.Hour)) if !h.reusable(step, now) { t.Fatal("spaced practice ignored") } }) t.Run("different pattern examples support fewer repetitions", func(t *testing.T) { h := history(attempt("base", "graded", 100, time.Hour), attempt("other", "graded", 100, 2*time.Hour), attempt("third", "graded", 100, 3*time.Hour)) if !h.reusable(step, now) { t.Fatal("transfer evidence ignored") } }) t.Run("unrelated words never replace a meaning", func(t *testing.T) { word := domain.LearningSequenceStep{Phase: "vocabulary", BankEntryID: "base"} h := history(attempt("other", "graded", 100, time.Hour)) n, reuse, _ := chooseFoundation(word, candidates, h, now) if n != 0 || reuse { t.Fatal(n, reuse) } }) for _, phase := range []string{"listen", "read", "repeat", "write", "compose", "speak"} { t.Run("thematic context preserved/"+phase, func(t *testing.T) { s := step s.Phase = phase h := history(attempt("base", "graded", 100, time.Minute)) n, reuse, _ := chooseFoundation(s, candidates, h, now) if n != 0 || reuse { t.Fatal("production/context was bypassed") } }) } }