import {
  describe,
  expect,
  it
} from 'vitest';

import {
  dedupeImageAttachmentsForDisplay,
  hasImageFilename,
  isImageAttachment,
  isInlineDisplayableImage,
  resolvePublishAttachmentIsImage
} from './attachment-image.rules';

describe('attachment-image rules', () => {
  it('detects images from mime, flag, or filename extension', () => {
    expect(isImageAttachment({
      id: '1',
      filename: 'logo.PNG',
      mime: 'application/octet-stream',
      isImage: false,
      available: false
    })).toBe(true);

    expect(hasImageFilename('photo.jpeg')).toBe(true);
    expect(resolvePublishAttachmentIsImage({ name: 'photo.png', type: '' })).toBe(true);
  });

  it('treats file protocol urls as not inline displayable', () => {
    expect(isInlineDisplayableImage({
      available: true,
      objectUrl: 'file:///tmp/photo.png'
    })).toBe(false);

    expect(isInlineDisplayableImage({
      available: true,
      objectUrl: 'blob:http://localhost/abc'
    })).toBe(true);
  });

  it('dedupes image attachments by filename and prefers displayable copies', () => {
    const deduped = dedupeImageAttachmentsForDisplay([
      {
        id: 'a',
        filename: 'photo.png',
        mime: 'image/png',
        isImage: true,
        available: false
      },
      {
        id: 'b',
        filename: 'photo.png',
        mime: 'application/octet-stream',
        isImage: false,
        available: true,
        objectUrl: 'blob:http://localhost/photo'
      }
    ]);

    expect(deduped).toHaveLength(1);
    expect(deduped[0]?.id).toBe('b');
  });
});
