1+ import { of } from 'rxjs' ;
12import { Model } from '../../json-crdt' ;
23import { Value } from '../../reactive-rpc/common/messages/Value' ;
34import { RpcError , RpcErrorCodes } from '../../reactive-rpc/common/rpc/caller' ;
45import { setup } from './setup' ;
6+ import { tick , until } from '../../__tests__/util' ;
57
68describe ( 'blocks.*' , ( ) => {
79 describe ( 'blocks.create' , ( ) => {
@@ -11,7 +13,7 @@ describe('blocks.*', () => {
1113 const { block} = ( await caller . call ( 'blocks.get' , { id : 'my-block' } , { } ) ) . data ;
1214 expect ( block ) . toMatchObject ( {
1315 id : 'my-block' ,
14- seq : 0 ,
16+ seq : - 1 ,
1517 blob : expect . any ( Uint8Array ) ,
1618 created : expect . any ( Number ) ,
1719 updated : expect . any ( Number ) ,
@@ -282,4 +284,74 @@ describe('blocks.*', () => {
282284 expect ( patches [ 2 ] . blob ) . toStrictEqual ( patch3 . toBinary ( ) ) ;
283285 } ) ;
284286 } ) ;
287+
288+ describe ( 'blocks.listen' , ( ) => {
289+ test ( 'can listen for block changes' , async ( ) => {
290+ const { call, caller} = setup ( ) ;
291+ await call ( 'blocks.create' , { id : 'my-block' , patches : [ ] } ) ;
292+ await tick ( 11 ) ;
293+ const emits : any [ ] = [ ] ;
294+ caller . call$ ( 'blocks.listen' , of ( { id : 'my-block' } ) , { } ) . subscribe ( ( data ) => emits . push ( data ) ) ;
295+ const model = Model . withLogicalClock ( ) ;
296+ model . api . root ( {
297+ text : 'Hell' ,
298+ } ) ;
299+ const patch1 = model . api . flush ( ) ;
300+ await tick ( 12 ) ;
301+ expect ( emits . length ) . toBe ( 0 ) ;
302+ await call ( 'blocks.edit' , { id : 'my-block' , patches : [ { seq : 0 , created : Date . now ( ) , blob : patch1 . toBinary ( ) } ] } ) ;
303+ await until ( ( ) => emits . length === 1 ) ;
304+ expect ( emits . length ) . toBe ( 1 ) ;
305+ expect ( emits [ 0 ] . data . patches . length ) . toBe ( 1 ) ;
306+ expect ( emits [ 0 ] . data . patches [ 0 ] . seq ) . toBe ( 0 ) ;
307+ model . api . root ( {
308+ text : 'Hello' ,
309+ } ) ;
310+ const patch2 = model . api . flush ( ) ;
311+ await tick ( 12 ) ;
312+ expect ( emits . length ) . toBe ( 1 ) ;
313+ await call ( 'blocks.edit' , { id : 'my-block' , patches : [ { seq : 1 , created : Date . now ( ) , blob : patch2 . toBinary ( ) } ] } ) ;
314+ await until ( ( ) => emits . length === 2 ) ;
315+ expect ( emits . length ) . toBe ( 2 ) ;
316+ expect ( emits [ 1 ] . data . patches . length ) . toBe ( 1 ) ;
317+ expect ( emits [ 1 ] . data . patches [ 0 ] . seq ) . toBe ( 1 ) ;
318+ } ) ;
319+
320+ test ( 'can subscribe before block is created' , async ( ) => {
321+ const { call, caller} = setup ( ) ;
322+ const emits : any [ ] = [ ] ;
323+ caller . call$ ( 'blocks.listen' , of ( { id : 'my-block' } ) , { } ) . subscribe ( ( data ) => emits . push ( data ) ) ;
324+ const model = Model . withLogicalClock ( ) ;
325+ model . api . root ( {
326+ text : 'Hell' ,
327+ } ) ;
328+ const patch1 = model . api . flush ( ) ;
329+ await tick ( 12 ) ;
330+ expect ( emits . length ) . toBe ( 0 ) ;
331+ await call ( 'blocks.create' , { id : 'my-block' , patches : [
332+ {
333+ seq : 0 ,
334+ created : Date . now ( ) ,
335+ blob : patch1 . toBinary ( ) ,
336+ } ,
337+ ] } ) ;
338+ await until ( ( ) => emits . length === 1 ) ;
339+ expect ( emits . length ) . toBe ( 1 ) ;
340+ expect ( emits [ 0 ] . data . patches . length ) . toBe ( 1 ) ;
341+ expect ( emits [ 0 ] . data . patches [ 0 ] . seq ) . toBe ( 0 ) ;
342+ expect ( emits [ 0 ] . data . patches [ 0 ] . blob ) . toStrictEqual ( patch1 . toBinary ( ) ) ;
343+ } ) ;
344+
345+ test ( 'can receive deletion events' , async ( ) => {
346+ const { call, caller} = setup ( ) ;
347+ const emits : any [ ] = [ ] ;
348+ caller . call$ ( 'blocks.listen' , of ( { id : 'my-block' } ) , { } ) . subscribe ( ( data ) => emits . push ( data ) ) ;
349+ await call ( 'blocks.create' , { id : 'my-block' , patches : [ ] } ) ;
350+ await until ( ( ) => emits . length === 1 ) ;
351+ expect ( emits [ 0 ] . data . block . seq ) . toBe ( - 1 ) ;
352+ await call ( 'blocks.remove' , { id : 'my-block' } ) ;
353+ await until ( ( ) => emits . length === 2 ) ;
354+ expect ( emits [ 1 ] . data . deleted ) . toBe ( true ) ;
355+ } ) ;
356+ } ) ;
285357} ) ;
0 commit comments