2023-11-05 08:06:34 +00:00
|
|
|
import { describe, expect, it } from "vitest";
|
2023-11-12 12:36:38 +00:00
|
|
|
import { Loro, toReadableVersion, setPanicHook, OpId } from "../src";
|
2023-11-05 08:06:34 +00:00
|
|
|
|
|
|
|
setPanicHook();
|
|
|
|
describe("Frontiers", () => {
|
|
|
|
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.cmpFrontiers(v0)).toBe(0);
|
|
|
|
text.insert(1, "0");
|
|
|
|
doc.commit();
|
|
|
|
expect(docB.cmpFrontiers(doc.frontiers())).toBe(-1);
|
|
|
|
const textB = docB.getText("text");
|
|
|
|
textB.insert(0, "0");
|
|
|
|
docB.commit();
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Version", () => {
|
|
|
|
const a = new Loro();
|
|
|
|
a.setPeerId(0n);
|
|
|
|
const b = new Loro();
|
|
|
|
b.setPeerId(1n);
|
|
|
|
a.getText("text").insert(0, "ha");
|
|
|
|
b.getText("text").insert(0, "yo");
|
|
|
|
a.import(b.exportFrom());
|
|
|
|
a.getText("text").insert(0, "k");
|
|
|
|
a.commit();
|
|
|
|
|
|
|
|
it("version vector to frontiers", () => {
|
|
|
|
{
|
|
|
|
const vv = new Map();
|
|
|
|
vv.set(0n, 3);
|
|
|
|
vv.set(1n, 2);
|
|
|
|
expect(toReadableVersion(a.version())).toStrictEqual(vv);
|
|
|
|
expect(toReadableVersion(a.version())).toStrictEqual(vv);
|
|
|
|
expect(a.vvToFrontiers(vv)).toStrictEqual(a.frontiers());
|
|
|
|
expect(a.vvToFrontiers(a.version())).toStrictEqual(a.frontiers());
|
2023-11-12 12:36:38 +00:00
|
|
|
expect(a.frontiers()).toStrictEqual([{ peer: 0n, counter: 2 }] as OpId[]);
|
2023-11-05 08:06:34 +00:00
|
|
|
}
|
2023-11-12 12:36:38 +00:00
|
|
|
});
|
2023-11-05 08:06:34 +00:00
|
|
|
|
|
|
|
it("get changes", () => {
|
|
|
|
const changes = a.getAllChanges();
|
2023-11-12 12:36:38 +00:00
|
|
|
expect(typeof changes.get(0n)?.[0].peer == "bigint").toBeTruthy();
|
2023-11-05 08:06:34 +00:00
|
|
|
expect(changes.size).toBe(2);
|
|
|
|
expect(changes.get(0n)?.length).toBe(2);
|
|
|
|
expect(changes.get(0n)?.[0].length).toBe(2);
|
|
|
|
expect(changes.get(0n)?.[1].lamport).toBe(2);
|
2023-11-12 12:36:38 +00:00
|
|
|
expect(changes.get(0n)?.[1].deps).toStrictEqual([
|
|
|
|
{ peer: 0n, counter: 1 },
|
|
|
|
{ peer: 1n, counter: 1 },
|
|
|
|
]);
|
2023-11-05 08:06:34 +00:00
|
|
|
expect(changes.get(1n)?.length).toBe(1);
|
2023-11-12 12:36:38 +00:00
|
|
|
});
|
2023-11-05 08:06:34 +00:00
|
|
|
|
|
|
|
it("get ops inside changes", () => {
|
|
|
|
const change = a.getOpsInChange({ peer: 0n, counter: 2 });
|
|
|
|
expect(change.length).toBe(1);
|
2023-11-12 12:36:38 +00:00
|
|
|
console.dir(change, { depth: 100 });
|
|
|
|
});
|
|
|
|
});
|