1
- import { Logger } from 'winston' ;
1
+ // @ts -ignore: Using old wallet-lib version, no types exported
2
2
import hathorLib from '@hathor/wallet-lib' ;
3
- import { mockedAddAlert } from '@tests/utils /alerting.utils.mock' ;
4
- import { Severity } from '@wallet-service/common/ src/types' ;
5
- import { MAX_METADATA_UPDATE_RETRIES , NftUtils } from '@src/utils/nft.utils' ;
6
- import { getHandlerContext , getTransaction } from '@ events/nftCreationTx' ;
3
+ import { mockedAddAlert } from '. /alerting.utils.mock' ;
4
+ import { Severity } from '@src/types' ;
5
+ import { NftUtils } from '@src/utils/nft.utils' ;
6
+ import { getHandlerContext , getTransaction } from '../ events/nftCreationTx' ;
7
7
import {
8
8
LambdaClient as LambdaClientMock ,
9
9
InvokeCommandOutput ,
10
10
} from '@aws-sdk/client-lambda' ;
11
+ import { Logger } from 'winston' ;
12
+
13
+ jest . mock ( 'winston' , ( ) => {
14
+ class FakeLogger {
15
+ warn ( ) {
16
+ return jest . fn ( ) ;
17
+ }
18
+ error ( ) {
19
+ return jest . fn ( ) ;
20
+ }
21
+ info ( ) {
22
+ return jest . fn ( ) ;
23
+ }
24
+ } ;
25
+
26
+ return {
27
+ Logger : FakeLogger ,
28
+ }
29
+ } ) ;
11
30
12
31
jest . mock ( '@aws-sdk/client-lambda' , ( ) => {
13
32
const mLambda = { send : jest . fn ( ) } ;
@@ -18,19 +37,23 @@ jest.mock('@aws-sdk/client-lambda', () => {
18
37
} ;
19
38
} ) ;
20
39
40
+ const network = new hathorLib . Network ( 'testnet' ) ;
41
+ const logger = new Logger ( ) ;
42
+
21
43
describe ( 'shouldInvokeNftHandlerForTx' , ( ) => {
22
44
it ( 'should return false for a NFT transaction if the feature is disabled' , ( ) => {
23
45
expect . hasAssertions ( ) ;
24
46
25
47
// Preparation
26
48
const tx = getTransaction ( ) ;
27
- const isNftTransaction = NftUtils . isTransactionNFTCreation ( tx ) ;
49
+ const isNftTransaction = NftUtils . isTransactionNFTCreation ( tx , network , logger ) ;
28
50
expect ( isNftTransaction ) . toStrictEqual ( true ) ;
29
51
30
52
expect ( process . env . NFT_AUTO_REVIEW_ENABLED ) . not . toStrictEqual ( 'true' ) ;
31
53
32
54
// Execution
33
- const result = NftUtils . shouldInvokeNftHandlerForTx ( tx ) ;
55
+ // @ts -ignore
56
+ const result = NftUtils . shouldInvokeNftHandlerForTx ( tx , network , logger ) ;
34
57
35
58
// Assertion
36
59
expect ( result ) . toBe ( false ) ;
@@ -41,14 +64,14 @@ describe('shouldInvokeNftHandlerForTx', () => {
41
64
42
65
// Preparation
43
66
const tx = getTransaction ( ) ;
44
- const isNftTransaction = NftUtils . isTransactionNFTCreation ( tx ) ;
67
+ const isNftTransaction = NftUtils . isTransactionNFTCreation ( tx , network , logger ) ;
45
68
expect ( isNftTransaction ) . toStrictEqual ( true ) ;
46
69
47
70
const oldValue = process . env . NFT_AUTO_REVIEW_ENABLED ;
48
71
process . env . NFT_AUTO_REVIEW_ENABLED = 'true' ;
49
72
50
73
// Execution
51
- const result = NftUtils . shouldInvokeNftHandlerForTx ( tx ) ;
74
+ const result = NftUtils . shouldInvokeNftHandlerForTx ( tx , network , logger ) ;
52
75
53
76
// Assertion
54
77
expect ( result ) . toBe ( true ) ;
@@ -71,21 +94,21 @@ describe('isTransactionNFTCreation', () => {
71
94
// Incorrect version
72
95
tx = getTransaction ( ) ;
73
96
tx . version = hathorLib . constants . DEFAULT_TX_VERSION ;
74
- result = NftUtils . isTransactionNFTCreation ( tx ) ;
97
+ result = NftUtils . isTransactionNFTCreation ( tx , network , logger ) ;
75
98
expect ( result ) . toBe ( false ) ;
76
99
expect ( spyCreateTx ) . not . toHaveBeenCalled ( ) ;
77
100
78
101
// Missing name
79
102
tx = getTransaction ( ) ;
80
103
tx . token_name = undefined ;
81
- result = NftUtils . isTransactionNFTCreation ( tx ) ;
104
+ result = NftUtils . isTransactionNFTCreation ( tx , network , logger ) ;
82
105
expect ( result ) . toBe ( false ) ;
83
106
expect ( spyCreateTx ) . not . toHaveBeenCalled ( ) ;
84
107
85
108
// Missing symbol
86
109
tx = getTransaction ( ) ;
87
110
tx . token_symbol = undefined ;
88
- result = NftUtils . isTransactionNFTCreation ( tx ) ;
111
+ result = NftUtils . isTransactionNFTCreation ( tx , network , logger ) ;
89
112
expect ( result ) . toBe ( false ) ;
90
113
expect ( spyCreateTx ) . not . toHaveBeenCalled ( ) ;
91
114
@@ -102,7 +125,7 @@ describe('isTransactionNFTCreation', () => {
102
125
103
126
// Validation
104
127
const tx = getTransaction ( ) ;
105
- const result = NftUtils . isTransactionNFTCreation ( tx ) ;
128
+ const result = NftUtils . isTransactionNFTCreation ( tx , network , logger ) ;
106
129
expect ( result ) . toBe ( true ) ;
107
130
108
131
// Reverting mocks
@@ -114,7 +137,7 @@ describe('isTransactionNFTCreation', () => {
114
137
115
138
// Validation
116
139
const tx = getTransaction ( ) ;
117
- const result = NftUtils . isTransactionNFTCreation ( tx ) ;
140
+ const result = NftUtils . isTransactionNFTCreation ( tx , network , logger ) ;
118
141
expect ( result ) . toBe ( true ) ;
119
142
} ) ;
120
143
@@ -129,7 +152,7 @@ describe('isTransactionNFTCreation', () => {
129
152
130
153
// Validation
131
154
const tx = getTransaction ( ) ;
132
- const result = NftUtils . isTransactionNFTCreation ( tx ) ;
155
+ const result = NftUtils . isTransactionNFTCreation ( tx , network , logger ) ;
133
156
expect ( result ) . toBe ( false ) ;
134
157
135
158
// Reverting mocks
@@ -155,11 +178,11 @@ describe('createOrUpdateNftMetadata', () => {
155
178
const expectedUpdateResponse = { updated : 'ok' } ;
156
179
157
180
spyUpdateMetadata . mockImplementation ( async ( ) => expectedUpdateResponse ) ;
158
- const result = await NftUtils . createOrUpdateNftMetadata ( 'sampleUid' ) ;
181
+ const result = await NftUtils . createOrUpdateNftMetadata ( 'sampleUid' , 5 , logger ) ;
159
182
160
183
expect ( spyUpdateMetadata ) . toHaveBeenCalledTimes ( 1 ) ;
161
184
162
- expect ( spyUpdateMetadata ) . toHaveBeenCalledWith ( 'sampleUid' , expectedUpdateRequest ) ;
185
+ expect ( spyUpdateMetadata ) . toHaveBeenCalledWith ( 'sampleUid' , expectedUpdateRequest , 5 , logger ) ;
163
186
expect ( result ) . toBeUndefined ( ) ; // The method returns void
164
187
} ) ;
165
188
} ) ;
@@ -182,7 +205,7 @@ describe('_updateMetadata', () => {
182
205
const oldStage = process . env . STAGE ;
183
206
process . env . STAGE = 'dev' ; // Testing all code branches, including the developer ones, for increased coverage
184
207
185
- const result = await NftUtils . _updateMetadata ( 'sampleUid' , { sampleData : 'fake' } ) ;
208
+ const result = await NftUtils . _updateMetadata ( 'sampleUid' , { sampleData : 'fake' } , 5 , logger ) ;
186
209
expect ( result ) . toStrictEqual ( expectedLambdaResponse ) ;
187
210
process . env . STAGE = oldStage ;
188
211
} ) ;
@@ -198,7 +221,7 @@ describe('_updateMetadata', () => {
198
221
} ;
199
222
const mLambdaClient = new LambdaClientMock ( { } ) ;
200
223
( mLambdaClient . send as jest . Mocked < any > ) . mockImplementation ( async ( ) => {
201
- if ( failureCount < MAX_METADATA_UPDATE_RETRIES - 1 ) {
224
+ if ( failureCount < 4 ) {
202
225
++ failureCount ;
203
226
return {
204
227
StatusCode : 500 ,
@@ -208,7 +231,7 @@ describe('_updateMetadata', () => {
208
231
return expectedLambdaResponse ;
209
232
} ) ;
210
233
211
- const result = await NftUtils . _updateMetadata ( 'sampleUid' , { sampleData : 'fake' } ) ;
234
+ const result = await NftUtils . _updateMetadata ( 'sampleUid' , { sampleData : 'fake' } , 5 , logger ) ;
212
235
expect ( result ) . toStrictEqual ( expectedLambdaResponse ) ;
213
236
} ) ;
214
237
@@ -219,7 +242,7 @@ describe('_updateMetadata', () => {
219
242
let failureCount = 0 ;
220
243
const mLambdaClient = new LambdaClientMock ( { } ) ;
221
244
( mLambdaClient . send as jest . Mocked < any > ) . mockImplementation ( ( ) => {
222
- if ( failureCount < MAX_METADATA_UPDATE_RETRIES ) {
245
+ if ( failureCount < 5 ) {
223
246
++ failureCount ;
224
247
return {
225
248
StatusCode : 500 ,
@@ -233,7 +256,7 @@ describe('_updateMetadata', () => {
233
256
} ) ;
234
257
235
258
// eslint-disable-next-line jest/valid-expect
236
- expect ( NftUtils . _updateMetadata ( 'sampleUid' , { sampleData : 'fake' } ) )
259
+ expect ( NftUtils . _updateMetadata ( 'sampleUid' , { sampleData : 'fake' } , network , logger ) )
237
260
. rejects . toThrow ( new Error ( 'Metadata update failed for tx_id: sampleUid.' ) ) ;
238
261
} ) ;
239
262
} ) ;
@@ -250,7 +273,7 @@ describe('invokeNftHandlerLambda', () => {
250
273
const mLambdaClient = new LambdaClientMock ( { } ) ;
251
274
( mLambdaClient . send as jest . Mocked < any > ) . mockImplementationOnce ( async ( ) => expectedLambdaResponse ) ;
252
275
253
- await expect ( NftUtils . invokeNftHandlerLambda ( 'sampleUid' ) ) . resolves . toBeUndefined ( ) ;
276
+ await expect ( NftUtils . invokeNftHandlerLambda ( 'sampleUid' , 'local' , logger ) ) . resolves . toBeUndefined ( ) ;
254
277
} ) ;
255
278
256
279
it ( 'should throw when payload response status is invalid' , async ( ) => {
@@ -264,15 +287,15 @@ describe('invokeNftHandlerLambda', () => {
264
287
} ;
265
288
( mLambdaClient . send as jest . Mocked < any > ) . mockImplementation ( ( ) => expectedLambdaResponse ) ;
266
289
267
- await expect ( NftUtils . invokeNftHandlerLambda ( 'sampleUid' ) )
290
+ await expect ( NftUtils . invokeNftHandlerLambda ( 'sampleUid' , 'local' , logger ) )
268
291
. rejects . toThrow ( new Error ( 'onNewNftEvent lambda invoke failed for tx: sampleUid' ) ) ;
269
292
270
293
expect ( mockedAddAlert ) . toHaveBeenCalledWith (
271
294
'Error on NFTHandler lambda' ,
272
295
'Erroed on invokeNftHandlerLambda invocation' ,
273
296
Severity . MINOR ,
274
297
{ TxId : 'sampleUid' } ,
275
- expect . any ( Logger ) ,
298
+ logger ,
276
299
) ;
277
300
} ) ;
278
301
} ) ;
0 commit comments