package services import ( "context" "encoding/json" "io" "log/slog" "net/http" "net/http/httptest" "strings" "testing" "speech-learning-api/internal/config" "speech-learning-api/internal/domain" ) func courseFixture() (domain.GenerateCourseTaskRequest, domain.GeneratedCourseTask) { goal := domain.IntroductionGoals()[2] in := domain.GenerateCourseTaskRequest{Task: "generate_course_task", SchemaVersion: 1, TargetLanguage: "en-US", CEFRLevel: "A1", UILanguage: "de-DE", ModuleID: "a1_introductions_v1", Skill: "grammar", Kind: "choice", LearningGoal: goal, AvoidPrompts: []string{}} out := domain.GeneratedCourseTask{SchemaVersion: 1, TargetLanguage: in.TargetLanguage, CEFRLevel: in.CEFRLevel, ModuleID: in.ModuleID, LearningGoalID: goal.ID, Skill: in.Skill, Kind: in.Kind, Prompt: "Ergänze: I ___ from Germany.", Options: []string{"am", "is", "are"}, Solution: "am", Explanation: "Nach I steht am."} return in, out } func TestCourseValidationRejectsWrongGoalAndAmbiguousAnswers(t *testing.T) { in, valid := courseFixture() tests := []struct { name string modify func(*domain.GeneratedCourseTask) }{ {"unreferenced solution", func(o *domain.GeneratedCourseTask) { o.Solution = "unknown" }}, {"duplicate options", func(o *domain.GeneratedCourseTask) { o.Options = []string{"am", "am", "are"} }}, {"wrong module", func(o *domain.GeneratedCourseTask) { o.ModuleID = "another" }}, {"wrong skill", func(o *domain.GeneratedCourseTask) { o.Skill = "writing" }}, {"missing explanation", func(o *domain.GeneratedCourseTask) { o.Explanation = "" }}, {"control character", func(o *domain.GeneratedCourseTask) { o.Explanation = "line\nline" }}, {"unsupported goal", func(o *domain.GeneratedCourseTask) { o.LearningGoalID = "invented" }}, } if err := ValidateGeneratedCourseTask(valid, in); err != nil { t.Fatal(err) } for _, test := range tests { t.Run(test.name, func(t *testing.T) { out := valid test.modify(&out) if ValidateGeneratedCourseTask(out, in) == nil { t.Fatal("accepted invalid task") } }) } in.AvoidPrompts = []string{" ERGÄNZE: I ___ from Germany. "} if ValidateGeneratedCourseTask(valid, in) == nil { t.Fatal("accepted repeated prompt") } } func TestCourseVocabularyChecksWordInsteadOfPhrase(t *testing.T) { in, out := courseFixture() in.LearningGoal = domain.IntroductionGoals()[1] in.Skill = "vocabulary" out.Skill = in.Skill out.LearningGoalID = in.LearningGoal.ID out.Prompt = "Was bedeutet „from“ in „I am from Germany“?" out.Options = []string{"aus", "mit", "bei"} out.Solution = "aus" if err := ValidateGeneratedCourseTask(out, in); err != nil { t.Fatal(err) } out.Options = []string{"aus Deutschland", "in Deutschland", "nach Deutschland"} out.Solution = "aus Deutschland" if err := ValidateGeneratedCourseTask(out, in); err != nil { t.Fatal("technical validator must not impose word translations", err) } } func TestCourseFlowUsesSeparateIDAndCompactQuestion(t *testing.T) { in, out := courseFixture() for _, envelope := range []string{"json", "text", "output", "direct"} { t.Run(envelope, func(t *testing.T) { called := false server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = true if r.URL.Path != "/api/v1/prediction/course-flow" || r.Header.Get("Authorization") != "Bearer course-key" { t.Error("wrong flow or key") } var payload struct { Question string `json:"question"` Streaming bool `json:"streaming"` } if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { t.Fatal(err) } var request domain.GenerateCourseTaskRequest if err := json.Unmarshal([]byte(payload.Question), &request); err != nil || request.Task != "generate_course_task" || request.LearningGoal.ID != in.LearningGoal.ID { t.Error("wrong course question") } if strings.Contains(payload.Question, "user_id") { t.Error("unnecessary user data") } if envelope == "direct" { json.NewEncoder(w).Encode(out) } else if envelope == "json" { json.NewEncoder(w).Encode(map[string]any{envelope: out}) } else { data, _ := json.Marshal(out) json.NewEncoder(w).Encode(map[string]any{envelope: string(data)}) } })) defer server.Close() teacher := NewFlowiseTeacher(config.Config{FlowiseBaseURL: server.URL, FlowiseChatflow: "pronunciation-flow", FlowiseCourseChatflow: "course-flow", FlowiseAPIKey: "old-key", FlowiseCourseAPIKey: "course-key"}, slog.New(slog.NewTextHandler(io.Discard, nil))) got, err := teacher.GenerateCourseTask(context.Background(), in) if err != nil || got.Solution != "am" || !called { t.Fatalf("got %#v, %v", got, err) } }) } } func TestCourseFlowFailureAndUnknownFields(t *testing.T) { in, out := courseFixture() data, _ := json.Marshal(out) unknown := strings.TrimSuffix(string(data), "}") + `,"owner_user_id":"someone"}` for _, test := range []struct { name, body string status int }{ {"unknown", unknown, 200}, {"oversized", strings.Repeat("x", 65537), 200}, {"http error", string(data), 500}, {"invalid", "not json", 200}, } { t.Run(test.name, func(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(test.status); io.WriteString(w, test.body) })) defer server.Close() teacher := NewFlowiseTeacher(config.Config{FlowiseBaseURL: server.URL, FlowiseCourseChatflow: "course"}, slog.Default()) if _, err := teacher.GenerateCourseTask(context.Background(), in); err == nil { t.Fatal("accepted bad response") } }) } teacher := NewFlowiseTeacher(config.Config{FlowiseBaseURL: "http://invalid", FlowiseChatflow: "pronunciation-only"}, slog.Default()) if _, err := teacher.GenerateCourseTask(context.Background(), in); err == nil { t.Fatal("used pronunciation flow") } } func TestCourseEnvelopePreservesActualOutputError(t *testing.T) { _, task := courseFixture() data, err := json.Marshal(task) if err != nil { t.Fatal(err) } for _, test := range []struct { name string output string want string }{ {"wrong numeric type", strings.Replace(string(data), `"schema_version":1`, `"schema_version":"1"`, 1), "schema_version"}, {"unknown task field", strings.TrimSuffix(string(data), "}") + `,"unexpected_field":true}`, "unexpected_field"}, {"unclosed markdown", "```json\n" + string(data), "unterminated Markdown"}, {"malformed JSON", `{broken`, "syntax at byte"}, {"missing schema", `{}`, "missing required field schema_version"}, } { t.Run(test.name, func(t *testing.T) { body, err := json.Marshal(map[string]any{"text": test.output, "chatId": "transport-metadata"}) if err != nil { t.Fatal(err) } _, err = parseCourseTaskEnvelope(body) if err == nil || !strings.Contains(err.Error(), test.want) || !strings.Contains(err.Error(), "text:") { t.Fatalf("wanted actual text output error containing %q, got %v", test.want, err) } if strings.Contains(err.Error(), "chatId") || strings.Contains(err.Error(), task.Explanation) { t.Fatal("reported transport metadata or response body instead of output error") } }) } } func TestEmptyStructuredCourseOutputDoesNotHideValidText(t *testing.T) { _, task := courseFixture() data, err := json.Marshal(task) if err != nil { t.Fatal(err) } body, err := json.Marshal(map[string]any{"json": map[string]any{}, "text": string(data)}) if err != nil { t.Fatal(err) } parsed, err := parseCourseTaskEnvelope(body) if err != nil || parsed.Solution != task.Solution { t.Fatalf("valid text was masked by empty json output: %v", err) } } func TestCourseFlowAcceptsFencedJSONAndStillValidates(t *testing.T) { input, task := courseFixture() encoded, err := json.Marshal(task) if err != nil { t.Fatal(err) } unknown := strings.TrimSuffix(string(encoded), "}") + `,"unexpected_field":true}` wrongSolution := strings.Replace(string(encoded), `"solution":"am"`, `"solution":"unknown"`, 1) for _, test := range []struct { name, text string valid bool }{ {"json fence", "```json\n" + string(encoded) + "\n```", true}, {"plain fence", "```\n" + string(encoded) + "\n```", true}, {"CRLF whitespace", " \n```JSON\r\n" + string(encoded) + "\r\n```\n ", true}, {"unknown field inside fence", "```json\n" + unknown + "\n```", false}, {"wrong solution inside fence", "```json\n" + wrongSolution + "\n```", false}, {"trailing prose", "```json\n" + string(encoded) + "\n```\nHere is your task.", false}, {"preceding prose", "Here is your task:\n```json\n" + string(encoded) + "\n```", false}, {"second JSON object", "```json\n" + string(encoded) + "\n{}\n```", false}, {"two fenced blocks", "```json\n" + string(encoded) + "\n```\n```json\n{}\n```", false}, {"missing closing fence", "```json\n" + string(encoded), false}, {"wrong block type", "```javascript\n" + string(encoded) + "\n```", false}, } { t.Run(test.name, func(t *testing.T) { for _, format := range []string{"text", "direct"} { t.Run(format, func(t *testing.T) { body := []byte(test.text) if format == "text" { var err error body, err = json.Marshal(map[string]any{"text": test.text, "chatId": "transport-only"}) if err != nil { t.Fatal(err) } } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write(body) })) defer server.Close() teacher := NewFlowiseTeacher(config.Config{FlowiseBaseURL: server.URL, FlowiseCourseChatflow: "course"}, slog.New(slog.NewTextHandler(io.Discard, nil))) got, err := teacher.GenerateCourseTask(context.Background(), input) if (err == nil) != test.valid { t.Fatalf("valid=%v, got error %v", test.valid, err) } if test.valid && (got.Prompt != task.Prompt || got.Solution != task.Solution) { t.Fatal("changed JSON content while removing wrapper") } }) } }) } } // This is the real course reply supplied while diagnosing the VPS fallback. // It is a valid self-introduction with I am, despite differing from the example // template and appending a harmless German instruction. func TestActualFlowiseStudentIntroduction(t *testing.T) { const reply = `{ "schema_version": 1, "target_language": "en-US", "cefr_level": "A1", "module_id": "a1_introductions_v1", "learning_goal_id": "introductions.be_i", "skill": "grammar", "kind": "choice", "prompt": "Ergänze: I ___ a student. (Wähle die richtige Form aus.)", "options": ["am", "is", "are"], "solution": "am", "explanation": "Hier wird die richtige Form von 'to be' für die erste Person Singular im Präsens verwendet. Da es um eine Aussage über die sprechende Person geht, heißt es 'I am'. Die anderen Formen ('is' und 'are') passen nicht in diesen Satz." }` input, _ := courseFixture() body, err := json.Marshal(map[string]any{"text": "```json\n" + reply + "\n```"}) if err != nil { t.Fatal(err) } task, err := parseCourseTaskEnvelope(body) if err != nil { t.Fatal(err) } if err := ValidateGeneratedCourseTask(task, input); err != nil { t.Fatalf("rejected real valid Flowise reply: %v", err) } if task.Solution != "am" { t.Fatal("changed expected solution") } task.Solution = "unknown" if ValidateGeneratedCourseTask(task, input) == nil { t.Fatal("accepted wrong answer for real reply") } }