feat(tools/basic): 内置基础工具(calculator(注入安全)/datetime/random/text/json/markdown)+ 批量注册
This commit is contained in:
1 parent
b3c61aa1ff
commit
08223c06a5
19 files changed
+2074
No files matched your search
@@ -0,0 +1,77 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/capability/registry"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/pkg/plugins/sdk"
|
||||
)
|
||||
|
||||
// TestTools verifies Tools returns six valid, uniquely-named sdk.Tool values.
|
||||
func TestTools(t *testing.T) {
|
||||
t.Parallel()
|
||||
tools := Tools()
|
||||
if len(tools) != 6 {
|
||||
t.Fatalf("Tools() = %d tools, want 6", len(tools))
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, tool := range tools {
|
||||
if tool.Name() == "" || tool.Description() == "" {
|
||||
t.Fatalf("tool %q missing Name or Description", tool.Name())
|
||||
}
|
||||
params, err := tool.Parameters()
|
||||
if err != nil {
|
||||
t.Fatalf("tool %q Parameters() error: %v", tool.Name(), err)
|
||||
}
|
||||
if params == nil {
|
||||
t.Fatalf("tool %q Parameters() returned nil", tool.Name())
|
||||
}
|
||||
if seen[tool.Name()] {
|
||||
t.Fatalf("duplicate tool name %q", tool.Name())
|
||||
}
|
||||
seen[tool.Name()] = true
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegister_Success verifies every tool is registered as a KindTool capability.
|
||||
func TestRegister_Success(t *testing.T) {
|
||||
t.Parallel()
|
||||
reg := registry.New()
|
||||
if err := Register(reg); err != nil {
|
||||
t.Fatalf("Register() error: %v", err)
|
||||
}
|
||||
caps := reg.List(registry.KindTool)
|
||||
if len(caps) != 6 {
|
||||
t.Fatalf("List(KindTool) = %d, want 6", len(caps))
|
||||
}
|
||||
for _, name := range []string{"calculator", "datetime", "random", "text", "json", "markdown"} {
|
||||
cap, err := reg.Get(name)
|
||||
if err != nil {
|
||||
t.Fatalf("Get(%q) error: %v", name, err)
|
||||
}
|
||||
if cap.Kind != registry.KindTool {
|
||||
t.Fatalf("capability %q kind = %v, want tool", name, cap.Kind)
|
||||
}
|
||||
if _, ok := cap.Value.(sdk.Tool); !ok {
|
||||
t.Fatalf("capability %q not an sdk.Tool", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegister_NilRegistry verifies nil registry is rejected.
|
||||
func TestRegister_NilRegistry(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := Register(nil)
|
||||
assertIs(t, err, sdk.ErrNilRegistrar)
|
||||
}
|
||||
|
||||
// TestRegister_Duplicate verifies a second registration is rejected.
|
||||
func TestRegister_Duplicate(t *testing.T) {
|
||||
t.Parallel()
|
||||
reg := registry.New()
|
||||
if err := Register(reg); err != nil {
|
||||
t.Fatalf("first Register() error: %v", err)
|
||||
}
|
||||
err := Register(reg)
|
||||
assertIs(t, err, registry.ErrDuplicate)
|
||||
}
|
||||
Reference in new issue
Block a user