11/* eslint-disable @typescript-eslint/no-magic-numbers */
22import { createDataKey , IndexBuilder } from '@editorjs/model' ;
33import { EditorJSModel } from '@editorjs/model' ;
4+ import type { CoreConfig } from '@editorjs/sdk' ;
45import { beforeAll , jest } from '@jest/globals' ;
56import { CollaborationManager } from './CollaborationManager.js' ;
67import { Operation , OperationType } from './Operation.js' ;
78
9+ const config : Partial < CoreConfig > = { userId : 'user' } ;
10+
811describe ( 'CollaborationManager' , ( ) => {
12+ beforeAll ( ( ) => {
13+ jest . useFakeTimers ( ) ;
14+ } ) ;
15+
916 describe ( 'applyOperation' , ( ) => {
1017 it ( 'should throw an error on unknown operation type' , ( ) => {
1118 const model = new EditorJSModel ( ) ;
1219
13- const collaborationManager = new CollaborationManager ( model ) ;
20+ const collaborationManager = new CollaborationManager ( config , model ) ;
1421
1522 // @ts -expect-error - for test purposes
1623 expect ( ( ) => collaborationManager . applyOperation ( new Operation ( 'unknown' , new IndexBuilder ( ) . build ( ) , 'hello' ) ) ) . toThrow ( 'Unknown operation type' ) ;
@@ -30,7 +37,7 @@ describe('CollaborationManager', () => {
3037 } ,
3138 } ] ,
3239 } ) ;
33- const collaborationManager = new CollaborationManager ( model ) ;
40+ const collaborationManager = new CollaborationManager ( config , model ) ;
3441 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
3542 . addDataKey ( createDataKey ( 'text' ) )
3643 . addTextRange ( [ 0 , 4 ] )
@@ -70,7 +77,7 @@ describe('CollaborationManager', () => {
7077 } ,
7178 } ] ,
7279 } ) ;
73- const collaborationManager = new CollaborationManager ( model ) ;
80+ const collaborationManager = new CollaborationManager ( config , model ) ;
7481 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
7582 . addDataKey ( createDataKey ( 'text' ) )
7683 . addTextRange ( [
@@ -103,7 +110,7 @@ describe('CollaborationManager', () => {
103110 model . initializeDocument ( {
104111 blocks : [ ] ,
105112 } ) ;
106- const collaborationManager = new CollaborationManager ( model ) ;
113+ const collaborationManager = new CollaborationManager ( config , model ) ;
107114 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
108115 . build ( ) ;
109116 const operation = new Operation ( OperationType . Insert , index , {
@@ -150,7 +157,7 @@ describe('CollaborationManager', () => {
150157 model . initializeDocument ( {
151158 blocks : [ block ] ,
152159 } ) ;
153- const collaborationManager = new CollaborationManager ( model ) ;
160+ const collaborationManager = new CollaborationManager ( config , model ) ;
154161 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
155162 . build ( ) ;
156163 const operation = new Operation ( OperationType . Delete , index , {
@@ -178,7 +185,7 @@ describe('CollaborationManager', () => {
178185 } ,
179186 } ] ,
180187 } ) ;
181- const collaborationManager = new CollaborationManager ( model ) ;
188+ const collaborationManager = new CollaborationManager ( config , model ) ;
182189 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
183190 . addDataKey ( createDataKey ( 'text' ) )
184191 . addTextRange ( [ 0 , 5 ] )
@@ -228,7 +235,7 @@ describe('CollaborationManager', () => {
228235 } ,
229236 } ] ,
230237 } ) ;
231- const collaborationManager = new CollaborationManager ( model ) ;
238+ const collaborationManager = new CollaborationManager ( config , model ) ;
232239 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
233240 . addDataKey ( createDataKey ( 'text' ) )
234241 . addTextRange ( [ 0 , 3 ] )
@@ -280,7 +287,7 @@ describe('CollaborationManager', () => {
280287 } ,
281288 } ] ,
282289 } ) ;
283- const collaborationManager = new CollaborationManager ( model ) ;
290+ const collaborationManager = new CollaborationManager ( config , model ) ;
284291 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
285292 . addDataKey ( createDataKey ( 'text' ) )
286293 . addTextRange ( [ 0 , 4 ] )
@@ -321,7 +328,7 @@ describe('CollaborationManager', () => {
321328 } ,
322329 } ] ,
323330 } ) ;
324- const collaborationManager = new CollaborationManager ( model ) ;
331+ const collaborationManager = new CollaborationManager ( config , model ) ;
325332 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
326333 . addDataKey ( createDataKey ( 'text' ) )
327334 . addTextRange ( [
@@ -363,7 +370,7 @@ describe('CollaborationManager', () => {
363370 } ,
364371 } ] ,
365372 } ) ;
366- const collaborationManager = new CollaborationManager ( model ) ;
373+ const collaborationManager = new CollaborationManager ( config , model ) ;
367374 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
368375 . addDataKey ( createDataKey ( 'text' ) )
369376 . addTextRange ( [ 0 , 4 ] )
@@ -405,7 +412,7 @@ describe('CollaborationManager', () => {
405412 } ,
406413 } ] ,
407414 } ) ;
408- const collaborationManager = new CollaborationManager ( model ) ;
415+ const collaborationManager = new CollaborationManager ( config , model ) ;
409416 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
410417 . addDataKey ( createDataKey ( 'text' ) )
411418 . addTextRange ( [ 0 , 4 ] )
@@ -440,7 +447,7 @@ describe('CollaborationManager', () => {
440447 model . initializeDocument ( {
441448 blocks : [ ] ,
442449 } ) ;
443- const collaborationManager = new CollaborationManager ( model ) ;
450+ const collaborationManager = new CollaborationManager ( config , model ) ;
444451 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
445452 . build ( ) ;
446453 const operation = new Operation ( OperationType . Insert , index , {
@@ -479,7 +486,7 @@ describe('CollaborationManager', () => {
479486 } ,
480487 } ] ,
481488 } ) ;
482- const collaborationManager = new CollaborationManager ( model ) ;
489+ const collaborationManager = new CollaborationManager ( config , model ) ;
483490 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
484491 . addDataKey ( createDataKey ( 'text' ) )
485492 . addTextRange ( [ 0 , 5 ] )
@@ -528,7 +535,7 @@ describe('CollaborationManager', () => {
528535 } ,
529536 } ] ,
530537 } ) ;
531- const collaborationManager = new CollaborationManager ( model ) ;
538+ const collaborationManager = new CollaborationManager ( config , model ) ;
532539 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
533540 . addDataKey ( createDataKey ( 'text' ) )
534541 . addTextRange ( [ 0 , 3 ] )
@@ -580,7 +587,7 @@ describe('CollaborationManager', () => {
580587 } ,
581588 } ] ,
582589 } ) ;
583- const collaborationManager = new CollaborationManager ( model ) ;
590+ const collaborationManager = new CollaborationManager ( config , model ) ;
584591 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
585592 . addDataKey ( createDataKey ( 'text' ) )
586593 . addTextRange ( [ 0 , 3 ] )
@@ -632,7 +639,7 @@ describe('CollaborationManager', () => {
632639 model . initializeDocument ( {
633640 blocks : [ block ] ,
634641 } ) ;
635- const collaborationManager = new CollaborationManager ( model ) ;
642+ const collaborationManager = new CollaborationManager ( config , model ) ;
636643 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
637644 . build ( ) ;
638645 const operation = new Operation ( OperationType . Delete , index , {
@@ -667,7 +674,7 @@ describe('CollaborationManager', () => {
667674 model . initializeDocument ( {
668675 blocks : [ block ] ,
669676 } ) ;
670- const collaborationManager = new CollaborationManager ( model ) ;
677+ const collaborationManager = new CollaborationManager ( config , model ) ;
671678 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
672679 . build ( ) ;
673680 const operation = new Operation ( OperationType . Delete , index , {
@@ -705,7 +712,7 @@ describe('CollaborationManager', () => {
705712 model . initializeDocument ( {
706713 blocks : [ block ] ,
707714 } ) ;
708- const collaborationManager = new CollaborationManager ( model ) ;
715+ const collaborationManager = new CollaborationManager ( config , model ) ;
709716 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
710717 . build ( ) ;
711718 const operation = new Operation ( OperationType . Delete , index , {
@@ -747,7 +754,7 @@ describe('CollaborationManager', () => {
747754 model . initializeDocument ( {
748755 blocks : [ block ] ,
749756 } ) ;
750- const collaborationManager = new CollaborationManager ( model ) ;
757+ const collaborationManager = new CollaborationManager ( config , model ) ;
751758 const index = new IndexBuilder ( ) . addBlockIndex ( 0 )
752759 . build ( ) ;
753760 const operation = new Operation ( OperationType . Delete , index , {
@@ -787,7 +794,7 @@ describe('CollaborationManager', () => {
787794 } ,
788795 } ] ,
789796 } ) ;
790- const collaborationManager = new CollaborationManager ( model ) ;
797+ const collaborationManager = new CollaborationManager ( config , model ) ;
791798 const index1 = new IndexBuilder ( ) . addBlockIndex ( 0 )
792799 . addDataKey ( createDataKey ( 'text' ) )
793800 . addTextRange ( [ 0 , 0 ] )
@@ -838,7 +845,7 @@ describe('CollaborationManager', () => {
838845 } ,
839846 } ] ,
840847 } ) ;
841- const collaborationManager = new CollaborationManager ( model ) ;
848+ const collaborationManager = new CollaborationManager ( config , model ) ;
842849 const index1 = new IndexBuilder ( ) . addBlockIndex ( 0 )
843850 . addDataKey ( createDataKey ( 'text' ) )
844851 . addTextRange ( [ 0 , 0 ] )
@@ -875,4 +882,40 @@ describe('CollaborationManager', () => {
875882 properties : { } ,
876883 } ) ;
877884 } ) ;
885+
886+ it ( 'should not undo operations from not a current user' , ( ) => {
887+ const model = new EditorJSModel ( ) ;
888+
889+ model . initializeDocument ( {
890+ blocks : [ {
891+ name : 'paragraph' ,
892+ data : {
893+ text : {
894+ value : '' ,
895+ $t : 't' ,
896+ } ,
897+ } ,
898+ } ] ,
899+ } ) ;
900+ const collaborationManager = new CollaborationManager ( config , model ) ;
901+
902+ model . insertText ( 'another-user' , 0 , createDataKey ( 'text' ) , 'hello' , 0 ) ;
903+
904+ collaborationManager . undo ( ) ;
905+
906+ expect ( model . serialized ) . toStrictEqual ( {
907+ blocks : [ {
908+ name : 'paragraph' ,
909+ tunes : { } ,
910+ data : {
911+ text : {
912+ $t : 't' ,
913+ value : 'hello' ,
914+ fragments : [ ] ,
915+ } ,
916+ } ,
917+ } ] ,
918+ properties : { } ,
919+ } ) ;
920+ } ) ;
878921} ) ;
0 commit comments