<commit_before>var WebSocket = require('ws');
var assert = require('assert');

var Server = require('../server').Server;
var MemoryPersistence = require('../persistence/memory');

var server = new Server({
  port: 8080,
  store: new MemoryPersistence(),
});

function socketConversation(ws, funcs) {
  var r = funcs[0]();
  if (r) ws.send(JSON.stringify(r));
  var next = 1;
  ws.onmessage = function(msg) {
    var r = funcs[next++](JSON.parse(msg.data));
    if (r) ws.send(JSON.stringify(r));
  };
}

describe('Backend', function() {
  var ws;
  beforeEach(function() {
    ws = new WebSocket('ws://localhost:8080');
  });
  afterEach(function(done) {
    ws.send(JSON.stringify({type: 'reset'}));
    ws.onmessage = function() {
      ws.close();
      done();
    };
  });
  it('get zero changes initially', function(done) {
    ws.onmessage = function(msg) {
      var data = JSON.parse(msg.data);
      assert.equal(data.type, 'sending-changes');
      assert.equal(data.nrOfRecordsToSync, 0);
      done();
    };
    ws.onopen = function() {
      ws.send(JSON.stringify({
        type: 'get-changes',
        since: null,
        storeName: 'animals',
        clientId: 'foo',
      }));
    };
  });
  it('sends created records on request', function(done) {
    var data1, data2;
    ws.onopen = function() {
      socketConversation(ws, [
        function() {
          return {
            type: 'create',
            storeName: 'animals',
            clientId: 'foo',
            record: {name: 'Stampe', key: 1},
          };
        },
        function(data) {
          data1 = data;
          return {
            type: 'create',
            storeName: 'animals',
            clientId: 'foo',
            record: {name: 'Thumper', key: 2},
          };
        },
        function(data) {
          data2 = data;
          assert.notEqual(data1.newVersion, data2.newVersion);
          return {
            type: 'get-changes',
            since: null,
            storeName: 'animals',
            clientId: 'otherfoo',
          };
        },
        function(data) {
          console.log(data);
          assert.equal(data.nrOfRecordsToSync, 2);
        },
        function(data) {
          assert.equal(data.record.name, 'Stampe');
        },
        function(data) {
          assert.equal(data.record.name, 'Thumper');
          done();
        }
      ]);
    };
  });
  it('only sends changes after timestamp', function(done) {
    var firstTimestamp;
    ws.onopen = function() {
      socketConversation(ws, [
        function() {
          return {
            type: 'create',
            storeName: 'animals',
            clientId: 'foo',
            record: {name: 'Stampe', key: 1},
          };
        },
        function(data) {
          assert.equal(data.type, 'ok');
          return {
            type: 'get-changes',
            since: null,
            storeName: 'animals',
            clientId: 'otherfoo',
          };
        },
        function(data) {
          assert.equal(data.nrOfRecordsToSync, 1);
        },
        function(data) {
          assert.equal(data.record.name, 'Stampe');
          firstTimestamp = data.timestamp;
          assert.equal(data.record.name, 'Stampe');
          return {
            type: 'create',
            storeName: 'animals',
            clientId: 'foo',
            record: {name: 'Smask', key: 2},
          };
        },
        function(data) {
          assert.equal(data.type, 'ok');
          return {
            type: 'get-changes',
            since: firstTimestamp,
            storeName: 'animals',
            clientId: 'otherfoo',
          };
        },
        function(data) {
          assert.equal(data.nrOfRecordsToSync, 1);
        },
        function(data) {
          assert.equal(data.record.name, 'Smask');
          done();
        }
      ]);
    };
  });
  it('only sends changes from requested store', function(done) {
    ws.onopen = function() {
      socketConversation(ws, [
        function() {
          return {
            type: 'create',
            storeName: 'animals',
            clientId: 'foo',
            record: {name: 'Stampe', key: 1},
          };
        },
        function(data) {
          data1 = data;
          return {
            type: 'create',
            storeName: 'roads',
            clientId: 'foo',
            record: {length: 100, key: 2},
          };
        },
        function(data) {
          return {
            type: 'get-changes',
            since: null,
            storeName: 'roads',
            clientId: 'otherfoo',
          };
        },
        function(data) {
          assert.equal(data.nrOfRecordsToSync, 1);
        },
        function(data) {
          assert.equal(data.record.length, 100);
          return {
            type: 'get-changes',
            since: null,
            storeName: 'animals',
            clientId: 'otherfoo',
          };
        },
        function(data) {
          assert.equal(data.nrOfRecordsToSync, 1);
        },
        function(data) {
          assert.equal(data.record.name, 'Stampe');
          done();
        },
      ]);
    };
  });
});<commit_msg>Keep connection to server open between test cases<commit_after>var WebSocket = require('ws');
var assert = require('assert');

