Synthesize dynamic_payload from flat legacy fields on outbound API responses so AUDIO, MCQ, and SHORT_ANSWER questions match DYNAMIC question structure. Co-authored-by: Cursor <cursoragent@cursor.com>
94 lines
2.9 KiB
Go
94 lines
2.9 KiB
Go
package domain
|
|
|
|
import "testing"
|
|
|
|
func TestResolveOutboundDynamicPayload_legacyAUDIO(t *testing.T) {
|
|
catalog := []QuestionTypeDefinition{
|
|
{
|
|
ID: 37,
|
|
Key: "audio_conversation_type",
|
|
StimulusSchema: []DynamicElementDefinition{
|
|
{ID: "question_text_1", Kind: "QUESTION_TEXT"},
|
|
{ID: "voice_prompt_1", Kind: "AUDIO_PROMPT"},
|
|
{ID: "sample_answer_voice_prompt_1", Kind: "AUDIO_PROMPT"},
|
|
},
|
|
ResponseSchema: []DynamicElementDefinition{
|
|
{ID: "audio_response_1", Kind: "AUDIO_RESPONSE"},
|
|
},
|
|
},
|
|
}
|
|
voice := "https://example.com/voice.mp3"
|
|
sample := "https://example.com/sample.mp3"
|
|
q := QuestionWithDetails{
|
|
Question: Question{
|
|
QuestionText: "How is the weather?",
|
|
QuestionType: "AUDIO",
|
|
QuestionTypeDefinitionID: int64Ptr(37),
|
|
VoicePrompt: &voice,
|
|
SampleAnswerVoicePrompt: &sample,
|
|
},
|
|
}
|
|
|
|
got := ResolveOutboundDynamicPayload(q, catalog)
|
|
if got == nil {
|
|
t.Fatal("expected synthesized payload")
|
|
}
|
|
if len(got.Stimulus) != 3 {
|
|
t.Fatalf("expected 3 stimulus elements, got %d", len(got.Stimulus))
|
|
}
|
|
if got.Stimulus[0].Kind != "QUESTION_TEXT" || got.Stimulus[0].Value != "How is the weather?" {
|
|
t.Fatalf("unexpected question text stimulus: %+v", got.Stimulus[0])
|
|
}
|
|
if got.Stimulus[1].ID != "voice_prompt_1" || got.Stimulus[1].Value != voice {
|
|
t.Fatalf("unexpected voice prompt: %+v", got.Stimulus[1])
|
|
}
|
|
if got.Stimulus[2].ID != "sample_answer_voice_prompt_1" || got.Stimulus[2].Value != sample {
|
|
t.Fatalf("unexpected sample voice prompt: %+v", got.Stimulus[2])
|
|
}
|
|
if len(got.Response) != 1 || got.Response[0].Kind != "AUDIO_RESPONSE" || got.Response[0].Value != sample {
|
|
t.Fatalf("unexpected response: %+v", got.Response)
|
|
}
|
|
}
|
|
|
|
func TestResolveOutboundDynamicPayload_storedDynamicPreferred(t *testing.T) {
|
|
stored := &DynamicQuestionPayload{
|
|
Stimulus: []DynamicElementInstance{{ID: "question_text_1", Kind: "QUESTION_TEXT", Value: "Stored"}},
|
|
}
|
|
q := QuestionWithDetails{
|
|
Question: Question{
|
|
QuestionText: "Legacy text",
|
|
QuestionType: "DYNAMIC",
|
|
DynamicPayload: stored,
|
|
},
|
|
}
|
|
got := ResolveOutboundDynamicPayload(q, nil)
|
|
if got != stored {
|
|
t.Fatal("expected stored payload to be returned unchanged")
|
|
}
|
|
}
|
|
|
|
func TestResolveOutboundDynamicPayload_legacyMCQ(t *testing.T) {
|
|
q := QuestionWithDetails{
|
|
Question: Question{
|
|
QuestionText: "Pick one",
|
|
QuestionType: "MCQ",
|
|
},
|
|
Options: []QuestionOption{
|
|
{ID: 1, OptionText: "A", OptionOrder: 1, IsCorrect: true},
|
|
{ID: 2, OptionText: "B", OptionOrder: 2, IsCorrect: false},
|
|
},
|
|
}
|
|
got := ResolveOutboundDynamicPayload(q, testQuestionTypeCatalog())
|
|
if got == nil {
|
|
t.Fatal("expected synthesized payload")
|
|
}
|
|
if len(got.Stimulus) != 1 || got.Stimulus[0].Kind != "QUESTION_TEXT" {
|
|
t.Fatalf("unexpected stimulus: %+v", got.Stimulus)
|
|
}
|
|
if len(got.Response) != 1 || got.Response[0].Kind != "MULTIPLE_CHOICE" {
|
|
t.Fatalf("unexpected response: %+v", got.Response)
|
|
}
|
|
}
|
|
|
|
func int64Ptr(v int64) *int64 { return &v }
|