Skip to content

Commit 0367b02

Browse files
committed
tests: New test: Concurrent insert and reorder
and make it pass the test Signed-off-by: Marcel Klehr <[email protected]>
1 parent f557f30 commit 0367b02

File tree

3 files changed

+144
-9
lines changed

3 files changed

+144
-9
lines changed

src/lib/Scanner.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,22 @@ export default class Scanner<L1 extends TItemLocation, L2 extends TItemLocation>
7474
this.result.UPDATE.commit({type: ActionType.UPDATE, payload: newFolder, oldItem: oldFolder})
7575
}
7676

77-
// Generate REORDERS before diffing anything to make sure REORDERS are from top to bottom
77+
// Generate REORDERS before diffing anything to make sure REORDERS are from top to bottom (necessary for tab sync)
7878
if (newFolder.children.length > 1) {
79-
this.result.REORDER.commit({
80-
type: ActionType.REORDER,
81-
payload: newFolder,
82-
order: newFolder.children.map(i => ({ type: i.type, id: i.id })),
83-
})
79+
let needReorder = false
80+
for (let i = 0; i < Math.max(newFolder.children.length, oldFolder.children.length); i++) {
81+
if (!oldFolder.children[i] || !newFolder.children[i] || !this.mergeable(oldFolder.children[i], newFolder.children[i])) {
82+
needReorder = true
83+
break
84+
}
85+
}
86+
if (needReorder) {
87+
this.result.REORDER.commit({
88+
type: ActionType.REORDER,
89+
payload: newFolder,
90+
order: newFolder.children.map(i => ({ type: i.type, id: i.id })),
91+
})
92+
}
8493
}
8594

8695
// Preserved Items and removed Items
@@ -105,12 +114,12 @@ export default class Scanner<L1 extends TItemLocation, L2 extends TItemLocation>
105114

