const sleep = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds)) const origin = process.argv.find((argument) => /^https?:\/\//.test(argument)) || 'http://localhost:5173' const connect = async () => { const pages = await (await fetch('http://127.0.0.1:9222/json/list')).json() const projectPages = pages.filter((item) => item.type === 'page' && item.url.startsWith(`${origin}/`)) if (projectPages.length !== 1) throw new Error(`Expected one ${origin} project tab, found ${projectPages.length}`) const socket = new WebSocket(projectPages[0].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(`${request.method}: ${message.error.message}`)) : request.resolve(message.result) }) const send = (method, params = {}) => new Promise((resolve, reject) => { id += 1 pending.set(id, { resolve, reject, method }) 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 < 60; attempt += 1) { try { if (await valueOf(send, expression)) return } catch (error) { if (!String(error.message).includes('Inspected target navigated or closed')) throw error } await sleep(100) } throw new Error(message) } const open = async (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`) const previousTimeOrigin = await valueOf(send, 'performance.timeOrigin') await send('Page.reload') await waitFor(send, `performance.timeOrigin !== ${JSON.stringify(previousTimeOrigin)}`, `${route} reload did not create a fresh document`) await waitFor(send, `Boolean(document.querySelector(${JSON.stringify(selector)}))`, `${route}${query} did not render ${selector}`) const overflow = await valueOf(send, 'document.documentElement.scrollWidth > innerWidth + 1') if (overflow) throw new Error(`${route}${query} overflows horizontally`) } const includesText = async (send, text) => valueOf(send, `document.body.innerText.includes(${JSON.stringify(text)})`) const assertText = async (send, text, message) => { if (!(await includesText(send, text))) throw new Error(message) } const run = async () => { const { socket, send } = await connect() try { await send('Page.enable') await send('Runtime.enable') await send('Emulation.setDeviceMetricsOverride', { width: 320, height: 568, deviceScaleFactor: 1, mobile: true }) await open(send, '/pages/tree/t03-member-profile', '?genealogyId=1001&personId=101', '.member-state--detail') await assertText(send, '汤文远', 'T03 did not load person 101') await assertText(send, '离世纪念', 'T03 did not expose the semantic deceased-state entry') await valueOf(send, 'document.querySelector(".member-status-entry").click()') await waitFor(send, 'location.hash.includes("/pages/tree/t08-member-states?genealogyId=1001&personId=101&sourceKey=T03")', 'T03 status entry did not open the routed member state') await open(send, '/pages/tree/t03-member-profile', '?genealogyId=1001&personId=102', '.member-state--restricted') await assertText(send, '汤正国', 'T03 did not derive content from personId 102') await assertText(send, '部分成员资料未公开', 'T03 did not fail closed for person 102 privacy status') await open(send, '/pages/tree/t04-add-relative', '?genealogyId=1001&personId=101', '.add-state--form') await assertText(send, '为汤文远添加一位亲属', 'T04 did not derive the current member task') await valueOf(send, 'document.querySelector(".form-action").click()') await waitFor(send, 'document.querySelectorAll(".field-error").length === 3', 'T04 did not keep required-field errors near their fields') await open(send, '/pages/tree/t04-add-relative', '?genealogyId=1001&mode=first', '.add-state--form') await assertText(send, '录入家谱中的第一位成员', 'T04 first-member mode did not render') await open(send, '/pages/tree/t05-edit-member', '?genealogyId=1001&personId=102', '.edit-state--no-permission') await assertText(send, '当前账号不能编辑这位成员', 'T05 did not fail closed for a privacy member') await open(send, '/pages/tree/t05-edit-member', '?state=no-permission&genealogyId=1001&personId=103', '.edit-state--no-permission') await assertText(send, '当前账号不能编辑这位成员', 'T05 permission state did not render') await open(send, '/pages/tree/t06-edit-relationship', '?genealogyId=1001&personId=102', '.relationship-state--form') await assertText(send, '汤正国', 'T06 did not preselect the routed member') await assertText(send, '关系影响预览', 'T06 relationship preview is missing') await valueOf(send, 'document.querySelector(".form-action").click()') await waitFor(send, 'document.querySelectorAll(".field-error").length === 2 && Boolean(document.querySelector(".relationship-state--form"))', 'T06 confused missing fields with a relationship conflict') await open(send, '/pages/tree/t06-edit-relationship', '?state=conflict&genealogyId=1001&personId=102', '.relationship-state--conflict') await assertText(send, '这段关系会造成世系矛盾', 'T06 conflict result did not render') // 冲突说明必须通过当前页面的真实操作打开共享弹窗,不能只检查静态冲突文案。 await valueOf(send, 'document.querySelector(".result-actions .form-action:last-child").click()') await waitFor(send, 'Boolean(document.querySelector(".app-dialog-layer"))', 'T06 conflict help did not open the shared dialog') await assertText(send, '为什么不能保存这段关系', 'T06 conflict help dialog title is missing') await open(send, '/pages/tree/t08-member-states', '?state=deceased&genealogyId=1001&personId=101', '.member-status--deceased') await assertText(send, '汤文远', 'T08 did not retain member identity') if (await valueOf(send, 'Boolean(document.querySelector(".status-tabs"))')) throw new Error('T08 still exposes demonstration state tabs') await open(send, '/pages/tree/t08-member-states', '?state=privacy&genealogyId=1001&personId=102', '.member-status--privacy') await assertText(send, '汤正国', 'T08 privacy state is not person-driven') await open(send, '/pages/tree/t08-member-states', '?state=forbidden&genealogyId=1001&personId=103', '.member-status--forbidden') await assertText(send, '汤正华', 'T08 forbidden state is not person-driven') await open(send, '/pages/tree/t08-member-states', '?state=privacy&genealogyId=1001', '.member-status--error') process.stdout.write('T03-T08-BUSINESS-SPECIALIZATION-RUNTIME-SMOKE PASS\n') } finally { socket.close() } } run().catch((error) => { process.stderr.write(`${error.stack || error.message}\n`) process.exit(1) })