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() socket.addEventListener('message', (event) => { const message = JSON.parse(event.data) 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 } } const valueOf = async (send, expression) => (await send('Runtime.evaluate', { expression, returnByValue: true })).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 waitForPageLoad = async (socket, trigger) => new Promise((resolve, reject) => { const timeout = setTimeout(() => { socket.removeEventListener('message', onMessage) reject(new Error('Page reload did not emit Page.loadEventFired')) }, 4000) const onMessage = (event) => { const message = JSON.parse(event.data) if (message.method === 'Page.loadEventFired') { clearTimeout(timeout) socket.removeEventListener('message', onMessage) resolve() } } socket.addEventListener('message', onMessage) Promise.resolve().then(trigger).catch((error) => { clearTimeout(timeout) socket.removeEventListener('message', onMessage) reject(error) }) }) const open = async (socket, 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 waitForPageLoad(socket, () => send('Page.reload')) await waitFor(send, `Boolean(document.querySelector(${JSON.stringify(selector)}))`, `${route}${query} did not render`) } const setFieldValue = async (send, selector, value) => { const prepared = await valueOf(send, `(() => { const field = document.querySelector(${JSON.stringify(selector)}) if (!field) return false field.value = ${JSON.stringify(value)} field.dispatchEvent(new Event('input', { bubbles: true })) return true })()`) if (!prepared) throw new Error(`Could not prepare field: ${selector}`) await sleep(50) } const run = async () => { const { socket, send } = await connect() try { await send('Page.enable') await send('Runtime.enable') // 步骤一:G11 从真实表单触发必填校验,再保存有效名称并进入成功反馈。 await open(socket, send, '/pages/genealogy/g11-genealogy-settings', '?genealogyId=1001', '.settings-state--form') await setFieldValue(send, '.settings-field input', '') await valueOf(send, "document.querySelector('.settings-action').click()") await waitFor(send, "Boolean(document.querySelector('.settings-field-error'))", 'G11 empty name did not show inline validation') await setFieldValue(send, '.settings-field input', '某氏家谱') await valueOf(send, "document.querySelector('.settings-action').click()") await waitFor(send, "Boolean(document.querySelector('.settings-state--success'))", 'G11 valid save did not enter success state') await waitFor(send, "Boolean(document.querySelector('.settings-feedback'))", 'G11 valid save did not show feedback') if (!(await valueOf(send, "document.body.textContent.includes('尚未提交服务器')"))) throw new Error('G11 local settings preview claimed a server save') await open(socket, send, '/pages/genealogy/g11-genealogy-settings', '?state=success&genealogyId=1001', '.settings-state--success') await open(socket, send, '/pages/genealogy/g11-genealogy-settings', '?role=owner&state=form&genealogyId=1002', '.settings-state--no-permission') if (await valueOf(send, "Boolean(document.querySelector('.settings-form'))")) throw new Error('G11 trusted a forged owner role for a non-owner genealogy') // 步骤二:G12 依次验证空内容、非法字符和本地保存成功,结果不能只靠查询参数摆出。 await open(socket, send, '/pages/genealogy/g12-generation-poems', '?genealogyId=1001', '.poem-state--list') await open(socket, send, '/pages/genealogy/g12-generation-poems', '?genealogyId=1002', '.poem-state--list') if (await valueOf(send, "Boolean(document.querySelector('.header-action, .poem-list > .poem-action'))")) throw new Error('G12 member view exposed an owner maintenance entry') await open(socket, send, '/pages/genealogy/g12-generation-poems', '?role=owner&state=edit&genealogyId=1002', '.poem-state--no-permission') await open(socket, send, '/pages/genealogy/g12-generation-poems', '?state=list&genealogyId=2001', '.poem-state--no-permission') await open(socket, send, '/pages/genealogy/g12-generation-poems', '?state=empty&genealogyId=1001', '.poem-state--empty') await open(socket, send, '/pages/genealogy/g12-generation-poems', '?state=edit&genealogyId=1001', '.poem-state--edit') await setFieldValue(send, '.poem-field textarea', '临时草稿') await valueOf(send, "document.querySelector('.poem-editor__actions .poem-action:first-child').click()") await waitFor(send, "Boolean(document.querySelector('.app-dialog-layer'))", 'G12 dirty cancel did not open the shared discard confirmation') await valueOf(send, "document.querySelector('.app-dialog__actions .app-button:first-child').click()") await waitFor(send, "Boolean(document.querySelector('.poem-state--edit'))", 'G12 continuing edit did not keep the editor open') await valueOf(send, "document.querySelector('.poem-editor__actions .poem-action:first-child').click()") await waitFor(send, "Boolean(document.querySelector('.app-dialog-layer'))", 'G12 second dirty cancel did not reopen confirmation') await valueOf(send, "document.querySelector('.app-dialog__actions .app-button:last-child').click()") await waitFor(send, "Boolean(document.querySelector('.poem-state--list'))", 'G12 confirmed discard did not restore the editor origin') await open(socket, send, '/pages/genealogy/g12-generation-poems', '?state=edit&genealogyId=1001', '.poem-state--edit') await setFieldValue(send, '.poem-field textarea', '') await valueOf(send, "document.querySelector('.poem-editor__actions .poem-action:last-child').click()") await waitFor(send, "document.querySelector('.poem-field-error')?.textContent.includes('请录入字辈内容')", 'G12 empty poem did not show validation') await setFieldValue(send, '.poem-field textarea', '启'.repeat(501)) await valueOf(send, "document.querySelector('.poem-editor__actions .poem-action:last-child').click()") await waitFor(send, "document.querySelector('.poem-field-error')?.textContent.includes('最多录入 500 个世代')", 'G12 generation limit did not render') // 步骤三:走完整的保留、停用和取消恢复接线,不能只证明纯函数会合并。 await setFieldValue(send, '.poem-field textarea', '启 宗 敦') await valueOf(send, "document.querySelector('.poem-editor__actions .poem-action:last-child').click()") await waitFor(send, "document.querySelectorAll('.poem-row').length === 4", 'G12 disableMissing=false did not preserve the uncovered tail') if (await valueOf(send, "document.querySelector('.poem-row:last-child')?.classList.contains('poem-row--disabled')")) throw new Error('G12 disableMissing=false unexpectedly disabled the tail') if (!(await valueOf(send, "document.querySelector('.poem-row:last-child .poem-row__character')?.textContent.includes('本')"))) throw new Error('G12 disableMissing=false did not preserve the tail text') await valueOf(send, "document.querySelector('.header-action').click()") await waitFor(send, "Boolean(document.querySelector('.poem-state--edit'))", 'G12 maintenance action did not reopen the editor') await valueOf(send, "document.querySelector('.poem-policy__option').click()") await valueOf(send, "document.querySelector('.poem-editor__actions .poem-action:last-child').click()") await waitFor(send, "document.querySelector('.poem-row:last-child')?.classList.contains('poem-row--disabled')", 'G12 disableMissing=true did not disable the uncovered tail') if (!(await valueOf(send, "document.querySelector('.poem-row:last-child .poem-row__character')?.textContent.includes('本')"))) throw new Error('G12 disabled tail lost its historical text') if ((await valueOf(send, "document.querySelectorAll('.poem-row--current').length")) !== 1) throw new Error('G12 did not keep exactly one active current generation') if (await valueOf(send, "document.querySelector('.poem-row--current')?.classList.contains('poem-row--disabled')")) throw new Error('G12 marked a disabled generation as current') await valueOf(send, "document.querySelector('.header-action').click()") await waitFor(send, "Boolean(document.querySelector('.poem-state--edit'))", 'G12 did not reopen after disabling the tail') await setFieldValue(send, '.poem-field textarea', '临时草稿') await valueOf(send, "document.querySelector('.poem-policy__option').click()") await valueOf(send, "document.querySelector('.poem-editor__actions .poem-action:first-child').click()") await waitFor(send, "Boolean(document.querySelector('.app-dialog-layer'))", 'G12 policy change did not trigger discard confirmation') await valueOf(send, "document.querySelector('.app-dialog__actions .app-button:last-child').click()") await waitFor(send, "Boolean(document.querySelector('.poem-state--list'))", 'G12 confirmed policy discard did not leave the editor') await valueOf(send, "document.querySelector('.header-action').click()") await waitFor(send, "Boolean(document.querySelector('.poem-state--edit'))", 'G12 did not reopen after policy discard') if (!(await valueOf(send, "document.querySelector('.poem-policy__option')?.textContent.includes('停用并保留记录')"))) throw new Error('G12 discard did not restore disableMissing=true') if ((await valueOf(send, "document.querySelector('.poem-field textarea')?.value")) !== '启 宗 敦') throw new Error('G12 discard did not restore the poem draft snapshot') // 步骤四:500×50 个补充平面字符接近双重上限,验证 textarea、解析、保存与分批渲染。 const maximumPoem = Array.from({ length: 500 }, () => '𠀀'.repeat(50)).join(' ') if (Array.from(maximumPoem).length !== 25499 || maximumPoem.length !== 50499) throw new Error('G12 maximum Unicode fixture is malformed') await setFieldValue(send, '.poem-field textarea', maximumPoem) await valueOf(send, "document.querySelector('.poem-editor__actions .poem-action:last-child').click()") await waitFor(send, "Boolean(document.querySelector('.poem-state--list'))", 'G12 valid save did not return to the list') await waitFor(send, "Boolean(document.querySelector('.poem-feedback'))", 'G12 valid save did not show feedback') if (!(await valueOf(send, "document.querySelector('.poem-feedback')?.textContent.includes('本地字辈预览已更新')"))) throw new Error('G12 local save feedback is not explicit') if ((await valueOf(send, "document.querySelectorAll('.poem-row').length")) !== 50) throw new Error('G12 did not render the 500-generation list in 50-row batches') if (!(await valueOf(send, "document.querySelector('.poem-load-more')?.textContent.includes('剩余 450 代')"))) throw new Error('G12 first batch did not expose the remaining generation count') for (let batch = 1; batch < 10; batch += 1) { await valueOf(send, "document.querySelector('.poem-load-more')?.click()") await sleep(30) } await waitFor(send, "document.querySelectorAll('.poem-row').length === 500 && !document.querySelector('.poem-load-more')", 'G12 did not make the final generation reachable') if (!(await valueOf(send, "[...document.querySelectorAll('.poem-row__number')].at(-1)?.textContent.includes('第 500 世')"))) throw new Error('G12 final generation is not reachable') if (!(await valueOf(send, "document.documentElement.scrollWidth <= innerWidth && [...document.querySelectorAll('.poem-row__character')].every((item) => item.scrollWidth <= item.clientWidth)"))) throw new Error('G12 legal supplementary 50-character generation text overflowed its row') process.stdout.write('G11-G12-SETTINGS-POEMS-RUNTIME-SMOKE PASS\n') } finally { socket.close() } } run().catch((error) => { process.stderr.write(`${error.stack || error.message}\n`) process.exit(1) })