1+ const chai = require ( 'chai' ) ;
2+ const initializationParams = require ( "./data" ) . initializationParams
3+ const ImageKit = require ( ".." ) ; // This will automatically pick main module (cjs bundle) as per package.json
4+ var imagekit = new ImageKit ( initializationParams ) ;
5+
6+ // helpers
7+ const errors = require ( './helpers/errors' ) ;
8+ const spies = require ( './helpers/spies' ) ;
9+
10+ const { expect } = chai ;
11+ const { pHashDistanceSpy } = spies ;
12+
13+ const failureHelper = ( expectedError , ...params ) => {
14+ const { message, help } = expectedError ;
15+ const { message : error } = imagekit . pHashDistance ( ...params ) ;
16+
17+ expect ( error ) . to . be . equal ( `${ message } : ${ help } ` ) ;
18+ } ;
19+
20+ const successHelper = ( distance , ...params ) => {
21+ const result = imagekit . pHashDistance ( ...params ) ;
22+
23+ expect ( result ) . to . be . a ( 'number' ) ;
24+ expect ( result ) . to . equal ( distance ) ;
25+ expect ( pHashDistanceSpy . calledOnceWithExactly ( ...params ) ) . to . equal ( true ) ;
26+ } ;
27+
28+ const pHash = {
29+ invalidAlphabeticalString : 'INVALIDHEXSTRING' ,
30+ invalidCharacterString : 'a4a655~!!@94518b' ,
31+ invalidHexStringLength : '42' ,
32+ numeric : 2222222222222222 ,
33+ valid : 'f06830ca9f1e3e90' ,
34+ // sets
35+ dissimilar : [
36+ 'a4a65595ac94518b' ,
37+ '7838873e791f8400' ,
38+ ] ,
39+ similar : [
40+ '2d5ad3936d2e015b' ,
41+ '2d6ed293db36a4fb' ,
42+ ] ,
43+ } ;
44+
45+ describe ( 'Utils > pHash > Distance calculator' , ( ) => {
46+ beforeEach ( ( ) => {
47+ pHashDistanceSpy . resetHistory ( ) ;
48+ } ) ;
49+
50+ after ( ( ) => {
51+ pHashDistanceSpy . resetHistory ( ) ;
52+ } ) ;
53+
54+ context ( 'Failure cases:' , ( ) => {
55+ it ( 'Should return error for missing first pHash' , ( ) => {
56+ failureHelper ( errors . MISSING_PHASH_VALUE , null , pHash . valid ) ;
57+ } ) ;
58+
59+ it ( 'Should return error for missing second pHash' , ( ) => {
60+ failureHelper ( errors . MISSING_PHASH_VALUE , pHash . valid ) ;
61+ } ) ;
62+
63+ it ( 'Should return error for invalid first pHash' , ( ) => {
64+ failureHelper ( errors . INVALID_PHASH_VALUE , pHash . invalidAlphabeticalString , pHash . valid ) ;
65+ } ) ;
66+
67+ it ( 'Should return error for invalid second pHash' , ( ) => {
68+ failureHelper ( errors . INVALID_PHASH_VALUE , pHash . valid , pHash . invalidCharacterString ) ;
69+ } ) ;
70+
71+ it ( 'Should return error for unequal pHash lengths' , ( ) => {
72+ failureHelper ( errors . UNEQUAL_STRING_LENGTH , pHash . valid , pHash . invalidHexStringLength ) ;
73+ } ) ;
74+ } ) ;
75+
76+ context ( 'Success cases:' , ( ) => {
77+ it ( 'Should return zero distance between pHash for same image' , ( ) => {
78+ successHelper ( 0 , pHash . valid , pHash . valid ) ;
79+ } ) ;
80+
81+ it ( 'Should return smaller distance between pHash for similar images' , ( ) => {
82+ successHelper ( 17 , pHash . similar [ 0 ] , pHash . similar [ 1 ] ) ;
83+ } ) ;
84+
85+ it ( 'Should return larger distance between pHash for dissimilar images' , ( ) => {
86+ successHelper ( 37 , pHash . dissimilar [ 0 ] , pHash . dissimilar [ 1 ] ) ;
87+ } ) ;
88+
89+ it ( 'Should return distance for non-string but valid hexanumeric pHash' , ( ) => {
90+ successHelper ( 30 , pHash . valid , pHash . numeric ) ;
91+ } ) ;
92+ } ) ;
93+ } ) ;
0 commit comments