preflite 1.1.1 → 1.1.3

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.
@@ -61,6 +61,156 @@ function parseScriptVars(raw) {
61
61
  }
62
62
  return { ok: true, value: out };
63
63
  }
64
+ function parseSingleMockRule(o, path) {
65
+ const urlPattern = typeof o.urlPattern === 'string' && o.urlPattern.trim() ? o.urlPattern.trim() : "";
66
+ const urlRegex = typeof o.urlRegex === 'string' && o.urlRegex.trim() ? o.urlRegex.trim() : "";
67
+ if (!urlPattern && !urlRegex)
68
+ return { ok: false, message: `${path}.urlPattern 或 urlRegex 必填其一` };
69
+ if (urlPattern && urlPattern.length > 1000)
70
+ return { ok: false, message: `${path}.urlPattern 过长` };
71
+ if (urlRegex) {
72
+ try {
73
+ new RegExp(urlRegex);
74
+ }
75
+ catch {
76
+ return { ok: false, message: `${path}.urlRegex 无效` };
77
+ }
78
+ }
79
+ const queryParams = o.queryParams != null && typeof o.queryParams === 'object' && !Array.isArray(o.queryParams) ? Object.fromEntries(Object.entries(o.queryParams).filter(([, v]) => typeof v === 'string').map(([k, v]) => [k, v])) : undefined;
80
+ const method = typeof o.method === 'string' ? o.method.trim().toUpperCase() : '';
81
+ if (method && !['GET', 'POST', 'PUT', 'DELETE', 'PATCH'].includes(method))
82
+ return { ok: false, message: `${path}.method 须为 GET|POST|PUT|DELETE|PATCH` };
83
+ if (!Array.isArray(o.responses) || o.responses.length === 0)
84
+ return { ok: false, message: `${path}.responses 须为非空数组` };
85
+ if (o.responses.length > 50)
86
+ return { ok: false, message: `${path}.responses 超过上限 50` };
87
+ const responses = [];
88
+ const bodyStr = (b) => String(b);
89
+ for (let j = 0; j < o.responses.length; j++) {
90
+ const rPath = `${path}.responses[${j}]`;
91
+ const ri = o.responses[j];
92
+ if (!ri || typeof ri !== 'object' || Array.isArray(ri))
93
+ return { ok: false, message: `${rPath} 须为对象` };
94
+ const r = ri;
95
+ if (r.body == null)
96
+ return { ok: false, message: `${rPath}.body 必填` };
97
+ const body = bodyStr(r.body);
98
+ if (body.length > 1_000_000)
99
+ return { ok: false, message: `${rPath}.body 过长` };
100
+ const status = r.status != null ? Number(r.status) : 200;
101
+ if (!Number.isFinite(status) || status < 100 || status > 599)
102
+ return { ok: false, message: `${rPath}.status 须为 100~599` };
103
+ const callIndex = r.callIndex != null ? Number(r.callIndex) : undefined;
104
+ if (callIndex != null && (!Number.isFinite(callIndex) || callIndex < 1))
105
+ return { ok: false, message: `${rPath}.callIndex 须为正整数` };
106
+ const delay = r.delay != null ? Number(r.delay) : undefined;
107
+ if (delay != null && (!Number.isFinite(delay) || delay < 0 || delay > 60_000))
108
+ return { ok: false, message: `${rPath}.delay 须为 0~60000` };
109
+ responses.push({ status: Math.floor(status), body, ...(callIndex != null ? { callIndex: Math.floor(callIndex) } : {}), ...(delay != null ? { delay: Math.floor(delay) } : {}), ...(typeof r.headers === 'object' && r.headers && !Array.isArray(r.headers) && Object.keys(r.headers).length > 0 ? { headers: Object.fromEntries(Object.entries(r.headers).filter(([, v]) => typeof v === 'string').map(([k, v]) => [k, v])) } : {}), ...(typeof r.requestBodyMatch === 'object' && r.requestBodyMatch && !Array.isArray(r.requestBodyMatch) && Object.keys(r.requestBodyMatch).length > 0 ? { requestBodyMatch: Object.fromEntries(Object.entries(r.requestBodyMatch).filter(([, v]) => typeof v === 'string').map(([k, v]) => [k, v])) } : {}) });
110
+ }
111
+ const description = typeof o.description === 'string' && o.description.trim() ? o.description.trim().slice(0, 500) : undefined;
112
+ return { ok: true, value: { ...(urlPattern ? { urlPattern } : {}), ...(urlRegex ? { urlRegex } : {}), ...(queryParams && Object.keys(queryParams).length > 0 ? { queryParams } : {}), ...(method ? { method: method } : {}), responses, ...(description ? { description } : {}) } };
113
+ }
114
+ function parseNetworkMocks(raw) {
115
+ if (raw === undefined || raw === null)
116
+ return { ok: true, value: [] };
117
+ if (!Array.isArray(raw))
118
+ return { ok: false, message: 'visualFlow.networkMocks 须为数组' };
119
+ if (raw.length > 50)
120
+ return { ok: false, message: 'mock 规则数量超过上限 50' };
121
+ const out = [];
122
+ for (let i = 0; i < raw.length; i++) {
123
+ const path = `networkMocks[${i}]`;
124
+ const item = raw[i];
125
+ if (!item || typeof item !== 'object' || Array.isArray(item)) {
126
+ return { ok: false, message: `${path} 须为对象` };
127
+ }
128
+ const o = item;
129
+ const urlPattern = typeof o.urlPattern === 'string' && o.urlPattern.trim() ? o.urlPattern.trim() : "";
130
+ const urlRegex = typeof o.urlRegex === 'string' && o.urlRegex.trim() ? o.urlRegex.trim() : "";
131
+ if (!urlPattern && !urlRegex)
132
+ return { ok: false, message: `${path}.urlPattern 或 urlRegex 必填其一` };
133
+ if (urlPattern && urlPattern.length > 1000)
134
+ return { ok: false, message: `${path}.urlPattern 过长` };
135
+ if (urlRegex) {
136
+ try {
137
+ new RegExp(urlRegex);
138
+ }
139
+ catch {
140
+ return { ok: false, message: `${path}.urlRegex 不是有效的正则表达式` };
141
+ }
142
+ }
143
+ const queryParams = o.queryParams != null && typeof o.queryParams === 'object' && !Array.isArray(o.queryParams)
144
+ ? Object.fromEntries(Object.entries(o.queryParams).filter(([, v]) => typeof v === "string").map(([k, v]) => [k, v]))
145
+ : undefined;
146
+ const method = typeof o.method === 'string' ? o.method.trim().toUpperCase() : '';
147
+ if (method && !['GET', 'POST', 'PUT', 'DELETE', 'PATCH'].includes(method)) {
148
+ return { ok: false, message: `${path}.method 须为 GET|POST|PUT|DELETE|PATCH` };
149
+ }
150
+ if (!Array.isArray(o.responses) || o.responses.length === 0) {
151
+ return { ok: false, message: `${path}.responses 须为非空数组` };
152
+ }
153
+ if (o.responses.length > 50) {
154
+ return { ok: false, message: `${path}.responses 数量超过上限 50` };
155
+ }
156
+ const responses = [];
157
+ for (let j = 0; j < o.responses.length; j++) {
158
+ const rPath = `${path}.responses[${j}]`;
159
+ const rItem = o.responses[j];
160
+ if (!rItem || typeof rItem !== 'object' || Array.isArray(rItem)) {
161
+ return { ok: false, message: `${rPath} 须为对象` };
162
+ }
163
+ const r = rItem;
164
+ if (r.body == null)
165
+ return { ok: false, message: `${rPath}.body 必填` };
166
+ const body = String(r.body);
167
+ if (body.length > 1_000_000)
168
+ return { ok: false, message: `${rPath}.body 过长(上限 1MB)` };
169
+ const status = r.status != null ? Number(r.status) : 200;
170
+ if (!Number.isFinite(status) || status < 100 || status > 599) {
171
+ return { ok: false, message: `${rPath}.status 须为 100~599` };
172
+ }
173
+ const callIndex = r.callIndex != null ? Number(r.callIndex) : undefined;
174
+ if (callIndex != null && (!Number.isFinite(callIndex) || callIndex < 1)) {
175
+ return { ok: false, message: `${rPath}.callIndex 须为正整数` };
176
+ }
177
+ const delay = r.delay != null ? Number(r.delay) : undefined;
178
+ if (delay != null && (!Number.isFinite(delay) || delay < 0 || delay > 60_000)) {
179
+ return { ok: false, message: `${rPath}.delay 须为 0~60000` };
180
+ }
181
+ const headers = r.headers != null && typeof r.headers === 'object' && !Array.isArray(r.headers)
182
+ ? Object.fromEntries(Object.entries(r.headers)
183
+ .filter(([, v]) => typeof v === 'string')
184
+ .map(([k, v]) => [k, v]))
185
+ : undefined;
186
+ const requestBodyMatch = r.requestBodyMatch != null && typeof r.requestBodyMatch === 'object' && !Array.isArray(r.requestBodyMatch)
187
+ ? Object.fromEntries(Object.entries(r.requestBodyMatch)
188
+ .filter(([, v]) => typeof v === 'string')
189
+ .map(([k, v]) => [k, v]))
190
+ : undefined;
191
+ responses.push({
192
+ status: Math.floor(status),
193
+ body,
194
+ ...(callIndex != null ? { callIndex: Math.floor(callIndex) } : {}),
195
+ ...(delay != null ? { delay: Math.floor(delay) } : {}),
196
+ ...(headers && Object.keys(headers).length > 0 ? { headers } : {}),
197
+ ...(requestBodyMatch && Object.keys(requestBodyMatch).length > 0 ? { requestBodyMatch } : {}),
198
+ });
199
+ }
200
+ const description = typeof o.description === 'string' && o.description.trim()
201
+ ? o.description.trim().slice(0, 500)
202
+ : undefined;
203
+ out.push({
204
+ ...(urlPattern ? { urlPattern } : {}),
205
+ ...(urlRegex ? { urlRegex } : {}),
206
+ ...(queryParams && Object.keys(queryParams).length > 0 ? { queryParams } : {}),
207
+ ...(method ? { method: method } : {}),
208
+ responses,
209
+ ...(description ? { description } : {}),
210
+ });
211
+ }
212
+ return { ok: true, value: out };
213
+ }
64
214
  function isNonEmptyString(v) {
65
215
  return typeof v === 'string' && v.trim().length > 0;
66
216
  }
