Kaynağa Gözat

add test for non-overlapping insert-delete case

Brian Gough 10 yıl önce
ebeveyn
işleme
ed0aaa189d

+ 2 - 1
services/track-changes/app/coffee/UpdateCompressor.coffee

@@ -133,6 +133,7 @@ module.exports = UpdateCompressor =
 		else if firstOp.i? and secondOp.d? and firstOp.p <= secondOp.p <= (firstOp.p + firstOp.i.length)
 		else if firstOp.i? and secondOp.d? and firstOp.p <= secondOp.p <= (firstOp.p + firstOp.i.length)
 			offset = secondOp.p - firstOp.p
 			offset = secondOp.p - firstOp.p
 			insertedText = firstOp.i.slice(offset, offset + secondOp.d.length)
 			insertedText = firstOp.i.slice(offset, offset + secondOp.d.length)
+			# Only trim the insert when the delete is fully contained within in it
 			if insertedText == secondOp.d
 			if insertedText == secondOp.d
 				insert = strRemove(firstOp.i, offset, secondOp.d.length)
 				insert = strRemove(firstOp.i, offset, secondOp.d.length)
 				return [
 				return [
@@ -146,7 +147,7 @@ module.exports = UpdateCompressor =
 					v: secondUpdate.v
 					v: secondUpdate.v
 				]
 				]
 			else
 			else
-				# This shouldn't be possible!
+				# This will only happen if the delete extends outside the insert
 				return [firstUpdate, secondUpdate]
 				return [firstUpdate, secondUpdate]
 
 
 		else
 		else

+ 20 - 0
services/track-changes/test/unit/coffee/UpdateCompressor/UpdateCompressorTests.coffee

@@ -263,6 +263,26 @@ describe "UpdateCompressor", ->
 					v: 43
 					v: 43
 				}]
 				}]
 
 
+			it "should not combine updates with overlap beyond the end", ->
+				expect(@UpdateCompressor.compressUpdates [{
+					op: { p: 3, i: "foobar" }
+					meta: ts: @ts1, user_id: @user_id
+					v: 42
+				}, {
+					op: { p: 6, d: "bardle" }
+					meta: ts: @ts2, user_id: @user_id
+					v: 43
+				}])
+				.to.deep.equal [{
+					op: { p: 3, i: "foobar" }
+					meta: start_ts: @ts1, end_ts: @ts1, user_id: @user_id
+					v: 42
+				}, {
+					op: { p: 6, d: "bardle" }
+					meta: start_ts: @ts2, end_ts: @ts2, user_id: @user_id
+					v: 43
+				}]
+
 		describe "noop - insert", ->
 		describe "noop - insert", ->
 			it "should leave them untouched", ->
 			it "should leave them untouched", ->
 				expect(@UpdateCompressor.compressUpdates [{
 				expect(@UpdateCompressor.compressUpdates [{