UNPKG

@x5e/gink

Version:

an eventually consistent database

113 lines 6.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const implementation_1 = require("../implementation"); const utils_1 = require("../implementation/utils"); it("test reset", async function () { for (const store of [ new implementation_1.IndexedDbStore("Keyed.reset", true), new implementation_1.MemoryStore(true), ]) { const database = new implementation_1.Database({ store }); await database.ready; const box = await implementation_1.Box.create(database); const pairMap = await implementation_1.PairMap.create(database); const schema = await implementation_1.Directory.create(database); await schema.set("a key", "a value"); await pairMap.set([box, schema], "a value"); const afterFirst = (0, utils_1.generateTimestamp)(); await schema.set("another key", "another value"); await pairMap.set([schema, box], "reversed"); (0, utils_1.ensure)((await schema.get("another key")) === "another value"); (0, utils_1.ensure)((await pairMap.get([box, schema])) === "a value"); (0, utils_1.ensure)((await pairMap.get([schema, box])) === "reversed"); const afterbox = (0, utils_1.generateTimestamp)(); // Reset when first entry is still there await schema.reset(afterFirst); (0, utils_1.ensure)((await schema.get("another key")) === undefined); (0, utils_1.ensure)((await schema.get("a key")) === "a value"); (0, utils_1.ensure)((await pairMap.get([box, schema])) === "a value"); (0, utils_1.ensure)((await pairMap.get([schema, box])) === "reversed"); // Reset to epoch await schema.reset(); (0, utils_1.ensure)((await schema.get("another key")) === undefined); (0, utils_1.ensure)((await schema.get("a key")) === undefined); (0, utils_1.ensure)((await pairMap.get([box, schema])) === "a value"); (0, utils_1.ensure)((await pairMap.get([schema, box])) === "reversed"); await schema.reset(afterFirst); (0, utils_1.ensure)((await schema.get("another key")) === undefined); (0, utils_1.ensure)((await schema.get("a key")) === "a value"); (0, utils_1.ensure)((await pairMap.get([box, schema])) === "a value"); (0, utils_1.ensure)((await pairMap.get([schema, box])) === "reversed"); await pairMap.reset(afterFirst); (0, utils_1.ensure)((await pairMap.get([box, schema])) === "a value"); (0, utils_1.ensure)((await pairMap.get([schema, box])) === undefined); await pairMap.reset(afterbox); (0, utils_1.ensure)((await pairMap.get([box, schema])) === "a value"); (0, utils_1.ensure)((await pairMap.get([schema, box])) === "reversed"); (0, utils_1.ensure)((await schema.get("another key")) === undefined); (0, utils_1.ensure)((await schema.get("a key")) === "a value"); // Test recursive reset const child = await implementation_1.Directory.create(database); const childOfChild = await implementation_1.Directory.create(database); await child.set("childOfChild", childOfChild); await child.set("random key", "random"); await childOfChild.set("key", "value"); await schema.set("child", child); const afterInit = (0, utils_1.generateTimestamp)(); await childOfChild.set("key", "changed"); await child.set("random key", "changed"); const afterChanged = (0, utils_1.generateTimestamp)(); await schema.reset(afterInit, true); (0, utils_1.ensure)((await childOfChild.get("key")) === "value"); (0, utils_1.ensure)((await child.get("random key")) === "random"); (0, utils_1.ensure)((await schema.get("child")) instanceof implementation_1.Directory); await schema.clear(); (0, utils_1.ensure)((await schema.size()) === 0); // Reset after a clear await schema.reset(afterInit, true); (0, utils_1.ensure)((await childOfChild.get("key")) === "value"); (0, utils_1.ensure)((await child.get("random key")) === "random"); (0, utils_1.ensure)((await schema.get("child")) instanceof implementation_1.Directory); // Same reset again, should not change anything await schema.reset(afterInit, true); (0, utils_1.ensure)((await childOfChild.get("key")) === "value"); (0, utils_1.ensure)((await child.get("random key")) === "random"); (0, utils_1.ensure)((await schema.get("child")) instanceof implementation_1.Directory); // Make sure a deletion doesn't cause problems await schema.delete("child"); (0, utils_1.ensure)((await schema.get("child")) === undefined); await schema.reset(afterInit, true); (0, utils_1.ensure)((await childOfChild.get("key")) === "value"); (0, utils_1.ensure)((await child.get("random key")) === "random"); (0, utils_1.ensure)((await schema.get("child")) instanceof implementation_1.Directory); // Recurse = false should not reset children await schema.reset(afterChanged); (0, utils_1.ensure)((await childOfChild.get("key")) === "value"); (0, utils_1.ensure)((await child.get("random key")) === "random"); (0, utils_1.ensure)((await schema.get("child")) instanceof implementation_1.Directory); // Test circular references doesn't cause infinite loops await box.set(schema); await schema.set("circle", box); const resetTo = (0, utils_1.generateTimestamp)(); await schema.set("circle", "not a box"); await schema.reset(resetTo, true); (0, utils_1.ensure)((await schema.get("circle")) instanceof implementation_1.Box); (0, utils_1.ensure)((await box.get()) instanceof implementation_1.Directory); // Test non-immediate reset const arr = [1, 2, 3]; await schema.set("hmm", 2); await schema.set(2, arr); const afterNumbers = (0, utils_1.generateTimestamp)(); await schema.set("hmm", 20); const bundler = await database.startBundle(); await schema.reset(afterNumbers, false, { bundler }); (0, utils_1.ensure)((await schema.get("hmm")) === 20); (0, utils_1.ensure)((0, lodash_1.isEqual)(await schema.get(2), arr)); await bundler.commit(); (0, utils_1.ensure)((await schema.get("hmm")) === 2, await schema.toJson()); (0, utils_1.ensure)((0, lodash_1.isEqual)(await schema.get(2), arr)); await store.close(); } }); //# sourceMappingURL=Keyed.test.js.map