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 fillJoinForm = (send, realName, relation) => valueOf(send, `(() => { const inputs = Array.from(document.querySelectorAll('.join-field input')) if (inputs.length < 2) return false const values = [${JSON.stringify(realName)}, ${JSON.stringify(relation)}] inputs.forEach((input, index) => { input.value = values[index] input.dispatchEvent(new Event('input', { bubbles: true })) }) return true })()`) 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', '.join-state--form') await waitFor(send, "document.querySelectorAll('.join-field input').length >= 2", 'G08 real input controls did not render') const prepared = await fillJoinForm(send, '汤明远', '某某某堂侄') if (!prepared) throw new Error('G08 form controls could not be prepared') await valueOf(send, "document.querySelector('.join-action')?.click()") await waitFor(send, "Boolean(document.querySelector('.join-state--success'))", 'G08 real form submission did not reach the same-page success state') if (!(await valueOf(send, "document.body.textContent.includes('申请尚未提交服务器')"))) throw new Error('G08 local search preview claimed a server submission') await valueOf(send, "document.querySelector('.join-result .join-action').click()") await waitFor(send, "location.href.includes('/pages/genealogy/g09-my-applications')", 'G08 local search preview did not continue to G09 without a result') for (const genealogyId of ['1001', '1002', '2003', 'unknown']) { await open(send, '/pages/genealogy/g08-join-application', `?role=guest&state=success&source=search&genealogyId=${genealogyId}`, '.join-state--ineligible') if (await valueOf(send, "Boolean(document.querySelector('.join-form'))")) throw new Error(`G08 exposed an application form for ineligible genealogy ${genealogyId}`) } // 步骤一:失败输入必须经过真实提交态进入错误结果,并能返回原表单重试。 await open(send, '/pages/genealogy/g08-join-application', '?source=search&genealogyId=2001', '.join-state--form') if (!await fillJoinForm(send, '失败', '某某某堂侄')) throw new Error('G08 failure controls could not be prepared') await valueOf(send, "document.querySelector('.join-action').click()") await waitFor(send, "document.querySelector('.join-action')?.textContent.includes('正在校验')", 'G08 did not expose its submitting state') await waitFor(send, "Boolean(document.querySelector('.join-state--error'))", 'G08 simulated submission failure did not render') await valueOf(send, "document.querySelector('.join-result .join-action').click()") await waitFor(send, "Boolean(document.querySelector('.join-state--form'))", 'G08 failure retry did not restore the form') 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 fillJoinForm(send, '汤明远', '某某某堂侄') 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 preview claimed a durable membership change') await valueOf(send, "document.querySelector('.join-result .join-action').click()") await waitFor(send, "location.href.includes('/pages/genealogy/g01-my-genealogies') && !location.href.includes('genealogyId=2001')", 'G08 invite preview selected a genealogy without backend success') 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) { const offenders = await valueOf(send, `Array.from(document.querySelectorAll('*')).map((node) => { const rect = node.getBoundingClientRect() return { tag: node.tagName, className: String(node.className || ''), left: rect.left, right: rect.right, width: rect.width } }).filter((item) => item.left < -1 || item.right > innerWidth + 1).slice(0, 12)`) throw new Error(`G08 has horizontal overflow at ${size.width}x${size.height}: ${JSON.stringify(offenders)}`) } } 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 use a local-only status') if (!(await valueOf(send, "document.querySelector('.application-card')?.textContent.includes('尚未提交服务器')"))) throw new Error('G09 local withdrawal claimed a durable server result') await valueOf(send, "document.querySelector('.application-card__status--rejected')?.closest('.application-card')?.querySelector('.application-card__action')?.click()") await waitFor(send, "location.href.includes('/pages/genealogy/g08-join-application?genealogyId=2004&source=search&sourceKey=G09')", 'G09 rejected action did not reopen G08 with the canonical contract') 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.querySelector('.review-actions .app-button:nth-child(2)').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') if (!(await valueOf(send, "document.querySelector('.review-feedback')?.textContent.includes('尚未提交服务器')"))) throw new Error('G10 local approval claimed a durable server result') 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', '?genealogyId=1001', '.review-state--list') await valueOf(send, "document.querySelector('.review-actions .app-button:first-child').click()") await waitFor(send, "Boolean(document.querySelector('.rejection-field textarea'))", 'G10 reject action did not open its reason field') await valueOf(send, "document.querySelector('.app-dialog__actions .app-button:last-child').click()") await waitFor(send, "Boolean(document.querySelector('#g10-rejection-error'))", 'G10 empty rejection reason did not show the associated error') await waitFor(send, "document.activeElement === document.querySelector('.rejection-field textarea') || Boolean(document.activeElement?.closest('#g10-rejection-reason'))", 'G10 rejection validation did not focus the reason field') const reasonPrepared = await valueOf(send, `(() => { const textarea = document.querySelector('.rejection-field textarea') if (!textarea) return false textarea.value = '请补充可核验的亲属关系材料' textarea.dispatchEvent(new Event('input', { bubbles: true })) return true })()`) if (!reasonPrepared) throw new Error('G10 rejection reason could not be prepared') await valueOf(send, "document.querySelector('.app-dialog__actions .app-button:last-child').click()") await waitFor(send, "document.querySelector('.application-card__status')?.textContent.includes('已拒绝')", 'G10 rejection did not update the card') await waitFor(send, "Boolean(document.querySelector('.review-feedback'))", 'G10 rejection did not show custom feedback') await open(send, '/pages/genealogy/g10-application-review', '?state=empty&genealogyId=1001', '.review-state--empty') await open(send, '/pages/genealogy/g10-application-review', '?role=owner&state=list&genealogyId=1002', '.review-state--no-permission') if (await valueOf(send, "Boolean(document.querySelector('.application-card'))")) throw new Error('G10 trusted a forged owner role for a non-owner genealogy') await open(send, '/pages/genealogy/g10-application-review', '?state=list&genealogyId=2001', '.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) })