Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/dm/ve.dm.RebaseServer.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel Rebase client/server logic tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.dm.RebaseServer' );
8
9 QUnit.test( 'Rebase', ( assert ) => {
10 const cases = [
11 {
12 name: 'Concurrent insertions',
13 initialData: [
14 { type: 'paragraph' },
15 { type: '/paragraph' },
16 { type: 'internalList' },
17 { type: '/internalList' }
18 ],
19 clients: [ '1', '2' ],
20 ops: [
21 // Client 1 submits abc
22 [ '1', 'apply', [
23 [ 'insert', 1, [ 'a' ], 3 ],
24 [ 'insert', 2, [ 'b' ], 3 ],
25 [ 'insert', 3, [ 'c' ], 3 ]
26 ] ],
27 // Client getHistorySummary() output looks like: confirmed/sent?/unsent!
28 // Obviously, the server only has confirmed items
29 [ '1', 'assertHist', 'abc!' ],
30 [ '1', 'submit' ],
31 [ '1', 'assertHist', 'abc?' ],
32 [ '1', 'deliver' ],
33 [ 'server', 'assertHist', 'abc' ],
34
35 // Client 2 submits AB
36 [ '2', 'apply', [
37 [ 'insert', 1, [ 'A' ], 3 ],
38 [ 'insert', 2, [ 'B' ], 3 ]
39 ] ],
40 [ '2', 'assertHist', 'AB!' ],
41 [ '2', 'submit' ],
42 [ '2', 'deliver' ],
43 // Server puts client 2's insertion after client 1's
44 [ 'server', 'assertHist', 'abcAB' ],
45
46 // Client 1 inserts bolded def
47 [ '1', 'apply', [
48 [ 'insert', 4, [ 'd', 'e', 'f' ], 3 ]
49 ] ],
50 [ '1', 'assertHist', 'abc?/def!' ],
51 // Client 1 receives confirmation of abc
52 [ '1', 'receive' ],
53 [ '1', 'assertHist', 'abc/def!' ],
54 // Client 1 submits def
55 [ '1', 'submit' ],
56 [ '1', 'assertHist', 'abc/def?' ],
57 [ '1', 'deliver' ],
58 // The summary order shows that def arrived after AB in the
59 // history (even though it lies before AB in document order)
60 [ 'server', 'assertHist', 'abcABdef' ],
61
62 // Client 2 inserts underlined CD
63 [ '2', 'apply', [
64 [ 'insert', 3, [ 'C', 'D' ], 3 ]
65 ] ],
66 [ '2', 'assertHist', 'AB?/CD!' ],
67 // Client 2 receives abc and rebases over it
68 [ '2', 'receive' ],
69 [ '2', 'assert', function ( client ) {
70 assert.true( Array.isArray( client.doc.completeHistory.storeLengthAtTransaction ), 'storeLengthAtTransaction array not clobbered by rebase' );
71 } ],
72 [ '2', 'assertHist', 'abc/AB?/CD!' ],
73 // Client 2 receives confirmation of AB
74 [ '2', 'receive' ],
75 [ '2', 'assertHist', 'abcAB/CD!' ],
76 // Client 2 submits CD
77 [ '2', 'submit' ],
78 [ '2', 'assertHist', 'abcAB/CD?' ],
79 [ '2', 'deliver' ],
80 [ 'server', 'assertHist', 'abcABdefCD' ],
81
82 // Client 1 receives AB, rebases def over it
83 [ '1', 'receive' ],
84 [ '1', 'assertHist', 'abcAB/def?' ],
85 // Client 1 receives confirmation of def
86 [ '1', 'receive' ],
87 [ '1', 'assertHist', 'abcABdef' ],
88
89 // Client 1 submits ghi
90 [ '1', 'apply', [
91 [ 'insert', 9, [ 'g', 'h', 'i' ], 3 ]
92 ] ],
93 [ '1', 'assertHist', 'abcABdef/ghi!' ],
94 [ '1', 'submit' ],
95 [ '1', 'assertHist', 'abcABdef/ghi?' ],
96 [ '1', 'deliver' ],
97 [ 'server', 'assertHist', 'abcABdefCDghi' ],
98 // Client 1 receives CD, rebases ghi over it
99 [ '1', 'receive' ],
100 [ '1', 'assertHist', 'abcABdefCD/ghi?' ],
101 // Client 1 receives confirmation of ghi
102 [ '1', 'receive' ],
103 [ '1', 'assertHist', 'abcABdefCDghi' ],
104
105 // Client 2 catches up
106 [ '2', 'receive' ],
107 [ '2', 'assertHist', 'abcABdef/CD?' ],
108 [ '2', 'receive' ],
109 [ '2', 'assertHist', 'abcABdefCD' ],
110 [ '2', 'receive' ],
111 [ '2', 'assertHist', 'abcABdefCDghi' ]
112 ]
113 },
114 {
115 name: 'Conflicting deletions',
116 initialData: [
117 { type: 'paragraph' },
118 ...'abcABdefCDghi',
119 { type: '/paragraph' },
120 { type: 'internalList' },
121 { type: '/internalList' }
122 ],
123 clients: [ '1', '2' ],
124 ops: [
125 // Client 1 delivers one deletion and leaves another one in the pipeline
126 [ '1', 'apply', [
127 [ 'remove', 5, 2, 10 ]
128 ] ],
129 [ '1', 'assertHist', '-(Bd)!' ],
130 [ '1', 'submit' ],
131 [ '1', 'assertHist', '-(Bd)?' ],
132 [ '1', 'apply', [
133 [ 'remove', 3, 2, 10 ]
134 ] ],
135 [ '1', 'assertHist', '-(Bd)?/-(cA)!' ],
136 [ '1', 'submit' ],
137 [ '1', 'assertHist', '-(Bd)-(cA)?' ],
138 [ '1', 'deliver' ],
139 [ 'server', 'assertHist', '-(Bd)' ],
140 [ '1', 'receive' ],
141 [ '1', 'assertHist', '-(Bd)/-(cA)?' ],
142
143 // Client 2 applies a partially-conflicting change
144 [ '2', 'apply', [
145 [ 'insert', 1, [ 'W' ], 16 ],
146 // Conflicts with undelivered deletion of 'cA'
147 [ 'insert', 5, [ 'X' ], 13 ],
148 // Conflicts with delivered deletion of 'Bd'
149 [ 'insert', 8, [ 'Y' ], 11 ],
150 [ 'insert', 12, [ 'Z' ], 8 ]
151 ] ],
152 [ '2', 'assertHist', 'WXYZ!' ],
153 [ '2', 'submit' ],
154 [ '2', 'assertHist', 'WXYZ?' ],
155 [ '2', 'receive' ],
156 // Y conflicts with -(Bd), so Y and Z are discarded
157 [ '2', 'assertHist', '-(Bd)/WX?' ],
158
159 // Client 2 applies a "doomed" change built on top of a change that will conflict
160 [ '2', 'apply', [
161 [ 'insert', 1, [ 'V' ], 16 ]
162 ] ],
163 [ '2', 'assertHist', '-(Bd)/WX?/V!' ],
164 [ '2', 'submit' ],
165 [ '2', 'assertHist', '-(Bd)/WXV?' ],
166
167 // Client 1 delivers -(cA)
168 [ '1', 'deliver' ],
169 [ 'server', 'assertHist', '-(Bd)-(cA)' ],
170 // Client 2 delivers W (accepted) and X (rejected)
171 [ '2', 'deliver' ],
172 [ '2', 'deliver' ],
173 [ 'server', 'assertHist', '-(Bd)-(cA)W' ],
174 // Client 2 receives -(cA), discards X and V
175 [ '2', 'receive' ],
176 [ '2', 'assertHist', '-(Bd)-(cA)/W?' ],
177
178 // Client 2 inserts and submits P, server accepts
179 [ '2', 'apply', [
180 [ 'insert', 1, [ 'P' ], 13 ]
181 ] ],
182 [ '2', 'assertHist', '-(Bd)-(cA)/W?/P!' ],
183 [ '2', 'submit' ],
184 [ '2', 'assertHist', '-(Bd)-(cA)/WP?' ],
185 [ '2', 'deliver' ],
186 [ 'server', 'assertHist', '-(Bd)-(cA)WP' ],
187
188 // Client 2 receives confirmation of W and P
189 [ '2', 'receive' ],
190 [ '2', 'assertHist', '-(Bd)-(cA)W/P?' ],
191 [ '2', 'receive' ],
192 [ '2', 'assertHist', '-(Bd)-(cA)WP' ]
193 ]
194 },
195 {
196 name: 'Double client-side rebase with annotation',
197 initialData: [
198 { type: 'paragraph' },
199 { type: '/paragraph' },
200 { type: 'internalList' },
201 { type: '/internalList' }
202 ],
203 clients: [ '1', '2' ],
204 ops: [
205 // Client 1 applies a local change that introduces an annotation
206 [ '1', 'apply', {
207 start: 0,
208 transactions: [
209 {
210 o: [
211 { type: 'retain', length: 1 },
212 { type: 'replace', remove: [], insert: [
213 [ 'X', [ 'h123' ] ],
214 [ 'Y', [ 'h123' ] ],
215 [ 'Z', [ 'h123' ] ]
216 ] },
217 { type: 'retain', length: 3 }
218 ],
219 a: '1'
220 }
221 ],
222 stores: [
223 {
224 hashes: [ 'h123' ],
225 hashStore: {
226 h123: {
227 type: 'annotation',
228 value: {
229 type: 'textStyle/bold'
230 }
231 }
232 }
233 }
234 ],
235 selections: {
236 1: {
237 type: 'linear',
238 range: {
239 type: 'range',
240 from: 4,
241 to: 4
242 }
243 }
244 }
245 } ],
246 [ '1', 'assert', function ( client ) {
247 const unsubmitted = client.getChangeSince( client.sentLength, false );
248 assert.deepEqual( unsubmitted.getStore( 0 ).hashes, [ 'h123' ], 'h123 is in the store' );
249 } ],
250
251 // Client 2 submits two changes
252 [ '2', 'apply', [
253 [ 'insert', 1, [ 'a' ], 3 ]
254 ] ],
255 [ '2', 'submit' ],
256 [ '2', 'apply', [
257 [ 'insert', 2, [ 'b' ], 3 ]
258 ] ],
259 [ '2', 'submit' ],
260
261 // Client 1 rebases its local change twice
262 [ '2', 'deliver' ],
263 [ 'server', 'assertHist', 'a' ],
264 [ '1', 'receive' ],
265 [ '1', 'assertHist', 'a/XYZ!' ],
266 [ '1', 'assert', function ( client ) {
267 const unsubmitted = client.getChangeSince( client.sentLength, false );
268 assert.deepEqual( unsubmitted.getStore( 0 ).hashes, [ 'h123' ], 'h123 is still in the store after the first rebase' );
269 } ],
270
271 [ '2', 'deliver' ],
272 [ 'server', 'assertHist', 'ab' ],
273 [ '1', 'receive' ],
274 [ '1', 'assertHist', 'ab/XYZ!' ],
275 [ '1', 'assert', function ( client ) {
276 const unsubmitted = client.getChangeSince( client.sentLength, false );
277 assert.deepEqual( unsubmitted.getStore( 0 ).hashes, [ 'h123' ], 'h123 is still in the store after the second rebase' );
278 } ],
279
280 // Client 1 submits its local change
281 [ '1', 'submit' ],
282 [ '1', 'deliver' ],
283 [ 'server', 'assertHist', 'abXYZ' ]
284 ]
285 },
286 {
287 name: 'Double client-side rebase with annotation and other preceding transaction',
288 initialData: [
289 { type: 'paragraph' },
290 { type: '/paragraph' },
291 { type: 'internalList' },
292 { type: '/internalList' }
293 ],
294 clients: [ '1', '2' ],
295 ops: [
296 // Client 1 applies a local change that inserts 'Q'
297 [ '1', 'apply', [
298 [ 'insert', 1, [ 'Q' ], 3 ]
299 ] ],
300
301 // Client 2 submits two changes
302 [ '2', 'apply', [
303 [ 'insert', 1, [ 'a' ], 3 ]
304 ] ],
305 [ '2', 'submit' ],
306 [ '2', 'apply', [
307 [ 'insert', 2, [ 'b' ], 3 ]
308 ] ],
309 [ '2', 'submit' ],
310
311 // Client 1 rebases its local change over client 2's first change
312 [ '2', 'deliver' ],
313 [ 'server', 'assertHist', 'a' ],
314 [ '1', 'receive' ],
315 [ '1', 'assertHist', 'a/Q!' ],
316 // Client 1 submits its local change
317 [ '1', 'submit' ],
318
319 // Client 1 applies a local change that introduces an annotation
320 [ '1', 'apply', {
321 start: 2,
322 transactions: [
323 {
324 o: [
325 { type: 'retain', length: 2 },
326 { type: 'replace', remove: [], insert: [
327 [ 'X', [ 'h123' ] ],
328 [ 'Y', [ 'h123' ] ],
329 [ 'Z', [ 'h123' ] ]
330 ] },
331 { type: 'retain', length: 3 }
332 ],
333 a: '1'
334 }
335 ],
336 stores: [
337 {
338 hashes: [ 'h123' ],
339 hashStore: {
340 h123: {
341 type: 'annotation',
342 value: {
343 type: 'textStyle/bold'
344 }
345 }
346 }
347 }
348 ],
349 selections: {
350 1: {
351 type: 'linear',
352 range: {
353 type: 'range',
354 from: 4,
355 to: 4
356 }
357 }
358 }
359 } ],
360 [ '1', 'assert', function ( client ) {
361 const unsubmitted = client.getChangeSince( client.sentLength, false );
362 assert.deepEqual( unsubmitted.getStore( 0 ).hashes, [ 'h123' ], 'h123 is in the store' );
363 } ],
364
365 // Client 1 rebases its local changes over client 2's second change
366 [ '2', 'deliver' ],
367 [ 'server', 'assertHist', 'ab' ],
368 [ '1', 'receive' ],
369 [ '1', 'assertHist', 'ab/Q?/XYZ!' ],
370 [ '1', 'assert', function ( client ) {
371 const unsubmitted = client.getChangeSince( client.sentLength, false );
372 // FIXME this fails. If uncommitted = client.getChangeSince( client.commitLength, false );
373 // then we expect uncommitted.getStore( 1 ) to contain 'h123', but instead uncommitted.getStore( 0 ) does.
374 assert.deepEqual( unsubmitted.getStore( 0 ).hashes, [ 'h123' ], 'h123 is still in the store after receiving a foreign change' );
375 } ],
376
377 // Client 1 receives its first change
378 [ '1', 'deliver' ],
379 [ 'server', 'assertHist', 'abQ' ],
380 [ '1', 'receive' ],
381 [ '1', 'assertHist', 'abQ/XYZ!' ],
382 [ '1', 'assert', function ( client ) {
383 const unsubmitted = client.getChangeSince( client.sentLength, false );
384 assert.deepEqual( unsubmitted.getStore( 0 ).hashes, [ 'h123' ], 'h123 is still in the store after receiving our own change' );
385 } ],
386
387 // Client 1 submits its second change
388 [ '1', 'submit' ],
389 [ '1', 'deliver' ],
390 [ 'server', 'assertHist', 'abQXYZ' ],
391
392 // Client 2 catches up, and receives the annotation correctly
393 [ '2', 'receive' ],
394 [ '2', 'receive' ],
395 [ '2', 'receive' ],
396 [ '2', 'receive' ],
397 [ '2', 'assert', function ( client ) {
398 const lastChange = client.getChangeSince( 3, false );
399 assert.deepEqual( lastChange.getStore( 0 ).hashes, [ 'h123' ], 'h123 is in the store on the other side' );
400 } ]
401 ]
402 }
403 ];
404
405 function makeTransaction( doc, data ) {
406 const builder = new ve.dm.TransactionBuilder();
407 if ( data[ 0 ] === 'insert' ) {
408 data = [
409 [ 'pushRetain', data[ 1 ] ],
410 [ 'pushReplacement', doc, data[ 1 ], 0, data[ 2 ] ],
411 [ 'pushRetain', data[ 3 ] ]
412 ];
413 } else if ( data[ 0 ] === 'remove' ) {
414 data = [
415 [ 'pushRetain', data[ 1 ] ],
416 [ 'pushReplacement', doc, data[ 1 ], data[ 2 ], [] ],
417 [ 'pushRetain', data[ 3 ] ]
418 ];
419 }
420 for ( let i = 0; i < data.length; i++ ) {
421 const method = data[ i ].shift();
422 builder[ method ].apply( builder, data[ i ] );
423 }
424 return builder.getTransaction();
425 }
426
427 cases.forEach( ( caseItem ) => {
428 const server = new ve.dm.TestRebaseServer(),
429 clients = {};
430 caseItem.clients.forEach( ( authorId ) => {
431 const client = new ve.dm.TestRebaseClient( server, caseItem.initialData );
432 client.setAuthorId( authorId );
433 clients[ authorId ] = client;
434 // Initialize
435 server.updateDocState( ve.dm.TestRebaseServer.static.fakeDocName, authorId );
436 } );
437
438 caseItem.ops.forEach( ( op, i ) => {
439 if ( op[ 0 ] === 'debugger' ) {
440 // eslint-disable-next-line no-debugger
441 debugger;
442 // eslint-disable-next-line qunit/no-early-return
443 return;
444 }
445
446 const client = clients[ op[ 0 ] ];
447 const action = op[ 1 ];
448 if ( action === 'apply' ) {
449 if ( Array.isArray( op[ 2 ] ) ) {
450 const txs = op[ 2 ].map( makeTransaction.bind( null, client.doc ) );
451 client.applyTransactions( txs );
452 } else {
453 client.applyChange( ve.dm.Change.static.deserialize( op[ 2 ] ) );
454 }
455 } else if ( action === 'assertHist' ) {
456 let summary;
457 if ( op[ 0 ] === 'server' ) {
458 summary = server.getHistorySummary();
459 } else {
460 summary = client.getHistorySummary();
461 }
462 assert.strictEqual( summary, op[ 2 ], caseItem.name + ': ' + ( op[ 3 ] || i ) );
463 } else if ( action === 'submit' ) {
464 client.submitChange();
465 } else if ( action === 'deliver' ) {
466 client.deliverOne();
467 } else if ( action === 'receive' ) {
468 client.receiveOne();
469 } else if ( action === 'assert' ) {
470 op[ 2 ]( client );
471 }
472 } );
473 } );
474 } );