TestHTTPBridgeServerEndpoints 测试 HTTP 服务器端点
(t *testing.T)
| 116 | |
| 117 | // TestHTTPBridgeServerEndpoints 测试 HTTP 服务器端点 |
| 118 | func TestHTTPBridgeServerEndpoints(t *testing.T) { |
| 119 | // 创建工具注册表和桥接 |
| 120 | registry := tools.NewRegistry() |
| 121 | registry.Register("MockTool", NewMockTool) |
| 122 | toolBridge := NewToolBridge(registry) |
| 123 | |
| 124 | // 创建并启动服务器 |
| 125 | server := NewHTTPBridgeServer(toolBridge, "localhost:18081") |
| 126 | if err := server.StartAsync(); err != nil { |
| 127 | t.Fatalf("Failed to start server: %v", err) |
| 128 | } |
| 129 | defer func() { |
| 130 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 131 | defer cancel() |
| 132 | _ = server.Shutdown(ctx) |
| 133 | }() |
| 134 | |
| 135 | // 等待服务器启动 |
| 136 | time.Sleep(200 * time.Millisecond) |
| 137 | |
| 138 | // 测试各个端点 |
| 139 | t.Run("ToolList", func(t *testing.T) { |
| 140 | tools := toolBridge.ListAvailableTools() |
| 141 | if len(tools) == 0 { |
| 142 | t.Error("Expected at least one tool") |
| 143 | } |
| 144 | if tools[0] != "MockTool" { |
| 145 | t.Errorf("Expected MockTool, got %s", tools[0]) |
| 146 | } |
| 147 | }) |
| 148 | |
| 149 | t.Run("ToolSchema", func(t *testing.T) { |
| 150 | schema, err := toolBridge.GetToolSchema("MockTool") |
| 151 | if err != nil { |
| 152 | t.Fatalf("Failed to get schema: %v", err) |
| 153 | } |
| 154 | if schema["name"] != "MockTool" { |
| 155 | t.Error("Schema name mismatch") |
| 156 | } |
| 157 | }) |
| 158 | |
| 159 | t.Run("ToolCall", func(t *testing.T) { |
| 160 | ctx := context.Background() |
| 161 | result, err := toolBridge.CallTool(ctx, "MockTool", map[string]any{ |
| 162 | "message": "Test message", |
| 163 | }, nil) |
| 164 | |
| 165 | if err != nil { |
| 166 | t.Fatalf("Failed to call tool: %v", err) |
| 167 | } |
| 168 | |
| 169 | if !result.Success { |
| 170 | t.Errorf("Tool call failed: %s", result.Error) |
| 171 | } |
| 172 | |
| 173 | resultMap, ok := result.Result.(map[string]any) |
| 174 | if !ok { |
| 175 | t.Fatal("Expected map result") |
nothing calls this directly
no test coverage detected