import { describe, expect, it } from 'vitest';
import { sniffImage } from './imageSniff';

const pad = (head: number[]) => Buffer.concat([Buffer.from(head), Buffer.alloc(16)]);

describe('sniffImage', () => {
  it('recognises the common camera/phone formats by magic bytes', () => {
    expect(sniffImage(pad([0xff, 0xd8, 0xff, 0xe0]))).toBe('jpeg');
    expect(sniffImage(pad([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))).toBe('png');
    expect(sniffImage(Buffer.from('RIFF____WEBPVP8 ....', 'latin1'))).toBe('webp');
    expect(sniffImage(Buffer.from('....ftypavif............', 'latin1'))).toBe('avif');
    expect(sniffImage(Buffer.from('....ftypheic............', 'latin1'))).toBe('heic');
    expect(sniffImage(pad([0x47, 0x49, 0x46, 0x38, 0x39, 0x61]))).toBe('gif');
  });

  it('rejects non-images and short buffers', () => {
    expect(sniffImage(Buffer.from('this is just some text, not an image'))).toBeNull();
    expect(sniffImage(Buffer.from([0x00, 0x01, 0x02]))).toBeNull();
    expect(sniffImage(Buffer.from('RIFF____AVI ', 'latin1'))).toBeNull(); // RIFF but not WEBP
  });
});
