@@ -13,6 +13,8 @@ import { WebhooksEventTypes } from "../../../../src/shared/schemas/webhooks";
13
13
describe ( "Webhook callback Address Balance Listener" , ( ) => {
14
14
let testCallbackServer : FastifyInstance ;
15
15
let engine : ReturnType < typeof setupEngine > ;
16
+ // state to be updated by webhook callback
17
+ let webhookCallbackState = false ;
16
18
17
19
beforeAll ( async ( ) => {
18
20
engine = ( await setup ( ) ) . engine ;
@@ -23,8 +25,6 @@ describe("Webhook callback Address Balance Listener", () => {
23
25
await testCallbackServer . close ( ) ;
24
26
} ) ;
25
27
26
- // state to be updated by webhook callback
27
- let webhookCallbackState = false ;
28
28
const createTempCallbackServer = async ( ) => {
29
29
const tempServer = Fastify ( ) ;
30
30
@@ -60,102 +60,67 @@ describe("Webhook callback Address Balance Listener", () => {
60
60
} ) ;
61
61
} ;
62
62
63
+ const testWithThreshold = async (
64
+ thresholdBal : number ,
65
+ expectedOutput : string ,
66
+ ) => {
67
+ const originalStateValue = webhookCallbackState ;
68
+
69
+ const whWrote = (
70
+ await engine . webhooks . create ( {
71
+ url : "http://localhost:3006/callback" ,
72
+ name : "TEST:DELETE LATER:PAYMASTER BALANCE LIMIT NOTIFY" ,
73
+ eventType : WebhooksEventTypes . BACKEND_WALLET_BALANCE ,
74
+ config : {
75
+ address : "0xE52772e599b3fa747Af9595266b527A31611cebd" ,
76
+ chainId : 137 ,
77
+ threshold : thresholdBal ,
78
+ } ,
79
+ } )
80
+ ) ?. result ;
81
+
82
+ const whRead = ( await engine . webhooks . getAll ( ) ) . result . find (
83
+ ( wh ) => wh . id === whWrote . id ,
84
+ ) ;
85
+
86
+ // check if webhook is registered correctly
87
+ expect ( whRead ?. id ) . toEqual ( whWrote ?. id ) ;
88
+ expect ( whRead ?. config ?. address ) . toEqual ( whWrote ?. config ?. address ) ;
89
+ expect ( whRead ?. config ?. chainId ) . toEqual ( whWrote ?. config ?. chainId ) ;
90
+ expect ( whRead ?. config ?. threshold ) . toEqual ( whWrote ?. config ?. threshold ) ;
91
+
92
+ let testStatus : string ;
93
+ try {
94
+ const response = await checkTestStateChange ( ) ;
95
+ expect ( response . status ) . toEqual ( true ) ;
96
+ expect ( response . newValue ) . not . toEqual ( originalStateValue ) ;
97
+ expect ( response . newValue ) . toEqual ( webhookCallbackState ) ;
98
+ testStatus = "completed" ;
99
+ } catch ( e ) {
100
+ console . error ( e ) ;
101
+ testStatus = "webhook not called" ;
102
+ }
103
+
104
+ // todo: delete api doesn't exist atm so only revoke for now. Dont delete manually.
105
+ // await prisma.webhooks.delete({ where: { id: whRead?.id } });
106
+ await engine . webhooks . revoke ( { id : whRead . id } ) ;
107
+
108
+ // should not throw error
109
+ expect ( testStatus ) . toEqual ( expectedOutput ) ;
110
+ } ;
111
+
63
112
test (
64
113
"test should throw error as balance > threshold" ,
65
114
async ( ) => {
66
- const originalStateValue = webhookCallbackState ;
67
-
68
- const whWrote = (
69
- await engine . webhooks . create ( {
70
- url : "http://localhost:3006/callback" ,
71
- name : "TEST:DELETE LATER:PAYMASTER BALANCE LIMIT NOTIFY" ,
72
- eventType : WebhooksEventTypes . BACKEND_WALLET_BALANCE ,
73
- config : {
74
- address : "0xE52772e599b3fa747Af9595266b527A31611cebd" ,
75
- chainId : 137 ,
76
- threshold : 0.1 ,
77
- } ,
78
- } )
79
- ) ?. result ;
80
-
81
- const whRead = ( await engine . webhooks . getAll ( ) ) . result . find (
82
- ( wh ) => wh . id === whWrote . id ,
83
- ) ;
84
-
85
- // check if webhook is registered correctly
86
- expect ( whRead ?. id ) . toEqual ( whWrote ?. id ) ;
87
- expect ( whRead ?. config ?. address ) . toEqual ( whWrote ?. config ?. address ) ;
88
- expect ( whRead ?. config ?. chainId ) . toEqual ( whWrote ?. config ?. chainId ) ;
89
- expect ( whRead ?. config ?. threshold ) . toEqual ( whWrote ?. config ?. threshold ) ;
90
-
91
- let testStatus : string ;
92
- try {
93
- const response = await checkTestStateChange ( ) ;
94
- expect ( response . status ) . toEqual ( true ) ;
95
- expect ( response . newValue ) . not . toEqual ( originalStateValue ) ;
96
- expect ( response . newValue ) . toEqual ( webhookCallbackState ) ;
97
- testStatus = "completed" ;
98
- } catch ( e ) {
99
- console . error ( e ) ;
100
- testStatus = "webhook not called" ;
101
- }
102
-
103
- // todo: delete api doesn't exist atm so only revoke for now. Dont delete manually.
104
- // await prisma.webhooks.delete({ where: { id: whRead?.id } });
105
- await engine . webhooks . revoke ( { id : whRead . id } ) ;
106
-
107
- // should not throw error
108
- expect ( testStatus ) . toEqual ( "webhook not called" ) ;
115
+ await testWithThreshold ( 0.1 , "webhook not called" ) ;
109
116
} ,
110
117
1000 * 60 , // increase timeout
111
118
) ;
112
119
113
120
test (
114
121
"test should call webhook as balance < threshold" ,
115
122
async ( ) => {
116
- const originalStateValue = webhookCallbackState ;
117
-
118
- const whWrote = (
119
- await engine . webhooks . create ( {
120
- url : "http://localhost:3006/callback" ,
121
- name : "TEST:DELETE LATER:PAYMASTER BALANCE LIMIT NOTIFY" ,
122
- eventType : WebhooksEventTypes . BACKEND_WALLET_BALANCE ,
123
- config : {
124
- address : "0xE52772e599b3fa747Af9595266b527A31611cebd" ,
125
- chainId : 137 ,
126
- threshold : 2000 , // high number to make sure its tiggered
127
- } ,
128
- } )
129
- ) ?. result ;
130
-
131
- const whRead = ( await engine . webhooks . getAll ( ) ) . result . find (
132
- ( wh ) => wh . id === whWrote . id ,
133
- ) ;
134
-
135
- // check if webhook is registered correctly
136
- expect ( whRead ?. id ) . toEqual ( whWrote ?. id ) ;
137
- expect ( whRead ?. config ?. address ) . toEqual ( whWrote ?. config ?. address ) ;
138
- expect ( whRead ?. config ?. chainId ) . toEqual ( whWrote ?. config ?. chainId ) ;
139
- expect ( whRead ?. config ?. threshold ) . toEqual ( whWrote ?. config ?. threshold ) ;
140
-
141
- let testStatus : string ;
142
- try {
143
- const response = await checkTestStateChange ( ) ;
144
- expect ( response . status ) . toEqual ( true ) ;
145
- expect ( response . newValue ) . not . toEqual ( originalStateValue ) ;
146
- expect ( response . newValue ) . toEqual ( webhookCallbackState ) ;
147
- testStatus = "completed" ;
148
- } catch ( e ) {
149
- console . error ( e ) ;
150
- testStatus = "webhook not called" ;
151
- }
152
-
153
- // todo: delete api doesn't exist atm so only revoke for now. Dont delete manually.
154
- // await prisma.webhooks.delete({ where: { id: whRead?.id } });
155
- await engine . webhooks . revoke ( { id : whRead . id } ) ;
156
-
157
- // should not throw error
158
- expect ( testStatus ) . toEqual ( "completed" ) ;
123
+ await testWithThreshold ( 2000 , "completed" ) ;
159
124
} ,
160
125
1000 * 60 , // increase timeout
161
126
) ;
0 commit comments