104 lines
3.8 KiB
JavaScript
104 lines
3.8 KiB
JavaScript
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 setSearchKeyword = async (send, value) => {
|
|
const expression = `(() => {
|
|
const input = document.querySelector('.search-input input')
|
|
const action = document.querySelector('.search-action')
|
|
if (!input || !action) return false
|
|
input.value = ${JSON.stringify(value)}
|
|
input.dispatchEvent(new Event('input', { bubbles: true }))
|
|
action.click()
|
|
return true
|
|
})()`
|
|
if (!await valueOf(send, expression)) throw new Error('Could not enter and submit a G06 search')
|
|
}
|
|
|
|
const origin = process.argv[2] || 'http://localhost:5173'
|
|
const g06Url = `${origin}/#/pages/genealogy/g06-search-genealogies`
|
|
|
|
const openG06 = async (send) => {
|
|
await send('Page.navigate', { url: g06Url })
|
|
await waitFor(send, `location.href === ${JSON.stringify(g06Url)}`, 'Browser did not navigate back to G06')
|
|
await send('Page.reload')
|
|
await waitFor(send, "Boolean(document.querySelector('.search-initial'))", 'G06 initial state did not render')
|
|
}
|
|
|
|
const run = async () => {
|
|
const { socket, send } = await connect()
|
|
|
|
try {
|
|
await send('Page.enable')
|
|
await send('Runtime.enable')
|
|
|
|
await openG06(send)
|
|
if (await valueOf(send, "Boolean(document.querySelector('.genealogy-card'))")) {
|
|
throw new Error('G06 initial state must not reveal a result card')
|
|
}
|
|
|
|
await setSearchKeyword(send, '汤')
|
|
await waitFor(send, "Boolean(document.querySelector('.search-results .genealogy-card'))", 'G06 matching search did not render a result card')
|
|
await valueOf(send, "document.querySelector('.search-results .genealogy-card')?.click()")
|
|
await waitFor(send, "location.href.includes('/pages/genealogy/g08-join-application?genealogyId=')", 'Selecting a G06 search result did not open G08')
|
|
|
|
await openG06(send)
|
|
await setSearchKeyword(send, '不存在的家谱')
|
|
await waitFor(send, "Boolean(document.querySelector('.search-empty'))", 'G06 empty search state did not render')
|
|
if (await valueOf(send, "Boolean(document.querySelector('.search-empty .genealogy-card'))")) {
|
|
throw new Error('G06 empty search state must not contain a result card')
|
|
}
|
|
|
|
process.stdout.write('G06-SEARCH-FLOW-RUNTIME-SMOKE PASS\n')
|
|
} finally {
|
|
socket.close()
|
|
}
|
|
}
|
|
|
|
run().catch((error) => {
|
|
process.stderr.write(`${error.stack || error.message}\n`)
|
|
process.exit(1)
|
|
})
|