TestPoolOperations 测试 Pool 操作
(t *testing.T)
| 121 | |
| 122 | // TestPoolOperations 测试 Pool 操作 |
| 123 | func TestPoolOperations(t *testing.T) { |
| 124 | srv, cleanup := setupTestServer(t) |
| 125 | defer cleanup() |
| 126 | |
| 127 | var agentID string |
| 128 | |
| 129 | // 1. 在 Pool 中创建 Agent |
| 130 | t.Run("CreateAgentInPool", func(t *testing.T) { |
| 131 | body := `{ |
| 132 | "template_id": "chat", |
| 133 | "model_config": { |
| 134 | "provider": "mock", |
| 135 | "model": "test-model" |
| 136 | } |
| 137 | }` |
| 138 | |
| 139 | req := httptest.NewRequest(http.MethodPost, "/v1/pool/agents", strings.NewReader(body)) |
| 140 | req.Header.Set("Content-Type", "application/json") |
| 141 | w := httptest.NewRecorder() |
| 142 | |
| 143 | srv.Router().ServeHTTP(w, req) |
| 144 | |
| 145 | assert.Equal(t, http.StatusCreated, w.Code) |
| 146 | |
| 147 | var resp map[string]any |
| 148 | err := json.Unmarshal(w.Body.Bytes(), &resp) |
| 149 | require.NoError(t, err) |
| 150 | |
| 151 | data := resp["data"].(map[string]any) |
| 152 | agentID = data["agent_id"].(string) |
| 153 | assert.NotEmpty(t, agentID) |
| 154 | }) |
| 155 | |
| 156 | // 2. 列出 Pool 中的 Agents |
| 157 | t.Run("ListPoolAgents", func(t *testing.T) { |
| 158 | req := httptest.NewRequest(http.MethodGet, "/v1/pool/agents", nil) |
| 159 | w := httptest.NewRecorder() |
| 160 | |
| 161 | srv.Router().ServeHTTP(w, req) |
| 162 | |
| 163 | assert.Equal(t, http.StatusOK, w.Code) |
| 164 | assert.Contains(t, w.Body.String(), "agents") |
| 165 | }) |
| 166 | |
| 167 | // 3. 获取 Pool 统计 |
| 168 | t.Run("GetPoolStats", func(t *testing.T) { |
| 169 | req := httptest.NewRequest(http.MethodGet, "/v1/pool/stats", nil) |
| 170 | w := httptest.NewRecorder() |
| 171 | |
| 172 | srv.Router().ServeHTTP(w, req) |
| 173 | |
| 174 | assert.Equal(t, http.StatusOK, w.Code) |
| 175 | assert.Contains(t, w.Body.String(), "total_agents") |
| 176 | }) |
| 177 | |
| 178 | // 4. 从 Pool 中移除 Agent |
| 179 | t.Run("RemoveAgentFromPool", func(t *testing.T) { |
| 180 | req := httptest.NewRequest(http.MethodDelete, "/v1/pool/agents/"+agentID, nil) |