TestHumanInTheLoopMiddleware_BasicApproval 测试基本审核流程
(t *testing.T)
| 46 | |
| 47 | // TestHumanInTheLoopMiddleware_BasicApproval 测试基本审核流程 |
| 48 | func TestHumanInTheLoopMiddleware_BasicApproval(t *testing.T) { |
| 49 | ctx := context.Background() |
| 50 | |
| 51 | // 创建中间件 |
| 52 | middleware, err := NewHumanInTheLoopMiddleware(&HumanInTheLoopMiddlewareConfig{ |
| 53 | InterruptOn: map[string]any{ |
| 54 | "sensitive_tool": true, // 需要审核 |
| 55 | "safe_tool": false, // 不需要审核 |
| 56 | }, |
| 57 | ApprovalHandler: func(ctx context.Context, request *ReviewRequest) ([]Decision, error) { |
| 58 | // 自动批准 |
| 59 | decisions := make([]Decision, len(request.ActionRequests)) |
| 60 | for i := range decisions { |
| 61 | decisions[i] = Decision{ |
| 62 | Type: DecisionApprove, |
| 63 | Reason: "Test approval", |
| 64 | } |
| 65 | } |
| 66 | return decisions, nil |
| 67 | }, |
| 68 | }) |
| 69 | if err != nil { |
| 70 | t.Fatalf("Failed to create middleware: %v", err) |
| 71 | } |
| 72 | |
| 73 | // 测试1: sensitive_tool 需要审核,但被批准 |
| 74 | t.Run("Approve sensitive tool", func(t *testing.T) { |
| 75 | req := &ToolCallRequest{ |
| 76 | ToolCallID: "test-1", |
| 77 | ToolName: "sensitive_tool", |
| 78 | ToolInput: map[string]any{ |
| 79 | "param": "value1", |
| 80 | }, |
| 81 | Tool: &mockTool{name: "sensitive_tool"}, |
| 82 | } |
| 83 | |
| 84 | handler := func(ctx context.Context, req *ToolCallRequest) (*ToolCallResponse, error) { |
| 85 | // 执行工具 |
| 86 | result, _ := req.Tool.Execute(ctx, req.ToolInput, nil) |
| 87 | return &ToolCallResponse{ |
| 88 | Result: result, |
| 89 | }, nil |
| 90 | } |
| 91 | |
| 92 | resp, err := middleware.WrapToolCall(ctx, req, handler) |
| 93 | if err != nil { |
| 94 | t.Fatalf("WrapToolCall failed: %v", err) |
| 95 | } |
| 96 | |
| 97 | resultMap, ok := resp.Result.(map[string]any) |
| 98 | if !ok { |
| 99 | t.Fatal("Result is not a map") |
| 100 | } |
| 101 | |
| 102 | if !resultMap["ok"].(bool) { |
| 103 | t.Errorf("Expected ok=true, got ok=false") |
| 104 | } |
| 105 |
nothing calls this directly
no test coverage detected