import { describe, test, expect } from 'vitest' import { hitTestCategoryRange, hitTestRects } from './hit-test' describe('hitTestCategoryRange', () => { test('returns empty when dataLength is zero', () => { expect(hitTestCategoryRange(0, 5, 0)).toEqual([]) }) test('snaps fractional range outward so partial overlaps are included', () => { // xStart=1.3, xEnd=3.7 → floor(1.3)=1, ceil(3.7)=4 → [1, 2, 3, 4] expect(hitTestCategoryRange(1.3, 3.7, 10)).toEqual([1, 2, 3, 4]) }) test('handles integer endpoints inclusively', () => { expect(hitTestCategoryRange(2, 4, 10)).toEqual([2, 3, 4]) }) test('clamps to [0, dataLength - 1]', () => { expect(hitTestCategoryRange(-2.5, 12.8, 5)).toEqual([0, 1, 2, 3, 4]) }) test('normalizes reversed inputs (xEnd < xStart)', () => { expect(hitTestCategoryRange(3.7, 1.3, 10)).toEqual([1, 2, 3, 4]) }) test('returns empty when range is entirely out of bounds', () => { expect(hitTestCategoryRange(10, 12, 5)).toEqual([]) expect(hitTestCategoryRange(-5, -1, 5)).toEqual([]) }) test('returns adjacent indices for a very narrow fractional range when snapping outward', () => { // xStart=2.1, xEnd=2.3 partially overlaps both categories, so floor=2 and ceil=3 → [2, 3] expect(hitTestCategoryRange(2.1, 2.3, 10)).toEqual([2, 3]) }) }) describe('hitTestRects', () => { test('returns empty for no rects', () => { expect(hitTestRects([], 10)).toEqual([]) }) test('unions and dedups across overlapping rects', () => { const rects = [ { xStart: 1, xEnd: 3 }, { xStart: 2, xEnd: 4 }, ] expect(hitTestRects(rects, 10)).toEqual([1, 2, 3, 4]) }) test('returns sorted ascending indices for disjoint rects', () => { const rects = [ { xStart: 7, xEnd: 8 }, { xStart: 1, xEnd: 2 }, ] expect(hitTestRects(rects, 10)).toEqual([1, 2, 7, 8]) }) test('drops rects entirely out of bounds', () => { const rects = [ { xStart: 20, xEnd: 25 }, { xStart: 1, xEnd: 2 }, ] expect(hitTestRects(rects, 10)).toEqual([1, 2]) }) })