import { describe, it, expect, vi } from 'vitest' import { boxesOverlap, findFreeLabelPosition, drawArrowHead, drawShape, type LabelBox } from '../../../src/renderer/src/components/drawRoughShapes' import type { RoughCanvas } from 'roughjs/bin/canvas' function fakeCtx(): { ctx: CanvasRenderingContext2D fillText: ReturnType beginPath: ReturnType fill: ReturnType } { const fillText = vi.fn() const beginPath = vi.fn() const fill = vi.fn() const ctx = { measureText: vi.fn(() => ({ width: 40 })), save: vi.fn(), restore: vi.fn(), beginPath, moveTo: vi.fn(), lineTo: vi.fn(), closePath: vi.fn(), fill, fillText, fillStyle: '', font: '' } as unknown as CanvasRenderingContext2D return { ctx, fillText, beginPath, fill } } function fakeRoughCanvas(): { rc: RoughCanvas rectangle: ReturnType circle: ReturnType line: ReturnType curve: ReturnType arc: ReturnType } { const rectangle = vi.fn() const circle = vi.fn() const line = vi.fn() const curve = vi.fn() const arc = vi.fn() const rc = { rectangle, circle, line, curve, arc } as unknown as RoughCanvas return { rc, rectangle, circle, line, curve, arc } } describe('boxesOverlap()', () => { it('should detect overlapping boxes', () => { const a: LabelBox = { x: 0, y: 0, width: 10, height: 10 } const b: LabelBox = { x: 5, y: 5, width: 10, height: 10 } expect(boxesOverlap(a, b)).toBe(true) }) it('should detect non-overlapping boxes', () => { const a: LabelBox = { x: 0, y: 0, width: 10, height: 10 } const b: LabelBox = { x: 100, y: 100, width: 10, height: 10 } expect(boxesOverlap(a, b)).toBe(false) }) }) describe('findFreeLabelPosition()', () => { it('should return the starting position when nothing is placed yet', () => { const { ctx } = fakeCtx() const box = findFreeLabelPosition(ctx, 'Label', 10, 30, []) expect(box).toEqual({ x: 10, y: 14, width: 40, height: 16 }) }) it('should shift downward to avoid an already-placed overlapping box', () => { const { ctx } = fakeCtx() const placed: LabelBox[] = [{ x: 10, y: 14, width: 40, height: 16 }] const box = findFreeLabelPosition(ctx, 'Label', 10, 30, placed) expect(box.y).toBeGreaterThan(14) }) it('should fall back to the last attempted position after 20 attempts if always overlapping', () => { const { ctx } = fakeCtx() const placed: LabelBox[] = Array.from({ length: 25 }, (_, i) => ({ x: 10, y: 14 + i * 16, width: 40, height: 16 })) const box = findFreeLabelPosition(ctx, 'Label', 10, 30, placed) expect(box).toBeDefined() }) }) describe('drawArrowHead()', () => { it('should draw a filled triangle at the arrow tip', () => { const { ctx, beginPath, fill } = fakeCtx() drawArrowHead(ctx, 0, 0, 10, 10, '#000') expect(beginPath).toHaveBeenCalled() expect(fill).toHaveBeenCalled() }) }) describe('drawShape()', () => { it('should draw a rectangle', () => { const { rc, rectangle } = fakeRoughCanvas() const { ctx } = fakeCtx() drawShape(rc, ctx, { type: 'rect', x: 1, y: 2, width: 10, height: 20 }, []) expect(rectangle).toHaveBeenCalledWith(1, 2, 10, 20, expect.anything()) }) it('should draw a circle', () => { const { rc, circle } = fakeRoughCanvas() const { ctx } = fakeCtx() drawShape(rc, ctx, { type: 'circle', x: 1, y: 2, radius: 5 }, []) expect(circle).toHaveBeenCalledWith(1, 2, 10, expect.anything()) }) it('should draw a line and position its label at the segment midpoint', () => { const { rc, line } = fakeRoughCanvas() const { ctx, fillText } = fakeCtx() drawShape(rc, ctx, { type: 'line', x: 0, y: 0, x2: 10, y2: 0, label: 'AB' }, []) expect(line).toHaveBeenCalledWith(0, 0, 10, 0, expect.anything()) expect(fillText).toHaveBeenCalled() }) it('should draw an arrow with an arrowhead', () => { const { rc, line } = fakeRoughCanvas() const { ctx, fill } = fakeCtx() drawShape(rc, ctx, { type: 'arrow', x: 0, y: 0, x2: 10, y2: 10 }, []) expect(line).toHaveBeenCalled() expect(fill).toHaveBeenCalled() }) it('should draw a stroke curve only when at least 2 points are given', () => { const { rc, curve } = fakeRoughCanvas() const { ctx } = fakeCtx() drawShape( rc, ctx, { type: 'stroke', points: [ { x: 0, y: 0 }, { x: 1, y: 1 } ] }, [] ) expect(curve).toHaveBeenCalled() }) it('should not draw a stroke with fewer than 2 points', () => { const { rc, curve } = fakeRoughCanvas() const { ctx } = fakeCtx() drawShape(rc, ctx, { type: 'stroke', points: [{ x: 0, y: 0 }] }, []) expect(curve).not.toHaveBeenCalled() }) it('should draw an arc', () => { const { rc, arc } = fakeRoughCanvas() const { ctx } = fakeCtx() drawShape(rc, ctx, { type: 'arc', x: 0, y: 0, radius: 10, startAngle: 0, endAngle: 90 }, []) expect(arc).toHaveBeenCalled() }) it('should draw text content directly and record its label box', () => { const { rc } = fakeRoughCanvas() const { ctx, fillText } = fakeCtx() const placed: LabelBox[] = [] drawShape(rc, ctx, { type: 'text', x: 0, y: 0, content: 'Bonjour' }, placed) expect(fillText).toHaveBeenCalledWith('Bonjour', 0, 0) expect(placed.length).toBe(1) }) })