ufutx_love_mp/tools/sharing/check-photo.cjs

30 lines
2.1 KiB
JavaScript

const assert=require('assert'),{createHarness}=require('./harness.cjs')
;(async()=>{
const h=createHarness(),{photoScore,selectSharingPhoto}=h.load('utils/sharing-photo')
assert.equal(photoScore(200,200),-1);assert.equal(photoScore(2400,400),-1)
assert(photoScore(1372,940)>photoScore(600,900))
h.wx.getImageInfo=({src,success,fail})=>queueMicrotask(()=>{
const sizes={small:[200,200],portrait:[600,900],fit:[1372,940],panorama:[2400,400]}
if(!sizes[src])return fail({});success({path:src,width:sizes[src][0],height:sizes[src][1]})
})
assert.equal(await selectSharingPhoto(['portrait','bad','small','fit','panorama']),'fit')
assert.equal(await selectSharingPhoto(['small','bad','panorama']),'')
assert.equal(await selectSharingPhoto([]),'')
const runtime=h.load('utils/sharing-runtime')
assert.equal(runtime.displayProfile({id:42,photo:'avatar-only'}).photo,'')
const empty=createHarness();empty.state.profile.photos=[]
const page=empty.page('shareCard');page.hook('onReady');await page.startSharing()
assert(page.poster);assert.equal(page.profile.photo,'');assert(empty.draws.some(x=>x[1]==='期待认真相识'))
const broken=createHarness();broken.state.profile.photos=['broken-photo']
const original=broken.wx.getImageInfo
broken.wx.getImageInfo=opts=>opts.src==='broken-photo'?queueMicrotask(()=>opts.fail({})):original(opts)
const fallback=broken.page('shareCard');fallback.hook('onReady');await fallback.startSharing()
assert(fallback.poster);assert.equal(fallback.profile.photo,'')
const decode=createHarness();decode.flags.failImageSrc='synthetic-photo'
const damaged=decode.page('shareCard');damaged.hook('onReady');await damaged.startSharing()
assert(damaged.poster);assert(decode.draws.some(x=>x[1]==='期待认真相识'))
const fingerprint=h.load('utils/sharing-cache').sharingFingerprint
assert.notEqual(fingerprint({id:42,photos:['one']},'personal'),fingerprint({id:42,photos:['two']},'personal'))
console.log('PASS: resolution/crop ranking, failed/tiny/extreme photos skipped, no avatar substitution, logo fallback rendered.')
})().catch(e=>{console.error(e.stack);process.exitCode=1})