Skip to content

Commit

Permalink
test: update loro-crdt
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Jun 7, 2024
1 parent 46413d5 commit 2d96fe2
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 53 deletions.
16 changes: 8 additions & 8 deletions 01_basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loro, LoroList, LoroMap, LoroText } from "npm:loro-crdt@0.15.0"
import { Loro, LoroList, LoroMap, LoroText } from "npm:loro-crdt@0.16.4-alpha.0"
import { expect } from "npm:[email protected]"

Deno.test("Basic usage", () => {
Expand All @@ -15,14 +15,14 @@ Deno.test("Basic usage", () => {
const map: LoroMap = doc.getMap("map");
// map can only has string key
map.set("key", "value");
expect(doc.toJson()).toStrictEqual({
expect(doc.toJSON()).toStrictEqual({
list: ["A", "B", "C"],
map: { key: "value" }
});

// delete 2 element at index 0
list.delete(0, 2)
expect(doc.toJson()).toStrictEqual({
expect(doc.toJSON()).toStrictEqual({
list: ["C"],
map: { key: "value" }
});
Expand All @@ -39,11 +39,11 @@ Deno.test("Sub containers", () => {
// insert a List container at index 0, and get the handler to that list
const subList = list.insertContainer(0, new LoroList());
subList.insert(0, "A");
expect(list.toJson()).toStrictEqual([["A"]]);
expect(list.toJSON()).toStrictEqual([["A"]]);
// create a Text container inside the Map container
const subtext = map.setContainer("text", new LoroText());
subtext.insert(0, "Hi");
expect(map.toJson()).toStrictEqual({ text: "Hi" });
expect(map.toJSON()).toStrictEqual({ text: "Hi" });
});

Deno.test("Sync", () => {
Expand All @@ -58,7 +58,7 @@ Deno.test("Sync", () => {
listA.insert(2, "C");
// B import the ops from A
docB.import(docA.exportFrom());
expect(docB.toJson()).toStrictEqual({
expect(docB.toJSON()).toStrictEqual({
list: ["A", "B", "C"]
})

Expand All @@ -68,8 +68,8 @@ Deno.test("Sync", () => {
// A import the missing ops from B
docA.import(docB.exportFrom(docA.version()))
// list at A is now ["A", "C"], with the same state as B
expect(docA.toJson()).toStrictEqual({
expect(docA.toJSON()).toStrictEqual({
list: ["A", "C"]
});
expect(docA.toJson()).toStrictEqual(docB.toJson());
expect(docA.toJSON()).toStrictEqual(docB.toJSON());
})
15 changes: 14 additions & 1 deletion 02_text.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { Delta, Loro } from "npm:[email protected].0";
import { Delta, Loro } from "npm:[email protected].2";
import { expect } from "npm:[email protected]";

Deno.test("Long text", () => {
/**
* Loro supports text manipulation.
*/
const doc = new Loro();
const text = doc.getText("text");
for (let i = 0; i < 1_000_000; i += 1) {
text.insert(i, i.toString())
}
doc.exportFrom();
doc.exportSnapshot();
});

Deno.test("Text", () => {
/**
* Loro supports text manipulation.
Expand Down
2 changes: 1 addition & 1 deletion 03_version.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loro, OpId } from "npm:loro-crdt@0.15.0";
import { Loro, OpId } from "npm:loro-crdt@0.16.4-alpha.0";
import { expect } from "npm:[email protected]";


Expand Down
10 changes: 5 additions & 5 deletions 04_time_travel.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loro } from "npm:loro-crdt@0.15.0";
import { Loro } from "npm:loro-crdt@0.16.4-alpha.0";
import { expect } from "npm:[email protected]";

Deno.test("Time Travel", () => {
Expand All @@ -11,24 +11,24 @@ Deno.test("Time Travel", () => {
text.insert(0, "Hello");
doc.commit();
text.insert(5, " world");
expect(doc.toJson()).toStrictEqual({
expect(doc.toJSON()).toStrictEqual({
text: "Hello world"
});

// Every unicode char insertion is a single operation for Text container
doc.checkout([{ peer: "0", counter: 0 }]);
expect(doc.toJson()).toStrictEqual({
expect(doc.toJSON()).toStrictEqual({
text: "H"
});

doc.checkout([{ peer: "0", counter: 4 }]);
expect(doc.toJson()).toStrictEqual({
expect(doc.toJSON()).toStrictEqual({
text: "Hello"
});

// Returns to the latest version
doc.attach();
expect(doc.toJson()).toStrictEqual({
expect(doc.toJSON()).toStrictEqual({
text: "Hello world"
});
})
12 changes: 6 additions & 6 deletions 05_save_and_load.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loro } from "npm:loro-crdt@0.15.0";
import { Loro } from "npm:loro-crdt@0.16.4-alpha.0";
import { expect } from "npm:[email protected]";

Deno.test("Save and load", () => {
Expand All @@ -10,7 +10,7 @@ Deno.test("Save and load", () => {

const newDoc = new Loro();
newDoc.import(data);
expect(newDoc.toJson()).toStrictEqual({
expect(newDoc.toJSON()).toStrictEqual({
text: "Hello world!"
});
})
Expand All @@ -37,19 +37,19 @@ Deno.test("Save and load incrementally", () => {
// import the snapshot
const newDoc = new Loro();
newDoc.import(data);
expect(newDoc.toJson()).toStrictEqual({
expect(newDoc.toJSON()).toStrictEqual({
text: "Hello world!"
});

// import update0
newDoc.import(update0)
expect(newDoc.toJson()).toStrictEqual({
expect(newDoc.toJSON()).toStrictEqual({
text: "✨Hello world!"
});

// import update1
newDoc.import(update1)
expect(newDoc.toJson()).toStrictEqual({
expect(newDoc.toJSON()).toStrictEqual({
text: "😶‍🌫️✨Hello world!"
});
}
Expand All @@ -60,7 +60,7 @@ Deno.test("Save and load incrementally", () => {
*/
const newDoc = new Loro();
newDoc.importUpdateBatch([update1, update0, data])
expect(newDoc.toJson()).toStrictEqual({
expect(newDoc.toJSON()).toStrictEqual({
text: "😶‍🌫️✨Hello world!"
});
}
Expand Down
2 changes: 1 addition & 1 deletion 06_event.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loro, LoroMap, LoroText, getType } from "npm:loro-crdt@0.15.0";
import { Loro, LoroMap, LoroText, getType } from "npm:loro-crdt@0.16.4-alpha.0";
import { expect } from "npm:[email protected]";

Deno.test("Event have delta that contains Container", async () => {
Expand Down
44 changes: 22 additions & 22 deletions 07_list.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cursor, Loro } from "npm:loro-crdt@0.15.0";
import { Cursor, Loro } from "npm:loro-crdt@0.16.4-alpha.0";
import { expect } from "npm:[email protected]";

Deno.test("List", () => {
Expand All @@ -17,15 +17,15 @@ Deno.test("List", () => {
// Concurrently docA and docB update element at index 2
// docA updates it to 8
// docB updates it to 9
// docA.toJson() should return { list: [0, 1, 8] }
// docB.toJson() should return { list: [0, 1, 9] }
// docA.toJSON() should return { list: [0, 1, 8] }
// docB.toJSON() should return { list: [0, 1, 9] }

listB.delete(2, 1);
listB.insert(2, 9);
expect(docB.toJson()).toStrictEqual({ list: [0, 1, 9] });
expect(docB.toJSON()).toStrictEqual({ list: [0, 1, 9] });
listA.delete(2, 1);
listA.insert(2, 8);
expect(docA.toJson()).toStrictEqual({ list: [0, 1, 8] });
expect(docA.toJSON()).toStrictEqual({ list: [0, 1, 8] });
}

{
Expand All @@ -34,8 +34,8 @@ Deno.test("List", () => {
docB.import(docA.exportFrom(docB.version()));
}

expect(docA.toJson()).toStrictEqual({ list: [0, 1, 8, 9] });
expect(docB.toJson()).toStrictEqual({ list: [0, 1, 8, 9] });
expect(docA.toJSON()).toStrictEqual({ list: [0, 1, 8, 9] });
expect(docB.toJSON()).toStrictEqual({ list: [0, 1, 8, 9] });
})

Deno.test("MovableList", () => {
Expand All @@ -54,13 +54,13 @@ Deno.test("MovableList", () => {
// Concurrently docA and docB update element at index 2
// docA updates it to 8
// docB updates it to 9
// docA.toJson() should return { list: [0, 1, 8] }
// docB.toJson() should return { list: [0, 1, 9] }
// docA.toJSON() should return { list: [0, 1, 8] }
// docB.toJSON() should return { list: [0, 1, 9] }

listA.set(2, 8);
expect(docA.toJson()).toStrictEqual({ list: [0, 1, 8] });
expect(docA.toJSON()).toStrictEqual({ list: [0, 1, 8] });
listB.set(2, 9);
expect(docB.toJson()).toStrictEqual({ list: [0, 1, 9] });
expect(docB.toJSON()).toStrictEqual({ list: [0, 1, 9] });
}

{
Expand All @@ -70,20 +70,20 @@ Deno.test("MovableList", () => {
}

// Converge to [0, 1, 9] because docB has larger peerId thus larger logical time
expect(docA.toJson()).toStrictEqual({ list: [0, 1, 9] });
expect(docB.toJson()).toStrictEqual({ list: [0, 1, 9] });
expect(docA.toJSON()).toStrictEqual({ list: [0, 1, 9] });
expect(docB.toJSON()).toStrictEqual({ list: [0, 1, 9] });

{
// Concurrently docA and docB move element at index 0
// docA moves it to 2
// docB moves it to 1
// docA.toJson() should return { list: [1, 9, 0] }
// docB.toJson() should return { list: [1, 0, 9] }
// docA.toJSON() should return { list: [1, 9, 0] }
// docB.toJSON() should return { list: [1, 0, 9] }

listA.move(0, 2);
listB.move(0, 1);
expect(docA.toJson()).toStrictEqual({ list: [1, 9, 0] });
expect(docB.toJson()).toStrictEqual({ list: [1, 0, 9] });
expect(docA.toJSON()).toStrictEqual({ list: [1, 9, 0] });
expect(docB.toJSON()).toStrictEqual({ list: [1, 0, 9] });
}

{
Expand All @@ -93,8 +93,8 @@ Deno.test("MovableList", () => {
}

// Converge to [1, 0, 9] because docB has larger peerId thus larger logical time
expect(docA.toJson()).toStrictEqual({ list: [1, 0, 9] });
expect(docB.toJson()).toStrictEqual({ list: [1, 0, 9] });
expect(docA.toJSON()).toStrictEqual({ list: [1, 0, 9] });
expect(docB.toJSON()).toStrictEqual({ list: [1, 0, 9] });
})


Expand All @@ -119,22 +119,22 @@ Deno.test("List Cursors", () => {
const listB = docB.getList("list");
docB.import(exported);
listB.insert(0, "Foo");
expect(docB.toJson()).toStrictEqual({ list: ["Foo", "Hello", "World"] });
expect(docB.toJSON()).toStrictEqual({ list: ["Foo", "Hello", "World"] });
const cursorB = Cursor.decode(encodedCursor);
{
// The cursor position is shifted to the right by 1
const pos = docB.getCursorPos(cursorB);
expect(pos.offset).toBe(2);
}
listB.insert(1, "Bar");
expect(docB.toJson()).toStrictEqual({ list: ["Foo", "Bar", "Hello", "World"] });
expect(docB.toJSON()).toStrictEqual({ list: ["Foo", "Bar", "Hello", "World"] });
{
// The cursor position is shifted to the right by 1
const pos = docB.getCursorPos(cursorB);
expect(pos.offset).toBe(3);
}
listB.delete(3, 1);
expect(docB.toJson()).toStrictEqual({ list: ["Foo", "Bar", "Hello"] });
expect(docB.toJSON()).toStrictEqual({ list: ["Foo", "Bar", "Hello"] });
{
// The position cursor points to is now deleted,
// but it should still get the position
Expand Down
10 changes: 5 additions & 5 deletions 08_map.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loro, LoroText } from "npm:loro-crdt@0.15.0";
import { Loro, LoroText } from "npm:loro-crdt@0.16.4-alpha.0";
import { expect } from "npm:[email protected]";

Deno.test("LoroMap", () => {
Expand All @@ -14,13 +14,13 @@ Deno.test("LoroMap", () => {
const textB = mapB.setContainer("a", new LoroText());
textB.insert(0, "Hi");

expect(docA.toJson()).toStrictEqual({ map: { a: 1 } });
expect(docB.toJson()).toStrictEqual({ map: { a: "Hi" } });
expect(docA.toJSON()).toStrictEqual({ map: { a: 1 } });
expect(docB.toJSON()).toStrictEqual({ map: { a: "Hi" } });

docA.import(docB.exportSnapshot());
docB.import(docA.exportSnapshot());

expect(docA.toJson()).toStrictEqual({ map: { a: "Hi" } });
expect(docB.toJson()).toStrictEqual({ map: { a: "Hi" } });
expect(docA.toJSON()).toStrictEqual({ map: { a: "Hi" } });
expect(docB.toJSON()).toStrictEqual({ map: { a: "Hi" } });
});

6 changes: 3 additions & 3 deletions 09_composition.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loro, LoroList, LoroText } from "npm:loro-crdt@0.15.0";
import { Loro, LoroList, LoroText } from "npm:loro-crdt@0.16.4-alpha.0";
import { expect } from "npm:[email protected]";

Deno.test("Composition", async () => {
Expand All @@ -18,7 +18,7 @@ Deno.test("Composition", async () => {
// Create a sub container for list
// { map: { list: [0, 1, LoroText] } }
const text = list.insertContainer(2, new LoroText());
expect(doc.toJson()).toStrictEqual({ map: { list: [0, 1, ""] } });
expect(doc.toJSON()).toStrictEqual({ map: { list: [0, 1, ""] } });
{
// Commit will trigger the event, because list is a sub container of map
doc.commit();
Expand All @@ -28,7 +28,7 @@ Deno.test("Composition", async () => {

text.insert(0, "Hello, ");
text.insert(7, "World!");
expect(doc.toJson()).toStrictEqual({ map: { list: [0, 1, "Hello, World!"] } });
expect(doc.toJSON()).toStrictEqual({ map: { list: [0, 1, "Hello, World!"] } });
{
// Commit will trigger the event, because text is a descendant of map
doc.commit();
Expand Down
2 changes: 1 addition & 1 deletion 10_op_and_change.ts → 10_op_and_change.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Change, Loro, LoroList, LoroText } from "npm:loro-crdt@0.15.0";
import { Change, Loro, LoroList, LoroText } from "npm:loro-crdt@0.16.4-alpha.0";
import { expect } from "npm:[email protected]";

Deno.test("op and change", () => {
Expand Down
15 changes: 15 additions & 0 deletions 11_tree_move.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Change, Loro, LoroList, LoroText } from "npm:[email protected]";
import { expect } from "npm:[email protected]";

Deno.test("Tree move", () => {
const docA = new Loro();

const treeA = docA.getTree("tree");
const node0 = treeA.createNode()
const node1 = treeA.createNode(node0.id, 0);
const node2 = treeA.createNode(node0.id, 1);
node2.moveBefore(node1);
expect(node2.index()).toBe(0);
expect(node1.index()).toBe(1);
});

Loading

0 comments on commit 2d96fe2

Please sign in to comment.