Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var ot = require('../ot'),
- sync = require('../sync');
- function createTarget()
- {
- return {"foo" : [{"id" : 0}, {"id" : 1}, {"id" : 5}, {"id" : 6}],
- "bar" : {"objects" : [{"id" : 8,
- "name" : "saikat",
- "children" : ["sheena", "divia", "kami", {"id" : 5}],
- "otherObjects" : [{"id" : 100}, {"id" : "testID"}]},
- {"id" : 9,
- "name" : "mockingbird",
- "children" : ["hatchling1", "hatchling2", "hatchling3", {"id" : 50}]},
- {"id" : 10,
- "name" : "hullo",
- "children" : [{"id" : 100}, {"id" : "testID"}, {"id" : "innerArray", "objects" : [{"id" : 100}]}]}]}};
- }
- var testDiffs = [
- // transform['create']['create']
- [sync.created(["foo", 3], 10),
- sync.created(["bar", "objects", 2], 20)],
- [sync.created(["bar", "objects", 1], 20),
- sync.created(["bar", "objects", {"id" : 9}, "children", 0], "hatchling0")],
- [sync.created(["bar", "objects", {"id" : 8}, "children", 1], "test"),
- sync.created(["bar", "objects", {"id" : 8}, "children", 2], "test1")],
- [sync.created(["bar", "objects", {"id" : 9}, "children", 1], "test"),
- sync.created(["bar", "objects", {"id" : 9}, "children", 1], "test1")],
- [sync.created(["bar", "objects", 2], "test"),
- sync.created(["bar", "objects", 1], "test1")],
- [sync.created(["foo", 3], 10),
- sync.removed(["bar", "objects", {"id" : 8, "index" : 0}])],
- // transform['create']['remove']
- [sync.created(["bar", "objects", 1], 10),
- sync.removed(["bar", "objects", {"id" : 8, "index" : 0}])],
- [sync.created(["bar", "objects", 1], 10),
- sync.removed(["bar", "objects", {"id" : 9, "index" : 1}])],
- [sync.created(["bar", "objects", {"id" : 8}, "children", 2], "test"),
- sync.removed(["bar", "objects", {"id" : 8, "index" : 0}]),
- 'noop'],
- [sync.created(["bar", "objects", {"id" : 8}, "children", 2], "test"),
- sync.removed(["bar", "objects", {"id" : 9, "index" : 1}])],
- [sync.created(["bar", "objects", {"id" : 8}, "children", 2], "test"),
- sync.removed(["bar", "objects", {"id" : 8}, "otherObjects", {"id" : 100, "index" : 0}])],
- [sync.created(["bar", "objects", 2], "test"),
- sync.removed(["bar", "objects", {"id" : 8}, "children", {"id" : 5, "index" : 3}])],
- // transform['create']['edit']
- [sync.created(["foo", 3], 8),
- sync.edited(["bar", "objects", {"id" : 8}, "name"], "supratik")],
- [sync.created(["bar", "objects", 0], "testValue"),
- sync.edited(["bar", "objects", {"id" : 9}, "name"], "macawkingbird")],
- [sync.created(["bar", "objects", {"id" : 8}, "otherObjects", 1], "testValue"),
- sync.edited(["bar", "objects", {"id" : 8}, "name"], "macawkingbird")],
- // transform['create']['move']
- // path1 <> path2, path1 <> movedPath - both unchanged
- [sync.created(["foo", 3], 8),
- sync.moved(["bar", "objects", {"id" : 8, "index" : 0}],
- ["bar", "objects", {"id" : 9}, "children", 1])],
- // path1 in path2, path1 <> movedPath - both unchanged
- [sync.created(["bar", "objects", 1], "test"),
- sync.moved(["bar", "objects", {"id" : 8}, "children", {"id" : 5, "index" : 3}],
- ["foo", 3])],
- // path2 in path 1, path1 <> movedPath - need to move the original diff to follow path2
- [sync.created(["bar", "objects", {"id" : 8}, "children", 2], "test"),
- sync.moved(["bar", "objects", {"id" : 8, "index" : 0}],
- ["foo", 3])],
- // path1 = path2, path1 <> movedPath - depends on the position of the moved object
- [sync.created(["bar", "objects", 1], "test"),
- sync.moved(["bar", "objects", {"id" : 8, "index" : 0}],
- ["foo", 3])],
- [sync.created(["bar", "objects", 1], "test"),
- sync.moved(["bar", "objects", {"id" : 9, "index" : 1}],
- ["foo", 3])],
- [sync.created(["bar", "objects", 1], "test"),
- sync.moved(["bar", "objects", {"id" : 10, "index" : 2}],
- ["foo", 3])],
- // path1 <> path2, path1 in movedPath - both unchanged
- [sync.created(["bar", "objects", 1], "test"),
- sync.moved(["foo", {"id" : 5, "index" : 2}],
- ["bar", "objects", {"id" : 9}, "children", 2])],
- ]
- var tests = {};
- function make_test(index) {
- return function(assert) {
- var diff1 = testDiffs[index][0][0],
- diff2 = testDiffs[index][1][0],
- newDiffs = ot.transform(diff1, diff2);
- if (testDiffs[index][2] == 'noop')
- assert.equal(newDiffs.diff1[0].action, 'noop');
- else
- assert.notEqual(newDiffs.diff1[0].action, 'noop');
- if (testDiffs[index][3] == 'noop')
- assert.equal(newDiffs.diff2[0].action, 'noop');
- else
- assert.notEqual(newDiffs.diff2[0].action, 'noop');
- var target1 = createTarget(),
- target2 = createTarget();
- var diffsToApply = [diff1].concat(newDiffs.diff2),
- appliedCommands = applyCommands(target1, diffsToApply),
- shouldHaveApplied = [];
- assert.equal(appliedCommands.errors.length, 0);
- for (var i = 0; i < diffsToApply.length; ++i)
- if (diffsToApply[i].action !== 'noop')
- shouldHaveApplied.push(diffsToApply[i]);
- assert.deepEqual(appliedCommands.appliedCommands, shouldHaveApplied);
- diffsToApply = [diff2].concat(newDiffs.diff1);
- appliedCommands = sync.applyCommands(target2, diffsToApply),
- shouldHaveApplied = [];
- assert.equal(appliedCommands.errors.length, 0);
- for (var i = 0; i < diffsToApply.length; ++i)
- if (diffsToApply[i].action !== 'noop')
- shouldHaveApplied.push(diffsToApply[i]);
- assert.deepEqual(appliedCommands.appliedCommands, shouldHaveApplied);
- assert.deepEqual(target1, target2);
- };
- }
- var testCount = testDiffs.length;
- for (var i = 0; i < testCount; ++i) {
- tests[i] = make_test(i);
- }
- module.exports = tests;
Add Comment
Please, Sign In to add comment