add error handling and more tests

This commit is contained in:
jwinterm
2025-09-29 11:20:11 -04:00
parent c7cbdff021
commit 95711b9d26
14 changed files with 582 additions and 519 deletions
+30 -12
View File
@@ -1,30 +1,52 @@
const PaymentHandlers = require('../src/network/paymentHandlers');
function makeMocks() {
const user = { id: 'user-1', clientId: 'client1', payout_address: 'wow1address' };
return {
io: { to: () => ({ emit: () => {} }) },
gameModeManager: {
gameMode: 'PAID_SINGLE',
cryptoType: 'WOW',
currencyDecimals: 11,
singleGamePrice: 1000,
createPaymentRequest: async () => ({ id: 'pid123', address: 'addr', amount: 1000, expiresAt: new Date() })
creditsPayoutEnabled: false,
formatAtomicHuman: jest.fn(() => '0.001'),
getOrCreateUser: jest.fn(async () => user),
createPaymentRequest: async () => ({
id: 'pid123',
address: 'addr',
amount: 1000,
amountFormatted: '0.001',
expiresAt: new Date()
})
},
walletService: {
startPaymentMonitoring: (addr, cb) => { /* simulate nothing */ },
stopPaymentMonitoring: () => {}
startPaymentMonitoring: jest.fn(),
stopPaymentMonitoring: jest.fn()
},
debugManager: { getCurrentBlockHeight: () => 123, CONSOLE_LOGGING: false },
queueManager: {
getUserBySocket: () => ({ clientId: 'client1' }),
getUserBySocket: () => user,
getPlayerIndex: () => -1,
addPlayer: () => {},
markConfirmed: () => {}
getQueueLength: () => 0,
addPlayer: jest.fn(),
markConfirmed: jest.fn(),
startGameImmediately: jest.fn(() => false)
},
broadcastManager: { sendStatusUpdate: () => {} }
broadcastManager: { sendStatusUpdate: jest.fn() }
};
}
describe('PaymentHandlers basic', () => {
let ph;
afterEach(() => {
if (ph && typeof ph.dispose === 'function') {
ph.dispose();
}
ph = null;
});
test('createAndShowPaymentRequest emits payment_created', async () => {
const mocks = makeMocks();
const emitted = {};
@@ -33,10 +55,6 @@ describe('PaymentHandlers basic', () => {
await ph.createAndShowPaymentRequest({ id: 'sock1' });
expect(emitted.payment_created).toBeDefined();
expect(emitted.payment_created.paymentId).toBe('pid123');
});
afterAll(() => {
if (ph && typeof ph.dispose === 'function') {
ph.dispose();
}
expect(mocks.walletService.startPaymentMonitoring).toHaveBeenCalled();
});
});
+12 -2
View File
@@ -36,7 +36,7 @@ describe('Payment flow integration: confirm-after-block immediate start', () =>
};
// User lookup
const userObj = { id: socketId, clientId: 'client-int' };
const userObj = { id: socketId, clientId: 'client-int', payout_address: 'wow1address' };
const getUserBySocket = (id) => id === socketId ? userObj : null;
// createGameForUser mock (records active game & returns state)
@@ -52,8 +52,18 @@ describe('Payment flow integration: confirm-after-block immediate start', () =>
const gameModeManager = {
gameMode: 'PAID_SINGLE',
cryptoType: 'WOW',
currencyDecimals: 11,
singleGamePrice: 1000,
createPaymentRequest: async () => ({ id: 'pay-1', address: 'addr-1', amount: 1000, expiresAt: new Date(Date.now()+60000) })
creditsPayoutEnabled: false,
formatAtomicHuman: () => '0.001',
getOrCreateUser: async () => ({ id: socketId, payout_address: 'wow1address', clientId: 'client-int' }),
createPaymentRequest: async () => ({
id: 'pay-1',
address: 'addr-1',
amount: 1000,
amountFormatted: '0.001',
expiresAt: new Date(Date.now()+60000)
})
};
// Wallet service mock with manual callback control
+2 -1
View File
@@ -138,7 +138,8 @@ describe('SQL Injection Prevention', () => {
// Verify only safe fields were updated
const updatedUser = await sessionManager.getBySocket('test-update-socket');
expect(updatedUser.credits).toBe(50);
expect(updatedUser.id).toBe(testSession.user.id); // Should not have changed
expect(updatedUser.id).not.toBe(dangerousUpdates.id); // Should ignore malicious id change
expect(updatedUser.socket_id).toBe('test-update-socket');
});
test('should generate cryptographically secure tokens', () => {
+23
View File
@@ -4,6 +4,7 @@ const WalletRPCService = require('../src/payments/walletRPCService');
const debugManagerMock = { CONSOLE_LOGGING: false };
describe('WalletRPCService interface', () => {
test('exposes expected methods', () => {
const svc = new WalletRPCService(debugManagerMock);
expect(typeof svc.initialize).toBe('function');
@@ -20,4 +21,26 @@ describe('WalletRPCService interface', () => {
const res = await svc.processBatchPayouts();
expect(res).toEqual({ processed: 0 });
});
test('rpcCall wraps failures in ExternalServiceError', async () => {
const svc = new WalletRPCService(debugManagerMock);
svc.walletEndpoint = 'http://127.0.0.1:65535';
await expect(svc.rpcCall('get_height')).rejects.toMatchObject({
code: 'EXTERNAL_SERVICE_ERROR',
safeMessage: 'Wallet RPC call failed.'
});
});
test('createPaymentRequest rethrows wrapped errors', async () => {
const svc = new WalletRPCService(debugManagerMock);
svc.rpcCall = jest.fn().mockRejectedValue(new Error('rpc unavailable'));
await expect(
svc.createPaymentRequest(10, 'test payment', 'user-1', 'socket-1')
).rejects.toMatchObject({
code: 'EXTERNAL_SERVICE_ERROR',
safeMessage: 'Unable to create a payment address at this time.'
});
});
});