23 lines
2.5 KiB
JavaScript
23 lines
2.5 KiB
JavaScript
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),vm=require('node:vm'),ts=require('typescript');
|
|
const serviceCode=ts.transpileModule(fs.readFileSync('assets/Script/module/Config/PyramidService.ts','utf8'),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2017}}).outputText;
|
|
test('Pyramid preloads retain dependencies, share a Prefab load and replace the cache on Bundle updates',async()=>{
|
|
let loads=0,holds=0,callback,bundle={load(_path,_type,done){loads++;callback=done;}};
|
|
const bridge={acquireBundle:async()=>{holds++;let closed=false;return{bundle,release(){if(!closed){closed=true;holds--;}}};}};
|
|
const context={exports:{},cc:{Prefab:class{}},require:name=>({default:name==='./ActivityBundleLoader'?bridge:{}})};vm.runInNewContext(serviceCode,context);const S=context.exports.default;
|
|
const a=S.prefab(),b=S.prefab();await new Promise(resolve=>setImmediate(resolve));assert.equal(loads,1);assert.equal(holds,2);
|
|
const first={name:'first'};callback(null,first);assert.equal(await a,first);assert.equal(await b,first);assert.equal(holds,0);
|
|
assert.equal(await S.prefab(),first);assert.equal(loads,1);assert.equal(holds,0);
|
|
bundle={load(_path,_type,done){loads++;done(null,{name:'new'});}};
|
|
assert.equal((await S.prefab()).name,'new');assert.equal(loads,2);assert.equal(holds,0);
|
|
bundle={load(_path,_type,done){loads++;done(Error('missing'));}};
|
|
await assert.rejects(S.prefab());assert.equal(holds,0);
|
|
});
|
|
test('editor uses Creator, WeChat uses the installed ZIP loader and missing build bootstrap is explicit',async()=>{
|
|
let legacy=0,native=0,cycles=0;const window={},cc={sys:{platform:'web',WECHAT_GAME:'wx'},assetManager:{loadBundle(name,done){legacy++;done(null,{name});}}};
|
|
const context={exports:{},cc,window};vm.runInNewContext(ts.transpileModule(fs.readFileSync('assets/Script/module/Config/ActivityBundleLoader.ts','utf8'),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2017}}).outputText,context);
|
|
const B=context.exports.default;assert.equal((await B.ensureBundle('pyramid')).name,'pyramid');assert.equal(legacy,1);
|
|
cc.sys.platform='wx';await assert.rejects(B.ensureBundle('pyramid'),/未初始化/);
|
|
window.__activityBundles={beginCycle(){cycles++;},ensureBundle:async name=>{native++;return{name};},acquireBundle:async()=>({bundle:{},release(){}})};
|
|
B.beginCycle();await B.ensureBundle('pyramid');await B.acquireBundle('pyramid');assert.equal(cycles,1);assert.equal(native,1);assert.equal(legacy,1);
|
|
});
|