42 lines
3.2 KiB
JavaScript
42 lines
3.2 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([]),'')
|
|
assert.equal(await selectSharingPhoto(['portrait','fit','small','bad'],{random:true,previous:'fit'}),'portrait')
|
|
assert.equal(await selectSharingPhoto(['fit','fit'],{random:true,previous:'fit'}),'fit')
|
|
assert.equal(await selectSharingPhoto(['bad','small'],{random:true}),'')
|
|
assert.equal(await selectSharingPhoto(['portrait','fit'],{preferred:'portrait'}),'portrait')
|
|
const refresh=createHarness();refresh.state.profile.photos=['first','second']
|
|
const refreshed=refresh.page('shareCard');refreshed.hook('onReady');await refreshed.startSharing()
|
|
const initial=refreshed.profile.photo;await refreshed.refreshPoster()
|
|
assert.notEqual(refreshed.profile.photo,initial);assert(refreshed.poster)
|
|
const chosen=refreshed.profile.photo,poster=refreshed.poster,exportsBefore=refresh.state.exportID
|
|
const revisit=refresh.page('shareCard');revisit.hook('onReady');await revisit.startSharing()
|
|
assert.equal(revisit.profile.photo,chosen);assert.equal(revisit.poster,poster);assert.equal(refresh.state.exportID,exportsBefore)
|
|
refresh.state.profile.photos=[initial];await revisit.loadSharingProfile();assert.equal(revisit.profile.photo,initial)
|
|
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})
|