Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel transport server tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.dm.TransportServer' );
8
9 QUnit.test( 'Create', ( assert ) => {
10 const done = assert.async(),
11 log = [],
12 io = ve.dm.FakeSocket.static.makeServer();
13
14 const protocolServer = {
15 context: null,
16 ensureLoaded: Promise.resolve.bind( Promise ),
17 authenticate: function ( docName, authorId, token ) {
18 const context = { docName: docName, authorId: authorId, token: token };
19 log.push( [ 'authenticate', ve.copy( context ) ] );
20 return context;
21 },
22 onSubmitChange: function ( context, data ) {
23 log.push( [ 'submitChange', data ] );
24 context.broadcast( 'newChange', data );
25 },
26 onLogEvent: function ( context, event ) {
27 log.push( [ 'logEvent', event ] );
28 },
29 onChangeAuthor: function ( context, newData ) {
30 log.push( [ 'changeAuthor', newData ] );
31 context.broadcast( 'authorChange', newData );
32 },
33 onDisconnect: function ( context ) {
34 log.push( [ 'disconnect' ] );
35 context.broadcast( 'authorDisconnect', context.authorId );
36 },
37 welcomeClient: function ( context ) {
38 this.context = context;
39 context.sendAuthor( 'registered', context.authorId );
40 context.broadcast( 'authorChange', context.authorId );
41 context.sendAuthor( 'initDoc', 'fakedoccontents' );
42 }
43 };
44
45 const transportServer = new ve.dm.TransportServer( protocolServer, protocolServer );
46 const socket = new ve.dm.FakeSocket( io, { docName: 'Foo', authorId: 1, token: 'xxx' } );
47 socket.on( 'registered', ( data ) => {
48 log.push( [ 'registered', data ] );
49 } );
50 socket.on( 'authorChange', ( data ) => {
51 log.push( [ 'broadcast', 'authorChange', data ] );
52 } );
53 socket.on( 'initDoc', ( data ) => {
54 log.push( [ 'initDoc', data ] );
55 } );
56 socket.on( 'newChange', ( data ) => {
57 log.push( [ 'broadcast', 'newChange', data ] );
58 } );
59 socket.on( 'authorDisconnect', ( authorId ) => {
60 log.push( [ 'broadcast', 'authorDisconnect', authorId ] );
61 } );
62
63 transportServer.onConnection( io.sockets.in.bind( io.sockets ), socket ).then( () => {
64 socket.receive( 'submitChange', 'foo' );
65 socket.receive( 'logEvent', 'bar' );
66 socket.receive( 'changeAuthor', 1 );
67 socket.receive( 'disconnect' );
68 return socket.wait();
69 } ).then( () => {
70 assert.deepEqual( log, [
71 [ 'authenticate', { docName: 'Foo', authorId: 1, token: 'xxx' } ],
72 [ 'registered', 1 ],
73 [ 'broadcast', 'authorChange', 1 ],
74 [ 'initDoc', 'fakedoccontents' ],
75 [ 'submitChange', 'foo' ],
76 [ 'broadcast', 'newChange', 'foo' ],
77 [ 'logEvent', 'bar' ],
78 [ 'changeAuthor', 1 ],
79 [ 'broadcast', 'authorChange', 1 ],
80 [ 'disconnect' ],
81 [ 'broadcast', 'authorDisconnect', 1 ]
82 ], 'Correct events received in correct order' );
83 } ).catch( ( err ) => {
84 assert.true( false, 'Exception: ' + err );
85 } ).finally( () => done() );
86 } );