import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import test from 'node:test'; const imgDir = path.resolve('public/img'); const maxWebpBytes = 150 * 1024; const maxOgBytes = 300 * 1024; const expectedWebpFiles = [ 'volver-a-ti-hero.webp', 'volver-a-ti-proceso-inicial.webp', 'volver-a-ti-practica-personal.webp', 'volver-a-ti-diario.webp', 'volver-a-ti-recursos.webp', ]; const ogFile = 'og-kundalini.jpg'; function assertAssetExists(fileName) { const filePath = path.join(imgDir, fileName); assert.ok(fs.existsSync(filePath), `missing expected asset file: public/img/${fileName}`); assert.ok( fs.statSync(filePath).isFile(), `expected asset must be a regular file: public/img/${fileName}`, ); return filePath; } // Why: dimensions are parsed directly from the image bytes instead of shelling out to // ImageMagick/ffmpeg, which may not be installed in CI. Tests enforce sane explicit bounds; // exact per-asset dimensions and hero/below-the-fold roles are recorded in the issue // completion comment, not duplicated here. function parseWebpDimensions(buffer) { let offset = 12; // skip the 12-byte RIFF/WEBP file header while (offset + 8 <= buffer.length) { const chunk = buffer.toString('ascii', offset, offset + 4); const size = buffer.readUInt32LE(offset + 4); const payloadOffset = offset + 8; if (payloadOffset + size > buffer.length) { break; } if (chunk === 'VP8X' && size >= 10) { return { width: 1 + (buffer[payloadOffset + 4] | (buffer[payloadOffset + 5] << 8) | (buffer[payloadOffset + 6] << 16)), height: 1 + (buffer[payloadOffset + 7] | (buffer[payloadOffset + 8] << 8) | (buffer[payloadOffset + 9] << 16)), }; } if ( chunk === 'VP8 ' && size >= 10 && buffer[payloadOffset + 3] === 0x9d && buffer[payloadOffset + 4] === 0x01 && buffer[payloadOffset + 5] === 0x2a ) { return { width: buffer.readUInt16LE(payloadOffset + 6) & 0x3fff, height: buffer.readUInt16LE(payloadOffset + 8) & 0x3fff, }; } if (chunk === 'VP8L' && size >= 5 && buffer[payloadOffset] === 0x2f) { const packed = buffer.readUInt32LE(payloadOffset + 1); return { width: (packed & 0x3fff) + 1, height: ((packed >>> 14) & 0x3fff) + 1, }; } // Chunks are padded to an even byte boundary in RIFF containers. offset = payloadOffset + size + (size % 2); } return null; } function parseJpegDimensions(buffer) { let offset = 2; // skip SOI (FFD8) while (offset + 1 < buffer.length) { if (buffer[offset] !== 0xff) { offset += 1; continue; } // Skip repeated 0xFF fill bytes before the marker code. while (offset < buffer.length && buffer[offset] === 0xff) { offset += 1; } if (offset >= buffer.length) { break; } const marker = buffer[offset]; offset += 1; // TEM, RST0-RST7, SOI and EOI are standalone markers without a length field. if (marker === 0x00 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd9)) { continue; } // SOS is followed by entropy-coded data, so segment scanning ends here. if (marker === 0xda) { break; } if (offset + 2 > buffer.length) { break; } const segmentLength = buffer.readUInt16BE(offset); if (segmentLength < 2 || offset + segmentLength > buffer.length) { break; } const isSof = marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker); if (isSof && segmentLength >= 7) { return { width: buffer.readUInt16BE(offset + 5), height: buffer.readUInt16BE(offset + 3), }; } offset += segmentLength; } return null; } test('Issue #4 Acceptance Criteria: Phase 0 media assets', async (t) => { await t.test( 'Criterion 2: all five volver-a-ti WebP files exist with deterministic names', () => { assert.ok( fs.existsSync(imgDir) && fs.statSync(imgDir).isDirectory(), 'missing expected asset directory: public/img', ); for (const fileName of expectedWebpFiles) { assertAssetExists(fileName); } const actualWebpFiles = fs .readdirSync(imgDir) .filter((fileName) => /^volver-a-ti-.*\.webp$/.test(fileName)) .sort(); assert.deepEqual( actualWebpFiles, [...expectedWebpFiles].sort(), 'public/img must contain exactly the five expected volver-a-ti-*.webp files (hero plus four sections) with no unexpected extras', ); }, ); await t.test( 'Criterion 2: each WebP has a valid RIFF/WEBP header, stays under 150 KB, and decodes to sane dimensions', () => { for (const fileName of expectedWebpFiles) { const buffer = fs.readFileSync(assertAssetExists(fileName)); assert.ok( buffer.length >= 12, `${fileName} must be at least 12 bytes for a RIFF/WEBP header`, ); assert.equal( buffer.toString('ascii', 0, 4), 'RIFF', `${fileName} must begin with the RIFF signature`, ); assert.equal( buffer.toString('ascii', 8, 12), 'WEBP', `${fileName} must declare the WEBP form type`, ); assert.ok( buffer.length <= maxWebpBytes, `${fileName} must be <= ${maxWebpBytes} bytes, found ${buffer.length}`, ); const dimensions = parseWebpDimensions(buffer); assert.ok( dimensions, `${fileName} must contain a decodable VP8X, VP8 lossy, or VP8L dimension chunk`, ); assert.ok( dimensions.width >= 600 && dimensions.width <= 2000, `${fileName} width must be between 600 and 2000 px, found ${dimensions.width}`, ); assert.ok( dimensions.height >= 300 && dimensions.height <= 2000, `${fileName} height must be between 300 and 2000 px, found ${dimensions.height}`, ); } }, ); await t.test( 'Criterion 3: og-kundalini.jpg has a valid JPEG SOI, stays under 300 KB, and is exactly 1200x630', () => { const buffer = fs.readFileSync(assertAssetExists(ogFile)); assert.ok(buffer.length >= 4, `${ogFile} must be at least 4 bytes for a JPEG SOI marker`); assert.equal(buffer[0], 0xff, `${ogFile} must begin with JPEG SOI marker FF`); assert.equal(buffer[1], 0xd8, `${ogFile} must begin with JPEG SOI marker D8`); assert.ok( buffer.length <= maxOgBytes, `${ogFile} must be <= ${maxOgBytes} bytes, found ${buffer.length}`, ); const dimensions = parseJpegDimensions(buffer); assert.ok(dimensions, `${ogFile} must contain a decodable SOF0-SOF15 dimension segment`); assert.equal( dimensions.width, 1200, `${ogFile} width must be exactly 1200 px, found ${dimensions.width}`, ); assert.equal( dimensions.height, 630, `${ogFile} height must be exactly 630 px, found ${dimensions.height}`, ); }, ); });