gitflarePublicPartly agent-ready50
cf-durable-whiteboard
Realtime collaborative whiteboard on Cloudflare Durable Objects: presence cursors, WebSocket hibernation, SQLite storage, undo, PNG export.
- LicenseApache-2.0
- LanguageTypeScript
- Last landed
6eb4fbdOct 20, 2026
Keep board zoom and pan stable across reloads
6eb4fbdimport { describe, it, expect } from "vitest";// @ts-ignore plain JS modulesimport * as V from "../public/viewport.js";// @ts-ignoreimport * as P from "../public/protocol.js"; describe("viewport", () => { it("round-trips coordinates", () => { const v = { x: 30, y: -10, k: 2 }; const w = V.toWorld(v, 100, 50); expect(V.toScreen(v, w.x, w.y)).toEqual({ x: 100, y: 50 }); }); it("zooms around the cursor and clamps", () => { const v = V.zoomAt({ x: 0, y: 0, k: 1 }, 100, 100, 2); expect(V.toWorld(v, 100, 100)).toEqual({ x: 100, y: 100 }); expect(V.zoomAt(v, 0, 0, 1000).k).toBe(4); expect(V.zoomAt(v, 0, 0, 0.0001).k).toBe(0.1); }); it("hit tests and bounds", () => { const r = { id: "a", kind: "rect", x: 0, y: 0, w: 10, h: 10 }; expect(V.hitTest(r, 5, 5)).toBe(true); expect(V.hitTest(r, 50, 5)).toBe(false); const ar = { id: "b", kind: "arrow", x: 0, y: 0, w: 100, h: 0 }; expect(V.hitTest(ar, 50, 3)).toBe(true); expect(V.allBounds([r, ar])).toEqual({ x: 0, y: 0, w: 100, h: 10 }); expect(V.allBounds([])).toBeNull(); expect(V.translate({ id: "p", kind: "pen", points: [0, 0, 1, 1] }, 2, 3).points).toEqual([2, 3, 3, 4]); }); it("fits a view", () => { expect(V.fitView({ x: 0, y: 0, w: 100, h: 100 }, 200, 200, 0).k).toBe(2); });}); describe("protocol", () => { it("makes valid board ids", () => { const id = P.newBoardId(); expect(id).toMatch(/^[abcdefghjkmnpqrstuvwxyz23456789]{8}$/); expect(P.isBoardId(id)).toBe(true); expect(P.isBoardId("../x")).toBe(false); }); it("applies ops", () => { const m = new Map(); P.applyOp(m, { type: "batch", ops: [{ type: "add", shape: { id: "a" } }, { type: "add", shape: { id: "b" } }] }); P.applyOp(m, { type: "delete", id: "a" }); expect([...m.keys()]).toEqual(["b"]); P.applyOp(m, { type: "clear" }); expect(m.size).toBe(0); }); it("backs off with a cap", () => { expect(P.backoff(0, () => 0)).toBe(250); expect(P.backoff(20, () => 1)).toBe(15000); }); it("labels, parses, stores", () => { expect(P.peopleLabel(1)).toBe("1 person here"); expect(P.peopleLabel(3)).toBe("3 people here"); expect(P.parseServer("nope")).toBeNull(); expect(P.parseServer('{"type":"x"}')).toEqual({ type: "x" }); const s: Record<string, string> = {}; const store = { getItem: (k: string) => s[k] ?? null, setItem: (k: string, v: string) => void (s[k] = v) }; P.pushRecent(store, "abcd2345"); P.pushRecent(store, "bbbb2345"); P.pushRecent(store, "abcd2345"); expect(P.loadRecent(store)).toEqual(["abcd2345", "bbbb2345"]); expect(P.initials("Ada Lovelace")).toBe("AL"); });});