Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: add first pass of pubsub tests (running in js-ipfs-api)
Browse files Browse the repository at this point in the history
  • Loading branch information
haadcode authored and daviddias committed Dec 21, 2016
1 parent 56cd45e commit 74003a7
Show file tree
Hide file tree
Showing 3 changed files with 610 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ exports.generic = require('./generic')
exports.swarm = require('./swarm')
exports.block = require('./block')
exports.dht = require('./dht')
exports.pubsub = require('./pubsub')
exports.pubsubMessage = require('./pubsub-message')
100 changes: 100 additions & 0 deletions src/pubsub-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const isNode = require('detect-node')

// NOTE!
// (Most of) these tests are skipped for now until we figure out the
// final data types for the messages coming over the wire

const topicName = 'js-ipfs-api-tests'

module.exports = (common, deps) => {
// Make sure the needed dependencies are injected
expect(deps.PubsubMessage).to.exist
expect(deps.PubsubMessageUtils).to.exist

const PubsubMessage = deps.PubsubMessage // eslint-disable-line no-unused-vars
const PubsubMessageUtils = deps.PubsubMessageUtils // eslint-disable-line no-unused-vars

// TESTS
describe('.pubsub-message', () => {
if (!isNode) {
return
}

it.skip('create message', () => {
// TODO
})

it.skip('deserialize message from JSON object', () => {
const obj = {
from: 'BI:ۛv�m�uyѱ����tU�+��#���V',
data: 'aGk=',
seqno: 'FIlj2BpyEgI=',
topicIDs: [ topicName ]
}
try {
const message = PubsubMessageUtils.deserialize(obj)
expect(message.from).to.equal('AAA')
expect(message.data).to.equal('hi')
expect(message.seqno).to.equal('\u0014�c�\u001ar\u0012\u0002')
expect(message.topicIDs.length).to.equal(1)
expect(message.topicIDs[0]).to.equal(topicName)
} catch (e) {
expect(e).to.not.exist
}
})

describe('immutable properties', () => {
const sender = 'A'
const data = 'hello'
const seqno = '123'
const topicIDs = ['hello world']

const message = PubsubMessageUtils.create(sender, data, seqno, topicIDs)

it('from', () => {
try {
message.from = 'not allowed'
} catch (e) {
expect(e).to.be.an('error')
expect(e.toString()).to.equal(`TypeError: Cannot set property from of #<PubsubMessage> which has only a getter`)
}
expect(message.from).to.equal(sender)
})

it('data', () => {
try {
message.data = 'not allowed'
} catch (e) {
expect(e).to.be.an('error')
expect(e.toString()).to.equal(`TypeError: Cannot set property data of #<PubsubMessage> which has only a getter`)
}
expect(message.data).to.equal(data)
})

it('seqno', () => {
try {
message.seqno = 'not allowed'
} catch (e) {
expect(e).to.be.an('error')
expect(e.toString()).to.equal(`TypeError: Cannot set property seqno of #<PubsubMessage> which has only a getter`)
}
expect(message.seqno).to.equal(seqno)
})

it('topicIDs', () => {
try {
message.topicIDs = ['not allowed']
} catch (e) {
expect(e).to.be.an('error')
expect(e.toString()).to.equal(`TypeError: Cannot set property topicIDs of #<PubsubMessage> which has only a getter`)
}
expect(message.topicIDs[0]).to.equal(topicIDs[0])
expect(message.topicIDs.length).to.equal(topicIDs.length)
})
})
})
}
Loading

0 comments on commit 74003a7

Please sign in to comment.