2023-05-11 03:59:48 +00:00
|
|
|
import { assertType, describe, expect, it } from "vitest";
|
2023-04-03 01:29:25 +00:00
|
|
|
import {
|
|
|
|
Loro,
|
2023-05-11 03:59:48 +00:00
|
|
|
LoroList,
|
2023-04-03 01:29:25 +00:00
|
|
|
LoroMap,
|
|
|
|
PrelimList,
|
|
|
|
PrelimMap,
|
|
|
|
PrelimText,
|
|
|
|
Transaction,
|
|
|
|
} from "../src";
|
2023-05-11 03:59:48 +00:00
|
|
|
import { expectTypeOf } from "vitest";
|
2023-04-03 01:29:25 +00:00
|
|
|
|
|
|
|
function assertEquals(a: any, b: any) {
|
|
|
|
expect(a).toStrictEqual(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("transaction", () => {
|
|
|
|
it("transaction", async () => {
|
|
|
|
const loro = new Loro();
|
|
|
|
const text = loro.getText("text");
|
|
|
|
let count = 0;
|
|
|
|
const sub = loro.subscribe(() => {
|
|
|
|
count += 1;
|
|
|
|
loro.unsubscribe(sub);
|
|
|
|
});
|
|
|
|
loro.transact((txn: Transaction) => {
|
|
|
|
expect(count).toBe(0);
|
|
|
|
text.insert(txn, 0, "hello world");
|
|
|
|
expect(count).toBe(0);
|
|
|
|
text.insert(txn, 0, "hello world");
|
|
|
|
assertEquals(count, 0);
|
|
|
|
});
|
|
|
|
assertEquals(count, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("transaction origin", async () => {
|
|
|
|
const loro = new Loro();
|
|
|
|
const text = loro.getText("text");
|
|
|
|
let count = 0;
|
|
|
|
const sub = loro.subscribe((event: { origin: string }) => {
|
|
|
|
count += 1;
|
|
|
|
loro.unsubscribe(sub);
|
|
|
|
assertEquals(event.origin, "origin");
|
|
|
|
});
|
|
|
|
loro.transact((txn: Transaction) => {
|
|
|
|
assertEquals(count, 0);
|
|
|
|
text.insert(txn, 0, "hello world");
|
|
|
|
assertEquals(count, 0);
|
|
|
|
text.insert(txn, 0, "hello world");
|
|
|
|
assertEquals(count, 0);
|
|
|
|
}, "origin");
|
|
|
|
assertEquals(count, 1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("subscribe", () => {
|
|
|
|
it("subscribe_lock", async () => {
|
|
|
|
const loro = new Loro();
|
|
|
|
const text = loro.getText("text");
|
|
|
|
const list = loro.getList("list");
|
|
|
|
let count = 0;
|
|
|
|
let i = 1;
|
|
|
|
const sub = loro.subscribe(() => {
|
|
|
|
if (i > 0) {
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(txn => {
|
|
|
|
list.insert(txn, 0, i);
|
|
|
|
i--;
|
|
|
|
})
|
2023-04-03 01:29:25 +00:00
|
|
|
}
|
2023-07-30 08:30:41 +00:00
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
count += 1;
|
|
|
|
});
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact((txn) => {
|
|
|
|
text.insert(txn, 0, "hello world");
|
|
|
|
})
|
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
assertEquals(count, 2);
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact((txn) => {
|
|
|
|
text.insert(txn, 0, "hello world");
|
|
|
|
});
|
2023-04-03 01:29:25 +00:00
|
|
|
assertEquals(count, 3);
|
|
|
|
loro.unsubscribe(sub);
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(txn => {
|
|
|
|
text.insert(txn, 0, "hello world");
|
|
|
|
})
|
2023-04-03 01:29:25 +00:00
|
|
|
assertEquals(count, 3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("subscribe_lock2", async () => {
|
|
|
|
const loro = new Loro();
|
|
|
|
const text = loro.getText("text");
|
|
|
|
let count = 0;
|
|
|
|
const sub = loro.subscribe(() => {
|
|
|
|
count += 1;
|
|
|
|
loro.unsubscribe(sub);
|
|
|
|
});
|
|
|
|
assertEquals(count, 0);
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(txn => {
|
|
|
|
text.insert(txn, 0, "hello world");
|
|
|
|
})
|
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
assertEquals(count, 1);
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(txn => {
|
|
|
|
text.insert(txn, 0, "hello world");
|
|
|
|
})
|
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
assertEquals(count, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("subscribe", async () => {
|
|
|
|
const loro = new Loro();
|
|
|
|
const text = loro.getText("text");
|
|
|
|
let count = 0;
|
|
|
|
const sub = loro.subscribe(() => {
|
|
|
|
count += 1;
|
|
|
|
});
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(loro => {
|
|
|
|
text.insert(loro, 0, "hello world");
|
|
|
|
})
|
2023-04-03 01:29:25 +00:00
|
|
|
assertEquals(count, 1);
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(loro => {
|
|
|
|
text.insert(loro, 0, "hello world");
|
|
|
|
})
|
2023-04-03 01:29:25 +00:00
|
|
|
assertEquals(count, 2);
|
|
|
|
loro.unsubscribe(sub);
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(loro => {
|
|
|
|
text.insert(loro, 0, "hello world");
|
|
|
|
})
|
2023-04-03 01:29:25 +00:00
|
|
|
assertEquals(count, 2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("sync", () => {
|
|
|
|
it("two insert at beginning", async () => {
|
|
|
|
const a = new Loro();
|
|
|
|
const b = new Loro();
|
|
|
|
let a_version: undefined | Uint8Array = undefined;
|
|
|
|
let b_version: undefined | Uint8Array = undefined;
|
|
|
|
a.subscribe((e: { local: boolean }) => {
|
|
|
|
if (e.local) {
|
|
|
|
const exported = a.exportFrom(a_version);
|
|
|
|
b.import(exported);
|
|
|
|
a_version = a.version();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
b.subscribe((e: { local: boolean }) => {
|
|
|
|
if (e.local) {
|
|
|
|
const exported = b.exportFrom(b_version);
|
|
|
|
a.import(exported);
|
|
|
|
b_version = b.version();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const aText = a.getText("text");
|
|
|
|
const bText = b.getText("text");
|
2023-07-30 08:30:41 +00:00
|
|
|
a.transact(txn => {
|
|
|
|
aText.insert(txn, 0, "abc");
|
|
|
|
});
|
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
assertEquals(aText.toString(), bText.toString());
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sync", () => {
|
|
|
|
const loro = new Loro();
|
|
|
|
const text = loro.getText("text");
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(txn => {
|
|
|
|
text.insert(txn, 0, "hello world");
|
|
|
|
});
|
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
const loro_bk = new Loro();
|
|
|
|
loro_bk.import(loro.exportFrom(undefined));
|
|
|
|
assertEquals(loro_bk.toJson(), loro.toJson());
|
|
|
|
const text_bk = loro_bk.getText("text");
|
|
|
|
assertEquals(text_bk.toString(), "hello world");
|
2023-07-30 08:30:41 +00:00
|
|
|
loro_bk.transact(txn => {
|
|
|
|
text_bk.insert(txn, 0, "a ");
|
|
|
|
});
|
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
loro.import(loro_bk.exportFrom(undefined));
|
|
|
|
assertEquals(text.toString(), "a hello world");
|
|
|
|
const map = loro.getMap("map");
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(txn => {
|
|
|
|
map.set(txn, "key", "value");
|
|
|
|
});
|
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("prelim", () => {
|
|
|
|
it("test prelim", async (t) => {
|
|
|
|
const loro = new Loro();
|
|
|
|
const map = loro.getMap("map");
|
|
|
|
const list = loro.getList("list");
|
|
|
|
const prelim_text = new PrelimText(undefined);
|
|
|
|
const prelim_map = new PrelimMap({ a: 1, b: 2 });
|
|
|
|
const prelim_list = new PrelimList([1, "2", { a: 4 }]);
|
|
|
|
|
|
|
|
it("prelim text", () => {
|
|
|
|
prelim_text.insert(0, "hello world");
|
|
|
|
assertEquals(prelim_text.value, "hello world");
|
|
|
|
prelim_text.delete(6, 5);
|
|
|
|
prelim_text.insert(6, "everyone");
|
|
|
|
assertEquals(prelim_text.value, "hello everyone");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("prelim map", () => {
|
|
|
|
prelim_map.set("ab", 123);
|
|
|
|
assertEquals(prelim_map.value, { a: 1, b: 2, ab: 123 });
|
|
|
|
prelim_map.delete("b");
|
|
|
|
assertEquals(prelim_map.value, { a: 1, ab: 123 });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("prelim list", () => {
|
|
|
|
prelim_list.insert(0, 0);
|
|
|
|
assertEquals(prelim_list.value, [0, 1, "2", { a: 4 }]);
|
|
|
|
prelim_list.delete(1, 2);
|
|
|
|
assertEquals(prelim_list.value, [0, { a: 4 }]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("prelim map integrate", () => {
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(txn => {
|
|
|
|
map.set(txn, "text", prelim_text);
|
|
|
|
map.set(txn, "map", prelim_map);
|
|
|
|
map.set(txn, "list", prelim_list);
|
|
|
|
});
|
|
|
|
|
|
|
|
assertEquals(map.getDeepValue(), {
|
2023-04-03 01:29:25 +00:00
|
|
|
text: "hello everyone",
|
|
|
|
map: { a: 1, ab: 123 },
|
|
|
|
list: [0, { a: 4 }],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("prelim list integrate", () => {
|
|
|
|
const prelim_text = new PrelimText("ttt");
|
|
|
|
const prelim_map = new PrelimMap({ a: 1, b: 2 });
|
|
|
|
const prelim_list = new PrelimList([1, "2", { a: 4 }]);
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(txn => {
|
|
|
|
list.insert(txn, 0, prelim_text);
|
|
|
|
list.insert(txn, 1, prelim_map);
|
|
|
|
list.insert(txn, 2, prelim_list);
|
|
|
|
});
|
|
|
|
|
|
|
|
assertEquals(list.getDeepValue(), ["ttt", { a: 1, b: 2 }, [1, "2", {
|
2023-04-03 01:29:25 +00:00
|
|
|
a: 4,
|
|
|
|
}]]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("wasm", () => {
|
|
|
|
const loro = new Loro();
|
|
|
|
const a = loro.getText("ha");
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(txn => {
|
|
|
|
a.insert(txn, 0, "hello world");
|
|
|
|
|
|
|
|
a.delete(txn, 6, 5);
|
|
|
|
a.insert(txn, 6, "everyone");
|
|
|
|
});
|
2023-04-03 01:29:25 +00:00
|
|
|
const b = loro.getMap("ha");
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(txn => {
|
|
|
|
b.set(txn, "ab", 123);
|
|
|
|
});
|
2023-04-03 01:29:25 +00:00
|
|
|
|
2023-07-30 08:30:41 +00:00
|
|
|
const bText = loro.transact(txn => {
|
|
|
|
return b.insertContainer(txn, "hh", "Text")
|
|
|
|
});
|
2023-04-03 01:29:25 +00:00
|
|
|
|
|
|
|
it("map get", () => {
|
|
|
|
assertEquals(b.get("ab"), 123);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getValueDeep", () => {
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(txn => {
|
|
|
|
bText.insert(txn, 0, "hello world Text");
|
|
|
|
});
|
|
|
|
|
|
|
|
assertEquals(b.getDeepValue(), { ab: 123, hh: "hello world Text" });
|
2023-04-03 01:29:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should throw error when using the wrong context", () => {
|
|
|
|
expect(() => {
|
|
|
|
const loro2 = new Loro();
|
2023-07-30 08:30:41 +00:00
|
|
|
loro2.transact(txn => {
|
|
|
|
bText.insert(txn, 0, "hello world Text");
|
|
|
|
});
|
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
}).toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("get container by id", () => {
|
|
|
|
const id = b.id;
|
|
|
|
const b2 = loro.getContainerById(id) as LoroMap;
|
|
|
|
assertEquals(b2.value, b.value);
|
|
|
|
assertEquals(b2.id, id);
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(txn => {
|
|
|
|
b2.set(txn, "0", 12);
|
|
|
|
});
|
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
assertEquals(b2.value, b.value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-05-11 03:59:48 +00:00
|
|
|
describe("type", () => {
|
|
|
|
it("test map type", () => {
|
|
|
|
const loro = new Loro<{ map: LoroMap<{ name: "he" }> }>();
|
|
|
|
const map = loro.getTypedMap("map");
|
|
|
|
const v = map.getTyped(loro, "name");
|
|
|
|
expectTypeOf(v).toEqualTypeOf<"he">();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("test recursive map type", () => {
|
|
|
|
const loro = new Loro<{ map: LoroMap<{ map: LoroMap<{ name: "he" }> }> }>();
|
|
|
|
const map = loro.getTypedMap("map");
|
2023-07-30 08:30:41 +00:00
|
|
|
loro.transact(txn => {
|
|
|
|
map.insertContainer(txn, "map", "Map");
|
|
|
|
});
|
|
|
|
|
2023-05-11 03:59:48 +00:00
|
|
|
const subMap = map.getTyped(loro, "map");
|
|
|
|
const name = subMap.getTyped(loro, "name");
|
|
|
|
expectTypeOf(name).toEqualTypeOf<"he">();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("works for list type", () => {
|
|
|
|
const loro = new Loro<{ list: LoroList<[string, number]> }>();
|
|
|
|
const list = loro.getTypedList("list");
|
2023-07-30 08:30:41 +00:00
|
|
|
console.dir((list as any).__proto__);
|
|
|
|
loro.transact(txn => {
|
|
|
|
list.insertTyped(txn, 0, "123");
|
|
|
|
});
|
|
|
|
|
|
|
|
loro.transact(txn => {
|
|
|
|
list.insertTyped(txn, 1, 123);
|
|
|
|
});
|
|
|
|
|
2023-05-11 03:59:48 +00:00
|
|
|
const v0 = list.getTyped(loro, 0);
|
|
|
|
expectTypeOf(v0).toEqualTypeOf<string>();
|
|
|
|
const v1 = list.getTyped(loro, 1);
|
|
|
|
expectTypeOf(v1).toEqualTypeOf<number>();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
function one_ms(): Promise<void> {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, 1));
|
|
|
|
}
|