验收20%
This commit is contained in:
@@ -1,98 +1,116 @@
|
||||
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 pages = await (await fetch('http://127.0.0.1:9222/json/list')).json()
|
||||
const page = pages.find((item) => item.type === 'page' && item.url.startsWith('http://localhost:5173'))
|
||||
if (!page) throw new Error('Chrome debugging has no localhost:5173 page')
|
||||
|
||||
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 }
|
||||
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 < 40; attempt += 1) {
|
||||
for (let attempt = 0; attempt < 50; 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')
|
||||
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 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 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 } = await connect()
|
||||
|
||||
const { socket, send, exceptions } = 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')
|
||||
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 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 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 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')
|
||||
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, '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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user