import { PrivateKey } from '@textile/crypto' ; import ThreadID from '@textile/threads-id' ; import { expect } from 'chai' ; import { Database } from '../local/db' ; import { personSchema , shouldHaveThrown } from '../utils/spec.utils' ; import { createDbClient } from './grpc' ; import { Errors } from './index' ; const databaseName = 'remote-db' ; const serviceHost = 'http://localhost:6007' ; describe ( 'ThreadDB' , function ( ) { context ( 'remote + db' , function ( ) { const privateKey = PrivateKey . fromRandom ( ) ; context ( 'schemas and pushing to remote' , async function ( ) { this . timeout ( 30000 ) ; let db ; let id ; after ( async function ( ) { db . close ( ) ; await db . delete ( ) ; } ) ; /**
             * Function to create a set of pre-defined udpates/changes
             */ async function createChanges ( ) { const collection = db . collection ( 'Person' ) ; if ( collection === undefined ) throw new Error ( 'should be defined' ) ; await collection . insert ( { name : 'child' , age : 4 } ) ; await collection . insert ( { name : 'kid' , age : 8 } ) ; await collection . insert ( { name : 'teen' , age : 16 } ) ; } it ( 'should be able to push local schemas to remote on push' , async function ( ) { this . timeout ( 30000 ) ; db = new Database ( databaseName ) ; db . remote . set ( { serviceHost } ) ; const token = await db . remote . authorize ( privateKey ) ; await db . open ( 1 ) ; id = await db . remote . initialize ( ) ; const client = createDbClient ( db . remote . config ) ; try { await client . getCollectionInfo ( ThreadID . fromString ( id ! ) , 'Person' ) ; throw shouldHaveThrown ; } catch ( err ) { expect ( err . toString ( ) ) . to . include ( 'collection not found' ) ; } db . close ( ) ; db = new Database ( databaseName , { name : 'Person' , schema : personSchema , indexes : [ { path : 'name' , unique : true } , { path : 'age' , unique : false } , ] , } ) ; db . remote . set ( { serviceHost , id , token } ) ; await db . open ( 2 ) ; id = await db . remote . initialize ( ) ; await db . remote . push ( 'Person' ) ; const info = await client . getCollectionInfo ( ThreadID . fromString ( db . id ! ) , 'Person' ) ; expect ( info . schema ) . to . deep . equal ( personSchema ) ; db . close ( ) ; } ) ; it ( 'should be able to push local schemas to remote on init' , async function ( ) { db = new Database ( databaseName , { name : 'Person' , schema : personSchema , indexes : [ { path : 'name' , unique : true } , { path : 'age' , unique : false } , ] , } , { name : 'Empty' , } ) ; await db . open ( 2 ) ; await createChanges ( ) ; db . remote . set ( { serviceHost } ) ; await db . remote . authorize ( privateKey ) ; try { await db . remote . push ( 'Person' ) ; } catch ( err ) { expect ( err ) . to . equal ( Errors . ThreadIDError ) ; await db . remote . initialize ( id ) ; } await db . remote . push ( 'Person' ) ; const client = createDbClient ( db . remote . config ) ; const info = await client . getCollectionInfo ( ThreadID . fromString ( db . id ! ) , 'Person' ) ; expect ( info . schema ) . to . deep . equal ( personSchema ) ; } ) ; } ) ; } ) ; } ) ;