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

125 lines
5.3 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const path = require('node:path');
const source = fs.readFileSync(path.join(__dirname, '../qimao-daily.auto.js'), 'utf8');
function load() {
let now = 0;
const c = vm.createContext({Date: {now: () => now}, Math, JSON, Error, isFinite});
vm.runInContext(source.slice(0, source.lastIndexOf('\ntry { runDaily(); }')), c);
c.device = {width: 1080, height: 2374};
c.sleep = ms => {now += ms;};
c.writeRunLog = () => {};
return c;
}
function item(text, left, top, right, bottom) {return {text, rect: {left, top, right, bottom}};}
test('运行时保持亮屏并按分钟续期,不在每次页面识别重复申请', () => {
const c = load();
const holds = [];
c.device.keepScreenOn = ms => holds.push(ms);
c.keepScreenAwake();
c.keepScreenAwake();
assert.deepEqual(holds, [3600000]);
c.sleep(60001);
c.keepScreenAwake();
assert.deepEqual(holds, [3600000, 3600000]);
});
test('饭点领取后入口变成去补领,仍匹配补贴行,排除其他任务按钮', () => {
const c = load();
const claim = item('去补领', 792, 1080, 1011, 1167);
const page = [item('吃饭补贴130金币', 69, 1059, 783, 1125), claim,
item('去领取', 792, 700, 1011, 787)];
assert.equal(c.findMealEntry(page), claim);
claim.text = '明日再来';
assert.equal(c.findMealEntry(page), null);
});
test('搜索滚动过程中出现顶部吃饭补贴图标,也能重新进入', () => {
const c = load();
const tile = item('吃饭补贴', 450, 480, 610, 540);
c.searchTaskAction = finder => {assert.equal(finder([]), null); return finder([tile]);};
assert.equal(c.searchMealEntry(), tile);
});
test('去补领后的空白加载帧不会使补贴任务立即退出', () => {
const c = load();
const meal = [item('早饭',126,1671,207,1725),item('看视频补领一次补贴',144,1944,936,2028)];
let frames = 0;
c.snapshot = () => ++frames <= 3 ? [] : meal;
assert.equal(c.waitForMealPage(), meal);
assert.equal(frames, 4);
});
test('补贴页始终空白时有限等待并明确报错', () => {
const c = load();
let frames = 0;
c.snapshot = () => {frames++; return [];};
assert.throws(() => c.waitForMealPage(), /加载超时/);
assert.ok(frames > 1 && frames < 10);
});
test('系统浮层短暂遮挡后继续识别,不跳过当前步骤', () => {
const c = load();
let calls = 0;
c.foregroundPackage = () => ++calls <= 14 ? 'com.android.systemui' : c.CONFIG.packageName;
const empty = {find: () => ({size: () => 0})};
c.textMatches = c.descMatches = () => empty;
c.files = {write() {}};
assert.equal(c.readSnapshot().length, 0);
assert.ok(calls > 14);
});
test('持续系统遮挡和其他应用不会触发错误页面点击', () => {
for (const pkg of ['com.android.systemui', 'com.heytap.market']) {
const c = load();
c.foregroundPackage = () => pkg;
assert.throws(() => c.readSnapshot(), error => {
assert.equal(error.foregroundTransient, pkg === 'com.android.systemui');
return true;
});
}
});
test('逛街被系统遮挡后重新搜索入口,不沿用原按钮坐标', () => {
const c = load();
let searches = 0, actions = 0;
c.runStep = name => {
if (name.startsWith('查找')) {
searches++;
return {ok: true, value: {button: searches <= 2 ? {} : null, labelFound: true}};
}
actions++;
return actions === 1 ? {ok: false, skipped: true, retryable: true} : {ok: true, value: {responded: true}};
};
assert.equal(c.runShoppingTask('street').ok, true);
assert.equal(actions, 2);
assert.equal(searches, 4);
});
test('系统浮层始终存在时限制恢复次数,不形成无限重试', () => {
const c = load();
let attempts = 0;
c.runStep = () => {attempts++; return {ok: false, skipped: true, retryable: true};};
assert.equal(c.runShoppingTask('street').ok, false);
assert.equal(attempts, 3);
});
function daily(c, pending) {
const logs = [], modes = [];
c.files = {write() {}};
c.writeRunLog = message => logs.push(message);
c.engines = {myEngine: () => ({id: 1, getSource: () => 'qimao-daily.auto.js'}), all: () => []};
c.auto = {waitFor() {}};
c.foregroundPackage = () => c.CONFIG.packageName;
c.localOcrEnabled = true;
c.isWelfarePage = () => true;
c.rawPage = () => [];
c.runStep = name => ({ok: true, value: name === '结束前复查逛街剩余入口' ? {button: pending ? {} : null} : undefined});
c.runShoppingTask = mode => {modes.push(mode); return {ok: mode === 'mall' || modes.length > 2};};
c.runDaily();
return {logs, modes};
}
test('商城结束不能直接结束整组,恢复仍有入口的逛街任务', () => {
const result = daily(load(), true);
assert.deepEqual(result.modes, ['street', 'mall', 'street']);
assert.ok(result.logs.some(s => s.startsWith('FLOW RESUME:')));
});
test('结束复查没有逛街入口则退出,不反复重新进入已结束的商城', () => {
const result = daily(load(), false);
assert.deepEqual(result.modes, ['street', 'mall']);
assert.ok(result.logs.some(s => s.includes('逛街入口已复查')));
});