Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112import assert from "node:assert/strict";import { test } from "node:test";import { Doc } from "../src/collab/doc.ts";import { connect, type Message, type Port } from "../src/collab/sync.ts";
const lines = (n: number) => Array.from({ length: n }, (_, i) => `line ${i + 1}`).join("\n");
function fork(doc: Doc): Doc { return Doc.load(doc.export());}
function sync(a: Doc, b: Doc): void { a.loro.import(b.loro.export({ mode: "update" })); b.loro.import(a.loro.export({ mode: "update" }));}
test("a write against the version read keeps edits made since", () => { const human = Doc.create(lines(5)); const agent = fork(human);
const read = agent.read(); human.loro.getText("content").insert(human.text.indexOf("line 3") + 6, " (human)"); human.loro.commit(); sync(human, agent);
agent.write(read.text.replace("line 1", "line 1 (agent)"), { base: read.version }); sync(human, agent);
assert.equal(human.text, "line 1 (agent)\nline 2\nline 3 (human)\nline 4\nline 5"); assert.equal(agent.text, human.text);});
test("a write without a base replaces the current text", () => { const doc = Doc.create("old"); doc.write("new"); assert.equal(doc.text, "new");});
test("shallow export loads to the same text", () => { const doc = Doc.create(lines(3)); doc.write(lines(4)); assert.equal(Doc.load(doc.export("shallow")).text, doc.text);});
test("connect syncs existing state and later edits both ways", async () => { const a = Doc.create("hello"); const b = Doc.create(); const { port1, port2 } = new MessageChannel(); const stop = [connect(a, port1 as Port), connect(b, port2 as Port)];
try { await waitFor(b, () => b.text === "hello"); assert.equal(b.text, "hello");
b.write("hello world", { base: b.read().version }); await waitFor(a, () => a.text === "hello world"); assert.equal(a.text, "hello world"); } finally { for (const s of stop) s(); port1.close(); port2.close(); }});
test("connect forwards messages a relay can pass through unchanged", async () => { const seen: Message["t"][] = []; const a = Doc.create("x"); const b = Doc.create(); const { port1, port2 } = new MessageChannel(); port2.addEventListener("message", (e) => seen.push(e.data.t)); const stop = [connect(a, port1 as Port), connect(b, port2 as Port)]; try { await waitFor(b, () => b.text === "x"); assert.deepEqual([...new Set(seen)].sort(), ["hello", "update"]); } finally { for (const s of stop) s(); port1.close(); port2.close(); }});
/** Waits until `predicate` holds, re-checking after every change to `doc`; rejects (instead of hanging) if it never does. */function waitFor(doc: Doc, predicate: () => boolean, timeoutMs = 2000): Promise<void> { if (predicate()) return Promise.resolve(); return new Promise((resolve, reject) => { let timer: NodeJS.Timeout; const unsubscribe = doc.onChange(() => { if (!predicate()) return; clearTimeout(timer); unsubscribe(); resolve(); }); timer = setTimeout(() => { unsubscribe(); reject(new Error("timed out waiting for condition")); }, timeoutMs); });}
test("a write reports the version of exactly the text written, and what changed", () => { const human = Doc.create("one\ntwo\nthree"); const read = human.read(); human.loro.getText("content").insert(0, "zero\n"); human.loro.commit();
const result = human.write("one\nTWO\nthree", { base: read.version }); assert.equal(human.text, "zero\none\nTWO\nthree"); assert.equal(human.text.slice(result.changed!.from, result.changed!.to), "TWO"); assert.equal(human.loro.forkAt(result.version).getText("content").toString(), "one\nTWO\nthree"); assert.equal(human.write(human.text).changed, null);});