import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import http from 'node:http'; import { createHash } from 'node:crypto'; import { PNG } from 'pngjs'; import jpeg from 'jpeg-js'; import { fixture,compatibleFixture,completion,until,assertToolPairs } from './helpers.mjs'; import { Store } from '../src/server/store.mjs'; import { Runtime } from '../src/server/runtime.mjs'; import { Provider } from '../src/server/provider.mjs'; import { createRequestHandler } from '../src/server/app.mjs'; import { InputAttachments,validateImage,IMAGE_LIMITS } from '../src/core/context/input-attachments.mjs'; import { verifyPlanEvidence } from '../src/core/context/request-plan.mjs'; import { completionBody,requestMetrics,requestImages } from '../src/shared/contracts/model-request.mjs'; export function pngInput(width=96,height=64) { const png=new PNG({width,height}); for(let p=0;pcreateHash('sha256').update(bytes).digest('hex'); const withBytes=(bytes,mediaType='image/png')=>({name:'image.'+(mediaType==='image/png'?'png':'jpg'),mediaType,base64:bytes.toString('base64')}); test('PNG bytes are fully decoded, metadata-free and bounded; JPEG EXIF orientation is applied to the sent PNG',()=>{ const value=validateImage(pngInput(2048,512)); assert.equal(value.sentWidth,1024);assert.equal(value.sentHeight,256);assert.ok(value.sent.length<=IMAGE_LIMITS.sentBytes); const rgba=Buffer.alloc(24*48*4);for(let p=0;p{ const bytes=Buffer.from(pngInput().base64,'base64'); for(const length of [0,1,8,16,22,24,32,bytes.length-1])assert.throws(()=>validateImage(withBytes(bytes.subarray(0,length)))); assert.throws(()=>validateImage(withBytes(bytes,'image/jpeg')),/类型不符/); const corrupt=Buffer.from(bytes);corrupt[29]^=1;assert.throws(()=>validateImage(withBytes(corrupt)),/解码/); assert.throws(()=>validateImage(withBytes(Buffer.concat([bytes,Buffer.from('payload')]))),/尾部/); const animation=Buffer.from(bytes);animation.write('acTL',37,'ascii');assert.throws(()=>validateImage(withBytes(animation)),/静态/); const huge=Buffer.from(bytes);huge.writeUInt32BE(9000,16);assert.throws(()=>validateImage(withBytes(huge)),error=>error.status===413); assert.throws(()=>validateImage({...pngInput(),name:'../secret.png'}),/文件名/); assert.throws(()=>validateImage({...pngInput(),base64:'%%%'}),/编码/); assert.throws(()=>validateImage(withBytes(Buffer.alloc(IMAGE_LIMITS.bytes+1))),error=>error.status===413); }); test('draft image ownership, withdrawal, reload and atomic Task binding retain original SHA and original bytes',t=>{ const f=fixture(t),manager=new InputAttachments(f.store),input=pngInput(),row=manager.upload(f.session,input); assert.equal(row.taskId,null);assert.equal(row.runId,null);assert.equal(row.sha256,sha(Buffer.from(input.base64,'base64'))); assert.deepEqual(manager.read(f.session,row.id,{original:true}).bytes,Buffer.from(input.base64,'base64')); const other=f.store.createSession(f.project.id);assert.throws(()=>manager.read(other,row.id),/归属|所属|不存在|不属于/); const selected=manager.validateSelection(f.session,[row.id]),run=f.store.beginRun(f.session,'Inspect screenshot',false,f.store.config,{inputAttachments:selected}); const boundId=manager.get(f.session,row.id).boundAttachmentId; assert.equal(manager.get(f.session,row.id).status,'consumed');assert.equal(manager.get(f.session,boundId).taskId,run.taskId);assert.equal(manager.images(f.session,f.store.core.task.get(run.taskId)).length,1); assert.throws(()=>manager.remove(f.session,row.id),/已发送/); const draft=manager.upload(f.session,pngInput(64,64));manager.remove(f.session,draft.id);assert.throws(()=>manager.read(f.session,draft.id),error=>error.status===410); f.store.close();const restored=new Store(f.data);f.onCleanup(()=>restored.close());const reloaded=new InputAttachments(restored),session=restored.session(f.session.id); assert.equal(reloaded.images(session,restored.core.task.get(run.taskId))[0].sha256,row.sentSha256); assert.equal(reloaded.list(session).length,1); }); test('a failed session transaction restores draft status and cannot leave a Run consuming the attachment',t=>{ const f=fixture(t),manager=new InputAttachments(f.store),row=manager.upload(f.session,pngInput()),save=f.store.storage.save; f.store.storage.save=()=>{throw new Error('owned transaction failure');}; assert.throws(()=>f.store.beginRun(f.session,'Inspect',false,f.store.config,{inputAttachments:manager.validateSelection(f.session,[row.id])}),/transaction failure/); f.store.storage.save=save;assert.equal(manager.get(f.session,row.id).status,'draft');assert.equal(f.store.core.run.size,0);assert.equal(f.session.runId,undefined); }); test('draft limits, duplicate selection, source changes and cross-Task association reject before dispatch',t=>{ const f=fixture(t),manager=new InputAttachments(f.store),row=manager.upload(f.session,pngInput()); assert.throws(()=>manager.validateSelection(f.session,[row.id,row.id]),/一张/); const artifact=f.store.core.artifact.get(row.sentArtifactId),file=path.join(f.store.artifactStore.root,artifact.relative); fs.appendFileSync(file,'tampered');assert.throws(()=>manager.validateSelection(f.session,[row.id]),/校验失败/); for(let i=1;i<8;i++)manager.upload(f.session,pngInput(i+10,10)); assert.throws(()=>manager.upload(f.session,pngInput(20,20)),/已满/); }); test('demo and legacy paths refuse image dispatch without creating a Task or consuming the draft',async t=>{ const f=fixture(t),provider=new Provider(),runtime=new Runtime(f.store,provider),row=runtime.inputAttachments().upload(f.session,pngInput()); assert.throws(()=>runtime.start(f.session.id,'Inspect',false,{attachmentIds:[row.id]}),/演示/); assert.equal(f.store.core.run.size,0);assert.equal(runtime.inputAttachments().get(f.session,row.id).status,'draft'); await assert.rejects(()=>provider.complete({...f.store.config,inputImages:[{dataUrl:'not-sent'}]},[],null),/视觉能力/); }); for(const protocol of ['chat-completions','responses'])test(protocol+' sends one owned image per actual request, pairs original tools and verifies exact sealed bytes',async t=>{ const f=fixture(t),server=await compatibleFixture(t,(_url,body,requests)=>{ assert.equal(requestImages(body).length,1); if(protocol==='responses')return {body:{status:'completed',output:requests.length===1?[{type:'function_call',call_id:'image_files',name:'list_files',arguments:'{"path":"","depth":1}'}] :[{type:'message',role:'assistant',content:[{type:'output_text',text:'截图与工程已核对'}]}],usage:{input_tokens:300,output_tokens:20,total_tokens:320}}}; return completion(requests.length===1?[{id:'image_files',type:'function',function:{name:'list_files',arguments:'{"path":"","depth":1}'}}]:[],requests.length===1?'':'截图与工程已核对'); }); const provider=new Provider(),hub=provider.modelHub(f.store),connection=hub.upsert({name:'Owned visual',model:'fixture',baseUrl:server.baseUrl,protocol,requiresKey:false,contextLimit:64000,capabilityDeclarations:{vision:'supported'}}); hub.select({type:'connection',id:connection.id});const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll()); const row=runtime.inputAttachments().upload(f.session,pngInput());runtime.start(f.session.id,'只根据此图核对本工程',false,{attachmentIds:[row.id]});await until(()=>!runtime.isActive(f.session.id),15000); assert.equal(f.session.status,'completed',JSON.stringify(f.session.messages));assert.equal(server.requests.length,2);assertToolPairs(f.session.providerMessages); assert.equal(JSON.stringify(f.session.providerMessages).includes('data:image'),false);assert.deepEqual(f.session.messages.find(value=>value.role==='user').attachmentIds,f.store.core.task.get(f.session.taskId).attachmentIds); for(const plan of f.store.core.context_plan.values()) { assert.equal(plan.imageCount,1);assert.ok(plan.imageReservationTokens>=4096);assert.doesNotThrow(()=>verifyPlanEvidence(f.store,f.session,plan)); plan.inputImages[0].sha256='0'.repeat(64);assert.throws(()=>verifyPlanEvidence(f.store,f.session,plan),/图片/);plan.inputImages[0].sha256=row.sentSha256; } }); test('mixed pool excludes unknown vision before lease and sends zero HTTP to the incompatible member',async t=>{ const f=fixture(t),bad=await compatibleFixture(t,()=>{throw new Error('must not dispatch');}),good=await compatibleFixture(t,()=>completion([],'Done')); const provider=new Provider(),hub=provider.modelHub(f.store),a=hub.upsert({name:'Text',model:'text',baseUrl:bad.baseUrl,requiresKey:false,contextLimit:64000}), b=hub.upsert({name:'Vision',model:'vision',baseUrl:good.baseUrl,requiresKey:false,contextLimit:64000,capabilityDeclarations:{vision:'supported'}}); const pool=hub.pool({name:'Mixed',members:[{connectionId:a.id},{connectionId:b.id}]});hub.select({type:'pool',id:pool.id}); const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());const row=runtime.inputAttachments().upload(f.session,pngInput()); runtime.start(f.session.id,'Inspect',false,{attachmentIds:[row.id]});await until(()=>!runtime.isActive(f.session.id)); assert.equal(f.session.status,'completed');assert.equal(bad.requests.length,0);assert.equal(good.requests.length,1); const attempt=hub.view().attempts[0];assert.ok(attempt.requirements.includes('vision'));assert.ok(attempt.rejections.some(item=>item.connectionId===a.id&&item.capability==='vision')); }); test('vision probe requires correct randomized pixel order and evidence expires when the connection revision changes',async t=>{ const f=fixture(t),server=await compatibleFixture(t,(_url,body)=>{ const png=PNG.sync.read(Buffer.from(requestImages(body)[0].split(',')[1],'base64')); const names=[[230,0,0,'RED'],[0,180,0,'GREEN'],[0,0,230,'BLUE'],[240,220,0,'YELLOW']]; const result=[[10,10],[100,10],[10,100],[100,100]].map(([x,y])=>names.find(rgb=>rgb.slice(0,3).every((value,c)=>png.data[(y*128+x)*4+c]===value))[3]); return completion([],result.join(',')); }); const provider=new Provider(),hub=provider.modelHub(f.store),connection=hub.upsert({name:'Probe',model:'owned',baseUrl:server.baseUrl,requiresKey:false}); await hub.probe(connection.id,'vision');assert.equal(hub.view().connections.find(value=>value.id===connection.id).capabilities.vision,'verified'); hub.upsert({...hub.connection(connection.id),name:'Changed'});assert.equal(hub.view().connections.find(value=>value.id===connection.id).capabilities.vision,'unknown'); const failed=await compatibleFixture(t,()=>completion([],'OK')),other=hub.upsert({name:'Blind',model:'owned',baseUrl:failed.baseUrl,requiresKey:false}); await assert.rejects(()=>hub.probe(other.id,'vision'),/未正确识别/);assert.equal(hub.view().connections.find(value=>value.id===other.id).capabilities.vision,'unknown'); }); test('HTTP upload/read/withdraw uses session authorization, returns byte SHA and refuses unknown ownership',async t=>{ const f=fixture(t),provider=new Provider(),runtime=new Runtime(f.store,provider),server=http.createServer(); server.on('request',createRequestHandler({appDirectory:process.cwd(),server,store:f.store,provider,runtime,token:'owned',clients:new Set(),started:Date.now(),getPort:()=>server.address().port})); await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));t.after(()=>new Promise(resolve=>{server.closeAllConnections();server.close(resolve);})); const root='http://127.0.0.1:'+server.address().port,base='/api/sessions/'+f.session.id+'/input-attachments'; const request=(route,method='GET',body,token='owned')=>fetch(root+route,{method,headers:{'X-Workbench-Token':token,'Content-Type':'application/json'},...(body?{body:JSON.stringify(body)}:{})}); assert.equal((await request(base,'POST',pngInput(),'invalid')).status,403); const uploaded=await request(base,'POST',pngInput());assert.equal(uploaded.status,201);const row=await uploaded.json(); const image=await request(base+'/'+row.id+'/image');assert.equal(image.headers.get('X-Content-SHA256'),row.sentSha256);assert.equal(sha(Buffer.from(await image.arrayBuffer())),row.sentSha256); const other=f.store.createSession(f.project.id);assert.equal((await request('/api/sessions/'+other.id+'/input-attachments/'+row.id+'/image')).status,404); assert.equal((await request(base+'/'+row.id,'DELETE',{})).status,200);assert.equal((await request(base+'/'+row.id+'/image')).status,410); }); test('image accounting excludes Base64 as text but seals every original request byte and reserves image input separately',()=>{ const value=validateImage(pngInput()),image={id:'owned',sha256:sha(value.sent),width:value.sentWidth,height:value.sentHeight,dataUrl:'data:image/png;base64,'+value.sent.toString('base64')}; const config={protocol:'responses',model:'owned',maxOutputTokens:512,inputImages:[image]}; const body=completionBody(config,[{role:'user',content:'Inspect'}],[]),metrics=requestMetrics(body); assert.equal(metrics.bytes,Buffer.byteLength(JSON.stringify(body)));assert.equal(metrics.sha256,sha(JSON.stringify(body)));assert.equal(metrics.imageCount,1); assert.equal(metrics.imageEstimate,'local-reservation-not-provider-billing');assert.equal(metrics.imageReservationTokens,4096); assert.ok(metrics.estimatedInputTokens>4096);assert.equal(requestImages(body).length,1); });