<commit_before>'use strict';

var test = require('tape');

var idb = require('./idb.js');
var dropDb = idb.dropDb;
var recreateDb = idb.recreateDb;

var PersDB = require('../lib/persdb.js');

// create two conflict fixtures
var conflict1 = {
  n: {
    h: {
      id: 'customers\x01john',
      v: 'Aaaa',
      pa: []
    },
    b: {
      eamil: '<EMAIL>'
    }
  },
  l: null,
  c: null,
  lcas: [],
  err: null
};
var conflict2 = {
  n: {
    h: {
      id: 'customers\x01jane',
      v: 'Aaaa',
      pa: []
    },
    b: {
      eamil: '<EMAIL>'
    }
  },
  l: null,
  c: null,
  lcas: [],
  err: null
};

// create some stores
var opts = {
  stores: {
    customers: { keyPath: 'email' },
    employees: {},
    _pdb: {},
    _conflicts: { autoIncrement: true }
  },
  fixtures: {
    customers: [
      { email: '<EMAIL>' },
    ],
    _conflicts: [conflict1, conflict2]
  }
}

var conflictStore = '_conflicts';

var pdbOpts = {
  snapshotStore: '_pdb',
  conflictStore: conflictStore
}
// ensure an empty database exists
recreateDb('PersDB', opts, function(err, db) {
  if (err) throw err;


  test('PersDB.createNode', function(t) {
    t.plan(2);

    PersDB.createNode(db, pdbOpts, (err, pdb) => {
      t.error(err);
      t.ok(pdb);
    });
  });

  test('pdb.getConflicts', function(t) {
    // first create a pdb node to test with
    t.plan(2);

    PersDB.createNode(db, pdbOpts, (err, pdb) => {
      if (err) throw err;

      t.test('list all', function(st) {
        var i = 0;
        pdb.getConflicts(function next(conflictKey, conflictObject, proceed) {
          i++;
          switch (i) {
          case 1:
            st.equal(conflictKey, 1);
            st.deepLooseEqual(conflictObject, conflict1);
            break;
          case 2:
            st.equal(conflictKey, 2);
            st.deepLooseEqual(conflictObject, conflict2);
            break;
          default:
            throw new Error('unexpected');
          }
          proceed();
        }, function(err) {
          st.error(err);
          st.equal(i, 2);
          st.end();
        });
      });

      t.test('don\'t proceed', function(st) {
        var i = 0;
        pdb.getConflicts(function next(conflictKey, conflictObject, proceed) {
          i++;
          switch (i) {
          case 1:
            st.equal(conflictKey, 1);
            st.deepLooseEqual(conflictObject, conflict1);
            break;
          default:
            throw new Error('unexpected');
          }
          proceed(false);
        }, function(err) {
          st.error(err);
          st.equal(i, 1);
          st.end();
        });
      });
    });
  });

  test('pdb.getConflict', function(t) {
    // first create a pdb node to test with
    t.plan(1);

    PersDB.createNode(db, pdbOpts, (err, pdb) => {
      if (err) throw err;

      t.test('get existing conflict', function(st) {
        pdb.getConflict(1, function(err, conflict, current) {
          st.error(err);
          st.deepEqual(conflict, conflict1);
          st.deepEqual(current, undefined);
          st.end()
        });
      });
    });
  });

  // drop db
  test.onFinish(function() {
    db.close();
    dropDb('PersDB', function() {})
  });
});<commit_msg>browser: getConflict with non-existing conflict<commit_after>'use strict';

var test = require('tape');

var idb = require('./idb.js');
var dropDb = idb.dropDb;
var recreateDb = idb.recreateDb;

var PersDB = require('../lib/persdb.js');

// create two conflict fixtures
var conflict1 = {
  n: {
    h: {
      id: 'customers\x01john',
      v: 'Aaaa',
      pa: []
    },
    b: {
      eamil: '<EMAIL>'
    }
  },
  l: null,
  c: null,
  lcas: [],
  err: null
};
var conflict2 = {
  n: {
    h: {
      id: 'customers\x01jane',
      v: 'Aaaa',
      pa: []
    },
    b: {
      eamil: '<EMAIL>'
    }
  },
  l: null,
  c: null,
  lcas: [],
  err: null
};

// create some stores
var opts = {
  stores: {
    customers: { keyPath: 'email' },
    employees: {},
    _pdb: {},
    _conflicts: { autoIncrement: true }
  },
  fixtures: {
    customers: [
      { email: '<EMAIL>' },
    ],
    _conflicts: [conflict1, conflict2]
  }
}

var conflictStore = '_conflicts';

var pdbOpts = {
  snapshotStore: '_pdb',
  conflictStore: conflictStore
}
// ensure an empty database exists
recreateDb('PersDB', opts, function(err, db) {
  if (err) throw err;


  test('PersDB.createNode', function(t) {
    t.plan(2);

    PersDB.createNode(db, pdbOpts, (err, pdb) => {
      t.error(err);
      t.ok(pdb);
    });
  });

  test('pdb.getConflicts', function(t) {
    // first create a pdb node to test with
    t.plan(2);

    PersDB.createNode(db, pdbOpts, (err, pdb) => {
      if (err) throw err;

      t.test('list all', function(st) {
        var i = 0;
        pdb.getConflicts(function next(conflictKey, conflictObject, proceed) {
          i++;
          switch (i) {
          case 1:
            st.equal(conflictKey, 1);
            st.deepLooseEqual(conflictObject, conflict1);
            break;
          case 2:
            st.equal(conflictKey, 2);
            st.deepLooseEqual(conflictObject, conflict2);
            break;
          default:
            throw new Error('unexpected');
          }
          proceed();
        }, function(err) {
          st.error(err);
          st.equal(i, 2);
          st.end();
        });
      });

      t.test('don\'t proceed', function(st) {
        var i = 0;
        pdb.getConflicts(function next(conflictKey, conflictObject, proceed) {
          i++;
          switch (i) {
          case 1:
            st.equal(conflictKey, 1);
            st.deepLooseEqual(conflictObject, conflict1);
            break;
          default:
            throw new Error('unexpected');
          }
          proceed(false);
        }, function(err) {
          st.error(err);
          st.equal(i, 1);
          st.end();
        });
      });
    });
  });

  test('pdb.getConflict', function(t) {
    // first create a pdb node to test with
    t.plan(2);

    PersDB.createNode(db, pdbOpts, (err, pdb) => {
      if (err) throw err;

      t.test('get existing conflict', function(st) {
        pdb.getConflict(1, function(err, conflict, current) {
          st.error(err);
          st.deepEqual(conflict, conflict1);
          st.deepEqual(current, undefined);
          st.end()
        });
      });

      t.test('get non-existing conflict', function(st) {
        pdb.getConflict(0, function(err, conflict, current) {
          st.equal(err.message, 'conflict not found');
          st.deepEqual(conflict, undefined);
          st.deepEqual(current, undefined);
          st.end()
        });
      });
    });
  });

  // drop db
  test.onFinish(function() {
    db.close();
    dropDb('PersDB', function() {})
  });
});
{'fraction_non_alphanumeric': 0.15805739514348785, 'fraction_numerical': 0.007799852832965416, 'mean_word_length': 1.8675105485232069, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '880198', 'n_tokens_mistral': 2361, 'n_tokens_neox': 2309, 'n_words': 493}