From ef5858851a939821da2d9ffc6e4d75465a579f5b Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Fri, 19 Jun 2026 03:37:08 -0700 Subject: [PATCH] feat: allow empty stimulus and response on question type definitions Co-authored-by: Cursor --- internal/domain/question_type_builder.go | 24 +++++-------- internal/domain/question_type_builder_test.go | 35 +++++++++++++++++-- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/internal/domain/question_type_builder.go b/internal/domain/question_type_builder.go index fef8ba1b..53ac79d1 100644 --- a/internal/domain/question_type_builder.go +++ b/internal/domain/question_type_builder.go @@ -139,21 +139,14 @@ func IsValidResponseComponentKind(s string) bool { } // ValidateDynamicQuestionTypeDefinition checks stimulus and response component selections for a -// temporary dynamic question-type definition. Empty or duplicate entries, unknown kinds, and -// disallowed combinations return a non-nil error. +// dynamic question-type definition. Both sides may be empty (shell definitions). Unknown kinds, +// duplicates, and disallowed combinations return a non-nil error. func ValidateDynamicQuestionTypeDefinition(stimulusKinds, responseKinds []string) error { var errs []string stimulus := normalizeKindList(stimulusKinds) response := normalizeKindList(responseKinds) - if len(stimulus) == 0 { - errs = append(errs, "at least one stimulus (question input) component is required") - } - if len(response) == 0 { - errs = append(errs, "at least one response component is required") - } - for i, k := range stimulus { if k == "" { errs = append(errs, fmt.Sprintf("stimulus [%d]: empty component kind", i)) @@ -400,8 +393,13 @@ func ResolveRuntimeQuestionTypeFromDefinition(key string, responseKinds []string return "TRUE_FALSE" } + response := normalizeKindList(responseKinds) + if len(response) == 0 { + return "DYNAMIC" + } + hasNonAuxiliary := false - for _, kind := range normalizeKindList(responseKinds) { + for _, kind := range response { if _, aux := responseKindsAuxiliary[kind]; !aux { hasNonAuxiliary = true } @@ -495,12 +493,6 @@ func ValidateDefinitionSchemas(stimulusSchema, responseSchema []DynamicElementDe func ValidateDynamicPayloadAgainstDefinition(payload DynamicQuestionPayload, def QuestionTypeDefinition) error { var errs []string - if len(payload.Stimulus) == 0 { - errs = append(errs, "dynamic_payload.stimulus must contain at least one element") - } - if len(payload.Response) == 0 { - errs = append(errs, "dynamic_payload.response must contain at least one element") - } allowedStimulus := make(map[string]struct{}) allowedResponse := make(map[string]struct{}) diff --git a/internal/domain/question_type_builder_test.go b/internal/domain/question_type_builder_test.go index bae7e939..4559d6f2 100644 --- a/internal/domain/question_type_builder_test.go +++ b/internal/domain/question_type_builder_test.go @@ -49,13 +49,20 @@ func TestValidateDynamicQuestionTypeDefinition_unknownStimulus(t *testing.T) { } } -func TestValidateDynamicQuestionTypeDefinition_emptyResponse(t *testing.T) { +func TestValidateDynamicQuestionTypeDefinition_emptyShell(t *testing.T) { + err := ValidateDynamicQuestionTypeDefinition(nil, nil) + if err != nil { + t.Fatalf("expected empty shell definition to be valid, got %v", err) + } +} + +func TestValidateDynamicQuestionTypeDefinition_stimulusOnly(t *testing.T) { err := ValidateDynamicQuestionTypeDefinition( []string{"INSTRUCTION"}, nil, ) - if err == nil || !strings.Contains(err.Error(), "at least one response") { - t.Fatalf("expected empty response error, got %v", err) + if err != nil { + t.Fatalf("expected stimulus-only definition to be valid, got %v", err) } } @@ -124,6 +131,28 @@ func TestValidatePersistableQuestionTypeDefinition_unmappable(t *testing.T) { } } +func TestValidatePersistableQuestionTypeDefinition_emptyShell(t *testing.T) { + err := ValidatePersistableQuestionTypeDefinition("custom_shell", nil) + if err != nil { + t.Fatalf("expected empty shell definition to be persistable, got %v", err) + } +} + +func TestResolveRuntimeQuestionTypeFromDefinition_emptyShell(t *testing.T) { + got := ResolveRuntimeQuestionTypeFromDefinition("custom_shell", nil) + if got != "DYNAMIC" { + t.Fatalf("expected DYNAMIC for empty shell, got %q", got) + } +} + +func TestValidateDynamicPayloadAgainstDefinition_emptyShell(t *testing.T) { + def := QuestionTypeDefinition{} + payload := DynamicQuestionPayload{} + if err := ValidateDynamicPayloadAgainstDefinition(payload, def); err != nil { + t.Fatalf("expected empty payload against empty shell definition, got %v", err) + } +} + func TestValidateDefinitionSchemas_valid(t *testing.T) { err := ValidateDefinitionSchemas( []DynamicElementDefinition{