forked from Devsol-01/Nestera
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain.controller.spec.ts
More file actions
165 lines (141 loc) · 5.27 KB
/
blockchain.controller.spec.ts
File metadata and controls
165 lines (141 loc) · 5.27 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
import { Test, TestingModule } from '@nestjs/testing';
import { BlockchainController } from './blockchain.controller';
import { StellarService } from './stellar.service';
import { BalanceSyncService } from './balance-sync.service';
import { TransactionDto } from './dto/transaction.dto';
import { TransactionBatchingService } from './transaction-batching.service';
import { TransactionBatchStatus } from './entities/transaction-batch.entity';
const MOCK_PUBLIC_KEY =
'GAAZI4TCR3TY5OJHCTJC2A4QSY6CJWJH5IAJTGKIN2ER7LBNVKOCCWN';
const MOCK_TRANSACTIONS: TransactionDto[] = [
{
date: '2024-01-15T10:30:00Z',
amount: '10.5000000',
token: 'XLM',
hash: 'abc123def456',
},
{
date: '2024-01-14T08:00:00Z',
amount: '25.0000000',
token: 'USDC',
hash: 'xyz789uvw012',
},
];
describe('BlockchainController', () => {
let controller: BlockchainController;
let stellarService: jest.Mocked<StellarService>;
let transactionBatchingService: jest.Mocked<TransactionBatchingService>;
beforeEach(async () => {
const mockStellarService: Partial<jest.Mocked<StellarService>> = {
generateKeypair: jest.fn().mockReturnValue({
publicKey: 'G_PUBLIC_KEY',
secretKey: 'S_SECRET',
}),
getRecentTransactions: jest.fn().mockResolvedValue(MOCK_TRANSACTIONS),
};
const mockBalanceSyncService = {
// Add any methods if needed, but since the controller doesn't use it in tests, empty is fine
getMetricsSummary: jest.fn().mockReturnValue({}),
};
const mockTransactionBatchingService = {
createAndProcessBatch: jest.fn().mockResolvedValue({
id: 'batch-1',
status: TransactionBatchStatus.COMPLETED,
requestedOperationCount: 1,
completedCount: 1,
failedCount: 0,
operations: [],
}),
getBatchStatus: jest.fn().mockResolvedValue({
id: 'batch-1',
status: TransactionBatchStatus.COMPLETED,
requestedOperationCount: 1,
completedCount: 1,
failedCount: 0,
operations: [],
}),
};
const module: TestingModule = await Test.createTestingModule({
controllers: [BlockchainController],
providers: [
{ provide: StellarService, useValue: mockStellarService },
{ provide: BalanceSyncService, useValue: mockBalanceSyncService },
{
provide: TransactionBatchingService,
useValue: mockTransactionBatchingService,
},
],
}).compile();
controller = module.get<BlockchainController>(BlockchainController);
stellarService = module.get(StellarService);
transactionBatchingService = module.get(TransactionBatchingService);
});
describe('getWalletTransactions', () => {
it('should call StellarService.getRecentTransactions with the correct public key', async () => {
await controller.getWalletTransactions(MOCK_PUBLIC_KEY);
expect(stellarService.getRecentTransactions).toHaveBeenCalledWith(
MOCK_PUBLIC_KEY,
);
});
it('should return the array returned by StellarService', async () => {
const result = await controller.getWalletTransactions(MOCK_PUBLIC_KEY);
expect(result).toBe(MOCK_TRANSACTIONS);
expect(result).toHaveLength(2);
});
it('should return an empty array when the service returns no transactions', async () => {
stellarService.getRecentTransactions.mockResolvedValue([]);
const result = await controller.getWalletTransactions(MOCK_PUBLIC_KEY);
expect(result).toEqual([]);
});
it('each returned item should have date, amount, token and hash fields', async () => {
const result = await controller.getWalletTransactions(MOCK_PUBLIC_KEY);
result.forEach((tx) => {
expect(tx).toHaveProperty('date');
expect(tx).toHaveProperty('amount');
expect(tx).toHaveProperty('token');
expect(tx).toHaveProperty('hash');
});
});
});
describe('generateWallet', () => {
it('should call StellarService.generateKeypair and return the result', () => {
const result = controller.generateWallet();
expect(stellarService.generateKeypair).toHaveBeenCalled();
expect(result).toMatchObject({
publicKey: expect.any(String),
secretKey: expect.any(String),
});
});
});
describe('transaction batches', () => {
it('should submit a batch without returning or persisting the source secret key in the controller', async () => {
const dto = {
sourceSecretKey: 'S_SECRET',
maxBatchSize: 10,
operations: [
{
contractId: 'C1',
functionName: 'deposit',
args: ['100'],
idempotencyKey: 'op-1',
},
],
};
const result = await controller.createBatch(dto);
expect(
transactionBatchingService.createAndProcessBatch,
).toHaveBeenCalledWith('S_SECRET', dto.operations, {
maxBatchSize: 10,
metadata: undefined,
});
expect(JSON.stringify(result)).not.toContain('S_SECRET');
});
it('should fetch batch status by id', async () => {
const result = await controller.getBatch('batch-1');
expect(transactionBatchingService.getBatchStatus).toHaveBeenCalledWith(
'batch-1',
);
expect(result).toMatchObject({ id: 'batch-1' });
});
});
});