import { describe, expect, it } from 'vitest' import { dropTarget } from '../src/page/dnd' import type { DndBlock } from '../src/page/dnd' const r = (y: number, h = 100, x = 0, w = 600) => ({ x, y, w, h }) // page: A (container: a1, a2) , B (container, empty), C (leaf) const blocks: DndBlock[] = [ { id: 'A', container: true, depth: 0, rect: r(0, 300) }, { id: 'a1', parentId: 'A', container: false, depth: 1, rect: r(10, 100, 10, 580) }, { id: 'a2', parentId: 'A', container: false, depth: 1, rect: r(120, 100, 10, 580) }, { id: 'B', container: true, depth: 0, rect: r(310, 100) }, { id: 'C', container: false, depth: 0, rect: r(420, 100) }, ] describe('dropTarget', () => { it('picks the index from the pointer relative to sibling midpoints', () => { expect(dropTarget(blocks, { x: 50, y: 20 }, 'C')).toMatchObject({ parentId: 'A', index: 0 }) expect(dropTarget(blocks, { x: 50, y: 100 }, 'C')).toMatchObject({ parentId: 'A', index: 1 }) expect(dropTarget(blocks, { x: 50, y: 250 }, 'C')).toMatchObject({ parentId: 'A', index: 2 }) }) it('excludes the dragged block from the index maths', () => { // dragging a1 within A: pointer below a2's midpoint -> after a2 = index 1 among [a2] expect(dropTarget(blocks, { x: 50, y: 200 }, 'a1')).toMatchObject({ parentId: 'A', index: 1 }) }) it('drops into an empty container', () => { const t = dropTarget(blocks, { x: 50, y: 350 }, 'C')! expect(t).toMatchObject({ parentId: 'B', index: 0 }) expect(t.line.y).toBeGreaterThanOrEqual(310) }) it('never targets the dragged block or its descendants', () => { expect(dropTarget(blocks, { x: 50, y: 20 }, 'A')?.parentId).toBe('') // A is not a drop target for itself -> page level }) it('page level uses the root sentinel and refuses far-away points', () => { expect(dropTarget(blocks, { x: 50, y: 460 }, 'a1')).toMatchObject({ parentId: '', index: 2 }) expect(dropTarget(blocks, { x: 50, y: 500 }, 'a1')).toMatchObject({ parentId: '', index: 3 }) expect(dropTarget(blocks, { x: 50, y: 900 }, 'a1')).toBeNull() }) })