Open History to see the last commit
1import { describe, expect, it } from "vitest"
2import { findNearestEdge } from "../../../src/lib/edge-picker"
3import { W, H, coordsToEdgeId } from "../../../shared/edge-math"
4
5describe("edge picking", () => {
6 for (const touch of [false, true]) {
7 it(`covers both ends of horizontal and vertical edges (${touch ? "touch" : "mouse"})`, () => {
8 for (const offset of [0.01, 0.1, 0.9, 0.99]) {
9 expect(findNearestEdge(10 + offset, 20, 20, touch)).toBe(coordsToEdgeId(10, 20, true))
10 expect(findNearestEdge(10, 20 + offset, 20, touch)).toBe(coordsToEdgeId(10, 20, false))
11 }
12 })
13
14 it(`chooses the closest edge near a junction (${touch ? "touch" : "mouse"})`, () => {
15 expect(findNearestEdge(10.02, 20.1, 20, touch)).toBe(coordsToEdgeId(10, 20, false))
16 expect(findNearestEdge(10.1, 20.02, 20, touch)).toBe(coordsToEdgeId(10, 20, true))
17 })
18 }
19
20 it("includes outer endpoints and only returns valid boundary edges", () => {
21 expect(findNearestEdge(0, 0, 20, false)).toBe(coordsToEdgeId(0, 0, true))
22 expect(findNearestEdge(W, H, 20, false)).toBe(coordsToEdgeId(W - 1, H, true))
23 expect(findNearestEdge(W, H - 0.01, 20, false)).toBe(coordsToEdgeId(W, H - 1, false))
24 expect(findNearestEdge(W - 0.01, H, 20, false)).toBe(coordsToEdgeId(W - 1, H, true))
25 expect(findNearestEdge(-0.1, 20.5, 20, false)).toBe(coordsToEdgeId(0, 20, false))
26 expect(findNearestEdge(-0.3, 20.5, 20, false)).toBeNull()
27 })
28
29 it("retains mouse/touch hit widths and minimum zoom levels", () => {
30 expect(findNearestEdge(10.5, 20.3, 20, false)).toBeNull()
31 expect(findNearestEdge(10.5, 20.3, 20, true)).toBe(coordsToEdgeId(10, 20, true))
32 expect(findNearestEdge(10.5, 20.5, 20, true)).toBeNull()
33 expect(findNearestEdge(10.5, 20, 14, false)).toBeNull()
34 expect(findNearestEdge(10.5, 20, 12, true)).toBe(coordsToEdgeId(10, 20, true))
35 expect(findNearestEdge(10.5, 20, 11, true)).toBeNull()
36 expect(findNearestEdge(NaN, 20, 20, false)).toBeNull()
37 })
38})