'use strict';

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

var utils = require('./utils.js');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');
var delay = require('delay');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var delay__default = /*#__PURE__*/_interopDefaultLegacy(delay);

function testLs(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.pubsub.ls', function () {
    this.timeout(80 * 1000);
    let ipfs;
    let subscribedTopics = [];
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    afterEach(async () => {
      for (let i = 0; i < subscribedTopics.length; i++) {
        await ipfs.pubsub.unsubscribe(subscribedTopics[i]);
      }
      subscribedTopics = [];
      await delay__default["default"](100);
    });
    after(() => factory.clean());
    it('should return an empty list when no topics are subscribed', async () => {
      const topics = await ipfs.pubsub.ls();
      chai_js.expect(topics.length).to.equal(0);
    });
    it('should return a list with 1 subscribed topic', async () => {
      const sub1 = () => {
      };
      const topic = utils.getTopic();
      subscribedTopics = [topic];
      await ipfs.pubsub.subscribe(topic, sub1);
      const topics = await ipfs.pubsub.ls();
      chai_js.expect(topics).to.be.eql([topic]);
    });
    it('should return a list with 3 subscribed topics', async () => {
      const topics = [
        {
          name: 'one',
          handler() {
          }
        },
        {
          name: 'two',
          handler() {
          }
        },
        {
          name: 'three',
          handler() {
          }
        }
      ];
      subscribedTopics = topics.map(t => t.name);
      for (let i = 0; i < topics.length; i++) {
        await ipfs.pubsub.subscribe(topics[i].name, topics[i].handler);
      }
      const list = await ipfs.pubsub.ls();
      chai_js.expect(list.sort()).to.eql(topics.map(t => t.name).sort());
    });
  });
}

exports.testLs = testLs;
