2023-08-04 02:45:23 +00:00
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
|
|
Loro,
|
|
|
|
} from "../src";
|
|
|
|
|
|
|
|
describe("Checkout", () => {
|
|
|
|
it("simple checkout", () => {
|
|
|
|
const doc = new Loro();
|
2023-11-10 12:13:01 +00:00
|
|
|
doc.setPeerId(0n);
|
2023-08-04 02:45:23 +00:00
|
|
|
const text = doc.getText("text");
|
2023-11-10 12:13:01 +00:00
|
|
|
text.insert(0, "H");
|
2023-10-30 10:32:36 +00:00
|
|
|
doc.commit();
|
2023-08-04 02:45:23 +00:00
|
|
|
const v = doc.frontiers();
|
2023-11-10 12:13:01 +00:00
|
|
|
text.insert(1, "i");
|
2023-08-04 02:45:23 +00:00
|
|
|
expect(doc.toJson()).toStrictEqual({
|
2023-11-10 12:13:01 +00:00
|
|
|
text: "Hi"
|
2023-08-04 02:45:23 +00:00
|
|
|
});
|
|
|
|
|
2024-01-05 04:06:13 +00:00
|
|
|
doc.checkout([{ peer: "0", counter: 0 }]);
|
2023-08-04 02:45:23 +00:00
|
|
|
expect(doc.toJson()).toStrictEqual({
|
2023-11-10 12:13:01 +00:00
|
|
|
text: "H"
|
2023-08-04 02:45:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Chinese char", () => {
|
|
|
|
const doc = new Loro();
|
|
|
|
const text = doc.getText("text");
|
2023-10-30 10:32:36 +00:00
|
|
|
text.insert(0, "你好世界");
|
|
|
|
doc.commit();
|
2023-08-04 02:45:23 +00:00
|
|
|
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");
|
2023-10-30 10:32:36 +00:00
|
|
|
text.insert(0, "0");
|
|
|
|
doc.commit();
|
2023-08-04 02:45:23 +00:00
|
|
|
|
|
|
|
const v0 = doc.frontiers();
|
|
|
|
const docB = new Loro();
|
|
|
|
docB.import(doc.exportFrom());
|
|
|
|
expect(docB.cmpFrontiers(v0)).toBe(0);
|
2023-10-30 10:32:36 +00:00
|
|
|
text.insert(1, "0");
|
|
|
|
doc.commit();
|
2023-08-04 02:45:23 +00:00
|
|
|
expect(docB.cmpFrontiers(doc.frontiers())).toBe(-1);
|
|
|
|
const textB = docB.getText("text");
|
2023-10-30 10:32:36 +00:00
|
|
|
textB.insert(0, "0");
|
|
|
|
docB.commit();
|
2023-08-04 02:45:23 +00:00
|
|
|
expect(docB.cmpFrontiers(doc.frontiers())).toBe(-1);
|
|
|
|
docB.import(doc.exportFrom());
|
|
|
|
expect(docB.cmpFrontiers(doc.frontiers())).toBe(1);
|
|
|
|
doc.import(docB.exportFrom());
|
|
|
|
expect(docB.cmpFrontiers(doc.frontiers())).toBe(0);
|
|
|
|
});
|
|
|
|
});
|