|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const expect = require('chai').expect |
| 5 | + |
| 6 | +const utils = require('../src/utils') |
| 7 | + |
| 8 | +describe('utils', () => { |
| 9 | + it('randomSeqno', () => { |
| 10 | + const first = utils.randomSeqno() |
| 11 | + const second = utils.randomSeqno() |
| 12 | + |
| 13 | + expect(first).to.have.length(20) |
| 14 | + expect(second).to.have.length(20) |
| 15 | + expect(first).to.not.eql(second) |
| 16 | + }) |
| 17 | + |
| 18 | + it('msgId', () => { |
| 19 | + expect(utils.msgId('hello', Buffer.from('world'))).to.be.eql('hello776f726c64') |
| 20 | + }) |
| 21 | + |
| 22 | + it('msgId should not generate same ID for two different buffers', () => { |
| 23 | + const peerId = 'QmPNdSYk5Rfpo5euNqwtyizzmKXMNHdXeLjTQhcN4yfX22' |
| 24 | + const msgId0 = utils.msgId(peerId, Buffer.from('15603533e990dfde', 'hex')) |
| 25 | + const msgId1 = utils.msgId(peerId, Buffer.from('15603533e990dfe0', 'hex')) |
| 26 | + expect(msgId0).to.not.eql(msgId1) |
| 27 | + }) |
| 28 | + |
| 29 | + it('anyMatch', () => { |
| 30 | + [ |
| 31 | + [[1, 2, 3], [4, 5, 6], false], |
| 32 | + [[1, 2], [1, 2], true], |
| 33 | + [[1, 2, 3], [4, 5, 1], true], |
| 34 | + [[5, 6, 1], [1, 2, 3], true], |
| 35 | + [[], [], false], |
| 36 | + [[1], [2], false] |
| 37 | + ].forEach((test) => { |
| 38 | + expect(utils.anyMatch(new Set(test[0]), new Set(test[1]))) |
| 39 | + .to.eql(test[2]) |
| 40 | + |
| 41 | + expect(utils.anyMatch(new Set(test[0]), test[1])) |
| 42 | + .to.eql(test[2]) |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + it('ensureArray', () => { |
| 47 | + expect(utils.ensureArray('hello')).to.be.eql(['hello']) |
| 48 | + expect(utils.ensureArray([1, 2])).to.be.eql([1, 2]) |
| 49 | + }) |
| 50 | + |
| 51 | + it('converts an IN msg.from to b58', () => { |
| 52 | + let binaryId = Buffer.from('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'hex') |
| 53 | + let stringId = 'QmdZEWgtaWAxBh93fELFT298La1rsZfhiC2pqwMVwy3jZM' |
| 54 | + const m = [ |
| 55 | + { from: binaryId }, |
| 56 | + { from: stringId } |
| 57 | + ] |
| 58 | + const expected = [ |
| 59 | + { from: stringId }, |
| 60 | + { from: stringId } |
| 61 | + ] |
| 62 | + expect(utils.normalizeInRpcMessages(m)).to.deep.eql(expected) |
| 63 | + }) |
| 64 | + |
| 65 | + it('converts an OUT msg.from to binary', () => { |
| 66 | + let binaryId = Buffer.from('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'hex') |
| 67 | + let stringId = 'QmdZEWgtaWAxBh93fELFT298La1rsZfhiC2pqwMVwy3jZM' |
| 68 | + const m = [ |
| 69 | + { from: binaryId }, |
| 70 | + { from: stringId } |
| 71 | + ] |
| 72 | + const expected = [ |
| 73 | + { from: binaryId }, |
| 74 | + { from: binaryId } |
| 75 | + ] |
| 76 | + expect(utils.normalizeOutRpcMessages(m)).to.deep.eql(expected) |
| 77 | + }) |
| 78 | +}) |
0 commit comments