import { readFileSync, existsSync } from 'node:fs'; import path from 'node:path'; import { describe, expect, it } from 'vitest'; import { RAW_EXTENSIONS, decodeRaw, extractEmbeddedJpegs, decodeStill, isRawName, isStillName, looksLikeRaw, rawEngine, } from './raw.mjs'; /** TIFF-family containers: CR2, NEF and DNG all start with the same header. */ function tiffHeader() { const buffer = Buffer.alloc(64); Buffer.from('49492a00', 'hex').copy(buffer, 0); return buffer; } function withAscii(bytes) { const buffer = Buffer.alloc(64); buffer.write(bytes, 0, 'latin1'); return buffer; } describe('raw format coverage', () => { it.each(['cr2', 'nef', 'cr3', 'dng'])('routes .%s to the raw decoder', (ext) => { expect(RAW_EXTENSIONS.has(ext)).toBe(true); expect(isRawName(`IMG_0001.${ext}`)).toBe(true); expect(isRawName(`IMG_0001.${ext.toUpperCase()}`)).toBe(true); expect(isStillName(`IMG_0001.${ext}`)).toBe(false); }); it.each(['tif', 'tiff'])('routes .%s to the still decoder', (ext) => { expect(isStillName(`scan.${ext}`)).toBe(true); expect(isStillName(`scan.${ext.toUpperCase()}`)).toBe(true); expect(isRawName(`scan.${ext}`)).toBe(false); }); it('recognises the container signatures of the main families', () => { expect(looksLikeRaw(tiffHeader())).toBe(true); // CR2, NEF, DNG const cr3 = Buffer.alloc(64); cr3.write('ftyp', 4, 'latin1'); expect(looksLikeRaw(cr3)).toBe(true); expect(looksLikeRaw(withAscii('IIRO'))).toBe(true); // ORF expect(looksLikeRaw(withAscii('IIU\0'))).toBe(true); // RW2 expect(looksLikeRaw(withAscii('FUJIFILMCCD-RAW'))).toBe(true); // RAF expect(looksLikeRaw(withAscii('FOVb'))).toBe(true); // X3F expect(looksLikeRaw(Buffer.alloc(64))).toBe(false); expect(looksLikeRaw(Buffer.alloc(8))).toBe(false); }); }); const JPEG_SOI = Buffer.from([0xff, 0xd8, 0xff]); const JPEG_EOI = Buffer.from([0xff, 0xd9]); /** A JPEG-shaped run of `total` bytes with no nested markers. */ function jpegBytes(total) { return Buffer.concat([JPEG_SOI, Buffer.alloc(Math.max(1, total - 5), 0x11), JPEG_EOI]); } describe('embedded preview fallback', () => { it('lists candidates largest first and ignores thumbnail-sized runs', () => { const small = jpegBytes(9000); const big = jpegBytes(90000); const tiny = jpegBytes(1200); const buffer = Buffer.concat([ Buffer.alloc(128), small, Buffer.alloc(32), big, Buffer.alloc(32), tiny, Buffer.alloc(32), ]); const lengths = extractEmbeddedJpegs(buffer).map((jpeg) => jpeg.length); // Splitting candidates at every end-of-image marker can yield over-long // slices that span two images; those fail to decode and are skipped. What // matters is that both real previews are offered, largest first, and that // thumbnail-sized runs are not. expect(lengths).toEqual([...lengths].sort((a, b) => b - a)); expect(lengths).toContain(big.length); expect(lengths).toContain(small.length); expect(lengths).not.toContain(tiny.length); }); it('does not let a nested EXIF thumbnail truncate the full preview', () => { // Regression: the scan used to stop at the thumbnail's end-of-image, so the // "preview" it stored was a cut-off JPEG that sharp refused to decode and // the import ended up as a 0x0 photo. const thumb = jpegBytes(6000); const outer = Buffer.concat([ JPEG_SOI, Buffer.alloc(2000, 0x11), thumb, Buffer.alloc(40000, 0x11), JPEG_EOI, ]); const found = extractEmbeddedJpegs(Buffer.concat([Buffer.alloc(64), outer])); expect(found[0].length).toBe(outer.length); }); }); describe('TIFF decoding', () => { it('turns a TIFF into a JPEG proxy', async () => { const { default: sharp } = await import('sharp'); const tiff = await sharp({ create: { width: 48, height: 32, channels: 3, background: { r: 20, g: 120, b: 210 }, }, }) .tiff() .toBuffer(); const decoded = await decodeStill(tiff); expect(decoded.engine).toBe('sharp'); expect(decoded.ext).toBe('jpg'); expect(decoded.width).toBe(48); expect(decoded.height).toBe(32); expect(decoded.jpeg.subarray(0, 2)).toEqual(Buffer.from([0xff, 0xd8])); }); it('rejects a file that is not an image', async () => { await expect(decodeStill(Buffer.from('not a tiff at all'))).rejects.toThrow( /could not decode/i, ); }); }); /** * Real containers are optional: point LUMINARY_RAW_FIXTURES at a directory * holding sample.cr2, sample.nef, sample.cr3 and sample.dng. The samples are * deliberately not committed (they are 10-40 MB each and camera-specific), but * when they are present this exercises the same libraw path an import takes. */ const FIXTURES = process.env.LUMINARY_RAW_FIXTURES; const fixture = (ext) => (FIXTURES ? path.join(FIXTURES, `sample.${ext}`) : ''); const available = (ext) => Boolean(FIXTURES) && existsSync(fixture(ext)); const rawFormats = ['cr2', 'nef', 'cr3', 'dng']; describe.skipIf(!FIXTURES)('real raw containers', () => { it('has libraw available', async () => { expect(await rawEngine()).toBe('ready'); }); for (const ext of rawFormats) { it.skipIf(!available(ext))(`decodes sample.${ext}`, async () => { const decoded = await decodeRaw(readFileSync(fixture(ext))); expect(decoded.jpeg.length).toBeGreaterThan(4096); expect(['libraw', 'embedded']).toContain(decoded.engine); expect(decoded.width).toBeGreaterThan(0); expect(decoded.height).toBeGreaterThan(0); }, 120000); } });