Skip to content

Commit

Permalink
chore: add utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Apr 18, 2019
2 parents be03d16 + d51a283 commit 0eaed4b
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions test/utils.spec.js
@@ -0,0 +1,78 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect

const utils = require('../src/utils')

describe('utils', () => {
it('randomSeqno', () => {
const first = utils.randomSeqno()
const second = utils.randomSeqno()

expect(first).to.have.length(20)
expect(second).to.have.length(20)
expect(first).to.not.eql(second)
})

it('msgId', () => {
expect(utils.msgId('hello', Buffer.from('world'))).to.be.eql('hello776f726c64')
})

it('msgId should not generate same ID for two different buffers', () => {
const peerId = 'QmPNdSYk5Rfpo5euNqwtyizzmKXMNHdXeLjTQhcN4yfX22'
const msgId0 = utils.msgId(peerId, Buffer.from('15603533e990dfde', 'hex'))
const msgId1 = utils.msgId(peerId, Buffer.from('15603533e990dfe0', 'hex'))
expect(msgId0).to.not.eql(msgId1)
})

it('anyMatch', () => {
[
[[1, 2, 3], [4, 5, 6], false],
[[1, 2], [1, 2], true],
[[1, 2, 3], [4, 5, 1], true],
[[5, 6, 1], [1, 2, 3], true],
[[], [], false],
[[1], [2], false]
].forEach((test) => {
expect(utils.anyMatch(new Set(test[0]), new Set(test[1])))
.to.eql(test[2])

expect(utils.anyMatch(new Set(test[0]), test[1]))
.to.eql(test[2])
})
})

it('ensureArray', () => {
expect(utils.ensureArray('hello')).to.be.eql(['hello'])
expect(utils.ensureArray([1, 2])).to.be.eql([1, 2])
})

it('converts an IN msg.from to b58', () => {
let binaryId = Buffer.from('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'hex')
let stringId = 'QmdZEWgtaWAxBh93fELFT298La1rsZfhiC2pqwMVwy3jZM'
const m = [
{ from: binaryId },
{ from: stringId }
]
const expected = [
{ from: stringId },
{ from: stringId }
]
expect(utils.normalizeInRpcMessages(m)).to.deep.eql(expected)
})

it('converts an OUT msg.from to binary', () => {
let binaryId = Buffer.from('1220e2187eb3e6c4fb3e7ff9ad4658610624a6315e0240fc6f37130eedb661e939cc', 'hex')
let stringId = 'QmdZEWgtaWAxBh93fELFT298La1rsZfhiC2pqwMVwy3jZM'
const m = [
{ from: binaryId },
{ from: stringId }
]
const expected = [
{ from: binaryId },
{ from: binaryId }
]
expect(utils.normalizeOutRpcMessages(m)).to.deep.eql(expected)
})
})

0 comments on commit 0eaed4b

Please sign in to comment.