@x5e/gink
Version:
an eventually consistent database
81 lines • 3.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const implementation_1 = require("../implementation");
const utils_1 = require("../implementation/utils");
it("test missing container", async () => {
for (const store of [
new implementation_1.IndexedDbStore("test missing container", true),
new implementation_1.MemoryStore(true),
]) {
const instance = new implementation_1.Database({ store });
await instance.ready;
const missingMuid = {
timestamp: 123456789,
offset: 1234,
medallion: 3234324,
};
const container = await (0, implementation_1.construct)(missingMuid, instance);
(0, utils_1.ensure)(!container); // expected missing container to return null
}
});
it("complex.toJSON", async function () {
for (const store of [
new implementation_1.IndexedDbStore("toJSON", true),
new implementation_1.MemoryStore(true),
]) {
const instance = new implementation_1.Database({ store });
await instance.ready;
const directory = await implementation_1.Directory.create(instance);
await directory.set("foo", "bar");
await directory.set("bar", 3);
const a_document = new Map()
.set("a date", new Date(1665892249196))
.set("some bytes", new Uint8Array([94, 32]))
.set("an array", [1, 3, true, false, null])
.set("sub object", new Map().set("key", "value"));
if (!(a_document instanceof Map)) {
throw Error("unexpected");
}
await directory.set("document", a_document);
await directory.set("tuple", ["yes"]);
const asJson = await directory.toJson();
const fromJson = JSON.parse(asJson);
// This is a little awkward, but MemoryStore holds entries in order,
// so toJson comes out in a different order than IndexedDb.
(0, utils_1.ensure)(fromJson.foo === "bar", fromJson.foo);
(0, utils_1.ensure)(fromJson.bar === 3, fromJson.bar);
(0, utils_1.ensure)(fromJson.document["a date"] === "2022-10-16T03:50:49.196Z", fromJson.document);
// null won't be included in array.toString()
(0, utils_1.ensure)(fromJson.document["an array"].toString() === "1,3,true,false,", fromJson.document["an array"].toString());
(0, utils_1.ensure)(fromJson.document["some bytes"].toString() === "5E20", fromJson.document);
(0, utils_1.ensure)(fromJson.document["sub object"].key === "value", fromJson.document);
(0, utils_1.ensure)(fromJson.tuple.toString() === "yes", fromJson.tuple);
// TODO: figure out why Map objects don't come out properly
const pulledOut = await directory.get("document");
(0, utils_1.ensure)(pulledOut[Symbol.toStringTag] === "Map"); // true
// ensure(pulledOut instanceof Map); // false
}
});
it("various.contents", async function () {
for (const store of [
new implementation_1.IndexedDbStore("contents", true),
new implementation_1.MemoryStore(true),
]) {
const instance = new implementation_1.Database({ store });
await instance.ready;
const box = await implementation_1.Box.create(instance);
const property = await implementation_1.Property.create(instance);
await box.set(property);
let found = await box.get();
(0, utils_1.ensure)(property.equals(found));
const pairSet = await implementation_1.PairSet.create(instance);
await box.set(pairSet);
found = await box.get();
(0, utils_1.ensure)(pairSet.equals(found));
const directory = implementation_1.Directory.get(instance);
await box.set(directory);
found = await box.get();
(0, utils_1.ensure)(directory.equals(found));
}
});
//# sourceMappingURL=factories.test.js.map