@@ -321,6 +471,23 @@ function parseStep(raw, path) {
321
471
  },
322
472
  };
323
473
  }
474
+ case 'setMock': {
475
+ if (!o.rule || typeof o.rule !== 'object' || Array.isArray(o.rule)) {
476
+ return { ok: false, message: `${path}.rule 须为 NetworkMockRule 对象` };
477
+ }
478
+ const mockParsed = parseSingleMockRule(o.rule, `${path}.rule`);
479
+ if (!mockParsed.ok)
480
+ return mockParsed;
481
+ return { ok: true, step: { type: 'setMock', rule: mockParsed.value } };
482
+ }
483
+ case 'removeMock': {
484
+ if (!isNonEmptyString(o.urlPattern))
485
+ return { ok: false, message: `${path}.urlPattern 必填` };
486
+ return { ok: true, step: { type: 'removeMock', urlPattern: o.urlPattern.trim() } };
487
+ }
488
+ case 'clearMocks': {
489
+ return { ok: true, step: { type: 'clearMocks' } };
490
+ }
324
491
  case 'callScript': {
325
492
  if (!isNonEmptyString(o.targetTestCaseId)) {
326
493
  return { ok: false, message: `${path}.targetTestCaseId 必填` };
@@ -591,6 +758,9 @@ export function tryParseVisualFlow(raw) {
591
758
  const scriptVarsParsed = parseScriptVars(o.scriptVars);
592
759
  if (!scriptVarsParsed.ok)
593
760
  return scriptVarsParsed;
761
+ const networkMocksParsed = parseNetworkMocks(o.networkMocks);
762
+ if (!networkMocksParsed.ok)
763
+ return networkMocksParsed;
594
764
  const steps = [];
595
765
  for (let i = 0; i < o.steps.length; i++) {
596
766
  const r = parseStep(o.steps[i], `steps[${i}]`);
@@ -612,6 +782,7 @@ export function tryParseVisualFlow(raw) {
612
782
  version: VISUAL_FLOW_VERSION,
613
783
  ...(scriptVarsParsed.value.length ? { scriptVars: scriptVarsParsed.value } : {}),
614
784
  steps,
785
+ ...(networkMocksParsed.value.length ? { networkMocks: networkMocksParsed.value } : {}),
615
786
  },
616
787
  };
617
788
  }
@@ -9,8 +9,9 @@
9
9
 
10
10
  | 字段 | 必填 | 说明 |
11
11
  | ------------ | --- | ------------------------------------ |
12
- | `version` | 是 | 固定为数字 `1`,其它值保存失败。 |
12
+ | `version` | 是 | 固定为数字 `2`,其它值保存失败。 |
13
13
  | `scriptVars` | 否 | 执行前由人填的变量声明数组;步骤文案里用 `{{变量名}}` 引用。 |
14
+ | `networkMocks` | 否 | 网络 mock 规则数组,在测试开始前启动代理并配置到设备。每条规则描述一个 API 的 URL 匹配与 mock 响应序列。 |
14
15
  | `steps` | 是 | 顶层步骤数组,按顺序执行;**展开后总条数 ≤ 500**(含子步骤)。 |
15
16
 
16
17
 
@@ -25,6 +26,70 @@
25
26
  | `scope` | 否 | `global`、`local` 或 `temp`;缺省按平台约定。 |
26
27
 
27
28
 
29
+ ### 1.2 `networkMocks[]` 每项
30
+
31
+ | 字段 | 必填 | 说明 |
32
+ | -------------- | --- | -------------------------------------- |
33
+ | `urlPattern` | 是 | 请求 URL 的子串匹配(`includes` 语义)。首个匹配的规则生效。 |
34
+ | `method` | 否 | HTTP 方法:`GET`、`POST`、`PUT`、`DELETE` 或 `PATCH`。不填则匹配任意方法。 |
35
+ | `responses` | 是 | 非空响应序列数组,按顺序匹配第一项满足条件的响应。 |
36
+ | `description` | 否 | 给人看的说明。 |
37
+
38
+ #### `responses[]` 每项
39
+
40
+ | 字段 | 必填 | 说明 |
41
+ | ------------------ | --- | -------------------------------------- |
42
+ | `body` | 是 | 响应体字符串(通常为 JSON)。上限 1MB。 |
43
+ | `status` | 否 | HTTP 状态码,默认 200。范围 100~599。 |
44
+ | `callIndex` | 否 | 仅第 n 次调用时匹配(1-based)。用于实现状态性 mock:首次返回错误,二次返回成功。 |
45
+ | `requestBodyMatch` | 否 | 键值对映射,必须在请求体的 JSON 中存在且值匹配才会命中。 |
46
+ | `headers` | 否 | 额外的响应头。Content-Type 默认为 `application/json; charset=utf-8`。 |
47
+ | `delay` | 否 | 响应延迟毫秒数。范围 0~60000。 |
48
+
49
+ #### 匹配规则
50
+
51
+ 1. 遍历 `networkMocks`,找到第一条 `urlPattern` 是请求 URL 子串的规则。
52
+ 2. 递增该规则的调用计数器。
53
+ 3. 按顺序遍历 `responses`,找到第一项满足 `callIndex` 和 `requestBodyMatch` 的响应并返回。
54
+ 4. 若无匹配响应或请求不匹配任何规则,透明转发到真实服务器。
55
+
56
+ #### 示例
57
+
58
+ ```json
59
+ {
60
+ "networkMocks": [
61
+ {
62
+ "urlPattern": "getMafangRosterNewFlowSwitch",
63
+ "description": "启用码放新流程",
64
+ "responses": [
65
+ {
66
+ "status": 200,
67
+ "body": "{\"code\":200,\"data\":{\"newFlowEnabled\":true},\"subcode\":200}"
68
+ }
69
+ ]
70
+ },
71
+ {
72
+ "urlPattern": "copyWorkScheduleGroup",
73
+ "method": "POST",
74
+ "description": "日复制先弹确认再成功",
75
+ "responses": [
76
+ {
77
+ "callIndex": 1,
78
+ "status": 200,
79
+ "body": "{\"code\":\"WORK_SCHEDULE_PARTITION_MAFANG_COPY_SKIP_CONFIRM\",\"data\":{\"blockedCount\":2}}"
80
+ },
81
+ {
82
+ "callIndex": 2,
83
+ "requestBodyMatch": {"mafangSkipConfirmed": "true"},
84
+ "status": 200,
85
+ "body": "{\"code\":200,\"data\":{\"flag\":true}}"
86
+ }
87
+ ]
88
+ }
89
+ ]
90
+ }
91
+ ```
92
+
28
93
  ---
29
94
 
30
95
  ## 2. 步骤类型与设计原则
@@ -100,7 +165,10 @@
100
165
 
101
166
  ## 4. 易错校验(生成后自查)
102
167
 
103
- - `version !== 1` → 失败。
168
+ - `version !== 2` → 失败。
169
+ - `networkMocks` 超过 50 条规则或单条规则的 `responses` 超过 50 项 → 失败。
170
+ - `networkMocks[].urlPattern` 为空或过长(>1000)→ 失败。
171
+ - `networkMocks[].responses` 为空数组 → 失败。
104
172
  - `if` / `ifDeviceType` 的 `thenSteps` 为空,或 `whileLoop` / `forLoop` 的 `bodySteps` 为空 → 失败。
105
173
  - `sleep.ms`、`whileLoop.maxIterations`、`forLoop.count` 超出上表范围 → 失败。
106
174
  - `callScript.targetTestCaseId` 非 24 位 hex,或 `scopeId` 不符合 `sub`+12hex → 失败。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "preflite",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Preflight — Local mobile AI testing via MCP. AI-assisted testing on real Android/iOS/Harmony devices.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",