import { describe, expect, it } from 'vitest'; import { dedupeImageAttachmentsForDisplay, hasImageFilename, isAttachmentPendingInlineHydration, 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('detects images waiting for on-demand blob hydration', () => { expect(isAttachmentPendingInlineHydration({ id: '1', filename: 'photo.png', mime: 'image/png', isImage: true, available: false, savedPath: '/appdata/photo.png' })).toBe(true); expect(isAttachmentPendingInlineHydration({ id: '2', filename: 'photo.png', mime: 'image/png', isImage: true, available: true, objectUrl: 'blob:http://localhost/photo', savedPath: '/appdata/photo.png' })).toBe(false); }); 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'); }); });