gongxiangp2/apps/detail-agent-pi/test/verify-delivery-bundle.test.mjs

138 lines
6.7 KiB
JavaScript

import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { createHash } from 'node:crypto';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { verifyDeliveryBundle } from '../scripts/verify-delivery-bundle.mjs';
async function fixture(t, { fakeHeader = false, compact = false } = {}) {
const temporary = await fs.mkdtemp(path.join(os.tmpdir(), 'xiaoyi-delivery-'));
t.after(() => fs.rm(temporary, { recursive: true, force: true }));
const root = path.join(temporary, 'bundle');
const files = [];
const counts = {};
const snapshot = 'e5ccd063-851d-449a-a731-f31571be73c0';
const dimensions = { width: 1, height: 1 };
async function add(relativePath, category, bytes) {
const destination = path.join(root, relativePath);
await fs.mkdir(path.dirname(destination), { recursive: true });
await fs.writeFile(destination, bytes);
counts[category] = (counts[category] ?? 0) + 1;
files.push({ relativePath, category, format: path.extname(destination).slice(1), ...dimensions,
sizeBytes: Buffer.byteLength(bytes), sha256: createHash('sha256').update(bytes).digest('hex'), verified: true });
}
await fs.writeFile(path.join(temporary, 'manifest.json'), JSON.stringify({ size: dimensions }));
execFileSync('tar', ['-a', '-cf', 'fixture.zip', 'manifest.json'], { cwd: temporary });
const zip = await fs.readFile(path.join(temporary, 'fixture.zip'));
const checkpoint = `工程文件/项目 资源/${snapshot}`;
const project = {
version: 2, resources: '项目 资源', snapshot, projectManifest: { included: ['canvas.pcraft'] },
};
if (compact) {
const context = `${checkpoint}/state-0001.json`;
project.projectManifest.included.push('context.json');
project.resourcePaths = { 'canvas.pcraft': '工程文件/主图.pcraft', 'context.json': context };
await add(context, '工程资源', '{}');
} else {
await add(`${checkpoint}/canvas.pcraft`, '工程资源', zip);
}
await add('工程文件/项目.xiaoyi', '工程文件', JSON.stringify(project));
await add('工程文件/主图.pcraft', '工程文件', zip);
const psd = Buffer.alloc(26);
psd.write('8BPS');
psd.writeUInt16BE(2, 4);
psd.writeUInt32BE(1, 14);
psd.writeUInt32BE(1, 18);
await add('PSD/主图.psb', 'PSD', psd);
const jpeg = Buffer.from('ffd8ffc0000b080001000101011100ffd9', 'hex');
if (fakeHeader) jpeg[0] = 0;
await add('主图/主图-01.jpg', '主图', jpeg);
await fs.writeFile(path.join(root, '交付清单.json'), JSON.stringify({
version: 1, deliveryId: snapshot, totalOutputs: 1, completedOutputs: 1,
allOutputsCompleted: true, counts, files,
}));
return root;
}
test('verifies the selected output and reports JPG dimensions', async t => {
const root = await fixture(t);
const report = await verifyDeliveryBundle(root, ['main_images']);
assert.equal(report.ok, true, JSON.stringify(report.errors));
assert.deepEqual(report.files.at(-1), { relativePath: '主图/主图-01.jpg', sizeBytes: 17, width: 1, height: 1 });
});
test('verifies a project whose canvas is the published PCraft', async t => {
const report = await verifyDeliveryBundle(await fixture(t, { compact: true }), ['main_images']);
assert.equal(report.ok, true, JSON.stringify(report.errors));
assert.equal(report.files.filter(row => row.relativePath.endsWith('.pcraft')).length, 1);
});
test('reports files missing from disk', async t => {
const root = await fixture(t);
await fs.unlink(path.join(root, 'PSD/主图.psb'));
const report = await verifyDeliveryBundle(root);
assert.equal(report.ok, false);
assert.equal(report.missingCount, 1);
});
test('rejects a fake JPG header even when the digest matches', async t => {
const report = await verifyDeliveryBundle(await fixture(t, { fakeHeader: true }));
assert.equal(report.ok, false);
assert.match(report.errors[0].message, /JPG 文件头/);
});
test('reports incomplete outputs alongside verified deliverables', async t => {
const root = await fixture(t);
const manifestPath = path.join(root, '交付清单.json');
const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8'));
const missing = [{ outputId: 'sku_images', variantName: '米白', reason: '尚未导出' }];
await fs.writeFile(manifestPath, JSON.stringify({
...manifest, totalOutputs: 2, allOutputsCompleted: false, missingOutputs: 1, missing,
}));
const report = await verifyDeliveryBundle(root, ['main_images', 'sku_images']);
assert.equal(report.ok, false);
assert.equal(report.failedCount, 0);
assert.equal(report.missingCount, 1);
assert.deepEqual(report.missing, missing);
assert.equal(report.files.length, manifest.files.length);
});
test('reports unlisted files in the bundle root and project resources', async t => {
const root = await fixture(t);
await fs.writeFile(path.join(root, 'debug.json'), '{}');
await fs.writeFile(path.join(root, '工程文件', '项目 资源', 'preview.png'), 'preview');
const report = await verifyDeliveryBundle(root);
assert.equal(report.ok, false);
assert.deepEqual(report.errors.map(error => error.path).sort(), ['debug.json', '工程文件/项目 资源/preview.png']);
});
test('checks disk path lengths and duplicate large files using verified digests', async t => {
const root = await fixture(t);
const manifestPath = path.join(root, '交付清单.json');
const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8'));
const bytes = Buffer.alloc(10 * 1024 * 1024, 42);
const sha256 = createHash('sha256').update(bytes).digest('hex');
const names = ['x'.repeat(112) + '.png', 'duplicate.png'];
for (const name of names) {
const relativePath = '采用图/' + name;
await fs.mkdir(path.join(root, '采用图'), { recursive: true });
await fs.writeFile(path.join(root, relativePath), bytes);
manifest.files.push({ relativePath, category: '采用图', format: 'png',
sizeBytes: bytes.length, sha256, verified: true });
}
manifest.counts['采用图'] = names.length;
await fs.writeFile(manifestPath, JSON.stringify(manifest));
const report = await verifyDeliveryBundle(root);
assert.equal(report.ok, false);
assert.equal(report.longestRelativePath, '采用图/' + names[0]);
assert.equal(report.longestRelativePathLength, 120);
assert.deepEqual(report.duplicateLargeFiles, [names.map(name => '采用图/' + name)]);
assert.equal(report.failedCount, 1);
await fs.writeFile(path.join(root, '采用图', 'y'.repeat(113) + '.png'), 'x');
const tooLong = await verifyDeliveryBundle(root);
assert.equal(tooLong.longestRelativePathLength, 121);
assert.ok(tooLong.errors.some(error => error.message === '相对路径超过 120 字符'));
});