@@ -819,6 +819,76 @@ describe('cache-control tests', () => {
819
819
expect ( request8 . text ) . toBe ( '' ) ;
820
820
} ) ;
821
821
822
+ test ( 'principal mempool cache on received tx balance confirmation' , async ( ) => {
823
+ const address = 'SP3FXEKSA6D4BW3TFP2BWTSREV6FY863Y90YY7D8G' ;
824
+ const url = `/extended/v1/address/${ address } /balances` ;
825
+ await db . update (
826
+ new TestBlockBuilder ( {
827
+ block_height : 1 ,
828
+ index_block_hash : '0x01' ,
829
+ parent_index_block_hash : '0x00' ,
830
+ } ) . build ( )
831
+ ) ;
832
+
833
+ // ETag zero.
834
+ const request1 = await supertest ( api . server ) . get ( url ) ;
835
+ expect ( request1 . status ) . toBe ( 200 ) ;
836
+ expect ( request1 . type ) . toBe ( 'application/json' ) ;
837
+ const etag0 = request1 . headers [ 'etag' ] ;
838
+
839
+ // Add receiving STX tx.
840
+ await db . updateMempoolTxs ( {
841
+ mempoolTxs : [
842
+ testMempoolTx ( {
843
+ tx_id : '0x0001' ,
844
+ token_transfer_amount : 2000n ,
845
+ token_transfer_recipient_address : address ,
846
+ } ) ,
847
+ ] ,
848
+ } ) ;
849
+
850
+ // Valid ETag.
851
+ const request2 = await supertest ( api . server ) . get ( url ) ;
852
+ expect ( request2 . status ) . toBe ( 200 ) ;
853
+ expect ( request2 . type ) . toBe ( 'application/json' ) ;
854
+ expect ( request2 . headers [ 'etag' ] ) . toBeTruthy ( ) ;
855
+ const json2 = JSON . parse ( request2 . text ) ;
856
+ expect ( json2 . stx . balance ) . toBe ( '0' ) ;
857
+ expect ( json2 . stx . estimated_balance ) . toBe ( '2000' ) ;
858
+ const etag1 = request2 . headers [ 'etag' ] ;
859
+ expect ( etag1 ) . not . toEqual ( etag0 ) ;
860
+
861
+ // Cache works with valid ETag.
862
+ const request3 = await supertest ( api . server ) . get ( url ) . set ( 'If-None-Match' , etag1 ) ;
863
+ expect ( request3 . status ) . toBe ( 304 ) ;
864
+ expect ( request3 . text ) . toBe ( '' ) ;
865
+
866
+ // Confirm mempool tx.
867
+ await db . update (
868
+ new TestBlockBuilder ( {
869
+ block_height : 2 ,
870
+ index_block_hash : '0x02' ,
871
+ parent_index_block_hash : '0x01' ,
872
+ } )
873
+ . addTx ( {
874
+ tx_id : '0x0001' ,
875
+ token_transfer_amount : 2000n ,
876
+ token_transfer_recipient_address : address ,
877
+ } )
878
+ . addTxStxEvent ( { amount : 2000n , recipient : address } )
879
+ . build ( )
880
+ ) ;
881
+
882
+ // Cache is now a miss.
883
+ const request4 = await supertest ( api . server ) . get ( url ) . set ( 'If-None-Match' , etag1 ) ;
884
+ expect ( request4 . status ) . toBe ( 200 ) ;
885
+ expect ( request4 . type ) . toBe ( 'application/json' ) ;
886
+ expect ( request4 . headers [ 'etag' ] ) . not . toEqual ( etag1 ) ;
887
+ const json4 = JSON . parse ( request4 . text ) ;
888
+ expect ( json4 . stx . balance ) . toBe ( '2000' ) ;
889
+ expect ( json4 . stx . estimated_balance ) . toBe ( '2000' ) ;
890
+ } ) ;
891
+
822
892
test ( 'block cache control' , async ( ) => {
823
893
await db . update (
824
894
new TestBlockBuilder ( {
0 commit comments