TestSubAgentMiddleware_Integration 集成测试: SubAgent Middleware
(t *testing.T)
| 50 | |
| 51 | // TestSubAgentMiddleware_Integration 集成测试: SubAgent Middleware |
| 52 | func TestSubAgentMiddleware_Integration(t *testing.T) { |
| 53 | ctx := context.Background() |
| 54 | |
| 55 | // 1. 定义子代理规格 |
| 56 | specs := []SubAgentSpec{ |
| 57 | { |
| 58 | Name: "test-agent", |
| 59 | Description: "测试用子代理", |
| 60 | Prompt: "Test prompt", |
| 61 | }, |
| 62 | } |
| 63 | |
| 64 | // 2. 创建工厂 |
| 65 | factory := func(ctx context.Context, spec SubAgentSpec) (SubAgent, error) { |
| 66 | execFn := func(ctx context.Context, description string, parentContext map[string]any) (string, error) { |
| 67 | return "Test result: " + description, nil |
| 68 | } |
| 69 | return NewSimpleSubAgent(spec.Name, spec.Prompt, execFn), nil |
| 70 | } |
| 71 | |
| 72 | // 3. 创建 SubAgentMiddleware |
| 73 | middleware, err := NewSubAgentMiddleware(&SubAgentMiddlewareConfig{ |
| 74 | Specs: specs, |
| 75 | Factory: factory, |
| 76 | }) |
| 77 | if err != nil { |
| 78 | t.Fatalf("Failed to create SubAgentMiddleware: %v", err) |
| 79 | } |
| 80 | |
| 81 | // 4. 测试工具 |
| 82 | tools := middleware.Tools() |
| 83 | if len(tools) != 1 { |
| 84 | t.Fatalf("Expected 1 tool, got %d", len(tools)) |
| 85 | } |
| 86 | |
| 87 | taskTool := tools[0] |
| 88 | if taskTool.Name() != "task" { |
| 89 | t.Errorf("Expected tool name 'task', got %s", taskTool.Name()) |
| 90 | } |
| 91 | |
| 92 | // 5. 测试 task 执行 |
| 93 | result, err := taskTool.Execute(ctx, map[string]any{ |
| 94 | "description": "Test task", |
| 95 | "subagent_type": "test-agent", |
| 96 | }, nil) |
| 97 | if err != nil { |
| 98 | t.Fatalf("Task execution failed: %v", err) |
| 99 | } |
| 100 | |
| 101 | resultMap, ok := result.(map[string]any) |
| 102 | if !ok { |
| 103 | t.Fatal("Result is not a map") |
| 104 | } |
| 105 | |
| 106 | if !resultMap["ok"].(bool) { |
| 107 | t.Errorf("Task should succeed, got ok=false") |
| 108 | } |
| 109 |
nothing calls this directly
no test coverage detected