Files
smom-dbis-138/tests/metamask-integration.test.ts
defiQUG 1fb7266469 Add Oracle Aggregator and CCIP Integration
- Introduced Aggregator.sol for Chainlink-compatible oracle functionality, including round-based updates and access control.
- Added OracleWithCCIP.sol to extend Aggregator with CCIP cross-chain messaging capabilities.
- Created .gitmodules to include OpenZeppelin contracts as a submodule.
- Developed a comprehensive deployment guide in NEXT_STEPS_COMPLETE_GUIDE.md for Phase 2 and smart contract deployment.
- Implemented Vite configuration for the orchestration portal, supporting both Vue and React frameworks.
- Added server-side logic for the Multi-Cloud Orchestration Portal, including API endpoints for environment management and monitoring.
- Created scripts for resource import and usage validation across non-US regions.
- Added tests for CCIP error handling and integration to ensure robust functionality.
- Included various new files and directories for the orchestration portal and deployment scripts.
2025-12-12 14:57:48 -08:00

146 lines
4.4 KiB
TypeScript

/**
* MetaMask Integration Tests
*
* Unit tests for MetaMask integration helpers
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
// Mock window.ethereum
const mockEthereum = {
isMetaMask: true,
request: vi.fn(),
on: vi.fn(),
removeListener: vi.fn(),
};
describe('MetaMask Integration', () => {
beforeEach(() => {
// Reset mocks
vi.clearAllMocks();
// Mock window.ethereum
Object.defineProperty(window, 'ethereum', {
value: mockEthereum,
writable: true,
configurable: true,
});
});
afterEach(() => {
// Clean up
delete (window as any).ethereum;
});
describe('addNetwork', () => {
it('should add network successfully', async () => {
mockEthereum.request.mockResolvedValue(null);
const { addNetwork } = await import('../metamask-sdk/src/addNetwork');
await addNetwork();
expect(mockEthereum.request).toHaveBeenCalledWith({
method: 'wallet_addEthereumChain',
params: [expect.objectContaining({
chainId: '0x8a',
chainName: 'DeFi Oracle Meta Mainnet',
})],
});
});
it('should handle network already added error', async () => {
const error = new Error('Network already added');
(error as any).code = 4902;
mockEthereum.request.mockRejectedValue(error);
// Mock switchNetwork
mockEthereum.request.mockResolvedValueOnce(null);
const { addNetwork } = await import('../metamask-sdk/src/addNetwork');
await expect(addNetwork()).resolves.not.toThrow();
});
it('should throw error if MetaMask is not installed', async () => {
delete (window as any).ethereum;
const { addNetwork } = await import('../metamask-sdk/src/addNetwork');
await expect(addNetwork()).rejects.toThrow('MetaMask is not installed');
});
});
describe('switchNetwork', () => {
it('should switch network successfully', async () => {
mockEthereum.request.mockResolvedValue(null);
const { switchNetwork } = await import('../metamask-sdk/src/switchNetwork');
await switchNetwork();
expect(mockEthereum.request).toHaveBeenCalledWith({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x8a' }],
});
});
it('should throw error if network not added', async () => {
const error = new Error('Network not added');
(error as any).code = 4902;
mockEthereum.request.mockRejectedValue(error);
const { switchNetwork } = await import('../metamask-sdk/src/switchNetwork');
await expect(switchNetwork()).rejects.toThrow('ChainID 138 is not added');
});
});
describe('addToken', () => {
it('should add token successfully', async () => {
mockEthereum.request.mockResolvedValue(null);
const { addToken } = await import('../metamask-sdk/src/addToken');
await addToken('0x1234567890123456789012345678901234567890', 'WETH', 18);
expect(mockEthereum.request).toHaveBeenCalledWith({
method: 'wallet_watchAsset',
params: {
type: 'ERC20',
options: {
address: '0x1234567890123456789012345678901234567890',
symbol: 'WETH',
decimals: 18,
},
},
});
});
it('should validate token address format', async () => {
const { addToken } = await import('../metamask-sdk/src/addToken');
await expect(addToken('invalid', 'WETH', 18)).rejects.toThrow('Invalid token address format');
});
it('should validate decimals', async () => {
const { addToken } = await import('../metamask-sdk/src/addToken');
await expect(addToken('0x1234567890123456789012345678901234567890', 'WETH', 19)).rejects.toThrow('Invalid decimals');
});
});
describe('isOnChain138', () => {
it('should return true if on ChainID 138', async () => {
mockEthereum.request.mockResolvedValue('0x8a');
const { isOnChain138 } = await import('../metamask-sdk/src/switchNetwork');
const result = await isOnChain138();
expect(result).toBe(true);
});
it('should return false if not on ChainID 138', async () => {
mockEthereum.request.mockResolvedValue('0x1');
const { isOnChain138 } = await import('../metamask-sdk/src/switchNetwork');
const result = await isOnChain138();
expect(result).toBe(false);
});
});
});