2020-08-06 19:17:38 +00:00
import {
2020-08-07 14:01:57 +00:00
findDuplicates ,
findCommonElementsOrDuplicates ,
2020-09-23 13:47:24 +00:00
} from "../script/generic/types" ;
2020-08-06 19:17:38 +00:00
import {
isChecksum ,
2020-08-31 21:33:20 +00:00
toChecksum ,
isEthereumAddress
2020-09-29 13:01:34 +00:00
} from "../script/generic/eth-address" ;
2020-08-06 19:17:38 +00:00
import {
isDimensionTooLarge ,
isDimensionOK ,
calculateTargetSize
2020-09-23 13:47:24 +00:00
} from "../script/generic/image" ;
2020-08-06 19:17:38 +00:00
import {
sortElements ,
makeUnique ,
2020-08-07 13:12:16 +00:00
arrayDiff ,
2020-08-07 14:01:57 +00:00
arrayDiffNocase ,
2020-08-10 14:56:02 +00:00
arrayEqual ,
reverseCase
2020-09-23 13:47:24 +00:00
} from "../script/generic/types" ;
2021-05-17 15:02:45 +00:00
import {
BinanceTokenInfo ,
findImagesToFetch
} from "../script/blockchain/binance" ;
2021-03-17 10:51:03 +00:00
import { isValidStatusValue } from "../script/generic/status-values" ;
2021-03-17 23:27:21 +00:00
import { isValidTagValue , isValidTagValues } from "../script/generic/tag-values" ;
2020-08-06 19:17:38 +00:00
2020-09-29 13:01:34 +00:00
describe ( "Test eth-address helpers" , ( ) = > {
2020-08-06 19:17:38 +00:00
test ( ` Test isChecksum ` , ( ) = > {
2020-08-10 14:56:02 +00:00
expect ( isChecksum ( "0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee" , "ethereum" ) , ` checksum ` ) . toBe ( true ) ;
expect ( isChecksum ( "0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee" , "ethereum" ) , ` lowercase ` ) . toBe ( false ) ;
expect ( isChecksum ( "0x7Bb09bC8aDE747178e95B1D035ecBe" , "ethereum" ) , ` too short ` ) . toBe ( false ) ;
2020-09-29 13:01:34 +00:00
expect ( isChecksum ( "7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee" , "ethereum" ) , ` checksum, no prefix ` ) . toBe ( true ) ;
expect ( isChecksum ( "7bb09bc8ade747178e95b1d035ecbeebbb18cfee" , "ethereum" ) , ` lowercase, no prefix ` ) . toBe ( false ) ;
2020-08-10 14:56:02 +00:00
expect ( isChecksum ( "0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee" , "wanchain" ) , ` wanchain wrong checksum ` ) . toBe ( false ) ;
expect ( isChecksum ( "0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee" , "wanchain" ) , ` wanchain lowercase ` ) . toBe ( false ) ;
expect ( isChecksum ( "0x7bB09Bc8Ade747178E95b1d035ECbEebBb18CfEE" , "wanchain" ) , ` wanchain checksum ` ) . toBe ( true ) ;
2020-08-06 19:17:38 +00:00
} ) ;
test ( ` Test toChecksum ` , ( ) = > {
2020-08-10 14:56:02 +00:00
expect ( toChecksum ( "0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee" , "ethereum" ) , ` from lowercase ` ) . toEqual ( "0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee" ) ;
expect ( toChecksum ( "0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee" , "ethereum" ) , ` from checksum ` ) . toEqual ( "0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee" ) ;
2020-09-29 13:01:34 +00:00
expect ( toChecksum ( "7bb09bc8ade747178e95b1d035ecbeebbb18cfee" , "ethereum" ) , ` from lowercase, no prefix ` ) . toEqual ( "0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee" ) ;
2020-08-10 14:56:02 +00:00
expect ( toChecksum ( "0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee" , "wanchain" ) , ` wanchain, from lowercase ` ) . toEqual ( "0x7bB09Bc8Ade747178E95b1d035ECbEebBb18CfEE" ) ;
2020-08-06 19:17:38 +00:00
} ) ;
2020-08-31 21:33:20 +00:00
test ( ` Test isEthereumAddress ` , ( ) = > {
2020-09-29 13:01:34 +00:00
expect ( isEthereumAddress ( "0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee" ) , ` valid, lowercase ` ) . toBe ( true ) ;
expect ( isEthereumAddress ( "0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee" ) , ` valid, checksum ` ) . toBe ( true ) ;
expect ( isEthereumAddress ( "b09bc8ade747178e95b1d035ecbeebbb18cfee" ) , ` invalid, short ` ) . toBe ( false ) ;
expect ( isEthereumAddress ( "7bb09bc8ade747178e95b1d035ecbeebbb18cfee" ) , ` valid, no prefix ` ) . toBe ( true ) ;
expect ( isEthereumAddress ( "0x7bb09bc8qde747178e95b1d035ecbeebbb18cfee" ) , ` invalid, length ok, invalid char ` ) . toBe ( false ) ;
2020-08-31 21:33:20 +00:00
} ) ;
2020-08-06 19:17:38 +00:00
} ) ;
describe ( "Test image helpers" , ( ) = > {
test ( ` Test isDimensionTooLarge ` , ( ) = > {
expect ( isDimensionTooLarge ( 256 , 256 ) , ` 256x256 ` ) . toBe ( false ) ;
expect ( isDimensionTooLarge ( 64 , 64 ) , ` 64x64 ` ) . toBe ( false ) ;
expect ( isDimensionTooLarge ( 800 , 800 ) , ` 800x800 ` ) . toBe ( true ) ;
expect ( isDimensionTooLarge ( 256 , 800 ) , ` 256x800 ` ) . toBe ( true ) ;
expect ( isDimensionTooLarge ( 800 , 256 ) , ` 800x256 ` ) . toBe ( true ) ;
} ) ;
test ( ` Test isDimensionOK ` , ( ) = > {
expect ( isDimensionOK ( 256 , 256 ) , ` 256x256 ` ) . toBe ( true ) ;
expect ( isDimensionOK ( 64 , 64 ) , ` 64x64 ` ) . toBe ( true ) ;
expect ( isDimensionOK ( 800 , 800 ) , ` 800x800 ` ) . toBe ( false ) ;
expect ( isDimensionOK ( 256 , 800 ) , ` 256x800 ` ) . toBe ( false ) ;
expect ( isDimensionOK ( 800 , 256 ) , ` 800x256 ` ) . toBe ( false ) ;
expect ( isDimensionOK ( 60 , 60 ) , ` 60x60 ` ) . toBe ( false ) ;
expect ( isDimensionOK ( 64 , 60 ) , ` 64x60 ` ) . toBe ( false ) ;
expect ( isDimensionOK ( 60 , 64 ) , ` 60x64 ` ) . toBe ( false ) ;
} ) ;
test ( ` Test calculateReducedSize ` , ( ) = > {
expect ( calculateTargetSize ( 256 , 256 , 512 , 512 ) , ` small 1.0 ` ) . toEqual ( { width : 512 , height : 512 } ) ;
expect ( calculateTargetSize ( 800 , 800 , 512 , 512 ) , ` large 1.0 ` ) . toEqual ( { width : 512 , height : 512 } ) ;
expect ( calculateTargetSize ( 200 , 100 , 512 , 512 ) , ` small 2.0 ` ) . toEqual ( { width : 512 , height : 256 } ) ;
expect ( calculateTargetSize ( 100 , 200 , 512 , 512 ) , ` small 0.5 ` ) . toEqual ( { width : 256 , height : 512 } ) ;
expect ( calculateTargetSize ( 1200 , 600 , 512 , 512 ) , ` small 2.0 ` ) . toEqual ( { width : 512 , height : 256 } ) ;
expect ( calculateTargetSize ( 600 , 1200 , 512 , 512 ) , ` small 0.5 ` ) . toEqual ( { width : 256 , height : 512 } ) ;
expect ( calculateTargetSize ( 256 , 0 , 512 , 512 ) , ` zero ` ) . toEqual ( { width : 512 , height : 512 } ) ;
} ) ;
} ) ;
describe ( "Test type helpers" , ( ) = > {
test ( ` Test sortElements ` , ( ) = > {
expect ( sortElements ( [ "c" , "a" , "b" ] ) , ` 3 elems ` ) . toEqual ( [ "a" , "b" , "c" ] ) ;
expect ( sortElements ( [ "C" , "a" , "b" ] ) , ` mixed case ` ) . toEqual ( [ "a" , "b" , "C" ] ) ;
2020-09-18 14:39:31 +00:00
expect ( sortElements ( [ "1" , "2" , "11" ] ) , ` numerical string ` ) . toEqual ( [ "1" , "2" , "11" ] ) ;
expect ( sortElements ( [ 1 , 2 , 11 ] ) , ` numerical ` ) . toEqual ( [ 1 , 2 , 11 ] ) ;
2020-08-06 19:17:38 +00:00
expect ( sortElements ( [ "C" , "a" , "1" , "b" , "2" , "11" ] ) , ` complex ` ) . toEqual ( [ "1" , "2" , "11" , "a" , "b" , "C" ] ) ;
} ) ;
test ( ` Test makeUnique ` , ( ) = > {
expect ( makeUnique ( [ "a" , "b" , "c" , "b" ] ) , ` 4 elems with 1 duplicate ` ) . toEqual ( [ "a" , "b" , "c" ] ) ;
} ) ;
test ( ` Test arrayDiff ` , ( ) = > {
expect ( arrayDiff ( [ "a" , "b" , "c" ] , [ "c" ] ) , ` 4 elems with 1 duplicate ` ) . toEqual ( [ "a" , "b" ] ) ;
2020-08-07 13:12:16 +00:00
expect ( arrayDiff ( [ "a" , "b" , "c" ] , [ "d" ] ) , ` 4 elems with 0 duplicate ` ) . toEqual ( [ "a" , "b" , "c" ] ) ;
2020-08-07 14:01:57 +00:00
expect ( arrayDiff ( [ "a" , "B" , "c" ] , [ "C" ] ) , ` 4 elems with 0 duplicate ` ) . toEqual ( [ "a" , "B" , "c" ] ) ;
expect ( arrayDiffNocase ( [ "a" , "B" , "c" ] , [ "C" ] ) , ` 4 elems with 0 duplicate ` ) . toEqual ( [ "a" , "B" ] ) ;
2020-08-06 19:17:38 +00:00
} ) ;
2020-08-07 14:01:57 +00:00
test ( ` Test findDuplicates ` , ( ) = > {
expect ( findDuplicates ( [ "a" , "bb" , "ccc" ] ) , ` No duplicates ` ) . toEqual ( [ ] ) ;
expect ( findDuplicates ( [ "a" , "bb" , "ccc" , "bb" ] ) , ` One double duplicate ` ) . toEqual ( [ "bb" ] ) ;
expect ( findDuplicates ( [ ] ) , ` Empty array ` ) . toEqual ( [ ] ) ;
expect ( findDuplicates ( [ "a" ] ) , ` One element ` ) . toEqual ( [ ] ) ;
expect ( findDuplicates ( [ "a" , "bb" , "ccc" , "bb" , "d" , "bb" ] ) , ` One triple duplicate ` ) . toEqual ( [ "bb" ] ) ;
expect ( findDuplicates ( [ "a" , "bb" , "ccc" , "bb" , "a" ] ) , ` Two double duplicates ` ) . toEqual ( [ "bb" , "a" ] ) ;
2020-08-06 19:17:38 +00:00
} ) ;
2020-08-07 14:01:57 +00:00
test ( ` Test findCommonElementsOrDuplicates ` , ( ) = > {
expect ( findCommonElementsOrDuplicates ( [ "a" , "bb" , "ccc" ] , [ "1" , "22" , "333" ] ) , ` No intersection or duplicates ` ) . toEqual ( [ ] ) ;
expect ( findCommonElementsOrDuplicates ( [ "a" , "bb" , "ccc" ] , [ "1" , "bb" , "333" ] ) , ` Common element ` ) . toEqual ( [ "bb" ] ) ;
expect ( findCommonElementsOrDuplicates ( [ "a" , "bb" , "ccc" , "bb" ] , [ "1" , "22" , "333" ] ) , ` Duplicate in first ` ) . toEqual ( [ "bb" ] ) ;
expect ( findCommonElementsOrDuplicates ( [ "a" , "bb" , "ccc" ] , [ "1" , "22" , "333" , "22" ] ) , ` Duplicate in second ` ) . toEqual ( [ "22" ] ) ;
expect ( findCommonElementsOrDuplicates ( [ "a" , "bb" , "ccc" , "1" , "bb" ] , [ "1" , "22" , "333" , "22" ] ) , ` Intersection and duplicates ` ) . toEqual ( [ "bb" , "1" , "22" ] ) ;
expect ( findCommonElementsOrDuplicates ( [ ] , [ ] ) , ` Empty lists ` ) . toEqual ( [ ] ) ;
2020-08-06 19:17:38 +00:00
} ) ;
2020-08-07 13:12:16 +00:00
test ( ` Test arrayEqual ` , ( ) = > {
expect ( arrayEqual ( [ "a" , "b" , "c" ] , [ "a" , "b" , "c" ] ) , ` equal ` ) . toBe ( true ) ;
expect ( arrayEqual ( [ "a" , "b" , "c" , "d" ] , [ "a" , "b" , "c" ] ) , ` length mismatch ` ) . toBe ( false ) ;
expect ( arrayEqual ( [ "a" , "b" , "c" ] , [ "a" , "b" , "b" ] ) , ` length mismatch ` ) . toBe ( false ) ;
expect ( arrayEqual ( [ "a" , "b" , "b" ] , [ "a" , "b" , "c" ] ) , ` length mismatch ` ) . toBe ( false ) ;
} ) ;
2020-08-10 14:56:02 +00:00
test ( ` Test reverseCase ` , ( ) = > {
expect ( reverseCase ( "abCDef12+-" ) , ` mixed ` ) . toEqual ( "ABcdEF12+-" ) ;
expect ( reverseCase ( "ABcdEF12+-" ) , ` mixed ` ) . toEqual ( "abCDef12+-" ) ;
} ) ;
2020-08-06 19:17:38 +00:00
} ) ;
2020-09-23 13:47:24 +00:00
describe ( "Test blockchain binance" , ( ) = > {
2020-08-06 19:17:38 +00:00
test ( ` Test findImagesToFetch ` , ( ) = > {
2021-05-17 15:02:45 +00:00
const infoA1 : BinanceTokenInfo = { asset : "A1-11" , name : "A 1" , mappedAsset : "A1" , assetImg : "imgurl1" , decimals : 8 } ;
const infoA2 : BinanceTokenInfo = { asset : "A2-12" , name : "A 2" , mappedAsset : "A2" , assetImg : "imgurl2" , decimals : 8 } ;
const assetsInfoListNonexisting : BinanceTokenInfo [ ] = [ infoA1 , infoA2 ] ;
const assetsInfoListExisting : BinanceTokenInfo [ ] = [
{ asset : "BUSD-BD1" , name : "Binance USD" , mappedAsset : "BUSD" , assetImg : "imgurlBUSD" , decimals : 8 } ,
{ asset : "ETH-1C9" , name : "Binance Ethereum" , mappedAsset : "BETH" , assetImg : "imgurlETH" , decimals : 8 }
] ;
2020-08-18 06:50:32 +00:00
const denyListEmpty : string [ ] = [ ] ;
2021-05-17 15:02:45 +00:00
const denyListA1 : string [ ] = [ "A1-11" ] ;
2020-08-18 06:50:32 +00:00
expect ( findImagesToFetch ( assetsInfoListNonexisting , denyListEmpty ) , ` 2 nonexisting ` ) . toEqual ( assetsInfoListNonexisting ) ;
2021-05-17 15:02:45 +00:00
expect ( findImagesToFetch ( assetsInfoListNonexisting , denyListA1 ) , ` 2 nonexisting with 1 denylisted ` ) . toEqual ( [ infoA2 ] ) ;
2020-08-18 06:50:32 +00:00
expect ( findImagesToFetch ( assetsInfoListExisting , denyListEmpty ) , ` 2 existing ` ) . toEqual ( [ ] ) ;
2020-08-06 19:17:38 +00:00
expect ( findImagesToFetch ( [ ] , [ ] ) , ` empty ` ) . toEqual ( [ ] ) ;
} ) ;
} ) ;
2021-03-17 10:51:03 +00:00
2021-03-17 23:27:21 +00:00
describe ( "Test status, tag values" , ( ) = > {
2021-03-17 10:51:03 +00:00
test ( ` Test status-values ` , ( ) = > {
expect ( isValidStatusValue ( "active" ) ) . toEqual ( true ) ;
expect ( isValidStatusValue ( "abandoned" ) ) . toEqual ( true ) ;
expect ( isValidStatusValue ( "invalidvalue" ) ) . toEqual ( false ) ;
expect ( isValidStatusValue ( "ACTIVE" ) ) . toEqual ( false ) ;
expect ( isValidStatusValue ( "" ) ) . toEqual ( false ) ;
} ) ;
2021-03-17 23:27:21 +00:00
test ( ` Test tag-values ` , ( ) = > {
expect ( isValidTagValue ( "defi" ) ) . toEqual ( true ) ;
2021-03-18 20:42:03 +00:00
expect ( isValidTagValue ( "staking" ) ) . toEqual ( true ) ;
2021-03-17 23:27:21 +00:00
expect ( isValidStatusValue ( "invalidvalue" ) ) . toEqual ( false ) ;
2021-03-18 20:42:03 +00:00
expect ( isValidStatusValue ( "STAKING" ) ) . toEqual ( false ) ;
2021-03-17 23:27:21 +00:00
expect ( isValidStatusValue ( "" ) ) . toEqual ( false ) ;
expect ( isValidTagValues ( [ "defi" ] ) ) . toEqual ( true ) ;
2021-03-18 20:42:03 +00:00
expect ( isValidTagValues ( [ "staking" ] ) ) . toEqual ( true ) ;
expect ( isValidTagValues ( [ "defi" , "staking" ] ) ) . toEqual ( true ) ;
2021-03-17 23:27:21 +00:00
expect ( isValidTagValues ( [ "invalid" ] ) ) . toEqual ( false ) ;
expect ( isValidTagValues ( [ "defi" , "invalid" ] ) ) . toEqual ( false ) ;
} ) ;
2021-03-17 10:51:03 +00:00
} ) ;