feat: harden pet consultation SOP execution
This commit is contained in:
48
internal/run/validation_test.go
Normal file
48
internal/run/validation_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package run
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/model"
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
func TestValidateNodeAnswers(t *testing.T) {
|
||||
fields := []model.ScenarioField{
|
||||
{FieldKey: "pet_name", FieldName: "宠物名称", FieldType: "text", Required: true, Validation: datatypes.JSON([]byte(`{"max_length":20}`))},
|
||||
{FieldKey: "pet_weight", FieldName: "体重", FieldType: "number", Validation: datatypes.JSON([]byte(`{"min":0.01,"max":200}`))},
|
||||
{FieldKey: "pet_type", FieldName: "宠物种类", FieldType: "select", Options: datatypes.JSON([]byte(`["犬","猫"]`))},
|
||||
}
|
||||
node := model.SOPNode{Type: "form", Config: datatypes.JSON([]byte(`{"field_keys":["pet_name","pet_weight","pet_type"]}`))}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
answers map[string]interface{}
|
||||
want string
|
||||
}{
|
||||
{name: "valid", answers: map[string]interface{}{"pet_name": "豆包", "pet_weight": 5.2, "pet_type": "犬"}},
|
||||
{name: "missing required", answers: map[string]interface{}{"pet_weight": 5.2}, want: "请填写宠物名称"},
|
||||
{name: "unknown field", answers: map[string]interface{}{"pet_name": "豆包", "owner_phone": "123"}, want: "不属于当前节点"},
|
||||
{name: "invalid number", answers: map[string]interface{}{"pet_name": "豆包", "pet_weight": 0.0}, want: "不能小于"},
|
||||
{name: "invalid option", answers: map[string]interface{}{"pet_name": "豆包", "pet_type": "兔"}, want: "选项不正确"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := validateNodeAnswers(node, fields, test.answers)
|
||||
if test.want == "" && err != nil {
|
||||
t.Fatalf("validateNodeAnswers() error = %v", err)
|
||||
}
|
||||
if test.want != "" && (err == nil || !strings.Contains(err.Error(), test.want)) {
|
||||
t.Fatalf("validateNodeAnswers() error = %v, want containing %q", err, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateNodeAnswersRejectsAnswersForMessage(t *testing.T) {
|
||||
err := validateNodeAnswers(model.SOPNode{Type: "message", Config: datatypes.JSON([]byte(`{}`))}, nil, map[string]interface{}{"pet_name": "豆包"})
|
||||
if err == nil || !strings.Contains(err.Error(), "不接受字段回答") {
|
||||
t.Fatalf("validateNodeAnswers() error = %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user