gongxiangp2/apps/detail-agent-pi/test/candidate-review.test.mjs

79 lines
5.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import assert from 'node:assert/strict';
import test from 'node:test';
import { reviewProductionCandidate } from '../src/candidate-review.mjs';
test('candidate review binds the candidate and separates shown fields from numbered screen copy',async()=>{
const slot={id:'cover',fieldId:'S01-I01',label:'主图'};
const pixels={type:'image',mimeType:'image/png',data:'fixture'};
const screenCopy=[{field:'K02',label:'颜色名称',text:'橡木'},{text:'无字段文案'}];
const canvas={detailImages:async()=>({candidateId:'photo',referenceCount:1,images:[pixels,pixels]})};
const screen={image:pixels,details:{page:0,screenFingerprint:'fixture',slots:[slot],screenCopy,overlayLayers:[]}};
const runAgent=async(_slot,system,prompt,[review])=>{
const data=JSON.parse(prompt);
assert.deepEqual(data.shownFieldOptions,[{field:'K02',label:'颜色名称'}]);
assert.deepEqual(data.composite.screenCopy,[{...screenCopy[0],screenFieldNumber:1}]);
assert.ok(data.screenCopy.every(row=>row.fieldNumber===undefined));
assert.match(system,/顶层 templateStyle/u);
// A dotted name reads as a literal key; round 28 reviewers sent "checks.templateStyle" at the top level.
assert.match(system,/checks 对象内的 templateStyle/u);
assert.doesNotMatch(system,/checks\.templateStyle/u);
assert.doesNotMatch(review.description,/checks\.templateStyle/u);
// Round 28: 88 of 90 candidates were sent back for unfinished neighbouring slots or a removable nameplate.
assert.match(system,/verdict 描述候选图,screenVerdict 描述整屏/u);
const result={verdict:'pass',reason:'颜色与文案相符',
screenVerdict:'needs_fix',screenSummary:'文案位置需调整',templateStyle:null,
copyFixes:[{field:'颜色名称',issue:'位置偏低',suggestion:'向上移动'}]};
for(const [change,error] of [[{reason:''},/reason:/u],[{verdict:undefined},/verdict:/u]]) {
assert.throws(()=>review.prepareArguments({...result,...change}),error);
}
assert.equal(review.prepareArguments({...result,unknownField:true,reasonDetail:'补充细节'}).verdict,'pass');
assert.match(review.prepareArguments({verdict:'pass',reasonDetail:'仅传 reasonDetail'}).reason,/仅传 reasonDetail/u);
assert.equal(review.prepareArguments({recommendedAction:'regenerate',reason:'结构不符'}).verdict,'revise');
assert.equal(review.prepareArguments({verdict:'pass',reason:'匹配实拍',checks:{color:true}}).checks.color,true);
assert.deepEqual(review.prepareArguments({verdict:'revise',reason:'标注偏移',screenReview:{imageIssues:[{nativeLayerId:12,location:'底部',issue:'偏移',suggestion:'移动'}]}}).overlayFixes,
[{nativeLayerId:12,location:'底部',issue:'偏移',suggestion:'移动'}]);
review.execute('',review.prepareArguments({...result,templateFidelity:{},shownFields:[{field:'颜色名称',location:'画面中央'},{field:'颜色名称 ',location:'底部'}]}));
};
const result=await reviewProductionCandidate({canvas,runAgent,slot,screen,candidateId:'photo',liveScreenCopy:screenCopy});
assert.equal(result.verdict,'pass');
assert.deepEqual(result.droppedReferences,[{path:'shownFields[1].field',value:'颜色名称 '}]);
assert.equal(result.observation.screenReview.templateStyle,null);
assert.equal(result.observation.screenReview.copyFixes[0].field,'K02');
assert.deepEqual(result.observation.visualEvidence,[{field:'K02',location:'画面中央'}]);
});
test('before any copy is written the review omits shown fields instead of rejecting screen field IDs',async()=>{
const slot={id:'cover',fieldId:'M01-I01',label:'主图'};
const pixels={type:'image',mimeType:'image/png',data:'fixture'};
// The screen still shows the template's sample text: nothing is live copy yet.
const screenCopy=[{field:'M01-T01',label:'产品名称',text:'模板标题',templateSample:true},{field:'M01-T02',label:'卖点',text:''}];
const canvas={detailImages:async()=>({candidateId:'photo',referenceCount:1,images:[pixels,pixels]})};
const screen={image:pixels,details:{page:0,screenFingerprint:'fixture',slots:[slot],screenCopy,overlayLayers:[]}};
const runAgent=async(_slot,system,prompt,[review])=>{
assert.deepEqual(JSON.parse(prompt).shownFieldOptions,[]);
assert.match(system,/省略 shownFields/u);
const args=review.prepareArguments({verdict:'pass',reason:'外形与实拍一致',screenVerdict:'pass',
shownFields:[{field:'M01-T01',location:'左上'},{field:'M01-T02',location:'右侧'}]});
review.execute('',args);
};
const result=await reviewProductionCandidate({canvas,runAgent,slot,screen,candidateId:'photo',liveScreenCopy:screenCopy});
assert.equal(result.verdict,'pass');
assert.deepEqual(result.observation.visualEvidence,[]);
});
test('template-base review includes the fixed slot image alongside the candidate',async()=>{
const slot={id:'slot',fieldId:'image',label:'商品图'};
const pixels=data=>({type:'image',mimeType:'image/png',data});
const [photo,base,candidate,composite]=['photo','base','candidate','composite'].map(pixels);
const canvas={detailImages:async()=>({candidateId:'candidate',referenceCount:1,layoutCount:1,layoutSource:'pinnedTemplateOriginal',images:[photo,base,candidate]})};
const screen={image:composite,details:{page:0,screenFingerprint:'fixture',slots:[slot],screenCopy:[],overlayLayers:[]}};
const runAgent=async(_slot,instructions,_prompt,[review],images,_scope,captions)=>{
assert.deepEqual(images,[candidate,composite,base,photo]);
assert.equal(captions[2],'固定模板图位原图');
assert.match(instructions,/其余图片按图注识别/u);
review.execute('',review.prepareArguments({verdict:'pass',reason:'本款与实拍一致',screenVerdict:'pass'}));
};
const result=await reviewProductionCandidate({canvas,runAgent,slot,screen,candidateId:'candidate',referenceMode:'template-base'});
assert.equal(result.verdict,'pass');
});