import { describe, expect, it } from 'vitest'; import { analyseGrey, differenceHash, greyFromPixels, groupDuplicates, hammingDistance, laplacianVariance, } from './cull'; function checkerboard(size: number): Uint8Array { const grey = new Uint8Array(size * size); for (let y = 0; y < size; y++) { for (let x = 0; x < size; x++) grey[y * size + x] = (x + y) % 2 === 0 ? 255 : 0; } return grey; } describe('cull assist', () => { it('scores a detailed frame above a flat one', () => { const sharp = laplacianVariance(checkerboard(64), 64, 64); const flat = laplacianVariance(new Uint8Array(64 * 64).fill(120), 64, 64); expect(sharp).toBeGreaterThan(flat); expect(analyseGrey(checkerboard(64), 64, 64).sharpness).toBeGreaterThan( analyseGrey(new Uint8Array(64 * 64).fill(120), 64, 64).sharpness + 20, ); }); it('hashes identical frames identically and reframes only a little', () => { const base = differenceHash(checkerboard(64), 64, 64); const same = differenceHash(checkerboard(64), 64, 64); const shifted = differenceHash(checkerboard(32), 32, 32); expect(base).toBe(same); expect(hammingDistance(base, shifted)).toBeLessThan(20); }); it('groups near-duplicates but keeps different frames apart', () => { const items = [ { id: 'a', hash: 'ffffffffffffffff' }, { id: 'b', hash: 'fffffffffffffffe' }, { id: 'c', hash: '0000000000000000' }, ]; const groups = groupDuplicates(items); expect(groups).toHaveLength(1); expect(groups[0].map((item) => item.id)).toEqual(['a', 'b']); }); it('reads luma from RGBA', () => { const grey = greyFromPixels(new Uint8ClampedArray([255, 255, 255, 255, 0, 0, 0, 255])); expect(grey[0]).toBe(255); expect(grey[1]).toBe(0); }); });