Files
yun-xue-tang-assistant/dev/tests/study-test.mjs
T
voidandClaude Sonnet 4.6 6b508a0b1a feat: Bark 增加开始运行与获得学分提醒
收尾通知去掉重复的积分台账与完成门数;学习统计在无 TTY 时照常累计,保证 Docker 里也能推送学分提醒。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-11-02 22:30:11 +08:00

235 lines
10 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
/**
* 学习统计(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. 学分事件:每次真正计入学分时产出 { course, credit },供 Bark 通知使用
// ---------------------------------------------------------------------------
console.log('学分事件');
{
const s = createStudyTracker({ now: clock().now });
let st = s.update({ course: 'A', credit: 2.5, completed: 0, video: video(false) });
eq(st.earned.length, 0, '只有学分、没完成时没有事件');
st = s.update({ course: 'A', credit: null, completed: 1, video: video(false) });
eq(st.earned.length, 1, '课程完成时产出一条事件');
eq(st.earned[0].course, 'A', '事件带课程名');
eq(st.earned[0].credit, 2.5, '事件带本门学分数');
eq(st.creditEarned, 2.5, '返回值里的累计学分同步更新');
st = s.update({ course: 'A', credit: null, completed: 1, video: video(false) });
eq(st.earned.length, 0, '同一门重复上报不重复产生事件');
st = s.update({ course: 'C', credit: null, completed: 2, video: video(false) });
eq(st.earned.length, 0, '没读到学分的课完成时不产生事件');
}
// ---------------------------------------------------------------------------
// 6. 学分文本:浮点累加不出现 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', '两位小数');
}
// ---------------------------------------------------------------------------
// 7. 摘要 / 空状态
// ---------------------------------------------------------------------------
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 学分', '只有时长没有学分时也照常输出');
}
// ---------------------------------------------------------------------------
// 8. 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('学习统计自测:全部通过');