import { strict as assert } from "assert" ; import { LocalClientId , UnassignedSequenceNumber , UniversalSequenceNumber } from "../constants" ; import { MergeTree } from "../mergeTree" ; import { MergeTreeMaintenanceType } from "../mergeTreeDeltaCallback" ; import { MergeTreeDeltaType , ReferenceType , } from "../ops" ; import { TextSegment } from "../textSegment" ; import { countOperations , insertMarker , insertText } from "./testUtils" ; describe ( "MergeTree" , ( ) => { let mergeTree ; const localClientId = 17 ; let currentSequenceNumber ; beforeEach ( ( ) => { mergeTree = new MergeTree ( ) ; mergeTree . insertSegments ( 0 , [ TextSegment . make ( "hello world!" ) ] , UniversalSequenceNumber , LocalClientId , UniversalSequenceNumber , undefined ) ; currentSequenceNumber = 0 ; mergeTree . startCollaboration ( localClientId , currentSequenceNumber , currentSequenceNumber ) ; } ) ; describe ( "insertText" , ( ) => { it ( "Insert starting text" , ( ) => { let eventCalled = 0 ; mergeTree . mergeTreeDeltaCallback = ( opArgs , deltaArgs ) => { eventCalled ++ ; } ; insertText ( mergeTree , 0 , currentSequenceNumber , localClientId , UnassignedSequenceNumber , "more " , undefined , { op : { type : MergeTreeDeltaType . INSERT } } ) ; assert . equal ( eventCalled , 1 ) ; } ) ; it ( "Insert ending text" , ( ) => { const textLength = mergeTree . getLength ( currentSequenceNumber , localClientId ) ; let eventCalled = 0 ; mergeTree . mergeTreeDeltaCallback = ( opArgs , deltaArgs ) => { eventCalled ++ ; } ; insertText ( mergeTree , textLength , currentSequenceNumber , localClientId , UnassignedSequenceNumber , "more " , undefined , { op : { type : MergeTreeDeltaType . INSERT } } ) ; assert . equal ( eventCalled , 1 ) ; } ) ; it ( "Insert middle text" , ( ) => { const count = countOperations ( mergeTree ) ; insertText ( mergeTree , 4 , currentSequenceNumber , localClientId , UnassignedSequenceNumber , "more " , undefined , { op : { type : MergeTreeDeltaType . INSERT } } ) ; assert . deepStrictEqual ( count , { [ MergeTreeDeltaType . INSERT ] : 1 , [ MergeTreeMaintenanceType . SPLIT ] : 1 } ) ; } ) ; it ( "Insert text remote" , ( ) => { const remoteClientId = 35 ; let remoteSequenceNumber = currentSequenceNumber ; const count = countOperations ( mergeTree ) ; insertText ( mergeTree , 0 , currentSequenceNumber , remoteClientId , ++ remoteSequenceNumber , "more " , undefined , { op : { type : MergeTreeDeltaType . INSERT } } ) ; assert . deepStrictEqual ( count , { [ MergeTreeDeltaType . INSERT ] : 1 } ) ; } ) ; } ) ; describe ( "insertMarker" , ( ) => { it ( "Insert marker" , ( ) => { const count = countOperations ( mergeTree ) ; insertMarker ( mergeTree , 4 , currentSequenceNumber , localClientId , UnassignedSequenceNumber , ReferenceType . Simple , undefined , { op : { type : MergeTreeDeltaType . INSERT } } ) ; assert . deepStrictEqual ( count , { [ MergeTreeDeltaType . INSERT ] : 1 , [ MergeTreeMaintenanceType . SPLIT ] : 1 } ) ; } ) ; } ) ; } ) ;