Files
yun-xue-tang-assistant/dev/real-e2e.mjs
T
void ae117222df feat: v2.8.0 无头运行器、考试自动交卷、改为退避重试不跳课
新增无头运行能力,解决 Linux 无图形化服务器上无法常驻的问题;同时改掉
"遇到考试课/无进展课就跳过"的策略,改为在同门课上退避重试。

- runner/:自己起无头 Chromium,用 CDP 把构建产物注入页面主世界(等价
  @grant none),不装插件、不需要桌面环境或 Xvfb;含浏览器探测与启动参数、
  持久化 profile、陈旧 SingletonLock 清理、优雅退出、日志轮转、启动预检
- runner.config.mjs / start-headless.cmd:通用配置 + Windows 一键入口;
  凭据只走 YUN_XUE_TANG_USR / YUN_XUE_TANG_PWD 环境变量
- src/exam.js:含考试章节的课程自动进考试 → 开始/继续 → 空卷提交 → 确定,
  交卷后靠平台记账推进课程;maxExamTries 限制同门课交卷次数,防不记账时死循环
- 去掉跳过与黑名单:没学完就退避重试(30s 起翻倍,最长 5 分钟),
  同门课超过 maxCourseRetries 次才停整轮并提示,替代原 maxConsecutiveFailures
- 每完成一门都重新拉列表接口取平台实时状态再选下一门,避免用陈旧队列
- 学习进度改存 sessionStorage(yun-xue-tang-run-state-v3),整页跳转/刷新可续;
  运行开关不跨会话,浏览器重启后必须显式开跑
- dev/:新增 real-cdp / real-run / real-test / real-e2e 实跑脚本与 check.mjs
  统一静态检查(JS 22 + 批处理 2),替代逐文件 node --check
- docs/evidence/headless-linux-evidence.txt + 实跑截图,README 补无头部署章节
2026-10-25 23:04:40 +08:00

