app_auto_all/fanqie_auto/tests/invite-cash.test.js
2026-10-26 16:09:54 +08:00

67 lines
5.4 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'),assert=require('node:assert/strict');
const fs=require('node:fs'),vm=require('node:vm'),path=require('node:path');
const source=fs.readFileSync(path.join(__dirname,'../fanqie-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 {\n runDaily();')),c);
c.device={width:1080,height:2400};c.sleep=ms=>now+=ms;c.writeRunLog=()=>{};
const page=JSON.parse(fs.readFileSync(path.join(__dirname,'fixtures/vivo-invite-cash-popup.json'),'utf8'));return {c,page};}
test('vivo邀请现金红包识别メ为中央关闭,优先通用处理',()=>{const {c,page}=load();
assert.ok(c.inviteCashPopupHeading(page));assert.equal(c.findInviteCashPopupClose(page).text,'メ');assert.equal(c.needsCommonHandling(page),true);});
test('缺少邀请组合特征不关闭普通现金签到或背景叉号',()=>{const {c,page}=load();
for(const text of ['送你现金红包','邀请好友立得']){const p=page.filter(i=>i.text!==text);assert.equal(c.inviteCashPopupHeading(p),null);assert.equal(c.findInviteCashPopupClose(p),null);}});
test('处理邀请弹窗先关闭,不进入背景金币或导航流程',()=>{const {c,page}=load();let current=page,closed=0;
c.snapshot=()=>current;c.tap=(button)=>{assert.equal(button.text,'メ');closed++;current=[];};
c.completedTaskPopupHeading=()=>{throw Error('背景规则不得先执行');};
c.sleep=()=>{if(!current.length)throw Error('验证结束');};
assert.throws(()=>c.handleVideosLoop(/./),/背景规则不得先执行/);assert.equal(closed,1);});
test('中央关闭坐标约束,不用顶部背景X作为邀请关闭',()=>{const {c,page}=load();
const p=page.filter(i=>i.text!=='メ');p.push({text:'X',rect:{left:990,right:1030,top:100,bottom:140}});
assert.equal(c.findInviteCashPopupClose(p),null);});
test('vivo小说推荐官推广弹窗只关闭,不点查看权益',()=>{
const {c}=load();const page=JSON.parse(fs.readFileSync(path.join(__dirname,'fixtures/vivo-recommendation-popup.json'),'utf8'));
assert.equal(c.dismissOnlyPromoPopupHeading(page).text,'小说推荐官');assert.equal(c.needsCommonHandling(page),true);
assert.equal(c.findInviteCashPopupClose(page),null,'现场没有可识别关闭叉号,走返回复查');
for(const text of ['赠送好友VIP会员','查看权益'])assert.equal(c.dismissOnlyPromoPopupHeading(page.filter(x=>x.text!==text)),null);
});
test('无叉号的推荐官弹窗返回一次关闭,确认消失后交还后续处理',()=>{
const {c}=load();const page=JSON.parse(fs.readFileSync(path.join(__dirname,'fixtures/vivo-recommendation-popup.json'),'utf8'));
let current=page,backs=0;c.snapshot=()=>current;c.back=()=>{backs++;current=[];return true;};
c.tap=()=>{throw Error('不得点击权益或背景按钮');};
c.completedTaskPopupHeading=()=>{throw Error('后续处理已接管');};
assert.throws(()=>c.handleVideosLoop(/./),/后续处理已接管/);assert.equal(backs,1);
});
test('原生无文字推广关闭图标按弹窗父容器及位置识别,不点击权益',()=>{
const {c}=load();const page=JSON.parse(fs.readFileSync(path.join(__dirname,'fixtures/vivo-recommendation-popup.json'),'utf8'));
const close={bounds:()=>({left:485,top:1726,right:596,bottom:1837}),visibleToUser:()=>true,
parent:()=>({bounds:()=>({left:152,top:563,right:928,bottom:1837})})};
c.className=name=>{assert.equal(name,'android.widget.ImageView');return {packageName:()=>({find:()=>({size:()=>1,get:()=>close})})};};
c.appendNativePromoClose(page);assert.equal(c.findInviteCashPopupClose(page).text,'关闭推广弹窗');
const noHeading=page.filter(x=>x.text!=='小说推荐官'&&x.text!=='关闭推广弹窗');c.appendNativePromoClose(noHeading);
assert.equal(c.findInviteCashPopupClose(noHeading),null);
});
test('抽奖推广标题与前往按钮同时存在才触发关闭,不把普通抽奖任务当弹窗',()=>{
const {c}=load();const page=JSON.parse(fs.readFileSync(path.join(__dirname,'fixtures/vivo-raffle-popup.json'),'utf8'));
assert.equal(c.dismissOnlyPromoPopupHeading(page).text,'抽奖嘉好礼');assert.equal(c.needsCommonHandling(page),true);
assert.equal(c.dismissOnlyPromoPopupHeading(page.filter(x=>x.text!=='前住抽奖')),null);
});
test('抽奖推广叉号局部OCR仅扫描操作按钮下方中央区域',()=>{
const {c}=load();const page=JSON.parse(fs.readFileSync(path.join(__dirname,'fixtures/vivo-raffle-popup.json'),'utf8'));let calls=0;
c.readPromoCross=()=>{};
c.readCloseRegion=(frame,items,region)=>{calls++;assert.ok(region.top>1510);assert.ok(region.left>=1080*.4);
items.push({text:'X',rect:{left:522,top:1740,right:558,bottom:1790}});};
c.readReceiptCloseRegion({},page);assert.equal(calls,1);assert.equal(c.findInviteCashPopupClose(page).text,'X');
});
test('白色叉号图形检测定位四条斜臂,拒绝实心白块和区域外悬浮叉号',()=>{
const {c}=load();const action={rect:{bottom:1510}};
function pixels(cx,cy,solid){return {pixel:(_frame,x,y)=>{
const dx=x-cx,dy=y-cy;const white=solid?Math.abs(dx)<18&&Math.abs(dy)<18:
Math.abs(dx)<=14&&Math.abs(dy)<=14&&Math.abs(Math.abs(dx)-Math.abs(dy))<=2;
return white?0xffffffff:0xff444444;}};}
for(const [cx,cy,solid,expected] of [[540,1766,false,true],[540,1766,true,false],[990,1766,false,false]]){
c.images=pixels(cx,cy,solid);const items=[];c.readPromoCross({},items,action);
assert.equal(items.length>0,expected);if(expected){const r=items[0].rect;assert.ok(Math.abs((r.left+r.right)/2-cx)<=2);}
}
});