<commit_before>var EmberSyncQueueModel = require('lib/ember-sync/ember-sync-queue-model').default;
var EmberSync = require(["lib/ember-sync"]).default;

var env = {}, emberSync,
    onlineResults,
    offlineStore, onlineStore,
    container;

module("Integration/Lib/EmberSync", {
  setup: function() {
    resetLocalStorage();

    var InventoryItem = DS.Model.extend({
      name:           DS.attr('string'),
      description:    DS.attr('string'),
      price:          DS.attr('string'),
      entryForSaleId: DS.attr('string'),
      onSale:         DS.attr('boolean')
    });

    var CashEntry = DS.Model.extend({
      amount:      DS.attr('number'),
      description: DS.attr('string'),
      createdAt:   DS.attr('date')
    });

    env = setupOfflineOnlineStore({
      inventoryItem: InventoryItem,
      cashEntry: CashEntry,
      emberSyncQueueModel: EmberSyncQueueModel
    });

    offlineStore = env.store;
    onlineStore = env.onlineStore;

    emberSync = EmberSync.create({ container: env });
  }
});

var Synchronize = function(record) {
  ok(true, "Synchronizing");
  return new Ember.RSVP.Promise(function(resolve, reject) {
    Em.run.later(function() { emberSync.synchronizeOnline(); }, 5);
    Em.run.later(function() { resolve(); }, 70);
  });
}

var StartQunit = function() { start(); }

var assertItemDoesntExistOffline = function(type, id) {
  var assertMessage = "No item exists offline for id "+id,
      queryFunction = (!!parseInt(id) ? 'find' : 'findQuery');

  return offlineStore[queryFunction](type, id).then(function(item) {
    console.error("Record for "+type+" should be in the offline store");
    ok(false, assertMessage);
    return Ember.RSVP.resolve();
  }, function() {
    ok(true, assertMessage);

    return Ember.RSVP.resolve();
  });
}

var assertItemExistsOffline = function(type, id) {
  var assertMessage = ""+type+" record was found for id "+id,
      queryFunction = (!!parseInt(id) ? 'find' : 'findQuery');

  return offlineStore[queryFunction](type, id).then(function(item) {
    ok(true, assertMessage);
    return Ember.RSVP.resolve();
  }, function() {
    ok(false, assertMessage);
    return Ember.RSVP.resolve();
  });
}

test("#createRecord creates a new record instance", function() {
  var record, prop;

  Em.run(function() {
    prop = {
      name: "Fender",
      description: "Super guitar",
      price: "123",
      entryForSaleId: "1",
      onSale: true
    };
    record = emberSync.createRecord('inventoryItem', prop);

    equal(record.get('name'), 'Fender', 'name is correct');
    equal(record.get('description'), 'Super guitar', 'description is correct');
    equal(record.get('price'), '123', 'price is correct');
    equal(record.get('entryForSaleId'), '1', 'entryForSaleId is correct');
    equal(record.get('onSale'), true, 'onSale is correct');

    var originalRecord    = record.emberSync.get('record'),
        emberSyncInstance = record.emberSync.get('emberSync'),
        recordType        = record.emberSync.get('recordType'),
        recordProperties  = record.emberSync.get('recordProperties');

    equal(record,            originalRecord,  'emberSync.record is correct');
    equal(recordType,        'inventoryItem', 'emberSync.recordType is correct');
    equal(emberSyncInstance, emberSync,       'emberSync instance is correct');
  });
});

test("#save - creates a record offline and online", function() {
  var record, offlineSave, generatedId;
  stop();

  Em.run(function() {
    record = emberSync.createRecord('inventoryItem', {
      name: "Fender",
      description: "Super guitar",
      price: "123",
      entryForSaleId: "1",
      onSale: true
    });

    generatedId = record.get('id');
    ok(generatedId, "ID is valid ("+generatedId+")");

    offlineSave = record.emberSync.save();
    offlineSave.then(function(record) {
      ok(true, "Record saved offline");
      equal(record.get('id'),             generatedId,    "id is correct");
      equal(record.get('name'),           "Fender",       "name is correct");
      equal(record.get('description'),    "Super guitar", "description is correct");
      equal(record.get('price'),          "123",          "price is correct");
      equal(record.get('entryForSaleId'), "1",            "entryForSaleId is correct");
      equal(record.get('onSale'),         true,           "onSale is correct");

      Em.run.later(function() {
        emberSync.synchronizeOnline();
      }, 5);

      Em.run.later(function() {
        var record = getModelLS('online', 'inventoryItem', generatedId);

        ok(true, "Record saved online");
        equal(record.id,             generatedId,    "id is correct");
        equal(record.name,           "Fender",       "name is correct");
        equal(record.description,    "Super guitar", "description is correct");
        equal(record.price,          "123",          "price is correct");
        equal(record.entryForSaleId, "1",            "entryForSaleId is correct");
        equal(record.onSale,         true,           "onSale is correct");
        start();
      }, 30);

    }, function() {
      ok(false, "Record saved offline");
      start();
    });
  });
});

