const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const source = fs.readFileSync(path.join(__dirname, '../hongguo-daily.auto.js'), 'utf8'); function load() { const context = vm.createContext({console, Date, Math, JSON, Error, isFinite}); vm.runInContext(source.slice(0, source.lastIndexOf('\ntry {\n var endMessage')), context); context.device = {width: 1080, height: 2374}; context.sleep = () => {}; context.files = {write() {}}; context.logs = []; context.writeRunLog = text => context.logs.push(text); return context; } const item = (text, top, bottom = top + 40, left = 826, right = 974) => ({text, rect: {left, right, top, bottom}}); const taskPage = top => [item('日常福利', 350, 390, 60, 260), item('看视频赚海量金币210', top - 30, top + 10, 187, 702), item('立即颌取', top)]; test('无障碍选择器使用AutoJs6支持的字符串参数,并保留原生文字节点', () => { const c = load(); c.foregroundPackage = () => c.CONFIG.packageName; const node = {bounds: () => ({left: 696, top: 2217, right: 798, bottom: 2285}), text: () => '福利', visibleToUser: () => true}; c.textMatches = pattern => { assert.equal(typeof pattern, 'string'); return {find: () => ({size: () => 1, get: () => node})}; }; c.descMatches = pattern => { assert.equal(typeof pattern, 'string'); return {find: () => ({size: () => 0})}; }; const page = c.readSnapshot(); assert.equal(page[0].text, '福利'); assert.equal(page[0].node, node); }); test('真实OPPO领取按钮位于宝箱区域时先慢滑避让,仅返回新帧按钮', () => { const c = load(); let page = JSON.parse(fs.readFileSync(path.join(__dirname, '../debug/2026-10-21/oppo-hongguo8-stalled.json'), 'utf8')); const oldButton = c.findMassVideoClaim(page); assert.equal(oldButton.rect.top, 1880); c.snapshot = () => page; let swipes = 0; c.swipe = (x1, y1, x2, y2, duration) => { assert.equal(x1, x2); assert.ok(y1 > y2); assert.ok(y1 - y2 < 300); assert.equal(duration, 800); swipes++; page = taskPage(1880 - swipes * 285); return true; }; const button = c.prepareMassVideoClaim(false); assert.equal(swipes, 2); assert.notEqual(button, oldButton); assert.ok(button.rect.bottom < 2374 * 0.68); }); test('安全区域按钮直接返回,无进展重试时至少移动一次并重新识别', () => { const c = load(); let page = taskPage(1200); c.snapshot = () => page; let swipes = 0; c.swipe = () => {swipes++; page = taskPage(950); return true;}; assert.equal(c.prepareMassVideoClaim(false), page[2]); assert.equal(swipes, 0); assert.equal(c.prepareMassVideoClaim(true), page[2]); assert.equal(swipes, 1); }); test('滑动失败或始终无法离开遮挡区时不点击旧位置', () => { for (const moved of [false, true]) { const c = load(); c.snapshot = () => taskPage(1880); c.swipe = () => moved; assert.throws(() => c.prepareMassVideoClaim(false), /调整位置失败|仍位于遮挡区/); } }); function massFlow() { const c = load(); const button = taskPage(1200)[2]; c.snapshot = () => taskPage(1200); c.ensureTaskPage = () => {}; c.searchMassVideoTask = () => ({labelFound: true, button}); c.moves = []; c.prepareMassVideoClaim = force => {c.moves.push(force); return button;}; c.taps = 0; c.tap = () => {c.taps++; c.actionSerial++;}; c.handleVideos = (_pattern, grace) => assert.equal(grace, 10000); c.runStep = (_name, action) => { try {return {ok: true, value: action()};} catch (error) {return {ok: !!error.noMatch, skipped: true, targetChanged: !!error.targetChanged};} }; return c; } test('press成功但广告未启动不能算完成,三次无进展后跳过而非无限空点', () => { const c = massFlow(); const result = c.massVideoStep(); assert.equal(c.taps, 3); assert.equal(result.unavailable, true); assert.equal(result.rounds, 0); assert.deepEqual(c.moves, [false, true, true]); assert.ok(c.logs.some(text => text.includes('不计为完成轮次'))); assert.ok(!c.logs.some(text => text.includes('MASS VIDEO RECHECK'))); }); test('真实广告通用操作有进展时继续同一按钮,不用总轮次截断正常循环', () => { const c = massFlow(); c.handleVideos = () => {c.actionSerial++;}; let searches = 0; c.searchMassVideoTask = () => ++searches <= 5 ? {button: taskPage(1200)[2]} : {labelFound: false}; const result = c.massVideoStep(); assert.equal(result.rounds, 5); assert.equal(c.taps, 5); assert.equal(searches, 7); assert.equal(result.unavailable, undefined); }); test('广告恢复成功清除连续无进展次数,新一轮故障仍有完整重试机会', () => { const c = massFlow(); c.handleVideos = () => {if (c.taps === 3) c.actionSerial++;}; const result = c.massVideoStep(); assert.equal(result.rounds, 1); assert.equal(result.unavailable, true); assert.equal(c.taps, 6); assert.deepEqual(c.moves, [false, true, true, false, true, true]); }); test('调整位置后按钮消失先复查,不点旧按钮也不记录完成', () => { const c = massFlow(); let searches = 0; c.searchMassVideoTask = () => ++searches === 1 ? {button: taskPage(1200)[2]} : {labelFound: false}; c.prepareMassVideoClaim = () => null; const result = c.massVideoStep(); assert.equal(c.taps, 0); assert.equal(result.rounds, 0); assert.equal(searches, 3); }); test('真实广告处理异常保留错误,不冒充正常无匹配或完成', () => { const c = massFlow(); c.handleVideos = () => {throw Error('广告关闭未响应');}; const result = c.massVideoStep(); assert.equal(result.ok, false); assert.equal(result.rounds, 0); assert.equal(c.taps, 1); }); test('点击前OCR领取同义字及位置发生变化,重新绑定当前任务按钮,不点旧坐标', () => { const c = load(); const old = item('立即领取', 900); const fresh = taskPage(1000); c.snapshot = () => fresh; c.foregroundPackage = () => c.CONFIG.packageName; const presses = []; c.press = (x, y) => {presses.push([x, y]); return true;}; c.tap(old, c.findSafeMassVideoClaim); assert.deepEqual(presses, [[900, 1020]]); }); test('真实停止页面仍可绑定海量金币按钮,其他任务领取和遮挡位置不能替代', () => { const c = load(); const page = JSON.parse(fs.readFileSync(path.join(__dirname, '../debug/2026-10-21/oppo-hongguo9-target-changed.json'), 'utf8')); assert.equal(c.findSafeMassVideoClaim(page).rect.top, 901); for (const fresh of [page.filter(row => !c.MASS_VIDEO_LABEL.test(row.text)), taskPage(1880)]) { c.snapshot = () => fresh; c.press = () => {throw Error('不能点击其他任务或被遮挡的入口');}; assert.throws(() => c.tap(item('立即领取', 900), c.findSafeMassVideoClaim), error => error.targetChanged === true); } }); test('第8轮点击前目标变化时复查并恢复,保留前7轮完成结果', () => { const c = massFlow(); // 使用真实runStep错误分类,模拟AutoJs线程执行的结果。 c.boundedCall = (_name, _timeout, action) => action(); vm.runInContext(source.slice(source.indexOf('function runStep('), source.indexOf('function foregroundPackage(')), c); let attempts = 0; c.tap = () => { attempts++; if (attempts === 8) { const error = Error('通用处理后目标标签已改变,跳过旧坐标点击'); error.noMatch = true; error.targetChanged = true; throw error; } c.taps++; c.actionSerial++; }; c.handleVideos = () => {c.actionSerial++;}; c.searchMassVideoTask = () => c.taps < 9 ? {button: taskPage(1200)[2]} : {labelFound: false}; const result = c.massVideoStep(); assert.equal(result.ok, true); assert.equal(result.rounds, 9); assert.equal(c.taps, 9); assert.equal(attempts, 10); assert.ok(c.logs.some(text => text.includes('MASS VIDEO TARGET REFRESH'))); assert.ok(!c.logs.some(text => text.includes('MASS VIDEO ERROR'))); }); test('入口持续变化时有限复查,不无限重试也不误报广告异常', () => { for (const changedDuringTap of [false, true]) { const c = massFlow(); let attempts = 0; c.prepareMassVideoClaim = () => { attempts++; if (!changedDuringTap) return null; return taskPage(1200)[2]; }; c.tap = () => { const error = Error('目标变化'); error.noMatch = true; error.targetChanged = true; throw error; }; const result = c.massVideoStep(); assert.equal(attempts, 3); assert.equal(result.unavailable, true); assert.equal(result.rounds, 0); assert.ok(!c.logs.some(text => text.includes('MASS VIDEO ERROR'))); } });