- 只发送有评分且非占位描述的片段,按 score 排序,每素材最多 8 段、全局最多 60 段 - 新增选材规则:优先高分片段、按主题匹配、多素材分散、禁止无关素材与编造音乐 - 提示词数据按预算逐级裁剪(本地模型 3800 字符),避免超出上下文窗口报错 - 区分新建创作与局部修改模式,新建时先清空再重建,修改时只动提及字段 - 明确数值范围与时间轴规则(不重叠、时长=(out-in)/speed、图片最长 60 秒)
38 lines
3.9 KiB
JavaScript
38 lines
3.9 KiB
JavaScript
import {test} from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import http from 'node:http';
|
|
import {complete,listModels,parseJson,plannerPrompt} from '../desktop/core/providers.mjs';
|
|
import {getStyle} from '../desktop/core/styles.mjs';
|
|
test('适配器协议合约(本机测试服务器,不代表云端联调)',async()=>{
|
|
let body,url,headers;const server=http.createServer(async(req,res)=>{let data='';for await(const part of req)data+=part;body=data?JSON.parse(data):null;url=req.url;headers=req.headers;res.setHeader('Content-Type','application/json');if(req.url==='/api/chat')res.end(JSON.stringify({message:{content:'{"ok":true}'},eval_count:4}));else if(req.url==='/messages')res.end(JSON.stringify({content:[{type:'text',text:'{"ok":true}'}]}));else if(req.url.includes('generateContent'))res.end(JSON.stringify({candidates:[{content:{parts:[{text:'{"ok":true}'}]}}]}));else if(req.url==='/models')res.end(JSON.stringify({data:[{id:'test-only'}]}));else res.end(JSON.stringify({choices:[{message:{content:'{"ok":true}'}}]}));});
|
|
await new Promise(r=>server.listen(0,'127.0.0.1',r));const baseUrl=`http://127.0.0.1:${server.address().port}`;
|
|
try{for(const protocol of ['ollama','openai','anthropic','gemini']){const r=await complete({protocol,baseUrl,model:'test-only',key:'contract-secret'},'json');assert.equal(r.data.ok,true);if(protocol==='anthropic'){assert.equal(headers['x-api-key'],'contract-secret');assert.equal(body.max_tokens,8192);}if(protocol==='gemini'){assert.ok(url.endsWith(':generateContent'));assert.equal(headers['x-goog-api-key'],'contract-secret');}if(protocol==='ollama')assert.equal(body.stream,false);}assert.equal((await listModels({protocol:'openai',baseUrl}))[0].id,'test-only');}
|
|
finally{server.closeAllConnections();server.close();}
|
|
await assert.rejects(complete({protocol:'openai',baseUrl:'https://example.com',model:'x',allowText:false},'private'),/未授权/);
|
|
assert.deepEqual(parseJson('```json\n{"ok":true}\n```'),{ok:true});
|
|
});
|
|
test('规划提示词:按评分筛选片段、丢弃占位描述、区分新建/修改模式、控制数据体量',()=>{
|
|
const mk=(id,score)=>({id,name:id,type:'video',duration:120,tags:['海边'],analysis:{segments:[
|
|
{start:0,end:5,description:'海滩日落,人物走过画面,光线柔和,构图开阔',score,reason:'构图好'},
|
|
{start:10,end:15,description:'尚未进行语义或动作识别',score:null},
|
|
{start:20,end:25,description:'城市夜景,车流灯光',score:score-10,reason:'节奏感强'},
|
|
]}});
|
|
const p={settings:{width:1920,height:1080,fps:30},media:[mk('a',90),mk('b',40)],clips:[{id:'c1',mediaId:'a',track:'video',start:0,in:0,out:5,speed:1,volume:1,motion:[{time:0,zoom:1,x:.5,y:.5}],text:'旧字幕'}]};
|
|
const fresh=plannerPrompt(p,'制作一个旅行短片',{fresh:true,target:45,style:getStyle('cinematic'),budget:9000});
|
|
assert.ok(fresh.includes('海滩日落'));
|
|
assert.ok(fresh.includes('城市夜景'));
|
|
assert.ok(!fresh.includes('尚未进行语义或动作识别'));
|
|
assert.ok(!fresh.includes('motion'));
|
|
assert.ok(fresh.includes('先 remove 现有全部片段'));
|
|
assert.ok(fresh.includes('45'));
|
|
assert.ok(fresh.includes('score'));
|
|
assert.ok(fresh.length<12000);
|
|
const modify=plannerPrompt(p,'把第一个镜头缩短一点',{fresh:false,budget:9000});
|
|
assert.ok(modify.includes('时间轴需要保留'));
|
|
assert.ok(modify.includes('c1'));
|
|
// 大数据量下仍受预算约束
|
|
const big={settings:p.settings,media:Array.from({length:40},(_,i)=>({id:'m'+i,name:'素材'+i,type:'video',duration:300,tags:['t'],analysis:{segments:Array.from({length:10},(_,j)=>({start:j*30,end:j*30+28,description:'第'+j+'段:这是很长的一段语义描述内容用来填充上下文窗口测试预算裁剪是否生效',score:99-j}))}})),clips:Array.from({length:60},(_,i)=>({id:'c'+i,mediaId:'m0',track:'video',start:i*5,in:0,out:5,speed:1,volume:1}))};
|
|
const small=plannerPrompt(big,'重新剪一遍',{fresh:true,budget:3800});
|
|
assert.ok(small.length<6500);
|
|
});
|