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";import { applyOp, UndoStacks, type Shapes } from "../src/lib/ops";import { parseClientMessage, validateShape } from "../src/lib/validate";import { TokenBucket } from "../src/lib/ratelimit";import { randomId, isBoardId } from "../src/lib/ids";import type { Shape } from "../src/lib/validate"; const r = (id: string): Shape => ({ id, kind: "rect", x: 0, y: 0, w: 5, h: 5 }); describe("ops", () => { it("add/delete inverse roundtrip", () => { const s: Shapes = new Map(); const inv = applyOp(s, { type: "add", shape: r("a") })!; expect(s.size).toBe(1); applyOp(s, inv); expect(s.size).toBe(0); }); it("clear is undoable", () => { const s: Shapes = new Map([["a", r("a")], ["b", r("b")]]); const inv = applyOp(s, { type: "clear" })!; expect(s.size).toBe(0); applyOp(s, inv); expect(s.size).toBe(2); }); it("update of missing is null", () => { expect(applyOp(new Map(), { type: "update", shape: r("x") })).toBeNull(); }); it("undo/redo stacks", () => { const s: Shapes = new Map(); const u = new UndoStacks(); const op = { type: "add" as const, shape: r("a") }; u.record("me", { op, inverse: applyOp(s, op)! }); applyOp(s, u.popUndo("me")!.op); expect(s.size).toBe(0); applyOp(s, u.popRedo("me")!.op); expect(s.size).toBe(1); expect(u.popRedo("me")).toBeNull(); });});describe("validate", () => { it("shapes", () => { expect(validateShape(r("a"))).toBe(true); expect(validateShape({ id: "a", kind: "pen", points: [1] })).toBe(false); expect(validateShape({ id: "a", kind: "bad" })).toBe(false); }); it("messages", () => { expect(parseClientMessage('{"type":"cursor","x":1,"y":2}')).toEqual({ type: "cursor", x: 1, y: 2 }); expect(parseClientMessage("nope")).toBeNull(); expect(parseClientMessage('{"type":"op","op":{"type":"clear"}}')).not.toBeNull(); expect(parseClientMessage(JSON.stringify({ type: "hello", name: "x".repeat(40000) }))).toBeNull(); });});describe("ratelimit", () => { it("bursts then refills", () => { const b = new TokenBucket(60, 120, 0); for (let i = 0; i < 120; i++) expect(b.take(0)).toBe(true); expect(b.take(0)).toBe(false); expect(b.take(1000)).toBe(true); });});describe("ids", () => { it("generates valid ids", () => { expect(isBoardId(randomId())).toBe(true); expect(isBoardId("abc")).toBe(false); });});