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