import { data , keys } from '../__data__' ; import { common } from '../common' ; import { contractParameters , smartContractConverters } from '../contractParameters' ; import { BufferContractParameter } from '../types' ; import { utils } from '../utils' ; describe ( 'contractParameters' , ( ) => { const randomName = 'random' ; const undefinedValue = { type : 'Buffer' , value : '' , name : randomName } ; test ( 'String - optional undefined' , ( ) => { const result = contractParameters . String ( undefinedValue , { type : 'String' , optional : true } ) ; expect ( result ) . toBeUndefined ( ) ; } ) ; test ( 'String - optional defined' , ( ) => { const value = 'foo' ; const result = contractParameters . String ( { type : 'String' , value , name : randomName } , { type : 'String' , optional : true } ) ; expect ( result ) . toEqual ( value ) ; } ) ; test ( 'String - defined' , ( ) => { const value = 'foo' ; const result = contractParameters . String ( { type : 'String' , value , name : randomName } , { type : 'String' } ) ; expect ( result ) . toEqual ( value ) ; } ) ; test ( 'String - Buffer' , ( ) => { const value = 'foo' ; const result = contractParameters . String ( { type : 'Buffer' , value : Buffer . from ( value , 'utf8' ) . toString ( 'hex' ) , name : randomName } , { type : 'String' } ) ; expect ( result ) . toEqual ( value ) ; } ) ; test ( 'String - Invalid' , ( ) => { const result = ( ) => contractParameters . String ( { type : 'Void' , name : randomName } , { type : 'String' } ) ; expect ( result ) . toThrowErrorMatchingSnapshot ( ) ; } ) ; test ( 'Address - optional undefined' , ( ) => { const result = contractParameters . Address ( undefinedValue , { type : 'Address' , optional : true } ) ; expect ( result ) . toBeUndefined ( ) ; } ) ; test ( 'Address - optional defined' , ( ) => { const value = keys [ 0 ] . address ; const result = contractParameters . Address ( { type : 'Address' , value , name : randomName } , { type : 'Address' , optional : true } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Address - defined' , ( ) => { const value = keys [ 0 ] . address ; const result = contractParameters . Address ( { type : 'Address' , value , name : randomName } , { type : 'Address' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Address - defined' , ( ) => { const value = keys [ 0 ] . scriptHash ; const result = contractParameters . Address ( { type : 'Buffer' , value : value . toString ( 'hex' ) , name : randomName } , { type : 'Address' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Address - Invalid' , ( ) => { const result = ( ) => contractParameters . Address ( { type : 'Void' , name : randomName } , { type : 'Address' } ) ; expect ( result ) . toThrowErrorMatchingSnapshot ( ) ; } ) ; test ( 'Hash256 - optional undefined' , ( ) => { const result = contractParameters . Hash256 ( undefinedValue , { type : 'Hash256' , optional : true } ) ; expect ( result ) . toBeUndefined ( ) ; } ) ; test ( 'Hash256 - optional defined' , ( ) => { const value = data . hash256s . a ; const result = contractParameters . Hash256 ( { type : 'Hash256' , value , name : randomName } , { type : 'Hash256' , optional : true } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Hash256 - defined' , ( ) => { const value = data . hash256s . a ; const result = contractParameters . Hash256 ( { type : 'Hash256' , value , name : randomName } , { type : 'Hash256' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Hash256 - Buffer' , ( ) => { const value = data . hash256s . a ; const result = contractParameters . Hash256 ( { type : 'Buffer' , value : common . stringToUInt256 ( value ) . toString ( 'hex' ) , name : randomName } , { type : 'Hash256' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Hash256 - Invalid' , ( ) => { const result = ( ) => contractParameters . Hash256 ( { type : 'Void' , name : randomName } , { type : 'Hash256' } ) ; expect ( result ) . toThrowErrorMatchingSnapshot ( ) ; } ) ; test ( 'PublicKey - optional undefined' , ( ) => { const result = contractParameters . PublicKey ( undefinedValue , { type : 'PublicKey' , optional : true } ) ; expect ( result ) . toBeUndefined ( ) ; } ) ; test ( 'PublicKey - optional defined' , ( ) => { const value = keys [ 0 ] . publicKeyString ; const result = contractParameters . PublicKey ( { type : 'PublicKey' , value , name : randomName } , { type : 'PublicKey' , optional : true } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'PublicKey - defined' , ( ) => { const value = keys [ 0 ] . publicKeyString ; const result = contractParameters . PublicKey ( { type : 'PublicKey' , value , name : randomName } , { type : 'PublicKey' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'PublicKey - Buffer' , ( ) => { const value = keys [ 0 ] . publicKey ; const result = contractParameters . PublicKey ( { type : 'Buffer' , value : value . toString ( 'hex' ) , name : randomName } , { type : 'PublicKey' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'PublicKey - Invalid' , ( ) => { const result = ( ) => contractParameters . PublicKey ( { type : 'Void' , name : randomName } , { type : 'PublicKey' } ) ; expect ( result ) . toThrowErrorMatchingSnapshot ( ) ; } ) ; test ( 'Integer - optional undefined' , ( ) => { const result = contractParameters . Integer ( undefinedValue , { type : 'Integer' , optional : true , decimals : 4 } ) ; expect ( result ) . toBeUndefined ( ) ; } ) ; test ( 'Integer - optional defined' , ( ) => { const value = data . bns . a ; const result = contractParameters . Integer ( { type : 'Integer' , value , name : randomName } , { type : 'Integer' , optional : true , decimals : 4 } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Integer - defined' , ( ) => { const value = data . bns . a ; const result = contractParameters . Integer ( { type : 'Integer' , value , name : randomName } , { type : 'Integer' , decimals : 4 } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Integer - Buffer' , ( ) => { const value = data . bns . a ; const result = contractParameters . Integer ( { type : 'Buffer' , value : utils . toSignedBuffer ( value ) . toString ( 'hex' ) , name : randomName } , { type : 'Integer' , decimals : 4 } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Integer - Invalid' , ( ) => { const result = ( ) => contractParameters . Integer ( { type : 'Void' , name : randomName } , { type : 'Integer' , decimals : 4 } ) ; expect ( result ) . toThrowErrorMatchingSnapshot ( ) ; } ) ; test ( 'Boolean - optional undefined' , ( ) => { const result = contractParameters . Boolean ( undefinedValue , { type : 'Boolean' , optional : true } ) ; expect ( result ) . toBeUndefined ( ) ; } ) ; test ( 'Boolean - optional defined' , ( ) => { const value = true ; const result = contractParameters . Boolean ( { type : 'Boolean' , value , name : randomName } , { type : 'Boolean' , optional : true } ) ; expect ( result ) . toEqual ( value ) ; } ) ; test ( 'Boolean - defined' , ( ) => { const value = false ; const result = contractParameters . Boolean ( { type : 'Boolean' , value , name : randomName } , { type : 'Boolean' } ) ; expect ( result ) . toEqual ( value ) ; } ) ; test ( 'Boolean - Array' , ( ) => { const value = false ; const result = contractParameters . Boolean ( { type : 'Array' , value : [ { type : 'Boolean' , value , name : randomName } ] , name : randomName } , { type : 'Boolean' } ) ; expect ( result ) . toEqual ( value ) ; } ) ; test ( 'Buffer - optional undefined' , ( ) => { const result = contractParameters . Buffer ( undefinedValue , { type : 'Buffer' , optional : true } ) ; expect ( result ) . toBeUndefined ( ) ; } ) ; test ( 'Buffer - optional defined' , ( ) => { const value = data . buffers . a ; const result = contractParameters . Buffer ( { type : 'Buffer' , value , name : randomName } , { type : 'Buffer' , optional : true } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Buffer - defined' , ( ) => { const value = data . buffers . a ; const result = contractParameters . Buffer ( { type : 'Buffer' , value , name : randomName } , { type : 'Buffer' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Buffer - Signature' , ( ) => { const value = data . signatures . a ; const result = contractParameters . Buffer ( { type : 'Signature' , value , name : randomName } , { type : 'Buffer' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Buffer - Boolean' , ( ) => { const value = true ; const result = contractParameters . Buffer ( { type : 'Boolean' , value , name : randomName } , { type : 'Buffer' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Buffer - Integer' , ( ) => { const value = data . bns . a ; const result = contractParameters . Buffer ( { type : 'Integer' , value , name : randomName } , { type : 'Buffer' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Buffer - Address' , ( ) => { const value = keys [ 0 ] . address ; const result = contractParameters . Buffer ( { type : 'Address' , value , name : randomName } , { type : 'Buffer' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Buffer - Hash256' , ( ) => { const value = data . hash256s . a ; const result = contractParameters . Buffer ( { type : 'Hash256' , value , name : randomName } , { type : 'Buffer' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Buffer - PublicKey' , ( ) => { const value = keys [ 0 ] . publicKeyString ; const result = contractParameters . Buffer ( { type : 'PublicKey' , value , name : randomName } , { type : 'Buffer' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Buffer - String' , ( ) => { const value = 'foo' ; const result = contractParameters . Buffer ( { type : 'String' , value , name : randomName } , { type : 'Buffer' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Buffer - InteropInterface' , ( ) => { const result = contractParameters . Buffer ( { type : 'InteropInterface' , name : randomName } , { type : 'Buffer' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Buffer - Void' , ( ) => { const result = contractParameters . Buffer ( { type : 'Void' , name : randomName } , { type : 'Buffer' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Buffer - Array' , ( ) => { const result = ( ) => contractParameters . Buffer ( { type : 'Array' , value : [ ] , name : randomName } , { type : 'Buffer' } ) ; expect ( result ) . toThrowErrorMatchingSnapshot ( ) ; } ) ; test ( 'Buffer - Array optional' , ( ) => { const result = ( ) => contractParameters . Buffer ( { type : 'Array' , value : [ ] , name : randomName } , { type : 'Buffer' , optional : true } ) ; expect ( result ) . toThrowErrorMatchingSnapshot ( ) ; } ) ; test ( 'Signature - optional undefined' , ( ) => { const result = contractParameters . Signature ( undefinedValue , { type : 'Signature' , optional : true } ) ; expect ( result ) . toBeUndefined ( ) ; } ) ; test ( 'Signature - optional defined' , ( ) => { const value = data . signatures . a ; const result = contractParameters . Signature ( { type : 'Signature' , value , name : randomName } , { type : 'Signature' , optional : true } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Signature - defined' , ( ) => { const value = data . signatures . a ; const result = contractParameters . Signature ( { type : 'Signature' , value , name : randomName } , { type : 'Signature' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Signature - defined' , ( ) => { const value = data . signatures . a ; const result = contractParameters . Signature ( { type : 'Buffer' , value , name : randomName } , { type : 'Signature' } ) ; expect ( result ) . toMatchSnapshot ( ) ; } ) ; test ( 'Signature - Invalid' , ( ) => { const result = ( ) => contractParameters . Signature ( { type : 'Void' , name : randomName } , { type : 'Signature' } ) ; expect ( result ) . toThrowErrorMatchingSnapshot ( ) ; } ) ; test ( 'Array - optional undefined' , ( ) => { const result = contractParameters . Array ( undefinedValue , { type : 'Array' , optional : true , value : { type : 'Boolean' } } ) ; expect ( result ) . toBeUndefined ( ) ; } ) ; test ( 'Array - optional defined' , ( ) => { const value = [ false ] ; const result = contractParameters . Array ( { type : 'Array' , value : [ { type : 'Boolean' , value : false , name : randomName } ] , name : randomName } , { type : 'Array' , optional : true , value : { type : 'Boolean' } } ) ; expect ( result ) . toEqual ( value ) ; } ) ; test ( 'Array - defined' , ( ) => { const value = [ true ] ; const result = contractParameters . Array ( { type : 'Array' , value : [ { type : 'Boolean' , value : true , name : randomName } ] , name : randomName } , { type : 'Array' , value : { type : 'Boolean' } } ) ; expect ( result ) . toEqual ( value ) ; } ) ; test ( 'Array - Invalid' , ( ) => { const result = ( ) => contractParameters . Array ( { type : 'Void' , name : randomName } , { type : 'Array' , value : { type : 'Boolean' } } ) ; expect ( result ) . toThrowErrorMatchingSnapshot ( ) ; } ) ; test ( 'InteropInterface - optional undefined' , ( ) => { const result = smartContractConverters . toInteropInterfaceNullable ( undefinedValue ) ; expect ( result ) . toBeUndefined ( ) ; } ) ; test ( 'InteropInterface - optional defined' , ( ) => { const result = smartContractConverters . toInteropInterfaceNullable ( { type : 'InteropInterface' , name : randomName } ) ; expect ( result ) . toEqual ( undefined ) ; } ) ; test ( 'InteropInterface - defined' , ( ) => { const result = smartContractConverters . toInteropInterface ( { type : 'InteropInterface' , name : randomName } ) ; expect ( result ) . toEqual ( undefined ) ; } ) ; test ( 'Void - optional undefined' , ( ) => { const result = contractParameters . Void ( undefinedValue , { type : 'Void' , optional : true } ) ; expect ( result ) . toBeUndefined ( ) ; } ) ; test ( 'Void - optional defined' , ( ) => { const result = contractParameters . Void ( { type : 'Void' , name : randomName } , { type : 'Void' , optional : true } ) ; expect ( result ) . toEqual ( undefined ) ; } ) ; test ( 'Void - defined' , ( ) => { const result = contractParameters . Void ( { type : 'Void' , name : randomName } , { type : 'Void' } ) ; expect ( result ) . toEqual ( undefined ) ; } ) ; } ) ; describe ( 'Extra Contract Parameter Coverage' , ( ) => { const randomName = 'random' ; const stringMap = [ { type : 'String' , value : 'one' } , { type : 'String' , value : '1' } , ] ; const mapParam = { type : 'Map' , value : [ stringMap ] , name : randomName } as any ; const mapABI = { type : 'Map' , key : { type : 'String' } , value : { type : 'String' } } as any ; const objABI = { type : 'Object' , properties : { one : { type : 'String' } } } as any ; test ( 'toForwardValue' , ( ) => { expect ( smartContractConverters . toForwardValue ( mapParam ) ) . toEqual ( mapParam ) ; } ) ; test ( 'toMap function' , ( ) => { const map = smartContractConverters . toMap ( mapParam , mapABI ) ; expect ( map ) . toEqual ( new Map ( [ [ 'one' , '1' ] ] ) ) ; } ) ; test ( 'toObject function' , ( ) => { const obj = smartContractConverters . toObject ( mapParam , objABI ) ; expect ( obj ) . toEqual ( { one : '1' } ) ; } ) ; test ( 'toMap Throws' , ( ) => { const toMapThrows = ( ) => smartContractConverters . toMap ( { type : 'Void' as 'Void' , name : randomName } , mapABI ) ; expect ( toMapThrows ) . toThrowError ( 'Expected one of ["Map"] ContractParameterTypes, found Void' ) ; } ) ; test ( 'toObject Throws' , ( ) => { const toObjectThrows = ( ) => smartContractConverters . toObject ( { type : 'Void' as 'Void' , name : randomName } , objABI ) ; expect ( toObjectThrows ) . toThrowError ( 'Expected one of ["Map"] ContractParameterTypes, found Void' ) ; } ) ; } ) ;