1
0
silverbullet/packages/common/spaces/sync.test.ts

124 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-04-05 15:02:17 +00:00
import { expect, test } from "@jest/globals";
2022-04-07 13:21:30 +00:00
import { IndexedDBSpacePrimitives } from "./indexeddb_space_primitives";
2022-04-05 15:02:17 +00:00
import { SpaceSync } from "./sync";
import { PageMeta } from "../types";
2022-04-07 13:21:30 +00:00
import { Space } from "./space";
2022-04-05 15:02:17 +00:00
// For testing in node.js
require("fake-indexeddb/auto");
test("Test store", async () => {
2022-04-07 13:21:30 +00:00
let primary = new Space(new IndexedDBSpacePrimitives("primary"), true);
let secondary = new Space(
new IndexedDBSpacePrimitives("secondary", -5000),
true
);
let sync = new SpaceSync(primary, secondary, 0, 0, "_trash/");
async function conflictResolver(pageMeta1: PageMeta, pageMeta2: PageMeta) {}
2022-04-05 15:02:17 +00:00
// Write one page to primary
2022-06-28 12:14:15 +00:00
await primary.writePage("index", "Hello");
2022-04-05 15:02:17 +00:00
expect((await secondary.listPages()).size).toBe(0);
await syncPages(conflictResolver);
2022-04-05 15:02:17 +00:00
expect((await secondary.listPages()).size).toBe(1);
2022-06-28 12:14:15 +00:00
expect((await secondary.readPage("index")).text).toBe("Hello");
2022-04-05 15:02:17 +00:00
// Should be a no-op
expect(await syncPages()).toBe(0);
2022-04-05 15:02:17 +00:00
// Now let's make a change on the secondary
2022-06-28 12:14:15 +00:00
await secondary.writePage("index", "Hello!!");
2022-04-05 15:02:17 +00:00
await secondary.writePage("test", "Test page");
// And sync it
await syncPages();
2022-04-05 15:02:17 +00:00
expect(primary.listPages().size).toBe(2);
expect(secondary.listPages().size).toBe(2);
2022-04-05 15:02:17 +00:00
2022-06-28 12:14:15 +00:00
expect((await primary.readPage("index")).text).toBe("Hello!!");
2022-04-05 15:02:17 +00:00
// Let's make some random edits on both ends
2022-06-28 12:14:15 +00:00
await primary.writePage("index", "1");
await primary.writePage("index2", "2");
await secondary.writePage("index3", "3");
await secondary.writePage("index4", "4");
await syncPages();
2022-04-05 15:02:17 +00:00
expect((await primary.listPages()).size).toBe(5);
expect((await secondary.listPages()).size).toBe(5);
expect(await syncPages()).toBe(0);
console.log("Deleting pages");
// Delete some pages
2022-06-28 12:14:15 +00:00
await primary.deletePage("index");
await primary.deletePage("index3");
console.log("Pages", await primary.listPages());
console.log("Trash", await primary.listTrash());
await syncPages();
2022-04-05 15:02:17 +00:00
expect((await primary.listPages()).size).toBe(3);
expect((await secondary.listPages()).size).toBe(3);
// No-op
expect(await syncPages()).toBe(0);
2022-06-28 12:14:15 +00:00
await secondary.deletePage("index4");
await primary.deletePage("index2");
await syncPages();
// Just "test" left
expect((await primary.listPages()).size).toBe(1);
expect((await secondary.listPages()).size).toBe(1);
// No-op
expect(await syncPages()).toBe(0);
2022-06-28 12:14:15 +00:00
await secondary.writePage("index", "I'm back");
await syncPages();
2022-06-28 12:14:15 +00:00
expect((await primary.readPage("index")).text).toBe("I'm back");
2022-04-05 15:02:17 +00:00
// Cause a conflict
2022-06-28 12:14:15 +00:00
await primary.writePage("index", "Hello 1");
await secondary.writePage("index", "Hello 2");
2022-04-05 15:02:17 +00:00
await syncPages(SpaceSync.primaryConflictResolver(primary, secondary));
// Sync conflicting copy back
await syncPages();
// Verify that primary won
2022-06-28 12:14:15 +00:00
expect((await primary.readPage("index")).text).toBe("Hello 1");
expect((await secondary.readPage("index")).text).toBe("Hello 1");
2022-06-28 12:14:15 +00:00
// test + index + index.conflicting copy
expect((await primary.listPages()).size).toBe(3);
expect((await secondary.listPages()).size).toBe(3);
async function syncPages(
conflictResolver?: (
pageMeta1: PageMeta,
pageMeta2: PageMeta
) => Promise<void>
): Promise<number> {
// Awesome practice: adding sleeps to fix issues!
await sleep(2);
let n = await sync.syncPages(conflictResolver);
await sleep(2);
return n;
}
2022-04-05 15:02:17 +00:00
});
function sleep(ms: number = 5): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}