Files
jiapuapp/tests/g08-g10-application-flow-runtime-smoke.js
T
2026-07-17 08:08:33 +08:00

130 lines
8.0 KiB
JavaScript

const sleep = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds))
const origin = process.argv[2] || 'http://localhost:5173'
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} application 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()
const exceptions = []
socket.addEventListener('message', (event) => {
const message = JSON.parse(event.data)
if (message.method === 'Runtime.exceptionThrown') exceptions.push(message.params.exceptionDetails.text)
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, exceptions }
}
const valueOf = async (send, expression) => {
const result = await send('Runtime.evaluate', { expression, returnByValue: true })
if (result.exceptionDetails) throw new Error(result.exceptionDetails.exception?.description || result.exceptionDetails.text)
return result.result?.value
}
const waitFor = async (send, expression, message) => {
for (let attempt = 0; attempt < 40; attempt += 1) {
if (await valueOf(send, expression)) return
await sleep(100)
}
throw new Error(message)
}
let auditId = 0
const open = async (send, route, query, selector) => {
auditId += 1
const url = `${origin}/?g0810Audit=${auditId}#${route}${query}`
await send('Page.navigate', { url })
await waitFor(send, `location.href === ${JSON.stringify(url)}`, `${route} navigation failed`)
await waitFor(send, `Boolean(document.querySelector(${JSON.stringify(selector)}))`, `${route}${query} did not render`)
}
const run = async () => {
const { socket, send, exceptions } = await connect()
try {
await send('Page.enable')
await send('Runtime.enable')
await open(send, '/pages/genealogy/g08-join-application', '?genealogyId=1002', '.join-state--form')
await waitFor(send, "document.querySelectorAll('.join-field input').length >= 2", 'G08 real input controls did not render')
const prepared = await valueOf(send, `(() => {
const inputs = Array.from(document.querySelectorAll('.join-field input'))
if (inputs.length < 2) return false
inputs[0].value = '汤明远'
inputs[0].dispatchEvent(new Event('input', { bubbles: true }))
inputs[1].value = '汤正华堂侄'
inputs[1].dispatchEvent(new Event('input', { bubbles: true }))
document.querySelector('.join-action')?.click()
return true
})()`)
if (!prepared) throw new Error('G08 form controls could not be prepared')
await waitFor(send, "Boolean(document.querySelector('.join-state--success'))", 'G08 real form submission did not reach the same-page success state')
await open(send, '/pages/genealogy/g08-join-application', '?source=invite&genealogyId=2001', '.join-state--form')
await valueOf(send, "document.querySelector('.join-action').click()")
await waitFor(send, "document.querySelectorAll('.join-field-error').length === 2", 'G08 invite source did not show two inline required errors')
const invitePrepared = await valueOf(send, `(() => {
const inputs = Array.from(document.querySelectorAll('.join-field input'))
inputs[0].value = '汤明远'
inputs[0].dispatchEvent(new Event('input', { bubbles: true }))
inputs[1].value = '汤正华堂侄'
inputs[1].dispatchEvent(new Event('input', { bubbles: true }))
return true
})()`)
if (!invitePrepared) throw new Error('G08 invite controls could not be prepared')
await sleep(100)
await valueOf(send, "document.querySelector('.join-action').click()")
await waitFor(send, "Boolean(document.querySelector('.join-state--success'))", 'G08 invite source did not reach success')
if (!(await valueOf(send, "document.body.textContent.includes('无需等待审核')"))) throw new Error('G08 invite source did not preserve direct-join semantics')
await valueOf(send, "document.querySelector('.join-result .join-action').click()")
await waitFor(send, "location.href.includes('/pages/genealogy/g01-my-genealogies?genealogyId=2001')", 'G08 invite success did not return to selected G01 genealogy')
for (const size of [{ width: 320, height: 568 }, { width: 360, height: 800 }, { width: 412, height: 915 }]) {
await send('Emulation.setDeviceMetricsOverride', { ...size, deviceScaleFactor: 1, mobile: true })
await open(send, '/pages/genealogy/g08-join-application', '?source=search&genealogyId=2001', '.join-state--form')
const width = await valueOf(send, 'document.documentElement.scrollWidth')
if (width > size.width + 1) throw new Error(`G08 has horizontal overflow at ${size.width}x${size.height}`)
}
await open(send, '/pages/genealogy/g09-my-applications', '', '.application-state--list')
if ((await valueOf(send, "document.querySelectorAll('.application-card__action').length")) !== 3) throw new Error('G09 list did not render the three status-specific actions')
await valueOf(send, "document.querySelector('.application-card__action').click()")
await waitFor(send, "Boolean(document.querySelector('.app-dialog-layer'))", 'G09 pending action did not open custom withdrawal confirmation')
await valueOf(send, "document.querySelector('.app-dialog__actions .app-button:last-child').click()")
await waitFor(send, "document.querySelector('.application-card__status')?.textContent.includes('已撤回')", 'G09 withdrawal did not update the local status')
await valueOf(send, "document.querySelectorAll('.application-card__action')[1].click()")
await waitFor(send, "location.href.includes('/pages/genealogy/g08-join-application?source=search&previous=rejected&genealogyId=')", 'G09 rejected action did not reopen G08 search source')
await open(send, '/pages/genealogy/g09-my-applications', '?state=empty', '.application-state--empty')
await open(send, '/pages/genealogy/g10-application-review', '?genealogyId=1001', '.review-state--list')
await valueOf(send, "document.querySelectorAll('.review-action')[1].click()")
await waitFor(send, "Boolean(document.querySelector('.app-dialog-layer'))", 'G10 approve action did not open custom confirmation')
await valueOf(send, "document.querySelector('.app-dialog__actions .app-button:last-child').click()")
await waitFor(send, "document.querySelector('.application-card__status')?.textContent.includes('已通过')", 'G10 approval did not update the card')
await waitFor(send, "Boolean(document.querySelector('.review-feedback'))", 'G10 approval did not show custom feedback')
await valueOf(send, "document.querySelector('.header-action').click()")
await waitFor(send, "document.querySelector('.app-dialog__title')?.textContent.includes('审核说明')", 'G10 help did not use the custom dialog')
await valueOf(send, "document.querySelector('.app-dialog__actions .app-button').click()")
await open(send, '/pages/genealogy/g10-application-review', '?state=empty&genealogyId=1001', '.review-state--empty')
await open(send, '/pages/genealogy/g10-application-review', '?state=no-permission&genealogyId=1001', '.review-state--no-permission')
if (exceptions.length) throw new Error(`G08-G10 raised browser exceptions: ${exceptions.join('; ')}`)
process.stdout.write('G08-G10-APPLICATION-FLOW-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)
})