Rebasely Beta: data may be purged

examples/live-notes Public

TypeScript · 59 lines · 2.4 KiB · at 99dd857

import assert from "node:assert/strict";import { describe, test } from "node:test";import { COLORS, NAMES, asEditor, joinMessages, pickEditor, presence } from "../src/presence.ts";import type { Editor } from "../src/protocol.ts";describe("pickEditor", () => {  test("the first editor gets the first name and color", () => {    assert.deepEqual(pickEditor([], "a1"), { id: "a1", name: "Otter", color: COLORS[0] });  });  test("skips names and colors already in use", () => {    const here: Editor[] = [      { id: "a1", name: "Otter", color: COLORS[0] },      { id: "b2", name: "Lynx", color: COLORS[2] },    ];    assert.deepEqual(pickEditor(here, "c3"), { id: "c3", name: "Heron", color: COLORS[1] });  });  test("reuses a name once its editor has left", () => {    const here: Editor[] = [{ id: "b2", name: "Heron", color: COLORS[1] }];    assert.equal(pickEditor(here, "c3").name, "Otter");  });  test("numbers names once all are taken, and cycles colors", () => {    const here: Editor[] = [];    for (let i = 0; i < NAMES.length + 2; i++) here.push(pickEditor(here, `e${i}`));    assert.deepEqual(here.slice(-2).map((e) => e.name), ["Otter 2", "Heron 2"]);    assert.equal(new Set(here.map((e) => e.name)).size, here.length);    assert.ok(here.every((e) => COLORS.includes(e.color)));  });});describe("presence messages", () => {  const otter: Editor = { id: "a1", name: "Otter", color: COLORS[0] };  const heron: Editor = { id: "b2", name: "Heron", color: COLORS[1] };  test("a join tells the newcomer who it is and everyone else who is here", () => {    const { you, others } = joinMessages([otter], heron);    assert.deepEqual(you, { type: "presence", here: [otter, heron], you: heron });    assert.deepEqual(others, { type: "presence", here: [otter, heron] });  });  test("a leave sends the editors still here", () => {    assert.deepEqual(presence([heron]), { type: "presence", here: [heron] });    assert.deepEqual(presence([]), { type: "presence", here: [] });  });});describe("asEditor", () => {  test("reads an editor back from a socket attachment", () => {    assert.deepEqual(asEditor({ id: "a1", name: "Otter", color: "#0f766e", extra: 1 }), { id: "a1", name: "Otter", color: "#0f766e" });  });  test("skips sockets without one", () => {    assert.equal(asEditor(null), null);    assert.equal(asEditor("Otter"), null);    assert.equal(asEditor({ id: "a1", name: "Otter" }), null);  });});