| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
QUnit.module( 've.dm.TransactionSquasher' ); |
| 8 |
|
| 9 |
QUnit.test( 'squash', ( assert ) => { |
| 10 |
const boldHash = 'hfbe3cfe099b83e1e', |
| 11 |
italicHash = 'he4e7c54e2204d10ba'; |
| 12 |
|
| 13 |
function insertionTxList( before, itemSequence, after ) { |
| 14 |
return itemSequence.split( '' ).map( ( item, n ) => [ before + n, [ '', item ], after ] ); |
| 15 |
} |
| 16 |
|
| 17 |
function annotationTx( allData, start, stop, length, method, hash, spliceAt ) { |
| 18 |
const oldData = Array.prototype.slice.call( allData, start, stop ); |
| 19 |
|
| 20 |
let newData; |
| 21 |
if ( method === 'set' ) { |
| 22 |
newData = oldData.map( ( item ) => { |
| 23 |
let ch, hashList; |
| 24 |
if ( Array.isArray( item ) ) { |
| 25 |
ch = item[ 0 ]; |
| 26 |
hashList = item[ 1 ]; |
| 27 |
} else { |
| 28 |
ch = item; |
| 29 |
hashList = []; |
| 30 |
} |
| 31 |
hashList = [].concat( |
| 32 |
hashList.slice( 0, spliceAt ), |
| 33 |
hash, |
| 34 |
hashList.slice( spliceAt ) |
| 35 |
); |
| 36 |
return [ ch, hashList ]; |
| 37 |
} ); |
| 38 |
} else { |
| 39 |
newData = oldData.map( ( item ) => { |
| 40 |
const ch = item[ 0 ]; |
| 41 |
let hashList = item[ 1 ]; |
| 42 |
hashList = [].concat( |
| 43 |
hashList.slice( 0, spliceAt ), |
| 44 |
hashList.slice( spliceAt + 1 ) |
| 45 |
); |
| 46 |
if ( hashList.length === 0 ) { |
| 47 |
return ch; |
| 48 |
} else { |
| 49 |
return [ ch, hashList ]; |
| 50 |
} |
| 51 |
} ); |
| 52 |
} |
| 53 |
return [ start, [ oldData, newData ], length - stop ]; |
| 54 |
} |
| 55 |
|
| 56 |
function sequence( data, ...hashList ) { |
| 57 |
return data.split( '' ).map( ( item ) => hashList.length === 0 ? item : [ item, hashList ] ); |
| 58 |
} |
| 59 |
|
| 60 |
const cases = [ |
| 61 |
{ |
| 62 |
message: 'Empty transaction list', |
| 63 |
transactions: [], |
| 64 |
error: true |
| 65 |
}, |
| 66 |
{ |
| 67 |
message: 'Replace part of replacement', |
| 68 |
transactions: [ |
| 69 |
[ 4, [ 'Aa', 'Bb' ], 4 ], |
| 70 |
[ 4, [ 'B', 'D' ], 5 ] |
| 71 |
], |
| 72 |
squashed: [ 4, [ 'Aa', 'Db' ], 4 ] |
| 73 |
}, |
| 74 |
{ |
| 75 |
message: 'Replace interior retain', |
| 76 |
transactions: [ |
| 77 |
[ 4, [ 'Aa', 'Bb' ], 4 ], |
| 78 |
[ 1, [ 'Xx', 'Yy' ], 7 ] |
| 79 |
], |
| 80 |
squashed: [ 1, [ 'Xx', 'Yy' ], 1, [ 'Aa', 'Bb' ], 4 ] |
| 81 |
}, |
| 82 |
{ |
| 83 |
message: 'Remove insertion', |
| 84 |
transactions: [ |
| 85 |
[ 4, [ '', 'abc' ], 4 ], |
| 86 |
[ 4, [ 'abc', '' ], 4 ] |
| 87 |
], |
| 88 |
squashed: [ 8 ] |
| 89 |
}, |
| 90 |
{ |
| 91 |
message: 'Re-insert removal', |
| 92 |
transactions: [ |
| 93 |
[ 4, [ 'abc', '' ], 4 ], |
| 94 |
[ 4, [ '', 'abc' ], 4 ] |
| 95 |
], |
| 96 |
squashed: [ 4, [ 'abc', 'abc' ], 4 ] |
| 97 |
}, |
| 98 |
{ |
| 99 |
message: 'Replace part of bolded replacement', |
| 100 |
transactions: [ |
| 101 |
[ 4, [ 'ef', 'EF' ], 4 ], |
| 102 |
annotationTx( 'abcdEFghij', 3, 8, 10, 'set', boldHash, 0 ), |
| 103 |
[ 4, [ |
| 104 |
[ [ 'E', [ boldHash ] ] ], |
| 105 |
[ 'E' ] |
| 106 |
], 5 ] |
| 107 |
], |
| 108 |
squashed: [ 3, [ 'defgh', [].concat( |
| 109 |
sequence( 'd', boldHash ), |
| 110 |
'E', |
| 111 |
sequence( 'Fgh', boldHash ) |
| 112 |
) ], 2 ] |
| 113 |
}, |
| 114 |
{ |
| 115 |
message: 'insert, insert, remove', |
| 116 |
transactions: [ |
| 117 |
[ 1, [ '', 'x' ], 3 ], |
| 118 |
[ 2, [ '', 'y' ], 3 ], |
| 119 |
[ 2, [ 'y', '' ], 3 ] |
| 120 |
], |
| 121 |
squashed: [ 1, [ '', 'x' ], 3 ] |
| 122 |
}, |
| 123 |
{ |
| 124 |
message: 'type, annotate, clear', |
| 125 |
transactions: [].concat( |
| 126 |
insertionTxList( 1, 'Foo bar baz qux quux', 3 ), |
| 127 |
[ annotationTx( 'WFoo bar baz qux quuxXYZ', 1, 21, 24, 'set', boldHash, 0 ) ], |
| 128 |
[ annotationTx( [].concat( |
| 129 |
'W', |
| 130 |
sequence( 'Foo bar baz qux quux', boldHash ), |
| 131 |
'XYZ' |
| 132 |
), 1, 4, 24, 'set', italicHash, 1 ) ], |
| 133 |
[ annotationTx( [].concat( |
| 134 |
'W', |
| 135 |
sequence( 'Foo', boldHash, italicHash ), |
| 136 |
sequence( ' bar baz qux quux', boldHash ), |
| 137 |
'XYZ' |
| 138 |
), 9, 12, 24, 'set', italicHash, 1 ) ], |
| 139 |
[ annotationTx( [].concat( |
| 140 |
'W', |
| 141 |
sequence( 'Foo', boldHash, italicHash ), |
| 142 |
sequence( ' bar ', boldHash ), |
| 143 |
sequence( 'baz', boldHash, italicHash ), |
| 144 |
sequence( ' qux quux', boldHash ), |
| 145 |
'XYZ' |
| 146 |
), 1, 17, 24, 'clear', boldHash, 0 ) ] |
| 147 |
), |
| 148 |
squashed: [ 1, [ '', [].concat( |
| 149 |
sequence( 'Foo', italicHash ), |
| 150 |
sequence( ' bar ' ), |
| 151 |
sequence( 'baz', italicHash ), |
| 152 |
sequence( ' qux ' ), |
| 153 |
sequence( 'quux', boldHash ) |
| 154 |
) ], 3 ] |
| 155 |
}, |
| 156 |
{ |
| 157 |
message: 'insert, change attribute, change attribute again', |
| 158 |
transactions: [ |
| 159 |
[ [ '', [ |
| 160 |
{ type: 'heading', attributes: { level: 1 } }, |
| 161 |
{ type: '/heading' } |
| 162 |
] ] ], |
| 163 |
[ { type: 'attribute', key: 'level', from: 1, to: 2 }, 2 ], |
| 164 |
[ { type: 'attribute', key: 'level', from: 2, to: 3 }, 2 ] |
| 165 |
], |
| 166 |
squashed: [ [ '', [ |
| 167 |
{ type: 'heading', attributes: { level: 3 } }, |
| 168 |
{ type: '/heading' } |
| 169 |
] ] ] |
| 170 |
}, |
| 171 |
{ |
| 172 |
message: 'insert, add attribute, remove attribute', |
| 173 |
transactions: [ |
| 174 |
[ [ '', [ |
| 175 |
{ type: 'heading' }, |
| 176 |
{ type: '/heading' } |
| 177 |
] ] ], |
| 178 |
[ { type: 'attribute', key: 'level', from: undefined, to: 1 }, 2 ], |
| 179 |
[ { type: 'attribute', key: 'level', from: 1, to: undefined }, 2 ] |
| 180 |
], |
| 181 |
squashed: [ [ '', [ |
| 182 |
{ type: 'heading' }, |
| 183 |
{ type: '/heading' } |
| 184 |
] ] ] |
| 185 |
}, |
| 186 |
{ |
| 187 |
message: 'h1->h2->h3', |
| 188 |
transactions: [ |
| 189 |
[ { type: 'attribute', key: 'level', from: 1, to: 2 }, 10 ], |
| 190 |
[ { type: 'attribute', key: 'level', from: 2, to: 3 }, 10 ] |
| 191 |
], |
| 192 |
squashed: [ { type: 'attribute', key: 'level', from: 1, to: 3 }, 10 ] |
| 193 |
}, |
| 194 |
{ |
| 195 |
message: 'h?->h1', |
| 196 |
transactions: [ |
| 197 |
[ { type: 'attribute', key: 'level', from: 1, to: 2 }, 10 ], |
| 198 |
[ { type: 'attribute', key: 'level', from: 2, to: 3 }, 10 ] |
| 199 |
], |
| 200 |
squashed: [ { type: 'attribute', key: 'level', from: 1, to: 3 }, 10 ] |
| 201 |
}, |
| 202 |
{ |
| 203 |
message: 'h1->h2->p->remove contents', |
| 204 |
transactions: [ |
| 205 |
[ { type: 'attribute', key: 'level', from: 1, to: 2 }, 9 ], |
| 206 |
[ [ |
| 207 |
[ { type: 'heading', attributes: { level: 2 } } ], |
| 208 |
[ { type: 'paragraph' } ] |
| 209 |
], 7, [ |
| 210 |
[ { type: '/heading' } ], |
| 211 |
[ { type: '/paragraph' } ] |
| 212 |
] ], |
| 213 |
[ 1, [ 'abcdefg', '' ], 1 ] |
| 214 |
], |
| 215 |
squashed: [ [ [].concat( |
| 216 |
{ type: 'heading', attributes: { level: 1 } }, |
| 217 |
sequence( 'abcdefg' ), |
| 218 |
{ type: '/heading' } |
| 219 |
), [ |
| 220 |
{ type: 'paragraph' }, |
| 221 |
{ type: '/paragraph' } |
| 222 |
] ] ] |
| 223 |
}, |
| 224 |
{ |
| 225 |
message: 'add attribute whose value is a reference type, then change it', |
| 226 |
transactions: [ |
| 227 |
[ [ '', [ |
| 228 |
{ type: 'foo', attributes: { bar: { baz: 42 } } }, |
| 229 |
{ type: '/foo' } |
| 230 |
] ] ], |
| 231 |
[ { type: 'attribute', key: 'bar', from: { baz: 42 }, to: { baz: 99 } }, 1 ] |
| 232 |
], |
| 233 |
squashed: [ [ '', [ |
| 234 |
{ type: 'foo', attributes: { bar: { baz: 99 } } }, |
| 235 |
{ type: '/foo' } |
| 236 |
] ] ] |
| 237 |
}, |
| 238 |
{ |
| 239 |
message: 'Overlapping replacements', |
| 240 |
transactions: [ |
| 241 |
[ [ 'AB', 'ab' ], 1 ], |
| 242 |
[ 1, [ 'bC', 'Bc' ] ] |
| 243 |
], |
| 244 |
squashed: [ [ 'ABC', 'aBc' ] ] |
| 245 |
}, |
| 246 |
{ |
| 247 |
message: 'Insert at an attribute change', |
| 248 |
transactions: [ |
| 249 |
[ { type: 'attribute', key: 'level', from: 1, to: 2 }, 9 ], |
| 250 |
[ [ '', 'AB' ], 9 ] |
| 251 |
], |
| 252 |
squashed: [ |
| 253 |
[ '', 'AB' ], |
| 254 |
{ type: 'attribute', key: 'level', from: 1, to: 2 }, |
| 255 |
9 |
| 256 |
] |
| 257 |
}, |
| 258 |
{ |
| 259 |
message: 'Attribute change at a retain', |
| 260 |
transactions: [ |
| 261 |
[ 9 ], |
| 262 |
[ 2, { type: 'attribute', key: 'level', from: 1, to: 2 }, 7 ] |
| 263 |
], |
| 264 |
squashed: [ |
| 265 |
2, |
| 266 |
{ type: 'attribute', key: 'level', from: 1, to: 2 }, |
| 267 |
7 |
| 268 |
] |
| 269 |
}, |
| 270 |
{ |
| 271 |
message: 'Remove from the middle of an insert', |
| 272 |
transactions: [ |
| 273 |
[ 2, [ 'ABCDEF', 'abcdef' ], 10 ], |
| 274 |
[ 4, [ 'cd', '' ], 12 ] |
| 275 |
], |
| 276 |
squashed: [ |
| 277 |
2, |
| 278 |
[ 'ABCDEF', 'abef' ], |
| 279 |
10 |
| 280 |
] |
| 281 |
} |
| 282 |
]; |
| 283 |
|
| 284 |
cases.forEach( ( caseItem ) => { |
| 285 |
const transactions = caseItem.transactions.map( ( txData ) => ve.dm.Transaction.static.deserialize( txData ) ); |
| 286 |
ve.deepFreeze( transactions ); |
| 287 |
if ( caseItem.error ) { |
| 288 |
assert.throws( () => { |
| 289 |
ve.dm.TransactionSquasher.static.squash( transactions ); |
| 290 |
}, Error, caseItem.message ); |
| 291 |
|
| 292 |
return; |
| 293 |
} |
| 294 |
const squashed = ve.dm.Transaction.static.deserialize( caseItem.squashed ); |
| 295 |
|
| 296 |
assert.deepEqual( |
| 297 |
ve.dm.TransactionSquasher.static.squash( transactions ).operations, |
| 298 |
squashed.operations, |
| 299 |
caseItem.message + ': squash all' |
| 300 |
); |
| 301 |
|
| 302 |
for ( let j = 1, jLen = transactions.length - 1; j < jLen; j++ ) { |
| 303 |
const squashedBefore = ve.dm.TransactionSquasher.static.squash( transactions.slice( 0, j ) ); |
| 304 |
const squashedAfter = ve.dm.TransactionSquasher.static.squash( transactions.slice( j ) ); |
| 305 |
assert.deepEqual( |
| 306 |
ve.dm.TransactionSquasher.static.squash( [ |
| 307 |
squashedBefore, |
| 308 |
squashedAfter |
| 309 |
] ).operations, |
| 310 |
squashed.operations, |
| 311 |
caseItem.message + ': squash squashed halves split at ' + j |
| 312 |
); |
| 313 |
} |
| 314 |
} ); |
| 315 |
} ); |