Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 74003a7

Browse files
haadcodedaviddias
authored andcommitted
feat: add first pass of pubsub tests (running in js-ipfs-api)
1 parent 56cd45e commit 74003a7

File tree

3 files changed

+610
-0
lines changed

3 files changed

+610
-0
lines changed

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ exports.generic = require('./generic')
88
exports.swarm = require('./swarm')
99
exports.block = require('./block')
1010
exports.dht = require('./dht')
11+
exports.pubsub = require('./pubsub')
12+
exports.pubsubMessage = require('./pubsub-message')

src/pubsub-message.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
const isNode = require('detect-node')
6+
7+
// NOTE!
8+
// (Most of) these tests are skipped for now until we figure out the
9+
// final data types for the messages coming over the wire
10+
11+
const topicName = 'js-ipfs-api-tests'
12+
13+
module.exports = (common, deps) => {
14+
// Make sure the needed dependencies are injected
15+
expect(deps.PubsubMessage).to.exist
16+
expect(deps.PubsubMessageUtils).to.exist
17+
18+
const PubsubMessage = deps.PubsubMessage // eslint-disable-line no-unused-vars
19+
const PubsubMessageUtils = deps.PubsubMessageUtils // eslint-disable-line no-unused-vars
20+
21+
// TESTS
22+
describe('.pubsub-message', () => {
23+
if (!isNode) {
24+
return
25+
}
26+
27+
it.skip('create message', () => {
28+
// TODO
29+
})
30+
31+
it.skip('deserialize message from JSON object', () => {
32+
const obj = {
33+
from: 'BI:ۛv�m�uyѱ����tU�+��#���V',
34+
data: 'aGk=',
35+
seqno: 'FIlj2BpyEgI=',
36+
topicIDs: [ topicName ]
37+
}
38+
try {
39+
const message = PubsubMessageUtils.deserialize(obj)
40+
expect(message.from).to.equal('AAA')
41+
expect(message.data).to.equal('hi')
42+
expect(message.seqno).to.equal('\u0014�c�\u001ar\u0012\u0002')
43+
expect(message.topicIDs.length).to.equal(1)
44+
expect(message.topicIDs[0]).to.equal(topicName)
45+
} catch (e) {
46+
expect(e).to.not.exist
47+
}
48+
})
49+
50+
describe('immutable properties', () => {
51+
const sender = 'A'
52+
const data = 'hello'
53+
const seqno = '123'
54+
const topicIDs = ['hello world']
55+
56+
const message = PubsubMessageUtils.create(sender, data, seqno, topicIDs)
57+
58+
it('from', () => {
59+
try {
60+
message.from = 'not allowed'
61+
} catch (e) {
62+
expect(e).to.be.an('error')
63+
expect(e.toString()).to.equal(`TypeError: Cannot set property from of #<PubsubMessage> which has only a getter`)
64+
}
65+
expect(message.from).to.equal(sender)
66+
})
67+
68+
it('data', () => {
69+
try {
70+
message.data = 'not allowed'
71+
} catch (e) {
72+
expect(e).to.be.an('error')
73+
expect(e.toString()).to.equal(`TypeError: Cannot set property data of #<PubsubMessage> which has only a getter`)
74+
}
75+
expect(message.data).to.equal(data)
76+
})
77+
78+
it('seqno', () => {
79+
try {
80+
message.seqno = 'not allowed'
81+
} catch (e) {
82+
expect(e).to.be.an('error')
83+
expect(e.toString()).to.equal(`TypeError: Cannot set property seqno of #<PubsubMessage> which has only a getter`)
84+
}
85+
expect(message.seqno).to.equal(seqno)
86+
})
87+
88+
it('topicIDs', () => {
89+
try {
90+
message.topicIDs = ['not allowed']
91+
} catch (e) {
92+
expect(e).to.be.an('error')
93+
expect(e.toString()).to.equal(`TypeError: Cannot set property topicIDs of #<PubsubMessage> which has only a getter`)
94+
}
95+
expect(message.topicIDs[0]).to.equal(topicIDs[0])
96+
expect(message.topicIDs.length).to.equal(topicIDs.length)
97+
})
98+
})
99+
})
100+
}

0 commit comments

Comments
 (0)