Debian Codegraph @

extensions/VisualEditor/lib/ve/rebaser/src/logToTestCase.js

raw symbols SCIP
1 'use strict';
2
3 // eslint-disable-next-line n/no-missing-require
4 const ve = require( '../dist/ve-rebaser.js' ),
5 fs = require( 'fs' );
6
7 /**
8 * Parse log file contents.
9 *
10 * @ignore
11 * @param {string} log Newline-separated list of JSON objects
12 * @return {Object[]} Array of parsed objects
13 */
14 function parseLog( log ) {
15 const result = [],
16 lines = log.split( '\n' );
17 for ( let i = 0; i < lines.length; i++ ) {
18 if ( lines[ i ] === '' ) {
19 continue;
20 }
21 try {
22 result.push( JSON.parse( lines[ i ] ) );
23 } catch ( e ) {
24 console.warn( e, lines[ i ] );
25 }
26 }
27 return result;
28 }
29
30 function toTestCase( parsedLog ) {
31 const clients = [],
32 ops = [],
33 clientStates = {};
34 for ( let i = 0; i < parsedLog.length; i++ ) {
35 const type = parsedLog[ i ].type;
36 const authorId = parsedLog[ i ].authorId;
37 const clientId = parsedLog[ i ].clientId;
38 if ( type === 'newClient' ) {
39 clients.push( authorId );
40 clientStates[ authorId ] = {
41 unsent: 0,
42 submitting: false
43 };
44 } else if ( type === 'applyChange' ) {
45 if ( clientStates[ authorId ].submitting ) {
46 ops.push( [ authorId, 'deliver' ] );
47 clientStates[ authorId ].submitting = false;
48 }
49 } else if ( type === 'acceptChange' ) {
50 const unsent = ve.dm.Change.static.deserialize( parsedLog[ i ].unsent, true );
51 const newChanges = unsent.mostRecent( unsent.start + clientStates[ clientId ].unsent );
52 // HACK: Deliberately using .getLength() > 0 instead of .isEmpty() to ignore selection-only changes
53 if ( newChanges.getLength() > 0 ) {
54 ops.push( [ clientId, 'apply', newChanges.serialize( true ) ] );
55 clientStates[ clientId ].unsent = unsent.getLength();
56 }
57
58 // .change is not a Change, but an array [start, length]
59 if ( parsedLog[ i ].change[ 1 ] > 0 ) {
60 ops.push( [ clientId, 'receive' ] );
61 }
62 } else if ( type === 'submitChange' ) {
63 const changes = ve.dm.Change.static.deserialize( parsedLog[ i ].change, true );
64 const newChanges = changes.mostRecent( changes.start + clientStates[ clientId ].unsent );
65 if ( newChanges.getLength() > 0 ) {
66 ops.push( [ clientId, 'apply', newChanges.serialize( true ) ] );
67 }
68
69 if ( clientStates[ clientId ].unsent + newChanges.getLength() > 0 ) {
70 ops.push( [ clientId, 'submit' ] );
71 clientStates[ clientId ].unsent = 0;
72 clientStates[ clientId ].submitting = true;
73 }
74 }
75 }
76 return {
77 initialData: [],
78 clients: clients,
79 ops: ops
80 };
81 }
82
83 // eslint-disable-next-line security/detect-non-literal-fs-filename
84 fs.readFile( process.argv[ 2 ], { encoding: 'utf8' }, ( err, data ) => {
85 const parsed = parseLog( data ),
86 testCase = toTestCase( parsed );
87 process.stdout.write( JSON.stringify( testCase ) );
88 } );
89
90 // acceptChange
91 // submitChange
92 // applyChange
93 // newClient
94 // disconnect