test-streaming.jsβ’6.59 kB
#!/usr/bin/env node
import { SequentialGraphitiIntegration } from './src/core/SequentialGraphitiIntegration.js';
console.log('π Testing meMCP Streaming Functionality\n');
async function testStreaming() {
const integration = new SequentialGraphitiIntegration();
try {
await integration.initialize();
console.log('β
meMCP system initialized\n');
// First, add some test facts for streaming
console.log('π Adding test facts for streaming...\n');
const testFacts = [
{ content: 'React hooks must be called in order', type: 'constraint', domain: 'react' },
{ content: 'Use async/await for better error handling', type: 'optimization', domain: 'javascript' },
{ content: 'Always validate user input for security', type: 'security_concern', domain: 'security' },
{ content: 'CSS Grid is better than Flexbox for 2D layouts', type: 'verified_pattern', domain: 'css' },
{ content: 'Use semantic HTML elements for accessibility', type: 'verified_pattern', domain: 'html' },
{ content: 'Debounce API calls to prevent rate limiting', type: 'optimization', domain: 'api' },
{ content: 'Use TypeScript for large JavaScript projects', type: 'decision_rationale', domain: 'javascript' },
{ content: 'Test your code before committing', type: 'workflow_improvement', domain: 'development' },
{ content: 'Document your API endpoints thoroughly', type: 'workflow_improvement', domain: 'documentation' },
{ content: 'Use environment variables for configuration', type: 'tool_configuration', domain: 'deployment' },
];
for (const fact of testFacts) {
await integration.memoryTools.storeInsight(fact);
}
console.log(`β
Added ${testFacts.length} test facts\n`);
// Test streaming functionality
console.log('π Testing streaming query...\n');
// Start a streaming query
const streamId = await integration.memoryTools.streamingManager.createBatchStream(
{ query: '', type: '', domain: '' }, // Get all facts
integration.factStore,
{ chunkSize: 3, maxResults: 20 }
);
console.log(`π‘ Started stream with ID: ${streamId}`);
// Get stream status
const initialStatus = await integration.memoryTools.streamingManager.getStreamStatus(streamId);
console.log(`π Initial status: ${initialStatus.progress.total} total facts, ${initialStatus.status} status`);
// Process chunks
let chunkCount = 0;
let totalProcessed = 0;
while (true) {
try {
const chunk = await integration.memoryTools.streamingManager.getNextChunk(streamId);
chunkCount++;
totalProcessed += chunk.facts.length;
console.log(`\nπ¦ Chunk ${chunkCount}:`);
console.log(` Facts: ${chunk.facts.length}`);
console.log(` Progress: ${chunk.progress.percentage}%`);
console.log(` Remaining: ${chunk.progress.remainingFacts}`);
if (chunk.progress.estimatedTimeRemaining) {
console.log(` ETA: ${chunk.progress.estimatedTimeRemaining}s`);
}
// Show first fact in chunk as example
if (chunk.facts.length > 0) {
console.log(` Example: "${chunk.facts[0].summary}"`);
}
if (chunk.isLastChunk) {
console.log(`\nβ
Streaming completed!`);
break;
}
// Add small delay to simulate real streaming
await new Promise(resolve => setTimeout(resolve, 100));
} catch (error) {
console.error(`β Error getting chunk: ${error.message}`);
break;
}
}
// Final status
const finalStatus = await integration.memoryTools.streamingManager.getStreamStatus(streamId);
console.log(`\nπ Final Status:`);
console.log(` Status: ${finalStatus.status}`);
console.log(` Progress: ${finalStatus.progress.current}/${finalStatus.progress.total}`);
console.log(` Duration: ${finalStatus.duration}ms`);
console.log(` Chunks processed: ${chunkCount}`);
console.log(` Total facts streamed: ${totalProcessed}`);
// Test streaming manager stats
const streamStats = integration.memoryTools.streamingManager.getStats();
console.log(`\nπ Streaming Manager Stats:`);
console.log(` Total streams: ${streamStats.totalStreams}`);
console.log(` Active streams: ${streamStats.activeStreams}`);
console.log(` Completed streams: ${streamStats.completedStreams}`);
console.log(` Total facts processed: ${streamStats.totalFactsProcessed}`);
// Test pause/resume functionality
console.log(`\nβΈοΈ Testing pause/resume functionality...`);
const streamId2 = await integration.memoryTools.streamingManager.createBatchStream(
{ query: 'javascript' },
integration.factStore,
{ chunkSize: 2, maxResults: 10 }
);
// Get first chunk
const firstChunk = await integration.memoryTools.streamingManager.getNextChunk(streamId2);
console.log(`π¦ Got first chunk: ${firstChunk.facts.length} facts`);
// Pause the stream
const pauseResult = await integration.memoryTools.streamingManager.pauseStream(streamId2);
console.log(`βΈοΈ Paused stream: ${pauseResult.status}`);
// Try to get next chunk (should fail)
try {
await integration.memoryTools.streamingManager.getNextChunk(streamId2);
console.log('β Should have failed to get chunk from paused stream');
} catch (error) {
console.log(`β
Correctly blocked chunk from paused stream: ${error.message}`);
}
// Resume the stream
const resumeResult = await integration.memoryTools.streamingManager.resumeStream(streamId2);
console.log(`βΆοΈ Resumed stream: ${resumeResult.status}`);
// Now get next chunk (should work)
const nextChunk = await integration.memoryTools.streamingManager.getNextChunk(streamId2);
console.log(`π¦ Got chunk after resume: ${nextChunk.facts.length} facts`);
// Cancel the stream
const cancelResult = await integration.memoryTools.streamingManager.cancelStream(streamId2);
console.log(`β Cancelled stream: processed ${cancelResult.processed}/${cancelResult.total} facts`);
console.log('\nβ
Streaming functionality test completed successfully!');
// Cleanup
await integration.memoryTools.streamingManager.cleanupCompletedStreams();
console.log('π§Ή Cleaned up completed streams');
} catch (error) {
console.error('β Error in streaming test:', error);
} finally {
await integration.shutdown();
}
}
testStreaming().catch(console.error);