@be-link/smart-test 1.0.1-beta.28 → 1.0.1-beta.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -101,11 +101,12 @@ export async function mockAddressApis(page: Page) {
101
101
  });
102
102
  });
103
103
 
104
- // 更新地址 - 基于数据源生成更新后的数据
104
+ // 更新地址 - 更新数据源并返回更新后的数据
105
105
  await page.route('**/api/address/update', async (route) => {
106
106
  const postData = route.request().postDataJSON();
107
107
  const id = postData?.id;
108
- const original = addressDatabase.find((addr) => addr.id === id) || addressDatabase[0];
108
+ const index = addressDatabase.findIndex((addr) => addr.id === id);
109
+ const original = index !== -1 ? addressDatabase[index] : addressDatabase[0];
109
110
 
110
111
  // 合并原数据和更新数据,模拟真实更新
111
112
  const updated = {
@@ -114,6 +115,11 @@ export async function mockAddressApis(page: Page) {
114
115
  updatedAt: Date.now(),
115
116
  };
116
117
 
118
+ // 【关键】将更新后的数据回写到数据源
119
+ if (index !== -1) {
120
+ addressDatabase[index] = updated;
121
+ }
122
+
117
123
  await route.fulfill({
118
124
  status: 200,
119
125
  headers: jsonHeaders,
@@ -125,7 +131,7 @@ export async function mockAddressApis(page: Page) {
125
131
  });
126
132
  });
127
133
 
128
- // 新增地址 - 基于请求生成新数据
134
+ // 新增地址 - 添加到数据源并返回新数据
129
135
  await page.route('**/api/address/create', async (route) => {
130
136
  const postData = route.request().postDataJSON();
131
137
  const newAddress = {
@@ -136,6 +142,9 @@ export async function mockAddressApis(page: Page) {
136
142
  updatedAt: Date.now(),
137
143
  };
138
144
 
145
+ // 【关键】将新数据添加到数据源
146
+ addressDatabase.push(newAddress);
147
+
139
148
  await route.fulfill({
140
149
  status: 200,
141
150
  headers: jsonHeaders,
@@ -147,8 +156,17 @@ export async function mockAddressApis(page: Page) {
147
156
  });
148
157
  });
149
158
 
150
- // 删除地址 - 返回成功
159
+ // 删除地址 - 从数据源移除并返回成功
151
160
  await page.route('**/api/address/delete', async (route) => {
161
+ const postData = route.request().postDataJSON();
162
+ const id = postData?.id;
163
+
164
+ // 【关键】从数据源中移除数据
165
+ const index = addressDatabase.findIndex((addr) => addr.id === id);
166
+ if (index !== -1) {
167
+ addressDatabase.splice(index, 1);
168
+ }
169
+
152
170
  await route.fulfill({
153
171
  status: 200,
154
172
  headers: jsonHeaders,
@@ -176,5 +194,6 @@ export async function mockAddressApis(page: Page) {
176
194
  - 只返回代码,不要包含额外的解释文本
177
195
  - 使用 TypeScript 代码块格式返回
178
196
  - **关键**:识别关联 API,使用共享数据源
179
- - **关键**:更新/新增 API 要能读取请求参数并动态生成响应
197
+ - **关键**:更新/新增/删除 API 必须修改数据源(回写、添加、移除),而不仅仅是返回响应
180
198
  - 确保列表数据和单条数据使用同一数据源
199
+ - 确保 CRUD 操作后,再次查询列表能看到变化
@@ -50,18 +50,23 @@ await page.route('**/api/address/*', async (route) => {
50
50
  });
51
51
  ```
52
52
 
53
- **更新 API**:基于请求体和数据源生成更新后的数据
53
+ **更新 API**:基于请求体更新数据源,并返回更新后的数据
54
54
 
55
55
  ```typescript
56
56
  await page.route('**/api/address/update', async (route) => {
57
- const request = route.request();
58
- const postData = request.postDataJSON();
57
+ const postData = route.request().postDataJSON();
59
58
  const id = postData?.id;
60
- const original = addressDatabase.find((addr) => addr.id === id) || addressDatabase[0];
59
+ const index = addressDatabase.findIndex((addr) => addr.id === id);
60
+ const original = index !== -1 ? addressDatabase[index] : addressDatabase[0];
61
61
 
62
62
  // 合并原数据和更新数据
63
63
  const updated = { ...original, ...postData, updatedAt: Date.now() };
64
64
 
65
+ // 【关键】将更新后的数据回写到数据源
66
+ if (index !== -1) {
67
+ addressDatabase[index] = updated;
68
+ }
69
+
65
70
  await route.fulfill({
66
71
  status: 200,
67
72
  headers: jsonHeaders,
@@ -70,7 +75,7 @@ await page.route('**/api/address/update', async (route) => {
70
75
  });
71
76
  ```
72
77
 
73
- **新增 API**:基于请求体生成新数据
78
+ **新增 API**:基于请求体生成新数据,并添加到数据源
74
79
 
75
80
  ```typescript
76
81
  await page.route('**/api/address/create', async (route) => {
@@ -82,6 +87,9 @@ await page.route('**/api/address/create', async (route) => {
82
87
  updatedAt: Date.now(),
83
88
  };
84
89
 
90
+ // 【关键】将新数据添加到数据源
91
+ addressDatabase.push(newItem);
92
+
85
93
  await route.fulfill({
86
94
  status: 200,
87
95
  headers: jsonHeaders,
@@ -90,10 +98,19 @@ await page.route('**/api/address/create', async (route) => {
90
98
  });
91
99
  ```
92
100
 
93
- **删除 API**:返回成功响应
101
+ **删除 API**:从数据源中移除数据,返回成功响应
94
102
 
95
103
  ```typescript
96
104
  await page.route('**/api/address/delete', async (route) => {
105
+ const postData = route.request().postDataJSON();
106
+ const id = postData?.id;
107
+
108
+ // 【关键】从数据源中移除数据
109
+ const index = addressDatabase.findIndex((addr) => addr.id === id);
110
+ if (index !== -1) {
111
+ addressDatabase.splice(index, 1);
112
+ }
113
+
97
114
  await route.fulfill({
98
115
  status: 200,
99
116
  headers: jsonHeaders,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@be-link/smart-test",
3
- "version": "1.0.1-beta.28",
3
+ "version": "1.0.1-beta.29",
4
4
  "description": "AI-powered visual testing for Playwright - 基于 AI 视觉模型的 Playwright 测试工具",
5
5
  "homepage": "https://github.com/snowmountain-top/be-link",
6
6
  "author": "shian",