/** * Change Detection Service Tests */ const { ChangeDetectionService } = require('../src/services/changeDetection'); describe('ChangeDetectionService', () => { let service; let baseImage; let modifiedImage; beforeEach(async () => { service = new ChangeDetectionService(); // Create test images with same dimensions const width = 640; const height = 640; // Create white image baseImage = Buffer.alloc(width * height * 3, 255); // Create modified image with red region modifiedImage = Buffer.from(baseImage); for (let y = 100; y < 200; y++) { for (let x = 100; x < 200; x++) { const idx = (y * width + x) * 3; modifiedImage[idx] = 255; // R modifiedImage[idx + 1] = 0; // G modifiedImage[idx + 2] = 0; // B } } }); describe('calculatePixelDifference', () => { it('should calculate pixel difference', () => { const img1 = Buffer.from([255, 255, 255, 0, 0, 0]); const img2 = Buffer.from([255, 255, 255, 255, 255, 255]); const diff = service.calculatePixelDifference(img1, img2); expect(diff).toBeGreaterThan(0); expect(diff).toBeLessThanOrEqual(1); }); it('should return 0 for identical images', () => { const img = Buffer.from([128, 128, 128, 128, 128, 128]); const diff = service.calculatePixelDifference(img, img); expect(diff).toBe(0); }); }); describe('classifyChange', () => { it('should classify no change', () => { const result = service.classifyChange(0, 0, 0); expect(result).toBe('none'); }); it('should classify minor change', () => { const result = service.classifyChange(0.03, 0.03, 0.02); expect(result).toBe('minor'); }); it('should classify significant change', () => { const result = service.classifyChange(0.2, 0.2, 0.2); expect(result).toBe('significant'); }); }); describe('combineMetrics', () => { it('should combine metrics with weights', () => { const result = service.combineMetrics(0.1, 0.2, 0.3); // Expected: 0.1*0.3 + 0.2*0.5 + 0.3*0.2 = 0.03 + 0.10 + 0.06 = 0.19 expect(result).toBeCloseTo(0.19, 2); }); }); describe('calculateConfidence', () => { it('should return high confidence for clear changes', () => { const result = service.calculateConfidence(0.25); expect(result).toBeGreaterThan(0.9); }); it('should return lower confidence for borderline changes', () => { const result = service.calculateConfidence(0.1); expect(result).toBeLessThan(0.8); expect(result).toBeGreaterThan(0.5); }); }); });