import {
  isBrowser,
  isWebWorker,
  isElectronRenderer
} from 'ipfs-utils/src/env.js';
import { getTopic } from './utils.js';
import {
  getDescribe,
  getIt
} from '../utils/mocha.js';
import waitFor from '../utils/wait-for.js';
export function testUnsubscribe(factory, options) {
  const describe = getDescribe(options);
  const it = getIt(options);
  describe('.pubsub.unsubscribe', function () {
    this.timeout(80 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    const count = isBrowser || isWebWorker || isElectronRenderer ? 5 : 10;
    it(`should subscribe and unsubscribe ${ count } times`, async () => {
      const someTopic = getTopic();
      const handlers = Array.from(Array(count), () => msg => {
      });
      for (let i = 0; i < count; i++) {
        await ipfs.pubsub.subscribe(someTopic, handlers[i]);
      }
      for (let i = 0; i < count; i++) {
        await ipfs.pubsub.unsubscribe(someTopic, handlers[i]);
      }
      await waitFor(async () => {
        const subs = await ipfs.pubsub.ls();
        return subs.length === 0;
      }, {
        interval: 1000,
        timeout: 30000,
        name: 'subscriptions to be empty'
      });
    });
    it(`should subscribe ${ count } handlers and unsubscribe once with no reference to the handlers`, async () => {
      const someTopic = getTopic();
      for (let i = 0; i < count; i++) {
        await ipfs.pubsub.subscribe(someTopic, msg => {
        });
      }
      await ipfs.pubsub.unsubscribe(someTopic);
      await waitFor(async () => {
        const subs = await ipfs.pubsub.ls();
        return subs.length === 0;
      }, {
        interval: 1000,
        timeout: 30000,
        name: 'subscriptions to be empty'
      });
    });
  });
}