Files
lik af666567c1 chore(tools): 新增页面结构与 UI 回归测试脚本
补齐 check-pages 与首页、预约记录、下单页的 node --test 用例,
并在 package.json 挂上对应命令,改动样式后可先跑脚本再进开发者工具。
2026-11-16 19:29:48 +08:00

114 lines
4.9 KiB
JavaScript

/* Static checks only; does not replace WeChat compilation or device testing. */
const fs = require('fs')
const path = require('path')
const { spawnSync } = require('child_process')
const root = path.resolve(__dirname, '..')
const app = JSON.parse(fs.readFileSync(path.join(root, 'app.json'), 'utf8'))
const errors = []
const fail = (file, message) => errors.push(`${file}: ${message}`)
const read = (file) => fs.readFileSync(path.join(root, file), 'utf8')
const stripComments = (text) => text.replace(/<!--[\s\S]*?-->/g, '')
const iconCss = read('miniprogram_npm/tdesign-miniprogram/icon/icon.wxss')
const baselineArg = process.argv.indexOf('--baseline')
const baseline = baselineArg >= 0
? JSON.parse(fs.readFileSync(process.argv[baselineArg + 1], 'utf8'))
: null
function checkWxml(file) {
const text = stripComments(read(file))
const configFile = file.replace(/\.wxml$/, '.json')
const config = JSON.parse(read(configFile))
const components = { ...app.usingComponents, ...config.usingComponents }
for (const match of text.matchAll(/<([a-z][\w]*-[\w-]+)\b/g)) {
const name = match[1]
if ((name.startsWith('t-') || name === 'hospital-select') && !components[name]) {
fail(file, `unregistered component ${name}`)
}
}
if (baseline && file.startsWith('pages/')) {
const page = file.replace(/\.wxml$/, '')
const before = baseline[page]
const after = [...text.matchAll(/\b((?:bind|catch|capture-bind|capture-catch)[\w:]*|data-[\w-]+|wx:(?:if|elif|else|for|for-item|for-index|key)|(?:value|disabled|open-type))\s*=\s*"([^"]*)"/g)]
.map(match => match[0]).sort()
const expected = page === 'pages/escort/recordlist'
? [...before, 'wx:if="{{statusFilter}}"'].sort()
: before
if (JSON.stringify(after) !== JSON.stringify(expected)) fail(file, 'bindings differ from baseline')
}
// Remove expressions before scanning tags: expressions may contain > and quotes.
const markup = text.replace(/\{\{[\s\S]*?\}\}/g, 'EXPRESSION')
const stack = []
for (const match of markup.matchAll(/<\/?[a-zA-Z][\w-]*(?:\s+(?:"[^"]*"|'[^']*'|[^'">])*)?\s*\/?>/g)) {
const tag = match[0]
const name = /^<\/?([\w-]+)/.exec(tag)[1]
if (tag.startsWith('</')) {
const opening = stack.pop()
if (opening !== name) fail(file, `closing ${name}, expected ${opening || 'no tag'}`)
} else if (!tag.endsWith('/>')) {
stack.push(name)
}
}
if (stack.length) fail(file, `unclosed tags: ${stack.join(', ')}`)
for (const match of text.matchAll(/<t-icon\b[^>]*\bname="([^"{}]+)"/g)) {
if (!iconCss.includes(`.t-icon-${match[1]}:`)) fail(file, `unknown icon ${match[1]}`)
}
for (const match of text.matchAll(/\bsrc="(\/images\/[^"{}]+)"/g)) {
if (!fs.existsSync(path.join(root, match[1].slice(1)))) fail(file, `missing image ${match[1]}`)
}
}
function checkWxss(file) {
const text = read(file).replace(/\/\*[\s\S]*?\*\//g, '')
const code = text.replace(/"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'/g, '')
let depth = 0
for (const char of code) {
if (char === '{') depth++
if (char === '}') depth--
if (depth < 0) break
}
if (depth !== 0) fail(file, 'unbalanced style braces')
for (const match of text.matchAll(/@import\s+["']([^"']+)["']/g)) {
if (!fs.existsSync(path.resolve(root, path.dirname(file), match[1]))) {
fail(file, `missing import ${match[1]}`)
}
}
}
for (const page of app.pages) {
for (const ext of ['.js', '.json', '.wxml', '.wxss']) {
const file = page + ext
if (!fs.existsSync(path.join(root, file))) {
fail(file, 'missing route file')
continue
}
try {
if (ext === '.json') JSON.parse(read(file))
if (ext === '.wxml') checkWxml(file)
if (ext === '.wxss') checkWxss(file)
if (ext === '.js') {
const result = spawnSync(process.execPath, ['--check', path.join(root, file)], { encoding: 'utf8' })
if (result.status !== 0) fail(file, result.stderr || String(result.error))
}
} catch (error) {
fail(file, error.message)
}
}
}
checkWxml('components/hospital-select/index.wxml')
checkWxss('components/hospital-select/index.wxss')
JSON.parse(read('components/hospital-select/index.json'))
for (const file of ['components/hospital-select/index.js', 'utils/booking-profiles.js', 'utils/navigation.js']) {
const result = spawnSync(process.execPath, ['--check', path.join(root, file)], { encoding: 'utf8' })
if (result.status !== 0) fail(file, result.stderr || String(result.error))
}
if (errors.length) {
console.error(errors.join('\n'))
process.exitCode = 1
} else {
console.log(`PASS: ${app.pages.length} pages — JS syntax, JSON, WXML nesting, WXSS braces/imports, icons and local images`)
if (baseline) console.log('PASS: events, data attributes, field values and render conditions match baseline; one intentional empty-state condition added.')
console.log('WeChat compilation, layout, keyboard and device interaction still require manual verification.')
}