|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/suite" |
| 14 | +) |
| 15 | + |
| 16 | +type IndexEventIntegrationTestSuite struct { |
| 17 | + BaseIntegrationTestSuite |
| 18 | +} |
| 19 | + |
| 20 | +type indexEventTestCase struct { |
| 21 | + name string |
| 22 | + workspace string |
| 23 | + data []map[string]interface{} |
| 24 | + wantProcessTime time.Duration |
| 25 | + validateIndex func(t *testing.T, indexes []map[string]interface{}) |
| 26 | +} |
| 27 | + |
| 28 | +func (s *IndexEventIntegrationTestSuite) TestPublishEvent() { |
| 29 | + // 定义测试用例表 |
| 30 | + testCases := []indexEventTestCase{ |
| 31 | + { |
| 32 | + name: "打开工作区", |
| 33 | + workspace: s.workspacePath, |
| 34 | + data: []map[string]interface{}{ |
| 35 | + { |
| 36 | + "eventType": "open_workspace", |
| 37 | + "eventTime": "2025-07-28 20:47:00", |
| 38 | + }, |
| 39 | + }, |
| 40 | + wantProcessTime: time.Second * 15, |
| 41 | + validateIndex: func(t *testing.T, indexes []map[string]interface{}) {}, |
| 42 | + }, |
| 43 | + |
| 44 | + { |
| 45 | + name: "删除文件", |
| 46 | + data: []map[string]interface{}{ |
| 47 | + { |
| 48 | + "eventType": "delete_file", |
| 49 | + "eventTime": "2025-07-28 20:47:00", |
| 50 | + "sourcePath": s.workspacePath, |
| 51 | + "targetPath": s.workspacePath, |
| 52 | + }, |
| 53 | + }, |
| 54 | + wantProcessTime: time.Second * 15, |
| 55 | + validateIndex: func(t *testing.T, indexes []map[string]interface{}) {}, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "删除文件夹", |
| 59 | + workspace: s.workspacePath, |
| 60 | + data: []map[string]interface{}{ |
| 61 | + { |
| 62 | + "eventType": "delete_file", |
| 63 | + "eventTime": "2025-07-28 20:47:00", |
| 64 | + "sourcePath": s.workspacePath, |
| 65 | + "targetPath": s.workspacePath, |
| 66 | + }, |
| 67 | + }, |
| 68 | + wantProcessTime: time.Second * 15, |
| 69 | + validateIndex: func(t *testing.T, indexes []map[string]interface{}) {}, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "新增文件", |
| 73 | + workspace: s.workspacePath, |
| 74 | + data: []map[string]interface{}{ |
| 75 | + { |
| 76 | + "eventType": "add_file", |
| 77 | + "eventTime": "2025-07-28 20:47:00", |
| 78 | + "sourcePath": s.workspacePath, |
| 79 | + "targetPath": s.workspacePath, |
| 80 | + }, |
| 81 | + }, |
| 82 | + wantProcessTime: time.Second * 15, |
| 83 | + validateIndex: func(t *testing.T, indexes []map[string]interface{}) {}, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "修改文件", |
| 87 | + workspace: s.workspacePath, |
| 88 | + data: []map[string]interface{}{ |
| 89 | + { |
| 90 | + "eventType": "modify_file", |
| 91 | + "eventTime": "2025-07-28 20:47:00", |
| 92 | + "sourcePath": s.workspacePath, |
| 93 | + "targetPath": s.workspacePath, |
| 94 | + }, |
| 95 | + }, |
| 96 | + wantProcessTime: time.Second * 15, |
| 97 | + validateIndex: func(t *testing.T, indexes []map[string]interface{}) {}, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "重命名文件", |
| 101 | + workspace: s.workspacePath, |
| 102 | + data: []map[string]interface{}{ |
| 103 | + { |
| 104 | + "eventType": "rename_file", |
| 105 | + "eventTime": "2025-07-28 20:47:00", |
| 106 | + "sourcePath": s.workspacePath, |
| 107 | + "targetPath": s.workspacePath, |
| 108 | + }, |
| 109 | + }, |
| 110 | + wantProcessTime: time.Second * 15, |
| 111 | + validateIndex: func(t *testing.T, indexes []map[string]interface{}) {}, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "重命名文件夹", |
| 115 | + workspace: s.workspacePath, |
| 116 | + data: []map[string]interface{}{ |
| 117 | + { |
| 118 | + "eventType": "rename_file", |
| 119 | + "eventTime": "2025-07-28 20:47:00", |
| 120 | + "sourcePath": s.workspacePath, |
| 121 | + "targetPath": s.workspacePath, |
| 122 | + }, |
| 123 | + }, |
| 124 | + wantProcessTime: time.Second * 15, |
| 125 | + validateIndex: func(t *testing.T, indexes []map[string]interface{}) {}, |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + // 执行表格驱动测试 |
| 130 | + for _, tc := range testCases { |
| 131 | + s.T().Run(tc.name, func(t *testing.T) { |
| 132 | + // 发布事件 |
| 133 | + |
| 134 | + // 等待一定事件 |
| 135 | + |
| 136 | + // 查询索引,达到期望状态 |
| 137 | + |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestIndexEventIntegrationTestSuite(t *testing.T) { |
| 143 | + suite.Run(t, new(IndexEventIntegrationTestSuite)) |
| 144 | +} |
| 145 | + |
| 146 | +// publishEvent 发布索引事件 |
| 147 | +func (s *IndexEventIntegrationTestSuite) publishEvent(workspace string, data any) error { |
| 148 | + var resp *http.Response |
| 149 | + var err error |
| 150 | + |
| 151 | + // 准备请求体 |
| 152 | + reqBody := map[string]interface{}{ |
| 153 | + "workspace": workspace, |
| 154 | + "data": data, |
| 155 | + } |
| 156 | + |
| 157 | + // 序列化请求体 |
| 158 | + jsonData, err := json.Marshal(reqBody) |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + // 创建HTTP请求 |
| 164 | + req, err := s.CreatePOSTRequest(s.baseURL+"/codebase-indexer/api/v1/events", jsonData) |
| 165 | + if err != nil { |
| 166 | + return err |
| 167 | + } |
| 168 | + |
| 169 | + // 发送请求 |
| 170 | + resp, err = s.SendRequest(req) |
| 171 | + |
| 172 | + s.Require().NoError(err) |
| 173 | + defer resp.Body.Close() |
| 174 | + |
| 175 | + // 验证响应状态码 |
| 176 | + if resp.StatusCode != http.StatusOK { |
| 177 | + return fmt.Errorf("unexpected status code: %d", resp.StatusCode) |
| 178 | + } |
| 179 | + // 读取响应体 |
| 180 | + body, err := io.ReadAll(resp.Body) |
| 181 | + s.Require().NoError(err) |
| 182 | + |
| 183 | + // 解析响应JSON |
| 184 | + var response map[string]interface{} |
| 185 | + if err = json.Unmarshal(body, &response); err != nil { |
| 186 | + return err |
| 187 | + } |
| 188 | + |
| 189 | + s.Equal("ok", response["message"]) |
| 190 | + if response["message"] != "ok" { |
| 191 | + return fmt.Errorf("expected message `ok` but got `%s`", response["message"]) |
| 192 | + } |
| 193 | + return nil |
| 194 | +} |
| 195 | + |
| 196 | +// 获取全部索引 |
| 197 | +func (s *IndexEventIntegrationTestSuite) dumpIndex() ([]map[string]interface{}, error) { |
| 198 | + // 构建请求URL |
| 199 | + reqURL, err := url.Parse(s.baseURL + "/codebase-indexer/api/v1/index/export") |
| 200 | + s.Require().NoError(err) |
| 201 | + |
| 202 | + // 添加查询参数 |
| 203 | + q := reqURL.Query() |
| 204 | + q.Add("codebasePath", s.workspacePath) |
| 205 | + |
| 206 | + reqURL.RawQuery = q.Encode() |
| 207 | + |
| 208 | + // 创建HTTP请求 |
| 209 | + req, err := s.CreateGETRequest(reqURL.String()) |
| 210 | + s.Require().NoError(err) |
| 211 | + |
| 212 | + // 发送请求 |
| 213 | + resp, err := s.SendRequest(req) |
| 214 | + s.Require().NoError(err) |
| 215 | + defer resp.Body.Close() |
| 216 | + body, err := io.ReadAll(resp.Body) |
| 217 | + |
| 218 | + // 先将读取到的body转换为字符串 |
| 219 | + bodyStr := string(body) |
| 220 | + |
| 221 | + // 按行分割内容 |
| 222 | + lines := strings.Split(bodyStr, "\n") |
| 223 | + var indexes []map[string]interface{} |
| 224 | + for _, line := range lines { |
| 225 | + var indexLine map[string]interface{} |
| 226 | + err := json.Unmarshal([]byte(line), &indexLine) |
| 227 | + if err != nil { |
| 228 | + return nil, err |
| 229 | + } |
| 230 | + indexes = append(indexes, indexLine) |
| 231 | + } |
| 232 | + return indexes, nil |
| 233 | +} |
0 commit comments