UNPKG

machinomy

Version:

Micropayments powered by Ethereum

161 lines 9.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ChannelId_1 = require("../ChannelId"); const PaymentChannel_1 = require("../PaymentChannel"); const bignumber_js_1 = require("bignumber.js"); const expects_rejection_1 = require("../util/expects_rejection"); const expect = require("expect"); const tmpStorage_1 = require("../util/tmpStorage"); const sinon = require("sinon"); describe('ChannelsDatabase', () => { let channels; let inflator = { inflate: async (paymentChannelJSON) => { return new PaymentChannel_1.PaymentChannel(paymentChannelJSON.sender, paymentChannelJSON.receiver, paymentChannelJSON.channelId, paymentChannelJSON.value, paymentChannelJSON.spent, paymentChannelJSON.state, paymentChannelJSON.tokenContract); } }; let fakeContract; before(async () => { let storage = await tmpStorage_1.tmpStorage(inflator); await storage.migrator.sync(); channels = storage.channelsDatabase; }); beforeEach(async () => { fakeContract = {}; fakeContract.channelById = sinon.stub(); fakeContract.getState = () => { return Promise.resolve(0); }; fakeContract.channelById.resolves([null, null, '2']); }); describe('#updateState', () => { it('updates the state value', async () => { const id = ChannelId_1.default.random().toString(); await channels.save(new PaymentChannel_1.PaymentChannel('sender', 'receiver', id, new bignumber_js_1.BigNumber(69), new bignumber_js_1.BigNumber(8), 0, '')); await channels.updateState(id, 2); let chan = await channels.firstById(id); expect(chan).toBeTruthy(); expect(chan.state).toBe(2); }); }); describe('#spend', () => { it('update spent amount', async () => { const channelId = ChannelId_1.default.random(); const hexChannelId = channelId.toString(); const paymentChannel = new PaymentChannel_1.PaymentChannel('sender', 'receiver', hexChannelId, new bignumber_js_1.BigNumber(10), new bignumber_js_1.BigNumber(0), undefined, ''); const spent = new bignumber_js_1.BigNumber(33); await channels.save(paymentChannel); await channels.spend(channelId, spent); let updated = await channels.firstById(channelId); expect(updated.channelId).toBe(hexChannelId); expect(updated.spent).toEqual(spent); }); }); describe('#save and #firstById', () => { it('match', async () => { const channelId = ChannelId_1.default.random(); const hexChannelId = channelId.toString(); const paymentChannel = new PaymentChannel_1.PaymentChannel('sender', 'receiver', hexChannelId, new bignumber_js_1.BigNumber(10), new bignumber_js_1.BigNumber(0), 0, ''); await channels.save(paymentChannel); let saved = await channels.firstById(channelId); expect(saved.toString()).toBe(paymentChannel.toString()); }); }); describe('#firstById', () => { it('return null if not found', async () => { const channelId = ChannelId_1.default.random(); let found = await channels.firstById(channelId); expect(found).toBeNull(); }); }); describe('#saveOrUpdate', () => { it('save new PaymentChannel', async () => { const channelId = ChannelId_1.default.random(); const hexChannelId = channelId.toString(); const paymentChannel = new PaymentChannel_1.PaymentChannel('sender', 'receiver', hexChannelId, new bignumber_js_1.BigNumber(3), new bignumber_js_1.BigNumber(0), 0, ''); let notFound = await channels.firstById(channelId); expect(notFound).toBeNull(); await channels.saveOrUpdate(paymentChannel); let found = await channels.firstById(channelId); expect(JSON.stringify(found)).toBe(JSON.stringify(paymentChannel)); }); it('update spent value on existing PaymentChannel', async () => { const channelId = ChannelId_1.default.random(); const hexChannelId = channelId.toString(); const spent = new bignumber_js_1.BigNumber(5); const paymentChannel = new PaymentChannel_1.PaymentChannel('sender', 'receiver', hexChannelId, new bignumber_js_1.BigNumber(3), new bignumber_js_1.BigNumber(0), undefined); const updatedPaymentChannel = new PaymentChannel_1.PaymentChannel('sender', 'receiver', hexChannelId, new bignumber_js_1.BigNumber(3), spent, undefined); await channels.save(paymentChannel); await channels.saveOrUpdate(updatedPaymentChannel); let found = await channels.firstById(channelId); expect(found.spent).toEqual(spent); }); }); describe('#deposit', () => { it('updates the channel value to the sum of the old value and new', async () => { const channelId = ChannelId_1.default.random(); const hexChannelId = channelId.toString(); const newValue = new bignumber_js_1.BigNumber(15); const paymentChannel = new PaymentChannel_1.PaymentChannel('sender', 'receiver', hexChannelId, new bignumber_js_1.BigNumber(10), new bignumber_js_1.BigNumber(0), undefined, ''); await channels.save(paymentChannel); await channels.deposit(hexChannelId, new bignumber_js_1.BigNumber(5)); let found = await channels.firstById(channelId); expect(found.value).toEqual(newValue); }); it('throws an error if the channel does not exist', () => { return expects_rejection_1.default(channels.deposit('123-abc', new bignumber_js_1.BigNumber(10))); }); }); describe('#all', () => { it('return all the channels', async () => { const channelId = ChannelId_1.default.random(); const hexChannelId = channelId.toString(); const paymentChannel = new PaymentChannel_1.PaymentChannel('sender', 'receiver', hexChannelId, new bignumber_js_1.BigNumber(10), new bignumber_js_1.BigNumber(0), undefined, ''); await channels.save(paymentChannel); let found = await channels.all(); expect(found.map(p => p.channelId).includes(channelId.toString())).toBeTruthy(); }); }); describe('#allSettling', () => { it('returns all settling channels', () => { const channelId1 = ChannelId_1.default.random(); const channelId2 = ChannelId_1.default.random(); const hexChannelId1 = channelId1.toString(); const hexChannelId2 = channelId2.toString(); const paymentChannel1 = new PaymentChannel_1.PaymentChannel('sender', 'receiver', hexChannelId1, new bignumber_js_1.BigNumber(10), new bignumber_js_1.BigNumber(0), 0, ''); const paymentChannel2 = new PaymentChannel_1.PaymentChannel('sender', 'receiver', hexChannelId2, new bignumber_js_1.BigNumber(10), new bignumber_js_1.BigNumber(0), 1, ''); fakeContract.getState = sinon.stub().withArgs(hexChannelId2).resolves(1); return Promise.all([ channels.save(paymentChannel1), channels.save(paymentChannel2) ]).then(() => { return channels.allSettling(); }).then(found => { expect(found.map(p => p.channelId).includes(paymentChannel2.channelId.toString())).toBeTruthy(); }); }); }); describe('#allOpen', () => { it('returns all open channels', () => { const channelId1 = ChannelId_1.default.random(); const channelId2 = ChannelId_1.default.random(); const channelId3 = ChannelId_1.default.random(); const hexChannelId1 = channelId1.toString(); const hexChannelId2 = channelId2.toString(); const hexChannelId3 = channelId3.toString(); const paymentChannel1 = new PaymentChannel_1.PaymentChannel('sender', 'receiver', hexChannelId1, new bignumber_js_1.BigNumber(10), new bignumber_js_1.BigNumber(0), 0, ''); const paymentChannel2 = new PaymentChannel_1.PaymentChannel('sender', 'receiver', hexChannelId2, new bignumber_js_1.BigNumber(10), new bignumber_js_1.BigNumber(0), 1, ''); const paymentChannel3 = new PaymentChannel_1.PaymentChannel('sender', 'receiver', hexChannelId3, new bignumber_js_1.BigNumber(10), new bignumber_js_1.BigNumber(0), 2, ''); return Promise.all([ channels.save(paymentChannel1), channels.save(paymentChannel2), channels.save(paymentChannel3) ]).then(() => { return channels.allOpen(); }).then(found => { expect(found.map(p => p.channelId).includes(paymentChannel1.channelId.toString())).toBeTruthy(); }); }); }); }); //# sourceMappingURL=ChannelsDatabase.test.js.map