@x5e/gink
Version:
an eventually consistent database
124 lines • 5.92 kB
JavaScript
;
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const implementation_1 = require("../implementation");
const utils_1 = require("../implementation/utils");
it("include and exclude work as intended", async function () {
for (const store of [
new implementation_1.IndexedDbStore("Group.test1", true),
new implementation_1.MemoryStore(true),
]) {
const instance = new implementation_1.Database(store);
await instance.ready;
const group1 = await instance.createGroup();
const box1 = await instance.createBox();
const box2 = await instance.createBox();
await group1.include(box1);
(0, utils_1.ensure)(await group1.isIncluded(box1), `Doesn't contain box1`);
(0, utils_1.ensure)(await group1.isIncluded(box1.address));
await group1.exclude(box1);
(0, utils_1.ensure)(!(await group1.isIncluded(box1)), `Still contains box1 after exclude`);
// Testing a container can be excluded before it was included.
await group1.exclude(box2);
let found = false;
const box2MuidStr = (0, utils_1.muidToString)(box2.address);
for (const entry of await store.getAllEntries()) {
if (Array.isArray(entry.storageKey) &&
entry.storageKey.length === 3) {
if (box2MuidStr ===
(0, utils_1.muidToString)((0, utils_1.muidTupleToMuid)(entry.storageKey))) {
found = true;
}
}
}
(0, utils_1.ensure)(found);
}
});
it("contains, toArray, and getMembers work properly", async function () {
var _a, e_1, _b, _c;
for (const store of [
new implementation_1.IndexedDbStore("Group.test2", true),
new implementation_1.MemoryStore(true),
]) {
const instance = new implementation_1.Database(store);
await instance.ready;
const group1 = await instance.createGroup();
const box1 = await instance.createBox();
const box2 = await instance.createBox();
const box3 = await instance.createBox();
await group1.include(box1);
await group1.include(box2);
(0, utils_1.ensure)(await group1.isIncluded(box1));
(0, utils_1.ensure)(!(await group1.isIncluded(box3)));
(0, utils_1.ensure)((await group1.size()) === 2);
(0, utils_1.ensure)((await group1.includedAsArray()).length === 2);
(0, utils_1.ensure)((await group1.includedAsArray())[0].behavior);
try {
for (var _d = true, _e = (e_1 = void 0, __asyncValues(group1.getMembers())), _f; _f = await _e.next(), _a = _f.done, !_a;) {
_c = _f.value;
_d = false;
try {
const member = _c;
(0, utils_1.ensure)(member.address && member.behavior && member.database);
}
finally {
_d = true;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
}
finally { if (e_1) throw e_1.error; }
}
}
});
it("Group.reset", async function () {
for (const store of [
new implementation_1.IndexedDbStore("Group.test3", true),
new implementation_1.MemoryStore(true),
]) {
const instance = new implementation_1.Database(store);
await instance.ready;
const box1 = await instance.createBox();
const box2 = await instance.createBox();
const group = await instance.createGroup();
const prop1 = await instance.createProperty();
const prop2 = await instance.createProperty();
await prop1.set(group, "foo");
await prop2.set(group, "bar");
await group.include(box1);
const afterOne = (0, utils_1.generateTimestamp)();
await group.include(box2);
await prop1.set(group, "foo2");
await prop2.set(group, "bar2");
(0, utils_1.ensure)(await group.isIncluded(box2));
await group.reset({ toTime: afterOne });
(0, utils_1.ensure)((await prop1.get(group)) === "foo");
(0, utils_1.ensure)((await prop2.get(group)) === "bar");
(0, utils_1.ensure)(!(await group.isIncluded(box2)));
(0, utils_1.ensure)(await group.isIncluded(box1));
await group.reset();
(0, utils_1.ensure)((await group.size()) === 0);
(0, utils_1.ensure)((await prop1.get(group)) === undefined);
(0, utils_1.ensure)((await prop2.get(group)) === undefined);
await group.include(box1);
await group.include(box2);
const beforeExclude = (0, utils_1.generateTimestamp)();
await group.exclude(box1);
await group.reset({ toTime: beforeExclude, skipProperties: true });
(0, utils_1.ensure)(await group.isIncluded(box1));
(0, utils_1.ensure)(await group.isIncluded(box2));
(0, utils_1.ensure)((await prop1.get(group)) === undefined);
(0, utils_1.ensure)((await prop2.get(group)) === undefined);
}
});
//# sourceMappingURL=Group.test.js.map