<commit_before>import assert = require('assert');
import express = require('express');
import {setupDiscoveryPeer, SwarmPeer, PeerInfo} from './discovery';
import {SWARM_ID_PREFIX} from './config';
const pkg = require('../package.json');
export const version = pkg.version;

describe('easy-ssb-pub discovery peer', function () {
  it('should allow Alice to find Bob and get an invite from him', function (done) {
    const BOB_HTTP_PORT = 4000;
    const bobServer = express().get('/invited/json', (req: any, res: any) =>
      res.json({invitation: 'The Invitation Goes Here'}),
    ).listen(BOB_HTTP_PORT);

    const ssbBotA = {
      invite: {
        create: (amount: number, cb: Function) => {
          done('alice bot.invite.create should not be called');
        },
        accept: (invitation: string) => {
          assert.strictEqual(invitation, 'The Invitation Goes Here');
          bobServer.close(done);
        },
      },
      gossip: {
        peers: () => [],
      },
    };

    const ssbConf = {host: 'localhost'};

    const peerA: SwarmPeer = {
      listen: (port: number) => {},
      join: (key: string, opts: any, cb: Function) => {},
      on: (event: 'connection', cb: Function) => {
        const peerInfoB: PeerInfo = {
          id: SWARM_ID_PREFIX + version,
          host: 'localhost:' + BOB_HTTP_PORT,
          port: 4001,
        };
        cb(null, peerInfoB);
      },
    };

    setupDiscoveryPeer({bot: ssbBotA, config: ssbConf, peer: peerA, port: 4002});
  });

  it('should make Alice ignore Bob if already connected to him', function (done) {
    const BOB_HTTP_PORT = 4000;
    const bobServer = express().get('/invited/json', (req: any, res: any) =>
      res.json({invitation: 'The Invitation Goes Here'}),
    ).listen(BOB_HTTP_PORT);

    const ssbBotA = {
      id: '@Alice=.ed25519',
      invite: {
        create: (amount: number, cb: Function) => {
          done('alice bot.invite.create should not be called');
        },
        accept: (invitation: string) => {
          done('alice bot.invite.accept should not be called');
        },
      },
      gossip: {
        peers: () => [
          {host: 'localhost:' + BOB_HTTP_PORT, state: 'connected'} as any,
        ],
      },
    };

    const ssbConf = {host: 'localhost'};

    const peerA: SwarmPeer = {
      listen: (port: number) => {},
      join: (key: string, opts: any, cb: Function) => {},
      on: (event: 'connection', cb: Function) => {
        const peerInfoB: PeerInfo = {
          id: SWARM_ID_PREFIX + version,
          host: 'localhost:' + BOB_HTTP_PORT,
          port: 4001,
        };
        cb(null, peerInfoB);
      },
    };

    setupDiscoveryPeer({bot: ssbBotA, config: ssbConf, peer: peerA, port: 4002});

    this.timeout(1500);
    setTimeout(() => bobServer.close(done), 1000);
  });

  it('should make Alice ignore Bob if it has an older version', function (done) {
    const BOB_HTTP_PORT = 4000;
    const bobServer = express().get('/invited/json', (req: any, res: any) =>
      res.json({invitation: 'The Invitation Goes Here'}),
    ).listen(BOB_HTTP_PORT);

    const ssbBotA = {
      invite: {
        create: (amount: number, cb: Function) => {
          done('alice bot.invite.create should not be called');
        },
        accept: (invitation: string) => {<commit_msg>test: max limit on connected pubs through discovery<commit_after>import assert = require('assert');
import express = require('express');
import {setupDiscoveryPeer, SwarmPeer, PeerInfo} from './discovery';
import {SWARM_ID_PREFIX} from './config';
const pkg = require('../package.json');
export const version = pkg.version;

describe('easy-ssb-pub discovery peer', function () {
  it('should allow Alice to find Bob and get an invite from him', function (done) {
    const BOB_HTTP_PORT = 4000;
    const bobServer = express().get('/invited/json', (req: any, res: any) =>
      res.json({invitation: 'The Invitation Goes Here'}),
    ).listen(BOB_HTTP_PORT);

    const ssbBotA = {
      invite: {
        create: (amount: number, cb: Function) => {
          done('alice bot.invite.create should not be called');
        },
        accept: (invitation: string) => {
          assert.strictEqual(invitation, 'The Invitation Goes Here');
          bobServer.close(done);
        },
      },
      gossip: {
        peers: () => [],
      },
    };

    const ssbConf = {host: 'localhost'};

    const peerA: SwarmPeer = {
      listen: (port: number) => {},
      join: (key: string, opts: any, cb: Function) => {},
      on: (event: 'connection', cb: Function) => {
        const peerInfoB: PeerInfo = {
          id: SWARM_ID_PREFIX + version,
          host: 'localhost:' + BOB_HTTP_PORT,
          port: 4001,
        };
        cb(null, peerInfoB);
      },
    };

    setupDiscoveryPeer({bot: ssbBotA, config: ssbConf, peer: peerA, port: 4002});
  });

  it('should make Alice ignore Bob if already connected to him', function (done) {
    const BOB_HTTP_PORT = 4000;
    const bobServer = express().get('/invited/json', (req: any, res: any) =>
      res.json({invitation: 'The Invitation Goes Here'}),
    ).listen(BOB_HTTP_PORT);

    const ssbBotA = {
      id: '@Alice=.ed25519',
      invite: {
        create: (amount: number, cb: Function) => {
          done('alice bot.invite.create should not be called');
        },
        accept: (invitation: string) => {
          done('alice bot.invite.accept should not be called');
        },
      },
      gossip: {
        peers: () => [
          {host: 'localhost:' + BOB_HTTP_PORT, state: 'connected'} as any,
        ],
      },
    };

    const ssbConf = {host: 'localhost'};

    const peerA: SwarmPeer = {
      listen: (port: number) => {},
      join: (key: string, opts: any, cb: Function) => {},
      on: (event: 'connection', cb: Function) => {
        const peerInfoB: PeerInfo = {
          id: SWARM_ID_PREFIX + version,
          host: 'localhost:' + BOB_HTTP_PORT,
          port: 4001,
        };
        cb(null, peerInfoB);
      },
    };

    setupDiscoveryPeer({bot: ssbBotA, config: ssbConf, peer: peerA, port: 4002});

    this.timeout(1500);
    setTimeout(() => bobServer.close(done), 1000);
  });

  it('should make Alice ignore Bob if already connected to many pubs', function (done) {
    const BOB_HTTP_PORT = 4000;
    const bobServer = express().get('/invited/json', (req: any, res: any) =>
      res.json({invitation: 'The Invitation Goes Here'}),
    ).listen(BOB_HTTP_PORT);

    const ssbBotA = {
      id: '@Alice=.ed25519',
      invite: {
        create: (amount: number, cb: Function) => {
          done('alice bot.invite.create should not be called');
        },
        accept: (invitation: string) => {
          done('alice bot.invite.accept should not be called');
        },
      },
      gossip: {
        peers: () => [
          {host: 'test1.local', state: 'connected'} as any,
          {host: 'test2.local', state: 'connected'} as any,
          {host: 'test3.local', state: 'connected'} as any,
          {host: 'test4.local', state: 'connected'} as any,
        ],
      },
    };

    const ssbConf = {host: 'localhost'};

    const peerA: SwarmPeer = {
      listen: (port: number) => {},
      join: (key: string, opts: any, cb: Function) => {},
      on: (event: 'connection', cb: Function) => {
        const peerInfoB: PeerInfo = {
          id: SWARM_ID_PREFIX + version,
          host: 'localhost:' + BOB_HTTP_PORT,
          port: 4001,
        };
        cb(null, peerInfoB);
      },
    };

    setupDiscoveryPeer({bot: ssbBotA, config: ssbConf, peer: peerA, port: 4002});

    this.timeout(1500);
    setTimeout(() => bobServer.close(done), 1000);
  });

  it('should make Alice ignore Bob if it has an older version', function (done) {
    const BOB_HTTP_PORT = 4000;
    const bobServer = express().get('/invited/json', (req: any, res: any) =>
      res.json({invitation: 'The Invitation Goes Here'}),
    ).listen(BOB_HTTP_PORT);

    const ssbBotA = {
      invite: {
        create: (amount: number, cb: Function) => {
          done('alice bot.invite.create should not be called');
        },
        accept: (invitation: string) => {
{'fraction_non_alphanumeric': 0.1543615984405458, 'fraction_numerical': 0.013523391812865496, 'mean_word_length': 2.475444538526672, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3586633', 'n_tokens_mistral': 2693, 'n_tokens_neox': 2562, 'n_words': 694}