import { expect } from 'chai' ; import { NaclInterface } from '../../src/nacl/nacl_types' ; import * as fast from '../../src/nacl/fast' ; import * as slow from '../../src/nacl/slow' ; const moduleLibrary = require ( 'module' ) ; const resetTest = ( ) => { delete process . env . NACL_FAST ; delete require . cache [ require . resolve ( '../../src/nacl' ) ] ; } ; interface naclLibrary extends NaclInterface { NACL_SIGN_PUBLICKEY_LENGTH : number ; NACL_SIGN_SIGNATURE_LENGTH : number ; } const stripConstants = ( library ) => { const { NACL_SIGN_PUBLICKEY_LENGTH , NACL_SIGN_SIGNATURE_LENGTH , ... strippedLib } = library ; return strippedLib ; } ; describe ( 'nacl index.js' , ( ) => { let initialEnvVar ; before ( ( ) => { initialEnvVar = process . env . NACL_FAST ; return Promise . resolve ( ) ; } ) ; after ( ( ) => { if ( initialEnvVar ) { process . env . NACL_FAST = initialEnvVar ; } else { delete process . env . NACL_FAST ; } return Promise . resolve ( ) ; } ) ; beforeEach ( ( ) => { resetTest ( ) ; return Promise . resolve ( ) ; } ) ; describe ( 'nacl fast installed' , ( ) => { beforeEach ( ( ) => { resetTest ( ) ; return Promise . resolve ( ) ; } ) ; it ( 'should load nacl fast if process.env.NACL_FAST is set to enable' , ( ) => { process . env . NACL_FAST = 'enable' ; const loadedLibrary = require ( '../../src/nacl' ) ; const strippedLibrary = stripConstants ( loadedLibrary ) ; return expect ( strippedLibrary ) . to . be . eql ( fast ) ; } ) ; it ( 'should load nacl slow if process.env.NACL_FAST is set to disable' , ( ) => { process . env . NACL_FAST = 'disable' ; const loadedLibrary = require ( '../../src/nacl' ) ; const strippedLibrary = stripConstants ( loadedLibrary ) ; return expect ( strippedLibrary ) . to . be . eql ( slow ) ; } ) ; it ( 'should load nacl fast if process.env.NACL_FAST is undefined' , ( ) => { process . env . NACL_FAST = undefined ; const loadedLibrary = require ( '../../src/nacl' ) ; const strippedLibrary = stripConstants ( loadedLibrary ) ; return expect ( strippedLibrary ) . to . be . eql ( fast ) ; } ) ; } ) ; describe ( 'nacl fast not installed' , ( ) => { const moduleNotFoundError = new Error ( 'MODULE_NOT_FOUND' ) ; beforeEach ( ( ) => { resetTest ( ) ; sandbox . stub ( moduleLibrary , '_load' ) . callThrough ( ) . withArgs ( './fast' ) . throws ( moduleNotFoundError ) ; return Promise . resolve ( ) ; } ) ; it ( 'should set process.env.NACL_FAST to disable' , ( ) => { require ( '../../src/nacl' ) ; return expect ( process . env . NACL_FAST ) . to . eql ( 'disable' ) ; } ) ; it ( 'should load nacl slow if process.env.NACL_FAST is set to enable' , ( ) => { process . env . NACL_FAST = 'enable' ; const loadedLibrary = require ( '../../src/nacl' ) ; const strippedLibrary = stripConstants ( loadedLibrary ) ; return expect ( strippedLibrary ) . to . eql ( slow ) ; } ) ; it ( 'should load nacl slow if process.env.NACL_FAST is set to disable' , ( ) => { process . env . NACL_FAST = 'disable' ; const loadedLibrary = require ( '../../src/nacl' ) ; const strippedLibrary = stripConstants ( loadedLibrary ) ; return expect ( strippedLibrary ) . to . eql ( slow ) ; } ) ; it ( 'should load nacl slow if process.env.NACL_FAST is undefined' , ( ) => { process . env . NACL_FAST = undefined ; const loadedLibrary = require ( '../../src/nacl' ) ; const strippedLibrary = stripConstants ( loadedLibrary ) ; return expect ( strippedLibrary ) . to . eql ( slow ) ; } ) ; } ) ; } ) ;