ufutx_love_mp/tools/local-acceptance/friend-eligibility.test.cjs

21 lines
3.1 KiB
JavaScript

const fs=require('fs'),vm=require('vm'),assert=require('assert');
const source=fs.readFileSync('ufutx_love_mp/src/pages/home/information.wpy','utf8').match(/<script>([\s\S]*?)<\/script>/)[1].replace(/^import .*$/gm,'');
let options,modal,sheet,navigations=[];
const wx={getStorageSync:()=> 'synthetic-token',showModal:o=>{modal=o},showActionSheet:o=>{sheet=o},navigateTo:o=>navigations.push(o.url)};
vm.runInNewContext(source,{wepy:{page:o=>{options=o}},https:{},base:{},service:{host:'https://synthetic.invalid'},wx,console});
const allow={allowed:true,action:'allow',remaining:1,unlimited:false,required:['education']};
function page(statuses){let gets=0,posts=0,greetings=0;return Object.assign({},options.methods,{id:123,message:'synthetic',throttle:true,modalName:'',checkingFriendEligibility:false,$signInVerify:()=>true,$showToast:()=>{},$get:async()=>({code:0,data:statuses[Math.min(gets++,statuses.length-1)]}),$post:async()=>{posts++;return {code:0}},getGreet:()=>{greetings++},counts:()=>({gets,posts,greetings})})}
(async()=>{
for(const [key,url] of Object.entries({real:'/pages/users/realName',photo:'/pages/users/unmarriV2?progressShow=false',profile:'/pages/users/unmarriV2?progressShow=false',education:'/pages/users/degreeCertificate'})) {
const p=page([{action:'tasks',required:[key],allowed:false,remaining:0}]);await p.contactFriendFn();assert.equal(p.counts().greetings,0);assert(!modal.content.includes('升级会员'));modal.success({confirm:false});assert.equal(navigations.length,0);modal.success({confirm:true});sheet.success({tapIndex:0});assert.equal(navigations.pop(),url);await p.sendMsg();assert.equal(p.counts().posts,0);
}
let p=page([{action:'tasks',required:['real','photo','profile','education'],allowed:false}]);await p.contactFriendFn();modal.success({confirm:true});assert.equal(sheet.itemList.length,4);
p=page([{action:'purchase',required:[],remaining:0,allowed:false}]);await p.contactFriendFn();assert.equal(navigations.length,0);assert(modal.content.includes('无限次'));modal.success({confirm:true});assert.equal(navigations.pop(),'/pages/users/upgradeVIP');
p=page([allow]);await p.contactFriendFn();assert.equal(p.counts().greetings,1);
p=page([allow,{action:'tasks',required:['photo'],remaining:0,allowed:false}]);await p.sendMsg();await new Promise(setImmediate);assert.equal(p.counts().posts,1);assert(modal.content.includes('真人头像'));assert.equal(navigations.length,0);
p=page([allow,{action:'purchase',required:[],remaining:0,allowed:false}]);await p.sendMsg();await new Promise(setImmediate);assert.equal(p.counts().posts,1);assert.equal(modal.confirmText,'升级会员');
p=page([{allowed:true,unlimited:true,remaining:-1,action:'allow'}]);await p.sendMsg();await new Promise(setImmediate);assert.equal(navigations.length,0);
p=page([]);p.$get=async()=>{throw Error('synthetic offline')};await p.sendMsg();assert.equal(p.counts().posts,0);assert(p.throttle);
console.log('PASS task choice, four independent rewards, exhausted guidance, membership confirmation, post-send refresh, member bypass, network failure; no real requests');
})().catch(e=>{console.error(e);process.exitCode=1});