-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_shallow_snapshot.test.ts
52 lines (47 loc) · 1.96 KB
/
12_shallow_snapshot.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { LoroDoc } from "npm:[email protected]";
import { expect } from "npm:[email protected]";
Deno.test("Shallow snapshot", () => {
const rootDoc = new LoroDoc();
rootDoc.setPeerId("0");
rootDoc.getText("text").insert(0, "Hello world!");
const snapshot = rootDoc.export({
mode: "shallow-snapshot",
frontiers: [{ peer: "0", counter: 5 }],
});
const shallowDoc = new LoroDoc();
shallowDoc.import(snapshot);
expect(shallowDoc.getText("text").toString()).toBe("Hello world!");
expect(shallowDoc.isShallow()).toBe(true);
});
Deno.test("Shallow snapshot - should throw if there is old update before the shallow root", () => {
const rootDoc = new LoroDoc();
rootDoc.setPeerId("0");
rootDoc.getText("text").insert(0, "He");
const oldDoc = new LoroDoc();
oldDoc.import(rootDoc.export({ mode: "update" }));
rootDoc.getText("text").insert(2, "llo world!");
const snapshot = rootDoc.export({
mode: "shallow-snapshot",
frontiers: [{ peer: "0", counter: 5 }],
});
//
// Shallow Snapshot
// ╔ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ╗
//
// ┌────┐ ┌─────┐ ║ ┌─────────┐ ║
// │ He │◀─┬──│ llo │◀────│ world! │
// └────┘ │ └─────┘ ║ └─────────┘ ║
// │
// │ ┌────┐ ║ ║
// └───│ e! │
// └────┘ ╚ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ╝
// old
// update
//
oldDoc.getText("text").insert(0, "e!");
const shallowDoc = new LoroDoc();
shallowDoc.import(snapshot);
const update = oldDoc.export({ mode: "update", from: rootDoc.version() });
expect(() => shallowDoc.import(update))
.toThrow();
});