422 lines
21 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* 真机端到端测试:真实 Chrome + 真实云学堂账号,跑「登录 → 列表 → 进课 → 播放」全链路。
*
* 账号来源(环境变量,不写进代码、不打印明文):
* YUN_XUE_TANG_USR 员工邮箱 / 账号
* YUN_XUE_TANG_PWD 密码
*
* 用法:
* node dev/real-e2e.mjs 登录 + 列表断言(不动任何课程进度)
* node dev/real-e2e.mjs --play 额外进入最短的一门课,采样播放 N 秒后停止
* node dev/real-e2e.mjs --play --seconds 60
* node dev/real-e2e.mjs --headless 无头模式
* node dev/real-e2e.mjs --keep 跑完不关浏览器(便于人工复核)
* node dev/real-e2e.mjs --shotdir X 截图目录
*
* 安全约束(刻意保守):
* - 只有显式传 --play 才会进入课程页;默认全程只读,不改任何学习进度。
* - --play 默认只采样 25 秒,采完立刻 pause + 释放脚本,不会把整节课刷完。
* - 结束前会打印本轮进度变化(开课前 / 结束后 的 已完成 x/y),便于核对影响。
*/
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { connect, sleep, httpJson, findChrome, forceHeadless, safetyArgs } from './real-cdp.mjs';
const HERE = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.resolve(HERE, '..');
const SCRIPT_SRC = fs.readFileSync(path.join(ROOT, 'yun-xue-tang-assistant.user.js'), 'utf8');
const argv = process.argv.slice(2);
const flag = (n) => argv.includes(n);
const opt = (n, d) => {
const i = argv.indexOf(n);
return i >= 0 && argv[i + 1] ? argv[i + 1] : d;
};
const USR = process.env.YUN_XUE_TANG_USR || '';
const PWD = process.env.YUN_XUE_TANG_PWD || '';
const PLAY = flag('--play');
const SAMPLE_SECONDS = Number(opt('--seconds', 25));
const HEADLESS = flag('--headless') || process.env.YUN_XUE_TANG_HEADLESS === '1';
const KEEP = flag('--keep');
const PORT = Number(process.env.YUN_XUE_TANG_CDP_PORT || 9334);
const SHOT_DIR = opt('--shotdir', path.join(ROOT, 'docs/screenshots'));
const ORIGIN = 'https://kfmic.yunxuetang.cn';
const CANDIDATES = [
process.env.YUN_XUE_TANG_CHROME,
'C:/Program Files/Google/Chrome/Application/chrome.exe',
'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe',
'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe',
'/usr/bin/google-chrome',
'/usr/bin/chromium',
].filter(Boolean);
const CHROME = findChrome(CANDIDATES);
const results = [];
const consoleLogs = [];
const log = (...a) => console.log(...a);
function check(name, ok, detail) {
results.push({ name, ok: !!ok, detail });
log((ok ? ' PASS ' : ' FAIL ') + name + (detail === undefined ? '' : ' → ' + JSON.stringify(detail)));
}
if (!CHROME) { console.error('找不到 Chrome/Edge,可用 YUN_XUE_TANG_CHROME=... 指定'); process.exit(2); }
if (!USR || !PWD) { console.error('缺少环境变量 YUN_XUE_TANG_USR / YUN_XUE_TANG_PWD'); process.exit(2); }
if (typeof WebSocket !== 'function' || typeof fetch !== 'function') {
console.error('需要 Node 22+,当前 ' + process.version);
process.exit(2);
}
log('[E2E] 账号:' + USR.slice(0, 3) + '***' + USR.slice(-2) + ' 密码:' + '*'.repeat(Math.min(PWD.length, 8)) + '(长度 ' + PWD.length + ')');
// Linux 无图形化服务器:没有 DISPLAY 时强制无头,并补容器/root 必需参数
const HEADLESS_ONLY = HEADLESS || forceHeadless();
log('[E2E] 浏览器:' + CHROME + (HEADLESS_ONLY ? '(headless)' : '(有界面)') + ' 进课采样:' + (PLAY ? SAMPLE_SECONDS + ' 秒' : '否(只读)'));
const userDataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'yun-xue-tang-e2e-'));
const args = [
`--remote-debugging-port=${PORT}`,
`--user-data-dir=${userDataDir}`,
'--no-first-run', '--no-default-browser-check', '--disable-sync',
'--disable-background-timer-throttling', '--disable-renderer-backgrounding',
'--disable-backgrounding-occluded-windows',
'--autoplay-policy=no-user-gesture-required',
'--window-size=1360,940',
].concat(safetyArgs(), ['about:blank']);
if (HEADLESS_ONLY) args.unshift('--headless=new');
let chrome = null;
let cdp = null;
try {
chrome = spawn(CHROME, args, { stdio: 'ignore', windowsHide: true });
let version = null;
for (let i = 0; i < 100; i++) {
try { const v = await httpJson(`http://127.0.0.1:${PORT}/json/version`); if (v && v.webSocketDebuggerUrl) { version = v; break; } } catch (e) {}
await sleep(250);
}
if (!version) throw new Error('Chrome CDP 未就绪');
log('[E2E] ' + version.Browser);
const target = await httpJson(`http://127.0.0.1:${PORT}/json/new?${encodeURIComponent(ORIGIN + '/login.html')}`, 'PUT');
cdp = await connect(target.webSocketDebuggerUrl);
await cdp.send('Page.enable');
await cdp.send('Runtime.enable');
await cdp.send('Log.enable');
cdp.onEvent((m) => {
if (m.method === 'Runtime.consoleAPICalled') {
const text = (m.params.args || []).map((a) => (a.value !== undefined ? a.value : a.description || '')).join(' ');
consoleLogs.push({ type: m.params.type, text: String(text) });
} else if (m.method === 'Log.entryAdded') {
consoleLogs.push({ type: m.params.entry.level, text: m.params.entry.text });
}
});
const evalJS = async (expr) => {
const r = await cdp.send('Runtime.evaluate', { expression: expr, returnByValue: true, awaitPromise: true, userGesture: true });
if (r.exceptionDetails) {
const d = r.exceptionDetails;
throw new Error((d.exception && d.exception.description) || d.text);
}
return r.result.value;
};
const json = async (expr) => JSON.parse(await evalJS(expr));
const waitFor = async (exprFn, timeoutMs, label) => {
const started = Date.now();
while (Date.now() - started < timeoutMs) {
try { if (await evalJS(exprFn)) return true; } catch (e) {}
await sleep(500);
}
return false;
};
const shot = async (name) => {
try {
fs.mkdirSync(SHOT_DIR, { recursive: true });
const s = await cdp.send('Page.captureScreenshot', { format: 'png' });
const f = path.join(SHOT_DIR, name);
fs.writeFileSync(f, Buffer.from(s.data, 'base64'));
return path.relative(ROOT, f);
} catch (e) { return '截图失败: ' + e.message; }
};
const loaded = cdp.waitEvent('Page.loadEventFired', 45000).catch(() => null);
await cdp.send('Page.navigate', { url: ORIGIN + '/login.html' });
await loaded;
await sleep(2000);
// ---------- 1. 登录页 + 注入 ----------
await evalJS(SCRIPT_SRC);
await sleep(1000);
const loginInfo = await json(`JSON.stringify((() => {
const a = window.__yunXueTangAuto;
return {
url: location.href,
title: document.title,
isLoginPage: a.isLoginPage(),
panel: !!document.getElementById('yun-xue-tang-panel'),
fields: (() => { const f = a.loginFields(); return { user: !!f.user, pwd: !!f.pwd, ph: f.user ? f.user.placeholder : null }; })(),
};
})())`);
check('登录页加载 + 脚本注入', loginInfo.isLoginPage === true && loginInfo.panel === true, loginInfo.url);
check('识别到账号/密码输入框', loginInfo.fields.user && loginInfo.fields.pwd, loginInfo.fields);
// ---------- 2. 填账号密码(走真实的 setNativeValue + Vue 双向绑定) ----------
const filled = await json(`JSON.stringify((() => {
const a = window.__yunXueTangAuto;
const u = ${JSON.stringify(USR)};
const p = ${JSON.stringify(PWD)};
document.getElementById('yun-xue-tang-login-user').value = u;
document.getElementById('yun-xue-tang-login-pass').value = p;
const okForm = a.fillLoginForm(u, p);
const f = a.loginFields();
return {
okForm,
userValueLen: f.user ? f.user.value.length : -1,
pwdValueLen: f.pwd ? f.pwd.value.length : -1,
userMatched: f.user ? f.user.value === u : false,
pwdMatched: f.pwd ? f.pwd.value === p : false,
panelUser: document.getElementById('yun-xue-tang-login-user').value.length,
};
})())`);
check('账号密码写入真实输入框', filled.okForm === true && filled.userMatched === true && filled.pwdMatched === true,
{ 长度: [filled.userValueLen, filled.pwdValueLen], 一致: [filled.userMatched, filled.pwdMatched] });
const agree = await json(`JSON.stringify((() => {
const a = window.__yunXueTangAuto;
const before = [...document.querySelectorAll('label')].filter(x => /已阅读并同意|隐私政策/.test(x.innerText||''))
.map(x => { const c = x.querySelector('input[type=checkbox]'); return c ? c.checked : null; });
const ok = a.checkLoginAgreement ? a.checkLoginAgreement() : 'no-api';
const after = [...document.querySelectorAll('label')].filter(x => /已阅读并同意|隐私政策/.test(x.innerText||''))
.map(x => { const c = x.querySelector('input[type=checkbox]'); return c ? c.checked : null; });
return { before, after, ok };
})())`);
check('隐私政策勾选处理正常', agree.ok === true || agree.ok === 'no-api', agree);
// ---------- 3. 点「一键登录并刷课」,等真实登录成功 ----------
// 注意:这个按钮会写 LS_PENDING,登录后自动开启列表刷课(脚本设计行为)。
// 默认(不加 --play)测完登录马上刹住,避免动到真实学习进度。
await evalJS(`(document.getElementById('yun-xue-tang-oneclick').click(), true)`);
const loginOk = await waitFor(`(() => {
if (/\\/login/.test(location.pathname)) return false;
if (!document.body) return false;
if (document.querySelector('input[type=password]')) return false;
return true;
})()`, 45000);
await sleep(2000);
const afterLogin = await json(`JSON.stringify((() => {
const a = window.__yunXueTangAuto;
const status = (document.getElementById('yun-xue-tang-status') || {}).textContent || '';
const bodyText = document.body ? (document.body.innerText || '').replace(/\\s+/g, ' ').slice(0, 400) : '';
return {
url: location.href, title: document.title, status,
alive: typeof window.__yunXueTangAuto,
isLoginPage: a ? a.isLoginPage() : null,
captcha: /验证码|滑块|安全验证/.test(bodyText),
errText: (bodyText.match(/(用户名密码错误|账号或密码错误|密码错误|账号不存在|登录失败)[^ ]{0,40}/) || [''])[0],
panel: !!document.getElementById('yun-xue-tang-panel'),
};
})())`);
check('真实登录成功(离开登录页)', loginOk === true, afterLogin);
const scriptErrs = () => consoleLogs.filter((l) => l.type === 'error' && /云学堂助手/.test(l.text));
check('登录流程无脚本 error', scriptErrs().length === 0, scriptErrs().map((e) => e.text.slice(0, 140)));
log(' 截图:' + await shot('e2e-01-after-login.png'));
// 登录是整页跳转(新 document),真实 Tampermonkey 会按 @match 重新注入;
// 这里模拟同样的行为。
if ((await evalJS('typeof window.__yunXueTangAuto')) === 'undefined') {
log('[E2E] 登录后是新 document(' + afterLogin.url + '),重新注入脚本');
await evalJS(SCRIPT_SRC);
await sleep(1200);
}
check('登录后新 document 上脚本已注入', (await evalJS('typeof window.__yunXueTangAuto')) === 'object', await evalJS('document.title || location.pathname'));
if (!PLAY) {
// 立即刹住列表刷课:清 pending 标记 + 关闭 running,防止在真实账号上推进课程
const halted = await json(`JSON.stringify((() => {
const a = window.__yunXueTangAuto;
try { localStorage.removeItem('yun-xue-tang-auto-start-after-login'); } catch (e) {}
if (a) { a.S.batchRunning = false; a.S.batchFinished = false; a.S.activeCourse = null; a.S.coursePhase = 'idle'; a.save(); }
return { running: a ? a.S.batchRunning : null, current: a ? a.S.activeCourse : null };
})())`);
check('默认只读:登录后立即停用列表刷课', halted.running === false && halted.current === null, halted);
}
// ---------- 4. 列表页断言 ----------
const listNavStarted = Date.now();
await evalJS(`(location.href = 'https://kfmic.yunxuetang.cn/kng/#/list', true)`);
await cdp.waitEvent('Page.loadEventFired', 30000).catch(() => null);
await sleep(2500);
// 换页后可能又是新 document,重新注入
if ((await evalJS('typeof window.__yunXueTangAuto')) === 'undefined') {
log('[E2E] 列表页是新 document,重新注入脚本');
await evalJS(SCRIPT_SRC);
await sleep(1000);
}
if (!PLAY) {
// 新 document 里 S 从 sessionStorage 恢复了 batchRunning=true,再次刹住
await evalJS(`(() => {
try { localStorage.removeItem('yun-xue-tang-auto-start-after-login'); } catch (e) {}
const a = window.__yunXueTangAuto;
if (a) { a.S.batchRunning = false; a.S.activeCourse = null; a.S.coursePhase = 'idle'; a.save(); }
return true;
})()`);
await sleep(400);
}
const listReady = await waitFor(`!!window.__yunXueTangAuto && document.querySelectorAll('li.kng-list-new__item').length > 0`, 45000);
await sleep(1500);
const listInfo = await json(`JSON.stringify((() => {
const a = window.__yunXueTangAuto; const cards = a.scanCards();
const vm = (() => {
const ul = document.querySelector('ul.kng-list-new');
let v = ul && ul.__vue__;
while (v && !(v.kngListData && v.kngListData.length)) v = v.$parent;
return v || null;
})();
const data = (vm && vm.kngListData) || [];
return {
url: location.href,
route: a.route(),
cards: cards.length,
page: a.currentPage(),
maxPage: a.maxPageNumber(),
first3: cards.slice(0, 3).map((c) => ({ title: c.title.slice(0, 30), status: c.status })),
vmCount: data.length,
vmFirst: data.slice(0, 3).map((d) => ({ title: String(d.title || d.name || '').slice(0, 26), studyHours: d.studyHours, id: String(d.id || d.kngId || '').slice(0, 12) })),
pending: cards.filter((c) => c.title && !/已学完|已完成|已学习/.test(c.status)).length,
};
})())`);
check('列表页卡片渲染', listReady === true && listInfo.cards > 0, { 卡片数: listInfo.cards, 页码: listInfo.page + '/' + listInfo.maxPage });
check('scanCards 解析标题与状态', listInfo.first3.every((c) => c.title.length > 0), listInfo.first3);
check('列表 Vue 组件可读(时长来源)', listInfo.vmCount > 0, { 条数: listInfo.vmCount, 样例: listInfo.vmFirst });
const dur = listInfo.vmFirst.filter((d) => Number.isFinite(d.studyHours) && d.studyHours > 0);
check('studyHours 时长可用(短课程优先的数据源)', dur.length > 0, dur);
// ---------- 5. 可选:真实进课 + 采样播放 ----------
let beforeProgress = null;
let playInfo = null;
if (PLAY) {
// 选一门「最短」的未学完课程,尽量减小对真实进度的影响
const pick = await json(`JSON.stringify((() => {
const a = window.__yunXueTangAuto;
const cards = a.scanCards();
const ul = document.querySelector('ul.kng-list-new');
let v = ul && ul.__vue__;
while (v && !(v.kngListData && v.kngListData.length)) v = v.$parent;
const data = (v && v.kngListData) || [];
const byTitle = new Map();
for (const d of data) byTitle.set(String(d.title || d.name || '').trim(), d);
const cand = cards
.filter((c) => c.title && !/已学完|已完成|已学习/.test(c.status))
.map((c) => ({ c, d: byTitle.get(c.title) }))
.filter((x) => x.d && Number(x.d.studyHours) > 0)
.sort((x, y) => Number(x.d.studyHours) - Number(y.d.studyHours));
if (!cand.length) {
const any = cards.find((c) => c.title && !/已学完|已完成|已学习/.test(c.status));
return any ? { title: any.title, studyHours: null, idx: any.i, fallback: true } : null;
}
return { title: cand[0].c.title, studyHours: Number(cand[0].d.studyHours), idx: cand[0].c.i };
})())`);
check('找到可学的最短课程', !!pick, pick);
if (pick) {
log('[E2E] 选课:' + pick.title + '(' + (pick.studyHours ? Math.round(pick.studyHours / 60) + ' 分钟' : '时长未知') + ')');
// 用脚本自己的 startCourse 走真实进课流程(会写 S.activeCourse 并点卡片)
await evalJS(`(() => {
const a = window.__yunXueTangAuto;
const card = a.scanCards().find((c) => c.i === ${pick.idx});
if (!card) return 'no-card';
a.startCourse(card);
return 'started';
})()`);
const entered = await waitFor(`window.__yunXueTangAuto && window.__yunXueTangAuto.route() === 'course' && /\\/play/.test(location.hash)`, 40000);
await sleep(2500);
const before = await json(`JSON.stringify((() => ({
url: location.hash,
summary: window.__yunXueTangAuto.readSummary(),
countdown: window.__yunXueTangAuto.readCountdown(),
lessons: window.__yunXueTangAuto.lessonItems().length,
active: window.__yunXueTangAuto.lessonItems().length ? (document.querySelector('.yxtulcdsdk-catalog .liactive') || {}).innerText || '' : '',
}))())`);
beforeProgress = before.summary && before.summary.text;
check('进入真实课程播放页', entered === true, before.url);
check('读取到课程汇总/目录', !!before.summary || before.lessons > 0, { 汇总: before.summary && before.summary.text, 目录: before.lessons });
// 触发「开始学习」并观察播放器
const play = await waitFor(`(() => {
const v = document.querySelector('video, audio');
return !!v && v.duration > 0;
})()`, 25000);
const t0 = Date.now();
// 让主循环自己动起来(模拟用户手动打开课程时的自动连播开关)
await evalJS(`(() => { window.__yunXueTangAuto.tick(); return true; })()`);
await sleep(SAMPLE_SECONDS * 1000);
playInfo = await json(`JSON.stringify((() => {
const v = document.querySelector('video, audio');
const a = window.__yunXueTangAuto;
return {
found: !!v,
muted: v ? v.muted : null,
paused: v ? v.paused : null,
rate: v ? v.playbackRate : null,
currentTime: v ? Math.round(v.currentTime) : null,
duration: v ? Math.round(v.duration) : null,
summary: a.readSummary(),
countdown: a.readCountdown(),
status: (document.getElementById('yun-xue-tang-status') || {}).textContent || '',
elapsedSec: Math.round((Date.now() - ${t0}) / 1000),
};
})())`);
check('播放器被找到并接管(静音 1x)', playInfo.found === true && playInfo.muted === true && playInfo.rate === 1, playInfo);
check('视频真实在播放(时间在走)', playInfo.paused === false && playInfo.currentTime > 0, { 已播到: playInfo.currentTime + 's / ' + playInfo.duration + 's' });
check('采样期间无脚本 error', scriptErrs().length === 0, scriptErrs().map((e) => e.text.slice(0, 140)));
log(' 截图:' + await shot('e2e-02-playing.png'));
// 立刻停手:暂停媒体 + 关闭批量 + dispose
await evalJS(`(() => {
const a = window.__yunXueTangAuto;
a.S.batchRunning = false;
a.save();
a.pauseAllMedia ? a.pauseAllMedia() : document.querySelectorAll('video, audio').forEach((x) => x.pause());
return true;
})()`);
await sleep(1200);
const stopped = await json(`JSON.stringify((() => {
const v = document.querySelector('video, audio');
return { paused: v ? v.paused : null, running: window.__yunXueTangAuto ? window.__yunXueTangAuto.S.batchRunning : null,
summary: window.__yunXueTangAuto ? window.__yunXueTangAuto.readSummary() : null };
})())`);
check('停止后媒体已暂停', stopped.paused === true && stopped.running === false, stopped);
}
}
// ---------- 6. 释放脚本,确认无残留 ----------
if (PLAY) {
const disposed = await json(`JSON.stringify((() => {
window.__yunXueTangDispose();
return { panel: !!document.getElementById('yun-xue-tang-panel'), api: typeof window.__yunXueTangAuto };
})())`);
check('dispose 后面板与 API 清理干净', disposed.panel === false && disposed.api === 'undefined', disposed);
}
log('\n[E2E] 脚本日志(' + consoleLogs.filter((l) => /云学堂助手/.test(l.text)).length + ' 条脚本相关):');
for (const l of consoleLogs.filter((x) => /云学堂助手/.test(x.text)).slice(-25)) log(' [' + l.type + '] ' + l.text.slice(0, 170));
if (beforeProgress) log('[E2E] 进度快照:进入前「' + beforeProgress + '」→ 采样后「' + (playInfo && playInfo.summary && playInfo.summary.text) + '」');
} catch (e) {
check('E2E 流程执行完毕', false, e && e.stack ? e.stack.split('\n').slice(0, 5).join(' | ') : String(e));
} finally {
if (cdp && !KEEP) cdp.close();
if (chrome && !KEEP) { try { chrome.kill(); } catch (e) {} }
if (!KEEP) { await sleep(700); try { fs.rmSync(userDataDir, { recursive: true, force: true }); } catch (e) {} }
else log('[E2E] --keep:浏览器保持打开 ' + userDataDir);
}
const failed = results.filter((r) => !r.ok);
log('\n===== E2E 结果:' + (results.length - failed.length) + '/' + results.length + ' 通过,失败 ' + failed.length + ' =====');
for (const f of failed) log(' FAIL ' + f.name + ' → ' + JSON.stringify(f.detail));
process.exit(failed.length ? 1 : 0);