|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import { preprocessImage, preprocessForSubtitles, preprocessForGeneralText, DEFAULT_PREPROCESSOR_CONFIG } from '@/utils/image-preprocessor' |
| 3 | +import { makeFrame } from '@/test-utils/frames' |
| 4 | + |
| 5 | +// Mock canvas and context |
| 6 | +const mockCanvas = { |
| 7 | + width: 0, |
| 8 | + height: 0, |
| 9 | + getContext: vi.fn(() => ({ |
| 10 | + putImageData: vi.fn(), |
| 11 | + })), |
| 12 | + toDataURL: vi.fn(() => 'data:image/png;base64,mock'), |
| 13 | + toBlob: vi.fn((cb: (blob: Blob) => void) => cb(new Blob(['mock'], { type: 'image/png' }))), |
| 14 | +} |
| 15 | + |
| 16 | +beforeEach(() => { |
| 17 | + vi.clearAllMocks() |
| 18 | + vi.stubGlobal('document', { |
| 19 | + createElement: vi.fn((tag: string) => { |
| 20 | + if (tag === 'canvas') return mockCanvas |
| 21 | + return {} |
| 22 | + }), |
| 23 | + }) |
| 24 | + vi.stubGlobal('HTMLCanvasElement', vi.fn(() => mockCanvas)) |
| 25 | +}) |
| 26 | + |
| 27 | +describe('preprocessImage', () => { |
| 28 | + const input = makeFrame(40, 40, 128, 128, 128) |
| 29 | + |
| 30 | + it('returns PreprocessorResult with processedData, canvas, toDataURL, toBlob', () => { |
| 31 | + const result = preprocessImage(input) |
| 32 | + expect(result.processedData).toBeInstanceOf(ImageData) |
| 33 | + expect(result.processedData.width).toBeGreaterThan(0) |
| 34 | + expect(result.processedData.height).toBeGreaterThan(0) |
| 35 | + expect(typeof result.toDataURL).toBe('function') |
| 36 | + expect(typeof result.toBlob).toBe('function') |
| 37 | + }) |
| 38 | + |
| 39 | + it('produces output larger than input with default config (scale + deskew)', () => { |
| 40 | + const result = preprocessImage(input) |
| 41 | + expect(result.processedData.width).toBeGreaterThan(input.width) |
| 42 | + expect(result.processedData.height).toBeGreaterThan(input.height) |
| 43 | + }) |
| 44 | + |
| 45 | + it('skips deskew when deskew=false', () => { |
| 46 | + const result = preprocessImage(input, { deskew: false }) |
| 47 | + expect(result.processedData.width).toBeGreaterThan(0) |
| 48 | + expect(result.processedData.height).toBeGreaterThan(0) |
| 49 | + }) |
| 50 | + |
| 51 | + it('skips upscale when scaleFactor=1', () => { |
| 52 | + const result = preprocessImage(input, { scaleFactor: 1, deskew: false }) |
| 53 | + // With scale=1 and no deskew, output should be same size as input |
| 54 | + expect(result.processedData.width).toBe(input.width) |
| 55 | + expect(result.processedData.height).toBe(input.height) |
| 56 | + }) |
| 57 | + |
| 58 | + it('skips denoise when denoise=false', () => { |
| 59 | + const result = preprocessImage(input, { denoise: false }) |
| 60 | + expect(result.processedData.width).toBeGreaterThan(0) |
| 61 | + expect(result.processedData.height).toBeGreaterThan(0) |
| 62 | + }) |
| 63 | + |
| 64 | + it('skips threshold when adaptiveThreshold=false', () => { |
| 65 | + const result = preprocessImage(input, { adaptiveThreshold: false }) |
| 66 | + expect(result.processedData.width).toBeGreaterThan(0) |
| 67 | + expect(result.processedData.height).toBeGreaterThan(0) |
| 68 | + }) |
| 69 | + |
| 70 | + it('skips morph cleanup when morphCleanup=false', () => { |
| 71 | + const result = preprocessImage(input, { morphCleanup: false }) |
| 72 | + expect(result.processedData.width).toBeGreaterThan(0) |
| 73 | + expect(result.processedData.height).toBeGreaterThan(0) |
| 74 | + }) |
| 75 | + |
| 76 | + it('applies invertColors when enabled', () => { |
| 77 | + const result = preprocessImage(input, { invertColors: true, scaleFactor: 1, deskew: false }) |
| 78 | + const normal = preprocessImage(input, { invertColors: false, scaleFactor: 1, deskew: false }) |
| 79 | + expect(result.processedData.data[0]).not.toBe(normal.processedData.data[0]) |
| 80 | + }) |
| 81 | + |
| 82 | + it('preserves alpha channel through pipeline', () => { |
| 83 | + const semiTransparent = makeFrame(10, 10, 200, 200, 200) |
| 84 | + semiTransparent.data[3] = 128 |
| 85 | + const result = preprocessImage(semiTransparent, { scaleFactor: 1, deskew: false }) |
| 86 | + expect(result.processedData.data[3]).toBeGreaterThanOrEqual(0) |
| 87 | + expect(result.processedData.data[3]).toBeLessThanOrEqual(255) |
| 88 | + }) |
| 89 | + |
| 90 | + it('toDataURL returns a data URL string', async () => { |
| 91 | + const result = preprocessImage(input) |
| 92 | + const url = await result.toDataURL() |
| 93 | + expect(url).toContain('data:image/png') |
| 94 | + }) |
| 95 | + |
| 96 | + it('toBlob resolves with a Blob', async () => { |
| 97 | + const result = preprocessImage(input) |
| 98 | + const blob = await result.toBlob() |
| 99 | + expect(blob).toBeInstanceOf(Blob) |
| 100 | + }) |
| 101 | +}) |
| 102 | + |
| 103 | +describe('preprocessForSubtitles', () => { |
| 104 | + it('uses subtitle-optimized defaults', () => { |
| 105 | + const result = preprocessForSubtitles(makeFrame(40, 40, 128, 128, 128)) |
| 106 | + expect(result.processedData.width).toBeGreaterThan(0) |
| 107 | + expect(result.processedData.height).toBeGreaterThan(0) |
| 108 | + }) |
| 109 | +}) |
| 110 | + |
| 111 | +describe('preprocessForGeneralText', () => { |
| 112 | + it('uses general-text defaults with multiPass=false', () => { |
| 113 | + const result = preprocessForGeneralText(makeFrame(40, 40, 128, 128, 128)) |
| 114 | + expect(result.processedData.width).toBeGreaterThan(0) |
| 115 | + expect(result.processedData.height).toBeGreaterThan(0) |
| 116 | + }) |
| 117 | +}) |
| 118 | + |
| 119 | +describe('DEFAULT_PREPROCESSOR_CONFIG', () => { |
| 120 | + it('has expected default values', () => { |
| 121 | + expect(DEFAULT_PREPROCESSOR_CONFIG.scaleFactor).toBe(2.0) |
| 122 | + expect(DEFAULT_PREPROCESSOR_CONFIG.enhanceContrast).toBe(true) |
| 123 | + expect(DEFAULT_PREPROCESSOR_CONFIG.contrastLevel).toBe(1.5) |
| 124 | + expect(DEFAULT_PREPROCESSOR_CONFIG.adaptiveThreshold).toBe(true) |
| 125 | + expect(DEFAULT_PREPROCESSOR_CONFIG.adaptiveBlockSize).toBe(11) |
| 126 | + expect(DEFAULT_PREPROCESSOR_CONFIG.denoise).toBe(true) |
| 127 | + expect(DEFAULT_PREPROCESSOR_CONFIG.morphCleanup).toBe(true) |
| 128 | + expect(DEFAULT_PREPROCESSOR_CONFIG.invertColors).toBe(false) |
| 129 | + expect(DEFAULT_PREPROCESSOR_CONFIG.deskew).toBe(true) |
| 130 | + expect(DEFAULT_PREPROCESSOR_CONFIG.multiPass).toBe(true) |
| 131 | + expect(DEFAULT_PREPROCESSOR_CONFIG.multiPassScale).toBe(3.0) |
| 132 | + }) |
| 133 | +}) |
0 commit comments