var Server = require('../server').Server;
var MemoryPersistence = require('../persistence/memory');

var server = new Server({
  port: 8080,
  store: new MemoryPersistence(),
});

function socketConversation(ws, funcs) {
  var r = funcs[0]();
  if (r) ws.send(JSON.stringify(r));
  var next = 1;
  ws.onmessage = function(msg) {
    var r = funcs[next++](JSON.parse(msg.data));
    if (r) ws.send(JSON.stringify(r));
  };
}

describe('Backend', function() {
  var ws = new WebSocket('ws://localhost:8080');
  beforeEach(function() {
  });
  afterEach(function(done) {
    ws.send(JSON.stringify({type: 'reset'}));
    ws.onmessage = function() {
      done();
    };
  });
  it('get zero changes initially', function(done) {
    ws.send(JSON.stringify({
      type: 'get-changes',
      since: null,
      storeName: 'animals',
      clientId: 'foo',
    }));
    ws.onmessage = function(msg) {
      var data = JSON.parse(msg.data);
      assert.equal(data.type, 'sending-changes');
      assert.equal(data.nrOfRecordsToSync, 0);
      done();
    };
  });
  it('sends created records on request', function(done) {
    var data1, data2;
    socketConversation(ws, [
      function() {
        return {
          type: 'create',
          storeName: 'animals',
          clientId: 'foo',
          record: {name: 'Stampe', key: 1},
        };
      },
      function(data) {
        data1 = data;
        return {
          type: 'create',
          storeName: 'animals',
          clientId: 'foo',
          record: {name: 'Thumper', key: 2},
        };
      },
      function(data) {
        data2 = data;
        assert.notEqual(data1.newVersion, data2.newVersion);
        return {
          type: 'get-changes',
          since: null,
          storeName: 'animals',
          clientId: 'otherfoo',
        };
      },
      function(data) {
        assert.equal(data.nrOfRecordsToSync, 2);
      },
      function(data) {
        assert.equal(data.record.name, 'Stampe');
      },
      function(data) {
        assert.equal(data.record.name, 'Thumper');
        done();
      }
    ]);
  });
  it('only sends changes after timestamp', function(done) {
    var firstTimestamp;
    socketConversation(ws, [
      function() {
        return {
          type: 'create',
          storeName: 'animals',
          clientId: 'foo',
          record: {name: 'Stampe', key: 1},
        };
      },
      function(data) {
        assert.equal(data.type, 'ok');
        return {
          type: 'get-changes',
          since: null,
          storeName: 'animals',
          clientId: 'otherfoo',
        };
      },
      function(data) {
        assert.equal(data.nrOfRecordsToSync, 1);
      },
      function(data) {
        assert.equal(data.record.name, 'Stampe');
        firstTimestamp = data.timestamp;
        assert.equal(data.record.name, 'Stampe');
        return {
          type: 'create',
          storeName: 'animals',
          clientId: 'foo',
          record: {name: 'Smask', key: 2},
        };
      },
      function(data) {
        assert.equal(data.type, 'ok');
        return {
          type: 'get-changes',
          since: firstTimestamp,
          storeName: 'animals',
          clientId: 'otherfoo',
        };
      },
      function(data) {
        assert.equal(data.nrOfRecordsToSync, 1);
      },
      function(data) {
        assert.equal(data.record.name, 'Smask');
        done();
      }
    ]);
  });
  it('only sends changes from requested store', function(done) {
    socketConversation(ws, [
      function() {
        return {
          type: 'create',
          storeName: 'animals',
          clientId: 'foo',
          record: {name: 'Stampe', key: 1},
        };
      },
      function(data) {
        data1 = data;
        return {
          type: 'create',
          storeName: 'roads',
          clientId: 'foo',
          record: {length: 100, key: 2},
        };
      },
      function(data) {
        return {
          type: 'get-changes',
          since: null,
          storeName: 'roads',
          clientId: 'otherfoo',
        };
      },
      function(data) {
        assert.equal(data.nrOfRecordsToSync, 1);
      },
      function(data) {
        assert.equal(data.record.length, 100);
        return {
          type: 'get-changes',
          since: null,
          storeName: 'animals',
          clientId: 'otherfoo',
        };
      },
      function(data) {
        assert.equal(data.nrOfRecordsToSync, 1);
      },
      function(data) {
        assert.equal(data.record.name, 'Stampe');
        done();
      },
    ]);
  });
});
{'fraction_non_alphanumeric': 0.1512766172166717, 'fraction_numerical': 0.007064284993440307, 'mean_word_length': 1.680551798755748, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3352120', 'n_tokens_mistral': 3038, 'n_tokens_neox': 2947, 'n_words': 584}