Files
jiapuapp/tests/g06-search-flow-runtime-smoke.js
T
2026-07-22 17:31:38 +08:00

131 lines
6.5 KiB
JavaScript

const assert = require('assert')
const origin = process.argv[2] || 'http://localhost:5173'
const sleep = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds))
const connect = async () => {
const port = process.env.CHROME_DEBUGGING_PORT || '9222'
const pages = await (await fetch(`http://127.0.0.1:${port}/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 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)
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 < 50; attempt += 1) {
if (await valueOf(send, expression)) return
await sleep(100)
}
throw new Error(message)
}
let navigationId = 0
const openG06 = async (send, mode = 'search') => {
navigationId += 1
const modeQuery = mode === 'invite' ? '?mode=invite' : ''
const url = `${origin}/?g06Audit=${navigationId}#/pages/genealogy/g06-search-genealogies${modeQuery}`
await send('Page.navigate', { url })
await waitFor(send, `document.querySelector('.mode-tab--active')?.textContent.includes(${JSON.stringify(mode === 'invite' ? '邀请码' : '搜索')})`, `G06 ${mode} mode did not render`)
}
const setInput = async (send, selector, value) => {
await valueOf(send, `(() => {
const input = document.querySelector(${JSON.stringify(selector)})
if (!input) return false
input.value = ${JSON.stringify(value)}
input.dispatchEvent(new Event('input', { bubbles: true }))
return true
})()`)
await sleep(100)
}
const run = async () => {
const { socket, send, exceptions } = await connect()
try {
await send('Page.enable')
await send('Runtime.enable')
await openG06(send)
assert.strictEqual(await valueOf(send, "Boolean(document.querySelector('.genealogy-card'))"), false, 'G06 initial state revealed results')
await setInput(send, '.search-input input', '汤')
await valueOf(send, "document.querySelector('.search-action').click()")
await waitFor(send, "document.querySelectorAll('.search-results .result-card').length === 6", 'G06 did not render all six relation states')
const firstCardText = await valueOf(send, "document.querySelector('.result-card').textContent")
for (const detail of ['地区', '所属上级谱', '当前支系', '管理信息', '位成员', '更新于']) {
assert(firstCardText.includes(detail), `G06 result card is missing ${detail}`)
}
await valueOf(send, "document.querySelector('.result-card').click()")
await waitFor(send, "location.href.includes('/pages/genealogy/g05-genealogy-overview?mode=public&genealogyId=')", 'G06 available card did not open G05 public preview')
await openG06(send)
await setInput(send, '.search-input input', '不存在的家谱')
await valueOf(send, "document.querySelector('.search-action').click()")
await waitFor(send, "Boolean(document.querySelector('.search-empty'))", 'G06 empty result state did not render')
// 步骤三:失败关键词必须进入错误态,页面内“重新搜索”要真正再次触发加载,而不是静态摆设。
await openG06(send)
await setInput(send, '.search-input input', '失败')
await valueOf(send, "document.querySelector('.search-action').click()")
await waitFor(send, "Boolean(document.querySelector('.search-error'))", 'G06 search failure state did not render')
await valueOf(send, "document.querySelector('.status-retry').click()")
await waitFor(send, "Boolean(document.querySelector('.app-loading'))", 'G06 retry did not restart loading')
await waitFor(send, "Boolean(document.querySelector('.search-error'))", 'G06 retry did not return the simulated failure state')
await openG06(send, 'invite')
await setInput(send, '.invite-input input', 'BAD-CODE')
await valueOf(send, "document.querySelector('.search-action').click()")
await waitFor(send, "Boolean(document.querySelector('.invite-invalid'))", 'G06 invalid invite state did not render')
await setInput(send, '.invite-input input', 'JP2026')
await valueOf(send, "document.querySelector('.search-action').click()")
await waitFor(send, "Boolean(document.querySelector('.invite-result .result-card'))", 'G06 valid invite target did not render')
await valueOf(send, "document.querySelector('.invite-result .result-card__action').click()")
await waitFor(send, "location.href.includes('/pages/genealogy/g08-join-application?source=invite&genealogyId=')", 'G06 invite action did not open G08 invite source')
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 openG06(send)
const width = await valueOf(send, 'document.documentElement.scrollWidth')
assert(width <= size.width + 1, `G06 has horizontal overflow at ${size.width}x${size.height}`)
}
assert.deepStrictEqual(exceptions, [], `G06 raised browser exceptions: ${exceptions.join('; ')}`)
process.stdout.write('G06-SEARCH-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)
})