hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import type { DrawScene } from "./draw-stroke"; |
| 3 | import { embedPngText, extractPngText } from "./png-meta"; |
| 4 | |
| 5 | const SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]; |
| 6 | |
| 7 | /** Structurally valid (chunk-walkable) 1x1 PNG: signature + IHDR + IEND. CRCs are zeroed — embed/extract never validate them. */ |
| 8 | function minimalPng(): Uint8Array { |
| 9 | const ihdrData = [0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0, 0, 0]; // 1x1, 8-bit, RGBA |
| 10 | return new Uint8Array([ |
| 11 | ...SIGNATURE, |
| 12 | 0, 0, 0, 13, 0x49, 0x48, 0x44, 0x52, ...ihdrData, 0, 0, 0, 0, // IHDR |
| 13 | 0, 0, 0, 0, 0x49, 0x45, 0x4e, 0x44, 0, 0, 0, 0, // IEND |
| 14 | ]); |
| 15 | } |
| 16 | |
| 17 | /** Reference CRC-32, pinned below to the spec's known IEND constant so it can vouch for embed's CRC. */ |
| 18 | function refCrc32(bytes: Uint8Array): number { |
| 19 | let crc = 0xffffffff; |
| 20 | for (let i = 0; i < bytes.length; i += 1) { |
| 21 | crc ^= bytes[i]; |
| 22 | for (let bit = 0; bit < 8; bit += 1) crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1)); |
| 23 | } |
| 24 | return (crc ^ 0xffffffff) >>> 0; |
| 25 | } |
| 26 | |
| 27 | const ascii = (s: string) => new Uint8Array([...s].map((c) => c.charCodeAt(0))); |
| 28 | |
| 29 | describe("png-meta", () => { |
| 30 | it("round-trips embed -> extract", () => { |
| 31 | const png = embedPngText(minimalPng(), "hezarfen-drawing", '{"v":1}'); |
| 32 | expect(extractPngText(png, "hezarfen-drawing")).toBe('{"v":1}'); |
| 33 | }); |
| 34 | |
| 35 | it("returns null for a wrong keyword", () => { |
| 36 | const png = embedPngText(minimalPng(), "hezarfen-drawing", "payload"); |
| 37 | expect(extractPngText(png, "other")).toBeNull(); |
| 38 | }); |
| 39 | |
| 40 | it("returns null when no tEXt chunk is present", () => { |
| 41 | expect(extractPngText(minimalPng(), "hezarfen-drawing")).toBeNull(); |
| 42 | }); |
| 43 | |
| 44 | it("inserts the tEXt chunk right after IHDR", () => { |
| 45 | const png = embedPngText(minimalPng(), "k", "v"); |
| 46 | // signature(8) + IHDR chunk(4+4+13+4 = 25) => next chunk type starts at 33+4. |
| 47 | const type = String.fromCharCode(png[37], png[38], png[39], png[40]); |
| 48 | expect(type).toBe("tEXt"); |
| 49 | }); |
| 50 | |
| 51 | it("writes a spec-correct CRC over chunk type + data", () => { |
| 52 | // Anchor the reference implementation to the spec's fixed IEND CRC. |
| 53 | expect(refCrc32(ascii("IEND"))).toBe(0xae426082); |
| 54 | |
| 55 | const png = embedPngText(minimalPng(), "kw", "hi"); |
| 56 | // tEXt data = "kw" + NUL + "hi" (5 bytes); length field sits at offset 33. |
| 57 | const dataLen = ((png[33] << 24) | (png[34] << 16) | (png[35] << 8) | png[36]) >>> 0; |
| 58 | expect(dataLen).toBe(5); |
| 59 | const typeAndData = png.subarray(37, 37 + 4 + dataLen); // "tEXt" + data |
| 60 | const crcAt = 37 + 4 + dataLen; |
| 61 | const embedded = ((png[crcAt] << 24) | (png[crcAt + 1] << 16) | (png[crcAt + 2] << 8) | png[crcAt + 3]) >>> 0; |
| 62 | expect(embedded).toBe(refCrc32(typeAndData)); |
| 63 | }); |
| 64 | |
| 65 | it("round-trips a full drawing scene's JSON — strokes, points, colour, width, erase, bg, w/h, dpr — byte-for-byte", () => { |
| 66 | const scene: DrawScene = { |
| 67 | v: 1, |
| 68 | w: 120, |
| 69 | h: 80, |
| 70 | bg: "grid", |
| 71 | dpr: 2, |
| 72 | strokes: [ |
| 73 | { color: "#1f2937", width: 6, erase: false, points: [{ x: 1, y: 2 }, { x: 3.5, y: 4 }] }, |
| 74 | { color: "#dc2626", width: 14, erase: true, points: [{ x: 10, y: 10 }] }, |
| 75 | ], |
| 76 | }; |
| 77 | const json = JSON.stringify(scene); |
| 78 | const png = embedPngText(minimalPng(), "hezarfen-drawing", json); |
| 79 | expect(extractPngText(png, "hezarfen-drawing")).toBe(json); |
| 80 | expect(JSON.parse(extractPngText(png, "hezarfen-drawing")!)).toEqual(scene); |
| 81 | }); |
| 82 | }); |