97 lines
4.3 KiB
JavaScript
97 lines
4.3 KiB
JavaScript
const origin = process.argv[2] || 'http://localhost:5173'
|
|
const debugPort = process.env.CDP_PORT || '9222'
|
|
const genealogyId = process.env.G10_GENEALOGY_ID || ''
|
|
const sleep = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds))
|
|
|
|
const connect = async () => {
|
|
const pages = await (await fetch(`http://127.0.0.1:${debugPort}/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 < 50; attempt += 1) {
|
|
if (await valueOf(send, expression)) return
|
|
await sleep(100)
|
|
}
|
|
throw new Error(message)
|
|
}
|
|
|
|
const open = async (send, route, query, selector) => {
|
|
const url = `${origin}/#${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=2001&genealogyName=%E6%B5%8B%E8%AF%95%E5%AE%B6%E8%B0%B1', '.join-form')
|
|
if ((await valueOf(send, "document.querySelectorAll('.form-field input, .form-field textarea').length")) !== 5) {
|
|
throw new Error('G08 did not render every APP join-application field')
|
|
}
|
|
if (await valueOf(send, "document.querySelector('.form-field .required-mark') !== null")) {
|
|
throw new Error('G08 marked an optional APP field as required')
|
|
}
|
|
|
|
await open(send, '/pages/genealogy/g09-my-applications', '', '.applications-page')
|
|
await waitFor(send, "!document.querySelector('.app-loading')", 'G09 application read did not settle')
|
|
if (!await valueOf(send, "Boolean(document.querySelector('.state-card, .application-list'))")) {
|
|
throw new Error('G09 did not render a settled real-read state')
|
|
}
|
|
|
|
if (genealogyId) {
|
|
await open(send, '/pages/genealogy/g10-application-review', `?genealogyId=${encodeURIComponent(genealogyId)}`, '.review-page')
|
|
await waitFor(send, "!document.querySelector('.app-loading')", 'G10 pending-application read did not settle')
|
|
if (!await valueOf(send, "Boolean(document.querySelector('.state-card, .application-list'))")) {
|
|
throw new Error('G10 did not render a settled real-read state')
|
|
}
|
|
} else {
|
|
await open(send, '/pages/genealogy/g10-application-review', '', '.state-card')
|
|
if (!await valueOf(send, "document.body.textContent.includes('审核入口无效')")) {
|
|
throw new Error('G10 invalid-context state did not render')
|
|
}
|
|
}
|
|
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 {
|
|
socket.close()
|
|
}
|
|
}
|
|
|
|
run().catch((error) => {
|
|
process.stderr.write(`${error.stack || error.message}\n`)
|
|
process.exit(1)
|
|
})
|