验收20%
This commit is contained in:
@@ -1,93 +1,98 @@
|
||||
const assert = require('assert')
|
||||
|
||||
const baseUrl = 'http://localhost:5173/#/pages/auth/a06-auth-status'
|
||||
const text = (base64) => Buffer.from(base64, 'base64').toString('utf8')
|
||||
const expected = {
|
||||
normal: text('55m75b2V5pyq5a6M5oiQ'),
|
||||
restricted: text('6LSm5Y+35pqC5pe25Y+X6ZmQ'),
|
||||
help: text('6LSm5Y+355Sz6K+J5Yqf6IO95b6F5o6l5YWl')
|
||||
}
|
||||
|
||||
const sleep = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds))
|
||||
|
||||
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.includes('localhost:5173'))
|
||||
if (!page) throw new Error('Chrome debugging has no localhost:5173 page')
|
||||
|
||||
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 }
|
||||
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 < 30; attempt += 1) {
|
||||
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, url, title) => {
|
||||
await send('Page.navigate', { url })
|
||||
await waitFor(send, `location.href === ${JSON.stringify(url)}`, `Did not navigate to ${url}`)
|
||||
await send('Page.reload')
|
||||
await waitFor(send, `location.href === ${JSON.stringify(url)}`, `Reload did not retain ${url}`)
|
||||
try {
|
||||
await waitFor(send, `document.querySelector('.status-title')?.innerText === ${JSON.stringify(title)}`, `A06 did not render ${title}`)
|
||||
} catch (error) {
|
||||
const heading = await valueOf(send, "document.querySelector('.status-title')?.innerText")
|
||||
throw new Error(`${error.message}; current heading: ${heading}`)
|
||||
}
|
||||
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 } = await connect()
|
||||
|
||||
const { socket, send, exceptions } = await connect()
|
||||
try {
|
||||
await send('Page.enable')
|
||||
await send('Runtime.enable')
|
||||
|
||||
await navigate(send, baseUrl, expected.normal)
|
||||
await valueOf(send, "document.querySelector('.status-primary')?.click()")
|
||||
await waitFor(send, "location.href.includes('/pages/auth/a01-entry')", 'Default A06 action did not return to A01')
|
||||
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')
|
||||
}
|
||||
|
||||
await navigate(send, `${baseUrl}?status=restricted`, expected.restricted)
|
||||
await valueOf(send, "document.querySelector('.status-primary')?.click()")
|
||||
await waitFor(send, `document.body.innerText.includes(${JSON.stringify(expected.help)})`, 'Restricted A06 action did not show the local help notice')
|
||||
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 navigate(send, `${baseUrl}?status=register-pending`, text('5rOo5YaM5bCa5pyq5a6M5oiQ'))
|
||||
await valueOf(send, "document.querySelector('.status-primary')?.click()")
|
||||
await waitFor(send, "location.href.includes('/pages/auth/a04-register')", 'Registration-pending A06 action did not open A04')
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user