index.test.ts•3.3 kB
// Basic tests for Universal Blockchain MCP Server
import { spawn } from 'child_process';
// Mock the child_process module
jest.mock('child_process');
const mockSpawn = spawn as jest.MockedFunction<typeof spawn>;
describe('Universal Blockchain MCP Server', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('Environment Detection', () => {
test('should detect test mode correctly', () => {
// Test environment detection logic
const isRemoteEnvironment = () => {
if (
process.env.NODE_ENV === 'test' ||
process.env.SMITHERY_SCAN === 'true'
) {
return true;
}
return false;
};
process.env.NODE_ENV = 'test';
expect(isRemoteEnvironment()).toBe(true);
process.env.NODE_ENV = 'production';
process.env.SMITHERY_SCAN = 'true';
expect(isRemoteEnvironment()).toBe(true);
process.env.NODE_ENV = 'production';
process.env.SMITHERY_SCAN = 'false';
expect(isRemoteEnvironment()).toBe(false);
});
});
describe('ZetaChain Command Execution', () => {
test('should handle command execution errors gracefully', async () => {
// Mock spawn to simulate command failure
const mockChild = {
stdout: { on: jest.fn() },
stderr: { on: jest.fn() },
on: jest.fn((event, callback) => {
if (event === 'close') {
setTimeout(() => callback(1), 10); // Simulate error exit
}
}),
};
mockSpawn.mockReturnValue(mockChild as any);
const executeCLI = async (args: string[]): Promise<string> => {
return new Promise((resolve, reject) => {
const child = spawn('zetachain', args);
child.on('close', code => {
if (code === 0) {
resolve('success');
} else {
reject(new Error(`Command failed with code ${code}`));
}
});
});
};
await expect(executeCLI(['invalid', 'command'])).rejects.toThrow(
'Command failed with code 1'
);
});
});
describe('API Response Formatting', () => {
test('should format chain list response correctly', () => {
const mockChainData = {
chainId: 7001,
chainName: 'zeta_testnet',
status: 'active',
};
const formatChainResponse = (data: any) => {
return `Chain ID: ${data.chainId}\nChain Name: ${data.chainName}\nStatus: ${data.status}`;
};
const result = formatChainResponse(mockChainData);
expect(result).toContain('Chain ID: 7001');
expect(result).toContain('Chain Name: zeta_testnet');
expect(result).toContain('Status: active');
});
});
describe('Error Handling', () => {
test('should handle unknown tool errors', () => {
const handleToolError = (toolName: string) => {
if (
!['create_account', 'list_accounts', 'get_balances'].includes(
toolName
)
) {
throw new Error(`Unknown tool: ${toolName}`);
}
return 'success';
};
expect(() => handleToolError('unknown_tool')).toThrow(
'Unknown tool: unknown_tool'
);
expect(handleToolError('create_account')).toBe('success');
});
});
});