indiamart-lms-sdk
Version:
Simple and powerful SDK for IndiaMART Lead Management System (LMS) - Get leads with just your CRM key
465 lines (398 loc) โข 15.4 kB
JavaScript
/**
* Simple Client Test - IndiaMART LMS SDK
*
* This example demonstrates a simple client implementation that tests
* the SDK functionality and validates it works as per requirements.
*/
import { IndiaMartSDK } from '../src/index.js';
import dotenv from 'dotenv';
import fs from 'fs/promises';
import path from 'path';
// Load environment variables
dotenv.config();
/**
* Simple Client Class
*
* This class demonstrates how to use the IndiaMART SDK in a real application
* with proper error handling, logging, and data management.
*/
class SimpleClient {
constructor(crmKey) {
this.sdk = new IndiaMartSDK(crmKey, {
paths: {
downloadPath: './client-downloads',
logPath: './client-logs',
dataPath: './client-data'
}
});
this.isInitialized = false;
this.stats = {
totalLeads: 0,
successfulCalls: 0,
failedCalls: 0,
cacheHits: 0
};
}
/**
* Initialize the client
*/
async initialize() {
try {
console.log('๐ Initializing Simple Client...');
// Check SDK health
const health = await this.sdk.getHealthStatus();
console.log('๐ SDK Health Status:', health.status);
if (health.status === 'healthy') {
console.log('โ
SDK is healthy and ready');
this.isInitialized = true;
return true;
} else {
console.log('โ SDK health check failed:', health.error);
return false;
}
} catch (error) {
console.error('โ Failed to initialize client:', error.message);
return false;
}
}
/**
* Test basic SDK functionality
*/
async testBasicFunctionality() {
console.log('\n๐งช Testing Basic SDK Functionality...');
try {
// Test 1: Input validation
console.log('\n1๏ธโฃ Testing Input Validation:');
const emailValidation = this.sdk.validateInput('test@example.com', {
type: 'string',
pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
min: 5,
max: 100
});
console.log(' Email validation:', emailValidation.isValid ? 'โ
' : 'โ');
const invalidEmailValidation = this.sdk.validateInput('invalid-email', {
type: 'string',
pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
min: 5,
max: 100
});
console.log(' Invalid email validation:', invalidEmailValidation.isValid ? 'โ
' : 'โ');
// Test 2: Cache functionality
console.log('\n2๏ธโฃ Testing Cache Functionality:');
const cacheStats = this.sdk.getCacheStats();
console.log(' Cache stats:', {
size: cacheStats.size,
hitRate: `${cacheStats.hitRate}%`,
totalRequests: cacheStats.totalRequests
});
// Test 3: Date formatting
console.log('\n3๏ธโฃ Testing Date Formatting:');
const today = new Date();
const formattedDate = IndiaMartSDK.formatDate(today, 'timestamp');
console.log(' Today formatted:', formattedDate);
// Test 4: Date range validation
console.log('\n4๏ธโฃ Testing Date Range Validation:');
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
const dateValidation = IndiaMartSDK.validateDateRange(yesterday, today);
console.log(' Date range validation:', dateValidation.isValid ? 'โ
' : 'โ');
if (!dateValidation.isValid) {
console.log(' Validation errors:', dateValidation.errors.join(', '));
}
console.log('\nโ
Basic functionality tests completed');
return true;
} catch (error) {
console.error('โ Basic functionality test failed:', error.message);
return false;
}
}
/**
* Test lead fetching functionality
*/
async testLeadFetching() {
console.log('\n๐ Testing Lead Fetching Functionality...');
try {
// Test 1: Get leads for yesterday (safer than today)
console.log('\n1๏ธโฃ Fetching leads for yesterday:');
const yesterdayResult = await this.sdk.getLeadsForYesterday();
if (yesterdayResult.success) {
console.log(' โ
Successfully fetched leads for yesterday');
console.log(' ๐ Total records:', yesterdayResult.totalRecords);
console.log(' ๐ Leads count:', yesterdayResult.leads.length);
if (yesterdayResult.downloadPath) {
console.log(' ๐พ Downloaded to:', yesterdayResult.downloadPath);
this.stats.totalLeads += yesterdayResult.leads.length;
}
this.stats.successfulCalls++;
} else {
console.log(' โ Failed to fetch leads for yesterday');
console.log(' Error:', yesterdayResult.error);
this.stats.failedCalls++;
}
// Test 2: Get leads for last 7 days
console.log('\n2๏ธโฃ Fetching leads for last 7 days:');
const lastWeekResult = await this.sdk.getLeadsForLastDays(7);
if (lastWeekResult.success) {
console.log(' โ
Successfully fetched leads for last 7 days');
console.log(' ๐ Total records:', lastWeekResult.totalRecords);
console.log(' ๐ Leads count:', lastWeekResult.leads.length);
if (lastWeekResult.downloadPath) {
console.log(' ๐พ Downloaded to:', lastWeekResult.downloadPath);
this.stats.totalLeads += lastWeekResult.leads.length;
}
this.stats.successfulCalls++;
} else {
console.log(' โ Failed to fetch leads for last 7 days');
console.log(' Error:', lastWeekResult.error);
this.stats.failedCalls++;
}
// Test 3: Test caching (make same request again)
console.log('\n3๏ธโฃ Testing cache (repeat last 7 days request):');
const cachedResult = await this.sdk.getLeadsForLastDays(7);
if (cachedResult.success) {
console.log(' โ
Cached request successful');
console.log(' ๐ Total records:', cachedResult.totalRecords);
console.log(' ๐ Leads count:', cachedResult.leads.length);
this.stats.cacheHits++;
} else {
console.log(' โ Cached request failed');
console.log(' Error:', cachedResult.error);
}
console.log('\nโ
Lead fetching tests completed');
return true;
} catch (error) {
console.error('โ Lead fetching test failed:', error.message);
this.stats.failedCalls++;
return false;
}
}
/**
* Test error handling
*/
async testErrorHandling() {
console.log('\nโ ๏ธ Testing Error Handling...');
try {
// Test 1: Invalid date range
console.log('\n1๏ธโฃ Testing invalid date range:');
const invalidDateResult = await this.sdk.getLeadsForDateRange('invalid-date', 'also-invalid');
if (!invalidDateResult.success) {
console.log(' โ
Properly handled invalid date range');
console.log(' Error:', invalidDateResult.error);
} else {
console.log(' โ Should have failed with invalid dates');
}
// Test 2: Future date range
console.log('\n2๏ธโฃ Testing future date range:');
const futureDate = new Date();
futureDate.setDate(futureDate.getDate() + 1);
const futureResult = await this.sdk.getLeadsForDateRange(futureDate, futureDate);
if (!futureResult.success) {
console.log(' โ
Properly handled future date range');
console.log(' Error:', futureResult.error);
} else {
console.log(' โ Should have failed with future dates');
}
// Test 3: Very old date range
console.log('\n3๏ธโฃ Testing very old date range:');
const oldDate = new Date();
oldDate.setFullYear(oldDate.getFullYear() - 2);
const oldResult = await this.sdk.getLeadsForDateRange(oldDate, oldDate);
if (!oldResult.success) {
console.log(' โ
Properly handled very old date range');
console.log(' Error:', oldResult.error);
} else {
console.log(' โ Should have failed with very old dates');
}
console.log('\nโ
Error handling tests completed');
return true;
} catch (error) {
console.error('โ Error handling test failed:', error.message);
return false;
}
}
/**
* Test logging and monitoring
*/
async testLoggingAndMonitoring() {
console.log('\n๐ Testing Logging and Monitoring...');
try {
// Test 1: Get API logs
console.log('\n1๏ธโฃ Getting API logs:');
const apiLogs = this.sdk.getAPILogs(5);
console.log(' ๐ Retrieved', apiLogs.length, 'API log entries');
// Test 2: Get secure logs
console.log('\n2๏ธโฃ Getting secure logs:');
const secureLogs = await this.sdk.getSecureLogs(5);
console.log(' ๐ Retrieved', secureLogs.length, 'secure log entries');
// Test 3: Get rate limit status
console.log('\n3๏ธโฃ Getting rate limit status:');
const rateLimitStatus = this.sdk.getRateLimitStatus();
console.log(' ๐ Rate limit status:', {
isBlocked: rateLimitStatus.isBlocked,
callsPerMinute: rateLimitStatus.callsPerMinute,
callsPerHour: rateLimitStatus.callsPerHour
});
// Test 4: Get API statistics
console.log('\n4๏ธโฃ Getting API statistics:');
const apiStats = this.sdk.getAPIStats();
console.log(' ๐ API stats:', {
totalCalls: apiStats.totalCalls,
successfulCalls: apiStats.successfulCalls,
failedCalls: apiStats.failedCalls
});
// Test 5: Get cache statistics
console.log('\n5๏ธโฃ Getting cache statistics:');
const cacheStats = this.sdk.getCacheStats();
console.log(' ๐ Cache stats:', {
size: cacheStats.size,
hitRate: `${cacheStats.hitRate}%`,
totalRequests: cacheStats.totalRequests
});
console.log('\nโ
Logging and monitoring tests completed');
return true;
} catch (error) {
console.error('โ Logging and monitoring test failed:', error.message);
return false;
}
}
/**
* Test data management
*/
async testDataManagement() {
console.log('\n๐พ Testing Data Management...');
try {
// Test 1: Check download directory
console.log('\n1๏ธโฃ Checking download directory:');
const downloadPath = this.sdk.getDownloadPath();
console.log(' ๐ Download path:', downloadPath);
try {
const files = await fs.readdir(downloadPath);
console.log(' ๐ Files in download directory:', files.length);
} catch (error) {
console.log(' ๐ Download directory does not exist yet');
}
// Test 2: Test cache clearing
console.log('\n2๏ธโฃ Testing cache clearing:');
const clearResult = await this.sdk.clearCache();
if (clearResult.success) {
console.log(' โ
Cache cleared successfully');
} else {
console.log(' โ Failed to clear cache:', clearResult.error);
}
// Test 3: Test log cleanup
console.log('\n3๏ธโฃ Testing log cleanup:');
const logCleanupResult = await this.sdk.clearOldSecureLogs(30);
if (logCleanupResult.success) {
console.log(' โ
Log cleanup completed');
} else {
console.log(' โ Failed to cleanup logs:', logCleanupResult.error);
}
console.log('\nโ
Data management tests completed');
return true;
} catch (error) {
console.error('โ Data management test failed:', error.message);
return false;
}
}
/**
* Generate test report
*/
generateTestReport() {
console.log('\n๐ Test Report Summary:');
console.log('=' .repeat(50));
console.log('๐ Client Statistics:');
console.log(' Total Leads Fetched:', this.stats.totalLeads);
console.log(' Successful API Calls:', this.stats.successfulCalls);
console.log(' Failed API Calls:', this.stats.failedCalls);
console.log(' Cache Hits:', this.stats.cacheHits);
const successRate = this.stats.successfulCalls + this.stats.failedCalls > 0
? (this.stats.successfulCalls / (this.stats.successfulCalls + this.stats.failedCalls)) * 100
: 0;
console.log(' Success Rate:', `${successRate.toFixed(1)}%`);
console.log('=' .repeat(50));
}
/**
* Cleanup resources
*/
async cleanup() {
try {
console.log('\n๐งน Cleaning up resources...');
const destroyResult = await this.sdk.destroy();
if (destroyResult.success) {
console.log(' โ
SDK destroyed successfully');
} else {
console.log(' โ Failed to destroy SDK:', destroyResult.error);
}
} catch (error) {
console.error('โ Cleanup failed:', error.message);
}
}
}
/**
* Main test function
*/
async function runClientTest() {
console.log('๐งช IndiaMART LMS SDK - Simple Client Test\n');
console.log('=' .repeat(60));
// Check if CRM key is available
if (!process.env.INDIAMART_CRM_KEY) {
console.log('โ ๏ธ No CRM key found. Running in demo mode...\n');
console.log('๐ก To test with real API calls, set INDIAMART_CRM_KEY environment variable');
console.log('๐ Demo mode will test validation, caching, and error handling only\n');
}
// Use a valid demo key format for testing
const demoKey = 'demo-key-12345678901234567890'; // 25 characters to pass validation
const client = new SimpleClient(process.env.INDIAMART_CRM_KEY || demoKey);
try {
// Initialize client
const initialized = await client.initialize();
if (!initialized) {
console.log('โ Client initialization failed. Exiting...');
return;
}
// Run all tests
const tests = [
{ name: 'Basic Functionality', fn: () => client.testBasicFunctionality() },
{ name: 'Lead Fetching', fn: () => client.testLeadFetching() },
{ name: 'Error Handling', fn: () => client.testErrorHandling() },
{ name: 'Logging and Monitoring', fn: () => client.testLoggingAndMonitoring() },
{ name: 'Data Management', fn: () => client.testDataManagement() }
];
let passedTests = 0;
const totalTests = tests.length;
for (const test of tests) {
console.log(`\n๐ Running ${test.name} Test...`);
const result = await test.fn();
if (result) {
passedTests++;
console.log(`โ
${test.name} Test Passed`);
} else {
console.log(`โ ${test.name} Test Failed`);
}
}
// Generate report
client.generateTestReport();
// Final results
console.log('\n๐ฏ Final Test Results:');
console.log(` Tests Passed: ${passedTests}/${totalTests}`);
console.log(` Success Rate: ${((passedTests / totalTests) * 100).toFixed(1)}%`);
if (passedTests === totalTests) {
console.log(' Status: โ
ALL TESTS PASSED');
} else {
console.log(' Status: โ ๏ธ SOME TESTS FAILED');
}
} catch (error) {
console.error('โ Test execution failed:', error.message);
console.error('Stack trace:', error.stack);
} finally {
// Cleanup
await client.cleanup();
console.log('\nโ
Client test completed');
}
}
// Run the test
if (import.meta.url === `file://${process.argv[1]}`) {
runClientTest().catch(console.error);
}
export { SimpleClient, runClientTest };