22import Channel from '../../src/simperium/channel'
33import util from 'util'
44import { parseMessage } from '../../src/simperium/util'
5- import assert , { equal } from 'assert'
5+ import assert , { equal , ok } from 'assert'
66import * as fn from './fn'
77import jsondiff from '../../src/simperium/jsondiff'
88import defaultGhostStoreProvider from '../../src/simperium/ghost/default'
@@ -12,6 +12,11 @@ import mockBucketStore from './mock_bucket_store'
1212
1313const differ = jsondiff ( )
1414const diff = differ . object_diff . bind ( differ )
15+ const cycle = ( ... fns ) => ( ... args ) => {
16+ const [ head , ... rest ] = fns
17+ head ( ... args )
18+ fns = rest . concat ( head )
19+ }
1520
1621describe ( 'Channel' , function ( ) {
1722 var channel , bucket , store ;
@@ -158,6 +163,21 @@ describe( 'Channel', function() {
158163 bucket . update ( 'mock-id' , data ) ;
159164 } ) ;
160165
166+ it ( 'should resend sent but unacknowledged changes on reconnect' , ( ) => new Promise ( resolve => {
167+ channel . localQueue . sent [ 'fake-ccid' ] = { fake : 'change' }
168+
169+ channel . on ( 'send' , cycle (
170+ ( ) => channel . handleMessage ( 'i:{"index":[],"current":"cv"}' ) ,
171+ m => {
172+ resolve ( )
173+ }
174+ ) )
175+
176+ channel . handleMessage ( 'auth:user@example.com' )
177+
178+ channel . emit ( 'ready' )
179+ } ) )
180+
161181 it ( 'should send remove operation' , function ( done ) {
162182 channel . on ( 'send' , function ( msg ) {
163183 var message = parseMessage ( msg ) ,
@@ -221,6 +241,11 @@ describe( 'Channel', function() {
221241 channel . handleMessage ( 'c:' + JSON . stringify ( [ change ] ) ) ;
222242 } ) ;
223243
244+ it ( 'should emit ready after receiving changes' , ( done ) => {
245+ channel . on ( 'ready' , ( ) => done ( ) )
246+ channel . handleMessage ( 'c:[]' ) ;
247+ } )
248+
224249 it ( 'should notify bucket after network deletion' , function ( done ) {
225250 var key = 'deleteTest' ;
226251
@@ -366,12 +391,10 @@ describe( 'Channel', function() {
366391 } ) ;
367392
368393 describe ( 'after authorizing' , function ( ) {
369- beforeEach ( function ( next ) {
370- channel . once ( 'send' , function ( ) {
371- next ( ) ;
372- } ) ;
394+ beforeEach ( ( ) => new Promise ( resolve => {
395+ channel . once ( 'send' , ( ) => resolve ( ) ) ;
373396 channel . onConnect ( ) ;
374- } ) ;
397+ } ) ) ;
375398
376399 it ( 'should request index' , function ( done ) {
377400 channel . once ( 'send' , function ( data ) {
@@ -402,17 +425,20 @@ describe( 'Channel', function() {
402425 } ) ;
403426 } ) ;
404427
405- it ( 'should emit index event when index complete' , function ( done ) {
428+ it ( 'should emit index and ready event when index complete' , ( ) => new Promise ( resolve => {
406429 var page = 'i:{"index":[{"id":"objectid","v":1,"d":{"title":"Hello World"}}],"current":"cv"}' ;
430+ let indexed = false
407431 channel . on ( 'index' , function ( cv ) {
408- setImmediate ( function ( ) {
409- assert . equal ( 'cv' , cv ) ;
410- assert ( ! bucket . isIndexing ) ;
411- done ( ) ;
412- } )
432+ assert . equal ( 'cv' , cv ) ;
433+ assert ( ! bucket . isIndexing ) ;
434+ indexed = true
413435 } ) ;
436+ channel . on ( 'ready' , ( ) => {
437+ ok ( indexed )
438+ resolve ( )
439+ } )
414440 channel . handleMessage ( page ) ;
415- } ) ;
441+ } ) ) ;
416442
417443 it ( 'should request next index page' , function ( done ) {
418444 var page = 'i:{"index":[{"id":"objectid","v":1,"d":{"title":"Hello World"}}],"mark":"next-mark","current":"cv"}' ;
0 commit comments