hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { constrainEnd, shapeStrokes, type ShapeKind } from "./board-shapes"; |
| 3 | import { strokesBounds } from "./draw-stroke"; |
| 4 | |
| 5 | const style = { color: "#1e1e1e", width: 2 }; |
| 6 | const a = { x: 10, y: 20 }; |
| 7 | const b = { x: 210, y: 120 }; |
| 8 | |
| 9 | describe("board-shapes", () => { |
| 10 | it("returns nothing for a click that never became a shape", () => { |
| 11 | for (const kind of ["rectangle", "diamond", "ellipse", "line", "arrow"] as ShapeKind[]) { |
| 12 | expect(shapeStrokes(kind, a, { x: 11, y: 20.5 }, style, 1, 7)).toEqual([]); |
| 13 | } |
| 14 | }); |
| 15 | |
| 16 | it("keeps a clean rectangle inside its dragged box", () => { |
| 17 | const strokes = shapeStrokes("rectangle", a, b, style, 0, 1); |
| 18 | expect(strokes).toHaveLength(1); |
| 19 | const box = strokesBounds(strokes)!; |
| 20 | expect(box.x).toBeCloseTo(a.x - 1, 5); |
| 21 | expect(box.y).toBeCloseTo(a.y - 1, 5); |
| 22 | expect(box.w).toBeCloseTo(b.x - a.x + 2, 5); |
| 23 | expect(box.h).toBeCloseTo(b.y - a.y + 2, 5); |
| 24 | }); |
| 25 | |
| 26 | it("draws a sloppy shape close to, not exactly on, its box", () => { |
| 27 | const box = strokesBounds(shapeStrokes("diamond", a, b, style, 1, 3))!; |
| 28 | expect(Math.abs(box.x - (a.x - 1))).toBeLessThan(6); |
| 29 | expect(Math.abs(box.w - (b.x - a.x + 2))).toBeLessThan(12); |
| 30 | }); |
| 31 | |
| 32 | it("is deterministic for a seed and varies across seeds", () => { |
| 33 | const one = shapeStrokes("ellipse", a, b, style, 1, 42); |
| 34 | expect(shapeStrokes("ellipse", a, b, style, 1, 42)).toEqual(one); |
| 35 | expect(shapeStrokes("ellipse", a, b, style, 1, 43)).not.toEqual(one); |
| 36 | }); |
| 37 | |
| 38 | it("splits an arrow into shaft and head, and doubles a cartoonist pass", () => { |
| 39 | expect(shapeStrokes("arrow", a, b, style, 0, 1)).toHaveLength(2); |
| 40 | expect(shapeStrokes("arrow", a, b, style, 2, 1)).toHaveLength(4); |
| 41 | expect(shapeStrokes("rectangle", a, b, style, 2, 1)).toHaveLength(2); |
| 42 | }); |
| 43 | |
| 44 | it("ends a clean arrow's shaft and head on the tip", () => { |
| 45 | const [shaft, head] = shapeStrokes("arrow", a, b, style, 0, 1); |
| 46 | expect(shaft.points[0]).toEqual(a); |
| 47 | expect(shaft.points[shaft.points.length - 1]).toEqual(b); |
| 48 | expect(head.points).toContainEqual(b); |
| 49 | }); |
| 50 | |
| 51 | it("carries the dash pattern onto every stroke", () => { |
| 52 | for (const s of shapeStrokes("arrow", a, b, { ...style, dash: "dotted" }, 1, 1)) { |
| 53 | expect(s.dash).toBe("dotted"); |
| 54 | expect(s.erase).toBe(false); |
| 55 | } |
| 56 | expect("dash" in shapeStrokes("line", a, b, style, 0, 1)[0]).toBe(false); |
| 57 | }); |
| 58 | |
| 59 | it("constrains a box to a square and a line to 45° steps", () => { |
| 60 | expect(constrainEnd("rectangle", { x: 0, y: 0 }, { x: -30, y: 10 })).toEqual({ x: -30, y: 30 }); |
| 61 | const end = constrainEnd("line", { x: 0, y: 0 }, { x: 100, y: 10 }); |
| 62 | expect(end.x).toBeCloseTo(Math.hypot(100, 10), 5); |
| 63 | expect(end.y).toBeCloseTo(0, 5); |
| 64 | }); |
| 65 | }); |