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 { |
| 3 | decodeSegment, |
| 4 | encodeEraseMarkers, |
| 5 | encodeStrokeSegments, |
| 6 | reassembleStrokes, |
| 7 | segmentToStroke, |
| 8 | } from "./board-stroke-codec"; |
| 9 | import type { Stroke } from "./draw-stroke"; |
| 10 | |
| 11 | function makeStroke(pointCount: number): Stroke { |
| 12 | const points = Array.from({ length: pointCount }, (_, i) => ({ x: i * 1.234, y: i * 5.678 })); |
| 13 | return { color: "#ff0000", width: 3, erase: false, points }; |
| 14 | } |
| 15 | |
| 16 | describe("board-stroke-codec", () => { |
| 17 | it("encodes a short stroke into a single segment", () => { |
| 18 | const payloads = encodeStrokeSegments(makeStroke(5), "s1"); |
| 19 | expect(payloads).toHaveLength(1); |
| 20 | const seg = decodeSegment(payloads[0]); |
| 21 | expect(seg).not.toBeNull(); |
| 22 | expect(seg?.sid).toBe("s1"); |
| 23 | expect(seg?.seg).toBe(0); |
| 24 | expect(seg?.last).toBe(true); |
| 25 | expect(seg?.pts).toHaveLength(5); |
| 26 | }); |
| 27 | |
| 28 | it("returns no segments for an empty stroke", () => { |
| 29 | expect(encodeStrokeSegments(makeStroke(0), "s1")).toEqual([]); |
| 30 | }); |
| 31 | |
| 32 | it("splits a long stroke into multiple capped segments", () => { |
| 33 | const payloads = encodeStrokeSegments(makeStroke(2000), "s2", 512); |
| 34 | expect(payloads.length).toBeGreaterThan(1); |
| 35 | for (const p of payloads) { |
| 36 | expect(new TextEncoder().encode(p).length).toBeLessThanOrEqual(512); |
| 37 | } |
| 38 | // exactly one final segment |
| 39 | const lasts = payloads.map((p) => decodeSegment(p)!.last).filter(Boolean); |
| 40 | expect(lasts).toHaveLength(1); |
| 41 | expect(decodeSegment(payloads[payloads.length - 1])!.last).toBe(true); |
| 42 | }); |
| 43 | |
| 44 | it("reassembles segments back into the original stroke (no duplicate overlap points)", () => { |
| 45 | const original = makeStroke(2000); |
| 46 | const payloads = encodeStrokeSegments(original, "s3", 512); |
| 47 | const [rebuilt] = reassembleStrokes(payloads); |
| 48 | expect(rebuilt.points).toHaveLength(original.points.length); |
| 49 | expect(rebuilt.color).toBe(original.color); |
| 50 | expect(rebuilt.width).toBe(original.width); |
| 51 | // rounded to 2dp, so compare with tolerance |
| 52 | expect(rebuilt.points[0].x).toBeCloseTo(original.points[0].x, 2); |
| 53 | expect(rebuilt.points[1999].y).toBeCloseTo(original.points[1999].y, 2); |
| 54 | }); |
| 55 | |
| 56 | it("keeps distinct sids as distinct strokes, in order", () => { |
| 57 | const a = encodeStrokeSegments({ ...makeStroke(3), color: "#111111" }, "a"); |
| 58 | const b = encodeStrokeSegments({ ...makeStroke(3), color: "#222222" }, "b"); |
| 59 | const strokes = reassembleStrokes([...a, ...b]); |
| 60 | expect(strokes).toHaveLength(2); |
| 61 | expect(strokes[0].color).toBe("#111111"); |
| 62 | expect(strokes[1].color).toBe("#222222"); |
| 63 | }); |
| 64 | |
| 65 | it("segmentToStroke paints a single segment's own points", () => { |
| 66 | const [payload] = encodeStrokeSegments(makeStroke(4), "s4"); |
| 67 | const stroke = segmentToStroke(decodeSegment(payload)!); |
| 68 | expect(stroke.points).toHaveLength(4); |
| 69 | expect(stroke.erase).toBe(false); |
| 70 | }); |
| 71 | |
| 72 | it("decodeSegment rejects malformed payloads", () => { |
| 73 | expect(decodeSegment(null)).toBeNull(); |
| 74 | expect(decodeSegment("not json")).toBeNull(); |
| 75 | expect(decodeSegment(JSON.stringify({ v: 2, sid: "x" }))).toBeNull(); |
| 76 | expect(decodeSegment(JSON.stringify({ v: 1, sid: "x", seg: 0, last: true, color: "#000000", width: 1, erase: false, pts: [[1]] }))).toBeNull(); |
| 77 | }); |
| 78 | |
| 79 | it("skips non-stroke rows during reassembly", () => { |
| 80 | const good = encodeStrokeSegments(makeStroke(2), "g"); |
| 81 | const strokes = reassembleStrokes([null, "garbage", ...good]); |
| 82 | expect(strokes).toHaveLength(1); |
| 83 | }); |
| 84 | |
| 85 | it("round-trips a dash pattern and leaves a solid payload without one", () => { |
| 86 | const dashed = { ...makeStroke(300), dash: "dashed" as const }; |
| 87 | const payloads = encodeStrokeSegments(dashed, "d", 512); |
| 88 | expect(payloads.length).toBeGreaterThan(1); |
| 89 | expect(payloads.every((p) => decodeSegment(p)?.dash === "dashed")).toBe(true); |
| 90 | expect(reassembleStrokes(payloads)[0].dash).toBe("dashed"); |
| 91 | expect(segmentToStroke(decodeSegment(payloads[0])!).dash).toBe("dashed"); |
| 92 | expect(encodeStrokeSegments(makeStroke(2), "s")[0]).not.toContain("dash"); |
| 93 | }); |
| 94 | |
| 95 | it("rejects an unknown dash pattern", () => { |
| 96 | const payload = JSON.parse(encodeStrokeSegments(makeStroke(2), "x")[0]); |
| 97 | expect(decodeSegment(JSON.stringify({ ...payload, dash: "wavy" }))).toBeNull(); |
| 98 | }); |
| 99 | |
| 100 | it("encodes an erase as ink-free markers split under the cap", () => { |
| 101 | const ids = Array.from({ length: 60 }, (_, i) => `user-${i}-1700000000000-${i}`); |
| 102 | const payloads = encodeEraseMarkers(ids, "e", 512); |
| 103 | expect(payloads.length).toBeGreaterThan(1); |
| 104 | const all: string[] = []; |
| 105 | for (const p of payloads) { |
| 106 | expect(new TextEncoder().encode(p).length).toBeLessThanOrEqual(512); |
| 107 | const seg = decodeSegment(p)!; |
| 108 | expect(seg.pts).toEqual([]); |
| 109 | all.push(...seg.del!); |
| 110 | } |
| 111 | expect(all).toEqual(ids); |
| 112 | }); |
| 113 | |
| 114 | it("drops erased strokes from a reassembled history", () => { |
| 115 | const keep = encodeStrokeSegments(makeStroke(3), "keep"); |
| 116 | const gone = encodeStrokeSegments(makeStroke(300), "gone", 512); |
| 117 | const strokes = reassembleStrokes([...gone, ...keep, ...encodeEraseMarkers(["gone"], "e")]); |
| 118 | expect(strokes).toHaveLength(1); |
| 119 | expect(strokes[0].points).toHaveLength(3); |
| 120 | }); |
| 121 | |
| 122 | it("rejects a marker whose del list is not strings", () => { |
| 123 | const payload = JSON.parse(encodeEraseMarkers(["a"], "e")[0]); |
| 124 | expect(decodeSegment(JSON.stringify({ ...payload, del: [1] }))).toBeNull(); |
| 125 | }); |
| 126 | }); |