import { jt400 } from './db' ; import { expect } from 'chai' ; import * as streamEqual from 'stream-equal' ; const { ifs } = jt400 ; describe ( 'ifs' , ( ) => { it ( 'should read file' , ( done ) => { const stream = ifs ( ) . createReadStream ( '/atm/test/hello_world.txt' ) ; let data = '' ; stream . on ( 'data' , ( chunk ) => { data += chunk ; } ) ; stream . on ( 'end' , ( ) => { expect ( data ) . to . equal ( 'Halló heimur!\n' ) ; done ( ) ; } ) ; stream . on ( 'error' , done ) ; } ) . timeout ( 50000 ) ; it ( 'should get file metadata' , async ( ) => { const metadata = await ifs ( ) . fileMetadata ( '/atm/test/hello_world.txt' ) ; expect ( metadata ) . to . deep . equal ( { exists : true , length : 15 , } ) ; } ) ; it ( 'should get metadata for file that does not exists' , async ( ) => { const metadata = await ifs ( ) . fileMetadata ( '/atm/test/___file_that_does_not_exists____.txt' ) ; expect ( metadata ) . to . deep . equal ( { exists : false , length : 0 , } ) ; } ) ; it ( 'should read filename promise' , ( done ) => { const stream = ifs ( ) . createReadStream ( Promise . resolve ( '/atm/test/hello_world.txt' ) ) ; let data = '' ; stream . on ( 'data' , ( chunk ) => { data += chunk ; } ) ; stream . on ( 'end' , ( ) => { expect ( data ) . to . equal ( 'Halló heimur!\n' ) ; done ( ) ; } ) ; stream . on ( 'error' , done ) ; } ) . timeout ( 50000 ) ; it ( 'should write file' , ( done ) => { const rs = ifs ( ) . createReadStream ( '/atm/test/hello_world.txt' ) ; const ws = ifs ( ) . createWriteStream ( '/atm/test2/new_file.txt' , { append : false , } ) ; rs . pipe ( ws ) . on ( 'finish' , ( ) => { const stream = ifs ( ) . createReadStream ( '/atm/test2/new_file.txt' ) ; let data = '' ; stream . on ( 'data' , ( chunk ) => { data += chunk ; } ) ; stream . on ( 'end' , ( ) => { expect ( data ) . to . equal ( 'Halló heimur!\n' ) ; done ( ) ; } ) ; stream . on ( 'error' , done ) ; } ) . on ( 'error' , done ) ; } ) . timeout ( 50000 ) ; it ( 'should pipe image' , ( ) => { const rs = ifs ( ) . createReadStream ( '/atm/test/image.jpg' ) ; const ws = ifs ( ) . createWriteStream ( '/atm/test2/image.jpg' , { append : false , } ) ; rs . pipe ( ws ) . on ( 'finish' , ( ) => { const oldImage = ifs ( ) . createReadStream ( '/atm/test/image.jpg' ) ; const newImage = ifs ( ) . createReadStream ( '/atm/test2/image.jpg' ) ; return streamEqual ( oldImage , newImage ) . then ( ( equal ) => { expect ( equal ) . to . be . equal ( true ) ; } ) ; } ) ; } ) . timeout ( 50000 ) ; } ) ;