daily pastebin goal
11%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ot = require('../ot'),
  2. sync = require('../sync');
  3.  
  4. function createTarget()
  5. {
  6.     return {"foo" : [{"id" : 0}, {"id" : 1}, {"id" : 5}, {"id" : 6}],
  7.         "bar" : {"objects" : [{"id" : 8,   
  8.                    "name" : "saikat",
  9.                    "children" : ["sheena", "divia", "kami", {"id" : 5}],
  10.                    "otherObjects" : [{"id" : 100}, {"id" : "testID"}]},
  11.                   {"id" : 9,
  12.                    "name" : "mockingbird",
  13.                    "children" : ["hatchling1", "hatchling2", "hatchling3", {"id" : 50}]},
  14.                   {"id" : 10,
  15.                    "name" : "hullo",
  16.                    "children" : [{"id" : 100}, {"id" : "testID"}, {"id" : "innerArray", "objects" : [{"id" : 100}]}]}]}};
  17. }
  18.  
  19. var testDiffs = [
  20.     // transform['create']['create']
  21.     [sync.created(["foo", 3], 10),
  22.      sync.created(["bar", "objects", 2], 20)],
  23.  
  24.     [sync.created(["bar", "objects", 1], 20),
  25.      sync.created(["bar", "objects", {"id" : 9}, "children", 0], "hatchling0")],
  26.  
  27.     [sync.created(["bar", "objects", {"id" : 8}, "children", 1], "test"),
  28.      sync.created(["bar", "objects", {"id" : 8}, "children", 2], "test1")],
  29.  
  30.     [sync.created(["bar", "objects", {"id" : 9}, "children", 1], "test"),
  31.      sync.created(["bar", "objects", {"id" : 9}, "children", 1], "test1")],
  32.  
  33.     [sync.created(["bar", "objects", 2], "test"),
  34.      sync.created(["bar", "objects", 1], "test1")],
  35.  
  36.     [sync.created(["foo", 3], 10),
  37.      sync.removed(["bar", "objects", {"id" : 8, "index" : 0}])],
  38.  
  39.     // transform['create']['remove']
  40.     [sync.created(["bar", "objects", 1], 10),
  41.      sync.removed(["bar", "objects", {"id" : 8, "index" : 0}])],
  42.  
  43.     [sync.created(["bar", "objects", 1], 10),
  44.      sync.removed(["bar", "objects", {"id" : 9, "index" : 1}])],
  45.  
  46.     [sync.created(["bar", "objects", {"id" : 8}, "children", 2], "test"),
  47.      sync.removed(["bar", "objects", {"id" : 8, "index" : 0}]),
  48.      'noop'],
  49.  
  50.     [sync.created(["bar", "objects", {"id" : 8}, "children", 2], "test"),
  51.      sync.removed(["bar", "objects", {"id" : 9, "index" : 1}])],
  52.  
  53.     [sync.created(["bar", "objects", {"id" : 8}, "children", 2], "test"),
  54.      sync.removed(["bar", "objects", {"id" : 8}, "otherObjects", {"id" : 100, "index" : 0}])],
  55.  
  56.     [sync.created(["bar", "objects", 2], "test"),
  57.      sync.removed(["bar", "objects", {"id" : 8}, "children", {"id" : 5, "index" : 3}])],
  58.  
  59.     // transform['create']['edit']
  60.     [sync.created(["foo", 3], 8),
  61.      sync.edited(["bar", "objects", {"id" : 8}, "name"], "supratik")],
  62.  
  63.     [sync.created(["bar", "objects", 0], "testValue"),
  64.      sync.edited(["bar", "objects", {"id" : 9}, "name"],  "macawkingbird")],
  65.  
  66.     [sync.created(["bar", "objects", {"id" : 8}, "otherObjects", 1], "testValue"),
  67.      sync.edited(["bar", "objects", {"id" : 8}, "name"], "macawkingbird")],
  68.  
  69.     // transform['create']['move']
  70.  
  71.     // path1 <> path2, path1 <> movedPath - both unchanged
  72.     [sync.created(["foo", 3], 8),
  73.      sync.moved(["bar", "objects", {"id" : 8, "index" : 0}],
  74.         ["bar", "objects", {"id" : 9}, "children", 1])],
  75.  
  76.     // path1 in path2, path1 <> movedPath - both unchanged
  77.     [sync.created(["bar", "objects", 1], "test"),
  78.      sync.moved(["bar", "objects", {"id" : 8}, "children", {"id" : 5, "index" : 3}],
  79.         ["foo", 3])],
  80.  
  81.     // path2 in path 1, path1 <> movedPath - need to move the original diff to follow path2
  82.     [sync.created(["bar", "objects", {"id" : 8}, "children", 2], "test"),
  83.      sync.moved(["bar", "objects", {"id" : 8, "index" : 0}],
  84.         ["foo", 3])],
  85.  
  86.     // path1 = path2, path1 <> movedPath - depends on the position of the moved object
  87.     [sync.created(["bar", "objects", 1], "test"),
  88.      sync.moved(["bar", "objects", {"id" : 8, "index" : 0}],
  89.         ["foo", 3])],
  90.  
  91.     [sync.created(["bar", "objects", 1], "test"),
  92.      sync.moved(["bar", "objects", {"id" : 9, "index" : 1}],
  93.         ["foo", 3])],
  94.  
  95.     [sync.created(["bar", "objects", 1], "test"),
  96.      sync.moved(["bar", "objects", {"id" : 10, "index" : 2}],
  97.         ["foo", 3])],
  98.    
  99.     // path1 <> path2, path1 in movedPath - both unchanged
  100.     [sync.created(["bar", "objects", 1], "test"),
  101.      sync.moved(["foo", {"id" : 5, "index" : 2}],
  102.         ["bar", "objects", {"id" : 9}, "children", 2])],
  103. ]
  104.  
  105. var tests = {};
  106.  
  107. function make_test(index) {
  108.     return function(assert) {
  109.     var diff1 = testDiffs[index][0][0],
  110.     diff2 = testDiffs[index][1][0],
  111.     newDiffs = ot.transform(diff1, diff2);
  112.  
  113.     if (testDiffs[index][2] == 'noop')
  114.         assert.equal(newDiffs.diff1[0].action, 'noop');
  115.     else
  116.         assert.notEqual(newDiffs.diff1[0].action, 'noop');
  117.  
  118.     if (testDiffs[index][3] == 'noop')
  119.         assert.equal(newDiffs.diff2[0].action, 'noop');
  120.     else
  121.         assert.notEqual(newDiffs.diff2[0].action, 'noop');
  122.  
  123.     var target1 = createTarget(),
  124.     target2 = createTarget();
  125.  
  126.     var diffsToApply = [diff1].concat(newDiffs.diff2),
  127.     appliedCommands = applyCommands(target1, diffsToApply),
  128.     shouldHaveApplied = [];
  129.     assert.equal(appliedCommands.errors.length, 0);
  130.     for (var i = 0; i < diffsToApply.length; ++i)
  131.         if (diffsToApply[i].action !== 'noop')
  132.         shouldHaveApplied.push(diffsToApply[i]);
  133.  
  134.     assert.deepEqual(appliedCommands.appliedCommands, shouldHaveApplied);
  135.  
  136.     diffsToApply = [diff2].concat(newDiffs.diff1);
  137.     appliedCommands = sync.applyCommands(target2, diffsToApply),
  138.     shouldHaveApplied = [];
  139.  
  140.     assert.equal(appliedCommands.errors.length, 0);
  141.     for (var i = 0; i < diffsToApply.length; ++i)
  142.         if (diffsToApply[i].action !== 'noop')
  143.         shouldHaveApplied.push(diffsToApply[i]);
  144.  
  145.     assert.deepEqual(appliedCommands.appliedCommands, shouldHaveApplied);
  146.  
  147.     assert.deepEqual(target1, target2);
  148.     };
  149. }
  150.  
  151. var testCount = testDiffs.length;
  152. for (var i = 0; i < testCount; ++i) {
  153.     tests[i] = make_test(i);
  154. }
  155.  
  156. module.exports = tests;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top