forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransactionService.test.js
More file actions
219 lines (193 loc) · 6.53 KB
/
Copy pathtransactionService.test.js
File metadata and controls
219 lines (193 loc) · 6.53 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
process.env.ISSUER_PUBLIC = 'GDQGIY5T5QULPD7V54LJODKC5CMKPNGTWVEMYBQH4LV6STKI6IGO543K';
process.env.HORIZON_URL = 'https://horizon-testnet.stellar.org';
process.env.STELLAR_NETWORK = 'testnet';
jest.mock('../../blockchain/stellarService', () => ({
server: {
transactions: jest.fn(),
payments: jest.fn(),
},
NOVA: {
code: 'NOVA',
issuer: 'GDQGIY5T5QULPD7V54LJODKC5CMKPNGTWVEMYBQH4LV6STKI6IGO543K',
},
isValidStellarAddress: jest.fn((value) => typeof value === 'string' && value.startsWith('G')),
}));
jest.mock('../db/transactionRepository', () => ({
recordTransaction: jest.fn(),
getTransactionByHash: jest.fn(),
getTransactionsByUser: jest.fn(),
getTransactionHistory: jest.fn(),
processRefund: jest.fn(),
reconcileTransactions: jest.fn(),
getTransactionReport: jest.fn(),
}));
jest.mock('../db/index', () => ({
query: jest.fn(),
}));
jest.mock('../db/userRepository', () => ({
getUserById: jest.fn(),
}));
const { server } = require('../../blockchain/stellarService');
const repository = require('../db/transactionRepository');
const { query } = require('../db/index');
const { getUserById } = require('../db/userRepository');
const service = require('../services/transactionService');
const { TransactionFactory, UserFactory, STELLAR_ADDRESSES } = require('./fixtures');
beforeEach(() => jest.clearAllMocks());
describe('transactionService.recordTransaction', () => {
test('validates and records a Stellar-backed transaction', async () => {
const tx = TransactionFactory.distribution({ tx_hash: 'abc123', amount: '10.0000000' });
server.transactions.mockReturnValue({
transaction: jest.fn().mockReturnValue({
call: jest.fn().mockResolvedValue({ ledger_attr: 12345 }),
}),
});
repository.recordTransaction.mockResolvedValue(tx);
const result = await service.recordTransaction({
txHash: tx.tx_hash,
txType: tx.tx_type,
amount: tx.amount,
fromWallet: STELLAR_ADDRESSES[0],
toWallet: STELLAR_ADDRESSES[1],
merchantId: tx.merchant_id,
userId: 2,
metadata: { channel: 'pos' },
});
expect(result.tx_hash).toBe('abc123');
expect(repository.recordTransaction).toHaveBeenCalledWith(expect.objectContaining({
txHash: 'abc123',
txType: 'distribution',
stellarLedger: 12345,
metadata: { channel: 'pos' },
}));
});
test('rejects invalid amount values', async () => {
await expect(service.recordTransaction({
txHash: 'abc123',
txType: 'distribution',
amount: '-1',
})).rejects.toMatchObject({
status: 400,
code: 'validation_error',
});
});
});
describe('transactionService.getUserHistory', () => {
test('loads user history with validated filters', async () => {
const user = UserFactory.build({ id: 1 });
const tx = TransactionFactory.build({ tx_hash: 'abc123' });
getUserById.mockResolvedValue(user);
repository.getTransactionsByUser.mockResolvedValue({
data: [tx],
total: 1,
page: 1,
limit: 20,
});
const result = await service.getUserHistory({
userId: '1',
status: 'completed',
startDate: '2026-03-01',
endDate: '2026-03-31',
});
expect(result.total).toBe(1);
expect(repository.getTransactionsByUser).toHaveBeenCalledWith(1, expect.objectContaining({
status: 'completed',
page: 1,
limit: 20,
}));
});
test('rejects reversed date ranges', async () => {
await expect(service.getUserHistory({
userId: '1',
startDate: '2026-03-31',
endDate: '2026-03-01',
})).rejects.toMatchObject({
status: 400,
code: 'validation_error',
});
});
});
describe('transactionService.getWalletHistory', () => {
test('falls back to PostgreSQL when Horizon is unavailable', async () => {
server.payments.mockReturnValue({
forAccount: jest.fn().mockReturnValue({
order: jest.fn().mockReturnValue({
limit: jest.fn().mockReturnValue({
call: jest.fn().mockRejectedValue(new Error('network error')),
}),
}),
}),
});
query.mockResolvedValue({ rows: [{ tx_hash: 'db-only' }] });
const result = await service.getWalletHistory('GWALLET');
expect(result.source).toBe('database');
expect(query).toHaveBeenCalledTimes(1);
});
});
describe('transactionService.refundTransaction', () => {
test('validates ownership and creates a refund record', async () => {
repository.getTransactionByHash.mockResolvedValue({
tx_hash: 'sale-1',
tx_type: 'distribution',
merchant_id: 9,
status: 'completed',
amount: '10.0000000',
});
server.transactions.mockReturnValue({
transaction: jest.fn().mockReturnValue({
call: jest.fn().mockResolvedValue({ ledger_attr: 4321 }),
}),
});
repository.processRefund.mockResolvedValue({
originalTransaction: { tx_hash: 'sale-1', status: 'refunded' },
refundTransaction: { tx_hash: 'refund-1', tx_type: 'refund' },
});
const result = await service.refundTransaction(9, {
txHash: 'sale-1',
refundTxHash: 'refund-1',
reason: 'Customer request',
});
expect(result.refundTransaction.tx_hash).toBe('refund-1');
expect(repository.processRefund).toHaveBeenCalledWith(expect.objectContaining({
txHash: 'sale-1',
refundTxHash: 'refund-1',
refundReason: 'Customer request',
stellarLedger: 4321,
}));
});
});
describe('transactionService merchant lifecycle helpers', () => {
test('reconciles merchant transactions', async () => {
repository.reconcileTransactions.mockResolvedValue({
count: 2,
totalAmount: '20.0000000',
transactions: [],
});
const result = await service.reconcileMerchantTransactions(4, {
startDate: '2026-03-01',
endDate: '2026-03-31',
status: 'completed',
});
expect(result.count).toBe(2);
expect(repository.reconcileTransactions).toHaveBeenCalledWith(expect.objectContaining({
merchantId: 4,
status: 'completed',
}));
});
test('builds merchant transaction reports', async () => {
repository.getTransactionReport.mockResolvedValue({
summary: { total_transactions: '4' },
breakdown: [],
});
const result = await service.getMerchantTransactionReport(7, {
reconciled: 'false',
type: 'distribution',
});
expect(result.summary.total_transactions).toBe('4');
expect(repository.getTransactionReport).toHaveBeenCalledWith(expect.objectContaining({
merchantId: 7,
type: 'distribution',
reconciled: false,
}));
});
});