<commit_before>var fs = require('fs')
var net = require('net')
var tap = require('tap')
var util = require('util')
var async = require('async')
var child_process = require('child_process')

var term_to_binary = require('../term_to_binary');


var ECHO = {child:null, port:null, wait_timer:null}
var THAI = '<<224,184,160,224,184,178,224,184,169,224,184,178,224,185,132,224,184,151,224,184,162>>'
var TERMS =
  [ '["two","array"]'      , ['two', 'array']                     // Innocent 2-array
  , '"ABCDE"'              , "ABCDE"                              // string (char list, encoded as STRING)
  , 'foo'                  , {a:'foo'}                            // atom
  , "'GET'"                , {a:'GET'}
  , '[{jason,awesome}]'    , [ {t:[{a:'jason'}, {a:'awesome'}]} ]
  , THAI                   , {b:'ภาษาไทย'}                        // binary
  , '[[[[23,"skidoo"]]]]'  , [[[[23, 'skidoo']]]]                 // nested objects
  , '123456'               , 123456                               // normal integer
  , '{"tuple",here,too}'   , {t:['tuple', {a:'here'}, {a:'too'}]} //tuple
  , '{booleans,true,false}', {t:[ {a:'booleans'}, true, false ]}  // Booleans
  ].reduce(join_pairs, [])

function join_pairs(state, item, i, terms) {
  if(i % 2 == 0)
    state.push({'repr':item, 'term':terms[i+1]})
  return state
}

tap.test('Erlang round-trip echo', function(t) {
  t.plan(TERMS.length)

  async.eachSeries(TERMS, test_pair, pairs_tested)

  function test_pair(pair, to_async) {
    send(pair.term, function(er, results) {
      // Find the newline boundary
      t.equal(results.repr, pair.repr, 'Good representation: ' + pair.repr)
      return to_async(null)
    })
  }

  function pairs_tested(er) {
    if(er)
      throw er

    t.end()
    process.kill(ECHO.child.pid)
    ECHO.wait_timer = setTimeout(function() { process.exit(1) }, 3000)
  }
})


function send(term, callback) {
  with_server(function() {
    var response = []

    console.log('Echo to :%d', ECHO.port)
    var client = net.connect({'port':ECHO.port}, on_connect)
    client.on('data', on_data)
    client.on('end', on_end)

    function on_connect() {
      // Send the term.
      var bin = term_to_binary(term)
      var length = new Buffer(4)
      length.writeUInt32BE(bin.length, 0)

      client.write(length)<commit_msg>Test the protocol then the encoding<commit_after>var fs = require('fs')
var net = require('net')
var tap = require('tap')
var util = require('util')
var async = require('async')
var child_process = require('child_process')

var term_to_binary = require('../term_to_binary');


var ECHO = {child:null, port:null, wait_timer:null}
var THAI = '<<224,184,160,224,184,178,224,184,169,224,184,178,224,185,132,224,184,151,224,184,162>>'
var TERMS =
  [ '["two","array"]'      , ['two', 'array']                     // Innocent 2-array
  , '"ABCDE"'              , "ABCDE"                              // string (char list, encoded as STRING)
  , 'foo'                  , {a:'foo'}                            // atom
  , "'GET'"                , {a:'GET'}
  , '[{jason,awesome}]'    , [ {t:[{a:'jason'}, {a:'awesome'}]} ]
  , THAI                   , {b:'ภาษาไทย'}                        // binary
  , '[[[[23,"skidoo"]]]]'  , [[[[23, 'skidoo']]]]                 // nested objects
  , '123456'               , 123456                               // normal integer
  , '{"tuple",here,too}'   , {t:['tuple', {a:'here'}, {a:'too'}]} //tuple
  , '{booleans,true,false}', {t:[ {a:'booleans'}, true, false ]}  // Booleans
  ].reduce(join_pairs, [])

function join_pairs(state, item, i, terms) {
  if(i % 2 == 0)
    state.push({'repr':item, 'term':terms[i+1]})
  return state
}

tap.test('Erlang round-trip echo server', function(t) {
  t.plan(3)

  var term = 87
  send(term, function(er, results) {
    // Find the newline boundary
    t.is(er, null, 'No problem sending and receiving a term')
    t.ok(results.repr, 'Got a code representation of the term')
    t.ok(results.encoded, 'Got a re-encoded term from Erlang')
    t.end()
  })
})

tap.test('Encoding', function(t) {
  t.plan(TERMS.length * 2)

  async.eachSeries(TERMS, test_pair, pairs_tested)

  function test_pair(pair, to_async) {
    send(pair.term, function(er, results) {
      t.equal(results.repr, pair.repr, 'Good representation: ' + pair.repr)

      return to_async(null)
    })
  }

  function pairs_tested(er) {
    if(er)
      throw er

    t.end()
    process.kill(ECHO.child.pid)
    ECHO.wait_timer = setTimeout(function() { process.exit(1) }, 3000)
  }
})


function send(term, callback) {
  with_server(function() {
    var response = []

    console.log('Echo to :%d', ECHO.port)
    var client = net.connect({'port':ECHO.port}, on_connect)
    client.on('data', on_data)
    client.on('end', on_end)

    function on_connect() {
      // Send the term.
      var bin = term_to_binary(term)
      var length = new Buffer(4)
      length.writeUInt32BE(bin.length, 0)

      client.write(length)
{'fraction_non_alphanumeric': 0.18200040088194028, 'fraction_numerical': 0.037682902385247546, 'mean_word_length': 2.5140845070422535, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1297904', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1748, 'n_words': 354}