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

20 lines
2.9 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,navigations=[];
const wx={getStorageSync:()=> 'synthetic-token',showModal:o=>{modal=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};
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()=>{
let incomplete=page([{action:'profile',required:['profile'],allowed:false,remaining:0}]);incomplete.$signInVerify=()=>false;await incomplete.contactFriendFn();assert.equal(incomplete.counts().greetings,0);assert.equal(modal.title,'请先完善个人资料');modal.success({confirm:true});assert.equal(navigations.pop(),'/pages/users/unmarriV2?progressShow=false');await incomplete.sendMsg();assert.equal(incomplete.counts().posts,0);
let p=page([{action:'verify',required:['real','education'],allowed:false}]);await p.contactFriendFn();assert.equal(p.counts().greetings,0);modal.success({confirm:true});assert.equal(navigations.pop(),'/pages/users/realName');
p=page([{action:'verify',required:['education'],allowed:false}]);await p.contactFriendFn();modal.success({confirm:true});assert.equal(navigations.pop(),'/pages/users/degreeCertificate');
p=page([{action:'purchase',remaining:0,allowed:false}]);await p.contactFriendFn();assert.equal(navigations.pop(),'/pages/users/upgradeVIP');assert.equal(p.counts().greetings,0);
p=page([allow]);await p.contactFriendFn();assert.equal(p.counts().greetings,1);
p=page([allow,{action:'purchase',remaining:0,allowed:false}]);await p.sendMsg();await new Promise(setImmediate);assert.equal(p.counts().posts,1);assert.equal(navigations.pop(),'/pages/users/upgradeVIP');
p=page([{action:'verify',required:['real'],allowed:false}]);await p.sendMsg();assert.equal(p.counts().posts,0);
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: real/education guidance, verified entry, exhausted redirect, third successful request redirect, direct-send gating, member unchanged, network failure closed; no real requests');
})().catch(e=>{console.error(e);process.exitCode=1});