23 lines
1.8 KiB
JavaScript
23 lines
1.8 KiB
JavaScript
const fs=require('fs'),vm=require('vm'),assert=require('assert'),path=require('path')
|
|
const source=fs.readFileSync(path.join(__dirname,'../../src/utils/sharing.js'),'utf8').replace(/export /g,'')
|
|
const calls=[]
|
|
const context={fillRect(){},drawImage(...a){calls.push(['image',...a])},setFillStyle(){},setFontSize(){},fillText(t){calls.push(['text',t])},measureText(t){return {width:t.length*28}},draw(_,cb){cb()}}
|
|
const sandbox={wx:{getImageInfo({src,success}){success({path:src,width:500,height:500})},createCanvasContext(){return context},canvasToTempFilePath({success}){success({tempFilePath:'synthetic.png'})}}}
|
|
vm.createContext(sandbox);vm.runInContext(source,sandbox)
|
|
assert.equal(sandbox.sharingPath(42,false),'/pages/tabBar/welcome?from_user_id=42')
|
|
assert.equal(sandbox.sharingPath(42,true),'/pages/home/information?id=42')
|
|
for(const id of [0,-1,'bad',1.5]) assert.throws(()=>sandbox.sharingPath(id,true))
|
|
;(async()=>{
|
|
await assert.rejects(sandbox.renderSharingPoster({$wx:{}},{},false))
|
|
const profile={nickname:'合成用户',photo:'synthetic-photo.png',qrcode:'synthetic-code.png',introduction:'这是用于离线验证的合成介绍。'.repeat(30),details:'合成城市 · 合成职业'}
|
|
await sandbox.renderSharingPoster({$wx:{}},profile,true)
|
|
assert(calls.some(c=>c[1]==='扫码认识我'))
|
|
assert(calls.some(c=>c[0]==='image'&&c[1]==='synthetic-photo.png'))
|
|
assert(calls.some(c=>c[0]==='text'&&c[1].endsWith('…')))
|
|
calls.length=0
|
|
await sandbox.renderSharingPoster({$wx:{}},profile,false)
|
|
assert(calls.some(c=>c[1]==='扫码加入福恋'))
|
|
assert(!calls.some(c=>c[0]==='image'&&c[1]==='synthetic-photo.png'))
|
|
console.log('PASS: separate destinations, invalid IDs, missing QR, personal/invite content and long-text truncation (offline mocks).')
|
|
})().catch(e=>{console.error(e.message);process.exitCode=1})
|