<commit_before>
msgflo = require '../'
participants = require './fixtures/participants'
foreignParticipants = require './fixtures/foreign'

async = require 'async'
chai = require 'chai' unless chai
path = require 'path'

fixturePath = (name) ->
  path.join __dirname, 'fixtures', name
fixtureGraph = (name, callback) ->
  p = fixturePath name
  msgflo.common.readGraph p, callback

linesContaining = (lines, str) =>
  lines = lines.split '\n'
  containing = lines.filter (s) -> s.indexOf(str) != -1

describe 'Setup functions', ->
  bindings = null
  graph = null

  beforeEach (done) ->
    fixtureGraph 'imgflo-server.fbp', (err, gr) ->
      graph = gr
      chai.expect(err).to.not.exist
      bindings = msgflo.setup.graphBindings graph
      done()

  describe 'Extracting from imgflo-server.fbp', ->
    it 'should have one binding per non-roundrobin connection or IIP', () ->
      chai.expect(bindings.length).to.equal graph.connections.length-3
    it 'should extract all pubsub bindings', () ->
      pubsubs = bindings.filter (b) -> b.type == 'pubsub'
      chai.expect(pubsubs).to.have.length 1
    it 'should extract roundrobin binding', () ->
      roundrobins = bindings.filter (b) -> b.type == 'roundrobin'
      chai.expect(roundrobins).to.have.length 1
  describe 'Pretty formatting bindings', ->
    pretty = null
    beforeEach () ->
      pretty = msgflo.setup.prettyFormatBindings bindings
    it 'should return one line per binding', () ->
      chai.expect(pretty.split('\n')).to.have.length bindings.length+1 # roundrobin also has deadletter
    it 'should have one roundrobin', () ->
      match = linesContaining pretty, 'ROUNDROBIN'
      chai.expect(match).to.have.length 1
    it 'should have one deadletter', () ->
      match = linesContaining pretty, 'DEADLETTER'
      chai.expect(match).to.have.length 1
    it 'should have one pubsub', () ->
      match = linesContaining pretty, 'PUBSUB'
      chai.expect(match).to.have.length 1

describe 'Setup bindings', ->
  address = 'amqp://localhost'
  manager = null
  options = null
  client = null
  bindings = null
  participants = null

  beforeEach (done) ->
    @timeout 4000
    options =
      graphfile: fixturePath 'simple.fbp'
      libraryfile: fixturePath 'library-simple.json'
      broker: address
      forward: 'stderr,stdout'
    client = msgflo.transport.getClient address

    setupParticipants = (callback) ->
      msgflo.setup.participants options, (err, p) ->
        participants = p
        return callback err
    setupBindings = (callback) ->
      msgflo.setup.bindings options, (err, b, graph) ->
        bindings = b if b
        return callback err
    async.series [
      setupParticipants
      client.connect.bind client
      setupBindings
    ], (err) ->
      chai.expect(err).to.not.exist<commit_msg>tests: Try longer setup timeout<commit_after>
msgflo = require '../'
participants = require './fixtures/participants'
foreignParticipants = require './fixtures/foreign'

async = require 'async'
chai = require 'chai' unless chai
path = require 'path'

fixturePath = (name) ->
  path.join __dirname, 'fixtures', name
fixtureGraph = (name, callback) ->
  p = fixturePath name
  msgflo.common.readGraph p, callback

linesContaining = (lines, str) =>
  lines = lines.split '\n'
  containing = lines.filter (s) -> s.indexOf(str) != -1

describe 'Setup functions', ->
  bindings = null
  graph = null

  beforeEach (done) ->
    fixtureGraph 'imgflo-server.fbp', (err, gr) ->
      graph = gr
      chai.expect(err).to.not.exist
      bindings = msgflo.setup.graphBindings graph
      done()

  describe 'Extracting from imgflo-server.fbp', ->
    it 'should have one binding per non-roundrobin connection or IIP', () ->
      chai.expect(bindings.length).to.equal graph.connections.length-3
    it 'should extract all pubsub bindings', () ->
      pubsubs = bindings.filter (b) -> b.type == 'pubsub'
      chai.expect(pubsubs).to.have.length 1
    it 'should extract roundrobin binding', () ->
      roundrobins = bindings.filter (b) -> b.type == 'roundrobin'
      chai.expect(roundrobins).to.have.length 1
  describe 'Pretty formatting bindings', ->
    pretty = null
    beforeEach () ->
      pretty = msgflo.setup.prettyFormatBindings bindings
    it 'should return one line per binding', () ->
      chai.expect(pretty.split('\n')).to.have.length bindings.length+1 # roundrobin also has deadletter
    it 'should have one roundrobin', () ->
      match = linesContaining pretty, 'ROUNDROBIN'
      chai.expect(match).to.have.length 1
    it 'should have one deadletter', () ->
      match = linesContaining pretty, 'DEADLETTER'
      chai.expect(match).to.have.length 1
    it 'should have one pubsub', () ->
      match = linesContaining pretty, 'PUBSUB'
      chai.expect(match).to.have.length 1

describe 'Setup bindings', ->
  address = 'amqp://localhost'
  manager = null
  options = null
  client = null
  bindings = null
  participants = null

  beforeEach (done) ->
    @timeout 8000
    options =
      graphfile: fixturePath 'simple.fbp'
      libraryfile: fixturePath 'library-simple.json'
      broker: address
      forward: 'stderr,stdout'
    client = msgflo.transport.getClient address

    setupParticipants = (callback) ->
      msgflo.setup.participants options, (err, p) ->
        participants = p
        return callback err
    setupBindings = (callback) ->
      msgflo.setup.bindings options, (err, b, graph) ->
        bindings = b if b
        return callback err
    async.series [
      setupParticipants
      client.connect.bind client
      setupBindings
    ], (err) ->
      chai.expect(err).to.not.exist
{'fraction_non_alphanumeric': 0.12318840579710146, 'fraction_numerical': 0.0042417815482502655, 'mean_word_length': 2.9216909216909217, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, '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': '1256981', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1711, 'n_words': 459}