loro/loro-js/tests/checkout.test.ts
2024-01-22 12:03:50 +08:00

73 lines
1.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
Loro,
} from "../src";
describe("Checkout", () => {
it("simple checkout", () => {
const doc = new Loro();
doc.setPeerId(0n);
const text = doc.getText("text");
text.insert(0, "H");
doc.commit();
const v = doc.frontiers();
text.insert(1, "i");
expect(doc.toJson()).toStrictEqual({
text: "Hi"
});
expect(doc.isDetached()).toBeFalsy();
doc.checkout([{ peer: "0", counter: 0 }]);
expect(doc.isDetached()).toBeTruthy();
expect(doc.toJson()).toStrictEqual({
text: "H"
});
});
it("Chinese char", () => {
const doc = new Loro();
const text = doc.getText("text");
text.insert(0, "你好世界");
doc.commit();
const v = doc.frontiers();
expect(v[0].counter).toBe(3);
v[0].counter -= 1;
doc.checkout(v);
expect(doc.toJson()).toStrictEqual({
text: "你好世"
});
v[0].counter -= 1;
doc.checkout(v);
expect(doc.toJson()).toStrictEqual({
text: "你好"
});
v[0].counter -= 1;
doc.checkout(v);
expect(doc.toJson()).toStrictEqual({
text: "你"
});
})
it("two clients", () => {
const doc = new Loro();
const text = doc.getText("text");
text.insert(0, "0");
doc.commit();
const v0 = doc.frontiers();
const docB = new Loro();
docB.import(doc.exportFrom());
expect(docB.cmpWithFrontiers(v0)).toBe(0);
text.insert(1, "0");
doc.commit();
expect(docB.cmpWithFrontiers(doc.frontiers())).toBe(-1);
const textB = docB.getText("text");
textB.insert(0, "0");
docB.commit();
expect(docB.cmpWithFrontiers(doc.frontiers())).toBe(-1);
docB.import(doc.exportFrom());
expect(docB.cmpWithFrontiers(doc.frontiers())).toBe(1);
doc.import(docB.exportFrom());
expect(docB.cmpWithFrontiers(doc.frontiers())).toBe(0);
});
});