Files
videoreg.ru/scripts/rebuild-v2/check-raster-sharp.mjs
T

37 lines
2.9 KiB
JavaScript

/** Real positive and truncated-image failure behavior, using only temp fixtures. */
import assert from 'node:assert/strict';
import { mkdtemp, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import sharp from 'sharp';
import { decodeBatch } from './decode-raster-sharp.mjs';
const root = await mkdtemp(join(tmpdir(), 'videoreg-v2-raster-fixture-'));
try {
const good = await sharp({ create: { width: 64, height: 32, channels: 3, background: '#17382f' } }).png().toBuffer();
await writeFile(join(root, 'good.png'), good);
await writeFile(join(root, 'truncated.png'), good.subarray(0, 32));
const report = await decodeBatch({ root, files: ['good.png', 'truncated.png'] });
assert.equal(report.results[0].valid, true, 'Valid PNG must decode all pixels');
assert.equal(report.results[0].width, 64);
assert.equal(report.results[0].height, 32);
assert.equal(report.results[1].valid, false, 'Truncated PNG must fail, not be skipped or treated as metadata-only pass');
const frames = await Promise.all([16, 32, 48].map(size => sharp({ create: { width: size, height: size, channels: 4, background: '#17382f' } }).png().toBuffer()));
const directory = Buffer.alloc(6 + frames.length * 16); directory.writeUInt16LE(1, 2); directory.writeUInt16LE(frames.length, 4);
let offset = directory.length;
frames.forEach((frame, i) => { const at = 6 + i * 16; directory[at] = [16,32,48][i]; directory[at+1] = [16,32,48][i]; directory.writeUInt16LE(1,at+4);directory.writeUInt16LE(32,at+6);directory.writeUInt32LE(frame.length,at+8);directory.writeUInt32LE(offset,at+12);offset += frame.length; });
const ico = Buffer.concat([directory,...frames]);
await writeFile(join(root,'all-frames.ico'),ico);
await writeFile(join(root,'truncated-directory.ico'),ico.subarray(0,24));
const bounds=Buffer.from(ico);bounds.writeUInt32LE(0xffffffff,18);await writeFile(join(root,'bad-bounds.ico'),bounds);
const overlap=Buffer.from(ico);overlap.writeUInt32LE(directory.length,34);await writeFile(join(root,'overlap.ico'),overlap);
const corrupt=Buffer.from(ico);corrupt[directory.length+frames[0].length]=0;await writeFile(join(root,'bad-middle-frame.ico'),corrupt);
const icons=await decodeBatch({root,files:['all-frames.ico','truncated-directory.ico','bad-bounds.ico','overlap.ico','bad-middle-frame.ico']});
assert.equal(icons.results[0].valid,true);assert.deepEqual(icons.results[0].frames.map(f=>[f.width,f.height]),[[16,16],[32,32],[48,48]]);
assert.ok(icons.results.slice(1).every(f=>f.valid===false),'Every corrupt directory/member must fail the full ICO');
process.stdout.write(JSON.stringify({test:'all-ICO-frames-and-corruption',status:'passed',results:icons.results})+'\n');
process.stdout.write(JSON.stringify({ test: 'full-raster-decode-positive-and-corruption', status: 'passed', results: report.results }) + '\n');
} finally {
await rm(root, { recursive: true, force: true });
}