import { describe, it, expect } from 'vitest'; import { MaskRegionSchema, MaskRegionsSchema, parseMaskRegions, maskRegionCount, maskForSubIndex, } from '../src/image-occlusion.ts'; describe('MaskRegionSchema', () => { it('akzeptiert valide Region', () => { const r = MaskRegionSchema.safeParse({ id: 'm1', x: 0.1, y: 0.2, w: 0.3, h: 0.1, }); expect(r.success).toBe(true); }); it('akzeptiert mit Label', () => { const r = MaskRegionSchema.safeParse({ id: 'm1', x: 0, y: 0, w: 1, h: 1, label: 'Hippocampus', }); expect(r.success).toBe(true); }); it('lehnt Coordinaten außerhalb 0..1 ab', () => { expect(MaskRegionSchema.safeParse({ id: 'm', x: 1.5, y: 0, w: 0.1, h: 0.1 }).success).toBe( false ); expect(MaskRegionSchema.safeParse({ id: 'm', x: 0, y: 0, w: -0.1, h: 0.1 }).success).toBe( false ); }); it('lehnt extra Felder ab (strict)', () => { const r = MaskRegionSchema.safeParse({ id: 'm1', x: 0, y: 0, w: 0.1, h: 0.1, malicious: 'x', }); expect(r.success).toBe(false); }); }); describe('MaskRegionsSchema', () => { it('verlangt mindestens eine Region', () => { expect(MaskRegionsSchema.safeParse([]).success).toBe(false); }); it('akzeptiert mehrere Regionen', () => { const r = MaskRegionsSchema.safeParse([ { id: 'm1', x: 0, y: 0, w: 0.1, h: 0.1 }, { id: 'm2', x: 0.5, y: 0.5, w: 0.1, h: 0.1 }, ]); expect(r.success).toBe(true); }); it('cap bei 100 Regionen', () => { const tooMany = Array.from({ length: 101 }, (_, i) => ({ id: `m${i}`, x: 0, y: 0, w: 0.01, h: 0.01, })); expect(MaskRegionsSchema.safeParse(tooMany).success).toBe(false); }); }); describe('parseMaskRegions', () => { it('parst und sortiert nach ID', () => { const json = JSON.stringify([ { id: 'm3', x: 0, y: 0, w: 0.1, h: 0.1 }, { id: 'm1', x: 0, y: 0, w: 0.1, h: 0.1 }, { id: 'm2', x: 0, y: 0, w: 0.1, h: 0.1 }, ]); const out = parseMaskRegions(json); expect(out.map((r) => r.id)).toEqual(['m1', 'm2', 'm3']); }); it('liefert leere Liste bei kaputtem JSON', () => { expect(parseMaskRegions('not json')).toEqual([]); }); it('liefert leere Liste bei Schema-Mismatch', () => { expect(parseMaskRegions(JSON.stringify([{ x: 0, y: 0, w: 0.1, h: 0.1 }]))).toEqual([]); }); }); describe('maskRegionCount + maskForSubIndex', () => { const json = JSON.stringify([ { id: 'm2', x: 0.1, y: 0.1, w: 0.2, h: 0.2 }, { id: 'm1', x: 0.3, y: 0.3, w: 0.2, h: 0.2 }, ]); it('zählt Regionen', () => { expect(maskRegionCount(json)).toBe(2); }); it('mapt subIndex auf sortierte Mask', () => { expect(maskForSubIndex(json, 0)?.id).toBe('m1'); expect(maskForSubIndex(json, 1)?.id).toBe('m2'); expect(maskForSubIndex(json, 2)).toBe(null); }); it('returnt 0 / null bei kaputtem JSON', () => { expect(maskRegionCount('garbage')).toBe(0); expect(maskForSubIndex('garbage', 0)).toBe(null); }); });