import PeerId from 'peer-id';
import { Multiaddr } from 'multiaddr';
import { expect } from 'aegir/utils/chai.js';
import {
  getDescribe,
  getIt
} from '../utils/mocha.js';
import { isWebWorker } from 'ipfs-utils/src/env.js';
import { ipfsOptionsWebsocketsFilterAll } from '../utils/ipfs-options-websockets-filter-all.js';
export function testAddrs(factory, options) {
  const ipfsOptions = ipfsOptionsWebsocketsFilterAll();
  const describe = getDescribe(options);
  const it = getIt(options);
  describe('.swarm.addrs', function () {
    this.timeout(80 * 1000);
    let ipfsA;
    let ipfsB;
    let ipfsBId;
    before(async () => {
      ipfsA = (await factory.spawn({
        type: 'proc',
        ipfsOptions
      })).api;
      ipfsB = (await factory.spawn({ type: isWebWorker ? 'go' : undefined })).api;
      ipfsBId = await ipfsB.id();
      await ipfsA.swarm.connect(ipfsBId.addresses[0]);
    });
    after(() => factory.clean());
    it('should get a list of node addresses', async () => {
      const peers = await ipfsA.swarm.addrs();
      expect(peers).to.not.be.empty();
      expect(peers).to.be.an('array');
      for (const peer of peers) {
        expect(PeerId.parse(peer.id)).to.be.ok();
        expect(peer).to.have.a.property('addrs').that.is.an('array');
        for (const ma of peer.addrs) {
          expect(Multiaddr.isMultiaddr(ma)).to.be.true();
        }
      }
    });
  });
}