hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { canvasPx, paintPaper, paintStroke, paintTip, parseScene, playableScene, sliceStrokes, strokesBounds, type DrawScene, type Stroke } from "./draw-stroke"; |
| 3 | |
| 4 | /** Records every 2d-context call so the three geometry branches can be asserted without a DOM. */ |
| 5 | function stubContext() { |
| 6 | const calls: string[] = []; |
| 7 | const props: Record<string, unknown> = {}; |
| 8 | const record = |
| 9 | (name: string) => |
| 10 | (...args: unknown[]) => { |
| 11 | calls.push(`${name}(${args.join(",")})`); |
| 12 | }; |
| 13 | const c = { |
| 14 | calls, |
| 15 | props, |
| 16 | beginPath: record("beginPath"), |
| 17 | moveTo: record("moveTo"), |
| 18 | lineTo: record("lineTo"), |
| 19 | quadraticCurveTo: record("quadraticCurveTo"), |
| 20 | arc: record("arc"), |
| 21 | fill: record("fill"), |
| 22 | stroke: record("stroke"), |
| 23 | }; |
| 24 | // trap the style assignments so composite mode / width can be asserted too |
| 25 | return new Proxy(c, { |
| 26 | set(target, key, value) { |
| 27 | props[String(key)] = value; |
| 28 | return Reflect.set(target, key, value); |
| 29 | }, |
| 30 | }) as unknown as CanvasRenderingContext2D & { calls: string[]; props: Record<string, unknown> }; |
| 31 | } |
| 32 | |
| 33 | const stroke = (points: Stroke["points"], over: Partial<Stroke> = {}): Stroke => ({ |
| 34 | color: "#000", |
| 35 | width: 4, |
| 36 | erase: false, |
| 37 | points, |
| 38 | ...over, |
| 39 | }); |
| 40 | |
| 41 | describe("paintStroke", () => { |
| 42 | it("draws nothing for an empty stroke", () => { |
| 43 | const c = stubContext(); |
| 44 | paintStroke(c, stroke([])); |
| 45 | expect(c.calls).toEqual([]); |
| 46 | }); |
| 47 | |
| 48 | it("fills a dot for a tap with no drag", () => { |
| 49 | const c = stubContext(); |
| 50 | paintStroke(c, stroke([{ x: 10, y: 20 }])); |
| 51 | // radius is half the line width, so a tap matches the pen it was drawn with |
| 52 | expect(c.calls).toContain(`arc(10,20,2,0,${Math.PI * 2})`); |
| 53 | expect(c.calls).toContain("fill()"); |
| 54 | expect(c.calls).not.toContain("stroke()"); |
| 55 | }); |
| 56 | |
| 57 | it("smooths interior points into quadratics and lands on the last point", () => { |
| 58 | const c = stubContext(); |
| 59 | paintStroke( |
| 60 | c, |
| 61 | stroke([ |
| 62 | { x: 0, y: 0 }, |
| 63 | { x: 10, y: 0 }, |
| 64 | { x: 20, y: 10 }, |
| 65 | { x: 30, y: 10 }, |
| 66 | ]), |
| 67 | ); |
| 68 | expect(c.calls).toEqual([ |
| 69 | "beginPath()", |
| 70 | "moveTo(0,0)", |
| 71 | "quadraticCurveTo(10,0,15,5)", |
| 72 | "quadraticCurveTo(20,10,25,10)", |
| 73 | "lineTo(30,10)", |
| 74 | "stroke()", |
| 75 | ]); |
| 76 | }); |
| 77 | |
| 78 | it("cuts through the layer when erasing instead of painting over it", () => { |
| 79 | const c = stubContext(); |
| 80 | paintStroke(c, stroke([{ x: 1, y: 1 }, { x: 2, y: 2 }], { erase: true })); |
| 81 | expect(c.props.globalCompositeOperation).toBe("destination-out"); |
| 82 | }); |
| 83 | |
| 84 | it("paints over the layer when not erasing", () => { |
| 85 | const c = stubContext(); |
| 86 | paintStroke(c, stroke([{ x: 1, y: 1 }, { x: 2, y: 2 }])); |
| 87 | expect(c.props.globalCompositeOperation).toBe("source-over"); |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | describe("paintTip", () => { |
| 92 | it("draws only the newest segment, not the whole stroke", () => { |
| 93 | const c = stubContext(); |
| 94 | paintTip( |
| 95 | c, |
| 96 | stroke([ |
| 97 | { x: 0, y: 0 }, |
| 98 | { x: 5, y: 5 }, |
| 99 | { x: 9, y: 9 }, |
| 100 | ]), |
| 101 | ); |
| 102 | expect(c.calls).toEqual(["beginPath()", "moveTo(5,5)", "lineTo(9,9)", "stroke()"]); |
| 103 | }); |
| 104 | |
| 105 | it("falls back to a dot on the first point of a stroke", () => { |
| 106 | const c = stubContext(); |
| 107 | paintTip(c, stroke([{ x: 3, y: 4 }])); |
| 108 | expect(c.calls).toContain("fill()"); |
| 109 | }); |
| 110 | }); |
| 111 | |
| 112 | describe("parseScene", () => { |
| 113 | const scene: DrawScene = { v: 1, w: 100, h: 80, strokes: [stroke([{ x: 1, y: 2 }, { x: 3, y: 4 }])] }; |
| 114 | |
| 115 | it("parses a valid scene", () => { |
| 116 | expect(parseScene(JSON.stringify(scene))).toEqual(scene); |
| 117 | }); |
| 118 | |
| 119 | it("rejects a wrong version", () => { |
| 120 | expect(parseScene(JSON.stringify({ ...scene, v: 2 }))).toBeNull(); |
| 121 | }); |
| 122 | |
| 123 | // w/h size the playback canvas and drive paintPaper's per-cell loop, from a PNG anyone |
| 124 | // can upload: a real export can be big, but never non-finite, negative or past MAX_SIDE. |
| 125 | it("accepts a large-but-real box, rejects non-finite, non-positive and absurd sides", () => { |
| 126 | expect(parseScene(JSON.stringify({ ...scene, w: 8000, h: 16384 }))).not.toBeNull(); |
| 127 | // 1e999 is valid JSON that parses to Infinity; null is what JSON.stringify makes of NaN. |
| 128 | for (const bad of ["1e999", "-1", "0", "1e9", "null", '"10"']) { |
| 129 | expect(parseScene(`{"v":1,"w":${bad},"h":10,"strokes":[]}`)).toBeNull(); |
| 130 | } |
| 131 | }); |
| 132 | |
| 133 | it("rejects a malformed stroke shape", () => { |
| 134 | expect(parseScene(JSON.stringify({ v: 1, w: 1, h: 1, strokes: [{ color: "#000" }] }))).toBeNull(); |
| 135 | }); |
| 136 | |
| 137 | it("rejects garbage and null", () => { |
| 138 | expect(parseScene("not json")).toBeNull(); |
| 139 | expect(parseScene(null)).toBeNull(); |
| 140 | }); |
| 141 | |
| 142 | // dpr multiplies the playback canvas allocation, from a PNG anyone can upload: it must |
| 143 | // survive the round trip, stay optional for drawings saved before it existed, and never |
| 144 | // arrive as something unusable or absurd. |
| 145 | it("round-trips dpr, tolerates its absence, rejects bogus or out-of-range ones", () => { |
| 146 | expect(parseScene(JSON.stringify({ ...scene, dpr: 2 }))?.dpr).toBe(2); |
| 147 | expect(parseScene(JSON.stringify({ ...scene, dpr: 0.5 }))?.dpr).toBe(0.5); // zoomed-out export |
| 148 | expect(parseScene(JSON.stringify(scene))?.dpr).toBeUndefined(); |
| 149 | expect(parseScene(JSON.stringify({ ...scene, dpr: 0 }))).toBeNull(); |
| 150 | expect(parseScene(JSON.stringify({ ...scene, dpr: "2" }))).toBeNull(); |
| 151 | expect(parseScene(JSON.stringify({ ...scene, dpr: 1e9 }))).toBeNull(); // tab-killer allocation |
| 152 | }); |
| 153 | }); |
| 154 | |
| 155 | describe("strokesBounds", () => { |
| 156 | it("is null with nothing drawn", () => { |
| 157 | expect(strokesBounds([])).toBeNull(); |
| 158 | }); |
| 159 | |
| 160 | it("wraps every point and pads by half the stroke width", () => { |
| 161 | const b = strokesBounds([ |
| 162 | stroke([{ x: 10, y: 10 }, { x: 30, y: 20 }], { width: 4 }), // half-width 2 |
| 163 | stroke([{ x: 50, y: 40 }], { width: 10 }), // half-width 5 |
| 164 | ]); |
| 165 | // minX 10-2=8, minY 10-2=8, maxX 50+5=55, maxY 40+5=45 |
| 166 | expect(b).toEqual({ x: 8, y: 8, w: 47, h: 37 }); |
| 167 | }); |
| 168 | |
| 169 | it("ignores eraser strokes so a far erase doesn't pad the export box", () => { |
| 170 | const b = strokesBounds([ |
| 171 | stroke([{ x: 10, y: 10 }, { x: 30, y: 20 }], { width: 4 }), // pen, half-width 2 |
| 172 | stroke([{ x: 500, y: 500 }], { width: 10, erase: true }), // far eraser — must not count |
| 173 | ]); |
| 174 | // same box as the pen stroke alone: minX 10-2=8, minY 10-2=8, maxX 30+2=32, maxY 20+2=22 |
| 175 | expect(b).toEqual({ x: 8, y: 8, w: 24, h: 14 }); |
| 176 | }); |
| 177 | |
| 178 | it("is null when the scene has only eraser strokes", () => { |
| 179 | expect(strokesBounds([stroke([{ x: 5, y: 5 }], { erase: true })])).toBeNull(); |
| 180 | }); |
| 181 | }); |
| 182 | |
| 183 | describe("sliceStrokes", () => { |
| 184 | const a = stroke([{ x: 0, y: 0 }, { x: 1, y: 1 }, { x: 2, y: 2 }]); // 3 points |
| 185 | const b = stroke([{ x: 5, y: 5 }, { x: 6, y: 6 }, { x: 7, y: 7 }, { x: 8, y: 8 }]); // 4 points |
| 186 | |
| 187 | it("reveals nothing at count 0", () => { |
| 188 | expect(sliceStrokes([a, b], 0)).toEqual({ full: [], partial: null }); |
| 189 | }); |
| 190 | |
| 191 | it("reveals every stroke, no partial, at the total point count", () => { |
| 192 | expect(sliceStrokes([a, b], 7)).toEqual({ full: [a, b], partial: null }); |
| 193 | }); |
| 194 | |
| 195 | it("clamps past the total to the whole drawing", () => { |
| 196 | expect(sliceStrokes([a, b], 999)).toEqual({ full: [a, b], partial: null }); |
| 197 | }); |
| 198 | |
| 199 | it("truncates the stroke the cut lands inside", () => { |
| 200 | const { full, partial } = sliceStrokes([a, b], 5); // a whole (3) + first 2 of b |
| 201 | expect(full).toEqual([a]); |
| 202 | expect(partial).toEqual({ ...b, points: b.points.slice(0, 2) }); |
| 203 | }); |
| 204 | |
| 205 | it("gives no partial when the cut sits on a stroke boundary", () => { |
| 206 | expect(sliceStrokes([a, b], 3)).toEqual({ full: [a], partial: null }); // a done, b not started |
| 207 | }); |
| 208 | |
| 209 | it("handles a single-point stroke on both sides of its one point", () => { |
| 210 | const dot = stroke([{ x: 9, y: 9 }]); |
| 211 | expect(sliceStrokes([dot], 0)).toEqual({ full: [], partial: null }); |
| 212 | expect(sliceStrokes([dot], 1)).toEqual({ full: [dot], partial: null }); |
| 213 | }); |
| 214 | |
| 215 | it("is empty for no strokes", () => { |
| 216 | expect(sliceStrokes([], 0)).toEqual({ full: [], partial: null }); |
| 217 | expect(sliceStrokes([], 5)).toEqual({ full: [], partial: null }); |
| 218 | }); |
| 219 | }); |
| 220 | |
| 221 | describe("paintPaper", () => { |
| 222 | it("draws nothing for none", () => { |
| 223 | const c = stubContext(); |
| 224 | paintPaper(c, "none", 100, 100, 24); |
| 225 | expect(c.calls).toEqual([]); |
| 226 | }); |
| 227 | |
| 228 | it("draws only horizontal rules for lines", () => { |
| 229 | const c = stubContext(); |
| 230 | paintPaper(c, "lines", 100, 100, 24); |
| 231 | const moves = c.calls.filter((s) => s.startsWith("moveTo")); |
| 232 | expect(moves.length).toBeGreaterThan(0); |
| 233 | // every rule starts at the left edge (x=0) — no vertical segments |
| 234 | expect(moves.every((s) => s.startsWith("moveTo(0,"))).toBe(true); |
| 235 | expect(c.calls).toContain("stroke()"); |
| 236 | }); |
| 237 | |
| 238 | it("draws both horizontal and vertical lines for grid", () => { |
| 239 | const c = stubContext(); |
| 240 | paintPaper(c, "grid", 100, 100, 24); |
| 241 | const moves = c.calls.filter((s) => s.startsWith("moveTo")); |
| 242 | const horizontal = moves.filter((s) => s.startsWith("moveTo(0,")); |
| 243 | const vertical = moves.filter((s) => !s.startsWith("moveTo(0,")); |
| 244 | expect(horizontal.length).toBeGreaterThan(0); |
| 245 | expect(vertical.length).toBeGreaterThan(0); |
| 246 | }); |
| 247 | }); |
| 248 | |
| 249 | // canvasPx is the one spot both the export (draw-canvas's renderBounds) and playback |
| 250 | // (drawing-playback's onMount) canvases round a world-px side into device px — if the |
| 251 | // two ever computed this differently, the replay would render at a different on-screen |
| 252 | // size than the static <img> it swaps in for. |
| 253 | describe("canvasPx", () => { |
| 254 | it("rounds world px * dpr to the nearest device px across export- and playback-typical dprs", () => { |
| 255 | for (const side of [1, 16.5, 100.4, 123.7, 8000]) { |
| 256 | for (const dpr of [0.5, 1, 2, 3]) { |
| 257 | expect(canvasPx(side, dpr)).toBe(Math.max(1, Math.round(side * dpr))); |
| 258 | } |
| 259 | } |
| 260 | }); |
| 261 | |
| 262 | it("floors at 1 device px so a tiny side at a sub-1 dpr never yields a 0-sized canvas", () => { |
| 263 | expect(canvasPx(0.5, 0.5)).toBe(1); // 0.25 rounds to 0, which a canvas can't allocate |
| 264 | }); |
| 265 | |
| 266 | // drawing-playback's onMount resolves `scene.dpr ?? window.devicePixelRatio` before |
| 267 | // calling canvasPx — mirrored here since onMount itself needs a real DOM mount, which |
| 268 | // this repo has no headless (jsdom-free) way to drive. |
| 269 | it("mirrors the playback side's dpr fallback: a scene without dpr uses the viewer's ratio", () => { |
| 270 | const viewerRatio = 3; |
| 271 | const missingDpr: number | undefined = undefined; |
| 272 | const savedDpr: number | undefined = 2; |
| 273 | expect(canvasPx(100, missingDpr ?? viewerRatio)).toBe(canvasPx(100, viewerRatio)); |
| 274 | expect(canvasPx(100, savedDpr ?? viewerRatio)).toBe(canvasPx(100, 2)); // a saved dpr wins over the fallback |
| 275 | }); |
| 276 | }); |
| 277 | |
| 278 | describe("playableScene", () => { |
| 279 | const scene: DrawScene = { v: 1, w: 10, h: 10, strokes: [stroke([{ x: 0, y: 0 }, { x: 1, y: 1 }])] }; |
| 280 | |
| 281 | it("passes through a scene with at least one stroke — the playback branch", () => { |
| 282 | expect(playableScene(scene)).toBe(scene); |
| 283 | }); |
| 284 | |
| 285 | it("is null for a scene with zero strokes — an emptied drawing falls back to the static image", () => { |
| 286 | expect(playableScene({ ...scene, strokes: [] })).toBeNull(); |
| 287 | }); |
| 288 | |
| 289 | it("is null for null — a plain photo with nothing embedded", () => { |
| 290 | expect(playableScene(null)).toBeNull(); |
| 291 | }); |
| 292 | }); |