import { describe, it, expect } from 'vitest'; import * as L from '../src/lib.js'; const rect = (W, H, x0, y0, x1, y1) => { const m = new Uint8Array(W * H); for (let y = y0; y <= y1; y++) for (let x = x0; x <= x1; x++) m[y * W + x] = 1; return m; }; describe('validation & geometry', () => { it('validateFile accepts allowed types and rejects others/oversize/empty', () => { expect(L.validateFile({ type: 'image/png', size: 1000 }, 'image').ok).toBe(true); expect(L.validateFile({ type: 'image/gif', size: 1000 }, 'image').ok).toBe(false); expect(L.validateFile({ type: 'image/jpeg', size: 16 * 1024 * 1024 }, 'image').error).toMatch(/15 MB/); expect(L.validateFile({ type: 'image/jpeg', size: 0 }, 'image').ok).toBe(false); expect(L.validateFile({ type: 'video/mp4', size: 5e6 }, 'video').ok).toBe(true); expect(L.validateFile({ type: 'video/x-msvideo', size: 5e6 }, 'video').ok).toBe(false); expect(L.validateFile({ type: 'video/webm', size: 61 * 1024 * 1024 }, 'video').ok).toBe(false); }); it('fitSize keeps aspect, never upscales', () => { expect(L.fitSize(3200, 1600, 1600)).toEqual({ width: 1600, height: 800, scale: 0.5 }); expect(L.fitSize(640, 480, 1600)).toEqual({ width: 640, height: 480, scale: 1 }); expect(() => L.fitSize(0, 10, 10)).toThrow(); }); it('megapixel limit', () => { expect(L.checkMegapixels(6000, 4000)).toBe(true); expect(L.checkMegapixels(6000, 4001)).toBe(false); }); it('displayToImage maps CSS pixels to image pixels and clamps', () => { const r = { left: 10, top: 20, width: 320, height: 240 }; expect(L.displayToImage(170, 140, r, 1280, 960)).toEqual([640, 480]); expect(L.displayToImage(-50, 1000, r, 1280, 960)).toEqual([0, 959]); }); it('normBox normalises drag direction and rejects tiny boxes', () => { expect(L.normBox([100, 80], [20, 10], 200, 200)).toEqual([20, 10, 100, 80]); expect(L.normBox([10, 10], [12, 50], 200, 200)).toBeNull(); expect(L.normBox([-5, -5], [300, 300], 200, 100)).toEqual([0, 0, 199, 99]); }); }); describe('prompt building (transformers.js Sam2/Sam3 processor shapes)', () => { it('points + labels nested as [[[ [x,y] ]]] and [[[ l ]]]', () => { const p = L.buildPrompt([{ x: 1.234, y: 5, label: 1 }, { x: 7, y: 8, label: 0 }], null); expect(p).toEqual({ input_points: [[[[1.23, 5], [7, 8]]]], input_labels: [[[1, 0]]] }); }); it('box only', () => expect(L.buildPrompt([], [1, 2, 3, 4])).toEqual({ input_boxes: [[[1, 2, 3, 4]]] })); it('points + box', () => expect(Object.keys(L.buildPrompt([{ x: 1, y: 1, label: 1 }], [0, 0, 5, 5])).sort()).toEqual(['input_boxes', 'input_labels', 'input_points'])); it('requires at least one prompt and caps point count', () => { expect(() => L.buildPrompt([], null)).toThrow(/al menos/); const many = Array.from({ length: 20 }, (_, i) => ({ x: i, y: i, label: 1 })); expect(L.buildPrompt(many, null).input_points[0][0]).toHaveLength(L.LIMITS.maxPoints); }); }); describe('mask post-processing', () => { it('bestMaskIndex picks highest predicted IoU', () => { expect(L.bestMaskIndex([0.2, 0.9, 0.5])).toBe(1); expect(L.bestMaskIndex(new Float32Array([0.7]))).toBe(0); }); it('sliceMask extracts candidate k from [K,H,W] bool data', () => { const H = 2, W = 3, data = new Uint8Array(3 * H * W); data[1 * 6 + 4] = 1; data[2 * 6 + 0] = 1; expect(Array.from(L.sliceMask(data, 1, H, W))).toEqual([0, 0, 0, 0, 1, 0]); expect(L.maskArea(L.sliceMask(data, 0, H, W))).toBe(0); }); it('area, bbox, centroid, describe', () => { const m = rect(10, 8, 2, 3, 5, 6); expect(L.maskArea(m)).toBe(16); expect(L.maskBBox(m, 10, 8)).toEqual([2, 3, 5, 6]); expect(L.maskCentroid(m, 10, 8)).toEqual([3.5, 4.5]); expect(L.describeMask(m, 10, 8)).toEqual({ area: 16, areaPct: 20, bbox: [2, 3, 5, 6] }); expect(L.maskBBox(new Uint8Array(4), 2, 2)).toBeNull(); }); it('interiorPoint lies on the mask even for a ring (centroid outside)', () => { const W = 21, H = 21, m = new Uint8Array(W * H); for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) { const d = Math.hypot(x - 10, y - 10); if (d >= 7 && d <= 9) m[y * W + x] = 1; } const p = L.interiorPoint(m, W, H); expect(m[p[1] * W + p[0]]).toBe(1); }); it('IoU helpers', () => { expect(L.boxIoU([0, 0, 10, 10], [0, 0, 10, 10])).toBe(1); expect(L.boxIoU([0, 0, 10, 10], [5, 0, 15, 10])).toBeCloseTo(1 / 3); expect(L.boxIoU([0, 0, 1, 1], [2, 2, 3, 3])).toBe(0); const a = rect(10, 10, 0, 0, 4, 9), b = rect(10, 10, 5, 0, 9, 9); expect(L.maskIoU(a, a)).toBe(1); expect(L.maskIoU(a, b)).toBe(0); expect(L.maskIoU(rect(10, 10, 0, 0, 9, 9), a)).toBe(0.5); expect(() => L.maskIoU(a, new Uint8Array(3))).toThrow(); expect(L.fractionInside(a, 10, 10, [0, 0, 4, 4])).toBe(0.5); }); it('expandBox clamps to image', () => expect(L.expandBox([10, 10, 20, 20], 0.5, 22, 100)).toEqual([5, 5, 21, 25])); it('resizeMask nearest neighbour preserves shape', () => { const m = rect(4, 4, 0, 0, 1, 1), r = L.resizeMask(m, 4, 4, 8, 8); expect(L.maskArea(r)).toBe(16); expect(L.maskBBox(r, 8, 8)).toEqual([0, 0, 3, 3]); }); it('overlayMask tints only mask pixels and draws opaque contour', () => { const W = 5, H = 5, rgba = new Uint8ClampedArray(W * H * 4).fill(100), m = rect(W, H, 1, 1, 3, 3); const o = L.overlayMask(rgba, m, W, H, [0, 200, 255], 0.5); expect(Array.from(o.slice(0, 4))).toEqual([100, 100, 100, 100]); // outside untouched const c = (2 * W + 2) * 4; expect(Array.from(o.slice(c, c + 3))).toEqual([50, 150, 178]); // interior blended const e = (1 * W + 1) * 4; expect(Array.from(o.slice(e, e + 3))).toEqual([0, 200, 255]); // contour opaque expect(rgba[c]).toBe(100); // input not mutated }); it('cutout keeps masked pixels and makes the rest transparent', () => { const rgba = new Uint8ClampedArray([1, 2, 3, 255, 4, 5, 6, 255]); expect(Array.from(L.cutout(rgba, new Uint8Array([0, 1])))).toEqual([0, 0, 0, 0, 4, 5, 6, 255]); }); }); describe('video propagation helpers', () => { it('nextFramePrompt = expanded bbox + interior positive point', () => { const m = rect(100, 100, 40, 40, 59, 59); const p = L.nextFramePrompt(m, 100, 100, 0.1); [38.1, 38.1, 60.9, 60.9].forEach((v, k) => expect(p.box[k]).toBeCloseTo(v)); // bbox 40..59 (w=19) ±10 % expect(p.points).toHaveLength(1); expect(p.points[0].label).toBe(1); expect(m[p.points[0].y * 100 + p.points[0].x]).toBe(1); expect(L.nextFramePrompt(new Uint8Array(100), 10, 10)).toBeNull(); }); it('isLost: negative object logit, empty mask or implausible area jump', () => { expect(L.isLost({ objectScoreLogit: -1, area: 100, prevArea: 100 })).toBe(true); expect(L.isLost({ objectScoreLogit: 5, area: 0, prevArea: 100 })).toBe(true); expect(L.isLost({ objectScoreLogit: 5, area: 500, prevArea: 100 })).toBe(true); expect(L.isLost({ objectScoreLogit: 5, area: 20, prevArea: 100 })).toBe(true); expect(L.isLost({ objectScoreLogit: 5, area: 150, prevArea: 100 })).toBe(false); }); it('sampleTimes respects fps, duration and caps', () => { expect(L.sampleTimes(4, 5)).toHaveLength(20); expect(L.sampleTimes(4, 5)[1]).toBe(0.2); expect(L.sampleTimes(100, 10)).toHaveLength(L.LIMITS.maxVideoFrames); expect(L.sampleTimes(30, 2)).toHaveLength(40); // capped at 20 s expect(L.sampleTimes(0, 5)).toEqual([]); }); it('formatBytes uses decimal MB with Spanish comma', () => { expect(L.formatBytes(302295094)).toBe('302,3 MB'); expect(L.formatBytes(5121)).toBe('5 kB'); }); });