|
|
|
@ -19,200 +19,170 @@ package plugin |
|
|
|
|
import ( |
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
common "github.com/coze-dev/coze-studio/backend/api/model/plugin_develop/common" |
|
|
|
|
workflow3 "github.com/coze-dev/coze-studio/backend/api/model/workflow" |
|
|
|
|
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr" |
|
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
|
|
|
|
|
|
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func TestPluginService_UnwrapArrayItemFieldsInVariable(t *testing.T) { |
|
|
|
|
s := &pluginService{} |
|
|
|
|
t.Run("unwraps a simple array item", func(t *testing.T) { |
|
|
|
|
input := &vo.Variable{ |
|
|
|
|
Name: "root", |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
{ |
|
|
|
|
Name: "[Array Item]", |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
{Name: "field1", Type: vo.VariableTypeString}, |
|
|
|
|
{Name: "field2", Type: vo.VariableTypeInteger}, |
|
|
|
|
func TestToWorkflowAPIParameter(t *testing.T) { |
|
|
|
|
cases := []struct { |
|
|
|
|
name string |
|
|
|
|
param *common.APIParameter |
|
|
|
|
expected *workflow3.APIParameter |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "nil parameter", |
|
|
|
|
param: nil, |
|
|
|
|
expected: nil, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "simple string parameter", |
|
|
|
|
param: &common.APIParameter{ |
|
|
|
|
Name: "prompt", |
|
|
|
|
Type: common.ParameterType_String, |
|
|
|
|
Desc: "User's prompt", |
|
|
|
|
}, |
|
|
|
|
expected: &workflow3.APIParameter{ |
|
|
|
|
Name: "prompt", |
|
|
|
|
Type: workflow3.ParameterType_String, |
|
|
|
|
Desc: "User's prompt", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "simple object parameter", |
|
|
|
|
param: &common.APIParameter{ |
|
|
|
|
Name: "user_info", |
|
|
|
|
Type: common.ParameterType_Object, |
|
|
|
|
SubParameters: []*common.APIParameter{ |
|
|
|
|
{ |
|
|
|
|
Name: "name", |
|
|
|
|
Type: common.ParameterType_String, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Name: "age", |
|
|
|
|
Type: common.ParameterType_Number, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
expected := &vo.Variable{ |
|
|
|
|
Name: "root", |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
{Name: "field1", Type: vo.VariableTypeString}, |
|
|
|
|
{Name: "field2", Type: vo.VariableTypeInteger}, |
|
|
|
|
expected: &workflow3.APIParameter{ |
|
|
|
|
Name: "user_info", |
|
|
|
|
Type: workflow3.ParameterType_Object, |
|
|
|
|
SubParameters: []*workflow3.APIParameter{ |
|
|
|
|
{ |
|
|
|
|
Name: "name", |
|
|
|
|
Type: workflow3.ParameterType_String, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Name: "age", |
|
|
|
|
Type: workflow3.ParameterType_Number, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err := s.UnwrapArrayItemFieldsInVariable(input) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Equal(t, expected, input) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("handles nested array items", func(t *testing.T) { |
|
|
|
|
input := &vo.Variable{ |
|
|
|
|
Name: "root", |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
{ |
|
|
|
|
Name: "[Array Item]", |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
{Name: "field1", Type: vo.VariableTypeString}, |
|
|
|
|
{ |
|
|
|
|
Name: "[Array Item]", |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
{Name: "nestedField", Type: vo.VariableTypeBoolean}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "array of strings", |
|
|
|
|
param: &common.APIParameter{ |
|
|
|
|
Name: "tags", |
|
|
|
|
Type: common.ParameterType_Array, |
|
|
|
|
SubParameters: []*common.APIParameter{ |
|
|
|
|
{ |
|
|
|
|
Name: "[Array Item]", |
|
|
|
|
Type: common.ParameterType_String, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
expected := &vo.Variable{ |
|
|
|
|
Name: "root", |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
{Name: "field1", Type: vo.VariableTypeString}, |
|
|
|
|
{Name: "nestedField", Type: vo.VariableTypeBoolean}, |
|
|
|
|
expected: &workflow3.APIParameter{ |
|
|
|
|
Name: "tags", |
|
|
|
|
Type: workflow3.ParameterType_Array, |
|
|
|
|
SubType: ptr.Of(workflow3.ParameterType_String), |
|
|
|
|
SubParameters: []*workflow3.APIParameter{ |
|
|
|
|
{ |
|
|
|
|
Type: workflow3.ParameterType_String, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err := s.UnwrapArrayItemFieldsInVariable(input) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Equal(t, expected, input) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("handles array item within a list", func(t *testing.T) { |
|
|
|
|
input := &vo.Variable{ |
|
|
|
|
Name: "rootList", |
|
|
|
|
Type: vo.VariableTypeList, |
|
|
|
|
Schema: &vo.Variable{ |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "array of objects", |
|
|
|
|
param: &common.APIParameter{ |
|
|
|
|
Name: "users", |
|
|
|
|
Type: common.ParameterType_Array, |
|
|
|
|
SubParameters: []*common.APIParameter{ |
|
|
|
|
{ |
|
|
|
|
Name: "[Array Item]", |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
{Name: "itemField", Type: vo.VariableTypeString}, |
|
|
|
|
Type: common.ParameterType_Object, |
|
|
|
|
SubParameters: []*common.APIParameter{ |
|
|
|
|
{ |
|
|
|
|
Name: "name", |
|
|
|
|
Type: common.ParameterType_String, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Name: "id", |
|
|
|
|
Type: common.ParameterType_Number, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
expected := &vo.Variable{ |
|
|
|
|
Name: "rootList", |
|
|
|
|
Type: vo.VariableTypeList, |
|
|
|
|
Schema: &vo.Variable{ |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
{Name: "itemField", Type: vo.VariableTypeString}, |
|
|
|
|
expected: &workflow3.APIParameter{ |
|
|
|
|
Name: "users", |
|
|
|
|
Type: workflow3.ParameterType_Array, |
|
|
|
|
SubType: ptr.Of(workflow3.ParameterType_Object), |
|
|
|
|
SubParameters: []*workflow3.APIParameter{ |
|
|
|
|
{ |
|
|
|
|
Name: "name", |
|
|
|
|
Type: workflow3.ParameterType_String, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Name: "id", |
|
|
|
|
Type: workflow3.ParameterType_Number, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err := s.UnwrapArrayItemFieldsInVariable(input) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Equal(t, expected, input) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("does nothing if no array item is present", func(t *testing.T) { |
|
|
|
|
input := &vo.Variable{ |
|
|
|
|
Name: "root", |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
{Name: "field1", Type: vo.VariableTypeString}, |
|
|
|
|
{Name: "field2", Type: vo.VariableTypeInteger}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create a copy for comparison as the input will be modified in place.
|
|
|
|
|
expected := &vo.Variable{ |
|
|
|
|
Name: "root", |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
{Name: "field1", Type: vo.VariableTypeString}, |
|
|
|
|
{Name: "field2", Type: vo.VariableTypeInteger}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err := s.UnwrapArrayItemFieldsInVariable(input) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Equal(t, expected, input) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("handles primitive type array item in object", func(t *testing.T) { |
|
|
|
|
input := &vo.Variable{ |
|
|
|
|
Name: "root", |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
{ |
|
|
|
|
Name: "[Array Item]", |
|
|
|
|
Type: vo.VariableTypeString, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Name: "anotherField", |
|
|
|
|
Type: vo.VariableTypeInteger, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "array of array of strings", |
|
|
|
|
param: &common.APIParameter{ |
|
|
|
|
Name: "matrix", |
|
|
|
|
Type: common.ParameterType_Array, |
|
|
|
|
SubParameters: []*common.APIParameter{ |
|
|
|
|
{ |
|
|
|
|
Name: "[Array Item]", |
|
|
|
|
Type: common.ParameterType_Array, |
|
|
|
|
SubParameters: []*common.APIParameter{ |
|
|
|
|
{ |
|
|
|
|
Name: "[Array Item]", |
|
|
|
|
Type: common.ParameterType_String, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
expected := &vo.Variable{ |
|
|
|
|
Name: "root", |
|
|
|
|
Type: vo.VariableTypeObject, |
|
|
|
|
Schema: []*vo.Variable{ |
|
|
|
|
{ |
|
|
|
|
Name: "", |
|
|
|
|
Type: vo.VariableTypeString, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
Name: "anotherField", |
|
|
|
|
Type: vo.VariableTypeInteger, |
|
|
|
|
expected: &workflow3.APIParameter{ |
|
|
|
|
Name: "matrix", |
|
|
|
|
Type: workflow3.ParameterType_Array, |
|
|
|
|
SubType: ptr.Of(workflow3.ParameterType_Array), |
|
|
|
|
SubParameters: []*workflow3.APIParameter{ |
|
|
|
|
{ |
|
|
|
|
Name: "", // Name is cleared
|
|
|
|
|
Type: workflow3.ParameterType_Array, |
|
|
|
|
SubType: ptr.Of(workflow3.ParameterType_String), |
|
|
|
|
SubParameters: []*workflow3.APIParameter{ |
|
|
|
|
{ |
|
|
|
|
Type: workflow3.ParameterType_String, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err := s.UnwrapArrayItemFieldsInVariable(input) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Equal(t, expected, input) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("handles list of primitives", func(t *testing.T) { |
|
|
|
|
input := &vo.Variable{ |
|
|
|
|
Name: "listOfStrings", |
|
|
|
|
Type: vo.VariableTypeList, |
|
|
|
|
Schema: &vo.Variable{ |
|
|
|
|
Name: "[Array Item]", |
|
|
|
|
Type: vo.VariableTypeString, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
expected := &vo.Variable{ |
|
|
|
|
Name: "listOfStrings", |
|
|
|
|
Type: vo.VariableTypeList, |
|
|
|
|
Schema: &vo.Variable{ |
|
|
|
|
Name: "", |
|
|
|
|
Type: vo.VariableTypeString, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err := s.UnwrapArrayItemFieldsInVariable(input) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Equal(t, expected, input) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("handles nil input", func(t *testing.T) { |
|
|
|
|
err := s.UnwrapArrayItemFieldsInVariable(nil) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
}) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, tc := range cases { |
|
|
|
|
t.Run(tc.name, func(t *testing.T) { |
|
|
|
|
actual := toWorkflowAPIParameter(tc.param) |
|
|
|
|
assert.Equal(t, tc.expected, actual) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|