live-config.mjs 4.37 KB
/**
 * @file AI/日报分包迁移的 DevTools 自动化运行时配置与安全门禁。
 */
import path from 'node:path'

export const DEVTOOLS_CLI_PATH =
	'C:\\Program Files (x86)\\Tencent\\微信web开发者工具\\cli.bat'

export const ALLOWED_SPLIT_E2E_BASE_URL =
	'https://dj.test.glsx.com.cn/rop-dj-car-boutique'

const asText = value => String(value ?? '').trim()

const isAllowedTestBaseUrl = value => {
	try {
		const actual = new URL(value)
		const allowed = new URL(ALLOWED_SPLIT_E2E_BASE_URL)
		return (
			actual.protocol === allowed.protocol &&
			actual.hostname === allowed.hostname &&
			actual.port === allowed.port &&
			actual.pathname === allowed.pathname &&
			actual.search === '' &&
			actual.hash === '' &&
			actual.username === '' &&
			actual.password === ''
		)
	} catch {
		return false
	}
}

const isValidPort = value =>
	Number.isInteger(value) && value >= 1024 && value <= 65535

/**
 * 读取分包 UI 自动化配置。凭据没有代码默认值,写操作也默认关闭。
 * @param {NodeJS.ProcessEnv|Record<string, string|undefined>} [env] 运行时变量
 * @returns {Readonly<object>} 尚未执行强校验的配置
 */
export function loadSplitUiConfig(env = process.env) {
	const automationPortText = asText(env.SPLIT_UI_AUTOMATION_PORT) || '9420'
	const servicePortText = asText(env.SPLIT_UI_SERVICE_PORT) || '32219'
	const automationPort = Number(automationPortText)
	const servicePort = Number(servicePortText)
	const phase = asText(env.SPLIT_E2E_PHASE) || 'baseline'
	return Object.freeze({
		enabled: env.RUN_SPLIT_UI_E2E === '1',
		baseUrl: asText(env.SPLIT_E2E_BASE_URL) || ALLOWED_SPLIT_E2E_BASE_URL,
		mobile: asText(env.SPLIT_E2E_MOBILE),
		code: asText(env.SPLIT_E2E_CODE),
		parkName: asText(env.SPLIT_E2E_PARK_NAME),
		storeName: asText(env.SPLIT_E2E_STORE_NAME),
		aiTaskId: asText(env.SPLIT_E2E_AI_TASK_ID),
		reportDate: asText(env.SPLIT_E2E_REPORT_DATE),
		allowAiWrite: env.SPLIT_E2E_ALLOW_AI_WRITE === '1',
		allowReportWrite: env.SPLIT_E2E_ALLOW_REPORT_WRITE === '1',
		phase,
		launchDevTools: true,
		cliPath: DEVTOOLS_CLI_PATH,
		projectPath: process.cwd(),
		automationPort,
		servicePort,
		port: automationPort,
		wsEndpoint:
			asText(env.SPLIT_UI_WS_ENDPOINT) ||
			`ws://127.0.0.1:${automationPortText}`,
		evidenceDir:
			asText(env.SPLIT_UI_EVIDENCE_DIR) ||
			path.resolve('outputs', 'subpackage-split', phase)
	})
}

/**
 * 校验测试环境、凭据、页面上下文与本地自动化端口。
 * @param {NodeJS.ProcessEnv|Record<string, string|undefined>} [env] 运行时变量
 * @returns {Readonly<object>} 可用于启动 DevTools 自动化的配置
 */
export function requireSplitUiConfig(env = process.env) {
	const config = loadSplitUiConfig(env)
	const missing = []
	if (!config.enabled) missing.push('RUN_SPLIT_UI_E2E')
	if (!config.mobile) missing.push('SPLIT_E2E_MOBILE')
	if (!config.code) missing.push('SPLIT_E2E_CODE')
	if (!config.parkName) missing.push('SPLIT_E2E_PARK_NAME')
	if (!config.storeName) missing.push('SPLIT_E2E_STORE_NAME')
	if (!config.aiTaskId) missing.push('SPLIT_E2E_AI_TASK_ID')
	if (!config.reportDate) missing.push('SPLIT_E2E_REPORT_DATE')
	if (missing.length) {
		throw new Error(`Missing split UI runtime values: ${missing.join(', ')}`)
	}

	if (!isAllowedTestBaseUrl(config.baseUrl)) {
		throw new Error('Split UI E2E base URL is not allowlisted')
	}
	if (!['baseline', 'post'].includes(config.phase)) {
		throw new Error('Split UI E2E phase must be baseline or post')
	}
	if (!/^\d{4}-\d{2}-\d{2}$/.test(config.reportDate)) {
		throw new Error('SPLIT_E2E_REPORT_DATE must use yyyy-MM-dd')
	}
	if (!isValidPort(config.automationPort)) {
		throw new Error('Split UI automation port must be an integer from 1024 to 65535')
	}
	if (!isValidPort(config.servicePort)) {
		throw new Error('Split UI service port must be an integer from 1024 to 65535')
	}
	if (config.automationPort === config.servicePort) {
		throw new Error('Split UI service and automation ports must differ')
	}

	let endpoint
	try {
		endpoint = new URL(config.wsEndpoint)
	} catch {
		throw new Error('Split UI websocket endpoint must be loopback')
	}
	if (
		endpoint.protocol !== 'ws:' ||
		!['127.0.0.1', 'localhost', '::1'].includes(endpoint.hostname)
	) {
		throw new Error('Split UI websocket endpoint must be loopback')
	}
	if (Number(endpoint.port || 80) !== config.automationPort) {
		throw new Error('Split UI websocket port must match automation port')
	}

	return config
}