// 浏览器端到端验证:真实 WebRTC 推流、家长观看、巡课多画面、Egress 录像与回放。 // 使用无头 Chrome + 虚拟摄像头/麦克风,全程通过真实浏览器跑,不是模拟。 // 运行:node scripts/e2e-browser.mjs import { spawn, execFileSync } from "node:child_process"; import { readFileSync, mkdtempSync, mkdirSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, dirname } from "node:path"; import { fileURLToPath } from "node:url"; const ROOT = join(dirname(fileURLToPath(import.meta.url)), ".."); const WEB = "http://localhost:18088"; const API = "http://localhost:8080/api"; const CHROME = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"; const PORT = 9333; const ARTIFACTS = join(ROOT, "scripts", ".e2e-artifacts"); const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); const stamp = new Date().toISOString().replace(/[-:T]/g, "").slice(0, 14); const results = []; let failures = 0; function pass(name, detail = "") { results.push(`PASS ${name}${detail ? ` — ${detail}` : ""}`); console.log(`PASS ${name}${detail ? ` — ${detail}` : ""}`); } function fail(name, detail = "") { failures++; results.push(`FAIL ${name}${detail ? ` — ${detail}` : ""}`); console.log(`FAIL ${name}${detail ? ` — ${detail}` : ""}`); } function envValue(key) { const text = readFileSync(join(ROOT, ".env"), "utf8"); const line = text.split(/\r?\n/).find((l) => l.trim().startsWith(`${key}=`)); return line ? line.split("=").slice(1).join("=").trim() : ""; } async function api(method, path, body, token) { const res = await fetch(`${API}${path}`, { method, headers: { "Content-Type": "application/json", "X-XH-Request": "1", ...(token ? { Authorization: `Bearer ${token}` } : {}) }, body: body === undefined ? undefined : JSON.stringify(body) }); const json = await res.json(); if (!json.success) throw new Error(`${method} ${path} -> ${json.code} ${json.message}`); return json.data; } // ---------- CDP ---------- class Cdp { constructor(ws) { this.ws = ws; this.id = 0; this.pending = new Map(); this.sessionEvents = []; ws.onmessage = (ev) => { const msg = JSON.parse(ev.data); if (msg.id && this.pending.has(msg.id)) { const { resolve, reject } = this.pending.get(msg.id); this.pending.delete(msg.id); msg.error ? reject(new Error(`${msg.error.message} (${msg.error.data ?? ""})`)) : resolve(msg.result); } else { this.sessionEvents.push(msg); } }; } static async connect(url) { const ws = new WebSocket(url); await new Promise((resolve, reject) => { ws.onopen = resolve; ws.onerror = () => reject(new Error(`CDP 连接失败 ${url}`)); }); return new Cdp(ws); } send(method, params = {}, sessionId) { const id = ++this.id; const payload = { id, method, params }; if (sessionId) payload.sessionId = sessionId; this.ws.send(JSON.stringify(payload)); return new Promise((resolve, reject) => this.pending.set(id, { resolve, reject })); } consumeEvents(sessionId) { const mine = this.sessionEvents.filter((e) => e.sessionId === sessionId || e.method === "Target.targetDestroyed"); this.sessionEvents = []; return mine; } } class Page { constructor(cdp, sessionId, name) { this.cdp = cdp; this.sessionId = sessionId; this.name = name; this.errors = []; } async send(method, params = {}) { return this.cdp.send(method, params, this.sessionId); } async enable() { await this.send("Page.enable"); await this.send("Runtime.enable"); await this.send("Log.enable"); } async injectBeforeLoad(script) { await this.send("Page.addScriptToEvaluateOnNewDocument", { source: script }); } async navigate(url) { await this.send("Page.navigate", { url }); await this.waitFor(`document.readyState === "complete"`, 15000, `页面加载 ${url}`); await sleep(800); } async eval(expression) { const r = await this.send("Runtime.evaluate", { expression, returnByValue: true, awaitPromise: true }); if (r.exceptionDetails) { throw new Error(`页面脚本异常: ${r.exceptionDetails.text} ${r.exceptionDetails.exception?.description ?? ""}`); } return r.result?.value; } async waitFor(expression, timeoutMs, desc) { const deadline = Date.now() + timeoutMs; while (Date.now() < deadline) { try { if (await this.eval(expression)) return; } catch { /* 页面导航期间忽略 */ } await sleep(500); } throw new Error(`等待超时: ${desc}`); } async clickText(text) { await this.eval(`(() => { const b = Array.from(document.querySelectorAll("button")).find((x) => x.textContent.trim() === ${JSON.stringify(text)}); if (!b) throw new Error("找不到按钮: " + ${JSON.stringify(text)}); b.click(); return true; })()`); } async clickNthText(text, index) { await this.eval(`(() => { const buttons = Array.from(document.querySelectorAll("button")).filter((x) => x.textContent.trim() === ${JSON.stringify(text)}); const b = buttons[${index}]; if (!b) throw new Error("找不到第 ${index} 个按钮: " + ${JSON.stringify(text)} + ",现有 " + buttons.length + " 个"); b.click(); return true; })()`); } async click(selector) { await this.eval(`(() => { const el = document.querySelector(${JSON.stringify(selector)}); if (!el) throw new Error("找不到元素: " + ${JSON.stringify(selector)}); el.click(); return true; })()`); } async selectByPlaceholder(placeholder, optionText) { await this.eval(`(() => { const sels = Array.from(document.querySelectorAll(".el-select")); const sel = sels.find((s) => (s.textContent ?? "").includes(${JSON.stringify(placeholder)})); if (!sel) throw new Error("找不到下拉框: " + ${JSON.stringify(placeholder)}); (sel.querySelector(".el-select__wrapper") ?? sel).click(); return true; })()`); await sleep(500); await this.eval(`(() => { const opts = Array.from(document.querySelectorAll(".el-select-dropdown__item")); const o = opts.find((x) => x.textContent.trim() === ${JSON.stringify(optionText)}); if (!o) throw new Error("找不到选项 " + ${JSON.stringify(optionText)} + ",现有: " + JSON.stringify(opts.map((x) => x.textContent.trim()))); o.click(); return true; })()`); await sleep(400); } async fillInputs(values) { await this.eval(`(() => { const inputs = document.querySelectorAll("input"); const values = ${JSON.stringify(values)}; const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value").set; values.forEach((v, i) => { const el = inputs[i]; if (!el) throw new Error("输入框数量不足"); setter.call(el, v); el.dispatchEvent(new Event("input", { bubbles: true })); }); return true; })()`); } async selectFormItem(labelPart, optionText) { await this.eval(`(() => { const items = Array.from(document.querySelectorAll(".el-form-item")); const item = items.find((f) => (f.querySelector("label")?.textContent ?? "").includes(${JSON.stringify(labelPart)})); if (!item) throw new Error("找不到表单项: " + ${JSON.stringify(labelPart)}); const sel = item.querySelector(".el-select"); if (!sel) throw new Error("表单项里没有下拉框: " + ${JSON.stringify(labelPart)}); (sel.querySelector(".el-select__wrapper") ?? sel).click(); return true; })()`); await sleep(500); await this.eval(`(() => { const opts = Array.from(document.querySelectorAll(".el-select-dropdown__item")); const o = opts.find((x) => x.textContent.trim() === ${JSON.stringify(optionText)}); if (!o) throw new Error("找不到选项 " + ${JSON.stringify(optionText)} + ",现有: " + JSON.stringify(opts.map((x) => x.textContent.trim()))); o.click(); return true; })()`); await sleep(400); } async stats() { return this.eval("window.__xhStats ?? null"); } async screenshot(file) { try { const r = await this.send("Page.captureScreenshot", { format: "png" }); writeFileSync(file, Buffer.from(r.data, "base64")); } catch { /* 截图失败不影响流程 */ } } collectErrors() { for (const ev of this.cdp.consumeEvents(this.sessionId)) { if (ev.method === "Runtime.exceptionThrown") { this.errors.push(ev.params.exceptionDetails?.exception?.description ?? ev.params.exceptionDetails?.text); } if (ev.method === "Runtime.consoleAPICalled" && ev.params.type === "error") { this.errors.push(ev.params.args.map((a) => a.value ?? a.description ?? "").join(" ")); } if (ev.method === "Log.entryAdded" && ["error", "warning"].includes(ev.params.entry.level)) { this.errors.push(`[${ev.params.entry.level}] ${ev.params.entry.text}`); } } } } const INSTRUMENT = `(() => { window.__xhStats = { outbound: {}, inbound: {}, pcStates: [] }; const origStats = RTCPeerConnection.prototype.getStats; RTCPeerConnection.prototype.getStats = function () { const promise = origStats.apply(this, arguments); if (this.__xhTracked) return promise; this.__xhTracked = true; const rec = window.__xhStats; const timer = setInterval(async () => { try { const report = await origStats.call(this); report.forEach((s) => { if (s.type === "outbound-rtp" && s.bytesSent > 0) rec.outbound[s.kind] = Math.max(rec.outbound[s.kind] || 0, s.bytesSent); if (s.type === "inbound-rtp" && s.bytesReceived > 0) rec.inbound[s.kind] = Math.max(rec.inbound[s.kind] || 0, s.bytesReceived); }); } catch (e) { /* pc 已关闭 */ } }, 1000); return promise; }; const origAdd = RTCPeerConnection.prototype.addEventListener; RTCPeerConnection.prototype.addEventListener = function (type, fn, ...rest) { if (type === "iceconnectionstatechange" || type === "connectionstatechange") { const wrapped = (e) => { window.__xhStats.pcStates.push({ type, state: this[type === "iceconnectionstatechange" ? "iceConnectionState" : "connectionState"], t: Date.now() }); fn(e); }; return origAdd.call(this, type, wrapped, ...rest); } return origAdd.call(this, type, fn, ...rest); }; })();`; async function startChrome() { const profile = mkdtempSync(join(tmpdir(), "xh-e2e-")); mkdirSync(ARTIFACTS, { recursive: true }); spawn(CHROME, [ "--headless=new", `--remote-debugging-port=${PORT}`, `--user-data-dir=${profile}`, "--use-fake-ui-for-media-stream", "--use-fake-device-for-media-stream", "--autoplay-policy=no-user-gesture-required", "--no-first-run", "--no-default-browser-check", "--window-size=1440,900", "about:blank" ], { stdio: "ignore", detached: false }); for (let i = 0; i < 40; i++) { try { const r = await fetch(`http://127.0.0.1:${PORT}/json/version`); return (await r.json()).webSocketDebuggerUrl; } catch { await sleep(500); } } throw new Error("Chrome 调试端口未就绪"); } async function newPage(browser, name) { const { targetId } = await browser.send("Target.createTarget", { url: "about:blank" }); const { sessionId } = await browser.send("Target.attachToTarget", { targetId, flatten: true }); const page = new Page(browser, sessionId, name); await page.enable(); await page.injectBeforeLoad(INSTRUMENT); return page; } async function login(page, account, password) { await page.navigate(`${WEB}/login`); await page.waitFor(`document.querySelectorAll("input").length >= 2`, 10000, "登录表单"); await page.fillInputs([account, password]); await page.clickText("登录"); await page.waitFor(`location.pathname !== "/login"`, 15000, "登录后跳转控制台"); await sleep(800); } async function main() { console.log(`== XH巡课 浏览器端到端验证 ${new Date().toLocaleString("zh-CN")} ==`); const adminUser = envValue("ADMIN_USERNAME") || "admin"; const adminPass = envValue("ADMIN_PASSWORD") || "ChangeMe_Admin_2026!"; // ---------- 1. API 准备数据 ---------- const adminLogin = await api("POST", "/auth/login", { account: adminUser, password: adminPass }); const adminToken = adminLogin.accessToken; const school = await api("POST", "/schools", { name: `端到端实验校${stamp}`, code: `E2E${stamp}`, liveEnabled: true, recordEnabled: true }, adminToken); const grade = await api("POST", "/grades", { schoolId: school.id, name: "一年级", sortOrder: 1 }, adminToken); const cls = await api("POST", "/classes", { schoolId: school.id, gradeId: grade.id, name: "1班" }, adminToken); const teacher = await api("POST", "/users", { username: `te${stamp}`, displayName: "张老师", password: "Teacher123", schoolId: school.id, role: "TEACHER" }, adminToken); await api("POST", `/classes/${cls.id}/teachers`, { teacherId: teacher.id, subject: "语文", head: true }, adminToken); await api("POST", "/students", { classId: cls.id, name: "小明", studentNo: `S${stamp}` }, adminToken); const course = await api("POST", "/courses", { classId: cls.id, teacherId: teacher.id, name: "语文", allowLive: true, allowRecord: true }, adminToken); const parentReg = await api("POST", "/auth/register", { username: `pa${stamp}`, displayName: "明爸", email: `pa${stamp}@example.com`, phone: `139${stamp.slice(-8)}`, password: "Parent123" }); await api("POST", `/users/${parentReg.id}/review`, { approve: true, note: "端到端审核" }, adminToken); const parentLogin = await api("POST", "/auth/login", { account: `pa${stamp}`, password: "Parent123" }); const parentToken = parentLogin.accessToken; const bind = await api("POST", "/bindings", { classId: cls.id, studentNo: `S${stamp}`, studentName: "小明", relationship: "父亲", requestLive: true, requestReplay: true }, parentToken); await api("POST", `/bindings/${bind.id}/review`, { approve: true, liveGranted: true, replayGranted: true, note: "端到端授权" }, adminToken); pass("数据准备", `学校${school.id} 班级${cls.id} 课程${course.id} 教师/家长已授权`); // ---------- 2. 浏览器 ---------- const browserWs = await startChrome(); const browser = await Cdp.connect(browserWs); const teacherPage = await newPage(browser, "teacher"); const parentPage = await newPage(browser, "parent"); const adminPage = await newPage(browser, "admin"); // ---------- 3. 教师开播 ---------- await login(teacherPage, `te${stamp}`, "Teacher123"); await teacherPage.navigate(`${WEB}/teacher`); await teacherPage.waitFor(`document.body.innerText.includes("选择课程")`, 10000, "教师控制台"); await teacherPage.clickNthText("允许并刷新", 0); await sleep(1500); await teacherPage.clickNthText("允许并刷新", 1); await sleep(1500); const deviceInfo = await teacherPage.eval(`(() => ({ cameras: Array.from(document.querySelectorAll(".el-select")).length, text: document.body.innerText.includes("摄像头") && document.body.innerText.includes("麦克风") }))()`); if (!deviceInfo.text) fail("设备区渲染", JSON.stringify(deviceInfo)); else pass("设备区渲染", "摄像头与麦克风分开列出"); await teacherPage.selectByPlaceholder("选择班级", "1班"); await teacherPage.selectByPlaceholder("选择课程", "语文"); await teacherPage.clickText("开始直播"); try { await teacherPage.waitFor(`document.body.innerText.includes("正在直播")`, 40000, "开始直播进入 LIVE"); } catch (e) { teacherPage.collectErrors(); const pageText = await teacherPage.eval("document.body.innerText.slice(0, 800)").catch(() => ""); throw new Error(`页面未显示正在直播。页面文本: ${pageText}`); } await teacherPage.screenshot(join(ARTIFACTS, "01-studio-live.png")); const liveList = await api("GET", "/live/sessions?status=LIVE", undefined, adminToken); const session = liveList.items.find((s) => s.classId === cls.id); if (!session) throw new Error("API 找不到 LIVE 会话"); await sleep(6000); pass("教师推流", `会话${session.id} 已 LIVE(推流由家长接收与录像探针双向验证)`); // ---------- 4. 家长观看 ---------- await login(parentPage, `pa${stamp}`, "Parent123"); await parentPage.navigate(`${WEB}/watch/${session.id}`); await parentPage.waitFor(`document.querySelector(".big-play")`, 15000, "观看页出现播放按钮"); await parentPage.click(".big-play"); await sleep(9000); await parentPage.screenshot(join(ARTIFACTS, "02-parent-watch.png")); const watchVideo = await parentPage.eval(`(async () => { const v = document.querySelector("video"); if (!v) return null; let audioLevel = -1; try { // 直接测量元素的远程媒体流(srcObject),不经过元素静音开关,最接近真实接收音频。 const stream = v.srcObject || v.captureStream(); const ac = new AudioContext(); const source = ac.createMediaStreamSource(stream); const analyser = ac.createAnalyser(); source.connect(analyser); const data = new Uint8Array(analyser.fftSize); let max = 0; for (let i = 0; i < 30; i++) { await new Promise((r) => setTimeout(r, 300)); analyser.getByteTimeDomainData(data); let sum = 0; for (const x of data) sum += Math.abs(x - 128); const level = Math.round(sum / data.length); if (level > max) max = level; } audioLevel = max; ac.close(); } catch (e) { audioLevel = -1; } return { width: v.videoWidth, paused: v.paused, muted: v.muted, readyState: v.readyState, frames: v.getVideoPlaybackQuality ? v.getVideoPlaybackQuality().totalVideoFrames : 0, audioLevel }; })()`); if (watchVideo && watchVideo.width > 0 && watchVideo.frames > 0 && watchVideo.audioLevel > 0) { pass("家长观看", `画面 ${watchVideo.width}px 已解码 ${watchVideo.frames} 帧,音频电平 ${watchVideo.audioLevel}`); } else { fail("家长观看", JSON.stringify(watchVideo)); } await api("POST", `/live/sessions/${session.id}/view`, undefined, parentToken); // ---------- 5. 巡课多画面 ---------- await login(adminPage, adminUser, adminPass); await adminPage.navigate(`${WEB}/inspect`); try { await adminPage.waitFor(`document.querySelectorAll("video").length >= 1`, 20000, "巡课页出现视频元素"); } catch (e) { adminPage.collectErrors(); throw e; } await sleep(9000); await adminPage.screenshot(join(ARTIFACTS, "03-inspect.png")); const inspectVideo = await adminPage.eval(`(() => { const vs = Array.from(document.querySelectorAll("video")); return vs.map((v) => ({ width: v.videoWidth, muted: v.muted })); })()`); const anyLive = inspectVideo.some((v) => v.width > 0); const allMuted = inspectVideo.every((v) => v.muted); if (anyLive && allMuted) pass("巡课多画面", `共${inspectVideo.length}间教室,默认全部静音`); else fail("巡课多画面", `anyLive=${anyLive} allMuted=${allMuted} ${JSON.stringify(inspectVideo)}`); // ---------- 5.5 管理后台概览 ---------- await adminPage.navigate(`${WEB}/admin`); try { await adminPage.waitFor(`document.body.innerText.includes("数据概览")`, 15000, "管理后台概览"); await sleep(2500); const adminOk = await adminPage.eval(`document.body.innerText.includes("用户总数") && document.body.innerText.includes("服务状态")`); if (adminOk) pass("管理后台概览", "统计卡片与服务状态渲染"); else fail("管理后台概览", "统计卡片缺失"); await adminPage.screenshot(join(ARTIFACTS, "05-admin-overview.png")); } catch (e) { adminPage.collectErrors(); throw e; } // ---------- 6. 结束直播与录像 ---------- await teacherPage.clickText("结束直播"); await sleep(2000); let recording = null; for (let i = 0; i < 30; i++) { const list = await api("GET", `/recordings?classId=${cls.id}`, undefined, adminToken); recording = list.items.find((r) => r.sessionId === session.id) ?? null; if (recording && ["COMPLETED", "FAILED"].includes(recording.status)) break; await sleep(5000); } if (!recording || recording.status !== "COMPLETED") { fail("Egress 录像", recording ? `状态=${recording.status} 原因=${recording.errorMessage}` : "没有找到录像任务"); } else { // 后端探针(ffprobe 内部预签名地址)是流级真相:必须同时有视频轨和音频轨。 const complete = recording.hasVideo && recording.hasAudio && (recording.fileSize ?? 0) > 0 && (recording.durationSeconds ?? 0) > 0; if (complete) { pass("Egress 录像", `录像${recording.id} COMPLETED 大小=${recording.fileSize} 时长=${recording.durationSeconds}s 视频+音频轨齐全`); } else { fail("Egress 录像", `COMPLETED 但 hasVideo=${recording.hasVideo} hasAudio=${recording.hasAudio} size=${recording.fileSize} duration=${recording.durationSeconds}`); } } // ---------- 6.5 录像权限隔离 ---------- { const parentRecordings = await api("GET", "/recordings?page=0&size=50", undefined, parentToken); const allOwn = parentRecordings.items.every((r) => r.classId === cls.id); if (allOwn) pass("录像权限隔离", `家长可见 ${parentRecordings.items.length} 条,全部属于授权班级`); else fail("录像权限隔离", `越权可见班级: ${JSON.stringify(parentRecordings.items.map((r) => r.classId))}`); } // ---------- 7. 回放页面 ---------- if (recording?.status === "COMPLETED") { await parentPage.navigate(`${WEB}/parent/replays`); try { await parentPage.waitFor(`document.querySelector(".replay-card")`, 15000, "回放列表"); } catch (e) { parentPage.collectErrors(); const pageText = await parentPage.eval("document.body.innerText.slice(0, 600)").catch(() => ""); throw new Error(`家长回放页没有卡片。页面文本: ${pageText}`); } await parentPage.click(".replay-card"); await sleep(7000); await parentPage.screenshot(join(ARTIFACTS, "04-replay.png")); const replayVideo = await parentPage.eval(`(() => { const v = document.querySelector(".dplayer video"); return v ? { width: v.videoWidth, paused: v.paused, readyState: v.readyState } : null; })()`); if (replayVideo && replayVideo.width > 0 && replayVideo.readyState >= 2) pass("回放播放", `DPlayer 画面 ${replayVideo.width}px`); else fail("回放播放", JSON.stringify(replayVideo)); } // ---------- 8. 清理与报告 ---------- try { await api("POST", `/live/sessions/${session.id}/force-stop`, undefined, adminToken); } catch { /* 已结束 */ } for (const page of [teacherPage, parentPage, adminPage]) page.collectErrors(); const allErrors = [...teacherPage.errors, ...parentPage.errors, ...adminPage.errors] .filter((e) => !e.includes("favicon") && !e.includes("net::ERR_ABORTED")); if (allErrors.length) { console.log("\n页面错误日志:"); allErrors.slice(0, 20).forEach((e) => console.log(" -", e.slice(0, 300))); } console.log("\n== 结果 =="); console.log(results.join("\n")); console.log(failures === 0 ? "\nE2E_OK" : `\nE2E_FAILED (${failures} 项失败)`); process.exit(failures === 0 ? 0 : 1); } main().catch((e) => { console.error("E2E 中断:", e.message); try { console.error("后端日志:", execFileSync("docker", ["logs", "--since", "8m", "xuenke-backend-1"], { encoding: "utf8" }).split("\n").slice(-15).join("\n")); } catch { /* 无日志 */ } process.exit(1); });