feature-validation.mjs
1 import { performGuardianRollback } from '../src/lib/server/agents/guardian' 2 import { applyMMR } from '../src/lib/server/mmr' 3 import { execSync } from 'child_process' 4 import fs from 'fs' 5 import path from 'path' 6 import os from 'os' 7 8 async function runTests() { 9 console.log('🚀 Starting SwarmClaw Advanced Feature Validation...\n') 10 11 // --- 1. Test MMR Diversity --- 12 console.log('--- Testing MMR (Maximal Marginal Relevance) ---') 13 const queryEmbedding = Array(1536).fill(0.1) // Mock embedding 14 const candidates = [ 15 { 16 entry: { id: '1', title: 'Python Loop', content: 'How to write a for loop in python', category: 'note' }, 17 salience: 0.9, 18 embedding: Array(1536).fill(0.1) 19 }, 20 { 21 entry: { id: '2', title: 'Python For', content: 'Writing for loops in python language', category: 'note' }, 22 salience: 0.89, 23 embedding: Array(1536).fill(0.101) 24 }, // Very similar to #1 25 { 26 entry: { id: '3', title: 'React Hooks', content: 'Using useEffect and useState in React', category: 'note' }, 27 salience: 0.7, 28 embedding: Array(1536).fill(0.5) 29 }, // Different topic 30 ] 31 32 const diverseResults = applyMMR(queryEmbedding, candidates, 2, 0.5) 33 console.log('Selected IDs (should favor 1 and 3 over 2 due to diversity):', diverseResults.map(r => r.id)) 34 if (diverseResults.some(r => r.id === '3') && !diverseResults.some(r => r.id === '2')) { 35 console.log('✅ MMR Diversity Test Passed!') 36 } else { 37 console.log('⚠️ MMR Diversity Test: Diversity not maximized as expected.') 38 } 39 40 // --- 2. Test Guardian Rollback --- 41 console.log('\n--- Testing Guardian Auto-Recovery (Rollback) ---') 42 const testRepoDir = path.join(os.tmpdir(), `swarmclaw-test-repo-${Date.now()}`) 43 fs.mkdirSync(testRepoDir) 44 45 try { 46 execSync('git init', { cwd: testRepoDir }) 47 execSync('git config user.email "test@example.com"', { cwd: testRepoDir }) 48 execSync('git config user.name "Test User"', { cwd: testRepoDir }) 49 fs.writeFileSync(path.join(testRepoDir, 'config.json'), '{"status": "ok"}') 50 execSync('git add . && git commit -m "Initial commit"', { cwd: testRepoDir }) 51 52 // Corrupt the file 53 fs.writeFileSync(path.join(testRepoDir, 'config.json'), '{"status": "CORRUPTED"}') 54 console.log('Current state: Corrupted (Uncommitted)') 55 56 const rollback = performGuardianRollback(testRepoDir) 57 const restoredContent = fs.readFileSync(path.join(testRepoDir, 'config.json'), 'utf8') 58 59 if (rollback.ok && restoredContent.includes('ok')) { 60 console.log('✅ Guardian Rollback Test Passed!') 61 } else { 62 console.log('❌ Guardian Rollback Test Failed!') 63 } 64 } catch (err) { 65 console.error('Guardian test error:', err) 66 } finally { 67 try { fs.rmSync(testRepoDir, { recursive: true, force: true }) } catch {} 68 } 69 70 console.log('\n--- Validation Complete ---') 71 } 72 73 runTests().catch(console.error)