mirror of
https://github.com/loro-dev/loro.git
synced 2025-02-05 20:17:13 +00:00
test: add more examples
This commit is contained in:
parent
fdd24bd836
commit
90079b2f1c
4 changed files with 108 additions and 29 deletions
|
@ -1,5 +1,10 @@
|
|||
{
|
||||
"version": "2",
|
||||
"version": "3",
|
||||
"redirects": {
|
||||
"https://deno.land/std/fmt/printf.ts": "https://deno.land/std@0.105.0/fmt/printf.ts",
|
||||
"https://deno.land/std/path/mod.ts": "https://deno.land/std@0.105.0/path/mod.ts",
|
||||
"https://deno.land/std/testing/asserts.ts": "https://deno.land/std@0.105.0/testing/asserts.ts"
|
||||
},
|
||||
"remote": {
|
||||
"https://deno.land/std@0.105.0/_util/assert.ts": "2f868145a042a11d5ad0a3c748dcf580add8a0dbc0e876eaa0026303a5488f58",
|
||||
"https://deno.land/std@0.105.0/_util/os.ts": "dfb186cc4e968c770ab6cc3288bd65f4871be03b93beecae57d657232ecffcac",
|
||||
|
|
|
@ -44,24 +44,6 @@ export type Value =
|
|||
|
||||
export type Prelim = PrelimList | PrelimMap | PrelimText;
|
||||
|
||||
export type Path = (number | string)[];
|
||||
|
||||
export type ListDiff = {
|
||||
type: "list";
|
||||
diff: Delta<Value[]>[];
|
||||
};
|
||||
|
||||
export type TextDiff = {
|
||||
type: "text";
|
||||
diff: Delta<string>[];
|
||||
};
|
||||
|
||||
export type MapDiff = {
|
||||
type: "map";
|
||||
updated: Record<string, Value | undefined>;
|
||||
};
|
||||
|
||||
export type Diff = ListDiff | TextDiff | MapDiff;
|
||||
|
||||
export interface LoroEvent {
|
||||
/**
|
||||
|
@ -82,6 +64,25 @@ export interface LoroEvent {
|
|||
path: Path;
|
||||
}
|
||||
|
||||
export type Path = (number | string)[];
|
||||
|
||||
export type Diff = ListDiff | TextDiff | MapDiff;
|
||||
|
||||
export type ListDiff = {
|
||||
type: "list";
|
||||
diff: Delta<Value[]>[];
|
||||
};
|
||||
|
||||
export type TextDiff = {
|
||||
type: "text";
|
||||
diff: Delta<string>[];
|
||||
};
|
||||
|
||||
export type MapDiff = {
|
||||
type: "map";
|
||||
updated: Record<string, Value | undefined>;
|
||||
};
|
||||
|
||||
interface Listener {
|
||||
(event: LoroEvent): void;
|
||||
}
|
||||
|
|
|
@ -2,11 +2,88 @@ import { describe, expect, it } from "vitest";
|
|||
import {
|
||||
ContainerID,
|
||||
Loro,
|
||||
LoroList,
|
||||
LoroMap,
|
||||
setPanicHook,
|
||||
} from "../src";
|
||||
|
||||
setPanicHook();
|
||||
|
||||
it("basic example", () => {
|
||||
const doc = new Loro();
|
||||
const list: LoroList = doc.getList("list");
|
||||
list.insert(0, "A");
|
||||
list.insert(1, "B");
|
||||
list.insert(2, "C");
|
||||
|
||||
const map: LoroMap = doc.getMap("map");
|
||||
// map can only has string key
|
||||
map.set("key", "value");
|
||||
expect(doc.toJson()).toStrictEqual({
|
||||
list: ["A", "B", "C"],
|
||||
map: { key: "value" }
|
||||
});
|
||||
|
||||
// delete 1 element at index 0
|
||||
list.delete(0, 2)
|
||||
expect(doc.toJson()).toStrictEqual({
|
||||
list: ["C"],
|
||||
map: { key: "value" }
|
||||
});
|
||||
|
||||
// Insert a text container to the list
|
||||
const text = list.insertContainer(0, "Text");
|
||||
text.insert(0, "Hello");
|
||||
text.insert(0, "Hi! ")
|
||||
|
||||
// delete 1 element at index 0
|
||||
expect(doc.toJson()).toStrictEqual({
|
||||
list: ["Hi! Hello", "C"],
|
||||
map: { key: "value" }
|
||||
});
|
||||
|
||||
// Insert a list container to the map
|
||||
const list2 = map.setContainer("test", "List");
|
||||
list2.insert(0, 1);
|
||||
expect(doc.toJson()).toStrictEqual({
|
||||
list: ["Hi! Hello", "C"],
|
||||
map: { key: "value", test: [1] }
|
||||
});
|
||||
})
|
||||
|
||||
it("basic sync example", () => {
|
||||
const docA = new Loro();
|
||||
const docB = new Loro();
|
||||
const listA: LoroList = docA.getList("list");
|
||||
listA.insert(0, "A");
|
||||
listA.insert(1, "B");
|
||||
listA.insert(2, "C");
|
||||
// B import the ops from A
|
||||
docB.import(docA.exportFrom());
|
||||
expect(docB.toJson()).toStrictEqual({
|
||||
list: ["A", "B", "C"]
|
||||
})
|
||||
|
||||
const listB: LoroList = docB.getList("list");
|
||||
// delete 1 element at index 1
|
||||
listB.delete(1, 1);
|
||||
// A import the 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({
|
||||
list: ["A", "C"]
|
||||
});
|
||||
expect(docA.toJson()).toStrictEqual(docB.toJson());
|
||||
})
|
||||
|
||||
it("basic events", () => {
|
||||
const doc = new Loro();
|
||||
doc.subscribe(event => {
|
||||
|
||||
});
|
||||
const list = doc.getList("list");
|
||||
})
|
||||
|
||||
describe("list", () => {
|
||||
it("insert containers", () => {
|
||||
const doc = new Loro();
|
||||
|
|
|
@ -8,23 +8,19 @@ setPanicHook();
|
|||
describe("Checkout", () => {
|
||||
it("simple checkout", () => {
|
||||
const doc = new Loro();
|
||||
doc.setPeerId(0n);
|
||||
const text = doc.getText("text");
|
||||
text.insert(0, "hello world");
|
||||
text.insert(0, "H");
|
||||
doc.commit();
|
||||
const v = doc.frontiers();
|
||||
text.insert(0, "000");
|
||||
text.insert(1, "i");
|
||||
expect(doc.toJson()).toStrictEqual({
|
||||
text: "000hello world"
|
||||
text: "Hi"
|
||||
});
|
||||
|
||||
doc.checkout(v);
|
||||
doc.checkout([{ peer: 0n, counter: 0 }]);
|
||||
expect(doc.toJson()).toStrictEqual({
|
||||
text: "hello world"
|
||||
});
|
||||
v[0].counter -= 1;
|
||||
doc.checkout(v);
|
||||
expect(doc.toJson()).toStrictEqual({
|
||||
text: "hello worl"
|
||||
text: "H"
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue