|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 8 | +import { |
| 9 | + detectImageMimeType, |
| 10 | + validateAndCorrectMimeType, |
| 11 | +} from './imageMimeDetection.js'; |
| 12 | +import { debugLogger } from './debugLogger.js'; |
| 13 | + |
| 14 | +describe('imageMimeDetection', () => { |
| 15 | + // Base64 helper strings representing magic bytes of various formats |
| 16 | + const pngBase64 = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00]).toString('base64'); |
| 17 | + const webpBase64 = Buffer.from([ |
| 18 | + 0x52, 0x49, 0x46, 0x46, // RIFF |
| 19 | + 0x00, 0x00, 0x00, 0x00, // Size |
| 20 | + 0x57, 0x45, 0x42, 0x50, // WEBP |
| 21 | + 0x00, 0x00 |
| 22 | + ]).toString('base64'); |
| 23 | + const jpegBase64 = Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0x00, 0x00]).toString('base64'); |
| 24 | + const gifBase64 = Buffer.from([0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x00, 0x00]).toString('base64'); |
| 25 | + const txtBase64 = Buffer.from('Hello world!').toString('base64'); |
| 26 | + |
| 27 | + describe('detectImageMimeType', () => { |
| 28 | + it('should correctly detect PNG files', () => { |
| 29 | + expect(detectImageMimeType(pngBase64)).toBe('image/png'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should correctly detect WebP files', () => { |
| 33 | + expect(detectImageMimeType(webpBase64)).toBe('image/webp'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should correctly detect JPEG files', () => { |
| 37 | + expect(detectImageMimeType(jpegBase64)).toBe('image/jpeg'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should correctly detect GIF files', () => { |
| 41 | + expect(detectImageMimeType(gifBase64)).toBe('image/gif'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return null for non-image / unknown files', () => { |
| 45 | + expect(detectImageMimeType(txtBase64)).toBeNull(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return null for empty or invalid base64 data', () => { |
| 49 | + expect(detectImageMimeType('')).toBeNull(); |
| 50 | + expect(detectImageMimeType('abc')).toBeNull(); // too short to decode 4 bytes |
| 51 | + }); |
| 52 | + |
| 53 | + it('should handle whitespace/newlines in base64 data', () => { |
| 54 | + const formattedPng = pngBase64.match(/.{1,4}/g)?.join('\n') || pngBase64; |
| 55 | + expect(detectImageMimeType(formattedPng)).toBe('image/png'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should handle leading whitespace/newlines in base64 data', () => { |
| 59 | + expect(detectImageMimeType(' \n\r ' + pngBase64)).toBe('image/png'); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('validateAndCorrectMimeType', () => { |
| 64 | + let warnSpy: ReturnType<typeof vi.spyOn>; |
| 65 | + |
| 66 | + beforeEach(() => { |
| 67 | + warnSpy = vi.spyOn(debugLogger, 'warn').mockImplementation(() => {}); |
| 68 | + }); |
| 69 | + |
| 70 | + afterEach(() => { |
| 71 | + vi.restoreAllMocks(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should return original mime type if there is no mismatch', () => { |
| 75 | + expect(validateAndCorrectMimeType('image/png', pngBase64)).toBe('image/png'); |
| 76 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should return corrected mime type and log a warning if there is a mismatch', () => { |
| 80 | + // e.g. Figma WebP labeled as PNG |
| 81 | + expect(validateAndCorrectMimeType('image/png', webpBase64)).toBe('image/webp'); |
| 82 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 83 | + expect(warnSpy.mock.calls[0][0]).toContain( |
| 84 | + 'Image MIME type mismatch: declared as image/png but detected as image/webp' |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should fallback to declared mime type if image format is not detected', () => { |
| 89 | + expect(validateAndCorrectMimeType('application/octet-stream', txtBase64)).toBe( |
| 90 | + 'application/octet-stream' |
| 91 | + ); |
| 92 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should NOT correct mime type if the declared type is not an image or generic', () => { |
| 96 | + const textStartingWithGif = Buffer.from('GIF is an image format, not a text file').toString('base64'); |
| 97 | + expect(validateAndCorrectMimeType('text/plain', textStartingWithGif)).toBe('text/plain'); |
| 98 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments