import { describe, expect, it } from 'vitest'; import type { PageImage, Stroke } from '$lib/model/types'; import { imageSize, instantiate, makeClip, placeCentred } from './clip'; const s: Stroke = { id: 's', tool: 'pen', color: '#000', width: 2, pressure: false, points: [110, 210, 0.5, 0, 0, 0, 150, 260, 0.5, 0, 0, 8] }; const img: PageImage = { id: 'i', assetId: 'a', x: 100, y: 200, width: 20, height: 10 }; const A4 = { width: 595.28, height: 841.89 }; describe('clip', () => { it('normalises to the bounding box and pastes fresh copies', () => { const clip = makeClip([s], [img])!; expect(clip).toMatchObject({ width: 51, height: 61 }); expect(clip.images[0]).toMatchObject({ x: 0, y: 0 }); expect(clip.strokes[0].points.slice(0, 2)).toEqual([10, 10]); const pasted = instantiate(clip, 300, 400); expect(pasted.images[0]).toMatchObject({ x: 300, y: 400, assetId: 'a' }); expect(pasted.strokes[0].points.slice(6, 8)).toEqual([350, 460]); expect(pasted.strokes[0].id).not.toBe('s'); expect(pasted.images[0].id).not.toBe('i'); expect(makeClip([], [])).toBeNull(); }); it('keeps pasted things on the page', () => { expect(placeCentred(300, 400, { width: 100, height: 50 }, A4)).toEqual({ x: 250, y: 375 }); expect(placeCentred(10, 10, { width: 100, height: 50 }, A4)).toEqual({ x: 0, y: 0 }); expect(placeCentred(590, 840, { width: 100, height: 50 }, A4)).toEqual({ x: 495.28, y: 791.89 }); // Larger than the page: centred on it. expect(placeCentred(0, 0, { width: 700, height: 50 }, A4).x).toBeCloseTo(-52.36); }); it('sizes images at 96 dpi, at most 80 % of the page', () => { expect(imageSize({ width: 400, height: 200 }, A4)).toEqual({ width: 300, height: 150 }); const big = imageSize({ width: 2400, height: 1200 }, A4); expect(big.width).toBeCloseTo(476.22); expect(big.height).toBeCloseTo(238.11); }); });