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('JSON 容错解析:思考标签、前置文字、嵌套花括号与字符串中的花括号',()=>{ assert.deepEqual(parseJson('思考过程{"ok":true}'),{ok:true}); assert.deepEqual(parseJson('好的,以下是结果:\n```json\n{"a":{"b":[1,{"c":"带}和{括号"}]}}\n```\n希望有帮助'),{a:{b:[1,{c:'带}和{括号'}]}}); assert.deepEqual(parseJson('{"results":[{"id":"x","text":"}]"}]}'),{results:[{id:'x',text:'}]'}]}); assert.throws(()=>parseJson('完全没有 JSON 内容'),/JSON/); }); test('JSON 解析失败自动重试:第二次不再强制 format 并追加严格提示',async()=>{ const calls=[]; const server=http.createServer(async(req,res)=>{let data='';for await(const part of req)data+=part;const body=JSON.parse(data);calls.push(body);res.setHeader('Content-Type','application/json'); if(body.format==='json')res.end(JSON.stringify({message:{content:'这是一段没有 JSON 的回答'},eval_count:3})); else res.end(JSON.stringify({message:{content:'{"ok":true}'},eval_count:4}));}); await new Promise(r=>server.listen(0,'127.0.0.1',r));const baseUrl=`http://127.0.0.1:${server.address().port}`; try{ const r=await complete({protocol:'ollama',baseUrl,model:'test-only'},'请输出 JSON'); assert.equal(r.data.ok,true); assert.equal(calls.length,2); assert.equal(calls[1].format,undefined); assert.ok(calls[1].messages[0].content.includes('只输出一个 JSON 对象')); }finally{server.closeAllConnections();server.close();} }); 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('城市夜景')); 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); // 多轮对话历史:指代(这个/那个/现在)的上下文必须发给模型 const withHist=plannerPrompt(p,'把刚才说的那个删掉',{history:[{role:'user',text:'把片头字幕删掉'},{role:'assistant',text:'已删除片头字幕'}],budget:9000}); assert.ok(withHist.includes('把片头字幕删掉')); assert.ok(withHist.includes('对话记录')); assert.ok(withHist.includes('不得以「未收到具体要求」为由返回空方案')); // 长对话压缩:12 轮长历史也不会撑爆预算,且最后几轮的关键内容仍在 const longHist=Array.from({length:12},(_,i)=>({role:'user',text:`第${i+1}轮:把视频改成${60+i}秒,人物要多一点,画面换成主角和伙伴一起`})).flatMap((u,i)=>[u,{role:'assistant',text:`已按第${i+1}轮要求调整时间轴,共使用${5+i}个片段`}]); const compressed=plannerPrompt(p,'按最后一轮的要求再调整一下',{history:longHist,budget:3800}); assert.ok(compressed.includes('第12轮')); assert.ok(compressed.includes('第11轮')); assert.ok(compressed.length<7000); // 素材未分析时明确拦截:不允许凭空假设或整段取用 const raw={settings:p.settings,media:[{id:'u',name:'未分析素材',type:'video',duration:600,tags:[]}],clips:[]}; const unanalyzed=plannerPrompt(raw,'剪一个鲁迪和希露菲在一起的片段',{budget:9000}); assert.ok(unanalyzed.includes('素材均未经过 AI 分析')); assert.ok(unanalyzed.includes('不要直接把整段素材放上时间轴')); // 大量未分析素材 + 历史很长时,裁剪也不允许把素材裁空(回归:模型误报 media 为空) const manyRaw={settings:p.settings,media:Array.from({length:40},(_,i)=>({id:'r'+i,name:'剧集'+i,type:'video',duration:1500,tags:[]})),clips:[]}; const longHist2=Array.from({length:12},(_,i)=>({role:'user',text:`第${i+1}轮要求:改成${60+i}秒,人物多一点,画面换成主角和伙伴一起`})).flatMap((u,i)=>[u,{role:'assistant',text:`已按第${i+1}轮要求调整时间轴,共使用${5+i}个片段`}]); const manyPrompt=plannerPrompt(manyRaw,'剪一个主角和伙伴的60秒混剪',{fresh:true,budget:3800,history:longHist2}); const dataStart=manyPrompt.indexOf('{',manyPrompt.indexOf('项目数据')); const sentData=JSON.parse(manyPrompt.slice(dataStart)); assert.ok(sentData.media.length>0,'裁剪后素材列表不应为空'); assert.ok(manyPrompt.includes('素材均未经过 AI 分析')); // 项目真没有素材时,给出导入提示而不是「未分析」 const empty=plannerPrompt({settings:p.settings,media:[],clips:[]},'剪一个混剪',{budget:9000}); assert.ok(empty.includes('没有可用素材')); }); test('模型不存在等常见错误附带可操作提示',async()=>{ const server=http.createServer((req,res)=>{res.statusCode=404;res.setHeader('Content-Type','application/json');res.end(JSON.stringify({error:"model 'gemma3:4b' not found"}));}); await new Promise(r=>server.listen(0,'127.0.0.1',r));const baseUrl=`http://127.0.0.1:${server.address().port}`; try{await assert.rejects(complete({protocol:'ollama',baseUrl,model:'gemma3:4b'},'帮我剪'),/未安装/);} finally{server.closeAllConnections();server.close();} });