app_auto_all/kuaishou_auto/tests/native-snapshot.test.js
2026-10-26 16:09:54 +08:00

55 lines
4.9 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'),fs=require('node:fs'),vm=require('node:vm');
const source=fs.readFileSync(require('node:path').join(__dirname,'../kuaishou-daily.auto.js'),'utf8');
function node(text,desc,id,box,visible=true){return {text:()=>text,desc:()=>desc,id:()=>id,bounds:()=>box,visibleToUser:()=>visible,enabled:()=>true};}
test('单次目标应用查询保留文字、描述与无文字广告关闭ID并剔除隐藏节点',()=>{
const c=vm.createContext({});vm.runInContext(source.slice(0,source.indexOf('// MAIN:')),c);
c.device={width:1080,height:2400};c.keepScreenAwake=()=>{};c.foregroundPackage=()=>c.CONFIG.packageName;c.files={write:()=>{}};
const box={left:10,top:100,right:200,bottom:160};
const nodes=[node('任务中心','任务中心','',box),node('','关闭弹窗','',box),
node('','','com.kuaishou.nebula:id/video_countdown_end_icon',box),node('隐藏','','',box,false),
node('越界','','',{left:0,top:2500,right:100,bottom:2600})];
let queries=0;c.packageName=p=>{assert.equal(p,'com.kuaishou.nebula');return {find:()=>{queries++;return {size:()=>nodes.length,get:i=>nodes[i]};}};};
const page=c.readSnapshot();assert.equal(queries,1);assert.deepEqual(Array.from(page,x=>x.text),['任务中心','关闭弹窗','@video_countdown_end_icon']);
});
test('vivo已成功领取14金币后识别原生关闭按钮,倒计时未结束时不关闭',()=>{
const c=vm.createContext({});vm.runInContext(source.slice(0,source.indexOf('// MAIN:')),c);c.device={width:1080,height:2400};
const p=JSON.parse(fs.readFileSync(require('node:path').join(__dirname,'fixtures/vivo-ad-reward.json'),'utf8'));
assert.equal(c.adReward(p),true);assert.equal(c.adClose(p).text,'@video_countdown_end_icon');
for(const label of ['28秒后可领取奖励','0秒后可领取奖励','已成功领取奖励']){
const page=p.map(x=>({...x,text:x.text==='已成功领取14金币'?label:x.text}));
assert.equal(!!c.adClose(page),label==='已成功领取奖励');
}
const background=p.map(x=>({...x,rect:{...x.rect,top:1500,bottom:1550}}));assert.equal(c.adClose(background),null);
});
test('去搜索点击紧邻按钮容器,不点击整行,容器过大时回退文字位置',()=>{
const c=vm.createContext({});vm.runInContext(source.slice(0,source.indexOf('// MAIN:')),c);c.device={width:1080,height:2400};c.foregroundPackage=()=>c.CONFIG.packageName;c.sleep=()=>{};c.writeRunLog=()=>{};
let clicks=0,presses=0;c.press=()=>{presses++;return true;};
const rect={left:822,top:1444,right:963,bottom:1507};let parentBox={left:763,top:1411,right:1022,bottom:1541};
const parent={bounds:()=>parentBox,enabled:()=>true,clickable:()=>true,click:()=>{clicks++;return true;}};
const item={text:'去搜索',rect,node:{clickable:()=>false,parent:()=>parent}};
c.clickItem(item);assert.equal(clicks,1);assert.equal(presses,0);
parentBox={left:43,top:1357,right:1037,bottom:1595};c.clickItem(item);assert.equal(clicks,1);assert.equal(presses,1);
});
test('实际到账1金币锁定退出信号,最高奖励与余额不触发',()=>{
const c=vm.createContext({});vm.runInContext(source.slice(0,source.indexOf('// MAIN:')),c);
for(const value of ['最高1金币','金币1','搜索得1金币'])c.recordRewardCoins(value);assert.equal(c.currentRoundRewardCoins,null);
c.recordRewardCoins('已成功领取1金币');c.recordRewardCoins('已获得50金币');assert.equal(c.currentRoundRewardCoins,1);
});
test('搜索与逛街类目单次1金币退出当前流程',()=>{
for(const kind of ['search','street']){
const c=vm.createContext({});vm.runInContext(source.slice(0,source.indexOf('// MAIN:')),c);
c.locateTask=()=>({title:'测试任务'});c.snapshot=()=>[];c.coinBalance=()=>100;c.ensureTaskCenter=()=>{};c.writeRunLog=()=>{};
c.boundedCall=(_name,_ms,fn)=>fn();c.executeRound=()=>{c.recordRewardCoins('恭喜获得1金币');return {rewardSeen:true};};
const result=c.drainTask({title:'测试任务'},kind,{});assert.equal(result.lastRewardCoins,1);assert.match(result.reason,/1金币/);
}
});
test('连续看视频区分待领取、观看和全部领完,阶段进度满不等于全部领完',()=>{
const c=vm.createContext({});vm.runInContext(source.slice(0,source.indexOf('// MAIN:')),c);c.device={width:1080,height:2400};
const p=JSON.parse(fs.readFileSync(require('node:path').join(__dirname,'fixtures/vivo-continuous-video.json'),'utf8'));
let card=c.continuousVideoCard(p);assert.equal(card.progress.done,900);assert.equal(card.watch.text,'去看视频');assert.equal(card.done,false);
let ready=p.map(x=>({...x,text:x.text==='去看视频'?'领金币':x.text}));assert.equal(c.continuousVideoCard(ready).claim.text,'领金币');
let full=p.map(x=>({...x,text:x.text==='900/12112'?'12112/12112':x.text}));assert.equal(c.continuousVideoCard(full).done,false);
const done=p.filter(x=>x.text!=='去看视频');done.push({text:'今日奖励已全部领取',rect:{left:100,top:942,right:800,bottom:990}});
assert.equal(c.continuousVideoCard(done).done,true);
});