loro/loro-js/tests/event.test.ts

334 lines
8.5 KiB
TypeScript
Raw Normal View History

import { describe, expect, expectTypeOf, it } from "vitest";
import {
Delta,
getType,
ListDiff,
Loro,
LoroEventBatch,
LoroText,
MapDiff,
TextDiff,
} from "../src";
2023-03-24 09:42:00 +00:00
describe("event", () => {
it("target", async () => {
const loro = new Loro();
let lastEvent: undefined | LoroEventBatch;
2023-03-24 09:42:00 +00:00
loro.subscribe((event) => {
lastEvent = event;
});
const text = loro.getText("text");
const id = text.id;
text.insert(0, "123");
loro.commit();
await oneMs();
expect(lastEvent?.events[0].target).toEqual(id);
2023-11-13 05:06:37 +00:00
});
2023-03-24 09:42:00 +00:00
it("path", async () => {
const loro = new Loro();
let lastEvent: undefined | LoroEventBatch;
2023-03-24 09:42:00 +00:00
loro.subscribe((event) => {
lastEvent = event;
});
const map = loro.getMap("map");
const subMap = map.setContainer("sub", "Map");
subMap.set("0", "1");
loro.commit();
await oneMs();
expect(lastEvent?.events[1].path).toStrictEqual(["map", "sub"]);
const list = subMap.setContainer("list", "List");
list.insert(0, "2");
const text = list.insertContainer(1, "Text");
loro.commit();
await oneMs();
text.insert(0, "3");
loro.commit();
await oneMs();
expect(lastEvent?.events[0].path).toStrictEqual(["map", "sub", "list", 1]);
2023-03-24 09:42:00 +00:00
});
it("text diff", async () => {
const loro = new Loro();
let lastEvent: undefined | LoroEventBatch;
loro.subscribe((event) => {
lastEvent = event;
});
const text = loro.getText("t");
text.insert(0, "3");
loro.commit();
await oneMs();
expect(lastEvent?.events[0].diff).toStrictEqual({
Feat: Peritext-like rich text support (#123) * feat: richtext wip * feat: add insert to style range map wip * feat: richtext state * fix: fix style state inserting and style map * fix: tiny vec merge err * fix: comment err * refactor: use new generic-btree & refine impl * feat: fugue tracker * feat: tracker * feat: tracker * fix: fix a few err in impl * feat: init richtext content state * feat: refactor arena * feat: extract anchor_type info out of style flag * refactor: state apply op more efficiently we can now reuse the repr in state and op * fix: new clippy errors * refactor: use state chunk as delta item * refactor: use two op to insert style start and style end * feat: diff calc * feat: handler * fix: tracker checkout err * fix: pass basic richtext handler tests * fix: pass handler basic marking tests * fix: pass all peritext criteria * feat: snapshot encoding for richtext init * refactor: replace Text with Richtext * refacotr: rm text code * fix: richtext checkout err * refactor: diff of text and map * refactor: del span * refactor: event * fix: fuzz err * fix: pass all tests * fix: fuzz err * fix: list child cache err * chore: rm debug code * fix: encode enhanced err * fix: encode enchanced * fix: fix several richtext issue * fix: richtext anchor err * chore: rm debug code * fix: richtext fuzz err * feat: speedup text snapshot decode * perf: optimize snapshot encoding * perf: speed up decode & insert * fix: fugue span merge err * perf: speedup delete & id cursor map * fix: fugue merge err * chore: update utils * perf: speedup text insert / del * fix: cursor cache * perf: reduce conversion by introducing InsertText * perf: speed up by refined cursor cache * chore: update gbtree dep * refactor(wasm): use quill delta format * chore: fix warnings
2023-10-29 06:02:13 +00:00
type: "text",
diff: [{ insert: "3" }],
} as TextDiff);
text.insert(1, "12");
loro.commit();
await oneMs();
expect(lastEvent?.events[0].diff).toStrictEqual({
Feat: Peritext-like rich text support (#123) * feat: richtext wip * feat: add insert to style range map wip * feat: richtext state * fix: fix style state inserting and style map * fix: tiny vec merge err * fix: comment err * refactor: use new generic-btree & refine impl * feat: fugue tracker * feat: tracker * feat: tracker * fix: fix a few err in impl * feat: init richtext content state * feat: refactor arena * feat: extract anchor_type info out of style flag * refactor: state apply op more efficiently we can now reuse the repr in state and op * fix: new clippy errors * refactor: use state chunk as delta item * refactor: use two op to insert style start and style end * feat: diff calc * feat: handler * fix: tracker checkout err * fix: pass basic richtext handler tests * fix: pass handler basic marking tests * fix: pass all peritext criteria * feat: snapshot encoding for richtext init * refactor: replace Text with Richtext * refacotr: rm text code * fix: richtext checkout err * refactor: diff of text and map * refactor: del span * refactor: event * fix: fuzz err * fix: pass all tests * fix: fuzz err * fix: list child cache err * chore: rm debug code * fix: encode enhanced err * fix: encode enchanced * fix: fix several richtext issue * fix: richtext anchor err * chore: rm debug code * fix: richtext fuzz err * feat: speedup text snapshot decode * perf: optimize snapshot encoding * perf: speed up decode & insert * fix: fugue span merge err * perf: speedup delete & id cursor map * fix: fugue merge err * chore: update utils * perf: speedup text insert / del * fix: cursor cache * perf: reduce conversion by introducing InsertText * perf: speed up by refined cursor cache * chore: update gbtree dep * refactor(wasm): use quill delta format * chore: fix warnings
2023-10-29 06:02:13 +00:00
type: "text",
diff: [{ retain: 1 }, { insert: "12" }],
} as TextDiff);
});
it("list diff", async () => {
const loro = new Loro();
let lastEvent: undefined | LoroEventBatch;
loro.subscribe((event) => {
lastEvent = event;
});
const text = loro.getList("l");
text.insert(0, "3");
loro.commit();
await oneMs();
expect(lastEvent?.events[0].diff).toStrictEqual({
Feat: Peritext-like rich text support (#123) * feat: richtext wip * feat: add insert to style range map wip * feat: richtext state * fix: fix style state inserting and style map * fix: tiny vec merge err * fix: comment err * refactor: use new generic-btree & refine impl * feat: fugue tracker * feat: tracker * feat: tracker * fix: fix a few err in impl * feat: init richtext content state * feat: refactor arena * feat: extract anchor_type info out of style flag * refactor: state apply op more efficiently we can now reuse the repr in state and op * fix: new clippy errors * refactor: use state chunk as delta item * refactor: use two op to insert style start and style end * feat: diff calc * feat: handler * fix: tracker checkout err * fix: pass basic richtext handler tests * fix: pass handler basic marking tests * fix: pass all peritext criteria * feat: snapshot encoding for richtext init * refactor: replace Text with Richtext * refacotr: rm text code * fix: richtext checkout err * refactor: diff of text and map * refactor: del span * refactor: event * fix: fuzz err * fix: pass all tests * fix: fuzz err * fix: list child cache err * chore: rm debug code * fix: encode enhanced err * fix: encode enchanced * fix: fix several richtext issue * fix: richtext anchor err * chore: rm debug code * fix: richtext fuzz err * feat: speedup text snapshot decode * perf: optimize snapshot encoding * perf: speed up decode & insert * fix: fugue span merge err * perf: speedup delete & id cursor map * fix: fugue merge err * chore: update utils * perf: speedup text insert / del * fix: cursor cache * perf: reduce conversion by introducing InsertText * perf: speed up by refined cursor cache * chore: update gbtree dep * refactor(wasm): use quill delta format * chore: fix warnings
2023-10-29 06:02:13 +00:00
type: "list",
diff: [{ insert: ["3"] }],
} as ListDiff);
text.insert(1, "12");
loro.commit();
await oneMs();
expect(lastEvent?.events[0].diff).toStrictEqual({
Feat: Peritext-like rich text support (#123) * feat: richtext wip * feat: add insert to style range map wip * feat: richtext state * fix: fix style state inserting and style map * fix: tiny vec merge err * fix: comment err * refactor: use new generic-btree & refine impl * feat: fugue tracker * feat: tracker * feat: tracker * fix: fix a few err in impl * feat: init richtext content state * feat: refactor arena * feat: extract anchor_type info out of style flag * refactor: state apply op more efficiently we can now reuse the repr in state and op * fix: new clippy errors * refactor: use state chunk as delta item * refactor: use two op to insert style start and style end * feat: diff calc * feat: handler * fix: tracker checkout err * fix: pass basic richtext handler tests * fix: pass handler basic marking tests * fix: pass all peritext criteria * feat: snapshot encoding for richtext init * refactor: replace Text with Richtext * refacotr: rm text code * fix: richtext checkout err * refactor: diff of text and map * refactor: del span * refactor: event * fix: fuzz err * fix: pass all tests * fix: fuzz err * fix: list child cache err * chore: rm debug code * fix: encode enhanced err * fix: encode enchanced * fix: fix several richtext issue * fix: richtext anchor err * chore: rm debug code * fix: richtext fuzz err * feat: speedup text snapshot decode * perf: optimize snapshot encoding * perf: speed up decode & insert * fix: fugue span merge err * perf: speedup delete & id cursor map * fix: fugue merge err * chore: update utils * perf: speedup text insert / del * fix: cursor cache * perf: reduce conversion by introducing InsertText * perf: speed up by refined cursor cache * chore: update gbtree dep * refactor(wasm): use quill delta format * chore: fix warnings
2023-10-29 06:02:13 +00:00
type: "list",
diff: [{ retain: 1 }, { insert: ["12"] }],
} as ListDiff);
});
it("map diff", async () => {
const loro = new Loro();
let lastEvent: undefined | LoroEventBatch;
loro.subscribe((event) => {
lastEvent = event;
});
const map = loro.getMap("m");
map.set("0", "3");
map.set("1", "2");
loro.commit();
await oneMs();
expect(lastEvent?.events[0].diff).toStrictEqual({
Feat: Peritext-like rich text support (#123) * feat: richtext wip * feat: add insert to style range map wip * feat: richtext state * fix: fix style state inserting and style map * fix: tiny vec merge err * fix: comment err * refactor: use new generic-btree & refine impl * feat: fugue tracker * feat: tracker * feat: tracker * fix: fix a few err in impl * feat: init richtext content state * feat: refactor arena * feat: extract anchor_type info out of style flag * refactor: state apply op more efficiently we can now reuse the repr in state and op * fix: new clippy errors * refactor: use state chunk as delta item * refactor: use two op to insert style start and style end * feat: diff calc * feat: handler * fix: tracker checkout err * fix: pass basic richtext handler tests * fix: pass handler basic marking tests * fix: pass all peritext criteria * feat: snapshot encoding for richtext init * refactor: replace Text with Richtext * refacotr: rm text code * fix: richtext checkout err * refactor: diff of text and map * refactor: del span * refactor: event * fix: fuzz err * fix: pass all tests * fix: fuzz err * fix: list child cache err * chore: rm debug code * fix: encode enhanced err * fix: encode enchanced * fix: fix several richtext issue * fix: richtext anchor err * chore: rm debug code * fix: richtext fuzz err * feat: speedup text snapshot decode * perf: optimize snapshot encoding * perf: speed up decode & insert * fix: fugue span merge err * perf: speedup delete & id cursor map * fix: fugue merge err * chore: update utils * perf: speedup text insert / del * fix: cursor cache * perf: reduce conversion by introducing InsertText * perf: speed up by refined cursor cache * chore: update gbtree dep * refactor(wasm): use quill delta format * chore: fix warnings
2023-10-29 06:02:13 +00:00
type: "map",
updated: {
"0": "3",
"1": "2",
},
} as MapDiff);
map.set("0", "0");
map.set("1", "1");
loro.commit();
await oneMs();
expect(lastEvent?.events[0].diff).toStrictEqual({
Feat: Peritext-like rich text support (#123) * feat: richtext wip * feat: add insert to style range map wip * feat: richtext state * fix: fix style state inserting and style map * fix: tiny vec merge err * fix: comment err * refactor: use new generic-btree & refine impl * feat: fugue tracker * feat: tracker * feat: tracker * fix: fix a few err in impl * feat: init richtext content state * feat: refactor arena * feat: extract anchor_type info out of style flag * refactor: state apply op more efficiently we can now reuse the repr in state and op * fix: new clippy errors * refactor: use state chunk as delta item * refactor: use two op to insert style start and style end * feat: diff calc * feat: handler * fix: tracker checkout err * fix: pass basic richtext handler tests * fix: pass handler basic marking tests * fix: pass all peritext criteria * feat: snapshot encoding for richtext init * refactor: replace Text with Richtext * refacotr: rm text code * fix: richtext checkout err * refactor: diff of text and map * refactor: del span * refactor: event * fix: fuzz err * fix: pass all tests * fix: fuzz err * fix: list child cache err * chore: rm debug code * fix: encode enhanced err * fix: encode enchanced * fix: fix several richtext issue * fix: richtext anchor err * chore: rm debug code * fix: richtext fuzz err * feat: speedup text snapshot decode * perf: optimize snapshot encoding * perf: speed up decode & insert * fix: fugue span merge err * perf: speedup delete & id cursor map * fix: fugue merge err * chore: update utils * perf: speedup text insert / del * fix: cursor cache * perf: reduce conversion by introducing InsertText * perf: speed up by refined cursor cache * chore: update gbtree dep * refactor(wasm): use quill delta format * chore: fix warnings
2023-10-29 06:02:13 +00:00
type: "map",
updated: {
"0": "0",
"1": "1",
},
} as MapDiff);
});
it("tree", async () => {
const loro = new Loro();
let lastEvent: undefined | LoroEventBatch;
loro.subscribe((event) => {
console.log(event);
lastEvent = event;
});
const tree = loro.getTree("tree");
const id = tree.id;
tree.createNode();
loro.commit();
await oneMs();
expect(lastEvent?.events[0].target).toEqual(id);
});
describe("subscribe container events", () => {
it("text", async () => {
const loro = new Loro();
const text = loro.getText("text");
let ran = 0;
const sub = text.subscribe(loro, (event) => {
if (!ran) {
expect((event.events[0].diff as any).diff).toStrictEqual([
Feat: Peritext-like rich text support (#123) * feat: richtext wip * feat: add insert to style range map wip * feat: richtext state * fix: fix style state inserting and style map * fix: tiny vec merge err * fix: comment err * refactor: use new generic-btree & refine impl * feat: fugue tracker * feat: tracker * feat: tracker * fix: fix a few err in impl * feat: init richtext content state * feat: refactor arena * feat: extract anchor_type info out of style flag * refactor: state apply op more efficiently we can now reuse the repr in state and op * fix: new clippy errors * refactor: use state chunk as delta item * refactor: use two op to insert style start and style end * feat: diff calc * feat: handler * fix: tracker checkout err * fix: pass basic richtext handler tests * fix: pass handler basic marking tests * fix: pass all peritext criteria * feat: snapshot encoding for richtext init * refactor: replace Text with Richtext * refacotr: rm text code * fix: richtext checkout err * refactor: diff of text and map * refactor: del span * refactor: event * fix: fuzz err * fix: pass all tests * fix: fuzz err * fix: list child cache err * chore: rm debug code * fix: encode enhanced err * fix: encode enchanced * fix: fix several richtext issue * fix: richtext anchor err * chore: rm debug code * fix: richtext fuzz err * feat: speedup text snapshot decode * perf: optimize snapshot encoding * perf: speed up decode & insert * fix: fugue span merge err * perf: speedup delete & id cursor map * fix: fugue merge err * chore: update utils * perf: speedup text insert / del * fix: cursor cache * perf: reduce conversion by introducing InsertText * perf: speed up by refined cursor cache * chore: update gbtree dep * refactor(wasm): use quill delta format * chore: fix warnings
2023-10-29 06:02:13 +00:00
{ insert: "123" },
] as Delta<string>[]);
}
ran += 1;
for (const containerDiff of event.events) {
expect(containerDiff.target).toBe(text.id);
}
});
2023-07-28 18:03:51 +00:00
text.insert(0, "123");
loro.commit();
await oneMs();
text.insert(1, "456");
loro.commit();
await oneMs();
expect(ran).toBeTruthy();
// subscribeOnce test
expect(text.toString()).toEqual("145623");
// unsubscribe
const oldRan = ran;
text.unsubscribe(loro, sub);
text.insert(0, "789");
loro.commit();
await oneMs();
expect(ran).toBe(oldRan);
});
it("map subscribe deep", async () => {
const loro = new Loro();
const map = loro.getMap("map");
let times = 0;
2023-07-28 18:03:51 +00:00
const sub = map.subscribe(loro, (event) => {
times += 1;
});
const subMap = map.setContainer("sub", "Map");
loro.commit();
await oneMs();
expect(times).toBe(1);
const text = subMap.setContainer("k", "Text");
loro.commit();
await oneMs();
expect(times).toBe(2);
text.insert(0, "123");
loro.commit();
await oneMs();
expect(times).toBe(3);
// unsubscribe
2023-07-28 18:03:51 +00:00
loro.unsubscribe(sub);
text.insert(0, "123");
loro.commit();
await oneMs();
expect(times).toBe(3);
});
it("list subscribe deep", async () => {
const loro = new Loro();
const list = loro.getList("list");
let times = 0;
2023-07-28 18:03:51 +00:00
const sub = list.subscribe(loro, (_) => {
times += 1;
});
const text = list.insertContainer(0, "Text");
loro.commit();
await oneMs();
expect(times).toBe(1);
text.insert(0, "123");
await oneMs();
loro.commit();
await oneMs();
expect(times).toBe(2);
// unsubscribe
2023-07-28 18:03:51 +00:00
loro.unsubscribe(sub);
text.insert(0, "123");
loro.commit();
await oneMs();
expect(times).toBe(2);
});
});
describe("text event length should be utf16", () => {
it("test", async () => {
const loro = new Loro();
const text = loro.getText("text");
let string = "";
text.subscribe(loro, (event) => {
for (const containerDiff of event.events) {
const diff = containerDiff.diff;
expect(diff.type).toBe("text");
if (diff.type === "text") {
let newString = "";
let pos = 0;
for (const delta of diff.diff) {
if (delta.retain != null) {
newString += string.slice(pos, pos + delta.retain);
pos += delta.retain;
} else if (delta.insert != null) {
newString += delta.insert;
} else {
pos += delta.delete;
}
}
string = newString + string.slice(pos);
}
}
});
text.insert(0, "你好");
loro.commit();
await oneMs();
expect(text.toString()).toBe(string);
text.insert(1, "世界");
loro.commit();
await oneMs();
expect(text.toString()).toBe(string);
text.insert(2, "👍");
loro.commit();
await oneMs();
expect(text.toString()).toBe(string);
text.insert(2, "♪(^∇^*)");
loro.commit();
await oneMs();
expect(text.toString()).toBe(string);
});
});
describe("handler in event", () => {
it("test", async () => {
const loro = new Loro();
const list = loro.getList("list");
let first = true;
loro.subscribe((e) => {
if (first) {
const diff = (e.events[0].diff as ListDiff).diff;
const text = diff[0].insert![0] as LoroText;
text.insert(0, "abc");
first = false;
}
});
list.insertContainer(0, "Text");
loro.commit();
await oneMs();
expect(loro.toJson().list[0]).toBe("abc");
});
});
it("diff can contain containers", async () => {
const doc = new Loro();
const list = doc.getList("list");
let ran = false;
doc.subscribe((event) => {
if (event.events[0].diff.type === "list") {
for (const item of event.events[0].diff.diff) {
const t = item.insert![0] as LoroText;
expect(t.toString()).toBe("Hello");
expect(item.insert?.length).toBe(2);
expect(getType(item.insert![0])).toBe("Text");
expect(getType(item.insert![1])).toBe("Map");
}
ran = true;
}
});
list.insertContainer(0, "Map");
const t = list.insertContainer(0, "Text");
t.insert(0, "He");
t.insert(2, "llo");
doc.commit();
await new Promise((resolve) => setTimeout(resolve, 1));
expect(ran).toBeTruthy();
});
2023-03-24 09:42:00 +00:00
});
function oneMs(): Promise<void> {
2023-03-24 09:42:00 +00:00
return new Promise((r) => setTimeout(r));
}