forked from fidlabs/allocator-rkh-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsub-multisig-approvals-service.test.ts
More file actions
334 lines (277 loc) · 10.2 KB
/
sub-multisig-approvals-service.test.ts
File metadata and controls
334 lines (277 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import 'reflect-metadata';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { Container } from 'inversify';
import { ICommandBus, Logger } from '@filecoin-plus/core';
import { TYPES } from '@src/types';
import { SubMultisigApprovalsSubscriberService } from './sub-multisig-approvals-service';
import { IFilfoxClient, Message, Subcall } from '@src/infrastructure/clients/filfox';
import { IApplicationDetailsRepository } from '@src/infrastructure/repositories/application-details.repository';
import { IIssueDetailsRepository } from '@src/infrastructure/repositories/issue-details.repository';
import { SignRefreshByRKHCommand } from './sign-refresh-by-rkh.command';
import { UpdateRKHApprovalsCommand } from './update-rkh-approvals.command';
import { DatabaseRefreshFactory } from '@mocks/factories';
import { faker } from '@faker-js/faker';
import { RkhConfig } from '@src/infrastructure/interfaces';
vi.mock('@src/config', () => ({
default: {
SUBSCRIBE_MULTISIG_APPROVALS_POLLING_INTERVAL: 1000,
},
}));
describe('SubMultisigApprovalsSubscriberService', () => {
let container: Container;
let service: SubMultisigApprovalsSubscriberService;
const loggerMock = {
info: vi.fn((...args) => console.log(...args)),
error: vi.fn((...args) => console.error(...args)),
warn: vi.fn((...args) => console.warn(...args)),
};
const filfoxClientMock = {
getFilfoxMessages: vi.fn(),
getSubcalls: vi.fn(),
};
const multisigAddress = 'f03661530';
const rkhConfigMock = {
indirectRKHAddresses: [multisigAddress, 'f03661512'],
};
const commandBusMock = { send: vi.fn() };
const applicationDetailsRepositoryMock = { getByAddress: vi.fn() };
const issuesRepositoryMock = { findPendingBy: vi.fn() };
const verifierAddress = 'f410fw325e6novwl57jcsbhz6koljylxuhqq5jnp5ftq';
const messageCid = 'bafy2bzaceexample';
const fixtureMessage: Message = {
cid: messageCid,
height: 12345,
timestamp: Date.now(),
from: multisigAddress,
to: 'f080',
nonce: 1,
value: '0',
method: 'Approve',
evmMethod: '',
params: '',
receipt: {
exitCode: 0,
return: '',
},
};
const fixtureSubcall: Subcall = {
from: multisigAddress,
fromId: multisigAddress,
fromActor: 'multisig',
to: 'f080',
toId: 'f080',
toActor: 'multisig',
value: '0',
method: 'Propose',
methodNumber: 2,
params:
'0x84420006400258228256040ab6f5d279aead97dfa45209f3e53969c2ef43c21d490006bc000000000000',
receipt: {
exitCode: 0,
return: '',
},
decodedParams: {
Method: 2,
Params: '0x8256040ab6f5d279aead97dfa45209f3e53969c2ef43c21d490006bc000000000000',
To: 'f06',
Value: '0',
},
decodedReturnValue: {
TxId: 123,
Applied: false,
ExitCode: 0,
Ret: '0x',
},
subcalls: [],
};
const fixtureIssue = DatabaseRefreshFactory.create({
msigAddress: verifierAddress,
});
const fixtureApplicationDetails = {
id: faker.string.uuid(),
address: verifierAddress,
status: 'PENDING',
};
beforeEach(() => {
container = new Container();
container
.bind<ICommandBus>(TYPES.CommandBus)
.toConstantValue(commandBusMock as unknown as ICommandBus);
container.bind<Logger>(TYPES.Logger).toConstantValue(loggerMock as unknown as Logger);
container
.bind<IFilfoxClient>(TYPES.FilfoxClient)
.toConstantValue(filfoxClientMock as unknown as IFilfoxClient);
container
.bind<IApplicationDetailsRepository>(TYPES.ApplicationDetailsRepository)
.toConstantValue(
applicationDetailsRepositoryMock as unknown as IApplicationDetailsRepository,
);
container
.bind<IIssueDetailsRepository>(TYPES.IssueDetailsRepository)
.toConstantValue(issuesRepositoryMock as unknown as IIssueDetailsRepository);
container
.bind<RkhConfig>(TYPES.RkhConfig)
.toConstantValue(rkhConfigMock as unknown as RkhConfig);
container
.bind<SubMultisigApprovalsSubscriberService>(TYPES.SubMultisigApprovalsSubscriberService)
.to(SubMultisigApprovalsSubscriberService);
service = container.get<SubMultisigApprovalsSubscriberService>(
TYPES.SubMultisigApprovalsSubscriberService,
);
filfoxClientMock.getFilfoxMessages.mockResolvedValue({
messages: [fixtureMessage],
methods: ['Approve'],
totalCount: 1,
});
filfoxClientMock.getSubcalls.mockResolvedValue([fixtureSubcall]);
issuesRepositoryMock.findPendingBy.mockResolvedValue(fixtureIssue);
vi.useFakeTimers();
});
afterEach(() => {
vi.clearAllMocks();
service.stop();
vi.useRealTimers();
});
describe('start', () => {
it('should start the interval and return timeout id', () => {
const intervalId = service.start();
expect(intervalId).toBeDefined();
expect(loggerMock.info).toHaveBeenCalledWith(
'Starting SubMultisigApprovalsSubscriberService',
);
vi.useRealTimers();
});
it('should call handle method on interval', async () => {
const handleSpy = vi.spyOn(service, 'handle').mockResolvedValue();
service.start();
await vi.advanceTimersByTimeAsync(1000);
expect(handleSpy).toHaveBeenCalled();
vi.useRealTimers();
handleSpy.mockRestore();
});
});
describe('stop', () => {
it('should clear interval when stop is called', () => {
const intervalId = service.start();
expect(intervalId).toBeDefined();
service.stop();
expect(() => service.stop()).not.toThrow();
vi.useRealTimers();
});
});
describe('handle', () => {
it('should process proposals successfully when issue is found', async () => {
await service.handle();
await vi.advanceTimersByTimeAsync(1000);
expect(filfoxClientMock.getFilfoxMessages).toHaveBeenCalledWith(multisigAddress, {
pageSize: 50,
page: 0,
method: 'Approve',
});
expect(filfoxClientMock.getSubcalls).toHaveBeenCalledWith(messageCid);
expect(issuesRepositoryMock.findPendingBy).toHaveBeenCalledWith({
msigAddress: verifierAddress,
});
expect(commandBusMock.send).toHaveBeenCalledWith(expect.any(SignRefreshByRKHCommand));
});
it('should process proposals successfully when application is found (fallback)', async () => {
issuesRepositoryMock.findPendingBy.mockResolvedValue(null);
applicationDetailsRepositoryMock.getByAddress.mockResolvedValue(fixtureApplicationDetails);
await service.handle();
await vi.advanceTimersByTimeAsync(1000);
expect(applicationDetailsRepositoryMock.getByAddress).toHaveBeenCalledWith(verifierAddress);
expect(commandBusMock.send).toHaveBeenCalledWith(expect.any(UpdateRKHApprovalsCommand));
});
it('should handle empty messages', async () => {
filfoxClientMock.getFilfoxMessages.mockResolvedValue({
messages: [],
methods: [],
totalCount: 0,
});
await service.handle();
await vi.advanceTimersByTimeAsync(1000);
expect(filfoxClientMock.getSubcalls).not.toHaveBeenCalled();
expect(commandBusMock.send).not.toHaveBeenCalled();
});
it('should filter subcalls correctly', async () => {
const invalidSubcall: Subcall = {
...fixtureSubcall,
to: 'f099',
method: 'Other',
};
filfoxClientMock.getSubcalls.mockResolvedValue([fixtureSubcall, invalidSubcall]);
await service.handle();
await vi.advanceTimersByTimeAsync(1000);
expect(commandBusMock.send).toHaveBeenCalledTimes(2);
expect(commandBusMock.send).toHaveBeenCalledWith(expect.any(SignRefreshByRKHCommand));
});
it('should handle both handlers failing', async () => {
issuesRepositoryMock.findPendingBy.mockResolvedValue(null);
applicationDetailsRepositoryMock.getByAddress.mockResolvedValue(null);
await service.handle();
await vi.advanceTimersByTimeAsync(1000);
expect(loggerMock.error).toHaveBeenCalledWith('Both handlers failed:');
expect(commandBusMock.send).not.toHaveBeenCalled();
});
it('should process multiple subcalls', async () => {
const secondSubcall: Subcall = {
...fixtureSubcall,
decodedReturnValue: {
...fixtureSubcall.decodedReturnValue!,
TxId: 456,
},
};
filfoxClientMock.getSubcalls.mockResolvedValue([fixtureSubcall, secondSubcall]);
await service.handle();
await vi.advanceTimersByTimeAsync(1000);
expect(commandBusMock.send).toHaveBeenCalledTimes(4);
await vi.advanceTimersByTimeAsync(1000);
});
});
describe('getSubcallsWithProposeAddVerifier', () => {
it('should filter subcalls with correct attributes', async () => {
const validSubcall: Subcall = {
...fixtureSubcall,
to: 'f080',
method: 'Propose',
decodedParams: {
Method: 2,
Params: '0x8256040ab6f5d279aead97dfa45209f3e53969c2ef43c21d490006bc000000000000',
To: 'f06',
Value: '0',
},
toActor: 'multisig',
};
const invalidSubcall: Subcall = {
...fixtureSubcall,
to: 'f099',
method: 'Other',
decodedParams: {
Method: 3,
Params: '',
To: 'f06',
Value: '0',
},
};
filfoxClientMock.getSubcalls.mockResolvedValue([validSubcall, invalidSubcall]);
await service.handle();
await vi.advanceTimersByTimeAsync(1000);
expect(commandBusMock.send).toHaveBeenCalledTimes(2);
});
});
describe('processProposal', () => {
it('should create SignRefreshByRKHCommand when issue exists', async () => {
await service.handle();
await vi.advanceTimersByTimeAsync(1000);
const sentCommand = commandBusMock.send.mock.calls[0][0];
expect(sentCommand).toBeInstanceOf(SignRefreshByRKHCommand);
});
it('should create UpdateRKHApprovalsCommand when application exists', async () => {
issuesRepositoryMock.findPendingBy.mockResolvedValue(null);
applicationDetailsRepositoryMock.getByAddress.mockResolvedValue(fixtureApplicationDetails);
await service.handle();
const sentCommand = commandBusMock.send.mock.calls[0][0];
expect(sentCommand).toBeInstanceOf(UpdateRKHApprovalsCommand);
});
});
});