"use strict"; const assert = require("assert"); const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); const origin = process.argv[2] || "http://localhost:5173"; const connect = async () => { const pages = await (await fetch("http://127.0.0.1:9222/json/list")).json(); const page = pages.find( (candidate) => candidate.type === "page" && candidate.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 < 60; attempt += 1) { if (await valueOf(send, expression)) return; await sleep(100); } throw new Error(message); }; let auditId = 0; const open = async (send, route, selector) => { auditId += 1; const url = `${origin}/?rBusiness=${auditId}#${route}`; await send("Page.navigate", { url }); await waitFor(send, `location.href===${JSON.stringify(url)}`, `navigation failed: ${route}`); await waitFor( send, `Boolean(document.querySelector(${JSON.stringify(selector)}))`, `missing ${selector}: ${route}`, ); }; const click = (send, selector) => valueOf(send, `document.querySelector(${JSON.stringify(selector)})?.click()`); const inputAt = (send, selector, index, value) => valueOf( send, `(() => { const input=document.querySelectorAll(${JSON.stringify(selector)})[${index}]; input.value=${JSON.stringify(value)}; input.dispatchEvent(new Event('input',{bubbles:true})); return input.value; })()`, ); const routes = [ ["/pages/records/r03-gift-list?genealogyId=1001", ".record-card"], ["/pages/records/r04-gift-editor?genealogyId=1001&mode=create", ".form-card"], ["/pages/records/r05-ritual-list?genealogyId=1001", ".record-card"], ["/pages/records/r06-ritual-detail?genealogyId=1001&ceremonyId=501", ".detail-card"], ["/pages/records/r07-ritual-editor?genealogyId=1001&mode=create", ".form-card"], ["/pages/records/r08-growth-journal?genealogyId=1001&personId=101", ".timeline-card"], ["/pages/records/r09-life-events?genealogyId=1001&personId=101", ".service-state--unavailable"], ["/pages/records/r10-memo-list?genealogyId=1001", ".memo-card"], ["/pages/records/r11-merit-records?genealogyId=1001", ".merit-card"], ]; const run = async () => { const { socket, send } = await connect(); try { await send("Page.enable"); await send("Runtime.enable"); for (const size of [ { width: 320, height: 568 }, { width: 412, height: 915 }, ]) { await send("Emulation.setDeviceMetricsOverride", { ...size, deviceScaleFactor: 1, mobile: true, screenWidth: size.width, screenHeight: size.height, }); for (const [route, selector] of routes) { await open(send, route, selector); const documentWidth = await valueOf(send, "document.documentElement.scrollWidth"); assert(documentWidth <= size.width + 1, `${route} overflows at ${size.width}`); } } await open(send, "/pages/records/r08-growth-journal?genealogyId=1002&personId=101", ".timeline-state--invalid"); await open(send, "/pages/records/r09-life-events", ".service-state--invalid"); await open(send, "/pages/records/r03-gift-list?genealogyId=1001", ".record-card"); await click(send, ".record-card"); await waitFor( send, "location.hash.includes('genealogyId=1001&mode=view&relativeId=301&sourceKey=R03')", "R03 did not open the scoped R04 record", ); await open(send, "/pages/records/r05-ritual-list?genealogyId=1001", ".record-card"); await click(send, ".record-card"); await waitFor( send, "location.hash.includes('genealogyId=1001&ceremonyId=501&sourceKey=R05')", "R05 did not open the scoped R06 ceremony", ); await open(send, "/pages/records/r08-growth-journal?genealogyId=1001&personId=101", ".timeline-card"); const growthCount = await valueOf(send, "document.querySelectorAll('.timeline-card').length"); await click(send, ".header-action"); await waitFor(send, "Boolean(document.querySelector('.app-dialog-layer'))", "R08 editor missing"); await inputAt(send, ".dialog-form input", 0, "第一次远行"); await click(send, ".app-dialog__actions .app-button:last-child"); await waitFor( send, "document.querySelector('.preview-card')?.innerText.includes('第一次远行')", "R08 local preview missing", ); assert.strictEqual( await valueOf(send, "document.querySelectorAll('.timeline-card').length"), growthCount, "R08 local preview must not enter the official timeline", ); await open(send, "/pages/records/r11-merit-records?genealogyId=1001", ".merit-card"); const meritCount = await valueOf(send, "document.querySelectorAll('.merit-card').length"); await click(send, ".header-action"); await inputAt(send, ".dialog-form input", 0, "整理旧谱"); await inputAt(send, ".dialog-form input", 1, "汤文清"); await click(send, ".app-dialog__actions .app-button:last-child"); await waitFor( send, "document.querySelector('.preview-card')?.innerText.includes('整理旧谱')", "R11 local preview missing", ); assert.strictEqual( await valueOf(send, "document.querySelectorAll('.merit-card').length"), meritCount, "R11 local preview must not change the official count", ); process.stdout.write("R-BUSINESS-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); });