loro/crates/loro-wasm/tests/gc.test.ts
Zixuan Chen 62a3a93552
Refactor: merge two js packages (#532)
* feat: make vitest tests pass

* chore: update readme & add deno test for web bundle

* chore: bump version to 1.0.8-alpha.0

* chore: bump loro-crdt version

* fix: build script
export init method from loro-wasm/web

* chore: bump version

* chore: specify which files to include for npm publish

* refactor: rename loro-js to loro-js-test

* refactor: remove the old loro-js folder

* fix: build scripts

* chore: 1.0.8-alpha.3

* chore: add release info
2024-10-29 21:46:56 +08:00

52 lines
1.7 KiB
TypeScript

import { describe, expect, expectTypeOf, it } from "vitest";
import {
Container,
getType,
isContainer,
LoroDoc,
LoroList,
LoroMap,
LoroText,
LoroTree,
} from "../bundler/index";
describe("gc", () => {
it("should export gc snapshot", () => {
const doc = new LoroDoc();
doc.setPeerId(1);
doc.getList("list").insert(0, "A");
doc.getList("list").insert(1, "B");
doc.getList("list").insert(2, "C");
const bytes = doc.export({ mode: "shallow-snapshot", frontiers: doc.oplogFrontiers() });
const newDoc = new LoroDoc();
newDoc.import(bytes);
expect(newDoc.toJSON()).toEqual(doc.toJSON());
doc.getList("list").delete(1, 1); // Delete "B"
doc.getMap("map").set("key", "value"); // Add a new key-value pair to a map
const updatedBytes = doc.export({ mode: "update", from: newDoc.version() });
newDoc.import(updatedBytes);
expect(newDoc.toJSON()).toEqual(doc.toJSON());
});
it("cannot import outdated updates", () => {
const doc = new LoroDoc();
doc.setPeerId(1);
doc.getList("list").insert(0, "A");
const docB = doc.fork();
const v = docB.version();
docB.getList("list").insert(1, "C");
const updates = docB.export({ mode: "update", from: v });
doc.getList("list").insert(1, "B");
doc.getList("list").insert(2, "C");
doc.commit();
const bytes = doc.export({ mode: "shallow-snapshot", frontiers: doc.oplogFrontiers() });
const gcDoc = new LoroDoc();
gcDoc.import(bytes);
expect(() => gcDoc.import(updates)).toThrow();
})
});