104 lines
4.8 KiB
JavaScript
104 lines
4.8 KiB
JavaScript
const assert = require('assert')
|
|
|
|
const baseUrl = 'http://localhost:5173/#/pages/auth/a06-auth-status'
|
|
const sleep = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds))
|
|
|
|
const connect = async () => {
|
|
const port = process.env.CHROME_DEBUGGING_PORT || '9222'
|
|
const pages = await (await fetch(`http://127.0.0.1:${port}/json/list`)).json()
|
|
const page = pages.find((item) => item.type === 'page' && item.url.startsWith('http://localhost:5173'))
|
|
if (!page) throw new Error('Chrome debugging has no 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)
|
|
if (message.error) request.reject(new Error(message.error.message))
|
|
else 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)
|
|
}
|
|
|
|
const navigate = async (send, status, title) => {
|
|
const routeQuery = status ? `?status=${status}` : ''
|
|
const targetUrl = `http://localhost:5173/?runtime=${Date.now()}#/pages/auth/a06-auth-status${routeQuery}`
|
|
await send('Page.navigate', { url: targetUrl })
|
|
await waitFor(send, `document.querySelector('.status-title')?.textContent === ${JSON.stringify(title)}`, `A06 did not render ${title}`)
|
|
}
|
|
|
|
const run = async () => {
|
|
const { socket, send, exceptions } = await connect()
|
|
try {
|
|
await send('Page.enable')
|
|
await send('Runtime.enable')
|
|
|
|
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 navigate(send, 'risk', '账号存在安全风险')
|
|
const metrics = await valueOf(send, `({
|
|
width: document.documentElement.scrollWidth,
|
|
pageClientHeight: document.querySelector('.auth-page').clientHeight,
|
|
pageScrollHeight: document.querySelector('.auth-page').scrollHeight
|
|
})`)
|
|
assert(metrics.width <= size.width + 1, `A06 has horizontal overflow at ${size.width}x${size.height}`)
|
|
if (size.width === 320) assert(metrics.pageScrollHeight > metrics.pageClientHeight, 'A06 must scroll naturally at 320x568')
|
|
}
|
|
|
|
for (const state of [
|
|
{ query: 'frozen', title: '账号已被冻结' },
|
|
{ query: 'disabled', title: '账号已被停用' },
|
|
{ query: 'risk', title: '账号存在安全风险' },
|
|
{ query: 'failed', title: '账号存在安全风险' }
|
|
]) {
|
|
await navigate(send, state.query, state.title)
|
|
assert.strictEqual(await valueOf(send, "document.querySelectorAll('.status-detail').length"), 3, `A06 ${state.query} is missing reason/impact/recovery details`)
|
|
}
|
|
|
|
await valueOf(send, "document.querySelector('.status-primary').click()")
|
|
await waitFor(send, "Boolean(document.querySelector('.recovery-layer'))", 'A06 did not open the custom recovery layer')
|
|
await valueOf(send, "document.querySelector('.recovery-action').click()")
|
|
await waitFor(send, "!document.querySelector('.recovery-layer')", 'A06 recovery layer did not close')
|
|
await valueOf(send, "document.querySelector('.status-secondary').click()")
|
|
await waitFor(send, "Boolean(document.querySelector('.login-tab')) && (location.hash === '#/' || location.href.includes('/pages/auth/a01-entry'))", 'A06 did not return to A01')
|
|
assert.deepStrictEqual(exceptions, [], `A06 raised browser exceptions: ${exceptions.join('; ')}`)
|
|
|
|
process.stdout.write('A06-AUTH-STATUS-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)
|
|
})
|