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 { |
| 3 | decodeSegment, |
| 4 | encodeStrokeSegments, |
| 5 | reassembleStrokes, |
| 6 | segmentToStroke, |
| 7 | } from "./board-stroke-codec"; |
| 8 | import type { Stroke } from "./draw-stroke"; |
| 9 | |
| 10 | function makeStroke(pointCount: number): Stroke { |
| 11 | const points = Array.from({ length: pointCount }, (_, i) => ({ x: i * 1.234, y: i * 5.678 })); |
| 12 | return { color: "#ff0000", width: 3, erase: false, points }; |
| 13 | } |
| 14 | |
| 15 | describe("board-stroke-codec", () => { |
| 16 | it("encodes a short stroke into a single segment", () => { |
| 17 | const payloads = encodeStrokeSegments(makeStroke(5), "s1"); |
| 18 | expect(payloads).toHaveLength(1); |
| 19 | const seg = decodeSegment(payloads[0]); |
| 20 | expect(seg).not.toBeNull(); |
| 21 | expect(seg?.sid).toBe("s1"); |
| 22 | expect(seg?.seg).toBe(0); |
| 23 | expect(seg?.last).toBe(true); |
| 24 | expect(seg?.pts).toHaveLength(5); |
| 25 | }); |
| 26 | |
| 27 | it("returns no segments for an empty stroke", () => { |
| 28 | expect(encodeStrokeSegments(makeStroke(0), "s1")).toEqual([]); |
| 29 | }); |
| 30 | |
| 31 | it("splits a long stroke into multiple capped segments", () => { |
| 32 | const payloads = encodeStrokeSegments(makeStroke(2000), "s2", 512); |
| 33 | expect(payloads.length).toBeGreaterThan(1); |
| 34 | for (const p of payloads) { |
| 35 | expect(new TextEncoder().encode(p).length).toBeLessThanOrEqual(512); |
| 36 | } |
| 37 | // exactly one final segment |
| 38 | const lasts = payloads.map((p) => decodeSegment(p)!.last).filter(Boolean); |
| 39 | expect(lasts).toHaveLength(1); |
| 40 | expect(decodeSegment(payloads[payloads.length - 1])!.last).toBe(true); |
| 41 | }); |
| 42 | |
| 43 | it("reassembles segments back into the original stroke (no duplicate overlap points)", () => { |
| 44 | const original = makeStroke(2000); |
| 45 | const payloads = encodeStrokeSegments(original, "s3", 512); |
| 46 | const [rebuilt] = reassembleStrokes(payloads); |
| 47 | expect(rebuilt.points).toHaveLength(original.points.length); |
| 48 | expect(rebuilt.color).toBe(original.color); |
| 49 | expect(rebuilt.width).toBe(original.width); |
| 50 | // rounded to 2dp, so compare with tolerance |
| 51 | expect(rebuilt.points[0].x).toBeCloseTo(original.points[0].x, 2); |
| 52 | expect(rebuilt.points[1999].y).toBeCloseTo(original.points[1999].y, 2); |
| 53 | }); |
| 54 | |
| 55 | it("keeps distinct sids as distinct strokes, in order", () => { |
| 56 | const a = encodeStrokeSegments({ ...makeStroke(3), color: "#111111" }, "a"); |
| 57 | const b = encodeStrokeSegments({ ...makeStroke(3), color: "#222222" }, "b"); |
| 58 | const strokes = reassembleStrokes([...a, ...b]); |
| 59 | expect(strokes).toHaveLength(2); |
| 60 | expect(strokes[0].color).toBe("#111111"); |
| 61 | expect(strokes[1].color).toBe("#222222"); |
| 62 | }); |
| 63 | |
| 64 | it("segmentToStroke paints a single segment's own points", () => { |
| 65 | const [payload] = encodeStrokeSegments(makeStroke(4), "s4"); |
| 66 | const stroke = segmentToStroke(decodeSegment(payload)!); |
| 67 | expect(stroke.points).toHaveLength(4); |
| 68 | expect(stroke.erase).toBe(false); |
| 69 | }); |
| 70 | |
| 71 | it("decodeSegment rejects malformed payloads", () => { |
| 72 | expect(decodeSegment(null)).toBeNull(); |
| 73 | expect(decodeSegment("not json")).toBeNull(); |
| 74 | expect(decodeSegment(JSON.stringify({ v: 2, sid: "x" }))).toBeNull(); |
| 75 | expect(decodeSegment(JSON.stringify({ v: 1, sid: "x", seg: 0, last: true, color: "#000000", width: 1, erase: false, pts: [[1]] }))).toBeNull(); |
| 76 | }); |
| 77 | |
| 78 | it("skips non-stroke rows during reassembly", () => { |
| 79 | const good = encodeStrokeSegments(makeStroke(2), "g"); |
| 80 | const strokes = reassembleStrokes([null, "garbage", ...good]); |
| 81 | expect(strokes).toHaveLength(1); |
| 82 | }); |
| 83 | }); |