TestHumanInTheLoopMiddleware_Reject 测试拒绝操作
(t *testing.T)
| 144 | |
| 145 | // TestHumanInTheLoopMiddleware_Reject 测试拒绝操作 |
| 146 | func TestHumanInTheLoopMiddleware_Reject(t *testing.T) { |
| 147 | ctx := context.Background() |
| 148 | |
| 149 | middleware, err := NewHumanInTheLoopMiddleware(&HumanInTheLoopMiddlewareConfig{ |
| 150 | InterruptOn: map[string]any{ |
| 151 | "dangerous_tool": map[string]any{ |
| 152 | "allowed_decisions": []any{"approve", "reject"}, |
| 153 | }, |
| 154 | }, |
| 155 | ApprovalHandler: func(ctx context.Context, request *ReviewRequest) ([]Decision, error) { |
| 156 | // 拒绝操作 |
| 157 | return []Decision{ |
| 158 | { |
| 159 | Type: DecisionReject, |
| 160 | Reason: "Too dangerous", |
| 161 | }, |
| 162 | }, nil |
| 163 | }, |
| 164 | }) |
| 165 | if err != nil { |
| 166 | t.Fatalf("Failed to create middleware: %v", err) |
| 167 | } |
| 168 | |
| 169 | req := &ToolCallRequest{ |
| 170 | ToolCallID: "test-reject", |
| 171 | ToolName: "dangerous_tool", |
| 172 | ToolInput: map[string]any{ |
| 173 | "action": "delete_all", |
| 174 | }, |
| 175 | Tool: &mockTool{name: "dangerous_tool"}, |
| 176 | } |
| 177 | |
| 178 | handler := func(ctx context.Context, req *ToolCallRequest) (*ToolCallResponse, error) { |
| 179 | t.Error("Handler should not be called when operation is rejected") |
| 180 | return nil, nil |
| 181 | } |
| 182 | |
| 183 | resp, err := middleware.WrapToolCall(ctx, req, handler) |
| 184 | if err != nil { |
| 185 | t.Fatalf("WrapToolCall failed: %v", err) |
| 186 | } |
| 187 | |
| 188 | resultMap, ok := resp.Result.(map[string]any) |
| 189 | if !ok { |
| 190 | t.Fatal("Result is not a map") |
| 191 | } |
| 192 | |
| 193 | if resultMap["ok"].(bool) { |
| 194 | t.Error("Expected ok=false for rejected operation") |
| 195 | } |
| 196 | |
| 197 | if !resultMap["rejected"].(bool) { |
| 198 | t.Error("Expected rejected=true") |
| 199 | } |
| 200 | |
| 201 | if resultMap["reason"] != "Too dangerous" { |
| 202 | t.Errorf("Expected reason='Too dangerous', got '%v'", resultMap["reason"]) |
| 203 | } |
nothing calls this directly
no test coverage detected