Skip to content

Commit 8486fd2

Browse files
committed
minor refactoring
1 parent 1a458ef commit 8486fd2

File tree

1 file changed

+53
-88
lines changed

1 file changed

+53
-88
lines changed

tests/e2e/tests/workers/address-balance-listener.test.ts

Lines changed: 53 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { WebhooksEventTypes } from "../../../../src/shared/schemas/webhooks";
1313
describe("Webhook callback Address Balance Listener", () => {
1414
let testCallbackServer: FastifyInstance;
1515
let engine: ReturnType<typeof setupEngine>;
16+
// state to be updated by webhook callback
17+
let webhookCallbackState = false;
1618

1719
beforeAll(async () => {
1820
engine = (await setup()).engine;
@@ -23,8 +25,6 @@ describe("Webhook callback Address Balance Listener", () => {
2325
await testCallbackServer.close();
2426
});
2527

26-
// state to be updated by webhook callback
27-
let webhookCallbackState = false;
2828
const createTempCallbackServer = async () => {
2929
const tempServer = Fastify();
3030

@@ -60,102 +60,67 @@ describe("Webhook callback Address Balance Listener", () => {
6060
});
6161
};
6262

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+
63112
test(
64113
"test should throw error as balance > threshold",
65114
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");
109116
},
110117
1000 * 60, // increase timeout
111118
);
112119

113120
test(
114121
"test should call webhook as balance < threshold",
115122
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");
159124
},
160125
1000 * 60, // increase timeout
161126
);

0 commit comments

Comments
 (0)