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 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) 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 } } const valueOf = async (send, expression) => { const result = await send('Runtime.evaluate', { expression, returnByValue: true }) 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 setInput = async (send, index, value) => { await waitFor( send, `Boolean(document.querySelectorAll('.flow-fields input')[${index}])`, `G03 input ${index} did not render` ) const expression = `(() => { const input = document.querySelectorAll('.flow-fields input')[${index}] if (!input) return false input.value = ${JSON.stringify(value)} input.dispatchEvent(new Event('input', { bubbles: true })) return true })()` if (!await valueOf(send, expression)) throw new Error(`Could not fill G03 input ${index}`) } const origin = process.argv[2] || 'http://localhost:5173' const g03Path = '/pages/genealogy/g03-create-genealogy' const defaultUrl = `${origin}/#${g03Path}` const openDefaultG03 = async (send) => { await send('Page.navigate', { url: defaultUrl }) await waitFor(send, "Boolean(document.querySelector('.create-flow-panel .flow-rule'))", 'Default G03 creation step did not render') } const run = async () => { const { socket, send } = await connect() try { await send('Page.enable') await send('Runtime.enable') await openDefaultG03(send) await setInput(send, 0, '汤') await setInput(send, 1, '自动验证汤氏家谱') await valueOf(send, "document.querySelector('.flow-primary-action')?.click()") await waitFor(send, "location.href.includes('g03-create-genealogy?step=ancestor&genealogyId=')", 'Creating a genealogy did not open G03 ancestor step') await waitFor(send, "Boolean(document.querySelector('.intro-field'))", 'G03 ancestor step did not render') await send('Page.reload') await waitFor(send, "Boolean(document.querySelector('.intro-field'))", 'G03 ancestor step did not survive reload') await setInput(send, 0, '汤始祖') await valueOf(send, "document.querySelector('.flow-primary-action')?.click()") await waitFor(send, "location.href.includes('/pages/tree/t01-tree-overview?genealogyId=')", 'Saving the first ancestor did not open T01') await openDefaultG03(send) if (await valueOf(send, "Boolean(document.querySelector('.intro-field'))")) { throw new Error('Default G03 must not render the ancestor step') } process.stdout.write('G03-CREATE-FLOW-RUNTIME-SMOKE PASS\n') } finally { socket.close() } } run().catch((error) => { process.stderr.write(`${error.stack || error.message}\n`) process.exit(1) })