const assert=require('assert'),{createHarness,settle}=require('./harness.cjs') ;(async()=>{ const h=createHarness(),sharing=h.load('utils/sharing'),runtime=h.load('utils/sharing-runtime') assert.equal(sharing.sharingPath(42,false),'/pages/tabBar/welcome?from_user_id=42') assert.equal(sharing.sharingPath(42,true),'/pages/home/information?id=42') for(const id of [0,-1,'bad',1.5])assert.throws(()=>sharing.sharingPath(id,true)) await assert.rejects(sharing.renderSharingPoster({$wx:{}},{},false)) const personal=h.page('shareCard');await personal.startSharing() assert.equal(personal.poster,'');assert(!h.calls.some(x=>x.url&&x.url.endsWith('/code')),'must wait for onReady') personal.hook('onReady');await settle(()=>personal.poster&&!personal.generating,'initial render') assert(h.draws.some(x=>x[1]==='扫码认识我'));assert(h.draws.some(x=>x[1]==='synthetic-photo'));assert(h.draws.some(x=>x[0]==='text'&&x[1].endsWith('…'))) assert(personal.cached);assert(personal.poster.startsWith('/synthetic-user/')) const requestCount=h.calls.filter(x=>x.url&&x.url.endsWith('/code')).length,exportCount=h.state.exportID const revisit=h.page('shareCard');revisit.hook('onReady');await revisit.startSharing() assert(revisit.poster);assert(revisit.cached);assert.equal(h.state.exportID,exportCount);assert.equal(h.calls.filter(x=>x.url&&x.url.endsWith('/code')).length,requestCount) h.draws.length=0;const invite=h.page('myShare');invite.hook('onReady');await invite.startSharing() assert(invite.poster);assert(h.draws.some(x=>x[1]==='扫码加入福恋'));assert(!h.draws.some(x=>x[1]==='synthetic-photo')) for(const response of [{statusCode:404,data:{}},{statusCode:500,data:'html'},{statusCode:200,data:{code:9}},{statusCode:200,data:{code:2}}]){h.flags.response=response;await assert.rejects(runtime.sharingRequest('https://synthetic.invalid/user/profile'))} delete h.flags.response;h.flags.requestHang=true;await assert.rejects(runtime.sharingRequest('https://synthetic.invalid/user/profile'));assert(h.state.abort>0) console.log('PASS: Canvas2D after onReady, first render and persistent cache reuse, purpose separation, truncation, HTTP/auth/unknown-code errors and missing callback timeout.') })().catch(error=>{console.error(error.stack);process.exitCode=1})