feat: v2.9.0 终端实时面板,学习时长与学分统计
新增 runner/tui.mjs(~470 行):前台运行时自动开一块实时面板, 框线布局显示状态 / 课程 / 学时 / 课时 / 视频进度条 / 进度 / 最近日志。 - 学习统计 runner/study.mjs:只累计视频真在播的时长(暂停、重试退避、 页面加载都不计),学分按课程去重(重试 / 刷新不重复计) - 页面侧新增 credit 快照:平台只把本门学分写在倒计时文案里, 学完文案就消失,所以边播边记 - log.mjs 增加 setConsoleSink:面板接管终端那一路,日志文件仍是纯文本 - 面板里按 q 等价 Ctrl+C,走 stop.request 同一条优雅收尾路径 - 尺寸自适应:宽度跟随终端(不封顶 120 列),无 resize 事件的平台 靠定时刷新发现尺寸变化;变窄时按旧帧在新宽度下的折行数回退光标, 折完超过窗口高就整屏重置;宽度 / 高度不足按重要性丢字段 - 非 TTY(重定向 / 管道 / 计划任务 / --check)自动降级为逐行日志, 不写 ANSI 控制字符;--no-tui / YUN_XUE_TANG_TUI=0 可手动关闭, --no-color / NO_COLOR 关颜色 - 监控轮询从 2000ms 调到 1000ms,与面板刷新对齐 - supervisor 收尾时先收面板再逐行打退出日志,并汇总本次学习时长 / 学分 - check.mjs 增加裸 CR 检查:脚本里的 config\runner 曾被写成裸 CR, 变成 config<CR>unner 导致找不到配置文件 自测:npm run test:tui(渲染 / 列宽 / 极端尺寸)、npm run test:study(假时钟口径)
This commit is contained in:
1 parent
c1e883d67f
commit
65bf63ea6a
17 files changed
+1608
-28
No files matched your search
@@ -0,0 +1,212 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* 学习统计(runner/study.mjs)自测:纯数据逻辑,假时钟,不需要浏览器也不碰终端。
|
||||
*
|
||||
* 为什么单独测:这段逻辑的口径一旦错,面板上的「已学 1:23:45 / 学分 12.75」就是假的,
|
||||
* 而这种错在日志里完全看不出来(日志只有收尾那一行)。用假时钟把
|
||||
* 「暂停不计时」「间隔过大不补计」「重复完成不重复计分」这些边界钉死。
|
||||
*
|
||||
* 用法:
|
||||
* node dev/tests/study-test.mjs
|
||||
* npm run test:study
|
||||
*/
|
||||
import { createStudyTracker } from '../../runner/study.mjs';
|
||||
|
||||
let failed = 0;
|
||||
function ok(cond, name, extra) {
|
||||
if (cond) console.log(' ✔ ' + name);
|
||||
else { failed++; console.log(' ✘ ' + name + (extra ? ' → ' + extra : '')); }
|
||||
}
|
||||
function eq(actual, expect, name) {
|
||||
ok(actual === expect, name, '期望 ' + JSON.stringify(expect) + ',实际 ' + JSON.stringify(actual));
|
||||
}
|
||||
|
||||
/** 假时钟:手动推进,不依赖真实时间 */
|
||||
function clock(start = 1700000000000) {
|
||||
let t = start;
|
||||
return { now: () => t, tick(sec) { t += sec * 1000; } };
|
||||
}
|
||||
|
||||
const video = (paused) => ({ t: 10, d: 100, paused, err: 0, src: 1 });
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 1. 学习时长:只在视频真的在播时累加
|
||||
// ---------------------------------------------------------------------------
|
||||
console.log('学习时长');
|
||||
{
|
||||
const c = clock();
|
||||
const s = createStudyTracker({ now: c.now });
|
||||
|
||||
s.update({ course: 'A', video: video(false) });
|
||||
eq(s.get().studySeconds, 0, '首帧没有上一帧可比,不计时');
|
||||
|
||||
c.tick(2);
|
||||
s.update({ course: 'A', video: video(false) });
|
||||
eq(s.get().studySeconds, 2, '连续两帧都在播,累加 2 秒');
|
||||
|
||||
c.tick(2);
|
||||
s.update({ course: 'A', video: video(true) });
|
||||
eq(s.get().studySeconds, 2, '这一帧已暂停:上一段播到暂停前就停了,不再补');
|
||||
|
||||
c.tick(30);
|
||||
s.update({ course: 'A', video: video(true) });
|
||||
eq(s.get().studySeconds, 2, '暂停 30 秒一点都不计(排队/挂机不算学习)');
|
||||
|
||||
c.tick(2);
|
||||
s.update({ course: 'A', video: video(false) });
|
||||
c.tick(3);
|
||||
s.update({ course: 'A', video: video(false) });
|
||||
eq(s.get().studySeconds, 5, '恢复播放后从 2 秒接着累加');
|
||||
|
||||
// 没有 video(列表页 / 加载中)等于没在播
|
||||
c.tick(10);
|
||||
s.update({ course: 'A', video: null });
|
||||
c.tick(2);
|
||||
s.update({ course: 'A', video: null });
|
||||
eq(s.get().studySeconds, 5, 'video 为 null 时不计时(列表页/加载中的时间不算学习)');
|
||||
c.tick(2);
|
||||
s.update({ course: 'A', video: video(false) });
|
||||
eq(s.get().studySeconds, 5, '从 null 回到播放:这一帧不算(上一帧没在播)');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 2. 学习时长:间隔异常(浏览器重启 / 进程挂起)不补计
|
||||
// ---------------------------------------------------------------------------
|
||||
console.log('异常间隔');
|
||||
{
|
||||
const c = clock();
|
||||
const s = createStudyTracker({ now: c.now });
|
||||
s.update({ course: 'A', video: video(false) });
|
||||
c.tick(600); // 10 分钟没有任何快照:中间肯定断过
|
||||
s.update({ course: 'A', video: video(false) });
|
||||
eq(s.get().studySeconds, 0, '间隔 600 秒视为断线,不补计(上限 60 秒)');
|
||||
|
||||
c.tick(59);
|
||||
s.update({ course: 'A', video: video(false) });
|
||||
eq(s.get().studySeconds, 59, '间隔 59 秒仍在容忍范围内');
|
||||
|
||||
c.tick(61);
|
||||
s.update({ course: 'A', video: video(false) });
|
||||
eq(s.get().studySeconds, 59, '间隔 61 秒丢弃');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 3. 学分:边播边记,倒计时消失后沿用缓存
|
||||
// ---------------------------------------------------------------------------
|
||||
console.log('学分读取');
|
||||
{
|
||||
const c = clock();
|
||||
const s = createStudyTracker({ now: c.now });
|
||||
|
||||
s.update({ course: 'A', credit: 2.5, video: video(false) });
|
||||
eq(s.get().credit, 2.5, '播放期间读到本门可得学分');
|
||||
|
||||
// 学完后倒计时元素消失(credit=null),本门学分仍要显示
|
||||
s.update({ course: 'A', credit: null, completed: 1, video: video(false) });
|
||||
eq(s.get().credit, 2.5, '倒计时消失后沿用缓存(否则面板会突然变 —)');
|
||||
eq(s.get().creditEarned, 2.5, '课程完成后计入获得学分');
|
||||
|
||||
// 换到新课,还没读到学分
|
||||
s.update({ course: 'B', credit: null, completed: 1, video: video(false) });
|
||||
eq(s.get().credit, null, '换课后本门学分先清空(还没读到新课的分)');
|
||||
|
||||
s.update({ course: 'B', credit: 1.25, completed: 1, video: video(false) });
|
||||
eq(s.get().credit, 1.25, '读到新课学分');
|
||||
s.update({ course: 'B', credit: null, completed: 2, video: video(false) });
|
||||
eq(s.get().creditEarned, 3.75, '第二门学完累加(2.5 + 1.25)');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 4. 学分:不重复计数
|
||||
// ---------------------------------------------------------------------------
|
||||
console.log('防重复计数');
|
||||
{
|
||||
const c = clock();
|
||||
const s = createStudyTracker({ now: c.now });
|
||||
|
||||
s.update({ course: 'A', credit: 2.5, completed: 0, video: video(false) });
|
||||
s.update({ course: 'A', credit: 2.5, completed: 1, video: video(false) });
|
||||
eq(s.get().creditEarned, 2.5, '第一次完成计 2.5');
|
||||
|
||||
// completed 没变(页面刷新 / 重试后重复上报)
|
||||
for (let i = 0; i < 5; i++) s.update({ course: 'A', credit: 2.5, completed: 1, video: video(false) });
|
||||
eq(s.get().creditEarned, 2.5, '完成数没变不重复加');
|
||||
|
||||
// completed 涨到 2 但课程名还是 A(重试同一门又完成了一遍)——按课程去重,仍不加
|
||||
s.update({ course: 'A', credit: 2.5, completed: 2, video: video(false) });
|
||||
eq(s.get().creditEarned, 2.5, '同一门课重复完成不重复计分');
|
||||
|
||||
// 没读到过学分的课,完成后也不瞎加
|
||||
s.update({ course: 'C', credit: null, completed: 3, video: video(false) });
|
||||
eq(s.get().creditEarned, 2.5, '没读到学分的课完成后不加分');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 5. 学分文本:浮点累加不出现 3.7499999
|
||||
// ---------------------------------------------------------------------------
|
||||
console.log('学分文本');
|
||||
{
|
||||
const s = createStudyTracker({ now: clock().now });
|
||||
eq(s.creditText(3.75), '3.75', '3.75 原样输出');
|
||||
eq(s.creditText(1), '1', '整数不带小数点');
|
||||
eq(s.creditText(0.1 + 0.2), '0.3', '浮点误差被抹平(0.1+0.2)');
|
||||
eq(s.creditText(6.5), '6.5', '一位小数不带尾随 0');
|
||||
eq(s.creditText(12.75), '12.75', '两位小数');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 6. 摘要 / 空状态
|
||||
// ---------------------------------------------------------------------------
|
||||
console.log('摘要与空状态');
|
||||
{
|
||||
const s = createStudyTracker({ now: clock().now });
|
||||
eq(s.summary(), '', '什么都没学时摘要为空(收尾不该打一行 0 学时的废话)');
|
||||
eq(s.get().studySeconds, 0, '初始时长为 0');
|
||||
eq(s.get().creditEarned, 0, '初始学分为 0');
|
||||
eq(s.get().credit, null, '初始本门学分为 null');
|
||||
|
||||
const c = clock();
|
||||
const s2 = createStudyTracker({ now: c.now });
|
||||
s2.update({ course: 'A', credit: 2.5, video: video(false) });
|
||||
c.tick(3600);
|
||||
s2.update({ course: 'A', credit: null, completed: 1, video: video(false) });
|
||||
// 间隔 3600 秒 > 60 被丢弃,所以时长仍是 0;这里主要是验证摘要格式
|
||||
eq(s2.summary(), '视频累计播放 00:00,获得 2.5 学分', '摘要把时长和学分写在一句里');
|
||||
|
||||
const c3 = clock();
|
||||
const s3 = createStudyTracker({ now: c3.now });
|
||||
s3.update({ course: 'A', video: video(false) });
|
||||
c3.tick(2);
|
||||
s3.update({ course: 'A', video: video(false) });
|
||||
eq(s3.summary(), '视频累计播放 00:02,获得 0 学分', '只有时长没有学分时也照常输出');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 7. reset:重开一轮(supervisor 重启的会话之间)要能清零
|
||||
// ---------------------------------------------------------------------------
|
||||
console.log('重置');
|
||||
{
|
||||
const c = clock();
|
||||
const s = createStudyTracker({ now: c.now });
|
||||
s.update({ course: 'A', credit: 2.5, video: video(false) });
|
||||
c.tick(2);
|
||||
s.update({ course: 'A', credit: null, completed: 1, video: video(false) });
|
||||
ok(s.get().studySeconds > 0 && s.get().creditEarned > 0, '重置前有数据');
|
||||
|
||||
s.reset();
|
||||
eq(s.get().studySeconds, 0, 'reset 清空时长');
|
||||
eq(s.get().creditEarned, 0, 'reset 清空学分');
|
||||
eq(s.get().credit, null, 'reset 清空本门学分');
|
||||
eq(s.summary(), '', 'reset 后摘要为空');
|
||||
|
||||
// 重置后再来一门同名的课,应该能重新计分(去重表也清了)
|
||||
s.update({ course: 'A', credit: 2.5, completed: 1, video: video(false) });
|
||||
eq(s.get().creditEarned, 2.5, 'reset 后同名课程能重新计分');
|
||||
}
|
||||
|
||||
console.log('');
|
||||
if (failed) {
|
||||
console.log('学习统计自测:' + failed + ' 项失败');
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('学习统计自测:全部通过');
|
||||
Reference in new issue
Block a user