const { SalesforceConnection } = require('./dist/salesforce/connection');
const { SalesforceMetadataClient } = require('./dist/salesforce/metadataClient');
const { SalesforceToolingClient } = require('./dist/salesforce/toolingClient');
const { MetadataProcessor } = require('./dist/chunking/processor');
const { VectorStore } = require('./dist/vector/store');
const { MockEmbedding } = require('./dist/vector/embedding');
const {
RagIngestOrgTool,
RagSearchTool,
RagWhereUsedTool,
RagOpenTool,
RagJobStatusTool
} = require('./dist/mcp/tools/rag');
const { AuthMiddleware } = require('./dist/mcp/middleware/auth');
async function testRagTools() {
console.log('Testing RAG MCP tools functionality...');
try {
// Initialize dependencies
const connection = new SalesforceConnection();
const metadataClient = new SalesforceMetadataClient(connection);
const toolingClient = new SalesforceToolingClient(connection);
const processor = new MetadataProcessor();
const embeddingModel = new MockEmbedding();
const vectorStore = new VectorStore({
host: 'localhost',
port: 5433,
database: 'sfdxrag',
user: 'postgres',
password: 'postgres'
}, embeddingModel);
// Initialize RAG tools
const ingestTool = new RagIngestOrgTool(metadataClient, toolingClient, processor, vectorStore);
const searchTool = new RagSearchTool(vectorStore);
const whereUsedTool = new RagWhereUsedTool(vectorStore);
const openTool = new RagOpenTool(vectorStore);
const jobStatusTool = new RagJobStatusTool();
// Create auth context
const authMiddleware = AuthMiddleware.getInstance();
const sessionId = 'rag-test-session';
const orgId = 'rag-test-org-123';
const userId = 'rag-test-user-456';
authMiddleware.createAuthContext(orgId, userId, sessionId);
console.log('\n=== 1. Testing RAG Ingestion Tool ===');
const ingestResult = await ingestTool.execute({
types: ['ApexClass'],
incremental: false
}, { sessionId });
if (!ingestResult.isError) {
const ingestData = JSON.parse(ingestResult.content[0].text);
console.log('✓ Ingestion started:', {
jobId: ingestData.jobId,
status: ingestData.status,
types: ingestData.types,
estimated: ingestData.estimatedMinutes + ' minutes'
});
// Wait a bit for ingestion to process
await new Promise(resolve => setTimeout(resolve, 3000));
console.log('\n=== 2. Testing Job Status Tool ===');
const statusResult = await jobStatusTool.execute({
jobId: ingestData.jobId
}, { sessionId });
if (!statusResult.isError) {
const statusData = JSON.parse(statusResult.content[0].text);
console.log('✓ Job status retrieved:', {
status: statusData.job.status,
progress: statusData.job.progress.percent + '%',
chunksProcessed: statusData.job.progress.chunksProcessed
});
}
} else {
console.log('❌ Ingestion failed:', ingestResult.content[0].text);
}
console.log('\n=== 3. Testing RAG Search Tool ===');
const searchResult = await searchTool.execute({
query: 'phone number validation',
searchMode: 'hybrid',
limit: 3
}, { sessionId });
if (!searchResult.isError) {
const searchData = JSON.parse(searchResult.content[0].text);
console.log('✓ Search completed:', {
query: searchData.query,
resultCount: searchData.resultCount,
searchMode: searchData.searchMode
});
searchData.results.forEach((result, i) => {
console.log(` ${i + 1}. ${result.name} (${result.type}) - similarity: ${result.similarity?.toFixed(3) || 'N/A'}`);
});
} else {
console.log('❌ Search failed:', searchResult.content[0].text);
}
console.log('\n=== 4. Testing Where-Used Tool ===');
const whereUsedResult = await whereUsedTool.execute({
symbol: 'CallManagementService',
limit: 5
}, { sessionId });
if (!whereUsedResult.isError) {
const whereUsedData = JSON.parse(whereUsedResult.content[0].text);
console.log('✓ Where-used search completed:', {
symbol: whereUsedData.symbol,
referenceCount: whereUsedData.resultCount
});
whereUsedData.references.forEach((ref, i) => {
console.log(` ${i + 1}. ${ref.name} (${ref.type}) - ${ref.symbols.length} matching symbols`);
});
} else {
console.log('❌ Where-used search failed:', whereUsedResult.content[0].text);
}
console.log('\n=== 5. Testing All Job Statuses ===');
const allJobsResult = await jobStatusTool.execute({}, { sessionId });
if (!allJobsResult.isError) {
const allJobsData = JSON.parse(allJobsResult.content[0].text);
console.log('✓ All jobs retrieved:', {
totalJobs: allJobsData.jobs.length
});
allJobsData.jobs.forEach((job, i) => {
console.log(` ${i + 1}. Job ${job.id.substring(0, 8)}... - ${job.status} (${job.progress.percent}%)`);
});
} else {
console.log('❌ Job status retrieval failed:', allJobsResult.content[0].text);
}
await vectorStore.close();
console.log('\n✅ RAG tools testing completed successfully!');
} catch (error) {
console.error('❌ RAG tools test failed:', error.message);
}
}
testRagTools();