uiSmokeConfig.mjs
3.59 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
import { FIXED_CONTEXT } from '../e2e/testConfig.mjs'
/**
* 读取订单 UI 冒烟配置,并将旧 ORDER_UI_PORT 兼容为自动化端口。
*
* @param {NodeJS.ProcessEnv|Record<string, string|undefined>} env 运行时环境变量。
* @returns {object} 尚未执行安全校验的 UI 冒烟配置。
*/
export const loadUiSmokeConfig = (env = process.env) => {
const automationPortText =
env.ORDER_UI_AUTOMATION_PORT?.trim() ||
env.ORDER_UI_PORT?.trim() ||
'9420'
const servicePortText = env.ORDER_UI_SERVICE_PORT?.trim() || ''
const automationPort = Number(automationPortText)
const servicePort = servicePortText ? Number(servicePortText) : null
return {
enabled: env.RUN_ORDER_UI_E2E === '1',
launchDevTools: env.RUN_ORDER_UI_LAUNCH === '1',
cliPath: env.ORDER_UI_CLI_PATH?.trim() || '',
projectPath: env.ORDER_UI_PROJECT_PATH?.trim() || process.cwd(),
automationPort,
servicePort,
// 兼容旧页面驱动;port 始终表示 miniprogram-automator 端口。
port: automationPort,
wsEndpoint:
env.ORDER_UI_WS_ENDPOINT?.trim() ||
`ws://127.0.0.1:${automationPortText}`,
mobile: env.ORDER_E2E_MOBILE?.trim() || '',
code: env.ORDER_E2E_CODE?.trim() || '',
parkName: env.ORDER_E2E_YQ_NAME?.trim() || FIXED_CONTEXT.yqName,
storeName: env.ORDER_E2E_STORE_NAME?.trim() || FIXED_CONTEXT.storeName,
sceneName: env.ORDER_E2E_SCENE_NAME?.trim() || FIXED_CONTEXT.sceneName
}
}
/**
* 校验订单 UI 冒烟的固定上下文、凭据和本地双端口配置。
*
* @param {NodeJS.ProcessEnv|Record<string, string|undefined>} env 运行时环境变量。
* @returns {object} 可安全用于测试环境的 UI 冒烟配置。
*/
export const requireUiSmokeConfig = (env = process.env) => {
const config = loadUiSmokeConfig(env)
const missing = []
if (!config.enabled) missing.push('RUN_ORDER_UI_E2E')
if (!config.mobile) missing.push('ORDER_E2E_MOBILE')
if (!config.code) missing.push('ORDER_E2E_CODE')
if (config.launchDevTools && !config.cliPath) missing.push('ORDER_UI_CLI_PATH')
if (config.launchDevTools && config.servicePort === null) {
missing.push('ORDER_UI_SERVICE_PORT')
}
if (missing.length) {
throw new Error(`Missing UI smoke runtime values: ${missing.join(', ')}`)
}
if (
config.parkName !== FIXED_CONTEXT.yqName ||
config.storeName !== FIXED_CONTEXT.storeName ||
config.sceneName !== FIXED_CONTEXT.sceneName
) {
throw new Error('UI smoke only allows the approved fixed context')
}
if (
!Number.isInteger(config.automationPort) ||
config.automationPort < 1024 ||
config.automationPort > 65535
) {
throw new Error(
'ORDER_UI_AUTOMATION_PORT must be an integer from 1024 to 65535'
)
}
if (
config.servicePort !== null &&
(!Number.isInteger(config.servicePort) ||
config.servicePort < 1024 ||
config.servicePort > 65535)
) {
throw new Error(
'ORDER_UI_SERVICE_PORT must be an integer from 1024 to 65535'
)
}
if (config.servicePort === config.automationPort) {
throw new Error('ORDER UI service and automation ports must differ')
}
let endpoint
try {
endpoint = new URL(config.wsEndpoint)
} catch {
throw new Error('ORDER_UI_WS_ENDPOINT must be a loopback ws endpoint')
}
if (
endpoint.protocol !== 'ws:' ||
!['127.0.0.1', 'localhost', '::1'].includes(endpoint.hostname)
) {
throw new Error('ORDER_UI_WS_ENDPOINT must be a loopback ws endpoint')
}
if (Number(endpoint.port || 80) !== config.automationPort) {
throw new Error('ORDER_UI_WS_ENDPOINT port must match automation port')
}
return config
}