Files
nullcart/backend/src/modules/bitcoinWallet/services/BitcoinWalletAdminService.spec.ts
T

75 lines
2.4 KiB
TypeScript

import { ServiceUnavailableException } from '@nestjs/common';
import type { ConfigService } from '@nestjs/config';
import { BitcoinWalletSyncStatus } from '../types/BitcoinWalletSyncStatus';
import type { ElectrumWalletRpcClient } from './ElectrumWalletRpcClient';
import { BitcoinWalletAdminService } from './BitcoinWalletAdminService';
describe('BitcoinWalletAdminService', () => {
let service: BitcoinWalletAdminService;
let walletRpcClient: {
getVersion: jest.Mock;
isSynchronized: jest.Mock;
getInfo: jest.Mock;
getBalance: jest.Mock;
};
let configService: {
get: jest.Mock;
};
beforeEach(() => {
walletRpcClient = {
getVersion: jest.fn().mockResolvedValue('4.8.1'),
isSynchronized: jest.fn().mockResolvedValue(true),
getInfo: jest.fn().mockResolvedValue({
blockchain_height: 900_000,
server_height: 900_000
}),
getBalance: jest.fn().mockResolvedValue({
balanceAtomic: '150000',
confirmedBalanceAtomic: '150000'
})
};
configService = {
get: jest.fn().mockReturnValue({
network: 'testnet4'
})
};
service = new BitcoinWalletAdminService(
walletRpcClient as unknown as ElectrumWalletRpcClient,
configService as unknown as ConfigService
);
});
it('returns wallet status when RPC calls succeed', async () => {
const status = await service.getStatus();
expect(status).toEqual(
expect.objectContaining({
network: 'testnet4',
rpcVersion: '4.8.1',
blockHeight: 900_000,
serverHeight: 900_000,
syncStatus: BitcoinWalletSyncStatus.Synced,
balanceBtc: '0.00150000',
confirmedBalanceBtc: '0.00150000'
})
);
});
it('reports syncing when the wallet is not synchronized', async () => {
walletRpcClient.isSynchronized.mockResolvedValue(false);
const status = await service.getStatus();
expect(status.syncStatus).toBe(BitcoinWalletSyncStatus.Syncing);
});
it('throws when RPC calls fail', async () => {
walletRpcClient.getBalance.mockRejectedValue(new Error('rpc down'));
await expect(service.getStatus()).rejects.toBeInstanceOf(ServiceUnavailableException);
});
});