Skip to content

Commit

Permalink
fix: adapt to v0.15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Apr 26, 2024
1 parent afbf996 commit db0b0d5
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 32 deletions.
6 changes: 3 additions & 3 deletions 01_basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loro, LoroList, LoroMap } from "npm:loro-crdt@0.6.3"
import { Loro, LoroList, LoroMap, LoroText } from "npm:loro-crdt@0.15.0"
import { expect } from "npm:[email protected]"

Deno.test("Basic usage", () => {
Expand Down Expand Up @@ -37,11 +37,11 @@ Deno.test("Sub containers", () => {
const list: LoroList = doc.getList("list");
const map: LoroMap = doc.getMap("list");
// insert a List container at index 0, and get the handler to that list
const subList = list.insertContainer(0, "List");
const subList = list.insertContainer(0, new LoroList());
subList.insert(0, "A");
expect(list.toJson()).toStrictEqual([["A"]]);
// create a Text container inside the Map container
const subtext = map.setContainer("text", "Text");
const subtext = map.setContainer("text", new LoroText());
subtext.insert(0, "Hi");
expect(map.toJson()).toStrictEqual({ text: "Hi" });
});
Expand Down
26 changes: 16 additions & 10 deletions 02_text.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Delta, Loro } from "npm:loro-crdt@0.6.3";
import { Delta, Loro } from "npm:loro-crdt@0.15.0";
import { expect } from "npm:[email protected]";

Deno.test("Text", () => {
Expand Down Expand Up @@ -37,9 +37,10 @@ Deno.test("Rich text custom expand behavior - Bold", () => {
* - Link: will not expand the style when inserting new text at the boundary.
*/
const doc = new Loro();
doc.configTextStyle({ bold: { expand: "after" } })
const text = doc.getText("text");
text.insert(0, "Hello world!");
text.mark({ start: 0, end: 5, expand: "after" }, "bold", true);
text.mark({ start: 0, end: 5 }, "bold", true);
text.insert(5, "!");
expect(text.toDelta()).toStrictEqual([{
insert: "Hello!",
Expand All @@ -59,9 +60,12 @@ Deno.test("Rich text custom expand behavior - Link", () => {
* - Link: will not expand the style when inserting new text at the boundary.
*/
const doc = new Loro();
doc.configTextStyle({
link: { expand: "none" },
})
const text = doc.getText("text");
text.insert(0, "Hello world!");
text.mark({ start: 0, end: 5, expand: "none" }, "link", true);
text.mark({ start: 0, end: 5 }, "link", true);
text.insert(5, "!");
expect(text.toDelta()).toStrictEqual([{
insert: "Hello",
Expand All @@ -80,13 +84,15 @@ Deno.test("Rich text event", async () => {
text.insert(0, "Hello world!");
doc.commit();
let ran = false;
text.subscribe(doc, (event) => {
if (event.diff.type === "text") {
expect(event.diff.diff).toStrictEqual([{
retain: 5,
attributes: { bold: true }
}]);
ran = true;
text.subscribe((events) => {
for (const event of events.events) {
if (event.diff.type === "text") {
expect(event.diff.diff).toStrictEqual([{
retain: 5,
attributes: { bold: true }
}]);
ran = true;
}
}
});
text.mark({ start: 0, end: 5 }, "bold", true);
Expand Down
6 changes: 3 additions & 3 deletions 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.6.3";
import { Loro, OpId } from "npm:loro-crdt@0.15.0";
import { expect } from "npm:[email protected]";


Expand All @@ -16,8 +16,8 @@ Deno.test("Frontiers & Version Vector Conversion", () => {
doc1.commit();

const frontiers = doc1.frontiers();
expect(frontiers).toStrictEqual([{ peer: 1n, counter: 1 } as OpId])
expect(frontiers).toStrictEqual([{ peer: "1", counter: 1 } as OpId])
const vv = doc1.frontiersToVV(frontiers);
expect(vv).toStrictEqual(new Map([[0n, 1], [1n, 2]]))
expect(vv.toJSON()).toStrictEqual(new Map([["0", 1], ["1", 2]]))
expect(doc1.vvToFrontiers(vv)).toStrictEqual(frontiers);
})
6 changes: 3 additions & 3 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.6.3";
import { Loro } from "npm:loro-crdt@0.15.0";
import { expect } from "npm:[email protected]";

Deno.test("Time Travel", () => {
Expand All @@ -16,12 +16,12 @@ Deno.test("Time Travel", () => {
});

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

doc.checkout([{ peer: 0n, counter: 4 }]);
doc.checkout([{ peer: "0", counter: 4 }]);
expect(doc.toJson()).toStrictEqual({
text: "Hello"
});
Expand Down
2 changes: 1 addition & 1 deletion 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.6.3";
import { Loro } from "npm:loro-crdt@0.15.0";
import { expect } from "npm:[email protected]";

Deno.test("Save and load", () => {
Expand Down
26 changes: 14 additions & 12 deletions 06_event.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { Loro, LoroText, getType } from "npm:loro-crdt@0.6.3";
import { Loro, LoroMap, LoroText, getType } from "npm:loro-crdt@0.15.0";
import { expect } from "npm:[email protected]";

Deno.test("Event have delta that contains Container", async () => {
const doc = new Loro();
const list = doc.getList("list");
let ran = false;
doc.subscribe(event => {
if (event.diff.type === "list") {
for (const item of event.diff.diff) {
expect(item.insert?.length).toBe(2);
expect(getType(item.insert![0])).toBe("Text")
expect(getType(item.insert![1])).toBe("Map")
const t = item.insert![0] as LoroText;
expect(t.toString()).toBe("Hello")
doc.subscribe(events => {
for (const event of events.events) {
if (event.diff.type === "list") {
for (const item of event.diff.diff) {
expect(item.insert?.length).toBe(2);
expect(getType(item.insert![0])).toBe("Text")
expect(getType(item.insert![1])).toBe("Map")
const t = item.insert![0] as LoroText;
expect(t.toString()).toBe("Hello")
}
ran = true;
}
ran = true;
}
})

list.insertContainer(0, "Map");
const t = list.insertContainer(0, "Text");
list.insertContainer(0, new LoroMap());
const t = list.insertContainer(0, new LoroText());
t.insert(0, "He");
t.insert(2, "llo");
doc.commit();
Expand Down
33 changes: 33 additions & 0 deletions 07_list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Loro, LoroList, LoroMovableList, LoroText } from "npm:[email protected]";
import { expect } from "npm:[email protected]";

Deno.test("List", () => {
let list = new LoroList();
list.push(0);
list.push("1");
const doc = new Loro();
const map = doc.getMap("root");
list = map.setContainer("list", list);
expect(doc.toJson()).toStrictEqual({ root: { list: [0, "1"] } });
list.delete(0, 1);
expect(doc.toJson()).toStrictEqual({ root: { list: ["1"] } });
})

Deno.test("MovableList", () => {
let list = new LoroMovableList();
list.push(0);
list.push("1");
const doc = new Loro();
const map = doc.getMap("root");
list = map.setContainer("list", list);
expect(doc.toJson()).toStrictEqual({ root: { list: [0, "1"] } });
list.move(0, 1);
expect(doc.toJson()).toStrictEqual({ root: { list: ["1", 0] } });
// Uint8Array is a special type in Loro
list.set(1, new Uint8Array([1, 2, 3]));
expect(doc.toJson()).toStrictEqual({ root: { list: ["1", new Uint8Array([1, 2, 3])] } });
const text = list.setContainer(0, new LoroText());
text.insert(0, "Hello")
expect(doc.toJson()).toStrictEqual({ root: { list: ["Hello", new Uint8Array([1, 2, 3])] } });
})

11 changes: 11 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit db0b0d5

Please sign in to comment.