-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathaction-validator.js
More file actions
412 lines (352 loc) · 9.98 KB
/
action-validator.js
File metadata and controls
412 lines (352 loc) · 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Action组件功能验证脚本
// 用于验证Action组件系统的各项功能是否正常工作
import { Actions, actionStateManager, globalConfig } from '@/components/actions'
/**
* 验证Action组件系统的基本功能
*/
export class ActionValidator {
constructor() {
this.results = []
this.errors = []
}
/**
* 记录测试结果
*/
log(test, success, message, data = null) {
const result = {
test,
success,
message,
data,
timestamp: new Date().toISOString()
}
this.results.push(result)
if (success) {
console.log(`✅ ${test}: ${message}`, data)
} else {
console.error(`❌ ${test}: ${message}`, data)
this.errors.push(result)
}
}
/**
* 验证组件导入
*/
async validateImports() {
try {
// 验证Actions对象
if (typeof Actions !== 'object') {
throw new Error('Actions对象未正确导入')
}
// 验证actionStateManager
if (typeof actionStateManager !== 'object') {
throw new Error('actionStateManager未正确导入')
}
// 验证Actions方法
const requiredMethods = [
'input', 'multiInput', 'menu', 'select',
'msgBox', 'webView', 'help', 'alert',
'confirm', 'info', 'success', 'warning',
'error', 'progress'
]
for (const method of requiredMethods) {
if (typeof Actions[method] !== 'function') {
throw new Error(`Actions.${method}方法不存在`)
}
}
this.log('组件导入验证', true, '所有组件和方法导入正常')
return true
} catch (error) {
this.log('组件导入验证', false, error.message, error)
return false
}
}
/**
* 验证状态管理器
*/
async validateStateManager() {
try {
// 验证配置设置
const originalConfig = { ...globalConfig.value }
actionStateManager.updateConfig({
defaultTimeout: 5000,
debugMode: true
})
const newConfig = globalConfig.value
if (newConfig.defaultTimeout !== 5000 || !newConfig.debugMode) {
throw new Error('配置设置失败')
}
// 恢复原配置
actionStateManager.updateConfig(originalConfig)
// 验证统计信息
const stats = actionStateManager.statistics
if (typeof stats !== 'object') {
throw new Error('统计信息获取失败')
}
this.log('状态管理器验证', true, '状态管理器功能正常', { stats })
return true
} catch (error) {
this.log('状态管理器验证', false, error.message, error)
return false
}
}
/**
* 验证基础Alert功能
*/
async validateBasicAlert() {
try {
// 创建一个快速关闭的Alert
const alertPromise = Actions.alert('这是一个测试消息', '测试Alert')
// 等待Alert完成
await alertPromise
this.log('基础Alert验证', true, 'Alert功能正常')
return true
} catch (error) {
this.log('基础Alert验证', false, error.message, error)
return false
}
}
/**
* 验证输入组件配置
*/
async validateInputConfig() {
try {
// 验证输入组件的配置解析
const config = {
title: '测试输入',
message: '请输入测试内容',
placeholder: '测试占位符',
required: true,
validation: {
minLength: 3,
maxLength: 10
},
timeout: 1000
}
// 创建输入Action但立即取消
const inputPromise = Actions.input({
actionId: 'validator-input-test',
...config
})
// 等待一小段时间后取消
setTimeout(() => {
ActionStateManager.cancelAction()
}, 100)
try {
await inputPromise
} catch (error) {
if (error.type === 'cancel') {
this.log('输入组件配置验证', true, '输入组件配置解析正常')
return true
}
throw error
}
this.log('输入组件配置验证', false, '输入组件未正确取消')
return false
} catch (error) {
this.log('输入组件配置验证', false, error.message, error)
return false
}
}
/**
* 验证多输入组件配置
*/
async validateMultiInputConfig() {
try {
const config = {
title: '测试多输入',
message: '请填写测试信息',
inputs: [
{
key: 'name',
label: '姓名',
required: true
},
{
key: 'email',
label: '邮箱',
validation: { type: 'email' }
}
],
timeout: 1000
}
const multiInputPromise = Actions.multiInput({
actionId: 'validator-multi-input-test',
...config
})
setTimeout(() => {
ActionStateManager.cancelAction()
}, 100)
try {
await multiInputPromise
} catch (error) {
if (error.type === 'cancel') {
this.log('多输入组件配置验证', true, '多输入组件配置解析正常')
return true
}
throw error
}
this.log('多输入组件配置验证', false, '多输入组件未正确取消')
return false
} catch (error) {
this.log('多输入组件配置验证', false, error.message, error)
return false
}
}
/**
* 验证选择组件配置
*/
async validateSelectConfig() {
try {
const config = {
title: '测试选择',
options: [
{ key: 'option1', title: '选项1' },
{ key: 'option2', title: '选项2' }
],
multiple: false,
timeout: 1000
}
const selectPromise = Actions.select({
actionId: 'validator-select-test',
...config
})
setTimeout(() => {
ActionStateManager.cancelAction()
}, 100)
try {
await selectPromise
} catch (error) {
if (error.type === 'cancel') {
this.log('选择组件配置验证', true, '选择组件配置解析正常')
return true
}
throw error
}
this.log('选择组件配置验证', false, '选择组件未正确取消')
return false
} catch (error) {
this.log('选择组件配置验证', false, error.message, error)
return false
}
}
/**
* 验证主题切换
*/
async validateTheme() {
try {
const originalTheme = ActionStateManager.getConfig().theme
// 切换到暗色主题
ActionStateManager.setTheme('dark')
if (ActionStateManager.getConfig().theme !== 'dark') {
throw new Error('暗色主题设置失败')
}
// 切换到亮色主题
ActionStateManager.setTheme('light')
if (ActionStateManager.getConfig().theme !== 'light') {
throw new Error('亮色主题设置失败')
}
// 恢复原主题
ActionStateManager.setTheme(originalTheme)
this.log('主题切换验证', true, '主题切换功能正常')
return true
} catch (error) {
this.log('主题切换验证', false, error.message, error)
return false
}
}
/**
* 验证事件监听
*/
async validateEvents() {
try {
let eventReceived = false
// 添加事件监听器
const removeListener = ActionStateManager.on('action:show', () => {
eventReceived = true
})
// 触发一个Action
const alertPromise = Actions.alert('测试事件监听', '事件测试')
await alertPromise
// 移除监听器
removeListener()
if (!eventReceived) {
throw new Error('事件未正确触发')
}
this.log('事件监听验证', true, '事件监听功能正常')
return true
} catch (error) {
this.log('事件监听验证', false, error.message, error)
return false
}
}
/**
* 运行所有验证测试
*/
async runAllTests() {
console.log('🚀 开始Action组件系统验证...')
this.results = []
this.errors = []
const tests = [
() => this.validateImports(),
() => this.validateStateManager(),
() => this.validateBasicAlert(),
() => this.validateInputConfig(),
() => this.validateMultiInputConfig(),
() => this.validateSelectConfig(),
() => this.validateTheme(),
() => this.validateEvents()
]
let passedTests = 0
const totalTests = tests.length
for (const test of tests) {
try {
const result = await test()
if (result) passedTests++
} catch (error) {
console.error('测试执行出错:', error)
}
// 在测试之间添加小延迟
await new Promise(resolve => setTimeout(resolve, 100))
}
const summary = {
total: totalTests,
passed: passedTests,
failed: totalTests - passedTests,
success: passedTests === totalTests,
results: this.results,
errors: this.errors
}
console.log('\n📊 验证结果汇总:')
console.log(`总测试数: ${summary.total}`)
console.log(`通过: ${summary.passed}`)
console.log(`失败: ${summary.failed}`)
console.log(`成功率: ${((summary.passed / summary.total) * 100).toFixed(1)}%`)
if (summary.success) {
console.log('🎉 所有测试通过!Action组件系统功能正常。')
} else {
console.log('⚠️ 部分测试失败,请检查错误信息。')
console.log('错误详情:', this.errors)
}
return summary
}
/**
* 获取验证报告
*/
getReport() {
return {
timestamp: new Date().toISOString(),
results: this.results,
errors: this.errors,
summary: {
total: this.results.length,
passed: this.results.filter(r => r.success).length,
failed: this.errors.length
}
}
}
}
// 创建全局验证器实例
export const actionValidator = new ActionValidator()
// 便捷方法
export const validateActionSystem = () => actionValidator.runAllTests()
export default ActionValidator