|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | + |
| 3 | +import { LoroDoc } from "../src/index"; |
| 4 | +import type { Frontiers } from "../src/index"; |
| 5 | + |
| 6 | +// Ports of crates/loro/tests/shallow_root_critical.rs. A shallow snapshot keeps |
| 7 | +// every op from its root to the latest version, so the root must be a critical |
| 8 | +// version: every retained op is causally after it, never concurrent with it |
| 9 | +// (loro-dev/loro#1095). The roots below are the ones loro-crdt picks. |
| 10 | + |
| 11 | +function shallowOf(doc: LoroDoc, frontiers: Frontiers): LoroDoc { |
| 12 | + const shallow = new LoroDoc(); |
| 13 | + shallow.import(doc.export({ mode: "shallow-snapshot", frontiers })); |
| 14 | + return shallow; |
| 15 | +} |
| 16 | + |
| 17 | +/** Every op of `doc` is in the root's causal past or has the root in its own. */ |
| 18 | +function expectCritical(doc: LoroDoc, root: Frontiers): void { |
| 19 | + if (root.length === 0) return; |
| 20 | + const rootVersion = doc.frontiersToVV(root); |
| 21 | + for (const [peer, end] of doc.oplogVersion().toJSON()) { |
| 22 | + for (let counter = rootVersion.get(peer) ?? 0; counter < end; counter += 1) { |
| 23 | + const opVersion = doc.frontiersToVV([{ peer, counter }]); |
| 24 | + const order = rootVersion.compare(opVersion); |
| 25 | + if (order !== -1 && order !== 0) { |
| 26 | + throw new Error(`${counter}@${peer} is concurrent with the root`); |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +describe("shallow snapshot root", () => { |
| 33 | + test("keeps the full history for independent heads", () => { |
| 34 | + for (let peers = 2; peers <= 4; peers += 1) { |
| 35 | + const doc = new LoroDoc(); |
| 36 | + doc.setPeerId(1); |
| 37 | + doc.getText("t").insert(0, "a"); |
| 38 | + doc.commit(); |
| 39 | + for (let peer = 2; peer <= peers; peer += 1) { |
| 40 | + const other = new LoroDoc(); |
| 41 | + other.setPeerId(peer); |
| 42 | + other.getText("t").insert(0, "b"); |
| 43 | + other.commit(); |
| 44 | + doc.import(other.export({ mode: "update" })); |
| 45 | + } |
| 46 | + const shallow = shallowOf(doc, doc.oplogFrontiers()); |
| 47 | + expect(shallow.shallowSinceFrontiers()).toEqual([]); |
| 48 | + expect(shallow.toJSON()).toEqual(doc.toJSON()); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + test("still trims to a root shared by every head", () => { |
| 53 | + const base = new LoroDoc(); |
| 54 | + base.setPeerId(100); |
| 55 | + base.getText("t").insert(0, "root"); |
| 56 | + base.commit(); |
| 57 | + const snapshot = base.export({ mode: "snapshot" }); |
| 58 | + for (let peer = 1; peer <= 5; peer += 1) { |
| 59 | + const fork = new LoroDoc(); |
| 60 | + fork.setPeerId(peer); |
| 61 | + fork.import(snapshot); |
| 62 | + fork.getText("t").insert(0, "x"); |
| 63 | + fork.commit(); |
| 64 | + base.import(fork.export({ mode: "update" })); |
| 65 | + } |
| 66 | + const shallow = shallowOf(base, base.oplogFrontiers()); |
| 67 | + expect(shallow.shallowSinceFrontiers()).toEqual([{ peer: "100", counter: 3 }]); |
| 68 | + expect(shallow.toJSON()).toEqual(base.toJSON()); |
| 69 | + }); |
| 70 | + |
| 71 | + test("moves below a past version when a later branch forked below it", () => { |
| 72 | + const doc = new LoroDoc(); |
| 73 | + doc.setPeerId(1); |
| 74 | + doc.getText("t").insert(0, "0"); |
| 75 | + doc.commit(); |
| 76 | + const atA0 = doc.export({ mode: "snapshot" }); |
| 77 | + doc.getText("t").insert(0, "1"); |
| 78 | + doc.commit(); |
| 79 | + doc.getText("t").insert(0, "2"); |
| 80 | + doc.commit(); |
| 81 | + const target = doc.oplogFrontiers(); |
| 82 | + const fork = new LoroDoc(); |
| 83 | + fork.setPeerId(2); |
| 84 | + fork.import(atA0); |
| 85 | + fork.getText("t").insert(1, "B"); |
| 86 | + fork.commit(); |
| 87 | + doc.import(fork.export({ mode: "update" })); |
| 88 | + |
| 89 | + const shallow = shallowOf(doc, target); |
| 90 | + // The requested version 2@1 is concurrent with 0@2; loro-crdt uses 0@1. |
| 91 | + expect(shallow.shallowSinceFrontiers()).toEqual([{ peer: "1", counter: 0 }]); |
| 92 | + expectCritical(doc, shallow.shallowSinceFrontiers()); |
| 93 | + shallow.checkout(target); |
| 94 | + expect(shallow.getText("t").toString()).toBe("210"); |
| 95 | + shallow.checkoutToLatest(); |
| 96 | + expect(shallow.getText("t").toString()).toBe("210B"); |
| 97 | + }); |
| 98 | + |
| 99 | + test("keeps concurrent list order through checkouts of the retained range", () => { |
| 100 | + const p2 = new LoroDoc(); |
| 101 | + p2.setPeerId(2); |
| 102 | + const p4 = new LoroDoc(); |
| 103 | + p4.setPeerId(4); |
| 104 | + p4.getList("l").insert(0, false); |
| 105 | + p4.commit(); |
| 106 | + p2.getList("l").insert(0, 45); |
| 107 | + p2.commit(); |
| 108 | + p2.getMap("m").set("k", 1); |
| 109 | + p2.commit(); |
| 110 | + p2.getMap("m").set("k", 2); |
| 111 | + p2.commit(); |
| 112 | + p2.import(p4.export({ mode: "update" })); |
| 113 | + expect(p2.getList("l").toArray()).toEqual([45, false]); |
| 114 | + |
| 115 | + // 4@0 is concurrent with 2@1, so the root cannot be 2@1. |
| 116 | + const shallow = shallowOf(p2, [{ peer: "2", counter: 1 }]); |
| 117 | + expect(shallow.shallowSinceFrontiers()).toEqual([]); |
| 118 | + for (const frontiers of [ |
| 119 | + [ |
| 120 | + { peer: "4", counter: 0 }, |
| 121 | + { peer: "2", counter: 2 }, |
| 122 | + ], |
| 123 | + [{ peer: "2", counter: 2 }], |
| 124 | + [{ peer: "2", counter: 1 }], |
| 125 | + ] as Frontiers[]) { |
| 126 | + shallow.checkout(frontiers); |
| 127 | + const full = new LoroDoc(); |
| 128 | + full.import(p2.export({ mode: "update" })); |
| 129 | + full.checkout(frontiers); |
| 130 | + expect(shallow.toJSON()).toEqual(full.toJSON()); |
| 131 | + } |
| 132 | + shallow.checkoutToLatest(); |
| 133 | + // Before the fix the retained concurrent insert came back as [false, 45]. |
| 134 | + expect(shallow.getList("l").toArray()).toEqual([45, false]); |
| 135 | + }); |
| 136 | + |
| 137 | + test("is critical on random multi-peer histories", () => { |
| 138 | + let random = 0x2c1b_3c6d; |
| 139 | + const next = (limit: number): number => { |
| 140 | + random ^= random << 13; |
| 141 | + random ^= random >>> 17; |
| 142 | + random ^= random << 5; |
| 143 | + return (random >>> 0) % limit; |
| 144 | + }; |
| 145 | + for (let round = 0; round < 40; round += 1) { |
| 146 | + const docs = [1, 2, 3].map((peer) => { |
| 147 | + const doc = new LoroDoc(); |
| 148 | + doc.setPeerId(peer); |
| 149 | + return doc; |
| 150 | + }); |
| 151 | + const targets: Frontiers[] = []; |
| 152 | + for (let step = 0; step < 24; step += 1) { |
| 153 | + const doc = docs[next(docs.length)]!; |
| 154 | + if (next(4) === 0) { |
| 155 | + const other = docs[next(docs.length)]!; |
| 156 | + if (other !== doc) doc.import(other.export({ mode: "update" })); |
| 157 | + } else { |
| 158 | + const text = doc.getText("t"); |
| 159 | + text.insert(next(text.length + 1), String.fromCharCode(97 + next(26))); |
| 160 | + doc.commit(); |
| 161 | + if (next(3) === 0) targets.push(doc.oplogFrontiers()); |
| 162 | + } |
| 163 | + } |
| 164 | + const doc = docs[0]!; |
| 165 | + for (const other of docs.slice(1)) doc.import(other.export({ mode: "update" })); |
| 166 | + for (const target of targets) { |
| 167 | + const shallow = shallowOf(doc, target); |
| 168 | + const root = shallow.shallowSinceFrontiers(); |
| 169 | + expectCritical(doc, root); |
| 170 | + expect(shallow.toJSON()).toEqual(doc.toJSON()); |
| 171 | + const rootVersion = doc.frontiersToVV(root); |
| 172 | + for (const version of targets) { |
| 173 | + const order = rootVersion.compare(doc.frontiersToVV(version)); |
| 174 | + if (order !== -1 && order !== 0) continue; |
| 175 | + const full = new LoroDoc(); |
| 176 | + full.import(doc.export({ mode: "update" })); |
| 177 | + full.checkout(version); |
| 178 | + shallow.checkout(version); |
| 179 | + expect(shallow.toJSON()).toEqual(full.toJSON()); |
| 180 | + } |
| 181 | + shallow.checkoutToLatest(); |
| 182 | + expect(shallow.toJSON()).toEqual(doc.toJSON()); |
| 183 | + } |
| 184 | + } |
| 185 | + }); |
| 186 | +}); |
0 commit comments