import { describe, test, expect } from 'vitest';
import { validateAddress, validateAmount } from '../src/simple-tools.js';
describe('Bridge Tools', () => {
describe('Parameter Validation', () => {
test('rejects zero address', () => {
expect(() => validateAddress('0x0000000000000000000000000000000000000000'))
.toThrow('Zero address is not allowed');
});
test('rejects invalid hex amounts', () => {
expect(() => validateAmount('not-hex'))
.toThrow('Amount must be hex string');
});
test('accepts valid hex amounts', () => {
expect(() => validateAmount('0x1000000000000000000')).not.toThrow();
});
test('rejects zero amounts', () => {
expect(() => validateAmount('0x0'))
.toThrow('Amount must be positive');
});
test('accepts valid addresses', () => {
expect(() => validateAddress('0x742d35cc6634c0532925a3b8d4c9db96c4b4d8b6')).not.toThrow();
});
});
describe('Transaction Generation', () => {
test('generates correct ETH bridge transaction structure', () => {
// This would test the generateBridgeTransaction function
// For now, we'll test the structure validation
const mockTransaction = {
to: '0x4Dbd4fc535Ac27206064B68FfCf827b0A60BAB3f',
data: {
abi: [],
functionName: 'createRetryableTicket',
args: []
},
value: '0x1000000000000000000'
};
expect(mockTransaction.to).toMatch(/^0x[a-fA-F0-9]{40}$/);
expect(mockTransaction.data).toHaveProperty('abi');
expect(mockTransaction.data).toHaveProperty('functionName');
expect(mockTransaction.data).toHaveProperty('args');
});
test('ERC20 bridge uses gateway router', () => {
const mockErc20Transaction = {
to: '0x72Ce9c846789fdB6fC1f34aC4AD25Dd9ef7031ef', // L1 Gateway Router
data: {
abi: [],
functionName: 'outboundTransfer',
args: []
},
value: '0x0'
};
expect(mockErc20Transaction.to).toMatch(/^0x[a-fA-F0-9]{40}$/);
expect(mockErc20Transaction.value).toBe('0x0');
});
});
describe('Gas Estimation', () => {
test('respects maximum gas limits', () => {
const MAX_GAS_LIMIT = BigInt(5000000);
const highGasEstimate = BigInt(6000000);
// Simulate gas limit enforcement
const safeGasLimit = highGasEstimate > MAX_GAS_LIMIT ? MAX_GAS_LIMIT : highGasEstimate;
expect(safeGasLimit).toBeLessThanOrEqual(MAX_GAS_LIMIT);
});
test('handles gas estimation failures gracefully', () => {
// Test that gas estimation failures don't crash the system
const fallbackGas = '200000';
expect(fallbackGas).toBe('200000');
});
});
describe('Security Features', () => {
test('enforces maximum amount limits', () => {
const MAX_ETH_AMOUNT = BigInt('100000000000000000000'); // 100 ETH
const largeAmount = BigInt('200000000000000000000'); // 200 ETH
expect(largeAmount).toBeGreaterThan(MAX_ETH_AMOUNT);
});
test('validates contract addresses', () => {
const zeroAddress = '0x0000000000000000000000000000000000000000';
const validAddress = '0x4Dbd4fc535Ac27206064B68FfCf827b0A60BAB3f';
expect(zeroAddress).toBe('0x0000000000000000000000000000000000000000');
expect(validAddress).toMatch(/^0x[a-fA-F0-9]{40}$/);
});
});
});