|
| 1 | +import { describe, test, expect, beforeEach, afterEach } from 'bun:test' |
| 2 | +import { writeFileSync, unlinkSync, mkdirSync, rmSync } from 'fs' |
| 3 | +import path from 'path' |
| 4 | +import { |
| 5 | + processImageFile, |
| 6 | + isImageFile, |
| 7 | + extractImagePaths, |
| 8 | +} from '../utils/image-handler' |
| 9 | + |
| 10 | +const TEST_DIR = path.join(__dirname, 'temp-test-images') |
| 11 | +const TEST_IMAGE_PATH = path.join(TEST_DIR, 'test-image.png') |
| 12 | +const TEST_LARGE_IMAGE_PATH = path.join(TEST_DIR, 'large-image.jpg') |
| 13 | + |
| 14 | +// Create a minimal PNG file (43 bytes) |
| 15 | +const MINIMAL_PNG = Buffer.from([ |
| 16 | + 0x89, |
| 17 | + 0x50, |
| 18 | + 0x4e, |
| 19 | + 0x47, |
| 20 | + 0x0d, |
| 21 | + 0x0a, |
| 22 | + 0x1a, |
| 23 | + 0x0a, // PNG signature |
| 24 | + 0x00, |
| 25 | + 0x00, |
| 26 | + 0x00, |
| 27 | + 0x0d, // IHDR chunk length |
| 28 | + 0x49, |
| 29 | + 0x48, |
| 30 | + 0x44, |
| 31 | + 0x52, // IHDR |
| 32 | + 0x00, |
| 33 | + 0x00, |
| 34 | + 0x00, |
| 35 | + 0x01, // width: 1 |
| 36 | + 0x00, |
| 37 | + 0x00, |
| 38 | + 0x00, |
| 39 | + 0x01, // height: 1 |
| 40 | + 0x08, |
| 41 | + 0x02, |
| 42 | + 0x00, |
| 43 | + 0x00, |
| 44 | + 0x00, // bit depth, color type, compression, filter, interlace |
| 45 | + 0x90, |
| 46 | + 0x77, |
| 47 | + 0x53, |
| 48 | + 0xde, // CRC |
| 49 | + 0x00, |
| 50 | + 0x00, |
| 51 | + 0x00, |
| 52 | + 0x00, // IEND chunk length |
| 53 | + 0x49, |
| 54 | + 0x45, |
| 55 | + 0x4e, |
| 56 | + 0x44, // IEND |
| 57 | + 0xae, |
| 58 | + 0x42, |
| 59 | + 0x60, |
| 60 | + 0x82, // CRC |
| 61 | +]) |
| 62 | + |
| 63 | +beforeEach(() => { |
| 64 | + mkdirSync(TEST_DIR, { recursive: true }) |
| 65 | + writeFileSync(TEST_IMAGE_PATH, MINIMAL_PNG) |
| 66 | + |
| 67 | + // Create a large fake image (10MB) |
| 68 | + const largeBuffer = Buffer.alloc(10 * 1024 * 1024, 0xff) |
| 69 | + // Add minimal JPEG header |
| 70 | + largeBuffer.writeUInt16BE(0xffd8, 0) // JPEG SOI marker |
| 71 | + largeBuffer.writeUInt16BE(0xffd9, largeBuffer.length - 2) // JPEG EOI marker |
| 72 | + writeFileSync(TEST_LARGE_IMAGE_PATH, largeBuffer) |
| 73 | +}) |
| 74 | + |
| 75 | +afterEach(() => { |
| 76 | + try { |
| 77 | + rmSync(TEST_DIR, { recursive: true, force: true }) |
| 78 | + } catch { |
| 79 | + // Ignore cleanup errors |
| 80 | + } |
| 81 | +}) |
| 82 | + |
| 83 | +describe('Image Upload Functionality', () => { |
| 84 | + describe('isImageFile', () => { |
| 85 | + test('should detect valid image extensions', () => { |
| 86 | + expect(isImageFile('test.jpg')).toBe(true) |
| 87 | + expect(isImageFile('test.jpeg')).toBe(true) |
| 88 | + expect(isImageFile('test.png')).toBe(true) |
| 89 | + expect(isImageFile('test.webp')).toBe(true) |
| 90 | + expect(isImageFile('test.gif')).toBe(true) |
| 91 | + expect(isImageFile('test.bmp')).toBe(true) |
| 92 | + expect(isImageFile('test.tiff')).toBe(true) |
| 93 | + }) |
| 94 | + |
| 95 | + test('should reject non-image extensions', () => { |
| 96 | + expect(isImageFile('test.txt')).toBe(false) |
| 97 | + expect(isImageFile('test.js')).toBe(false) |
| 98 | + expect(isImageFile('test.pdf')).toBe(false) |
| 99 | + expect(isImageFile('test')).toBe(false) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + describe('extractImagePaths', () => { |
| 104 | + test('should extract image paths from text with @ syntax', () => { |
| 105 | + const input = 'Look at this @test.png and @image.jpg files' |
| 106 | + const paths = extractImagePaths(input) |
| 107 | + expect(paths).toEqual(['test.png', 'image.jpg']) |
| 108 | + }) |
| 109 | + |
| 110 | + test('should ignore non-image paths', () => { |
| 111 | + const input = 'Check @script.js and @test.png' |
| 112 | + const paths = extractImagePaths(input) |
| 113 | + expect(paths).toEqual(['test.png']) |
| 114 | + }) |
| 115 | + |
| 116 | + test('should return empty array when no image paths found', () => { |
| 117 | + const input = 'No images here @script.js @readme.txt' |
| 118 | + const paths = extractImagePaths(input) |
| 119 | + expect(paths).toEqual([]) |
| 120 | + }) |
| 121 | + |
| 122 | + test('should auto-detect absolute paths', () => { |
| 123 | + const input = 'Look at /path/to/image.png and ~/screenshots/photo.jpg' |
| 124 | + const paths = extractImagePaths(input) |
| 125 | + expect(paths).toEqual(['/path/to/image.png', '~/screenshots/photo.jpg']) |
| 126 | + }) |
| 127 | + |
| 128 | + test('should auto-detect relative paths with separators', () => { |
| 129 | + const input = 'Check ./assets/logo.png and ../images/banner.jpg' |
| 130 | + const paths = extractImagePaths(input) |
| 131 | + expect(paths).toEqual(['./assets/logo.png', '../images/banner.jpg']) |
| 132 | + }) |
| 133 | + |
| 134 | + test('should auto-detect quoted paths', () => { |
| 135 | + const input = |
| 136 | + 'Files: "./my folder/image.png" and \'../photos/vacation.jpg\'' |
| 137 | + const paths = extractImagePaths(input) |
| 138 | + expect(paths).toEqual(['./my folder/image.png', '../photos/vacation.jpg']) |
| 139 | + }) |
| 140 | + |
| 141 | + test('should ignore paths in code blocks', () => { |
| 142 | + const input = |
| 143 | + 'See ```./test.png``` and `inline.jpg` but process ./real.png' |
| 144 | + const paths = extractImagePaths(input) |
| 145 | + expect(paths).toEqual(['./real.png']) |
| 146 | + }) |
| 147 | + |
| 148 | + test('should remove trailing punctuation from auto-detected paths', () => { |
| 149 | + const input = 'Look at /path/image.png, and ./other.jpg!' |
| 150 | + const paths = extractImagePaths(input) |
| 151 | + expect(paths).toEqual(['/path/image.png', './other.jpg']) |
| 152 | + }) |
| 153 | + |
| 154 | + test('should deduplicate paths', () => { |
| 155 | + const input = '@test.png and /absolute/test.png and @test.png again' |
| 156 | + const paths = extractImagePaths(input) |
| 157 | + expect(paths).toEqual(['test.png', '/absolute/test.png']) |
| 158 | + }) |
| 159 | + |
| 160 | + test('should NOT auto-detect bare filenames without separators', () => { |
| 161 | + const input = 'Mentioned logo.png and banner.jpg in the text' |
| 162 | + const paths = extractImagePaths(input) |
| 163 | + expect(paths).toEqual([]) |
| 164 | + }) |
| 165 | + |
| 166 | + test('should handle weird characters and spaces in quoted paths', () => { |
| 167 | + const input = 'Files: "./ConstellationFS Demo · 1.21am · 09-11.jpeg" and \'../images/café ñoño (2024).png\'' |
| 168 | + const paths = extractImagePaths(input) |
| 169 | + expect(paths).toEqual(['./ConstellationFS Demo · 1.21am · 09-11.jpeg', '../images/café ñoño (2024).png']) |
| 170 | + }) |
| 171 | + |
| 172 | + test('should auto-detect paths with spaces and special characters', () => { |
| 173 | + const input = |
| 174 | + '/Users/brandonchen/Downloads/ConstellationFS Demo · 1.21am · 09-11.jpeg' |
| 175 | + const paths = extractImagePaths(input) |
| 176 | + expect(paths).toEqual([ |
| 177 | + '/Users/brandonchen/Downloads/ConstellationFS Demo · 1.21am · 09-11.jpeg', |
| 178 | + ]) |
| 179 | + }) |
| 180 | + |
| 181 | + test('should handle standalone paths with spaces as the entire input', () => { |
| 182 | + const input = ' /Users/test/My Documents/screenshot file.png ' |
| 183 | + const paths = extractImagePaths(input) |
| 184 | + expect(paths).toEqual(['/Users/test/My Documents/screenshot file.png']) |
| 185 | + }) |
| 186 | + }) |
| 187 | + |
| 188 | + describe('processImageFile', () => { |
| 189 | + test('should successfully process a valid image file', async () => { |
| 190 | + const result = await processImageFile(TEST_IMAGE_PATH, TEST_DIR) |
| 191 | + |
| 192 | + expect(result.success).toBe(true) |
| 193 | + expect(result.imagePart).toBeDefined() |
| 194 | + expect(result.imagePart!.type).toBe('image') |
| 195 | + expect(['image/jpeg', 'image/png']).toContain(result.imagePart!.mediaType) // May be compressed to JPEG |
| 196 | + expect(result.imagePart!.filename).toBe('test-image.png') |
| 197 | + expect(result.imagePart!.image).toMatch(/^[A-Za-z0-9+/]+=*$/) // Base64 regex |
| 198 | + }) |
| 199 | + |
| 200 | + test('should reject file that does not exist', async () => { |
| 201 | + const result = await processImageFile('nonexistent.png', TEST_DIR) |
| 202 | + |
| 203 | + expect(result.success).toBe(false) |
| 204 | + expect(result.error).toContain('File not found') |
| 205 | + }) |
| 206 | + |
| 207 | + test('should reject files that are too large', async () => { |
| 208 | + const result = await processImageFile(TEST_LARGE_IMAGE_PATH, TEST_DIR) |
| 209 | + |
| 210 | + expect(result.success).toBe(false) |
| 211 | + expect(result.error).toContain('File too large') |
| 212 | + }) |
| 213 | + |
| 214 | + test('should reject non-image files', async () => { |
| 215 | + const textFilePath = path.join(TEST_DIR, 'test.txt') |
| 216 | + writeFileSync(textFilePath, 'hello world') |
| 217 | + |
| 218 | + const result = await processImageFile(textFilePath, TEST_DIR) |
| 219 | + |
| 220 | + expect(result.success).toBe(false) |
| 221 | + expect(result.error).toContain('Unsupported image format') |
| 222 | + }) |
| 223 | + }) |
| 224 | +}) |
0 commit comments