Files
past-go/weapp/tests/file-media.test.cjs
T
Blizzard 4c9708b562 feat: add WeChat Pay, Redis logging, storage tiers and personal albums
Replace virtual payments with WeChat Pay API v3 and verify callbacks and order polling. Cache WeChat access tokens in Redis and write structured Zap logs. Support configurable capacity plans across the API, admin and mini program, with purchase snapshots and quota enforcement. Allow personal albums before binding and share them with the current partner. Update deployment configuration and domain routing.
2026-11-02 23:40:49 +08:00

60 lines
3.5 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const path = require('node:path');
const createLoader = require('./load-module.cjs');
const {uploadReference} = require('../utils/file-media');
const id = '6083e119-6b26-441b-8b70-44d224366238';
const id2 = '7083e119-6b26-441b-8b70-44d224366238';
function client(extra = {}) {
const calls = [];
const load = createLoader({console, Promise, Date, wx: {
getStorageSync: () => 'token', hideLoading() {},
request(o) { calls.push(o); o.success({statusCode:200, data:{success:true}}); }, ...extra
}});
return {...load(path.join(__dirname, '../utils/request.js')), calls};
}
test('business requests persist IDs, preserve order and distinguish clear from omitted fields', async () => {
const c = client();
await c.api.diaryUpdate({id:'diary',images:['https://we.sundynix.cn/api/files/'+id, id2]});
await c.api.albumUpdate({id:'album',cover:''});
await c.api.updateProfile({nickname:'test'});
await c.api.photoAdd({album_id:'album',path:'/api/files/'+id});
await c.api.checkinDo({id:'checkin',photo:id});
assert.deepEqual(JSON.parse(JSON.stringify(c.calls[0].data)), {id:'diary',imageFileIds:[id,id2]});
assert.equal(c.calls[1].data.coverFileId,'');
assert.equal('avatarFileId' in c.calls[2].data,false);
assert.equal(c.calls[3].data.fileId,id);
assert.equal(c.calls[4].data.photoFileId,id);
});
test('duplicate and ID-only uploads keep stable display URLs and file metadata', async () => {
const c = client({compressImage(o){o.fail();},uploadFile(o){o.success({statusCode:200,data:JSON.stringify({success:true,duplicate:true,file:{id,url:'https://minio/raw-object'}})});}});
const result = await c.uploadFileCompat({url:c.API_BASE+'/upload',filePath:'http://tmp/new.jpg'});
assert.equal(uploadReference(result),id);
assert.equal(result.duplicate,true);
assert.equal(result.file.url,'https://minio/raw-object');
assert.equal(c.getImageUrl(result.path),'https://we.sundynix.cn/api/files/'+id);
assert.throws(()=>uploadReference({success:true,path:'http://tmp/new.jpg'}),/文件 ID/);
});
test('diary edit uploads HTTP temporary images, retains existing IDs and stops on upload failure', async () => {
let definition, saved, uploads=[];
const c = client();
const load = createLoader({console, Date, Promise, setTimeout(){}, Page(p){definition=p;}, wx:{showLoading(){},hideLoading(){},showToast(){}}}, {
'../../utils/request':{getImageUrl:c.getImageUrl,api:{async diaryUpdate(data){saved=data;}}}
});
load(path.join(__dirname,'../pages/diary_edit/diary_edit.js'));
const p = {...definition, data:{loading:false,form:{content:'text'},existingImages:[id],tempImages:[c.getImageUrl(id),'http://tmp/new.jpg']},setData(v){Object.assign(this.data,v);},async _uploadImage(v){uploads.push(v);return id2;}};
await p.submit();
assert.deepEqual(uploads,['http://tmp/new.jpg']);
assert.deepEqual(Array.from(saved.images),[id,id2]);
saved=null;p._uploadImage=async()=>{throw Error('network');};
await p.submit();assert.equal(saved,null);
});
test('media display keeps local assets and temporary paths without a backend directory prefix', () => {
const c = client();
assert.equal(c.getImageUrl('/images/default-avatar.png'), '/images/default-avatar.png');
assert.equal(c.getImageUrl('wxfile://tmp/photo.jpg'), 'wxfile://tmp/photo.jpg');
assert.equal(c.getImageUrl('http://tmp/photo.jpg'), 'http://tmp/photo.jpg');
assert.equal(c.getImageUrl(id), 'https://we.sundynix.cn/api/files/' + id);
});