forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

drawing-file.test.ts42 lines, 1.6 KiBHistoryRawDownload
1import { describe, expect, it } from "vitest";
2import type { DrawScene } from "./draw-stroke";
3import { pngBytesToScene } from "./drawing-file";
4import { embedPngText } from "./png-meta";
5
6const SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
7
8/** Chunk-walkable 1x1 PNG (signature + IHDR + IEND); enough carrier for embed/extract. */
9function minimalPng(): Uint8Array {
10 const ihdr = [0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0, 0, 0];
11 return new Uint8Array([
12 ...SIGNATURE,
13 0, 0, 0, 13, 0x49, 0x48, 0x44, 0x52, ...ihdr, 0, 0, 0, 0,
14 0, 0, 0, 0, 0x49, 0x45, 0x4e, 0x44, 0, 0, 0, 0,
15 ]);
16}
17
18describe("pngBytesToScene", () => {
19 it("round-trips a scene carried in the PNG's tEXt chunk, every field intact", () => {
20 const scene: DrawScene = {
21 v: 1,
22 w: 120,
23 h: 90,
24 bg: "grid",
25 dpr: 2,
26 strokes: [
27 { color: "#1f2937", width: 6, erase: false, points: [{ x: 1, y: 2 }, { x: 3, y: 4 }] },
28 { color: "#dc2626", width: 14, erase: true, points: [{ x: 10, y: 10 }] },
29 ],
30 };
31 // "hezarfen-drawing" here must match drawing-file.ts's private DRAWING_KEYWORD
32 // constant exactly — it's a wire format shared with every already-saved
33 // .hzdraw.png. If pngBytesToScene's own keyword ever drifted from this literal,
34 // extractPngText would find no chunk under it and this would come back null.
35 const png = embedPngText(minimalPng(), "hezarfen-drawing", JSON.stringify(scene));
36 expect(pngBytesToScene(png)).toEqual(scene);
37 });
38
39 it("returns null for a plain PNG with no embedded scene", () => {
40 expect(pngBytesToScene(minimalPng())).toBeNull();
41 });
42});