106115
// created Items
107116
// (using map here, because 'each' doesn't provide indices)
108-
await Parallel.map(unmatchedChildren, async(newChild, index) => {
117+
await Parallel.map(unmatchedChildren, async(newChild) => {
109118
if (oldFolder.isRoot && oldFolder.location === ItemLocation.LOCAL) {
110119
// We can't create root folders locally
111120
return
112121
}
113-
this.result.CREATE.commit({type: ActionType.CREATE, payload: newChild, index})
122+
this.result.CREATE.commit({type: ActionType.CREATE, payload: newChild, index: newFolder.children.findIndex(child => child === newChild)})
114123
}, 1)
115124
}
116125

src/lib/strategies/Default.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ export default class SyncProcess {
772772
return
773773
}
774774

775-
if (targetLocation === this.masterLocation) {
775+
if (targetLocation !== this.masterLocation) {
776776
const concurrentReorder = targetReorders.find(a =>
777777
action.payload.type === a.payload.type && Mappings.mappable(mappingsSnapshot, action.payload, a.payload))
778778
if (concurrentReorder) {

src/test/test.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5232,6 +5232,132 @@ describe('Floccus', function() {
52325232
expectTreeEqual(secondLocalTree1, secondLocalTree2, true, true)
52335233
})
52345234

5235+
it('should synchronize ordering (2)', async function() {
5236+
if (ACCOUNT_DATA.type === 'linkwarden' || ACCOUNT_DATA.type === 'karakeep') {
5237+
return this.skip()
5238+
}
5239+
expect(
5240+
(await getAllBookmarks(account1)).children
5241+
).to.have.lengthOf(0)
5242+
5243+
const localRoot1 = account1.getData().localRoot
5244+
const fooFolder = await browser.bookmarks.create({
5245+
title: 'foo',
5246+
parentId: localRoot1
5247+
})
5248+
const folder11 = await browser.bookmarks.create({
5249+
title: 'folder11',
5250+
parentId: fooFolder.id
5251+
})
5252+
const folder12 = await browser.bookmarks.create({
5253+
title: 'folder12',
5254+
parentId: fooFolder.id
5255+
})
5256+
const bookmark11 = await browser.bookmarks.create({
5257+
title: 'url11',
5258+
url: 'http://ur.l/',
5259+
parentId: fooFolder.id
5260+
})
5261+
const bookmark12 = await browser.bookmarks.create({
5262+
title: 'url12',
5263+
url: 'http://ur.ll/',
5264+
parentId: fooFolder.id
5265+
})
5266+
5267+
await account1.sync()
5268+
expect(account1.getData().error).to.not.be.ok
5269+
5270+
await account2.sync()
5271+
expect(account2.getData().error).to.not.be.ok
5272+
5273+
console.log('Checking local folder after initial sync')
5274+
5275+
const localTree1 = await account1.localTree.getBookmarksTree(true)
5276+
const localTree2 = await account2.localTree.getBookmarksTree(true)
5277+
localTree2.title = localTree1.title
5278+
expectTreeEqual(localTree1, localTree2, true, true)
5279+
5280+
const localTree1Foo = localTree1.children.find(item => item.title === 'foo')
5281+
const localTree2Foo = localTree2.children.find(item => item.title === 'foo')
5282+
5283+
const newBookmark1 = await browser.bookmarks.create({
5284+
title: 'newBookmark1',
5285+
url: 'http://ur.lllll/',
5286+
parentId: localTree1Foo.id
5287+
})
5288+
await browser.bookmarks.move(newBookmark1.id, { index: 0 })
5289+
await browser.bookmarks.move(folder11.id, { index: 4 })
5290+
await browser.bookmarks.move(folder12.id, { index: 3 })
5291+
await browser.bookmarks.move(bookmark11.id, { index: 2 })
5292+
await browser.bookmarks.move(bookmark12.id, { index: 1 })
5293+
5294+
const newBookmark2 = await browser.bookmarks.create({
5295+
title: 'newBookmark2',
5296+
url: 'http://ur.llllll/',
5297+
parentId: localTree2Foo.id
5298+
})
5299+
await browser.bookmarks.move(newBookmark2.id, { index: 3 })
5300+
5301+
await account1.sync()
5302+
expect(account1.getData().error).to.not.be.ok
5303+
5304+
await account2.sync()
5305+
expect(account2.getData().error).to.not.be.ok
5306+
5307+
await account1.sync()
5308+
expect(account1.getData().error).to.not.be.ok
5309+
5310+
const secondLocalTree1 = await account1.localTree.getBookmarksTree(
5311+
true
5312+
)
5313+
5314+
expectTreeEqual(
5315+
secondLocalTree1,
5316+
new Folder({
5317+
title: secondLocalTree1.title,
5318+
children: [
5319+
new Folder({
5320+
title: 'foo',
5321+
children: [
5322+
new Bookmark({
5323+
title: 'newBookmark1',
5324+
url: newBookmark1.url
5325+
}),
5326+
new Bookmark({
5327+
title: 'url12',
5328+
url: bookmark12.url
5329+
}),
5330+
new Bookmark({
5331+
title: 'url11',
5332+
url: bookmark11.url
5333+
}),
5334+
new Folder({
5335+
title: 'folder12',
5336+
children: []
5337+
}),
5338+
new Bookmark({
5339+
title: 'newBookmark2',
5340+
url: newBookmark2.url
5341+
}),
5342+
new Folder({
5343+
title: 'folder11',
5344+
children: []
5345+
}),
5346+
]
5347+
}),
5348+
]
5349+
}),
5350+
true,
5351+
true
5352+
)
5353+
5354+
const secondLocalTree2 = await account2.localTree.getBookmarksTree(
5355+
true
5356+
)
5357+
secondLocalTree2.title = secondLocalTree1.title
5358+
expectTreeEqual(secondLocalTree1, secondLocalTree2, true, true)
5359+
})
5360+
52355361
// Skipping this, because nextcloud adapter currently
52365362
// isn't able to track bookmarks across dirs, thus in this
52375363
// scenario both bookmarks survive :/

0 commit comments

Comments
 (0)