app_auto_all/task_manager/tests/scheduler.test.js
2026-10-21 15:14:07 +08:00

181 lines
8.8 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.

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', {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);
});