270 lines
13 KiB
JavaScript
270 lines
13 KiB
JavaScript
const test = require('node:test');
|
||
const assert = require('node:assert/strict');
|
||
const {Scheduler, normalizePlan, knownResultLog, resultFromLog} = require('../lib/scheduler.js');
|
||
|
||
const task = (name, extra = {}) => ({id: name, name, path: '/sdcard/' + name + '.js', enabled: true,
|
||
times: 1, timeoutMinutes: 1, retries: 0, onFailure: 'skip', ...extra});
|
||
const plan = (tasks, extra = {}) => ({tasks, loops: 1, gapSeconds: 0, ...extra});
|
||
function harness(options) {
|
||
const launches = [], stops = [], released = [], events = [];
|
||
const adapter = {
|
||
launch(task, time) {
|
||
const h = {task, time, done: false, ok: true}; launches.push(h); return h;
|
||
},
|
||
poll: h => ({done: h.done, ok: h.ok, reason: h.reason}),
|
||
stop: h => stops.push(h), release: h => released.push(h)
|
||
};
|
||
return {adapter, launches, stops, released, events,
|
||
s: new Scheduler(adapter, text => events.push(text), options)};
|
||
}
|
||
|
||
test('两轮 A各两次→B一次,逐个等待真正完成,保持稳定顺序', () => {
|
||
const {s, launches} = harness();
|
||
s.start(plan([task('A', {times: 2}), task('B')], {loops: 2}), 0);
|
||
const expected = ['A', 'A', 'B', 'A', 'A', 'B'];
|
||
expected.forEach((name, i) => {
|
||
s.tick(i * 10);
|
||
assert.equal(launches.length, i + 1);
|
||
assert.equal(launches[i].task.name, name);
|
||
s.tick(i * 10 + 1);
|
||
assert.equal(launches.length, i + 1, '旧任务运行时不启动下一项');
|
||
launches[i].done = true;
|
||
s.tick(i * 10 + 2);
|
||
});
|
||
assert.equal(s.phase, 'completed');
|
||
assert.equal(s.successes, 6);
|
||
assert.equal(s.round, 2);
|
||
});
|
||
|
||
test('整组无限循环保持次序,手动停止会等待当前引擎退出且不再启动', () => {
|
||
const {s, launches, stops} = harness();
|
||
s.start(plan([task('A'), task('B')], {loops: 0}), 0);
|
||
for (let i = 0; i < 8; i++) {
|
||
s.tick(i * 10); launches[i].done = true; s.tick(i * 10 + 1);
|
||
}
|
||
assert.equal(s.round, 5);
|
||
s.tick(100);
|
||
s.requestStop(); s.tick(101);
|
||
assert.equal(s.phase, 'stopping');
|
||
assert.equal(stops.length, 1);
|
||
s.tick(102);
|
||
assert.equal(s.phase, 'stopping');
|
||
launches[8].done = true; s.tick(103); s.tick(200);
|
||
assert.equal(s.phase, 'stopped');
|
||
assert.equal(launches.length, 9);
|
||
assert.deepEqual(launches.map(h => h.task.name), ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A']);
|
||
});
|
||
|
||
test('启动前和间隔期间停止均不多运行一个任务', () => {
|
||
for (const beforeLaunch of [true, false]) {
|
||
const {s, launches} = harness();
|
||
s.start(plan([task('A'), task('B')], {gapSeconds: 10}), 0);
|
||
if (!beforeLaunch) {s.tick(0); launches[0].done = true; s.tick(1);}
|
||
s.requestStop(); s.tick(2); s.tick(100000);
|
||
assert.equal(s.phase, 'stopped');
|
||
assert.equal(launches.length, beforeLaunch ? 0 : 1);
|
||
}
|
||
});
|
||
|
||
test('启动前暂停阻止首个任务启动,继续后恢复原来的等待时间', () => {
|
||
const {s, launches} = harness();
|
||
s.start(plan([task('A')]), 500);
|
||
s.requestPause(100); s.tick(100000);
|
||
assert.equal(s.phase, 'paused'); assert.equal(s.isBusy(), true);
|
||
assert.equal(launches.length, 0);
|
||
assert.throws(() => s.start(plan([task('B')]), 100001), /尚未完全停止/);
|
||
s.requestResume(100000); s.tick(100399);
|
||
assert.equal(launches.length, 0);
|
||
s.tick(100400); assert.equal(launches.length, 1);
|
||
});
|
||
|
||
test('运行中暂停会结束当前脚本,确认退出后暂停下一项,继续不重跑当前任务', () => {
|
||
const {s, launches, stops} = harness();
|
||
s.start(plan([task('A'), task('B')]), 0); s.tick(0);
|
||
s.requestPause(100); s.tick(101);
|
||
assert.equal(s.phase, 'pausing'); assert.equal(stops.length, 1);
|
||
launches[0].done = true; s.tick(200); s.tick(100000);
|
||
assert.equal(s.phase, 'paused'); assert.equal(s.successes, 0); assert.equal(s.interrupted, 1);
|
||
assert.equal(launches.length, 1);
|
||
s.requestResume(100000); s.tick(100001);
|
||
assert.deepEqual(launches.map(h => h.task.name), ['A', 'B']);
|
||
});
|
||
|
||
test('任务间隔内暂停保留剩余间隔,重复暂停和继续不会重复启动', () => {
|
||
const {s, launches} = harness();
|
||
s.start(plan([task('A'), task('B')], {gapSeconds: 10}), 0); s.tick(0);
|
||
launches[0].done = true; s.tick(100);
|
||
s.requestPause(3100); s.requestPause(5100);
|
||
s.requestResume(100000); s.requestResume(101000);
|
||
s.tick(106999); assert.equal(launches.length, 1);
|
||
s.tick(107000); assert.equal(launches.length, 2);
|
||
});
|
||
|
||
test('当前脚本退出确认前点击继续,仍等待旧引擎销毁才运行下一项', () => {
|
||
const {s, launches} = harness();
|
||
s.start(plan([task('A'), task('B')]), 0); s.tick(0);
|
||
s.requestPause(100); s.requestResume(200); s.tick(300);
|
||
assert.equal(s.phase, 'pausing'); assert.equal(launches.length, 1);
|
||
launches[0].done = true; s.tick(400); s.tick(401);
|
||
assert.equal(launches.length, 2);
|
||
});
|
||
|
||
test('暂停中仍能停止整组,当前任务未退出时不放行后续任务', () => {
|
||
for (const active of [false, true]) {
|
||
const {s, launches, stops} = harness();
|
||
s.start(plan([task('A'), task('B')]), 0);
|
||
if (active) s.tick(0);
|
||
s.requestPause(100); s.requestStop(); s.tick(200);
|
||
assert.equal(s.canTogglePause(), false);
|
||
if (active) {
|
||
assert.equal(stops.length, 1); assert.equal(s.phase, 'stopping');
|
||
launches[0].done = true; s.tick(300);
|
||
}
|
||
assert.equal(s.phase, 'stopped');
|
||
s.tick(100000); assert.equal(launches.length, active ? 1 : 0);
|
||
}
|
||
});
|
||
|
||
test('暂停结束的执行不触发失败重试,继续运行同任务的下一次未执行次数', () => {
|
||
const {s, launches, stops} = harness();
|
||
s.start(plan([task('A', {times: 2, retries: 1})]), 0); s.tick(0);
|
||
s.requestPause(100); s.tick(200);
|
||
assert.equal(stops.length, 1); assert.equal(s.phase, 'pausing');
|
||
launches[0].done = true; launches[0].ok = false; s.tick(1000); s.tick(100000);
|
||
assert.equal(s.phase, 'paused'); assert.equal(s.attempt, 0); assert.equal(s.repetition, 2);
|
||
assert.equal(s.failures, 0);
|
||
assert.equal(launches.length, 1);
|
||
s.requestResume(100000); s.tick(100001);
|
||
assert.equal(launches.length, 2);
|
||
});
|
||
|
||
test('暂停时旧脚本无法退出会阻止继续,晚到退出也不自动运行后续任务', () => {
|
||
const {s, launches} = harness({stopGraceMs: 1000});
|
||
s.start(plan([task('A'), task('B')]), 0); s.tick(0); s.requestPause(100);
|
||
s.tick(1100); assert.equal(s.phase, 'blocked');
|
||
s.requestResume(1200); s.tick(1300); assert.equal(launches.length, 1);
|
||
launches[0].done = true; s.tick(1400); s.tick(1500);
|
||
assert.equal(s.phase, 'stopped'); assert.equal(launches.length, 1);
|
||
});
|
||
|
||
test('最后一个任务完成时正常结束整组,不停在没有后续任务的暂停状态', () => {
|
||
const {s, launches} = harness();
|
||
s.start(plan([task('A')]), 0); s.tick(0); s.requestPause(100);
|
||
launches[0].done = true; s.tick(200);
|
||
assert.equal(s.phase, 'completed'); assert.equal(s.pauseRequested, false);
|
||
assert.equal(s.canTogglePause(), false);
|
||
});
|
||
|
||
test('失败重试后恢复,不占用配置的正常执行次数,执行间隔也作用于重试', () => {
|
||
const {s, launches} = harness();
|
||
s.start(plan([task('A', {times: 2, retries: 1})], {gapSeconds: 2}), 0);
|
||
s.tick(0); launches[0].done = true; launches[0].ok = false; s.tick(100);
|
||
assert.equal(s.attempt, 1); assert.equal(s.repetition, 1);
|
||
s.tick(2099); assert.equal(launches.length, 1);
|
||
s.tick(2100); launches[1].done = true; s.tick(2101);
|
||
assert.equal(s.repetition, 2); assert.equal(s.attempt, 0);
|
||
s.tick(4101); launches[2].done = true; s.tick(4102);
|
||
assert.equal(s.phase, 'completed'); assert.equal(s.successes, 2); assert.equal(s.failures, 1);
|
||
});
|
||
|
||
test('重试耗尽跳过该任务剩余次数,继续下一个任务', () => {
|
||
const {s, launches} = harness();
|
||
s.start(plan([task('A', {times: 100, retries: 1}), task('B')]), 0);
|
||
for (let i = 0; i < 2; i++) {
|
||
s.tick(i * 10); launches[i].done = true; launches[i].ok = false; s.tick(i * 10 + 1);
|
||
}
|
||
s.tick(100);
|
||
assert.deepEqual(launches.map(h => h.task.name), ['A', 'A', 'B']);
|
||
assert.equal(s.skipped, 1); assert.equal(s.failures, 2);
|
||
});
|
||
|
||
test('失败停止策略不继续其他任务', () => {
|
||
const {s, launches} = harness();
|
||
s.start(plan([task('A', {onFailure: 'stop'}), task('B')]), 0);
|
||
s.tick(0); launches[0].done = true; launches[0].ok = false; s.tick(1); s.tick(2);
|
||
assert.equal(s.phase, 'failed'); assert.equal(launches.length, 1);
|
||
});
|
||
|
||
test('超时先强停并等待销毁,不能直接重试形成两个并发引擎', () => {
|
||
const {s, launches, stops} = harness();
|
||
s.start(plan([task('A', {retries: 1}), task('B')]), 0);
|
||
s.tick(0); s.tick(60000);
|
||
assert.equal(s.phase, 'stopping'); assert.equal(stops.length, 1);
|
||
s.tick(60001); assert.equal(launches.length, 1);
|
||
launches[0].done = true; s.tick(61000);
|
||
assert.equal(s.phase, 'waiting'); assert.equal(s.attempt, 1);
|
||
s.tick(61001); assert.equal(launches.length, 2);
|
||
});
|
||
|
||
test('旧脚本拒绝退出时停止整组放行;晚到退出也不自动启动新任务', () => {
|
||
const {s, launches, stops} = harness({stopGraceMs: 1000});
|
||
s.start(plan([task('A', {retries: 1}), task('B')]), 0);
|
||
s.tick(0); s.tick(60000); s.tick(61000);
|
||
assert.equal(s.phase, 'blocked'); assert.equal(s.isBusy(), true);
|
||
assert.throws(() => s.start(plan([task('C')]), 61001), /尚未完全停止/);
|
||
s.requestStop(); assert.equal(stops.length, 2);
|
||
s.tick(62000); assert.equal(launches.length, 1);
|
||
launches[0].done = true; s.tick(62001); s.tick(62002);
|
||
assert.equal(s.phase, 'stopped'); assert.equal(launches.length, 1);
|
||
});
|
||
|
||
test('状态查询异常先停止旧引擎,确认退出后才遵循失败策略', () => {
|
||
const {s, adapter, launches, stops} = harness();
|
||
s.start(plan([task('A'), task('B')]), 0); s.tick(0);
|
||
adapter.poll = () => {throw Error('桥接错误');};
|
||
s.tick(1);
|
||
assert.equal(s.phase, 'stopping'); assert.equal(stops.length, 1);
|
||
adapter.poll = () => ({done: true, ok: true});
|
||
s.tick(2); s.tick(3);
|
||
assert.equal(s.failures, 1); assert.equal(launches[1].task.name, 'B');
|
||
});
|
||
|
||
test('文件启动异常可重试或跳过,无已启动句柄不会等待假引擎', () => {
|
||
const {s, adapter, launches} = harness();
|
||
const original = adapter.launch;
|
||
adapter.launch = (t, now) => {if (t.name === 'A') throw Error('文件不存在'); return original(t, now);};
|
||
s.start(plan([task('A', {retries: 1}), task('B')]), 0);
|
||
s.tick(0); s.tick(1); s.tick(2);
|
||
assert.equal(s.failures, 2); assert.equal(s.skipped, 1);
|
||
assert.deepEqual(launches.map(h => h.task.name), ['B']);
|
||
});
|
||
|
||
test('运行中编辑原始配置不会改变已启动的快照;禁用任务不运行', () => {
|
||
const {s, launches} = harness();
|
||
const original = plan([task('disabled', {enabled: false}), task('A'), task('B')]);
|
||
s.start(original, 0); original.tasks[2].path = '/sdcard/changed.js';
|
||
s.tick(0); launches[0].done = true; s.tick(1); s.tick(2);
|
||
assert.deepEqual(launches.map(h => h.task.path), ['/sdcard/A.js', '/sdcard/B.js']);
|
||
});
|
||
|
||
test('日志写入或界面观察者异常不能将已启动脚本再启动一次', () => {
|
||
const {adapter, launches} = harness();
|
||
const s = new Scheduler(adapter, () => {throw Error('磁盘写入失败');});
|
||
s.start(plan([task('A')]), 0); s.tick(0); s.tick(1);
|
||
assert.equal(launches.length, 1); assert.equal(s.phase, 'running');
|
||
assert.match(s.lastObserverError, /磁盘/);
|
||
});
|
||
|
||
test('配置拒绝非法次数、超时和空启用列表', () => {
|
||
for (const [field, value] of [['loops', '-1'], ['loops', '1.5'], ['loops', Infinity], ['gapSeconds', '']]) {
|
||
assert.throws(() => normalizePlan(plan([task('A')], {[field]: value})));
|
||
}
|
||
for (const [field, value] of [['times', 0], ['timeoutMinutes', 0], ['retries', 11], ['path', 'relative.js']]) {
|
||
assert.throws(() => normalizePlan(plan([task('A', {[field]: value})])));
|
||
}
|
||
assert.throws(() => harness().s.start(plan([task('A', {enabled: false})]), 0), /至少启用/);
|
||
});
|
||
|
||
test('已有番茄红果只识别本轮末尾结果,正常退出但业务失败不会算成功', () => {
|
||
assert.equal(knownResultLog('/sdcard/脚本/红果/hongguo-daily.auto.js'), '/sdcard/Download/hongguo-daily-run.log');
|
||
assert.equal(knownResultLog('/sdcard/custom.js'), '');
|
||
for (const ending of ['END: 本地OCR未就绪,保留页面。', 'END: 视频流程遇到异常,保留页面与文字日志。',
|
||
'END: 出版物阅读遇到异常,保留现场和状态。', 'ERROR: TypeError: xx']) {
|
||
assert.equal(resultFromLog('old', 'new\n' + ending).ok, false);
|
||
}
|
||
assert.equal(resultFromLog('same', 'same').ok, false);
|
||
assert.equal(resultFromLog('', 'START: running').ok, false);
|
||
assert.equal(resultFromLog('old', 'ERROR: old error\nEND: 视频标签或对应领取按钮无匹配,已跳过;本次流程结束。').ok, true);
|
||
assert.equal(resultFromLog('old', 'END: 红果每日金币已配置流程执行结束。').ok, true);
|
||
});
|