|
| 1 | +import { mkdirSync, mkdtempSync, rmSync, readFileSync, writeFileSync } from 'fs' |
| 2 | +import os from 'os' |
| 3 | +import path from 'path' |
| 4 | + |
| 5 | +import { describe, test, expect, beforeEach, afterEach, mock } from 'bun:test' |
| 6 | +import { Jimp } from 'jimp' |
| 7 | + |
| 8 | +import { setProjectRoot } from '../../project-files' |
| 9 | +import { processImageFile } from '../image-handler' |
| 10 | +import { MAX_IMAGE_BASE64_SIZE } from '@codebuff/common/constants/images' |
| 11 | + |
| 12 | +// Mock the logger to prevent analytics initialization errors in tests |
| 13 | +mock.module('../logger', () => ({ |
| 14 | + logger: { |
| 15 | + debug: () => {}, |
| 16 | + info: () => {}, |
| 17 | + warn: () => {}, |
| 18 | + error: () => {}, |
| 19 | + fatal: () => {}, |
| 20 | + }, |
| 21 | +})) |
| 22 | + |
| 23 | +let TEST_DIR: string |
| 24 | + |
| 25 | +beforeEach(async () => { |
| 26 | + TEST_DIR = mkdtempSync(path.join(os.tmpdir(), 'cli-image-integrity-')) |
| 27 | + mkdirSync(path.join(TEST_DIR, 'debug'), { recursive: true }) |
| 28 | + setProjectRoot(TEST_DIR) |
| 29 | +}) |
| 30 | + |
| 31 | +afterEach(() => { |
| 32 | + try { |
| 33 | + rmSync(TEST_DIR, { recursive: true, force: true }) |
| 34 | + } catch { |
| 35 | + // Ignore cleanup errors |
| 36 | + } |
| 37 | +}) |
| 38 | + |
| 39 | +/** Fill an image with high-entropy content so its PNG encoding is large |
| 40 | + * enough to exercise the compression path. */ |
| 41 | +function addNoise(image: InstanceType<typeof Jimp>, amount = 0.9): void { |
| 42 | + image.scan(0, 0, image.bitmap.width, image.bitmap.height, (x, y, idx) => { |
| 43 | + if (Math.random() < amount) { |
| 44 | + image.bitmap.data[idx] = Math.floor(Math.random() * 256) |
| 45 | + image.bitmap.data[idx + 1] = Math.floor(Math.random() * 256) |
| 46 | + image.bitmap.data[idx + 2] = Math.floor(Math.random() * 256) |
| 47 | + // Leave alpha as-is |
| 48 | + } |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +describe('image pipeline integrity', () => { |
| 53 | + test('small PNG passes through byte-identical and decodes', async () => { |
| 54 | + const image = new Jimp({ width: 200, height: 100, color: 0x1e90ffff }) |
| 55 | + // Draw a readable "UI-like" pattern: white bar + dark text-like stripes |
| 56 | + image.scan(0, 0, 200, 100, (x, y, idx) => { |
| 57 | + if (x >= 20 && x < 180 && y >= 30 && y < 70) { |
| 58 | + image.bitmap.data[idx] = 0xff |
| 59 | + image.bitmap.data[idx + 1] = 0xff |
| 60 | + image.bitmap.data[idx + 2] = 0xff |
| 61 | + } |
| 62 | + }) |
| 63 | + const filePath = path.join(TEST_DIR, 'small-200x100.png') as `${string}.${string}` |
| 64 | + await image.write(filePath) |
| 65 | + const originalBytes = readFileSync(filePath) |
| 66 | + |
| 67 | + const result = await processImageFile('small-200x100.png', TEST_DIR) |
| 68 | + |
| 69 | + expect(result.success).toBe(true) |
| 70 | + expect(result.imagePart).toBeDefined() |
| 71 | + expect(result.wasCompressed).toBe(false) |
| 72 | + |
| 73 | + const part = result.imagePart! |
| 74 | + expect(part.width).toBe(200) |
| 75 | + expect(part.height).toBe(100) |
| 76 | + expect(part.mediaType).toBe('image/png') |
| 77 | + |
| 78 | + // The passthrough path must not mutate the payload. |
| 79 | + const decoded = Buffer.from(part.image, 'base64') |
| 80 | + expect(decoded.equals(originalBytes)).toBe(true) |
| 81 | + |
| 82 | + // And it must re-decode as a valid PNG with the right dimensions. |
| 83 | + const reread = await Jimp.read(decoded) |
| 84 | + expect(reread.bitmap.width).toBe(200) |
| 85 | + expect(reread.bitmap.height).toBe(100) |
| 86 | + }) |
| 87 | + |
| 88 | + test('large noisy image is compressed to a valid JPEG that still decodes', async () => { |
| 89 | + // 1600x1600 noise produces a PNG far above MAX_IMAGE_BASE64_SIZE. |
| 90 | + const image = new Jimp({ width: 1600, height: 1600, color: 0x000000ff }) |
| 91 | + addNoise(image) |
| 92 | + const filePath = path.join(TEST_DIR, 'big-noisy.png') as `${string}.${string}` |
| 93 | + await image.write(filePath) |
| 94 | + |
| 95 | + const originalBase64 = readFileSync(filePath).toString('base64') |
| 96 | + expect(originalBase64.length).toBeGreaterThan(MAX_IMAGE_BASE64_SIZE) |
| 97 | + |
| 98 | + const result = await processImageFile('big-noisy.png', TEST_DIR) |
| 99 | + |
| 100 | + expect(result.success).toBe(true) |
| 101 | + expect(result.wasCompressed).toBe(true) |
| 102 | + const part = result.imagePart! |
| 103 | + expect(part.mediaType).toBe('image/jpeg') |
| 104 | + expect(part.image.length).toBeLessThanOrEqual(MAX_IMAGE_BASE64_SIZE) |
| 105 | + expect(part.width).toBeDefined() |
| 106 | + expect(part.height).toBeDefined() |
| 107 | + |
| 108 | + // The compressed payload must decode as a real JPEG. |
| 109 | + const decoded = Buffer.from(part.image, 'base64') |
| 110 | + expect(decoded[0]).toBe(0xff) |
| 111 | + expect(decoded[1]).toBe(0xd8) |
| 112 | + |
| 113 | + const reread = await Jimp.read(decoded) |
| 114 | + // Aspect ratio preserved (1600x1600 → square at reduced dimension). |
| 115 | + expect(reread.bitmap.width).toBe(reread.bitmap.height) |
| 116 | + expect(reread.bitmap.width).toBe(part.width!) |
| 117 | + expect(reread.bitmap.height).toBe(part.height!) |
| 118 | + // Resized to the largest dimension that fit the budget. |
| 119 | + expect(reread.bitmap.width).toBeGreaterThan(0) |
| 120 | + expect(reread.bitmap.width).toBeLessThanOrEqual(1600) |
| 121 | + |
| 122 | + // Content must survive: average luminance should stay high (noise). |
| 123 | + let sum = 0 |
| 124 | + let count = 0 |
| 125 | + reread.scan(0, 0, reread.bitmap.width, reread.bitmap.height, (_x, _y, idx) => { |
| 126 | + sum += reread.bitmap.data[idx] |
| 127 | + count++ |
| 128 | + }) |
| 129 | + const avg = sum / count |
| 130 | + expect(avg).toBeGreaterThan(30) |
| 131 | + expect(avg).toBeLessThan(235) |
| 132 | + }) |
| 133 | + |
| 134 | + test('landscape compression preserves aspect ratio', async () => { |
| 135 | + const image = new Jimp({ width: 2400, height: 1200, color: 0x00ff00ff }) |
| 136 | + addNoise(image, 0.7) |
| 137 | + const filePath = path.join(TEST_DIR, 'wide-2400x1200.png') as `${string}.${string}` |
| 138 | + await image.write(filePath) |
| 139 | + |
| 140 | + const result = await processImageFile('wide-2400x1200.png', TEST_DIR) |
| 141 | + expect(result.success).toBe(true) |
| 142 | + expect(result.wasCompressed).toBe(true) |
| 143 | + |
| 144 | + const reread = await Jimp.read(Buffer.from(result.imagePart!.image, 'base64')) |
| 145 | + expect(reread.bitmap.width / reread.bitmap.height).toBeCloseTo(2, 1) |
| 146 | + }) |
| 147 | + |
| 148 | + test('portrait compression preserves aspect ratio', async () => { |
| 149 | + const image = new Jimp({ width: 1200, height: 2400, color: 0xff0000ff }) |
| 150 | + addNoise(image, 0.7) |
| 151 | + const filePath = path.join(TEST_DIR, 'tall-1200x2400.png') as `${string}.${string}` |
| 152 | + await image.write(filePath) |
| 153 | + |
| 154 | + const result = await processImageFile('tall-1200x2400.png', TEST_DIR) |
| 155 | + expect(result.success).toBe(true) |
| 156 | + expect(result.wasCompressed).toBe(true) |
| 157 | + |
| 158 | + const reread = await Jimp.read(Buffer.from(result.imagePart!.image, 'base64')) |
| 159 | + expect(reread.bitmap.height / reread.bitmap.width).toBeCloseTo(2, 1) |
| 160 | + }) |
| 161 | + |
| 162 | + test('transparent PNG is compressed without crashing and keeps dimensions', async () => { |
| 163 | + const image = new Jimp({ width: 800, height: 600, color: 0x00000000 }) |
| 164 | + // Transparent background with a small opaque shape — common for pasted |
| 165 | + // UI elements; JPEG re-encode must still produce a decodable image. |
| 166 | + image.scan(0, 0, 800, 600, (x, y, idx) => { |
| 167 | + if (x > 100 && x < 300 && y > 100 && y < 300) { |
| 168 | + image.bitmap.data[idx] = 0x00 |
| 169 | + image.bitmap.data[idx + 1] = 0x80 |
| 170 | + image.bitmap.data[idx + 2] = 0xff |
| 171 | + image.bitmap.data[idx + 3] = 0xff |
| 172 | + } |
| 173 | + }) |
| 174 | + const filePath = path.join(TEST_DIR, 'alpha-800x600.png') as `${string}.${string}` |
| 175 | + await image.write(filePath) |
| 176 | + |
| 177 | + const result = await processImageFile('alpha-800x600.png', TEST_DIR) |
| 178 | + expect(result.success).toBe(true) |
| 179 | + if (result.wasCompressed) { |
| 180 | + const part = result.imagePart! |
| 181 | + const reread = await Jimp.read(Buffer.from(part.image, 'base64')) |
| 182 | + expect(reread.bitmap.width).toBe(part.width!) |
| 183 | + expect(reread.bitmap.height).toBe(part.height!) |
| 184 | + } |
| 185 | + }) |
| 186 | + |
| 187 | + test('jpeg input is accepted and re-encodes validly when compressed', async () => { |
| 188 | + const image = new Jimp({ width: 1400, height: 900, color: 0x808080ff }) |
| 189 | + addNoise(image, 0.8) |
| 190 | + const filePath = path.join(TEST_DIR, 'photo-1400x900.jpg') as `${string}.${string}` |
| 191 | + await image.write(filePath) |
| 192 | + |
| 193 | + const result = await processImageFile('photo-1400x900.jpg', TEST_DIR) |
| 194 | + expect(result.success).toBe(true) |
| 195 | + const part = result.imagePart! |
| 196 | + expect(part.mediaType).toBe('image/jpeg') |
| 197 | + |
| 198 | + const reread = await Jimp.read(Buffer.from(part.image, 'base64')) |
| 199 | + expect(reread.bitmap.width).toBe(part.width!) |
| 200 | + expect(reread.bitmap.height).toBe(part.height!) |
| 201 | + }) |
| 202 | + |
| 203 | + test('oversized file is rejected with a clear error, not corrupted', async () => { |
| 204 | + // MAX_IMAGE_FILE_SIZE is 10MB; write a file that exceeds it and confirm |
| 205 | + // we get a descriptive error instead of a silent failure. |
| 206 | + const bigPath = path.join(TEST_DIR, 'huge.png') |
| 207 | + const chunk = Buffer.alloc(1024 * 1024, 0x89) |
| 208 | + writeFileSync(bigPath, Buffer.concat(Array(11).fill(chunk))) |
| 209 | + |
| 210 | + const result = await processImageFile('huge.png', TEST_DIR) |
| 211 | + expect(result.success).toBe(false) |
| 212 | + expect(result.error).toContain('too large') |
| 213 | + }) |
| 214 | +}) |
0 commit comments