forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

board-hit.test.ts48 lines, 1.6 KiBHistoryRawDownload
1import { describe, expect, it } from "vitest";
2import { segmentDist, strokeHit } from "./board-hit";
3import type { Stroke } from "./draw-stroke";
4
5const line = (over: Partial<Stroke> = {}): Stroke => ({
6 color: "#000",
7 width: 4,
8 erase: false,
9 points: [
10 { x: 0, y: 0 },
11 { x: 100, y: 0 },
12 ],
13 ...over,
14});
15
16describe("segmentDist", () => {
17 it("is zero for crossing segments", () => {
18 expect(segmentDist({ x: 0, y: -5 }, { x: 0, y: 5 }, { x: -5, y: 0 }, { x: 5, y: 0 })).toBe(0);
19 });
20
21 it("measures the gap between parallel segments", () => {
22 expect(segmentDist({ x: 0, y: 3 }, { x: 10, y: 3 }, { x: 0, y: 0 }, { x: 10, y: 0 })).toBeCloseTo(3);
23 });
24});
25
26describe("strokeHit", () => {
27 it("hits when the eraser crosses the line", () => {
28 expect(strokeHit(line(), { x: 50, y: -20 }, { x: 50, y: 20 }, 2)).toBe(true);
29 });
30
31 it("hits within half the stroke width plus the eraser reach", () => {
32 expect(strokeHit(line(), { x: 50, y: 5 }, { x: 60, y: 5 }, 3)).toBe(true);
33 expect(strokeHit(line(), { x: 50, y: 6 }, { x: 60, y: 6 }, 3)).toBe(false);
34 });
35
36 it("misses a stroke far outside its box", () => {
37 expect(strokeHit(line(), { x: 500, y: 500 }, { x: 510, y: 510 }, 4)).toBe(false);
38 });
39
40 it("hits a single-point dot", () => {
41 expect(strokeHit(line({ points: [{ x: 10, y: 10 }] }), { x: 0, y: 12 }, { x: 20, y: 12 }, 1)).toBe(true);
42 });
43
44 it("never targets a pixel-eraser stroke or an empty one", () => {
45 expect(strokeHit(line({ erase: true }), { x: 50, y: -20 }, { x: 50, y: 20 }, 2)).toBe(false);
46 expect(strokeHit(line({ points: [] }), { x: 50, y: -20 }, { x: 50, y: 20 }, 2)).toBe(false);
47 });
48});