-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathPayout.spec.ts
More file actions
269 lines (240 loc) · 7.67 KB
/
Copy pathPayout.spec.ts
File metadata and controls
269 lines (240 loc) · 7.67 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
import { PayoutModule } from '../modules/Payout';
import { Database } from '../modules/Database';
import { Payout } from '@zoneless/shared-types';
import {
CreateMockDatabase,
DeterministicId,
ResetIdCounter,
GetFixedTimestamp,
} from './Setup';
jest.mock('../modules/Database');
jest.mock('../utils/IdGenerator', () => ({
GenerateId: jest.fn((prefix: string) => DeterministicId(prefix)),
}));
jest.mock('../utils/Timestamp', () => ({
Now: jest.fn(() => GetFixedTimestamp()),
}));
jest.mock('../modules/AppConfig', () => ({
GetAppConfig: jest.fn(() => ({
dashboardUrl: 'http://localhost:4200',
livemode: false,
appSecret: 'test-secret',
})),
}));
jest.mock('../modules/chains/Settlement', () => ({
GetSettlement: () => ({
CheckWalletExists: jest.fn().mockResolvedValue(true),
GetUSDCBalance: jest.fn().mockResolvedValue(100),
GetSOLBalance: jest.fn().mockResolvedValue(1),
BuildBatchPayoutTransaction: jest.fn().mockResolvedValue({
unsigned_transaction: 'base64tx',
estimated_fee_lamports: 5000,
blockhash: 'blockhash123',
last_valid_block_height: 100000,
recipients_count: 1,
}),
BroadcastSignedTransaction: jest.fn().mockResolvedValue({
status: 'paid',
signature: 'sig123',
viewer_url: 'https://solscan.io/tx/sig123',
}),
}),
IsSimulatedSettlement: jest.fn(() => false),
}));
describe('PayoutModule', () => {
let module: PayoutModule;
let mockDb: jest.Mocked<Database>;
beforeEach(() => {
jest.clearAllMocks();
ResetIdCounter();
mockDb = CreateMockDatabase();
module = new PayoutModule(mockDb);
});
describe('PayoutObject', () => {
it('should build a payout with correct fields', () => {
const payout = module.PayoutObject({
account: 'acct_z_seller',
platformAccountId: 'acct_z_platform',
amount: 5000,
currency: 'usdc',
destination: 'wa_z_1',
description: 'Weekly payout',
method: 'instant',
metadata: {},
});
expect(payout.object).toBe('payout');
expect(payout.amount).toBe(5000);
expect(payout.currency).toBe('usdc');
expect(payout.account).toBe('acct_z_seller');
expect(payout.platform_account).toBe('acct_z_platform');
expect(payout.destination).toBe('wa_z_1');
expect(payout.status).toBe('pending');
expect(payout.method).toBe('instant');
expect(payout.type).toBe('wallet');
expect(payout.source_type).toBe('wallet');
expect(payout.id).toMatch(/^po_z_test/);
});
it('should default to instant method', () => {
const payout = module.PayoutObject({
account: 'acct_z_1',
platformAccountId: 'acct_z_platform',
amount: 100,
currency: 'usdc',
destination: 'wa_z_1',
});
expect(payout.method).toBe('instant');
});
it('should default automatic to false', () => {
const payout = module.PayoutObject({
account: 'acct_z_1',
platformAccountId: 'acct_z_platform',
amount: 100,
currency: 'usdc',
destination: 'wa_z_1',
});
expect(payout.automatic).toBe(false);
});
});
describe('GetPayout', () => {
it('should return the payout when found', async () => {
const mockPayout = { id: 'po_z_1', object: 'payout' } as Payout;
mockDb.Get = jest.fn().mockResolvedValue(mockPayout);
const result = await module.GetPayout('po_z_1');
expect(result).toEqual(mockPayout);
});
it('should return null when not found', async () => {
const result = await module.GetPayout('nonexistent');
expect(result).toBeNull();
});
});
describe('CreatePayout', () => {
it('should reject when payouts are not enabled', async () => {
jest
.spyOn((module as any).accountModule, 'GetAccount')
.mockResolvedValue({
id: 'acct_z_seller',
payouts_enabled: false,
});
await expect(
module.CreatePayout('acct_z_seller', {
amount: 1000,
currency: 'usdc',
})
).rejects.toThrow(/Payouts are not enabled/);
});
});
describe('UpdatePayout', () => {
it('should update payout metadata', async () => {
const existingPayout = {
id: 'po_z_1',
object: 'payout',
account: 'acct_z_1',
metadata: {},
} as Payout;
mockDb.Get = jest.fn().mockResolvedValue(existingPayout);
const result = await module.UpdatePayout('po_z_1', {
metadata: { tracking: '12345' },
});
expect(mockDb.Update).toHaveBeenCalledWith(
'Payouts',
'po_z_1',
expect.objectContaining({ metadata: { tracking: '12345' } })
);
expect(result).toEqual(existingPayout);
});
it('should throw when payout not found', async () => {
await expect(
module.UpdatePayout('nonexistent', { metadata: {} })
).rejects.toThrow('Payout not found');
});
});
describe('CancelPayout', () => {
it('should cancel a pending payout and refund the balance', async () => {
const pendingPayout = {
id: 'po_z_1',
object: 'payout',
status: 'pending',
amount: 1000,
currency: 'usdc',
account: 'acct_z_1',
balance_transaction: 'txn_z_1',
} as Payout;
const balance = {
id: 'bal_z_1',
available: [{ amount: 0, currency: 'usdc' }],
pending: [],
};
mockDb.Get = jest
.fn()
.mockResolvedValueOnce(pendingPayout) // GetPayout
.mockResolvedValue(pendingPayout); // subsequent gets
mockDb.Find = jest.fn().mockResolvedValue([balance]);
const result = await module.CancelPayout('po_z_1');
expect(mockDb.Update).toHaveBeenCalledWith(
'Payouts',
'po_z_1',
{ status: 'canceled' },
expect.anything()
);
});
it('should throw when payout not found', async () => {
await expect(module.CancelPayout('nonexistent')).rejects.toThrow(
'Payout not found'
);
});
it('should throw when payout is not pending', async () => {
const paidPayout = {
id: 'po_z_1',
status: 'paid',
} as Payout;
mockDb.Get = jest.fn().mockResolvedValue(paidPayout);
await expect(module.CancelPayout('po_z_1')).rejects.toThrow(
'Payout cannot be canceled'
);
});
});
describe('BuildPayoutsBatch', () => {
it('keeps payouts pending while waiting for a client signature', async () => {
const payout = {
id: 'po_z_1',
object: 'payout',
account: 'acct_z_seller',
platform_account: 'acct_z_platform',
amount: 1000,
currency: 'usdc',
destination: 'wa_z_seller',
status: 'pending',
} as Payout;
jest.spyOn(module, 'GetPayout').mockResolvedValue(payout);
jest
.spyOn((module as any).accountModule, 'GetAccount')
.mockResolvedValue({
id: 'acct_z_seller',
platform_account: 'acct_z_platform',
});
jest
.spyOn((module as any).externalWalletModule, 'GetExternalWallet')
.mockResolvedValue({
id: 'wa_z_seller',
wallet_address: 'seller_wallet',
});
jest
.spyOn(
(module as any).externalWalletModule,
'GetExternalWalletsByAccount'
)
.mockResolvedValue([
{
id: 'wa_z_platform',
wallet_address: 'platform_wallet',
default_for_currency: true,
},
]);
const result = await module.BuildPayoutsBatch('acct_z_platform', {
payouts: [payout.id],
});
expect(result.payouts).toEqual([payout]);
expect(mockDb.Update).not.toHaveBeenCalled();
});
});
});