import test from 'ava' ; import { SC , startServer , stopServer } from './helpers/nats_server_control' ; import { connect , NatsConnectionOptions , NatsError } from '../src/nats' ; import { createInbox } from '../src/util' ; test . before ( async ( t ) => { let server = await startServer ( ) ; t . context = { server : server } ; } ) ; test . after . always ( ( t ) => { stopServer ( t . context . server ) ; } ) ; test ( 'pub sub with utf8 payloads by default' , async ( t ) => { t . plan ( 5 ) ; let sc = t . context as SC ; let nc = await connect ( sc . server . nats ) ; let data = '\u00bd + \u00bc = \u00be' ; t . is ( data . length , 9 ) ; t . is ( Buffer . byteLength ( data ) , 12 ) ; let subj = createInbox ( ) ; let sub = nc . subscribe ( subj , ( err , msg ) => { t . is ( msg . data . length , 9 ) ; t . is ( Buffer . byteLength ( msg . data ) , 12 ) ; t . is ( msg . data , data ) ; } , { max : 1 } ) ; nc . publish ( subj , data ) ; await nc . flush ( ) ; } ) ; test ( 'override encoding' , async ( t ) => { t . plan ( 5 ) ; let sc = t . context as SC ; let opts = { url : sc . server . nats , encoding : 'ascii' } as NatsConnectionOptions ; let nc = await connect ( opts ) ; let data = '\u00bd + \u00bc = \u00be' ; let plain_data = 'Hello World' ; t . is ( data . length , 9 ) ; t . is ( Buffer . byteLength ( data ) , 12 ) ; let subj1 = createInbox ( ) ; let sub1 = nc . subscribe ( subj1 , ( err , msg ) => { t . is ( msg . data . length , 12 ) ; t . not ( msg . data , data ) ; } , { max : 1 } ) ; nc . publish ( subj1 , data ) ; let subj2 = createInbox ( ) ; let sub2 = nc . subscribe ( subj2 , ( err , msg ) => { t . is ( msg . data , plain_data ) ; } , { max : 1 } ) ; nc . publish ( subj2 , plain_data ) ; await nc . flush ( ) ; } ) ; test ( 'unsupported encoding' , async ( t ) => { t . plan ( 1 ) ; let sc = t . context as SC ; let opts = { url : sc . server . nats , encoding : 'foobar' } as NatsConnectionOptions ; let error = connect ( opts ) ; await t . throwsAsync ( error , { instanceOf : NatsError , code : 'INVALID_ENCODING' } ) ; } ) ;