import { MongoAdapter } from '.' ; import { newid } from '@localfirst/relay-client' ; const pause = ( t = 100 ) => new Promise < void > ( yes => setTimeout ( ( ) => yes ( ) , t ) ) ; describe ( 'MongoAdapter' , ( ) => { const setup = async ( ) => { const storage = new MongoAdapter ( { discoveryKey : 'sly-mongoose' , databaseName : `test-${ newid ( ) }` , } ) ; await pause ( ) ; return { storage } ; } ; afterEach ( ( ) => { } ) ; describe ( 'open & close' , ( ) => { test ( `doesn't crash` , async ( ) => { const { storage } = await setup ( ) ; await storage . open ( ) ; await pause ( ) ; await storage . close ( ) ; } ) ; } ) ; describe ( 'snapshots' , ( ) => { test ( 'putSnapshot' , async ( ) => { const { storage } = await setup ( ) ; await storage . open ( ) ; const documentId = '123' ; const snapshot = { foo : 42 } ; const clock = { local : 1 } ; await storage . putSnapshot ( { documentId , snapshot , clock } ) ; for await ( const s of storage . snapshots ( ) ) expect ( s ) . toEqual ( { documentId , snapshot , clock } ) ; await storage . close ( ) ; } ) ; test ( 'deleteSnapshot' , async ( ) => { const { storage } = await setup ( ) ; await storage . open ( ) ; const documentId = '123' ; const snapshot = { foo : 42 } ; const clock = { local : 1 } ; await storage . putSnapshot ( { documentId , snapshot , clock } ) ; await storage . deleteSnapshot ( documentId ) ; let count = 0 ; for await ( const cursor of storage . snapshots ( ) ) count ++ ; expect ( count ) . toBe ( 0 ) ; await storage . close ( ) ; } ) ; } ) ; describe ( 'changes' , ( ) => { test ( 'changes' , async ( ) => { const { storage } = await setup ( ) ; await storage . open ( ) ; const changeSet = { documentId : '123' , changes : [ ] } ; await storage . appendChanges ( changeSet ) ; for await ( const c of storage . changes ( ) ) expect ( c ) . toEqual ( expect . objectContaining ( changeSet ) ) ; await storage . close ( ) ; } ) ; test ( 'getChanges' , async ( ) => { const { storage } = await setup ( ) ; await storage . open ( ) ; const changeSet = { documentId : '123' , changes : [ ] } ; await storage . appendChanges ( changeSet ) ; expect ( await storage . getChanges ( '123' ) ) . toEqual ( [ expect . objectContaining ( changeSet ) ] ) ; await storage . close ( ) ; } ) ; } ) ; describe ( 'hasData' , ( ) => { test ( 'returns false on a newly created adapter' , async ( ) => { const { storage } = await setup ( ) ; await storage . open ( ) ; expect ( await storage . hasData ( ) ) . toBe ( false ) ; await storage . close ( ) ; } ) ; test ( 'returns true after a document is added' , async ( ) => { const { storage } = await setup ( ) ; await storage . open ( ) ; const changeSet = { documentId : '123' , changes : [ ] } ; await storage . appendChanges ( changeSet ) ; expect ( await storage . hasData ( ) ) . toBe ( true ) ; await storage . close ( ) ; } ) ; } ) ; } ) ;