143 lines
10 KiB
JavaScript
143 lines
10 KiB
JavaScript
const assert = require('assert')
|
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
const origin = process.argv[2] || 'http://localhost:5173'
|
|
const routes = [
|
|
['/pages/notification/n02-message-detail?id=review-1', '.notice-state--ready'],
|
|
['/pages/profile/m02-edit-profile', '.profile-state--ready'],
|
|
['/pages/profile/m03-security-settings', '.device-state--limited'],
|
|
['/pages/profile/m04-change-password', '.password-state--ready'],
|
|
['/pages/profile/m05-change-phone', '.phone-state--ready'],
|
|
['/pages/profile/m06-help-center', '.help-page'],
|
|
['/pages/profile/m07-feedback', '.feedback-state--ready'],
|
|
['/pages/profile/m08-promotion', '.share-state--unavailable'],
|
|
['/pages/profile/m09-vip-orders', '.order-state--unavailable'],
|
|
['/pages/profile/m10-about-settings', '.about-page'],
|
|
]
|
|
|
|
const connect = async () => {
|
|
const pages = await (await fetch('http://127.0.0.1:9222/json/list')).json()
|
|
const page = pages.find((item) => item.type === 'page' && item.url.startsWith(`${origin}/`))
|
|
if (!page) throw new Error(`Chrome debugging has no ${origin} page`)
|
|
const socket = new WebSocket(page.webSocketDebuggerUrl)
|
|
await new Promise((resolve, reject) => { socket.addEventListener('open', resolve, { once: true }); socket.addEventListener('error', reject, { once: true }) })
|
|
let id = 0
|
|
const pending = new Map()
|
|
socket.addEventListener('message', ({ data }) => { const message = JSON.parse(data); const request = pending.get(message.id); if (!request) return; pending.delete(message.id); message.error ? request.reject(new Error(message.error.message)) : request.resolve(message.result) })
|
|
const send = (method, params = {}) => new Promise((resolve, reject) => { id += 1; pending.set(id, { resolve, reject }); socket.send(JSON.stringify({ id, method, params })) })
|
|
return { socket, send }
|
|
}
|
|
const valueOf = async (send, expression) => (await send('Runtime.evaluate', { expression, returnByValue: true })).result?.value
|
|
const waitFor = async (send, expression, message) => { for (let i = 0; i < 60; i += 1) { if (await valueOf(send, expression)) return; await sleep(100) } throw new Error(message) }
|
|
|
|
const run = async () => {
|
|
const { socket, send } = await connect()
|
|
let audit = 0
|
|
const open = async (route, selector) => {
|
|
audit += 1
|
|
const url = `${origin}/?nmAudit=${audit}#${route}`
|
|
await send('Page.navigate', { url })
|
|
await waitFor(send, `location.href === ${JSON.stringify(url)}`, `navigation failed: ${route}`)
|
|
await waitFor(send, `Boolean(document.querySelector(${JSON.stringify(selector)}))`, `selector missing: ${route} ${selector}`)
|
|
}
|
|
try {
|
|
await send('Page.enable'); await send('Runtime.enable')
|
|
for (const size of [{ width: 320, height: 568 }, { width: 412, height: 915 }]) {
|
|
await send('Emulation.setDeviceMetricsOverride', { ...size, deviceScaleFactor: 1, mobile: true, screenWidth: size.width, screenHeight: size.height })
|
|
for (const [route, selector] of routes) {
|
|
await open(route, selector)
|
|
const metrics = await valueOf(send, `({ width: document.documentElement.scrollWidth, modulePage: Boolean(document.querySelector('.module-page')) })`)
|
|
assert(metrics.width <= size.width + 1, `${route} overflows at ${size.width}x${size.height}`)
|
|
assert.strictEqual(metrics.modulePage, false, `${route} still renders ModulePage`)
|
|
const stress = await valueOf(send, `(() => {
|
|
document.querySelectorAll('text').forEach((node) => {
|
|
if (node.children.length || !node.textContent.trim()) return
|
|
node.textContent = node.textContent.repeat(3)
|
|
const size = Number.parseFloat(getComputedStyle(node).fontSize)
|
|
if (size) node.style.fontSize = (size * 1.3) + 'px'
|
|
})
|
|
return { width: document.documentElement.scrollWidth, height: document.documentElement.scrollHeight }
|
|
})()`)
|
|
assert(stress.width <= size.width + 1, `${route} long text overflows at ${size.width}x${size.height}`)
|
|
assert(stress.height >= size.height, `${route} long content did not remain reachable at ${size.width}x${size.height}`)
|
|
}
|
|
}
|
|
|
|
await open('/pages/notification/n02-message-detail?id=review-1', '.notice-state--ready')
|
|
await valueOf(send, `document.querySelector('.app-button').click()`)
|
|
await waitFor(send, `document.body.innerText.includes('已读')`, 'N02 did not update read state')
|
|
|
|
await open('/pages/notification/n02-message-detail?id=missing', '.notice-state--expired')
|
|
assert.strictEqual(await valueOf(send, `Boolean(document.querySelector('.action-stack'))`), false, 'N02 unknown notice exposed business actions')
|
|
|
|
await open('/pages/profile/m02-edit-profile', '.profile-state--ready')
|
|
await valueOf(send, `(() => { const input=document.querySelector('input'); input.value=''; input.dispatchEvent(new Event('input',{bubbles:true})); document.querySelector('.app-button').click(); return true })()`)
|
|
await waitFor(send, `Boolean(document.querySelector('.field-error'))`, 'M02 did not validate nickname')
|
|
|
|
await open('/pages/profile/m03-security-settings', '.device-state--limited')
|
|
await valueOf(send, `document.querySelector('.security-row').click()`)
|
|
await waitFor(send, `location.hash.includes('/pages/profile/m04-change-password')`, 'M03 password navigation failed')
|
|
|
|
await open('/pages/profile/m04-change-password', '.password-state--ready')
|
|
await valueOf(send, `document.querySelector('.app-button').click()`)
|
|
assert.strictEqual(await valueOf(send, `document.querySelectorAll('.field-error').length`), 3, 'M04 must report all empty fields')
|
|
|
|
await open('/pages/profile/m05-change-phone', '.phone-state--ready')
|
|
await valueOf(send, `document.querySelector('.code-action').click()`)
|
|
await waitFor(send, `Boolean(document.querySelector('.field-error'))`, 'M05 did not validate phone before code')
|
|
|
|
await open('/pages/profile/m06-help-center', '.help-page')
|
|
await valueOf(send, `(() => { const input=document.querySelector('input'); input.value='不存在的问题'; input.dispatchEvent(new Event('input',{bubbles:true})); return true })()`)
|
|
await waitFor(send, `Boolean(document.querySelector('.empty-card'))`, 'M06 search empty state missing')
|
|
|
|
await open('/pages/profile/m07-feedback', '.feedback-state--ready')
|
|
await valueOf(send, `document.querySelector('.app-button').click()`)
|
|
assert.strictEqual(await valueOf(send, `document.querySelectorAll('.field-error').length`), 1, 'M07 must only require feedbackContent')
|
|
await valueOf(send, `(() => {
|
|
document.querySelector('.type-chip').click()
|
|
const content = document.querySelector('textarea')
|
|
content.value = 'runtime feedback'
|
|
content.dispatchEvent(new Event('input', { bubbles: true }))
|
|
const contact = document.querySelector('.contact-row input')
|
|
contact.value = 'runtime@example.com'
|
|
contact.dispatchEvent(new Event('input', { bubbles: true }))
|
|
document.querySelector('.page-content > .app-button').click()
|
|
return true
|
|
})()`)
|
|
await waitFor(send, `document.querySelector('.feedback-result--error')?.textContent.includes('本地预览模式')`, 'M07 must fail closed instead of faking feedback submission in mock mode')
|
|
|
|
await open('/pages/profile/m08-promotion', '.share-state--unavailable')
|
|
assert.strictEqual(await valueOf(send, `Boolean(document.querySelector('.copy-action, .invite-code'))`), false, 'M08 exposed an invitation code action')
|
|
assert.strictEqual(await valueOf(send, `document.body.innerText.includes('TN20260720')`), false, 'M08 exposed the retired fake invitation code')
|
|
await valueOf(send, `document.querySelector('.page-content > .app-button').click()`)
|
|
await waitFor(send, `document.querySelector('.app-dialog__title')?.textContent.includes('功能尚未接入')`, 'M08 unavailable explanation dialog missing')
|
|
|
|
await open('/pages/profile/m09-vip-orders', '.order-state--unavailable')
|
|
assert.strictEqual(await valueOf(send, `Boolean(document.querySelector('.order-card'))`), false, 'M09 exposed an order card')
|
|
assert.strictEqual(await valueOf(send, `document.body.innerText.includes('O20260720001') || document.body.innerText.includes('¥0.00') || document.body.innerText.includes('演示记录')`), false, 'M09 exposed retired fake order data')
|
|
await valueOf(send, `document.querySelector('.page-content > .app-button').click()`)
|
|
await waitFor(send, `document.querySelector('.app-dialog__title')?.textContent.includes('付费服务尚未开放')`, 'M09 unavailable service dialog missing')
|
|
|
|
await open('/pages/profile/m10-about-settings', '.about-page')
|
|
await valueOf(send, `document.querySelector('.settings-row').click()`)
|
|
await waitFor(send, `Boolean(document.querySelector('.app-dialog'))`, 'M10 agreement dialog missing')
|
|
await valueOf(send, `document.querySelector('.app-dialog__actions .app-button').click()`)
|
|
await waitFor(send, `!document.querySelector('.app-dialog')`, 'M10 agreement dialog did not close')
|
|
await valueOf(send, `(() => {
|
|
uni.setStorageSync('jiapu_token', 'runtime-token')
|
|
uni.setStorageSync('jiapu_current_genealogy_id', '1001')
|
|
document.querySelector('.page-content > .app-button').click()
|
|
return true
|
|
})()`)
|
|
await waitFor(send, `document.querySelector('.app-dialog__title')?.textContent.includes('确认退出登录')`, 'M10 logout confirmation missing')
|
|
await valueOf(send, `document.querySelector('.app-dialog__actions .app-button:last-child').click()`)
|
|
await waitFor(send, `location.hash === '#/pages/auth/a01-entry' && Boolean(document.querySelector('.login-page'))`, 'M10 logout did not goRoot A01')
|
|
assert.strictEqual(await valueOf(send, `uni.getStorageSync('jiapu_token')`), '', 'M10 logout did not clear the session token')
|
|
assert.strictEqual(await valueOf(send, `uni.getStorageSync('jiapu_current_genealogy_id')`), '', 'M10 logout did not clear the genealogy context')
|
|
process.stdout.write('NM-BUSINESS-RUNTIME-SMOKE PASS\n')
|
|
} finally {
|
|
try { await send('Emulation.clearDeviceMetricsOverride') } catch (_) {}
|
|
socket.close()
|
|
}
|
|
}
|
|
run().catch((error) => { process.stderr.write(`${error.stack || error.message}\n`); process.exit(1) })
|