Skip to content

gitflarePublicPartly agent-ready50

cf-durable-whiteboard

Realtime collaborative whiteboard on Cloudflare Durable Objects: presence cursors, WebSocket hibernation, SQLite storage, undo, PNG export.

Keep board zoom and pan stable across reloads6eb4fbd
TypeScript45 lines2.1 KB
import { describe, it, expect, vi } from "vitest"; vi.mock("cloudflare:workers", () => ({ DurableObject: class { constructor(public ctx: any, public env: any) {} } }));const { default: worker, withSecurity, jsonError } = await import("../src/index"); const env = { ASSETS: { fetch: async () => new Response("asset") }, BOARD: {} } as any; describe("worker", () => {  it("health with security headers", async () => {    const r = await worker.fetch(new Request("https://x/health"), env);    expect(r.status).toBe(200);    expect(r.headers.get("X-Content-Type-Options")).toBe("nosniff");  });  it("bad ws id gives JSON error", async () => {    const r = await worker.fetch(new Request("https://x/ws/bad"), env);    expect(r.status).toBe(400);    expect(((await r.json()) as any).error.code).toBe("bad_board_id");  });  it("unknown api 404", async () => {    expect((await worker.fetch(new Request("https://x/api/nope"), env)).status).toBe(404);  });  it("helpers", () => {    expect(jsonError(418, "a", "b").status).toBe(418);    expect(withSecurity(new Response("x")).headers.get("X-Frame-Options")).toBe("DENY");  });}); describe("Board protocol (fake)", () => {  it("rejects invalid message and relays cursor", async () => {    const { Board } = await import("../src/board");    const rows: any[] = [];    const sql = { exec: () => Object.assign([], { one: () => ({ s: 0, c: 0, u: 0 }) }) };    const sent: string[][] = [[], []];    const mk = (i: number) => ({ att: { userId: "u" + i, name: "n", colour: "#fff" }, send: (s: string) => sent[i].push(s), close() {}, deserializeAttachment() { return this.att; }, serializeAttachment(a: any) { this.att = a; } });    const a = mk(0), b = mk(1);    const ctx = { storage: { sql }, getWebSockets: () => [a, b] } as any;    const board = new (Board as any)(ctx, {});    board.webSocketMessage(a, "nope");    expect(JSON.parse(sent[0][0]).type).toBe("error");    board.webSocketMessage(a, JSON.stringify({ type: "cursor", x: 1, y: 2 }));    expect(JSON.parse(sent[1][0])).toEqual({ type: "cursor", userId: "u0", x: 1, y: 2 });    expect(rows.length).toBe(0);  });});