Forked from vadimfedenko/analyze-images
Forked from vadimfedenko/analyze-images
scripts / image-compatibility-smoke.cjs
scripts / image-compatibility-smoke.cjs
const assert = require('node:assert/strict');
const sharp = require('sharp');
const { compatibleImage } = require('../dist/compatibleImage');
const { inlineImage } = require('../dist/inlineImage');
const { IMAGE_LIMIT } = require('../dist/attachmentIdentity');
(async () => {
const client = { files: {} };
const source = {create: {width: 64, height: 48, channels: 3, background: '#e02020'}};
const webp = await sharp(source).webp().toBuffer();
const png = await sharp(source).png().toBuffer();
const jpg = await sharp(source).jpeg().toBuffer();
const file = await compatibleImage(client, 'aia-source.webp', webp);
const decoded = Buffer.from(file.identifier.slice(7), 'base64');
assert(file.name.endsWith('.png'));
const metadata = await sharp(decoded).metadata();
assert.equal(metadata.format, 'png');
assert.equal(metadata.width, 64); assert.equal(metadata.height, 48);
await sharp(decoded).raw().toBuffer(); // Full decode, not just the PNG signature.
const untouched = await compatibleImage(client, 'native.png', png);
assert.deepEqual(Buffer.from(untouched.identifier.slice(7), 'base64'), png);
const jpeg = await compatibleImage(client, 'native.jpeg', jpg);
assert.deepEqual(Buffer.from(jpeg.identifier.slice(7), 'base64'), jpg);
const mislabeled = await compatibleImage(client, 'actually-webp.jpg', webp);
assert(mislabeled.name.endsWith('.png'));
await assert.rejects(() => compatibleImage(client, 'bad.webp', Buffer.from('<html>not an image</html>')));
await assert.rejects(() => compatibleImage(client, 'huge.webp', Buffer.alloc(IMAGE_LIMIT + 1)), /size limit/);
await assert.rejects(() => compatibleImage(client, 'empty.webp', Buffer.alloc(0)), /size limit/);
console.log('PASS: real WebP decode to full-size PNG, native JPEG/PNG byte preservation, byte-based format detection, malformed/empty/oversized input rejection.');
if (process.argv.includes('--live')) {
const model = 'google/gemma-4-31b-qat';
const paths = [
['original WebP', inlineImage(client, 'original.webp', webp), 'image/webp'],
['normalized PNG', file, 'image/png'],
];
for (const [label, handle, mime] of paths) {
const response = await fetch('http://127.0.0.1:1234/v1/chat/completions', {
method: 'POST', headers: {'Content-Type': 'application/json'},
signal: AbortSignal.timeout(120_000),
body: JSON.stringify({model, stream: false, max_tokens: 8, messages: [{role: 'user', content: [
{type: 'text', text: 'Name the main color in this image.'},
{type: 'image_url', image_url: {url: `data:${mime};base64,${handle.identifier.slice(7)}`}},
]}]}),
});
const body = await response.json();
const errorText = typeof body.error === 'string' ? body.error : body.error?.message;
console.log(`${label}: HTTP ${response.status}${errorText ? '; ' + errorText : response.ok ? '; prediction accepted' : '; media request rejected'}`);
if (label === 'normalized PNG') assert(response.ok, JSON.stringify(body.error));
}
}
})().catch(error => {console.error(error); process.exitCode = 1;});
const assert = require('node:assert/strict');
const sharp = require('sharp');
const { compatibleImage } = require('../dist/compatibleImage');
const { inlineImage } = require('../dist/inlineImage');
const { IMAGE_LIMIT } = require('../dist/attachmentIdentity');
(async () => {
const client = { files: {} };
const source = {create: {width: 64, height: 48, channels: 3, background: '#e02020'}};
const webp = await sharp(source).webp().toBuffer();
const png = await sharp(source).png().toBuffer();
const jpg = await sharp(source).jpeg().toBuffer();
const file = await compatibleImage(client, 'aia-source.webp', webp);
const decoded = Buffer.from(file.identifier.slice(7), 'base64');
assert(file.name.endsWith('.png'));
const metadata = await sharp(decoded).metadata();
assert.equal(metadata.format, 'png');
assert.equal(metadata.width, 64); assert.equal(metadata.height, 48);
await sharp(decoded).raw().toBuffer(); // Full decode, not just the PNG signature.
const untouched = await compatibleImage(client, 'native.png', png);
assert.deepEqual(Buffer.from(untouched.identifier.slice(7), 'base64'), png);
const jpeg = await compatibleImage(client, 'native.jpeg', jpg);
assert.deepEqual(Buffer.from(jpeg.identifier.slice(7), 'base64'), jpg);
const mislabeled = await compatibleImage(client, 'actually-webp.jpg', webp);
assert(mislabeled.name.endsWith('.png'));
await assert.rejects(() => compatibleImage(client, 'bad.webp', Buffer.from('<html>not an image</html>')));
await assert.rejects(() => compatibleImage(client, 'huge.webp', Buffer.alloc(IMAGE_LIMIT + 1)), /size limit/);
await assert.rejects(() => compatibleImage(client, 'empty.webp', Buffer.alloc(0)), /size limit/);
console.log('PASS: real WebP decode to full-size PNG, native JPEG/PNG byte preservation, byte-based format detection, malformed/empty/oversized input rejection.');
if (process.argv.includes('--live')) {
const model = 'google/gemma-4-31b-qat';
const paths = [
['original WebP', inlineImage(client, 'original.webp', webp), 'image/webp'],
['normalized PNG', file, 'image/png'],
];
for (const [label, handle, mime] of paths) {
const response = await fetch('http://127.0.0.1:1234/v1/chat/completions', {
method: 'POST', headers: {'Content-Type': 'application/json'},
signal: AbortSignal.timeout(120_000),
body: JSON.stringify({model, stream: false, max_tokens: 8, messages: [{role: 'user', content: [
{type: 'text', text: 'Name the main color in this image.'},
{type: 'image_url', image_url: {url: `data:${mime};base64,${handle.identifier.slice(7)}`}},
]}]}),
});
const body = await response.json();
const errorText = typeof body.error === 'string' ? body.error : body.error?.message;
console.log(`${label}: HTTP ${response.status}${errorText ? '; ' + errorText : response.ok ? '; prediction accepted' : '; media request rejected'}`);
if (label === 'normalized PNG') assert(response.ok, JSON.stringify(body.error));
}
}
})().catch(error => {console.error(error); process.exitCode = 1;});