<commit_before>chai = require 'chai'
path = require 'path'
# spawn = require('child_process').spawn
shelljs = require 'shelljs'
WebSocketClient = require('websocket').client
semver = require 'semver'

check = (done, f) ->
  try
    f()
  catch e
    done e

exports.testRuntime = (runtimeType, startServer, stopServer, host='localhost', port=8080, collection='core', version='0.5') ->
  if version.length is 3
    semanticVersion = "#{version}.0"
  else
    semanticVersion = version

  address = "ws://#{host}:#{port}/"
  describe "#{runtimeType} webSocket network runtime version #{version}", ->
    client = null
    connection = null
    send = null
    describe "Connecting to the runtime at #{address}", ->
      it 'should succeed', (done) ->
        @timeout 4000
        tries = 10
        startServer ->
          client = new WebSocketClient
          client.on 'connect', (conn) ->
            connection = conn
            done()
          client.on 'connectFailed', (err) ->
            tries--
            chai.expect(tries).to.be.above 0
            setTimeout(
              ->
                client.connect address, 'noflo'
              200
            )
          client.connect address, 'noflo'
    after stopServer

    send = (protocol, command, payload) ->
      payload = {} unless payload
      payload.secret = process.env.FBP_PROTOCOL_SECRET if process.env.FBP_PROTOCOL_SECRET
      connection.sendUTF JSON.stringify
        protocol: protocol
        command: command
        payload: payload

    receive = (expects, done, ignore=null) ->
      listener = (message) ->
        check done, ->
          chai.expect(message.utf8Data).to.be.a 'string'
          msg = JSON.parse message.utf8Data

          if not ignore or not ignore(msg)
            expected = expects.shift()
            if expected.payload
              for key, value of expected.payload
                type = null
                if value is String
                  type = 'string'
                else if value is Number
                  type = 'number'<commit_msg>Start by testing protocol and command<commit_after>chai = require 'chai'
path = require 'path'
# spawn = require('child_process').spawn
shelljs = require 'shelljs'
WebSocketClient = require('websocket').client
semver = require 'semver'

check = (done, f) ->
  try
    f()
  catch e
    done e

exports.testRuntime = (runtimeType, startServer, stopServer, host='localhost', port=8080, collection='core', version='0.5') ->
  if version.length is 3
    semanticVersion = "#{version}.0"
  else
    semanticVersion = version

  address = "ws://#{host}:#{port}/"
  describe "#{runtimeType} webSocket network runtime version #{version}", ->
    client = null
    connection = null
    send = null
    describe "Connecting to the runtime at #{address}", ->
      it 'should succeed', (done) ->
        @timeout 4000
        tries = 10
        startServer ->
          client = new WebSocketClient
          client.on 'connect', (conn) ->
            connection = conn
            done()
          client.on 'connectFailed', (err) ->
            tries--
            chai.expect(tries).to.be.above 0
            setTimeout(
              ->
                client.connect address, 'noflo'
              200
            )
          client.connect address, 'noflo'
    after stopServer

    send = (protocol, command, payload) ->
      payload = {} unless payload
      payload.secret = process.env.FBP_PROTOCOL_SECRET if process.env.FBP_PROTOCOL_SECRET
      connection.sendUTF JSON.stringify
        protocol: protocol
        command: command
        payload: payload

    receive = (expects, done, ignore=null) ->
      listener = (message) ->
        check done, ->
          chai.expect(message.utf8Data).to.be.a 'string'
          msg = JSON.parse message.utf8Data

          if not ignore or not ignore(msg)
            expected = expects.shift()
            chai.expect(msg.protocol, 'protocol').to.equal expected.protocol
            chai.expect(msg.command, 'command').to.equal expected.command
            if expected.payload
              for key, value of expected.payload
                type = null
                if value is String
                  type = 'string'
                else if value is Number
                  type = 'number'
{'fraction_non_alphanumeric': 0.10660486674391657, 'fraction_numerical': 0.009269988412514484, 'mean_word_length': 1.9561643835616438, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '1737472', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1159, 'n_words': 330}