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.startsWith(origin)) 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() 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 < 30; 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 origin = process.argv[2] || 'http://localhost:5173' const g01Path = '/pages/genealogy/g01-my-genealogies' let auditId = 0 const nextUrl = (query = '') => { auditId += 1 return `${origin}/?g01Audit=${auditId}#${g01Path}${query}` } const openEmptyG01 = async (send) => { const emptyUrl = nextUrl('?state=empty') await send('Page.navigate', { url: emptyUrl }) await waitFor(send, "location.href.includes('g01-my-genealogies?state=empty')", 'Did not navigate to G01 empty state') await waitFor(send, "Boolean(document.querySelector('.genealogy-empty-state .empty-create-action'))", 'G01 empty state did not render') } const run = async () => { const { socket, send, exceptions } = await connect() try { await send('Page.enable') await send('Runtime.enable') await openEmptyG01(send) await valueOf(send, "document.querySelector('.empty-create-action')?.click()") await waitFor(send, "location.href.includes('/pages/genealogy/g03-create-genealogy')", 'G01 empty create action did not open G03') await openEmptyG01(send) await valueOf(send, "document.querySelector('.empty-search-action')?.click()") await waitFor(send, "location.href.includes('/pages/genealogy/g06-search-genealogies')", 'G01 empty search action did not open G06') await openEmptyG01(send) await valueOf(send, "document.querySelector('.empty-invite-action')?.click()") await waitFor(send, "location.href.includes('/pages/genealogy/g06-search-genealogies?mode=invite')", 'G01 empty invite action did not open G06 invite mode') const defaultUrl = nextUrl() await send('Page.navigate', { url: defaultUrl }) await waitFor(send, "location.href.endsWith('/pages/genealogy/g01-my-genealogies')", 'Did not navigate to default G01') await waitFor(send, "Boolean(document.querySelector('.current-slip'))", 'Default G01 list did not render') if (await valueOf(send, "Boolean(document.querySelector('.genealogy-empty-state'))")) { throw new Error('Default G01 must not render the empty state') } if ((await valueOf(send, "document.querySelectorAll('.application-record').length")) !== 3) { throw new Error('Default G01 must render the application status group') } await valueOf(send, "document.querySelector('.create-action').click()") await waitFor(send, "Boolean(document.querySelector('.add-dialog-layer'))", 'G01 add action did not open the custom add dialog') if ((await valueOf(send, "document.querySelectorAll('.add-dialog .app-button').length")) !== 3) { throw new Error('G01 add dialog must keep search, invite, and create visible together') } await valueOf(send, "document.querySelector('.add-dialog__close').click()") await waitFor(send, "!document.querySelector('.add-dialog-layer')", 'G01 add dialog did not close') await valueOf(send, "document.querySelector('.current-slip').click()") await waitFor(send, "Boolean(document.querySelector('.genealogy-switcher-layer'))", 'G01 current genealogy did not open the switcher') await valueOf(send, "document.querySelectorAll('.switcher-item')[1].click()") await waitFor(send, "document.querySelector('.current-name')?.textContent.includes('汤氏宗谱')", 'G01 switcher did not change the displayed current genealogy') for (const state of ['loading', 'error']) { const stateUrl = nextUrl(`?state=${state}`) await send('Page.navigate', { url: stateUrl }) await waitFor(send, `Boolean(document.querySelector('.state-panel--${state}'))`, `G01 did not render ${state}`) } for (const size of [{ width: 320, height: 568 }, { width: 360, height: 640 }, { width: 360, height: 800 }, { width: 412, height: 915 }, { width: 412, height: 1000 }]) { await send('Emulation.setDeviceMetricsOverride', { ...size, deviceScaleFactor: 1, mobile: true }) const responsiveUrl = nextUrl() await send('Page.navigate', { url: responsiveUrl }) await waitFor(send, "Boolean(document.querySelector('.current-slip'))", `G01 did not render at ${size.width}x${size.height}`) const width = await valueOf(send, 'document.documentElement.scrollWidth') if (width > size.width + 1) throw new Error(`G01 has horizontal overflow at ${size.width}x${size.height}`) const before = await valueOf(send, `(() => { const host = document.querySelector('.genealogy-list-scroll') if (!host) return null const scrollNodes = host.querySelectorAll('.uni-scroll-view') const scroller = scrollNodes[scrollNodes.length - 1] || host.shadowRoot?.querySelector('.uni-scroll-view') || host const fixedSelectors = ['.page-header', '.current-slip', '.shortcut-grid', '.section-divider', '.app-tabbar'] return { fixed: Object.fromEntries(fixedSelectors.map((selector) => [selector, document.querySelector(selector)?.getBoundingClientRect().top])), lowerTop: document.querySelector('.section-heading')?.getBoundingClientRect().top, clientHeight: scroller.clientHeight, scrollHeight: scroller.scrollHeight } })()`) if (!before) throw new Error(`G01 has no independent list scroller at ${size.width}x${size.height}`) if (before.clientHeight <= 0 || before.scrollHeight <= before.clientHeight) { throw new Error(`G01 independent list scroller has no usable range at ${size.width}x${size.height}: ${JSON.stringify(before)}`) } await valueOf(send, `(() => { const host = document.querySelector('.genealogy-list-scroll') const scrollNodes = host.querySelectorAll('.uni-scroll-view') const scroller = scrollNodes[scrollNodes.length - 1] || host.shadowRoot?.querySelector('.uni-scroll-view') || host scroller.scrollTop = Math.min(180, scroller.scrollHeight - scroller.clientHeight) scroller.dispatchEvent(new Event('scroll', { bubbles: true })) })()`) await sleep(100) const after = await valueOf(send, `(() => { const fixedSelectors = ['.page-header', '.current-slip', '.shortcut-grid', '.section-divider', '.app-tabbar'] return { fixed: Object.fromEntries(fixedSelectors.map((selector) => [selector, document.querySelector(selector)?.getBoundingClientRect().top])), lowerTop: document.querySelector('.section-heading')?.getBoundingClientRect().top } })()`) for (const selector of Object.keys(before.fixed)) { if (Math.abs(after.fixed[selector] - before.fixed[selector]) > 1) { throw new Error(`G01 fixed selector moved while list scrolled at ${size.width}x${size.height}: ${selector}`) } } if (!(after.lowerTop < before.lowerTop - 2)) { throw new Error(`G01 lower list did not move independently at ${size.width}x${size.height}`) } } const readListScrollTop = `(() => { const host = document.querySelector('.genealogy-list-scroll') const scrollNodes = host.querySelectorAll('.uni-scroll-view') const scroller = scrollNodes[scrollNodes.length - 1] || host.shadowRoot?.querySelector('.uni-scroll-view') || host return scroller.scrollTop })()` await valueOf(send, `(() => { const host = document.querySelector('.genealogy-list-scroll') const scrollNodes = host.querySelectorAll('.uni-scroll-view') const scroller = scrollNodes[scrollNodes.length - 1] || host.shadowRoot?.querySelector('.uni-scroll-view') || host scroller.scrollTop = scroller.scrollHeight })()`) await sleep(100) const endGeometry = await valueOf(send, `(() => { const action = document.querySelector('.create-action').getBoundingClientRect() const tabbar = document.querySelector('.app-tabbar').getBoundingClientRect() return { actionTop: action.top, actionBottom: action.bottom, tabbarTop: tabbar.top } })()`) if (endGeometry.actionTop >= endGeometry.tabbarTop || endGeometry.actionBottom > endGeometry.tabbarTop + 1) { throw new Error(`G01 add action is not fully reachable above the fixed tabbar: ${JSON.stringify(endGeometry)}`) } const scrollTopBeforeDialog = await valueOf(send, readListScrollTop) if (scrollTopBeforeDialog <= 0) throw new Error('G01 list must be scrolled before testing position preservation') await valueOf(send, "document.querySelector('.create-action').click()") await waitFor(send, "Boolean(document.querySelector('.add-dialog-layer'))", 'G01 add dialog did not open over the independent list') await valueOf(send, "document.querySelector('.add-dialog__close').click()") await waitFor(send, "!document.querySelector('.add-dialog-layer')", 'G01 add dialog did not close over the independent list') const scrollTopAfterDialog = await valueOf(send, readListScrollTop) if (Math.abs(scrollTopAfterDialog - scrollTopBeforeDialog) > 1) { throw new Error('G01 add dialog changed the independent list position') } await valueOf(send, "document.querySelector('.current-slip').click()") await waitFor(send, "Boolean(document.querySelector('.genealogy-switcher-layer'))", 'G01 switcher did not open over the independent list') await valueOf(send, "document.querySelectorAll('.switcher-item')[1].click()") await waitFor(send, "document.querySelector('.current-name')?.textContent.includes('汤氏宗谱')", 'G01 switcher did not change the current genealogy after scrolling') await waitFor(send, `${readListScrollTop} <= 1`, 'G01 independent list did not return to the top after switching genealogy') if (exceptions.length) throw new Error(`G01 raised browser exceptions: ${exceptions.join('; ')}`) process.stdout.write('G01-EMPTY-STATE-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) })