2023-01-16 17:55:35 +00:00
|
|
|
import { removeDirectiveBody, SpaceSync, SyncStatusItem } from "./sync.ts";
|
2023-01-13 14:41:29 +00:00
|
|
|
import { DiskSpacePrimitives } from "./disk_space_primitives.ts";
|
|
|
|
import { assertEquals } from "../../test_deps.ts";
|
|
|
|
|
|
|
|
Deno.test("Test store", async () => {
|
|
|
|
const primaryPath = await Deno.makeTempDir();
|
|
|
|
const secondaryPath = await Deno.makeTempDir();
|
|
|
|
console.log("Primary", primaryPath);
|
|
|
|
console.log("Secondary", secondaryPath);
|
|
|
|
const primary = new DiskSpacePrimitives(primaryPath);
|
|
|
|
const secondary = new DiskSpacePrimitives(secondaryPath);
|
2023-05-23 18:53:53 +00:00
|
|
|
const snapshot = new Map<string, SyncStatusItem>();
|
|
|
|
const sync = new SpaceSync(primary, secondary, {
|
|
|
|
conflictResolver: SpaceSync.primaryConflictResolver,
|
|
|
|
});
|
2023-01-13 14:41:29 +00:00
|
|
|
|
|
|
|
// Write one page to primary
|
2023-05-23 18:53:53 +00:00
|
|
|
await primary.writeFile("index", stringToBytes("Hello"));
|
2023-01-13 14:41:29 +00:00
|
|
|
assertEquals((await secondary.fetchFileList()).length, 0);
|
|
|
|
console.log("Initial sync ops", await doSync());
|
|
|
|
|
|
|
|
assertEquals((await secondary.fetchFileList()).length, 1);
|
2023-05-23 18:53:53 +00:00
|
|
|
assertEquals(
|
|
|
|
(await secondary.readFile("index")).data,
|
|
|
|
stringToBytes("Hello"),
|
|
|
|
);
|
2023-01-13 14:41:29 +00:00
|
|
|
|
|
|
|
// Should be a no-op
|
|
|
|
assertEquals(await doSync(), 0);
|
|
|
|
|
|
|
|
// Now let's make a change on the secondary
|
2023-05-23 18:53:53 +00:00
|
|
|
await secondary.writeFile("index", stringToBytes("Hello!!"));
|
|
|
|
await secondary.writeFile("test", stringToBytes("Test page"));
|
2023-01-13 14:41:29 +00:00
|
|
|
|
|
|
|
// And sync it
|
|
|
|
await doSync();
|
|
|
|
|
|
|
|
assertEquals((await primary.fetchFileList()).length, 2);
|
|
|
|
assertEquals((await secondary.fetchFileList()).length, 2);
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
assertEquals(
|
|
|
|
(await primary.readFile("index")).data,
|
|
|
|
stringToBytes("Hello!!"),
|
|
|
|
);
|
2023-01-13 14:41:29 +00:00
|
|
|
|
|
|
|
// Let's make some random edits on both ends
|
2023-05-23 18:53:53 +00:00
|
|
|
await primary.writeFile("index", stringToBytes("1"));
|
|
|
|
await primary.writeFile("index2", stringToBytes("2"));
|
|
|
|
await secondary.writeFile("index3", stringToBytes("3"));
|
|
|
|
await secondary.writeFile("index4", stringToBytes("4"));
|
2023-01-13 14:41:29 +00:00
|
|
|
await doSync();
|
|
|
|
|
|
|
|
assertEquals((await primary.fetchFileList()).length, 5);
|
|
|
|
assertEquals((await secondary.fetchFileList()).length, 5);
|
|
|
|
|
|
|
|
assertEquals(await doSync(), 0);
|
|
|
|
|
|
|
|
console.log("Deleting pages");
|
|
|
|
// Delete some pages
|
|
|
|
await primary.deleteFile("index");
|
|
|
|
await primary.deleteFile("index3");
|
|
|
|
|
|
|
|
await doSync();
|
|
|
|
|
|
|
|
assertEquals((await primary.fetchFileList()).length, 3);
|
|
|
|
assertEquals((await secondary.fetchFileList()).length, 3);
|
|
|
|
|
|
|
|
// No-op
|
|
|
|
assertEquals(await doSync(), 0);
|
|
|
|
|
|
|
|
await secondary.deleteFile("index4");
|
|
|
|
await primary.deleteFile("index2");
|
|
|
|
|
|
|
|
await doSync();
|
|
|
|
|
|
|
|
// Just "test" left
|
|
|
|
assertEquals((await primary.fetchFileList()).length, 1);
|
|
|
|
assertEquals((await secondary.fetchFileList()).length, 1);
|
|
|
|
|
|
|
|
// No-op
|
|
|
|
assertEquals(await doSync(), 0);
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
await secondary.writeFile("index", stringToBytes("I'm back"));
|
2023-01-13 14:41:29 +00:00
|
|
|
|
|
|
|
await doSync();
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
assertEquals(
|
|
|
|
(await primary.readFile("index")).data,
|
|
|
|
stringToBytes("I'm back"),
|
|
|
|
);
|
2023-01-13 14:41:29 +00:00
|
|
|
|
|
|
|
// Cause a conflict
|
|
|
|
console.log("Introducing a conflict now");
|
2023-05-23 18:53:53 +00:00
|
|
|
await primary.writeFile("index", stringToBytes("Hello 1"));
|
|
|
|
await secondary.writeFile("index", stringToBytes("Hello 2"));
|
2023-01-13 14:41:29 +00:00
|
|
|
|
|
|
|
await doSync();
|
|
|
|
|
|
|
|
// Sync conflicting copy back
|
|
|
|
await doSync();
|
|
|
|
|
|
|
|
// Verify that primary won
|
2023-05-23 18:53:53 +00:00
|
|
|
assertEquals(
|
|
|
|
(await primary.readFile("index")).data,
|
|
|
|
stringToBytes("Hello 1"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
(await secondary.readFile("index")).data,
|
|
|
|
stringToBytes("Hello 1"),
|
|
|
|
);
|
2023-01-13 14:41:29 +00:00
|
|
|
|
|
|
|
// test + index + index.conflicting copy
|
|
|
|
assertEquals((await primary.fetchFileList()).length, 3);
|
|
|
|
assertEquals((await secondary.fetchFileList()).length, 3);
|
|
|
|
|
|
|
|
// Introducing a fake conflict (same content, so not really conflicting)
|
2023-05-23 18:53:53 +00:00
|
|
|
await primary.writeFile("index", stringToBytes("Hello 1"));
|
|
|
|
await secondary.writeFile("index", stringToBytes("Hello 1"));
|
2023-01-13 14:41:29 +00:00
|
|
|
|
2023-01-16 17:55:35 +00:00
|
|
|
// And two more files with different bodies, but only within a query directive — shouldn't conflict
|
|
|
|
await primary.writeFile(
|
|
|
|
"index.md",
|
2023-05-23 18:53:53 +00:00
|
|
|
stringToBytes(
|
|
|
|
"Hello\n<!-- #query page -->\nHello 1\n<!-- /query -->",
|
|
|
|
),
|
2023-01-16 17:55:35 +00:00
|
|
|
);
|
|
|
|
await secondary.writeFile(
|
|
|
|
"index.md",
|
2023-05-23 18:53:53 +00:00
|
|
|
stringToBytes("Hello\n<!-- #query page -->\nHello 2\n<!-- /query -->"),
|
2023-01-16 17:55:35 +00:00
|
|
|
);
|
|
|
|
|
2023-01-13 14:41:29 +00:00
|
|
|
await doSync();
|
|
|
|
await doSync();
|
|
|
|
|
2023-01-16 17:55:35 +00:00
|
|
|
// test + index + index.md + previous index.conflicting copy but nothing more
|
|
|
|
assertEquals((await primary.fetchFileList()).length, 4);
|
2023-01-13 14:41:29 +00:00
|
|
|
|
|
|
|
console.log("Bringing a third device in the mix");
|
|
|
|
|
|
|
|
const ternaryPath = await Deno.makeTempDir();
|
|
|
|
|
|
|
|
console.log("Ternary", ternaryPath);
|
|
|
|
|
|
|
|
const ternary = new DiskSpacePrimitives(ternaryPath);
|
|
|
|
const sync2 = new SpaceSync(
|
|
|
|
secondary,
|
|
|
|
ternary,
|
2023-05-23 18:53:53 +00:00
|
|
|
{
|
|
|
|
conflictResolver: SpaceSync.primaryConflictResolver,
|
|
|
|
},
|
2023-01-13 14:41:29 +00:00
|
|
|
);
|
2023-05-23 18:53:53 +00:00
|
|
|
const snapshot2 = new Map<string, SyncStatusItem>();
|
2023-01-20 15:08:01 +00:00
|
|
|
console.log(
|
|
|
|
"N ops",
|
2023-05-23 18:53:53 +00:00
|
|
|
await sync2.syncFiles(snapshot2),
|
2023-01-20 15:08:01 +00:00
|
|
|
);
|
2023-01-13 14:41:29 +00:00
|
|
|
await sleep(2);
|
2023-05-23 18:53:53 +00:00
|
|
|
assertEquals(await sync2.syncFiles(snapshot2), 0);
|
2023-01-13 14:41:29 +00:00
|
|
|
|
2023-02-28 10:13:18 +00:00
|
|
|
// I had to look up what follows ternary (https://english.stackexchange.com/questions/25116/what-follows-next-in-the-sequence-unary-binary-ternary)
|
|
|
|
const quaternaryPath = await Deno.makeTempDir();
|
|
|
|
const quaternary = new DiskSpacePrimitives(quaternaryPath);
|
|
|
|
const sync3 = new SpaceSync(
|
|
|
|
secondary,
|
|
|
|
quaternary,
|
|
|
|
{
|
2023-05-23 18:53:53 +00:00
|
|
|
isSyncCandidate: (path) => !path.startsWith("index"),
|
|
|
|
conflictResolver: SpaceSync.primaryConflictResolver,
|
2023-02-28 10:13:18 +00:00
|
|
|
},
|
|
|
|
);
|
2023-05-23 18:53:53 +00:00
|
|
|
const selectingOps = await sync3.syncFiles(new Map());
|
2023-02-28 10:13:18 +00:00
|
|
|
|
|
|
|
assertEquals(selectingOps, 1);
|
|
|
|
|
2023-01-13 14:41:29 +00:00
|
|
|
await Deno.remove(primaryPath, { recursive: true });
|
|
|
|
await Deno.remove(secondaryPath, { recursive: true });
|
|
|
|
await Deno.remove(ternaryPath, { recursive: true });
|
2023-02-28 10:13:18 +00:00
|
|
|
await Deno.remove(quaternaryPath, { recursive: true });
|
2023-01-13 14:41:29 +00:00
|
|
|
|
|
|
|
async function doSync() {
|
|
|
|
await sleep();
|
2023-05-23 18:53:53 +00:00
|
|
|
const r = await sync.syncFiles(snapshot);
|
2023-01-13 14:41:29 +00:00
|
|
|
await sleep();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function sleep(ms = 10): Promise<void> {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
|
|
|
}
|
2023-01-16 17:55:35 +00:00
|
|
|
|
|
|
|
Deno.test("Remove directive bodies", () => {
|
|
|
|
assertEquals(
|
|
|
|
removeDirectiveBody(`<!-- #query page -->
|
|
|
|
This is a body
|
|
|
|
bla bla bla
|
|
|
|
<!-- /query -->
|
|
|
|
Hello
|
|
|
|
<!-- #include [[test]] -->
|
|
|
|
This is a body
|
|
|
|
<!-- /include -->
|
|
|
|
`),
|
|
|
|
`<!-- #query page -->
|
|
|
|
<!-- /query -->
|
|
|
|
Hello
|
|
|
|
<!-- #include [[test]] -->
|
|
|
|
<!-- /include -->
|
|
|
|
`,
|
|
|
|
);
|
|
|
|
});
|
2023-05-23 18:53:53 +00:00
|
|
|
|
|
|
|
function stringToBytes(s: string): Uint8Array {
|
|
|
|
return new TextEncoder().encode(s);
|
|
|
|
}
|