test("#find - searches offline/online simultaneously, syncing online into offline and returning a stream of data", function() {

  onlineStore.push('cashEntry', {
    id: 1,
    amount: '120',
    description: 'First entry',
    createdAt: myDate()
  });

  stop();

  Em.run(function() {

    assertItemDoesntExistOffline('cashEntry', 1).then(function() {<commit_msg>fix race condition on localStorage reset<commit_after>var EmberSyncQueueModel = require('lib/ember-sync/ember-sync-queue-model').default;
var EmberSync = require(["lib/ember-sync"]).default;

var env = {}, emberSync,
    onlineResults,
    offlineStore, onlineStore,
    container;

module("Integration/Lib/EmberSync", {
  setup: function() {
    resetLocalStorage();

    var InventoryItem = DS.Model.extend({
      name:           DS.attr('string'),
      description:    DS.attr('string'),
      price:          DS.attr('string'),
      entryForSaleId: DS.attr('string'),
      onSale:         DS.attr('boolean')
    });

    var CashEntry = DS.Model.extend({
      amount:      DS.attr('number'),
      description: DS.attr('string'),
      createdAt:   DS.attr('date')
    });

    env = setupOfflineOnlineStore({
      inventoryItem: InventoryItem,
      cashEntry: CashEntry,
      emberSyncQueueModel: EmberSyncQueueModel
    });

    offlineStore = env.store;
    onlineStore = env.onlineStore;

    emberSync = EmberSync.create({ container: env });
  }
});

var Synchronize = function(record) {
  ok(true, "Synchronizing");
  return new Ember.RSVP.Promise(function(resolve, reject) {
    Em.run.later(function() { emberSync.synchronizeOnline(); }, 5);
    Em.run.later(function() { resolve(); }, 70);
  });
}

var StartQunit = function() { start(); }

var assertItemDoesntExistOffline = function(type, id) {
  var assertMessage = "No item exists offline for id "+id,
      queryFunction = (!!parseInt(id) ? 'find' : 'findQuery');

  return offlineStore[queryFunction](type, id).then(function(item) {
    console.error("Record for "+type+" should be in the offline store");
    ok(false, assertMessage);
    return Ember.RSVP.resolve();
  }, function() {
    ok(true, assertMessage);

    return Ember.RSVP.resolve();
  });
}

var assertItemExistsOffline = function(type, id) {
  var assertMessage = ""+type+" record was found for id "+id,
      queryFunction = (!!parseInt(id) ? 'find' : 'findQuery');

  return offlineStore[queryFunction](type, id).then(function(item) {
    ok(true, assertMessage);
    return Ember.RSVP.resolve();
  }, function() {
    ok(false, assertMessage);
    return Ember.RSVP.resolve();
  });
}

test("#createRecord creates a new record instance", function() {
  var record, prop;

  Em.run(function() {
    prop = {
      name: "Fender",
      description: "Super guitar",
      price: "123",
      entryForSaleId: "1",
      onSale: true
    };
    record = emberSync.createRecord('inventoryItem', prop);

    equal(record.get('name'), 'Fender', 'name is correct');
    equal(record.get('description'), 'Super guitar', 'description is correct');
    equal(record.get('price'), '123', 'price is correct');
    equal(record.get('entryForSaleId'), '1', 'entryForSaleId is correct');
    equal(record.get('onSale'), true, 'onSale is correct');

    var originalRecord    = record.emberSync.get('record'),
        emberSyncInstance = record.emberSync.get('emberSync'),
        recordType        = record.emberSync.get('recordType'),
        recordProperties  = record.emberSync.get('recordProperties');

    equal(record,            originalRecord,  'emberSync.record is correct');
    equal(recordType,        'inventoryItem', 'emberSync.recordType is correct');
    equal(emberSyncInstance, emberSync,       'emberSync instance is correct');
  });
});

test("#save - creates a record offline and online", function() {
  var record, offlineSave, generatedId;
  stop();

  Em.run(function() {
    record = emberSync.createRecord('inventoryItem', {
      name: "Fender",
      description: "Super guitar",
      price: "123",
      entryForSaleId: "1",
      onSale: true
    });

    generatedId = record.get('id');
    ok(generatedId, "ID is valid ("+generatedId+")");

    offlineSave = record.emberSync.save();
    offlineSave.then(function(record) {
      ok(true, "Record saved offline");
      equal(record.get('id'),             generatedId,    "id is correct");
      equal(record.get('name'),           "Fender",       "name is correct");
      equal(record.get('description'),    "Super guitar", "description is correct");
      equal(record.get('price'),          "123",          "price is correct");
      equal(record.get('entryForSaleId'), "1",            "entryForSaleId is correct");
      equal(record.get('onSale'),         true,           "onSale is correct");

      Em.run.later(function() {
        emberSync.synchronizeOnline();
      }, 5);

      Em.run.later(function() {
        var record = getModelLS('online', 'inventoryItem', generatedId);

        ok(true, "Record saved online");
        equal(record.id,             generatedId,    "id is correct");
        equal(record.name,           "Fender",       "name is correct");
        equal(record.description,    "Super guitar", "description is correct");
        equal(record.price,          "123",          "price is correct");
        equal(record.entryForSaleId, "1",            "entryForSaleId is correct");
        equal(record.onSale,         true,           "onSale is correct");
        start();
      }, 50);

    }, function() {
      ok(false, "Record saved offline");
      start();
    });
  });
});

test("#find - searches offline/online simultaneously, syncing online into offline and returning a stream of data", function() {

  onlineStore.push('cashEntry', {
    id: 1,
    amount: '120',
    description: 'First entry',
    createdAt: myDate()
  });

  stop();

  Em.run(function() {

    assertItemDoesntExistOffline('cashEntry', 1).then(function() {
{'fraction_non_alphanumeric': 0.15286624203821655, 'fraction_numerical': 0.005562034628151072, 'mean_word_length': 3.027456647398844, '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': '7374661', 'n_tokens_mistral': 3411, 'n_tokens_neox': 3278, 'n_words': 747}