gongxiangp2/evals/xiaoyi-golden/tests/objective.test.mjs

192 lines
9.2 KiB
JavaScript

import assert from 'node:assert/strict';
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
import path from 'node:path';
import test from 'node:test';
import { scoreObjective } from '../src/objective-scorer.mjs';
import { fixture, sku1, sku2, unused, jpeg, png } from './fixture.mjs';
const codes = result => result.issues.map(issue => issue.code);
test('valid delivery checks real JPG and PNG dimensions, multiple sizes and same-size subfolders', async t => {
const run = await fixture(t);
const actual = await scoreObjective(run);
assert.equal(actual.status, 'passed', JSON.stringify(actual));
assert.equal(actual.layout.status, 'passed');
assert.equal(actual.fileCount, run.manifest.files.length + 1);
});
test('missing, corrupt or incomplete delivery manifest is a failure', async t => {
const run = await fixture(t);
run.manifest.allOutputsCompleted = false;
await run.save();
assert.ok(codes(await scoreObjective(run)).includes('incomplete-outputs'));
await writeFile(run.manifestPath, '{broken');
assert.deepEqual(codes(await scoreObjective(run)), ['manifest-json']);
await rm(run.manifestPath);
assert.deepEqual(codes(await scoreObjective(run)), ['manifest-missing']);
});
test('a complete flag does not hide missing or empty output artifacts', async t => {
const run = await fixture(t);
await rm(path.join(run.deliveryDir, 'PSD/01 原木/主图.psd'));
await writeFile(path.join(run.deliveryDir, '工程文件/原木长桌.xiaoyi'), '');
const result = await scoreObjective(run);
assert.equal(result.status, 'failed');
assert.deepEqual(new Set(codes(result)), new Set(['file-missing', 'file-size']));
});
test('detects SKU prefixes, extension case, and cross-binding of same-color product sizes', async t => {
const run = await fixture(t);
const change = async (before, after) => {
await rename(path.join(run.deliveryDir, before), path.join(run.deliveryDir, after));
run.manifest.files.find(record => record.relativePath === before).relativePath = after;
};
await change(sku1, sku1.replace('0001-Ab', 'SKU-0001-Ab'));
await change(sku2, sku2.replace('.jpg', '.JPG'));
run.manifest.files.find(record => record.relativePath.endsWith('8x6/0001-Ab.jpg')).specId = 'desk-2';
await run.save();
const result = await scoreObjective(run);
assert.equal(result.issues.filter(issue => issue.code === 'sku-filename').length, 2);
assert.ok(codes(result).includes('sku-binding'));
assert.ok(codes(result).includes('sku-contract-size'));
});
test('rejects incorrect resolution directories and duplicates outside numbered folders', async t => {
const run = await fixture(t);
const file = 'SKU/01 原木/8x9/0001-Ab.jpg';
await mkdir(path.dirname(path.join(run.deliveryDir, file)));
await rename(path.join(run.deliveryDir, sku1), path.join(run.deliveryDir, file));
run.manifest.files.find(record => record.relativePath === sku1).relativePath = file;
await run.save();
const result = await scoreObjective(run);
assert.ok(codes(result).includes('sku-folders'));
assert.ok(codes(result).includes('sku-duplicate-folders'));
});
test('confirms image signatures and width and height rather than trusting extensions or manifest', async t => {
const run = await fixture(t);
const record = run.manifest.files.find(item => item.relativePath === sku1);
await writeFile(path.join(run.deliveryDir, sku1), png);
record.sizeBytes = png.length;
run.manifest.files.find(item => item.relativePath.endsWith('M01.png')).height = 9;
const brokenPng = Buffer.from(png);
brokenPng.writeUInt32BE(128, 16);
await writeFile(path.join(run.deliveryDir, unused), brokenPng);
await run.save();
const result = await scoreObjective(run);
assert.equal(result.issues.filter(issue => issue.code === 'image-header').length, 2);
assert.ok(codes(result).includes('image-manifest-size'));
});
test('detects all three internal names, raw tables and hashes without marking ordinary pipe prose', async t => {
const run = await fixture(t);
run.evidence.visibleText.push('这是 Pi。', 'PhotoCraft 引擎', '历史 Codex 状态');
run.evidence.visibleText.push('成品 | 状态\n--- | ---\n主图 | 完成');
run.evidence.visibleText.push('| SKU 图 | 完成 |', '制作记录 ' + 'a'.repeat(64));
const filename = 'Codex-' + 'b'.repeat(64) + '.txt';
await writeFile(path.join(run.deliveryDir, filename), '备注');
const result = await scoreObjective(run);
assert.equal(result.issues.filter(issue => issue.code === 'white-label').length, 4);
assert.equal(result.issues.filter(issue => issue.code === 'markdown-table').length, 3);
assert.equal(result.issues.filter(issue => issue.code === 'visible-hash').length, 2);
assert.doesNotMatch(JSON.stringify(result), /[ab]{64}|Codex|PhotoCraft/);
});
test('detects candidates omitted from the delivery ledger and changes in archived pixels', async t => {
const run = await fixture(t);
run.manifest.candidateImages.pop();
run.manifest.candidateCount = 1;
run.manifest.unadoptedCandidateCount = 0;
const archive = run.manifest.files.find(record => record.relativePath === unused);
archive.relativePath = unused.replace('.png', '.jpg');
archive.format = 'jpg';
archive.sizeBytes = jpeg.length;
await rm(path.join(run.deliveryDir, unused));
await writeFile(path.join(run.deliveryDir, archive.relativePath), jpeg);
await run.save();
const result = await scoreObjective(run);
assert.ok(codes(result).includes('candidate-not-retained'));
assert.ok(codes(result).includes('candidate-digest'));
});
test('marks missing independent evidence and missing layout samples as unmeasured', async t => {
const run = await fixture(t);
delete run.evidence.candidateIds;
delete run.evidence.visibleText;
run.contract.sku.pop();
await rm(path.join(run.deliveryDir, sku2));
run.manifest.files = run.manifest.files.filter(record => record.relativePath !== sku2);
await run.save();
delete run.evidence.screens[0].contrastP50;
run.contract.layout.screenIds.push('S18');
const result = await scoreObjective(run);
assert.equal(result.status, 'failed');
assert.ok(codes(result).includes('output-images-missing'));
assert.deepEqual(result.missingEvidence.map(issue => issue.code), [
'sku-pair-missing', 'candidate-evidence-missing', 'visible-text-missing',
]);
assert.equal(result.layout.status, 'unmeasured');
assert.deepEqual(result.layout.issues.map(issue => issue.code), ['layout-screen-missing', 'layout-sample-missing']);
});
test('fails layout independently for clipping, contrast, white rectangles and low source resolution', async t => {
const run = await fixture(t);
run.evidence.screens[0] = {
id: 'S17', textClipping: [{ nativeLayerId: 20 }], contrastP10: 1, contrastP50: 3,
slotImageFacts: [{ nearWhiteFlatRatio: 0.42, resolutionRatio: 0.85 }],
};
const result = await scoreObjective(run);
assert.equal(result.status, 'passed');
assert.equal(result.layout.status, 'failed');
assert.equal(result.layout.issues.filter(issue => issue.code === 'layout-threshold').length, 5);
});
test('example plan passes the objective scoring entry point with its matching delivery', async t => {
const { workspace } = await fixture(t);
const plan = JSON.parse(await readFile(new URL('../cases/plan.example.json', import.meta.url), 'utf8'));
const [run] = plan.runs;
const result = await scoreObjective({ ...run, deliveryDir: path.resolve(workspace, run.deliveryDir) });
assert.equal(result.status, 'passed', JSON.stringify(result));
assert.equal(result.layout.status, 'passed');
});
test('accepts contract.images with varied slice prefixes and numeric suffixes without contract.detailSlices', async t => {
const run = await fixture(t);
const slices = [
'详情页切片/01 原木/slice-01.jpg',
'详情页切片/01 原木/屏-02.jpg',
'详情页切片/01 原木/详情页_03.png',
];
for (const relativePath of slices) {
const bytes = relativePath.endsWith('.png') ? png : jpeg;
await writeFile(path.join(run.deliveryDir, relativePath), bytes);
run.manifest.files.push({
relativePath, category: '详情页切片', format: path.extname(relativePath).slice(1),
sizeBytes: bytes.length, width: 8, height: 4, verified: true,
});
run.contract.images.push({ relativePath, width: 8, height: 4 });
}
await run.save();
assert.equal(run.contract.detailSlices, undefined);
const passed = await scoreObjective(run);
assert.equal(passed.status, 'passed', JSON.stringify(passed));
run.contract.images.push({ relativePath: '详情页切片/01 原木/slice-04.jpg', width: 8, height: 4 });
const missing = await scoreObjective(run);
assert.equal(missing.status, 'failed');
assert.ok(codes(missing).includes('image-contract-size'));
});
test('keeps legacy single-screen compatibility and marks missing evidence as unmeasured', async t => {
const run = await fixture(t);
const legacyResult = await scoreObjective(run);
assert.equal(legacyResult.status, 'passed');
assert.equal(legacyResult.layout.status, 'passed');
delete run.evidence.candidateIds;
const unmeasured = await scoreObjective(run);
assert.equal(unmeasured.status, 'unmeasured');
assert.deepEqual(unmeasured.issues, []);
assert.deepEqual(unmeasured.missingEvidence.map(issue => issue.code), ['candidate-evidence-missing']);
});