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(`${origin}/`)) if (!page) throw new Error(`Chrome debugging has no ${origin} project 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) message.error ? request.reject(new Error(message.error.message)) : 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) => ( await send('Runtime.evaluate', { expression, returnByValue: true }) ).result?.value const waitFor = async (send, expression, message) => { for (let attempt = 0; attempt < 50; attempt += 1) { if (await valueOf(send, expression)) return await sleep(80) } throw new Error(message) } const run = async () => { const { socket, send } = await connect() let auditId = 0 const navigate = async (route, selector) => { auditId += 1 const url = `${origin}/?f09RuntimeAudit=${auditId}#${route}` await send('Page.navigate', { url }) await waitFor(send, `location.href === ${JSON.stringify(url)}`, `F09 navigation failed: ${route}`) await waitFor(send, `Boolean(document.querySelector(${JSON.stringify(selector)}))`, `F09 state missing: ${selector}`) } try { await send('Page.enable') await send('Runtime.enable') await navigate('/pages/family/f09-media-upload', '.media-upload-state--initial') assert.strictEqual(await valueOf(send, "document.querySelectorAll('.media-photo-tile').length"), 0) await send('Runtime.evaluate', { expression: "document.querySelector('.media-primary-action')?.click()" }) await waitFor(send, "Boolean(document.querySelector('.media-upload-state--selected'))", 'F09 did not enter selected state') assert.strictEqual(await valueOf(send, "document.querySelectorAll('.media-photo-tile').length"), 4) await send('Runtime.evaluate', { expression: "document.querySelectorAll('.media-photo-tile')[1]?.click()" }) await waitFor(send, "document.querySelectorAll('.media-photo-tile')[1]?.classList.contains('media-photo-tile--active') === true", 'F09 did not switch current photo') await waitFor(send, "document.querySelector('.media-photo-editor__position')?.textContent.includes('第 2 张 / 共 4 张') === true", 'F09 editor did not identify the second photo') await waitFor(send, "document.querySelector('.media-photo-editor__thumb img')?.currentSrc.includes('f08-family-portrait.png') === true", 'F09 editor thumbnail did not follow the active photo') await send('Runtime.evaluate', { expression: `(() => { const textarea = document.querySelector('.media-photo-field textarea') textarea.value = '院前合影' textarea.dispatchEvent(new Event('input', { bubbles: true })) document.querySelectorAll('.media-photo-tile')[0]?.click() document.querySelectorAll('.media-photo-tile')[1]?.click() })()` }) await waitFor(send, "document.querySelector('.media-photo-field textarea')?.value === '院前合影'", 'F09 per-photo note did not remain bound after switching photos') await send('Runtime.evaluate', { expression: "document.querySelectorAll('.media-photo-remove')[1]?.click()" }) await waitFor(send, "document.querySelectorAll('.media-photo-tile').length === 3", 'F09 did not remove one photo') await send('Runtime.evaluate', { expression: "document.querySelector('.media-primary-action')?.click()" }) await waitFor(send, "Boolean(document.querySelector('.media-field-error'))", 'F09 batch description validation did not render') await send('Runtime.evaluate', { expression: `(() => { const textarea = document.querySelector('.media-batch-field textarea') textarea.value = '春节团圆照片整理' textarea.dispatchEvent(new Event('input', { bubbles: true })) document.querySelector('.media-primary-action')?.click() })()` }) await waitFor(send, "Boolean(document.querySelector('.media-upload-state--uploading'))", 'F09 did not enter uploading state') await navigate('/pages/family/f09-media-upload?state=permission', '.media-upload-state--permission') await send('Runtime.evaluate', { expression: "document.querySelector('.media-permission-action')?.click()" }) await waitFor(send, "Boolean(document.querySelector('.media-upload-state--selected'))", 'F09 permission action did not return to selected state') await navigate('/pages/family/f09-media-upload?state=error', '.media-upload-state--error') assert.strictEqual(await valueOf(send, "document.querySelectorAll('.media-photo-status--error').length"), 2) await send('Runtime.evaluate', { expression: "document.querySelector('.media-retry-action')?.click()" }) await waitFor(send, "Boolean(document.querySelector('.media-upload-state--uploading'))", 'F09 retry did not enter uploading state') await navigate('/pages/family/f09-media-upload?state=success', '.media-upload-state--success') await waitFor(send, "document.querySelector('.media-success-title')?.textContent.includes('4 张照片已上传') === true", 'F09 success copy missing') process.stdout.write('F09-MEDIA-UPLOAD-RUNTIME-SMOKE PASS\n') } finally { socket.close() } } run().catch((error) => { process.stderr.write(`${error.stack || error.message}\n`) process.exit(1) })