package main import ( "bytes" "encoding/json" "io" "net/http" "testing" "git.x2erp.com/qdy/go-base/model/request/configreq" ) const ( agentTestAgentID = "test-agent-001" agentTestProjectID = "test-project-agent-001" agentTestDescription = "测试Agent描述" agentTestContent = `{"model": "gpt-4", "max_tokens": 4096}` agentUpdatedContent = `{"model": "gpt-4-turbo", "max_tokens": 8192}` agentBaseURL = "http://localhost:8080" ) // TestProjectAgentCRUD 测试项目Agent的增删改查 func TestProjectAgentCRUD(t *testing.T) { // 1. 创建项目Agent t.Run("CreateProjectAgent", testCreateProjectAgent) // 2. 查询单个项目Agent t.Run("GetProjectAgent", testGetProjectAgent) // 3. 查询项目Agent列表 t.Run("ListProjectAgents", testListProjectAgents) // 4. 更新项目Agent t.Run("UpdateProjectAgent", testUpdateProjectAgent) // 5. 查询更新后的项目Agent t.Run("GetUpdatedProjectAgent", testGetUpdatedProjectAgent) // 6. 删除项目Agent t.Run("DeleteProjectAgent", testDeleteProjectAgent) // 7. 验证项目Agent已删除 t.Run("VerifyProjectAgentDeleted", testVerifyProjectAgentDeleted) } func testCreateProjectAgent(t *testing.T) { httpClient := &http.Client{} url := agentBaseURL + "/api/create/config/project/agent" reqBody := configreq.ProjectAgentRequest{ AgentID: agentTestAgentID, ProjectID: agentTestProjectID, Description: agentTestDescription, Content: agentTestContent, } jsonData, err := json.Marshal(reqBody) if err != nil { t.Fatalf("JSON序列化失败: %v", err) } req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { t.Fatalf("创建请求失败: %v", err) } // 使用Basic认证 req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } if success, ok := result["success"].(bool); !ok || !success { t.Errorf("创建项目Agent失败: %v", result) } t.Logf("创建项目Agent成功: %s", string(body)) } func testGetProjectAgent(t *testing.T) { httpClient := &http.Client{} url := agentBaseURL + "/api/query/config/project/agent/" + agentTestAgentID req, err := http.NewRequest("POST", url, nil) if err != nil { t.Fatalf("创建请求失败: %v", err) } req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } if success, ok := result["success"].(bool); !ok || !success { t.Errorf("查询项目Agent失败: %v", result) } t.Logf("查询项目Agent成功: %s", string(body)) } func testListProjectAgents(t *testing.T) { httpClient := &http.Client{} url := agentBaseURL + "/api/query/config/project/agents" req, err := http.NewRequest("POST", url, nil) if err != nil { t.Fatalf("创建请求失败: %v", err) } req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } if success, ok := result["success"].(bool); !ok || !success { t.Errorf("查询项目Agent列表失败: %v", result) } t.Logf("查询项目Agent列表成功: %s", string(body)) } func testUpdateProjectAgent(t *testing.T) { httpClient := &http.Client{} url := agentBaseURL + "/api/update/config/project/agent/" + agentTestAgentID reqBody := configreq.ProjectAgentRequest{ AgentID: agentTestAgentID, ProjectID: agentTestProjectID, Description: "更新后的Agent描述", Content: agentUpdatedContent, } jsonData, err := json.Marshal(reqBody) if err != nil { t.Fatalf("JSON序列化失败: %v", err) } req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { t.Fatalf("创建请求失败: %v", err) } req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } if success, ok := result["success"].(bool); !ok || !success { t.Errorf("更新项目Agent失败: %v", result) } t.Logf("更新项目Agent成功: %s", string(body)) } func testGetUpdatedProjectAgent(t *testing.T) { httpClient := &http.Client{} url := agentBaseURL + "/api/query/config/project/agent/" + agentTestAgentID req, err := http.NewRequest("POST", url, nil) if err != nil { t.Fatalf("创建请求失败: %v", err) } req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } if success, ok := result["success"].(bool); !ok || !success { t.Errorf("查询更新后项目Agent失败: %v", result) } t.Logf("查询更新后项目Agent成功: %s", string(body)) } func testDeleteProjectAgent(t *testing.T) { httpClient := &http.Client{} url := agentBaseURL + "/api/delete/config/project/agent/" + agentTestAgentID req, err := http.NewRequest("POST", url, nil) if err != nil { t.Fatalf("创建请求失败: %v", err) } req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } if success, ok := result["success"].(bool); !ok || !success { t.Errorf("删除项目Agent失败: %v", result) } t.Logf("删除项目Agent成功: %s", string(body)) } func testVerifyProjectAgentDeleted(t *testing.T) { httpClient := &http.Client{} url := agentBaseURL + "/api/query/config/project/agent/" + agentTestAgentID req, err := http.NewRequest("POST", url, nil) if err != nil { t.Fatalf("创建请求失败: %v", err) } req.SetBasicAuth("admin", "123") req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) if err != nil { t.Fatalf("请求失败: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("读取响应失败: %v", err) } var result map[string]interface{} if err := json.Unmarshal(body, &result); err != nil { t.Fatalf("JSON解析失败: %v", err) } // 删除后查询应该返回失败或空数据 if success, ok := result["success"].(bool); ok && success { // 如果成功,检查数据是否为空 if data, ok := result["data"].(map[string]interface{}); ok && data != nil { t.Errorf("项目Agent应该已删除但仍有数据: %v", result) } } t.Logf("验证项目Agent已删除: %s", string(body)) }