MatchMaster/tools/coin-madness/check-entry-loading.cjs

21 lines
2.0 KiB
JavaScript

const assert=require('node:assert/strict'),fs=require('fs'),ts=require('typescript');
const code=ts.transpileModule(fs.readFileSync('assets/Script/coin_madness/CoinMadnessHost.ts','utf8'),{compilerOptions:{target:ts.ScriptTarget.ES2017,module:ts.ModuleKind.CommonJS,experimentalDecorators:true}}).outputText;
const flush=()=>new Promise(r=>setImmediate(r));
(async()=>{for(const fail of [false,true]) {
let resolve,reject,asset,events=[];
const first=new Promise((a,b)=>{resolve=a;reject=b});
const service={readForDisplay:()=>first,refresh:()=>new Promise(()=>{}),stopPolling(){},settlementNotice:0};
const bundle={load:(path,type,cb)=>asset=cb,releaseAll(){}};
const panel={getComponent:()=>({initialize(data,api){assert(api.initialReady);events.push('initialize')}}),destroy(){}};
const cc={fx:{GameConfig:{GM_INFO:{uid:"test"}}},Component:class{},_decorator:{ccclass:x=>x},assetManager:{loadBundle:(name,cb)=>cb(null,bundle),getBundle:()=>bundle,removeBundle(){}},Prefab:class{},macro:{MAX_ZINDEX:100},instantiate:()=>{events.push('instantiate');return panel},director:{once:(e,cb)=>cb()},Director:{EVENT_AFTER_DRAW:1}};
const out={};new Function('require','exports','cc',code)(()=>({default:{serverUrl:()=> 'test'}}),out,cc);
const host=new out.default();host.service=service;host.toast=()=>{};host.refreshCoinDisplay=()=>{};host.clearHelp=()=>{};
host.node={getComponent:()=>({openLoad:()=>events.push('loading'),closeLoad:()=>events.push('stop')}),addChild:()=>events.push('mount')};
host.open();host.open();assert(host.isPopupActive());asset(null,{});await flush();assert.deepEqual(events,['loading'],'assets alone must not reveal the panel');
if(fail)reject(new Error('network'));else resolve({state:{}});
await flush();
assert.deepEqual(events,fail?['loading','stop']:['loading','stop','instantiate','mount','initialize']);
}
console.log('PASS: slow first snapshot keeps only loader; loader stops before panel mounts; failed snapshot closes loader without mounting.');
})().catch(e=>{console.error(e);process.exitCode=1});