änderungen von vorherigen Codexnutzung: sollte den Agent lassen die Aufgaben zu erzeugen

This commit is contained in:
Eugenius Naumenko
2026-09-22 16:14:41 +00:00
parent a7d8f72129
commit 74a4caa1c6
12 changed files with 457 additions and 239 deletions
+60 -67
View File
@@ -13,73 +13,27 @@ import (
"speech-learning-api/internal/domain"
)
func TestParseTeacherResponse(t *testing.T) {
response, ok := parseTeacherResponse(`{
"exercise_type": "repeat_sentence",
"target_text": "Hello.",
"teacher_feedback": "Gut.",
"tts_text": "Hello.",
"ssml": "",
"next_recommendation": "Noch einmal."
}`)
if !ok {
t.Fatal("expected response to parse")
}
if response.TargetText != "Hello." {
t.Fatalf("unexpected target text %q", response.TargetText)
}
}
func TestParseTeacherResponseJSONFromObject(t *testing.T) {
response, ok := parseTeacherResponseJSON(json.RawMessage(`{
"exercise_type": "repeat_sentence",
"target_text": "Hello.",
"teacher_feedback": "Gut.",
"tts_text": "Hello.",
"ssml": "",
"next_recommendation": "Noch einmal."
}`))
if !ok {
t.Fatal("expected response to parse")
}
if response.TeacherFeedback != "Gut." {
t.Fatalf("unexpected feedback %q", response.TeacherFeedback)
}
}
func TestParseTeacherResponseJSONFromString(t *testing.T) {
response, ok := parseTeacherResponseJSON(json.RawMessage(`"{\"exercise_type\":\"repeat_sentence\",\"target_text\":\"Hello.\",\"teacher_feedback\":\"Gut.\",\"tts_text\":\"Hello.\",\"ssml\":\"\",\"next_recommendation\":\"Noch einmal.\"}"`))
if !ok {
t.Fatal("expected response to parse")
}
if response.NextRecommendation != "Noch einmal." {
t.Fatalf("unexpected recommendation %q", response.NextRecommendation)
}
}
func TestFlowiseAskSendsQuestionOnly(t *testing.T) {
func TestFlowiseNextExerciseSendsSessionAndParsesResponse(t *testing.T) {
var requestBody map[string]json.RawMessage
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/prediction/chatflow-id" {
t.Fatalf("unexpected path %q", r.URL.Path)
}
if r.Header.Get("Authorization") != "Bearer api-key" {
t.Fatalf("unexpected authorization header %q", r.Header.Get("Authorization"))
}
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
t.Fatalf("decode request body: %v", err)
}
response := domain.TeacherResponse{
ExerciseType: "conversation",
TargetText: "Hello.",
TeacherFeedback: "Gut.",
TTSText: "Hello.",
NextRecommendation: "Noch einmal.",
response := domain.GeneratedExercise{
ExerciseType: "repeat_sentence",
TargetLanguage: "en-US",
CEFRLevel: "A1",
ReferenceText: "I live in Berlin.",
DisplayPrompt: "Sprich den Satz klar nach.",
TTSText: "I live in Berlin.",
FocusPoints: []string{"live"},
SuccessCriteria: []string{"accuracy above 80"},
ReasonForChoice: "Uebt einen kurzen A1-Satz.",
}
responseText, err := json.Marshal(response)
if err != nil {
@@ -94,22 +48,61 @@ func TestFlowiseAskSendsQuestionOnly(t *testing.T) {
teacher := NewFlowiseTeacher(config.Config{
FlowiseBaseURL: server.URL,
FlowiseChatflow: "chatflow-id",
FlowiseAPIKey: "api-key",
}, slog.New(slog.NewTextHandler(io.Discard, nil)))
response, err := teacher.ask(context.Background(), map[string]any{"task": "start_conversation"}, domain.TeacherResponse{
TeacherFeedback: "fallback",
exercise, err := teacher.NextExercise(context.Background(), domain.GeneratedExerciseRequest{
UserID: "usr_1",
TargetLanguage: "en-US",
CEFRLevel: "A1",
SessionID: "usr_1:en-US:practice",
})
if err != nil {
t.Fatalf("ask: %v", err)
t.Fatalf("NextExercise: %v", err)
}
if response.TeacherFeedback != "Gut." {
t.Fatalf("unexpected feedback %q", response.TeacherFeedback)
if exercise.ReferenceText != "I live in Berlin." {
t.Fatalf("unexpected reference text %q", exercise.ReferenceText)
}
if _, ok := requestBody["question"]; !ok {
t.Fatal("expected question in request body")
if exercise.Source != "flowise" {
t.Fatalf("unexpected source %q", exercise.Source)
}
if _, ok := requestBody["form"]; ok {
t.Fatal("did not expect form in request body")
var override struct {
SessionID string `json:"sessionId"`
}
if err := json.Unmarshal(requestBody["overrideConfig"], &override); err != nil {
t.Fatalf("decode overrideConfig: %v", err)
}
if override.SessionID != "usr_1:en-US:practice" {
t.Fatalf("unexpected sessionId %q", override.SessionID)
}
var question map[string]any
if err := json.Unmarshal(requestBody["question"], &question); err == nil {
t.Fatalf("expected question to be a JSON string, got object %#v", question)
}
var questionText string
if err := json.Unmarshal(requestBody["question"], &questionText); err != nil {
t.Fatalf("decode question text: %v", err)
}
if err := json.Unmarshal([]byte(questionText), &question); err != nil {
t.Fatalf("decode question payload: %v", err)
}
if question["task"] != "next_exercise" {
t.Fatalf("unexpected task %#v", question["task"])
}
}
func TestParseGeneratedExerciseEnvelope(t *testing.T) {
body := []byte(`{"text":"{\"exercise_type\":\"repeat_sentence\",\"target_language\":\"en-US\",\"cefr_level\":\"A1\",\"reference_text\":\"I live in Berlin.\",\"display_prompt\":\"Sprich den Satz klar nach.\",\"tts_text\":\"I live in Berlin.\",\"focus_points\":[\"live\"],\"success_criteria\":[\"accuracy above 80\"],\"reason_for_choice\":\"Practise /l/.\"}"}`)
exercise, ok := parseGeneratedExerciseEnvelope(body)
if !ok {
t.Fatal("expected generated exercise to parse")
}
if exercise.ReferenceText != "I live in Berlin." {
t.Fatalf("unexpected reference text %q", exercise.ReferenceText)
}
if len(exercise.FocusPoints) != 1 || exercise.FocusPoints[0] != "live" {
t.Fatalf("unexpected focus points %#v", exercise.FocusPoints)
}
}