diff --git a/client/recordUtil.js b/client/recordUtil.js index 27160f5..f4fa2a8 100644 --- a/client/recordUtil.js +++ b/client/recordUtil.js @@ -250,25 +250,49 @@ const mergeRecord = (record1, record2) => { /** * Within an array of [record, object], merge items whose records have the - * same objectId. + * same objectId and same actions. + * Requirements: + * 1) should not combine CREATE when object is not null: + * [[CREATE, object], [UPDATE, object]] => [[CREATE, object], [UPDATE, object]] + * [[CREATE, null], [UPDATE, null]] => [[CREATE(2), null]] + * [[CREATE, null], [UPDATE, object]] => ??? seems impossible => [[CREATE(2), object]] + * 2) should not combine any DELETE + * 3) all of UPDATE should be combined to single update * @param {Array} recordsAndObjects Same format as input to resolveRecords(). * @returns {Array} */ const mergeRecords = (recordsAndObjects) => { - const objectIdMap = {} // map of objectId to [record, object] + var mergedResult = [] recordsAndObjects.forEach((recordAndObject) => { const record = recordAndObject[0] - const object = recordAndObject[1] + const existingObject = recordAndObject[1] const id = JSON.stringify(record.objectId) - if (objectIdMap[id]) { - const mergedRecord = mergeRecord(objectIdMap[id][0], record) - objectIdMap[id] = [mergedRecord, object] - } else { - objectIdMap[id] = recordAndObject + + if (!mergedResult.length || + (record.action === proto.actions.CREATE && existingObject) || + record.action === proto.actions.DELETE) { + mergedResult.push(recordAndObject) + return } - }) - // webkit does not support Object.values - return Object.keys(objectIdMap).map((key) => objectIdMap[key]) + + // We reach this point in 2 cases + // 1. record.action is UPDATE => should merge with UPDATE or [CREATE, null] + // 2. record.action is CREATE and existingObject is null => should merge with UPDATE or [CREATE, null] + + // Go through mergedResult from last to first + for (var j = mergedResult.length - 1; j >= 0; --j) { + if (JSON.stringify(mergedResult[j][0].objectId) === id && + // Should not merge with CREATE or DELETE, only UPDATE + (mergedResult[j][0].action === proto.actions.UPDATE || + (mergedResult[j][0].action === proto.actions.CREATE && !mergedResult[j][1]))) { + mergedResult[j][0] = mergeRecord(mergedResult[j][0], record) + return + } + } + mergedResult.push(recordAndObject) + }) // recordsAndObjects.forEach + + return mergedResult } /** diff --git a/test/client/fixtures/resolveLaptop.js b/test/client/fixtures/resolveLaptop.js index bea0fae..bbd3c5a 100644 --- a/test/client/fixtures/resolveLaptop.js +++ b/test/client/fixtures/resolveLaptop.js @@ -1 +1 @@ -module.exports.data = [[{"action":0,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.facebook.com/","title":"Facebook – log in or sign up"}},"deviceId":[1],"objectData":"bookmark","objectId":[180,199,181,215,249,207,92,243,63,69,164,188,146,53,214,145],"syncTimestamp":1499182364028},null],[{"action":1,"bookmark":{"isFolder":false,"site":{"creationTime":0,"customTitle":"","favicon":"https://abs.twimg.com/favicons/favicon.ico","lastAccessedTime":1499182519524,"location":"https://twitter.com/brave","title":"Brave Software (@brave) | Twitter"}},"deviceId":[0],"objectData":"bookmark","objectId":[102,169,71,255,160,7,199,37,174,1,89,148,37,235,137,188],"syncTimestamp":1499182523711},null],[{"action":0,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://en.m.wikipedia.org/wiki/Main_Page","title":"Wikipedia, the free encyclopedia"}},"deviceId":[1],"objectData":"bookmark","objectId":[50,155,253,23,226,251,247,250,190,65,73,221,118,109,70,61],"syncTimestamp":1499182560767},null],[{"action":0,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://www.yahoo.com/","title":"Yahoo"}},"deviceId":[1],"objectData":"bookmark","objectId":[100,238,226,152,162,241,78,165,80,209,69,56,43,239,246,120],"syncTimestamp":1499183235567},null],[{"action":1,"bookmark":{"isFolder":false,"site":{"creationTime":0,"customTitle":"","favicon":"chrome-extension://mnojpmjdmbbfmejpflffifhffcmidifd/img/newtab/defaultTopSitesIcon/brave.ico","lastAccessedTime":1,"location":"https://brave.com/","title":"Brave Software | Building a Better Web"}},"deviceId":[0],"objectData":"bookmark","objectId":[57,70,144,212,237,131,12,182,117,184,46,131,46,82,113,92],"syncTimestamp":1499183408150},null],[{"action":0,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://www.theguardian.com/international","title":"News, sport and opinion from the Guardian's global edition | The Guardian"}},"deviceId":[2],"objectData":"bookmark","objectId":[50,18,21,48,179,186,120,100,165,225,251,230,29,214,223,181],"syncTimestamp":1499197431917},null],[{"action":0,"bookmark":{"isFolder":true,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder1"}},"deviceId":[2],"objectData":"bookmark","objectId":[8,146,83,105,4,60,9,110,111,10,161,34,175,6,41,174],"syncTimestamp":1499197515513},null],[{"action":1,"bookmark":{"isFolder":false,"parentFolderObjectId":[8,146,83,105,4,60,9,110,111,10,161,34,175,6,41,174],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://www.theguardian.com/international","title":"News, sport and opinion from the Guardian's global edition | The Guardian"}},"deviceId":[2],"objectData":"bookmark","objectId":[50,18,21,48,179,186,120,100,165,225,251,230,29,214,223,181],"syncTimestamp":1499197515547},null],[{"action":0,"bookmark":{"isFolder":true,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder0"}},"deviceId":[2],"objectData":"bookmark","objectId":[185,137,250,90,121,233,6,219,254,150,22,101,172,45,193,26],"syncTimestamp":1499197572153},null],[{"action":1,"bookmark":{"isFolder":true,"parentFolderObjectId":[185,137,250,90,121,233,6,219,254,150,22,101,172,45,193,26],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder1"}},"deviceId":[2],"objectData":"bookmark","objectId":[8,146,83,105,4,60,9,110,111,10,161,34,175,6,41,174],"syncTimestamp":1499197572168},null],[{"action":1,"bookmark":{"isFolder":true,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder1"}},"deviceId":[2],"objectData":"bookmark","objectId":[8,146,83,105,4,60,9,110,111,10,161,34,175,6,41,174],"syncTimestamp":1499197616732},null],[{"action":0,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.slashdot.org/","title":"Slashdot"}},"deviceId":[2],"objectData":"bookmark","objectId":[28,88,77,105,60,148,56,216,234,167,75,220,142,91,173,37],"syncTimestamp":1499197733375},null],[{"action":0,"bookmark":{"isFolder":true,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder2"}},"deviceId":[4],"objectData":"bookmark","objectId":[12,150,93,122,6,116,243,239,237,197,68,195,0,188,183,150],"syncTimestamp":1499276712538},null],[{"action":1,"bookmark":{"isFolder":false,"parentFolderObjectId":[12,150,93,122,6,116,243,239,237,197,68,195,0,188,183,150],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://www.yahoo.com/","title":"Yahoo"}},"deviceId":[4],"objectData":"bookmark","objectId":[100,238,226,152,162,241,78,165,80,209,69,56,43,239,246,120],"syncTimestamp":1499276712550},null],[{"action":0,"bookmark":{"isFolder":true,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder3"}},"deviceId":[4],"objectData":"bookmark","objectId":[186,183,34,202,153,217,44,45,153,87,34,117,39,115,246,80],"syncTimestamp":1499276733365},null],[{"action":1,"bookmark":{"isFolder":true,"parentFolderObjectId":[186,183,34,202,153,217,44,45,153,87,34,117,39,115,246,80],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder2"}},"deviceId":[4],"objectData":"bookmark","objectId":[12,150,93,122,6,116,243,239,237,197,68,195,0,188,183,150],"syncTimestamp":1499276733388},null],[{"action":1,"bookmark":{"isFolder":true,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder2"}},"deviceId":[4],"objectData":"bookmark","objectId":[12,150,93,122,6,116,243,239,237,197,68,195,0,188,183,150],"syncTimestamp":1499276801913},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.slashdot.org/","title":"Slashdot"}},"deviceId":[4],"objectData":"bookmark","objectId":[28,88,77,105,60,148,56,216,234,167,75,220,142,91,173,37],"syncTimestamp":1499277601715},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.slashdot.org/","title":"Slashdot"}},"deviceId":[4],"objectData":"bookmark","objectId":[28,88,77,105,60,148,56,216,234,167,75,220,142,91,173,37],"syncTimestamp":1499277601736},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.slashdot.org/","title":"Slashdot"}},"deviceId":[4],"objectData":"bookmark","objectId":[28,88,77,105,60,148,56,216,234,167,75,220,142,91,173,37],"syncTimestamp":1499277601751},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.slashdot.org/","title":"Slashdot"}},"deviceId":[4],"objectData":"bookmark","objectId":[28,88,77,105,60,148,56,216,234,167,75,220,142,91,173,37],"syncTimestamp":1499277601768},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.slashdot.org/","title":"Slashdot"}},"deviceId":[4],"objectData":"bookmark","objectId":[28,88,77,105,60,148,56,216,234,167,75,220,142,91,173,37],"syncTimestamp":1499277601785},null],[{"action":2,"bookmark":{"isFolder":false,"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.facebook.com/","title":"Facebook – log in or sign up"}},"deviceId":[5],"objectData":"bookmark","objectId":[180,199,181,215,249,207,92,243,63,69,164,188,146,53,214,145],"syncTimestamp":1499277751881},null],[{"action":2,"bookmark":{"isFolder":false,"site":{"creationTime":0,"customTitle":"","favicon":"https://abs.twimg.com/favicons/favicon.ico","lastAccessedTime":1499182519524,"location":"https://twitter.com/brave","title":"Brave Software (@brave) | Twitter"}},"deviceId":[5],"objectData":"bookmark","objectId":[102,169,71,255,160,7,199,37,174,1,89,148,37,235,137,188],"syncTimestamp":1499277754662},null],[{"action":2,"bookmark":{"isFolder":false,"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://en.m.wikipedia.org/wiki/Main_Page","title":"Wikipedia, the free encyclopedia"}},"deviceId":[5],"objectData":"bookmark","objectId":[50,155,253,23,226,251,247,250,190,65,73,221,118,109,70,61],"syncTimestamp":1499277757092},null],[{"action":2,"bookmark":{"isFolder":false,"site":{"creationTime":0,"customTitle":"","favicon":"chrome-extension://mnojpmjdmbbfmejpflffifhffcmidifd/img/newtab/defaultTopSitesIcon/brave.ico","lastAccessedTime":1,"location":"https://brave.com/","title":"Brave Software | Building a Better Web"}},"deviceId":[5],"objectData":"bookmark","objectId":[57,70,144,212,237,131,12,182,117,184,46,131,46,82,113,92],"syncTimestamp":1499277759402},null]] +module.exports.data = [[{"action":0,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.facebook.com/","title":"Facebook – log in or sign up"}},"deviceId":[1],"objectData":"bookmark","objectId":[180,199,181,215,249,207,92,243,63,69,164,188,146,53,214,145],"syncTimestamp":1499182364028},null],[{"action":1,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"https://abs.twimg.com/favicons/favicon.ico","lastAccessedTime":1499182519524,"location":"https://twitter.com/brave","title":"Brave Software (@brave) | Twitter"}},"deviceId":[0],"objectData":"bookmark","objectId":[102,169,71,255,160,7,199,37,174,1,89,148,37,235,137,188],"syncTimestamp":1499182523711},null],[{"action":0,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://en.m.wikipedia.org/wiki/Main_Page","title":"Wikipedia, the free encyclopedia"}},"deviceId":[1],"objectData":"bookmark","objectId":[50,155,253,23,226,251,247,250,190,65,73,221,118,109,70,61],"syncTimestamp":1499182560767},null],[{"action":0,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://www.yahoo.com/","title":"Yahoo"}},"deviceId":[1],"objectData":"bookmark","objectId":[100,238,226,152,162,241,78,165,80,209,69,56,43,239,246,120],"syncTimestamp":1499183235567},null],[{"action":1,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"chrome-extension://mnojpmjdmbbfmejpflffifhffcmidifd/img/newtab/defaultTopSitesIcon/brave.ico","lastAccessedTime":1,"location":"https://brave.com/","title":"Brave Software | Building a Better Web"}},"deviceId":[0],"objectData":"bookmark","objectId":[57,70,144,212,237,131,12,182,117,184,46,131,46,82,113,92],"syncTimestamp":1499183408150},null],[{"action":0,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://www.theguardian.com/international","title":"News, sport and opinion from the Guardian's global edition | The Guardian"}},"deviceId":[2],"objectData":"bookmark","objectId":[50,18,21,48,179,186,120,100,165,225,251,230,29,214,223,181],"syncTimestamp":1499197431917},null],[{"action":0,"bookmark":{"isFolder":true,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder1"}},"deviceId":[2],"objectData":"bookmark","objectId":[8,146,83,105,4,60,9,110,111,10,161,34,175,6,41,174],"syncTimestamp":1499197515513},null],[{"action":1,"bookmark":{"isFolder":false,"parentFolderObjectId":[8,146,83,105,4,60,9,110,111,10,161,34,175,6,41,174],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://www.theguardian.com/international","title":"News, sport and opinion from the Guardian's global edition | The Guardian"}},"deviceId":[2],"objectData":"bookmark","objectId":[50,18,21,48,179,186,120,100,165,225,251,230,29,214,223,181],"syncTimestamp":1499197515547},null],[{"action":0,"bookmark":{"isFolder":true,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder0"}},"deviceId":[2],"objectData":"bookmark","objectId":[185,137,250,90,121,233,6,219,254,150,22,101,172,45,193,26],"syncTimestamp":1499197572153},null],[{"action":1,"bookmark":{"isFolder":true,"parentFolderObjectId":[185,137,250,90,121,233,6,219,254,150,22,101,172,45,193,26],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder1"}},"deviceId":[2],"objectData":"bookmark","objectId":[8,146,83,105,4,60,9,110,111,10,161,34,175,6,41,174],"syncTimestamp":1499197572168},null],[{"action":1,"bookmark":{"isFolder":true,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder1"}},"deviceId":[2],"objectData":"bookmark","objectId":[8,146,83,105,4,60,9,110,111,10,161,34,175,6,41,174],"syncTimestamp":1499197616732},null],[{"action":0,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.slashdot.org/","title":"Slashdot"}},"deviceId":[2],"objectData":"bookmark","objectId":[28,88,77,105,60,148,56,216,234,167,75,220,142,91,173,37],"syncTimestamp":1499197733375},null],[{"action":0,"bookmark":{"isFolder":true,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder2"}},"deviceId":[4],"objectData":"bookmark","objectId":[12,150,93,122,6,116,243,239,237,197,68,195,0,188,183,150],"syncTimestamp":1499276712538},null],[{"action":1,"bookmark":{"isFolder":false,"parentFolderObjectId":[12,150,93,122,6,116,243,239,237,197,68,195,0,188,183,150],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://www.yahoo.com/","title":"Yahoo"}},"deviceId":[4],"objectData":"bookmark","objectId":[100,238,226,152,162,241,78,165,80,209,69,56,43,239,246,120],"syncTimestamp":1499276712550},null],[{"action":0,"bookmark":{"isFolder":true,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder3"}},"deviceId":[4],"objectData":"bookmark","objectId":[186,183,34,202,153,217,44,45,153,87,34,117,39,115,246,80],"syncTimestamp":1499276733365},null],[{"action":1,"bookmark":{"isFolder":true,"parentFolderObjectId":[186,183,34,202,153,217,44,45,153,87,34,117,39,115,246,80],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder2"}},"deviceId":[4],"objectData":"bookmark","objectId":[12,150,93,122,6,116,243,239,237,197,68,195,0,188,183,150],"syncTimestamp":1499276733388},null],[{"action":1,"bookmark":{"isFolder":true,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"","title":"Folder2"}},"deviceId":[4],"objectData":"bookmark","objectId":[12,150,93,122,6,116,243,239,237,197,68,195,0,188,183,150],"syncTimestamp":1499276801913},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.slashdot.org/","title":"Slashdot"}},"deviceId":[4],"objectData":"bookmark","objectId":[28,88,77,105,60,148,56,216,234,167,75,220,142,91,173,37],"syncTimestamp":1499277601715},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.slashdot.org/","title":"Slashdot"}},"deviceId":[4],"objectData":"bookmark","objectId":[28,88,77,105,60,148,56,216,234,167,75,220,142,91,173,37],"syncTimestamp":1499277601736},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.slashdot.org/","title":"Slashdot"}},"deviceId":[4],"objectData":"bookmark","objectId":[28,88,77,105,60,148,56,216,234,167,75,220,142,91,173,37],"syncTimestamp":1499277601751},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.slashdot.org/","title":"Slashdot"}},"deviceId":[4],"objectData":"bookmark","objectId":[28,88,77,105,60,148,56,216,234,167,75,220,142,91,173,37],"syncTimestamp":1499277601768},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.slashdot.org/","title":"Slashdot"}},"deviceId":[4],"objectData":"bookmark","objectId":[28,88,77,105,60,148,56,216,234,167,75,220,142,91,173,37],"syncTimestamp":1499277601785},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://m.facebook.com/","title":"Facebook – log in or sign up"}},"deviceId":[5],"objectData":"bookmark","objectId":[180,199,181,215,249,207,92,243,63,69,164,188,146,53,214,145],"syncTimestamp":1499277751881},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"https://abs.twimg.com/favicons/favicon.ico","lastAccessedTime":1499182519524,"location":"https://twitter.com/brave","title":"Brave Software (@brave) | Twitter"}},"deviceId":[5],"objectData":"bookmark","objectId":[102,169,71,255,160,7,199,37,174,1,89,148,37,235,137,188],"syncTimestamp":1499277754662},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"","lastAccessedTime":0,"location":"https://en.m.wikipedia.org/wiki/Main_Page","title":"Wikipedia, the free encyclopedia"}},"deviceId":[5],"objectData":"bookmark","objectId":[50,155,253,23,226,251,247,250,190,65,73,221,118,109,70,61],"syncTimestamp":1499277757092},null],[{"action":2,"bookmark":{"isFolder":false,"parentFolderObjectId":[],"site":{"creationTime":0,"customTitle":"","favicon":"chrome-extension://mnojpmjdmbbfmejpflffifhffcmidifd/img/newtab/defaultTopSitesIcon/brave.ico","lastAccessedTime":1,"location":"https://brave.com/","title":"Brave Software | Building a Better Web"}},"deviceId":[5],"objectData":"bookmark","objectId":[57,70,144,212,237,131,12,182,117,184,46,131,46,82,113,92],"syncTimestamp":1499277759402},null]] diff --git a/test/client/recordUtil.js b/test/client/recordUtil.js index fa3dc3b..0a87639 100644 --- a/test/client/recordUtil.js +++ b/test/client/recordUtil.js @@ -377,7 +377,7 @@ test('recordUtil.resolve', (t) => { }) test('recordUtil.resolveRecords()', (t) => { - t.plan(5) + t.plan(7) t.test(`${t.name} resolves same data cross-platform on laptop and android`, (t) => { t.plan(1) @@ -405,7 +405,7 @@ test('recordUtil.resolveRecords()', (t) => { t.deepEquals(resolved, expected, t.name) }) - t.test(`${t.name} sequential Updates should become no op`, (t) => { + t.test(`${t.name} sequential Updates should become no op if the merged result equals to existingObject`, (t) => { t.plan(1) const update1 = UpdateRecord({ objectId: recordBookmark.objectId, @@ -437,11 +437,47 @@ test('recordUtil.resolveRecords()', (t) => { { site: Object.assign({}, siteProps, updateSiteProps) } ) }) - const input = [[recordBookmark, null], [updateBookmark, null]] + var recordBookmarkCopy = recordBookmark + recordBookmarkCopy.action = proto.actions.CREATE + const input = [[recordBookmarkCopy, null], [updateBookmark, null]] const resolved = recordUtil.resolveRecords(input) t.deepEquals(resolved, [expectedRecord], t.name) }) + t.test(`${t.name} Create + Update + Update of an existing object should resolve to a separate Update`, (t) => { + t.plan(1) + + var createBookmark = recordBookmark + createBookmark.action = proto.actions.CREATE + const existingObject = recordBookmark + + var updateBookmark1 = updateBookmark + updateBookmark1.bookmark.site.title = 'Title1' + var updateBookmark2 = updateBookmark + updateBookmark2.bookmark.site.title = 'Title2' + + const input = [[createBookmark, existingObject], [updateBookmark1, existingObject], [updateBookmark2, existingObject]] + const resolved = recordUtil.resolveRecords(input) + const expected = [updateBookmark2] + t.deepEquals(resolved, expected, t.name) + }) + + t.test(`${t.name} Update + Update of an existing object should resolve a single Update`, (t) => { + t.plan(1) + + const existingObject = recordBookmark + + var updateBookmark1 = updateBookmark + updateBookmark1.bookmark.site.title = 'Title1' + var updateBookmark2 = updateBookmark + updateBookmark2.bookmark.site.title = 'Title2' + + const input = [[updateBookmark1, existingObject], [updateBookmark2, existingObject]] + const resolved = recordUtil.resolveRecords(input) + const expected = [updateBookmark2] + t.deepEquals(resolved, expected, t.name) + }) + t.test(`${t.name} resolves bookmark records with same parent folder`, (t) => { t.plan(1) const record = {