15 lines
3.5 KiB
JavaScript
15 lines
3.5 KiB
JavaScript
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),vm=require('node:vm'),{parse}=require('acorn');
|
|
const source=fs.readFileSync('src/app.js','utf8'),functions=parse(source,{ecmaVersion:2017}).body[0].expression.callee.body.body.filter(n=>n.type==='FunctionDeclaration'&&['makeVideo','shareVideo'].includes(n.id.name)).map(n=>source.slice(n.start,n.end)).join('\n');
|
|
function app({generate,api=true,failPublish=false,modal=false}={}){
|
|
const calls=[],toasts=[],output={blob:{},seconds:4},session={},card={levelName:'浙江',score:100,snapName:'轻松拼',bounceName:'蹦蹦跳'};let generated=0;
|
|
const native={writeTempFile:async value=>{calls.push({file:value});return {filePath:'tmp/'+calls.length};},postNote:async value=>{calls.push({post:value});if(failPublish)throw Error('cancelled');}};
|
|
const state={videoJob:null,videoCapture:session,videoOutput:null,videoNotice:'',videoSettled:true,result:card,exporting:false,screen:'result',Date,Math,syncVideoUI(){},toast:s=>toasts.push(s),$:id=>id==='modal'?{hasAttribute:()=>modal}:{toDataURL:()=> 'data:image/jpeg;base64,eA=='},window:{xhs:api?{miniTool:native}:null,PuzzleVideo:{generate:async (...args)=>{generated++;return generate?generate(...args):output;},dataURL:async()=> 'data:video/mp4;base64,eA=='}}};
|
|
vm.createContext(state);vm.runInContext(functions,state);return {state,calls,toasts,output,generated:()=>generated};
|
|
}
|
|
test('发视频一次点击完成生成并打开视频发布编辑页,携带视频与成绩封面',async()=>{const a=app();await a.state.makeVideo();assert.equal(a.generated(),1);assert.equal(a.calls.length,3);assert.equal(a.calls[0].file.data,'data:video/mp4;base64,eA==');assert.equal(a.calls[1].file.data,'data:image/jpeg;base64,eA==');assert.equal(a.calls[2].post.pageType,'video_publish');assert.equal(a.calls[2].post.mediaInfo.video_resources.video_url,'tmp/1');assert.equal(a.calls[2].post.mediaInfo.video_resources.cover_url,'tmp/2');assert.equal(a.state.videoJob,null);assert.equal(a.state.exporting,false);});
|
|
test('生成中再次点击取消,不打开发布页且可重新尝试',async()=>{let done;const a=app({generate:()=>new Promise(resolve=>{done=resolve;})});const task=a.state.makeVideo();await a.state.makeVideo();assert.equal(a.state.videoJob.cancelled,true);done(a.output);await task;assert.equal(a.calls.length,0);assert.equal(a.state.videoOutput,null);assert.equal(a.state.videoJob,null);});
|
|
test('离开原局时不会自动发布旧视频',async()=>{let done;const a=app({generate:()=>new Promise(resolve=>{done=resolve;})}),task=a.state.makeVideo();a.state.videoJob=null;a.state.videoCapture={};done(a.output);await task;assert.equal(a.calls.length,0);assert.equal(a.state.videoOutput,null);});
|
|
test('无小红书SDK时保留生成的视频并提示客户端发布',async()=>{const a=app({api:false});await a.state.makeVideo();assert.equal(a.state.videoOutput,a.output);assert.equal(a.calls.length,0);assert.ok(a.toasts.some(s=>s.includes('视频已生成')));});
|
|
test('发布取消或失败后再次点击复用视频,不重复编码',async()=>{const a=app({failPublish:true});await a.state.makeVideo();assert.equal(a.generated(),1);assert.equal(a.state.videoOutput,a.output);await a.state.makeVideo();assert.equal(a.generated(),1);assert.equal(a.calls.filter(c=>c.post).length,2);assert.equal(a.state.exporting,false);});
|
|
test('生成期间打开重开确认弹窗,完成后暂存视频而不突然拉起发布页',async()=>{const a=app({modal:true});await a.state.makeVideo();assert.equal(a.state.videoOutput,a.output);assert.equal(a.calls.length,0);});
|