@@ -4,6 +4,7 @@ import {RpcErrorCodes} from '../../common/rpc/caller';
44import { tick , until } from 'thingies' ;
55import type { ApiTestSetup } from '../../common/rpc/__tests__/runApiTests' ;
66import type { JsonCrdtTestSetup } from '../../__demos__/json-crdt-server/__tests__/setup' ;
7+ import type { TBlockEvent } from '../../__demos__/json-crdt-server/routes/block/schema' ;
78
89const sid = Math . random ( ) . toString ( 36 ) . slice ( 2 ) ;
910let seq = 0 ;
@@ -232,96 +233,109 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
232233 } ) ;
233234 } ) ;
234235
235- // if (!params.staticOnly) {
236- // describe('block.listen', () => {
237- // test('can listen for block changes', async () => {
238- // const {call, call$, stop} = await setup();
239- // const id = getId();
240- // await call('block.new', {id, patches: []});
241- // await tick(11);
242- // const emits: any[] = [];
243- // call$('block.listen', {id}).subscribe((data) => emits.push(data));
244- // const model = Model.withLogicalClock();
245- // model.api.root({
246- // text: 'Hell',
247- // });
248- // const patch1 = model.api.flush();
249- // await tick(12);
250- // expect(emits.length).toBe(0);
251- // await call('block.upd', {
252- // id,
253- // patches: [{seq: 0, created: Date.now(), blob: patch1.toBinary()}],
254- // });
255- // await until(() => emits.length === 1);
256- // expect(emits.length).toBe(1);
257- // expect(emits[0][0]).toBe('upd');
258- // expect(emits[0][1].patches.length).toBe(1);
259- // expect(emits[0][1].patches[0].seq).toBe(0);
260- // model.api.root({
261- // text: 'Hello',
262- // });
263- // const patch2 = model.api.flush();
264- // await tick(12);
265- // expect(emits.length).toBe(1);
266- // await call('block.upd', {
267- // id,
268- // patches: [{seq: 1, created: Date.now(), blob: patch2.toBinary()}],
269- // });
270- // await until(() => emits.length === 2);
271- // expect(emits.length).toBe(2);
272- // expect(emits[1][0]).toBe('upd');
273- // expect(emits[1][1].patches.length).toBe(1);
274- // expect(emits[1][1].patches[0].seq).toBe(1);
275- // stop();
276- // });
236+ if ( ! params . staticOnly ) {
237+ describe ( 'block.listen' , ( ) => {
238+ test ( 'can listen for block changes' , async ( ) => {
239+ const { call, call$, stop} = await setup ( ) ;
240+ const id = getId ( ) ;
241+ await call ( 'block.new' , { id, patches : [ ] } ) ;
242+ await tick ( 11 ) ;
243+ const emits : TBlockEvent [ ] = [ ] ;
244+ call$ ( 'block.listen' , { id} ) . subscribe ( ( { event} ) => emits . push ( event ) ) ;
245+ const model = Model . withLogicalClock ( ) ;
246+ model . api . root ( {
247+ text : 'Hell' ,
248+ } ) ;
249+ const patch1 = model . api . flush ( ) ;
250+ await tick ( 12 ) ;
251+ expect ( emits . length ) . toBe ( 0 ) ;
252+ await call ( 'block.upd' , {
253+ id,
254+ patches : [ { blob : patch1 . toBinary ( ) } ] ,
255+ } ) ;
256+ await until ( ( ) => emits . length === 1 ) ;
257+ expect ( emits . length ) . toBe ( 1 ) ;
258+ expect ( emits [ 0 ] [ 0 ] ) . toBe ( 'upd' ) ;
259+ if ( emits [ 0 ] [ 0 ] === 'upd' ) {
260+ expect ( emits [ 0 ] [ 1 ] . patches . length ) . toBe ( 1 ) ;
261+ expect ( emits [ 0 ] [ 1 ] . patches [ 0 ] ) . toMatchObject ( {
262+ ts : expect . any ( Number ) ,
263+ blob : patch1 . toBinary ( ) ,
264+ } ) ;
265+ }
266+ model . api . root ( {
267+ text : 'Hello' ,
268+ } ) ;
269+ const patch2 = model . api . flush ( ) ;
270+ await tick ( 12 ) ;
271+ expect ( emits . length ) . toBe ( 1 ) ;
272+ await call ( 'block.upd' , {
273+ id,
274+ patches : [ { blob : patch2 . toBinary ( ) } ] ,
275+ } ) ;
276+ await until ( ( ) => emits . length === 2 ) ;
277+ expect ( emits . length ) . toBe ( 2 ) ;
278+ expect ( emits [ 1 ] [ 0 ] ) . toBe ( 'upd' ) ;
279+ if ( emits [ 1 ] [ 0 ] === 'upd' ) {
280+ expect ( emits [ 1 ] [ 1 ] . patches . length ) . toBe ( 1 ) ;
281+ expect ( emits [ 1 ] [ 1 ] . patches [ 0 ] ) . toMatchObject ( {
282+ ts : expect . any ( Number ) ,
283+ blob : patch2 . toBinary ( ) ,
284+ } ) ;
285+ }
286+ stop ( ) ;
287+ } ) ;
277288
278- // test('can subscribe before block is created', async () => {
279- // const {call, call$, stop} = await setup();
280- // const emits: any[] = [];
281- // const id = getId();
282- // call$('block.listen', {id}).subscribe((data) => emits.push(data));
283- // const model = Model.withLogicalClock();
284- // model.api.root({
285- // text: 'Hell',
286- // });
287- // const patch1 = model.api.flush();
288- // await tick(12);
289- // expect(emits.length).toBe(0);
290- // await call('block.new', {
291- // id,
292- // patches: [
293- // {
294- // blob: patch1.toBinary(),
295- // },
296- // ],
297- // });
298- // await until(() => emits.length === 1);
299- // expect(emits.length).toBe(1);
300- // expect(emits[0][0]).toBe('upd');
301- // expect(emits[0][1].patches.length).toBe(1);
302- // expect(emits[0][1].patches[0].seq).toBe(0);
303- // expect(emits[0][1].patches[0].blob).toStrictEqual(patch1.toBinary());
304- // stop();
305- // });
289+ test ( 'can subscribe before block is created' , async ( ) => {
290+ const { call, call$, stop} = await setup ( ) ;
291+ const emits : TBlockEvent [ ] = [ ] ;
292+ const id = getId ( ) ;
293+ call$ ( 'block.listen' , { id} ) . subscribe ( ( { event} ) => emits . push ( event ) ) ;
294+ const model = Model . withLogicalClock ( ) ;
295+ model . api . root ( {
296+ text : 'Hell' ,
297+ } ) ;
298+ const patch1 = model . api . flush ( ) ;
299+ await tick ( 12 ) ;
300+ expect ( emits . length ) . toBe ( 0 ) ;
301+ await call ( 'block.new' , {
302+ id,
303+ patches : [
304+ {
305+ blob : patch1 . toBinary ( ) ,
306+ } ,
307+ ] ,
308+ } ) ;
309+ await until ( ( ) => emits . length === 1 ) ;
310+ expect ( emits . length ) . toBe ( 1 ) ;
311+ expect ( emits [ 0 ] [ 0 ] ) . toBe ( 'upd' ) ;
312+ if ( emits [ 0 ] [ 0 ] === 'upd' ) {
313+ expect ( emits [ 0 ] [ 1 ] . patches . length ) . toBe ( 1 ) ;
314+ expect ( emits [ 0 ] [ 1 ] . patches [ 0 ] ) . toMatchObject ( {
315+ ts : expect . any ( Number ) ,
316+ blob : patch1 . toBinary ( ) ,
317+ } ) ;
318+ }
319+ stop ( ) ;
320+ } ) ;
306321
307- // test('can receive deletion events', async () => {
308- // const {call, call$, stop} = await setup();
309- // const emits: any[] = [];
310- // const id = getId();
311- // call$('block.listen', {id}).subscribe((data) => {
312- // emits.push(data);
313- // });
314- // await call('block.new', {id, patches: []});
315- // await until(() => emits.length === 1);
316- // expect(emits[0][1].model.seq).toBe(-1);
317- // await tick(3);
318- // await call('block.del', {id});
319- // await until(() => emits.length === 2);
320- // expect(emits[1][0]).toBe('del');
321- // stop();
322- // });
323- // });
324- // }
322+ test ( 'can receive deletion events' , async ( ) => {
323+ const { call, call$, stop} = await setup ( ) ;
324+ const emits : TBlockEvent [ ] = [ ] ;
325+ const id = getId ( ) ;
326+ call$ ( 'block.listen' , { id} ) . subscribe ( ( { event} ) => {
327+ emits . push ( event ) ;
328+ } ) ;
329+ await call ( 'block.new' , { id, patches : [ ] } ) ;
330+ await until ( ( ) => emits . length === 1 ) ;
331+ await tick ( 3 ) ;
332+ await call ( 'block.del' , { id} ) ;
333+ await until ( ( ) => emits . length === 2 ) ;
334+ expect ( emits [ 1 ] [ 0 ] ) . toBe ( 'del' ) ;
335+ stop ( ) ;
336+ } ) ;
337+ } ) ;
338+ }
325339
326340 describe ( 'block.scan' , ( ) => {
327341 test ( 'can retrieve change history' , async ( ) => {
0 commit comments