110 lines
5.3 KiB
JavaScript
110 lines
5.3 KiB
JavaScript
const test = require('node:test');
|
||
const assert = require('node:assert/strict');
|
||
const {EventEmitter} = require('node:events');
|
||
const {createAdapter} = require('../lib/autojs-adapter.js');
|
||
|
||
function runtime() {
|
||
const engines = new EventEmitter();
|
||
const data = {};
|
||
let current = null;
|
||
let nextId = 0;
|
||
const stops = [];
|
||
const launches = [];
|
||
const files = {exists: p => !p.includes('missing'), isDir: () => false, read: p => data[p] || ''};
|
||
engines.execScriptFile = (path, config) => {
|
||
launches.push({path, config});
|
||
const id = ++nextId;
|
||
const engine = {destroyed: false, getId: () => id, isDestroyed() {return this.destroyed;},
|
||
forceStop: () => stops.push(id), getUncaughtException: () => null};
|
||
const execution = {engine, getId: () => id, getEngine() {return this.engine;}};
|
||
current = {engine, execution};
|
||
return execution;
|
||
};
|
||
return {engines, files, parentDirectory: p => p.slice(0, p.lastIndexOf('/')) || '/',
|
||
data, stops, launches, get current() {return current;}};
|
||
}
|
||
const task = (extra = {}) => ({path: '/sdcard/a.js', resultLog: '', ...extra});
|
||
|
||
test('收到finish但引擎仍未销毁时,必须继续等待;销毁后才报告结束', () => {
|
||
const r = runtime(); const a = createAdapter(r); const h = a.launch(task(), 0);
|
||
r.engines.emit('finish', r.current.engine);
|
||
assert.equal(a.poll(h, 1).done, false);
|
||
r.current.engine.destroyed = true;
|
||
assert.deepEqual(a.poll(h, 2), {done: true, ok: true});
|
||
assert.deepEqual(r.launches[0].config, {path: '/sdcard', delay: 0, loopTimes: 1});
|
||
});
|
||
|
||
test('全局error随后也会发finish,错误仍优先,不能因finish覆盖失败', () => {
|
||
const r = runtime(); const a = createAdapter(r); const h = a.launch(task(), 0);
|
||
r.engines.emit('error', r.current.engine, [Error('真实异常')]);
|
||
r.engines.emit('finish', r.current.engine); r.current.engine.destroyed = true;
|
||
const result = a.poll(h, 1);
|
||
assert.equal(result.ok, false); assert.match(result.reason, /真实异常/);
|
||
});
|
||
|
||
test('极速脚本事件早于execScriptFile返回也不会漏记', () => {
|
||
const r = runtime(); const original = r.engines.execScriptFile;
|
||
r.engines.execScriptFile = (p, c) => {
|
||
const e = original(p, c); r.engines.emit('finish', e.engine); e.engine.destroyed = true; return e;
|
||
};
|
||
const a = createAdapter(r); const h = a.launch(task(), 0);
|
||
assert.equal(a.poll(h, 1).ok, true);
|
||
});
|
||
|
||
test('停止发生在启动尚未取得引擎时,拿到引擎后补发停止,不操作其他引擎', () => {
|
||
const r = runtime(); const a = createAdapter(r); const h = a.launch(task(), 0);
|
||
const engine = r.current.engine;
|
||
r.current.execution.engine = null;
|
||
a.stop(h); assert.deepEqual(r.stops, []); assert.equal(a.poll(h, 1).done, false);
|
||
r.current.execution.engine = engine;
|
||
assert.equal(a.poll(h, 2).done, false);
|
||
assert.deepEqual(r.stops, [engine.getId()]);
|
||
r.engines.emit('finish', engine); engine.destroyed = true;
|
||
assert.equal(a.poll(h, 3).done, true);
|
||
});
|
||
|
||
test('启动15秒仍无引擎返回明确异常,不冒充启动失败已完全停止', () => {
|
||
const r = runtime(); const a = createAdapter(r); const h = a.launch(task(), 0);
|
||
r.current.execution.engine = null;
|
||
assert.equal(a.poll(h, 14999).done, false);
|
||
assert.throws(() => a.poll(h, 15000), /无法确认启动状态/);
|
||
});
|
||
|
||
test('外部停止且没有正常结束事件,以及子线程异常,均不算成功', () => {
|
||
const r = runtime(); const a = createAdapter(r); const h = a.launch(task(), 0);
|
||
r.current.engine.destroyed = true;
|
||
assert.equal(a.poll(h, 1).done, false);
|
||
assert.equal(a.poll(h, 1001).ok, false);
|
||
r.current.engine.getUncaughtException = () => Error('子线程异常');
|
||
r.engines.emit('finish', r.current.engine);
|
||
assert.match(a.poll(h, 1002).reason, /子线程异常/);
|
||
});
|
||
|
||
test('管理器退出只停止自己启动的未释放句柄,并移除自己的事件监听', () => {
|
||
const r = runtime(); const foreign = () => {};
|
||
r.engines.on('finish', foreign);
|
||
const a = createAdapter(r); const first = a.launch(task(), 0);
|
||
const firstId = r.current.engine.getId(); a.release(first);
|
||
a.launch(task(), 1); const secondId = r.current.engine.getId();
|
||
a.close();
|
||
assert.deepEqual(r.stops, [secondId]); assert.ok(!r.stops.includes(firstId));
|
||
assert.deepEqual(r.engines.listeners('finish'), [foreign]); assert.equal(r.engines.listenerCount('error'), 0);
|
||
});
|
||
|
||
test('默认番茄/红果的业务失败、陈旧日志和正常跳过均按本轮结果处理', () => {
|
||
const r = runtime(); const a = createAdapter(r);
|
||
const path = '/sdcard/Download/hongguo-daily-run.log';
|
||
r.data[path] = 'END: old';
|
||
const h = a.launch(task({resultLog: path}), 0);
|
||
r.engines.emit('finish', r.current.engine); r.current.engine.destroyed = true;
|
||
assert.equal(a.poll(h, 1).ok, false, '旧日志不能作为本轮成功证据');
|
||
r.data[path] = 'END: 本地OCR未就绪,保留页面。';
|
||
assert.equal(a.poll(h, 2).ok, false);
|
||
r.data[path] = 'END: 视频标签或对应领取按钮无匹配,已跳过;本次流程结束。';
|
||
assert.equal(a.poll(h, 3).ok, true);
|
||
});
|
||
|
||
test('没有引擎事件能力的旧环境直接提示版本限制,不静默降级', () => {
|
||
assert.throws(() => createAdapter({engines: {}, files: {}}), /6\.6\.3/);
|
||
});
|