@@ -2,6 +2,7 @@ import 'should';
22import { BitGoAPI } from '../../src/bitgoAPI' ;
33import { ProxyAgent } from 'proxy-agent' ;
44import * as sinon from 'sinon' ;
5+ import nock from 'nock' ;
56
67describe ( 'Constructor' , function ( ) {
78 describe ( 'cookiesPropagationEnabled argument' , function ( ) {
@@ -305,4 +306,102 @@ describe('Constructor', function () {
305306 result . should . containDeep ( [ 'wallet-id-2' , 'wallet-id-4' ] ) ;
306307 } ) ;
307308 } ) ;
309+
310+ describe ( 'constants parameter' , function ( ) {
311+ it ( 'should allow passing constants via options and expose via fetchConstants' , async function ( ) {
312+ const bitgo = new BitGoAPI ( {
313+ env : 'custom' ,
314+ customRootURI : 'https://app.example.local' ,
315+ clientConstants : { maxFeeRate : '123123123123123' } ,
316+ } ) ;
317+
318+ const constants = await bitgo . fetchConstants ( ) ;
319+ constants . should . have . property ( 'maxFeeRate' , '123123123123123' ) ;
320+ } ) ;
321+
322+ it ( 'should refresh constants when cache has expired' , async function ( ) {
323+ const bitgo = new BitGoAPI ( {
324+ env : 'custom' ,
325+ customRootURI : 'https://app.example.local' ,
326+ } ) ;
327+
328+ // Set up cached constants with an expired cache
329+ ( BitGoAPI as any ) . _constants = ( BitGoAPI as any ) . _constants || { } ;
330+ ( BitGoAPI as any ) . _constantsExpire = ( BitGoAPI as any ) . _constantsExpire || { } ;
331+ ( BitGoAPI as any ) . _constants [ 'custom' ] = { maxFeeRate : 'old-value' } ;
332+ ( BitGoAPI as any ) . _constantsExpire [ 'custom' ] = new Date ( Date . now ( ) - 1000 ) ; // Expired 1 second ago
333+
334+ const scope = nock ( 'https://app.example.local' )
335+ . get ( '/api/v1/client/constants' )
336+ . reply ( 200 , {
337+ constants : { maxFeeRate : 'new-value' , newConstant : 'added' } ,
338+ } ) ;
339+
340+ const constants = await bitgo . fetchConstants ( ) ;
341+
342+ // Should return the new constants from the server
343+ constants . should . have . property ( 'maxFeeRate' , 'new-value' ) ;
344+ constants . should . have . property ( 'newConstant' , 'added' ) ;
345+
346+ scope . isDone ( ) . should . be . true ( ) ;
347+
348+ nock . cleanAll ( ) ;
349+ } ) ;
350+
351+ it ( 'should use cached constants when cache is still valid' , async function ( ) {
352+ const bitgo = new BitGoAPI ( {
353+ env : 'custom' ,
354+ customRootURI : 'https://app.example.local' ,
355+ } ) ;
356+
357+ // Set up cached constants with a future expiry
358+ const cachedConstants = { maxFeeRate : 'cached-value' , anotherSetting : 'cached-setting' } ;
359+ ( BitGoAPI as any ) . _constants = ( BitGoAPI as any ) . _constants || { } ;
360+ ( BitGoAPI as any ) . _constantsExpire = ( BitGoAPI as any ) . _constantsExpire || { } ;
361+ ( BitGoAPI as any ) . _constants [ 'custom' ] = cachedConstants ;
362+ ( BitGoAPI as any ) . _constantsExpire [ 'custom' ] = new Date ( Date . now ( ) + 5 * 60 * 1000 ) ; // Valid for 5 more minutes
363+
364+ const scope = nock ( 'https://app.example.local' )
365+ . get ( '/api/v1/client/constants' )
366+ . reply ( 200 , { constants : { shouldNotBeUsed : true } } ) ;
367+
368+ const constants = await bitgo . fetchConstants ( ) ;
369+
370+ // Should return the cached constants
371+ constants . should . deepEqual ( cachedConstants ) ;
372+
373+ // Verify that no HTTP request was made (since cache was valid)
374+ scope . isDone ( ) . should . be . false ( ) ;
375+
376+ nock . cleanAll ( ) ;
377+ } ) ;
378+
379+ it ( 'should use cached constants when no cache expiry is set' , async function ( ) {
380+ const bitgo = new BitGoAPI ( {
381+ env : 'custom' ,
382+ customRootURI : 'https://app.example.local' ,
383+ } ) ;
384+
385+ // Set up cached constants with no expiry
386+ const cachedConstants = { maxFeeRate : 'no-expiry-value' } ;
387+ ( BitGoAPI as any ) . _constants = ( BitGoAPI as any ) . _constants || { } ;
388+ ( BitGoAPI as any ) . _constantsExpire = ( BitGoAPI as any ) . _constantsExpire || { } ;
389+ ( BitGoAPI as any ) . _constants [ 'custom' ] = cachedConstants ;
390+ ( BitGoAPI as any ) . _constantsExpire [ 'custom' ] = undefined ;
391+
392+ const scope = nock ( 'https://app.example.local' )
393+ . get ( '/api/v1/client/constants' )
394+ . reply ( 200 , { constants : { shouldNotBeUsed : true } } ) ;
395+
396+ const constants = await bitgo . fetchConstants ( ) ;
397+
398+ // Should return the cached constants
399+ constants . should . deepEqual ( cachedConstants ) ;
400+
401+ // Verify that no HTTP request was made (since no expiry means cache is always valid)
402+ scope . isDone ( ) . should . be . false ( ) ;
403+
404+ nock . cleanAll ( ) ;
405+ } ) ;
406+ } ) ;
308407} ) ;
0 commit comments