<commit_before>var assert = require('assert')
  , jrs = require('jsonrpc-serializer')
  , Handler = require('../lib/p2p-rpc')
  , sse = require('../lib/sse');

function CheckRes(fn) {
  this.json = fn;
}

function CheckSseRes(fn) {
  this.send = fn;
}

function mockPost(jstring) {
  return {
    type: 'POST',
    body: JSON.parse(jstring)
  };
}

function mockGet(jstring) {
  return {
    type: 'GET',
    query: {
      q: jstring
    }
  };
}

describe('sse', function() {
  var cases = [
    'oh my god',
    'oh my god\noh my baby',
    'oh my god\noh my baby\n'
  ];
  it('#send & #parse', function(done) {
    var finished = 0;
    for (var i in cases) {
      (function(c) {
        sse.send(c, function(data) {
          assert.equal(sse.parse(data), c);
          finished++;
          if (finished == cases.length)
            done();
        });
      })(cases[i]);
    }
  });
});

describe('simple-rpc-call', function() {
  var handler = new Handler().handler;
  describe('request & response', function() {
    it('should receive a reponse', function(done) {
      var resCount = 0;
      function checkDone() {
        if (resCount == 5)
          done();
      }
      // peerId: id2, recv request & send response
      var longReq = jrs.notification('wait_request', {
        peerId: 'id2'
      });

      handler(mockGet(longReq), new CheckSseRes(function(data) {
        var obj = jrs.deserialize(sse.parse(data));
        assert.equal(obj.type, 'notification');
        assert.equal(obj.payload.method, 'forward_request');
        assert.equal(obj.payload.params.peerId, 'id1');
        assert.equal(obj.payload.params.remoteId, 'id2');
        assert.equal(obj.payload.params.request, 'Hello World!');
        resCount++;

        var res = jrs.notification('response', {
          peerId: 'id2',
          remoteId: 'id1',
          id: obj.payload.params.id,
          response: 'Hello Sb!'
        });

        handler(mockPost(res), new CheckRes(function(code, data) {
          assert.equal(code, 200);
          var obj = jrs.deserializeObject(data);
          assert.equal(obj.type, 'notification');
          assert.equal(obj.payload.method, 'responseSuccess'); 
          resCount++;
          checkDone();
        }));
      }));

      // peerId: id3, recv notification
      var longReq2 = jrs.notification('wait_request', {
        peerId: 'id3'
      });

      handler(mockGet(longReq2), new CheckSseRes(function(data) {
        var obj = jrs.deserialize(sse.parse(data));
        assert.equal(obj.type, 'notification');
        assert.equal(obj.payload.method, 'forward_notification');
        assert.equal(obj.payload.params.peerId, 'id1');
        assert.equal(obj.payload.params.remoteId, 'id3');
        assert.equal(obj.payload.params.notification, 'Admire You');
        resCount++;
        checkDone();
      }));

      // peerId: id1, send request & recv response
      var req = jrs.request('test_id', 'request', {
        peerId: 'id1',
        remoteId: 'id2',
        request: 'Hello World!'
      });

      handler(mockPost(req), new CheckRes(function(code, data) {
        assert.equal(code, 200);
        var obj = jrs.deserializeObject(data);
        assert.equal(obj.type, 'success');
        assert.equal(obj.payload.id, 'test_id');
        assert.equal(obj.payload.result.peerId, 'id2');
        assert.equal(obj.payload.result.remoteId, 'id1');
        assert.equal(obj.payload.result.response, 'Hello Sb!');
        resCount++;
        checkDone();
      }));

      var req2 = jrs.notification('notification', {
        peerId: 'id1',
        remoteId: 'id3',
        notification: '<NAME>'
      });

      handler(mockPost(req2), new CheckRes(function(code, data) {
        assert.equal(code, 200);
        var obj = jrs.deserializeObject(data);
        assert.equal(obj.type, 'notification');
        assert.equal(obj.method, 'forwardSuccess');
        resCount++;<commit_msg>fix test cases<commit_after>var assert = require('assert')
  , jrs = require('jsonrpc-serializer')
  , Handler = require('../lib/p2p-rpc')
  , sse = require('../lib/sse');

function CheckRes(fn) {
  this.json = fn;
}

function CheckSseRes(fn) {
  this.send = fn;
}

function mockPost(jstring) {
  return {
    type: 'POST',
    body: JSON.parse(jstring)
  };
}

function mockGet(jstring) {
  return {
    type: 'GET',
    query: {
      q: jstring
    }
  };
}

describe('sse', function() {
  var cases = [
    'oh my god',
    'oh my god\noh my baby',
    'oh my god\noh my baby\n'
  ];
  it('#send & #parse', function(done) {
    var finished = 0;
    for (var i in cases) {
      (function(c) {
        sse.send(c, function(data) {
          assert.equal(sse.parse(data), c);
          finished++;
          if (finished == cases.length)
            done();
        });
      })(cases[i]);
    }
  });
});

describe('simple-rpc-call', function() {
  var handler = new Handler().handler;
  describe('request & response', function() {
    it('should receive a reponse', function(done) {
      var resCount = 0;
      function checkDone() {
        if (resCount == 5)
          done();
      }
      // peerId: id2, recv request & send response
      var longReq = jrs.notification('wait_request', {
        peerId: 'id2'
      });

      handler(mockGet(longReq), new CheckSseRes(function(data) {
        var obj = jrs.deserialize(sse.parse(data));
        assert.equal(obj.type, 'notification');
        assert.equal(obj.payload.method, 'forward_request');
        assert.equal(obj.payload.params.peerId, 'id1');
        assert.equal(obj.payload.params.remoteId, 'id2');
        assert.equal(obj.payload.params.request, 'Hello World!');
        resCount++;

        var res = jrs.notification('response', {
          peerId: 'id2',
          remoteId: 'id1',
          id: obj.payload.params.id,
          response: 'Hello Sb!'
        });

        handler(mockPost(res), new CheckRes(function(code, data) {
          assert.equal(code, 200);
          var obj = jrs.deserializeObject(data);
          assert.equal(obj.type, 'notification');
          assert.equal(obj.payload.method, 'responseSuccess'); 
          resCount++;
          checkDone();
        }));
      }));

      // peerId: id3, recv notification
      var longReq2 = jrs.notification('wait_request', {
        peerId: 'id3'
      });

      handler(mockGet(longReq2), new CheckSseRes(function(data) {
        var obj = jrs.deserialize(sse.parse(data));
        assert.equal(obj.type, 'notification');
        assert.equal(obj.payload.method, 'forward_notification');
        assert.equal(obj.payload.params.peerId, 'id1');
        assert.equal(obj.payload.params.remoteId, 'id3');
        assert.equal(obj.payload.params.request, '<NAME>');
        resCount++;
        checkDone();
      }));

      // peerId: id1, send request & recv response
      var req = jrs.request('test_id', 'request', {
        peerId: 'id1',
        remoteId: 'id2',
        request: 'Hello World!'
      });

      handler(mockPost(req), new CheckRes(function(code, data) {
        assert.equal(code, 200);
        var obj = jrs.deserializeObject(data);
        assert.equal(obj.type, 'success');
        assert.equal(obj.payload.id, 'test_id');
        assert.equal(obj.payload.result.peerId, 'id2');
        assert.equal(obj.payload.result.remoteId, 'id1');
        assert.equal(obj.payload.result.response, 'Hello Sb!');
        resCount++;
        checkDone();
      }));

      var req2 = jrs.notification('request', {
        peerId: 'id1',
        remoteId: 'id3',
        request: '<NAME>'
      });

      handler(mockPost(req2), new CheckRes(function(code, data) {
        assert.equal(code, 200);
        var obj = jrs.deserializeObject(data);
        assert.equal(obj.type, 'notification');
        assert.equal(obj.payload.method, 'notificationSuccess');
        resCount++;
{'fraction_non_alphanumeric': 0.15924375079304656, 'fraction_numerical': 0.00862834665651567, 'mean_word_length': 2.3669372063220844, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '1505871', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2486, 'n_words': 480}