import { readFileSync, existsSync } from 'node:fs'; import path from 'node:path'; import { describe, expect, it } from 'vitest'; import { RAW_EXTENSIONS, decodeRaw, 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); }); }); 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); } });