Skip to content

Instantly share code, notes, and snippets.

@canadaduane
Created January 26, 2026 00:20
Show Gist options
  • Select an option

  • Save canadaduane/916cf020c668365959b93d249928b4a5 to your computer and use it in GitHub Desktop.

Select an option

Save canadaduane/916cf020c668365959b93d249928b4a5 to your computer and use it in GitHub Desktop.
Try to replicate an issue mentioned by matsuyama
import { describe, expect, it } from "vitest";
import { LoroDoc, UndoManager } from "../bundler/index";
describe("Reproduction", () => {
it("checkout after snapshot import", () => {
const doc = new LoroDoc();
doc.setPeerId(0n);
const text = doc.getText("text");
text.insert(0, "H");
doc.commit();
const v0 = doc.frontiers();
text.insert(1, "i");
doc.commit();
const snapshot = doc.export({ mode: "snapshot" });
const doc2 = new LoroDoc();
doc2.import(snapshot);
// Try to checkout to v0
doc2.checkout(v0);
expect(doc2.toJSON()).toStrictEqual({
text: "H",
});
});
it("checkout after snapshot import with different peer", () => {
const doc = new LoroDoc();
doc.setPeerId(0n);
const text = doc.getText("text");
text.insert(0, "H");
doc.commit();
const v0 = doc.frontiers();
text.insert(1, "i");
doc.commit();
const snapshot = doc.export({ mode: "snapshot" });
const doc2 = new LoroDoc();
doc2.setPeerId(1n);
doc2.import(snapshot);
// Try to checkout to v0
doc2.checkout(v0);
expect(doc2.toJSON()).toStrictEqual({
text: "H",
});
});
it("checkout after fromSnapshot", () => {
const doc = new LoroDoc();
doc.setPeerId(0n);
const text = doc.getText("text");
text.insert(0, "H");
doc.commit();
const v0 = doc.frontiers();
text.insert(1, "i");
doc.commit();
const snapshot = doc.export({ mode: "snapshot" });
const doc2 = LoroDoc.fromSnapshot(snapshot);
// Try to checkout to v0
doc2.checkout(v0);
expect(doc2.toJSON()).toStrictEqual({
text: "H",
});
});
it("checkout with rich text", () => {
const doc = new LoroDoc();
doc.setPeerId(0n);
const text = doc.getText("text");
text.insert(0, "Hello");
text.mark({ start: 0, end: 5 }, "bold", true);
doc.commit();
const v0 = doc.frontiers();
text.insert(5, " World");
doc.commit();
const snapshot = doc.export({ mode: "snapshot" });
const doc2 = new LoroDoc();
doc2.import(snapshot);
doc2.checkout(v0);
expect(doc2.toJSON()).toStrictEqual({
text: "Hello",
});
});
it("checkout with subscription", () => {
const doc = new LoroDoc();
doc.setPeerId(0n);
const text = doc.getText("text");
text.insert(0, "H");
doc.commit();
const v0 = doc.frontiers();
text.insert(1, "i");
doc.commit();
const snapshot = doc.export({ mode: "snapshot" });
const doc2 = new LoroDoc();
doc2.import(snapshot);
let eventTriggered = false;
doc2.subscribe((e) => {
eventTriggered = true;
expect(e.by).toBe("checkout");
});
doc2.checkout(v0);
expect(eventTriggered).toBe(true);
});
it("checkout with undo manager", () => {
const doc = new LoroDoc();
doc.setPeerId(0n);
const text = doc.getText("text");
text.insert(0, "H");
doc.commit();
const v0 = doc.frontiers();
const undoManager = new UndoManager(doc, {});
text.insert(1, "i");
doc.commit();
const snapshot = doc.export({ mode: "snapshot" });
const doc2 = new LoroDoc();
doc2.import(snapshot);
const undoManager2 = new UndoManager(doc2, {});
doc2.checkout(v0);
expect(doc2.toJSON()).toStrictEqual({
text: "H",
});
});
it("checkout event inspection", () => {
const doc = new LoroDoc();
doc.setPeerId(0n);
const text = doc.getText("text");
text.insert(0, "H");
doc.commit();
const v0 = doc.frontiers();
text.insert(1, "i");
doc.commit();
const snapshot = doc.export({ mode: "snapshot" });
const doc2 = new LoroDoc();
doc2.import(snapshot);
doc2.subscribe((e) => {
if (e.by === "checkout") {
expect(e.events.length).toBeGreaterThan(0);
}
});
doc2.checkout(v0);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment