#!/usr/bin/env node /** * Demonstration of incremental update functionality in Claude Flow * This script tests various incremental update scenarios across the system */ const { SwarmMemory } = require('../../dist/swarm/memory'); const { ConfigurationManager } = require('../../dist/core/config'); const { SimpleCache } = require('../../dist/memory/cache'); const { deepMerge } = require('../../dist/utils/helpers'); console.log('๐Ÿงช Testing Incremental Updates in Claude Flow\n'); // Test 1: SwarmMemory Version Tracking console.log('1๏ธโƒฃ Testing SwarmMemory Version Tracking'); async function testSwarmMemoryVersions() { const memory = new SwarmMemory(); // Initial set await memory.set('counter', 0); console.log(' โœ“ Initial value set'); // Perform incremental updates for (let i = 1; i <= 5; i++) { const current = await memory.get('counter'); await memory.update('counter', current.value + 1); console.log(` โœ“ Update ${i}: value=${current.value + 1}, version=${current.version + 1}`); } const final = await memory.get('counter'); console.log(` ๐Ÿ“Š Final state: value=${final.value}, version=${final.version}`); console.log(` ๐Ÿ“œ Version history: ${final.previousVersions.length} entries`); return final.version === 6 && final.value === 5; } // Test 2: Configuration Updates console.log('\n2๏ธโƒฃ Testing Configuration Manager Updates'); function testConfigurationUpdates() { const configManager = new ConfigurationManager({ defaultConfig: { model: 'claude-3', temperature: 0.7, maxTokens: 4096, tools: { webSearch: true, memory: true } } }); console.log(' โœ“ Initial config created'); // Partial update configManager.update({ temperature: 0.9, tools: { webSearch: false } }); const config = configManager.getConfig(); console.log(` โœ“ Temperature updated: ${config.temperature}`); console.log(` โœ“ Tools.webSearch updated: ${config.tools.webSearch}`); console.log(` โœ“ Tools.memory preserved: ${config.tools.memory}`); const diff = configManager.getDiff(); console.log(` ๐Ÿ“Š Config diff: ${JSON.stringify(diff)}`); return config.temperature === 0.9 && config.tools.webSearch === false && config.tools.memory === true; } // Test 3: Cache Hit/Miss Counters console.log('\n3๏ธโƒฃ Testing Cache Hit/Miss Counters'); function testCacheCounters() { const cache = new SimpleCache({ maxSize: 3 }); // Add items cache.set('key1', 'value1'); cache.set('key2', 'value2'); console.log(' โœ“ Added 2 items to cache'); // Generate hits cache.get('key1'); // hit cache.get('key2'); // hit cache.get('key1'); // hit console.log(' โœ“ Generated 3 cache hits'); // Generate misses cache.get('key3'); // miss cache.get('key4'); // miss console.log(' โœ“ Generated 2 cache misses'); const stats = cache.stats(); console.log(` ๐Ÿ“Š Cache stats: ${JSON.stringify(stats)}`); return stats.hits === 3 && stats.misses === 2; } // Test 4: Deep Merge Updates console.log('\n4๏ธโƒฃ Testing Deep Merge Incremental Updates'); function testDeepMerge() { const original = { settings: { ui: { theme: 'light', fontSize: 14 }, features: ['search', 'export'] } }; const update = { settings: { ui: { theme: 'dark' }, features: ['search', 'export', 'share'] } }; const result = deepMerge(original, update); console.log(' โœ“ Deep merge completed'); console.log(` โœ“ Theme updated: ${result.settings.ui.theme}`); console.log(` โœ“ FontSize preserved: ${result.settings.ui.fontSize}`); console.log(` โœ“ Features updated: ${result.settings.features.join(', ')}`); return result.settings.ui.theme === 'dark' && result.settings.ui.fontSize === 14 && result.settings.features.length === 3; } // Test 5: Batch Updates console.log('\n5๏ธโƒฃ Testing Batch Incremental Updates'); async function testBatchUpdates() { const memory = new SwarmMemory(); const batchSize = 10; // Store initial batch for (let i = 0; i < batchSize; i++) { await memory.set(`item-${i}`, { count: i }); } console.log(` โœ“ Stored ${batchSize} initial items`); // Batch update all items const updatePromises = []; for (let i = 0; i < batchSize; i++) { const promise = memory.get(`item-${i}`).then(item => memory.update(`item-${i}`, { count: item.value.count + 10 }) ); updatePromises.push(promise); } await Promise.all(updatePromises); console.log(' โœ“ Completed batch updates'); // Verify updates let allUpdated = true; for (let i = 0; i < batchSize; i++) { const item = await memory.get(`item-${i}`); if (item.value.count !== i + 10 || item.version !== 2) { allUpdated = false; break; } } console.log(` ๐Ÿ“Š All items updated correctly: ${allUpdated}`); return allUpdated; } // Test 6: Concurrent Counter Updates console.log('\n6๏ธโƒฃ Testing Concurrent Counter Updates'); async function testConcurrentCounters() { const memory = new SwarmMemory(); await memory.set('shared-counter', 0); const numAgents = 5; const incrementsPerAgent = 10; // Simulate multiple agents incrementing counter const agentPromises = []; for (let agent = 0; agent < numAgents; agent++) { const agentWork = async () => { for (let i = 0; i < incrementsPerAgent; i++) { const current = await memory.get('shared-counter'); await memory.update('shared-counter', current.value + 1); // Small random delay to simulate real work await new Promise(resolve => setTimeout(resolve, Math.random() * 10)); } }; agentPromises.push(agentWork()); } await Promise.all(agentPromises); const final = await memory.get('shared-counter'); console.log(` โœ“ ${numAgents} agents completed ${incrementsPerAgent} increments each`); console.log(` ๐Ÿ“Š Final counter: value=${final.value}, version=${final.version}`); console.log(` โœ“ Expected: ${numAgents * incrementsPerAgent}, Actual: ${final.value}`); return final.value === numAgents * incrementsPerAgent; } // Run all tests async function runAllTests() { const tests = [ { name: 'SwarmMemory Versions', fn: testSwarmMemoryVersions }, { name: 'Configuration Updates', fn: testConfigurationUpdates }, { name: 'Cache Counters', fn: testCacheCounters }, { name: 'Deep Merge', fn: testDeepMerge }, { name: 'Batch Updates', fn: testBatchUpdates }, { name: 'Concurrent Counters', fn: testConcurrentCounters } ]; let passed = 0; let failed = 0; for (const test of tests) { try { const result = await test.fn(); if (result) { passed++; console.log(`\nโœ… ${test.name}: PASSED`); } else { failed++; console.log(`\nโŒ ${test.name}: FAILED`); } } catch (error) { failed++; console.log(`\nโŒ ${test.name}: ERROR - ${error.message}`); } } console.log('\n๐Ÿ“Š Test Summary:'); console.log(` โœ… Passed: ${passed}`); console.log(` โŒ Failed: ${failed}`); console.log(` ๐Ÿ“ˆ Total: ${tests.length}`); console.log(` ๐ŸŽฏ Success Rate: ${((passed / tests.length) * 100).toFixed(1)}%`); process.exit(failed > 0 ? 1 : 0); } // Execute tests runAllTests().catch(console.error);