const assert = require('assert') const a01Url = 'http://localhost:5173/#/pages/auth/a01-entry' const a05Url = 'http://localhost:5173/#/pages/auth/a05-reset-password' 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, url, selector) => { const targetUrl = url.replace('/#/', `/?runtime=${Date.now()}#/`) await send('Page.navigate', { url: targetUrl }) await waitFor(send, `location.href.includes(${JSON.stringify(new URL(url).hash.slice(1))})`, `Page did not navigate to ${url}`) await waitFor(send, `Boolean(document.querySelector(${JSON.stringify(selector)}))`, `Page did not render ${selector}`) } const setInputs = (values) => `(() => { const inputs = document.querySelectorAll('.auth-input input') ;${JSON.stringify(values)}.forEach((value, index) => { inputs[index].value = value inputs[index].dispatchEvent(new Event('input', { bubbles: true })) }) })()` const run = async () => { const { socket, send, exceptions } = await connect() try { await send('Page.enable') await send('Runtime.enable') await navigate(send, a01Url, '.forgot-password') await valueOf(send, "document.querySelector('.forgot-password').click()") await waitFor(send, "location.href.includes('/pages/auth/a05-reset-password')", 'A01 forgot-password entry did not open A05') 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, a05Url, '.reset-submit') 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, `A05 has horizontal overflow at ${size.width}x${size.height}`) if (size.width === 320) assert(metrics.pageScrollHeight > metrics.pageClientHeight, 'A05 must scroll naturally at 320x568') } await valueOf(send, "document.querySelector('.get-code').click()") await waitFor(send, "Boolean(document.querySelector('.field-error'))", 'A05 invalid phone did not show inline error') await valueOf(send, setInputs(['13800138000', '', '', ''])) await valueOf(send, "document.querySelector('.get-code').click()") await waitFor(send, "Boolean(document.querySelector('.verification-layer'))", 'A05 code request did not open custom verification') await valueOf(send, "document.querySelector('.verification-action').click()") await waitFor(send, "!document.querySelector('.verification-layer')", 'A05 verification did not close') assert.strictEqual(await valueOf(send, "document.querySelector('.auth-input input').value"), '13800138000', 'A05 verification close cleared the form') await valueOf(send, "document.querySelector('.get-code').click()") await waitFor(send, "Boolean(document.querySelector('.verification-layer'))", 'A05 verification did not reopen') await valueOf(send, "document.querySelector('.verification-action--primary').click()") await waitFor(send, "Boolean(document.querySelector('.feedback-toast'))", 'A05 verified code request did not use custom feedback') assert.strictEqual(await valueOf(send, "document.querySelector('.get-code').textContent"), '重新获取', 'A05 code action did not enter requested state') await valueOf(send, "document.querySelector('.reset-submit').click()") await waitFor(send, "document.querySelectorAll('.field-error').length === 3", 'A05 incomplete submit did not show three remaining field errors') await valueOf(send, setInputs(['13800138000', '123456', 'new-password', 'different-password'])) await sleep(100) await valueOf(send, "document.querySelector('.reset-submit').click()") await sleep(100) const mismatchState = await valueOf(send, `({ values: Array.from(document.querySelectorAll('.auth-input input')).map((item) => item.value), errors: Array.from(document.querySelectorAll('.field-error')).map((item) => item.textContent), success: Boolean(document.querySelector('.success-layer')) })`) assert(mismatchState.errors.some((message) => message.includes('不一致')), `A05 mismatched passwords did not show inline error: ${JSON.stringify(mismatchState)}`) await valueOf(send, setInputs(['13800138000', '123456', 'new-password', 'new-password'])) await sleep(100) await valueOf(send, "document.querySelector('.reset-submit').click()") await waitFor(send, "Boolean(document.querySelector('.success-layer'))", 'A05 valid submit did not show custom success result') await valueOf(send, "document.querySelector('.success-action').click()") await waitFor(send, "Boolean(document.querySelector('.login-tab')) && (location.hash === '#/' || location.href.includes('/pages/auth/a01-entry'))", 'A05 success action did not return to A01') assert.deepStrictEqual(exceptions, [], `A05 raised browser exceptions: ${exceptions.join('; ')}`) process.stdout.write('A05-RESET-PASSWORD-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) })