import { SC , startServer , stopServer } from './helpers/nats_server_control' ; import test from 'ava' ; import { next } from 'nuid' ; import { connect , Msg , Payload } from '../src/nats' ; import { Lock } from './helpers/latch' ; test . before ( async ( t ) => { let server = await startServer ( ) ; t . context = { server : server } ; } ) ; test . after . always ( ( t ) => { stopServer ( t . context . server ) ; } ) ; test ( 'json types' , async ( t ) => { t . plan ( 2 ) ; let lock = new Lock ( ) ; let sc = t . context as SC ; let nc = await connect ( { url : sc . server . nats , payload : Payload . JSON } ) ; let subj = next ( ) ; nc . subscribe ( subj , ( err , msg ) => { if ( err ) { t . fail ( err . message ) ; } t . is ( typeof msg . data , 'number' ) ; t . is ( msg . data , 6691 ) ; lock . unlock ( ) ; } , { max : 1 } ) ; nc . publish ( subj , 6691 ) ; nc . flush ( ) ; return lock . latch ; } ) ; test ( 'string types' , async ( t ) => { t . plan ( 2 ) ; let lock = new Lock ( ) ; let sc = t . context as SC ; let nc = await connect ( { url : sc . server . nats , payload : Payload . STRING } ) ; let subj = next ( ) ; nc . subscribe ( subj , ( err , msg ) => { t . is ( typeof msg . data , 'string' ) ; t . is ( msg . data , 'hello world' ) ; lock . unlock ( ) ; } , { max : 1 } ) ; nc . publish ( subj , Buffer . from ( 'hello world' ) ) ; nc . flush ( ) ; return lock . latch ; } ) ; test ( 'binary types' , async ( t ) => { t . plan ( 2 ) ; let lock = new Lock ( ) ; let sc = t . context as SC ; let nc = await connect ( { url : sc . server . nats , payload : Payload . BINARY } ) ; let subj = next ( ) ; nc . subscribe ( subj , ( err , msg ) => { t . truthy ( Buffer . isBuffer ( msg . data ) ) ; t . is ( msg . data . toString ( ) , 'hello world' ) ; lock . unlock ( ) ; } , { max : 1 } ) ; nc . publish ( subj , Buffer . from ( 'hello world' ) ) ; nc . flush ( ) ; return lock . latch ; } ) ; test ( 'binary encoded per client' , async ( t ) => { t . plan ( 4 ) ; let lock = new Lock ( 2 ) ; let sc = t . context as SC ; let nc1 = await connect ( { url : sc . server . nats , payload : Payload . BINARY } ) ; let nc2 = await connect ( { url : sc . server . nats , payload : Payload . STRING } ) ; let subj = next ( ) ; nc1 . subscribe ( subj , ( err , msg ) => { if ( err ) { t . fail ( err . message ) ; } t . truthy ( Buffer . isBuffer ( msg . data ) ) ; t . is ( msg . data . toString ( ) , 'hello world' ) ; lock . unlock ( ) ; } , { max : 1 } ) ; nc2 . subscribe ( subj , ( err , msg ) => { if ( err ) { t . fail ( err . message ) ; } t . is ( typeof msg . data , 'string' ) ; t . is ( msg . data , 'hello world' ) ; lock . unlock ( ) ; } , { max : 1 } ) ; await nc1 . flush ( ) ; await nc2 . flush ( ) ; nc2 . publish ( subj , 'hello world' ) ; return lock . latch ; } ) ; test ( 'binary client gets binary' , async ( t ) => { t . plan ( 2 ) ; let lock = new Lock ( ) ; let sc = t . context as SC ; let nc1 = await connect ( { url : sc . server . nats , payload : Payload . BINARY } ) ; let subj = next ( ) ; nc1 . subscribe ( subj , ( err , msg ) => { t . truthy ( Buffer . isBuffer ( msg . data ) ) ; t . is ( msg . data . toString ( ) , 'hello world' ) ; lock . unlock ( ) ; } , { max : 1 } ) ; nc1 . publish ( subj , 'hello world' ) ; await nc1 . flush ( ) ; return lock . latch ; } ) ;