'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var fromString = require('uint8arrays/from-string');
var nanoid = require('nanoid');
var utils = require('./utils.js');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');

function testPublish(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.pubsub.publish', function () {
    this.timeout(80 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should fail with undefined msg', async () => {
      const topic = utils.getTopic();
      await chai_js.expect(ipfs.pubsub.publish(topic)).to.eventually.be.rejected();
    });
    it('should publish message from buffer', () => {
      const topic = utils.getTopic();
      return ipfs.pubsub.publish(topic, fromString.fromString(nanoid.nanoid()));
    });
    it('should publish 10 times within time limit', async () => {
      const count = 10;
      const topic = utils.getTopic();
      for (let i = 0; i < count; i++) {
        await ipfs.pubsub.publish(topic, fromString.fromString(nanoid.nanoid()));
      }
    });
  });
}

exports.testPublish = testPublish;
