package httpapi import ( "context" "io" "log/slog" "testing" "speech-learning-api/internal/domain" "speech-learning-api/internal/services" ) type pronunciationHistoryStore struct { DataStore user, language, level string fallbackAvoid []string } func (s *pronunciationHistoryStore) RecentPronunciationTexts(_ context.Context, user, language, level string) ([]string, error) { s.user, s.language, s.level = user, language, level return []string{"Saved after reload."}, nil } func (s *pronunciationHistoryStore) NextLessonAndExercise(_ context.Context, _ domain.LearnerProfile, avoid ...string) (domain.Lesson, domain.Exercise, error) { s.fallbackAvoid = avoid return domain.Lesson{}, domain.Exercise{Source: "seed"}, nil } type pronunciationHistoryTeacher struct { services.TeacherAgent request domain.GeneratedExerciseRequest } func (t *pronunciationHistoryTeacher) NextExercise(_ context.Context, request domain.GeneratedExerciseRequest) (domain.GeneratedExercise, error) { t.request = request return domain.GeneratedExercise{}, nil } func TestPronunciationLoadsHistoryWithoutClientIDs(t *testing.T) { for _, client := range [][]string{nil, {"From browser."}} { repo, teacher := &pronunciationHistoryStore{}, &pronunciationHistoryTeacher{} s := &Server{store: repo, teacher: teacher, logger: slog.New(slog.NewTextHandler(io.Discard, nil))} _, _, err := s.nextAgentExercise(context.Background(), domain.User{ID: "learner"}, domain.LearnerProfile{TargetLanguage: "en-US", CEFRLevel: "A1"}, domain.ProgressSummary{}, client) if err != nil { t.Fatal(err) } if repo.user != "learner" || repo.language != "en-US" || repo.level != "A1" { t.Fatal("wrong history scope") } avoid := teacher.request.AvoidReferenceTexts if len(avoid) != len(client)+1 || avoid[len(avoid)-1] != "Saved after reload." { t.Fatalf("history not passed to generator: %v", avoid) } if len(repo.fallbackAvoid) != len(client) { t.Fatal("history exhausted fixed fallback") } } }