Files
Yeuoly 9730b7dc63 tests: enhance integration testing for specific plugins (#242)
* feat: enhance integration testing for specific plugins

- Added integration tests for the official agent, including test data for agent strategy invocation.
- Introduced JSON schema generation and validation utilities to ensure proper request formatting.
- Enhanced mock invocation handling in the plugin manager to support tool parameters.
- Added new test utilities for simulating OpenAI server responses and managing plugin runtime.

* fix: update RunOnce function to return response stream and enhance integration test

- Modified the RunOnce function to return a response stream instead of an error, allowing for better handling of streamed responses.
- Updated the integration test for the official agent to read from the response stream, ensuring proper validation of the agent strategy invocation.
2025-04-24 16:33:50 +08:00

87 lines
1.7 KiB
Go

package jsonschema
import (
"testing"
"github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
"github.com/stretchr/testify/assert"
)
func TestGenerateValidateJson(t *testing.T) {
schema := map[string]any{
"type": "object",
"properties": map[string]any{
"name": map[string]any{
"type": "string",
},
},
}
type nameStruct struct {
Name string `json:"name"`
}
validateJson, err := GenerateValidateJson(schema)
assert.NoError(t, err)
// convert validateJson to nameStruct
name, err := parser.MapToStruct[nameStruct](validateJson)
assert.NoError(t, err)
assert.NotEmpty(t, name.Name)
}
func TestGenerateValidateJsonWithEnum(t *testing.T) {
schema := map[string]any{
"type": "object",
"properties": map[string]any{
"name": map[string]any{
"type": "string",
"enum": []string{"test", "test2"},
},
},
}
type nameStruct struct {
Name string `json:"name"`
}
validateJson, err := GenerateValidateJson(schema)
assert.NoError(t, err)
// convert validateJson to nameStruct
name, err := parser.MapToStruct[nameStruct](validateJson)
assert.NoError(t, err)
assert.Contains(t, []string{"test", "test2"}, name.Name)
}
func TestGenerateValidateJsonWithNumber(t *testing.T) {
schema := map[string]any{
"type": "object",
"properties": map[string]any{
"name": map[string]any{
"type": "string",
},
"age": map[string]any{
"type": "integer",
},
},
}
type nameStruct struct {
Name string `json:"name"`
Age int `json:"age"`
}
validateJson, err := GenerateValidateJson(schema)
assert.NoError(t, err)
// convert validateJson to nameStruct
name, err := parser.MapToStruct[nameStruct](validateJson)
assert.NoError(t, err)
assert.NotEmpty(t, name.Name)
assert.NotEmpty(t, name.Age)
}