Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should emit changes when the database is updated', async () => {
const db = new StateDB();
const changes: StateDB.Change[] = [
{ id: 'foo', type: 'save' },
{ id: 'foo', type: 'remove' },
{ id: 'bar', type: 'save' },
{ id: 'bar', type: 'remove' }
];
const recorded: StateDB.Change[] = [];
db.changed.connect((_, change) => {
recorded.push(change);
});
await db.save('foo', 0);
await db.remove('foo');
await db.save('bar', 1);
await db.remove('bar');
it('should remove a stored key', async () => {
const db = new StateDB();
const key = 'foo:bar';
const value = { baz: 'qux' };
expect(await db.fetch(key)).to.be.undefined;
await db.save(key, value);
expect(await db.fetch(key)).to.deep.equal(value);
await db.remove(key);
expect(await db.fetch(key)).to.be.undefined;
});
});
it('should create a state database', () => {
const db = new StateDB();
expect(db).to.be.an.instanceof(StateDB);
});
it('should fetch a stored namespace', async () => {
const db = new StateDB();
const keys = [
'foo:bar',
'foo:baz',
'foo:qux',
'abc:def',
'abc:ghi',
'abc:jkl'
];
await Promise.all(keys.map(key => db.save(key, { value: key })));
let fetched = await db.list('foo');
expect(fetched.ids.length).to.equal(3);
expect(fetched.values.length).to.equal(3);
let sorted = fetched.ids.sort((a, b) => a.localeCompare(b));
it('should fetch a stored key', async () => {
const db = new StateDB();
const key = 'foo:bar';
const value = { baz: 'qux' };
expect(await db.fetch(key)).to.be.undefined;
await db.save(key, value);
expect(await db.fetch(key)).to.deep.equal(value);
});
});
it('should allow a merge data transformation', async () => {
const connector = new StateDB.Connector();
const k1 = 'foo';
const v1 = 'bar';
const k2 = 'baz';
const v2 = 'qux';
expect(await connector.fetch(k1)).to.be.undefined;
expect(await connector.fetch(k2)).to.be.undefined;
await connector.save(k1, `{ "v": "${v1}"}`);
expect(JSON.parse(await connector.fetch(k1)).v).to.equal(v1);
const transform = new PromiseDelegate();
const db = new StateDB({ connector, transform: transform.promise });
const transformation: StateDB.DataTransform = {
type: 'merge',
contents: { [k2]: v2 }
};
transform.resolve(transformation);
await transform.promise;
expect(await db.fetch(k1)).to.equal(v1);
expect(await db.fetch(k2)).to.equal(v2);
});
});
it('should empty the items in a state database', async () => {
const connector = new StateDB.Connector();
const db = new StateDB({ connector });
expect((await connector.list()).ids).to.be.empty;
await db.save('foo', 'bar');
expect((await connector.list()).ids).not.to.be.empty;
await db.clear();
expect((await connector.list()).ids).to.be.empty;
});
});
it('return the full contents of a state database', async () => {
const db = new StateDB();
const contents: ReadonlyJSONObject = {
abc: 'def',
ghi: 'jkl',
mno: 1,
pqr: {
foo: { bar: { baz: 'qux' } }
}
};
await Promise.all(
Object.keys(contents).map(key => db.save(key, contents[key]))
);
const serialized = await db.toJSON();
expect(serialized).to.deep.equal(contents);
});
});