/**
* Simple Hot-Reload Race Condition Test
*
* Strategy:
* 1. Make rapid tool calls via MCP
* 2. During the calls, modify conversation-manager.ts
* 3. Continue making calls
* 4. Check if any calls fail or return inconsistent results
*/
import { writeFileSync, readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { execSync } from 'child_process';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
console.log('Hot-Reload Race Condition Test');
console.log('================================\n');
// Step 1: Establish baseline - approve write-file
console.log('1️⃣ Establishing baseline - approving write-file');
console.log(' Run: claude -p "Use mcp__mcp-tool-factory__example-tool with action: approve:write-file"\n');
// Step 2: Trigger hot-reload
console.log('2️⃣ Triggering hot-reload by modifying conversation-manager.ts');
const filePath = join(__dirname, 'src', 'core', 'conversation-manager.ts');
const originalContent = readFileSync(filePath, 'utf8');
// Add timestamp comment
const modifiedContent = originalContent.replace(
'class ConversationManager {',
`class ConversationManager {\n // Race test: ${Date.now()}`
);
writeFileSync(filePath, modifiedContent);
console.log(' ✅ Modified conversation-manager.ts');
// Build
console.log(' 🔨 Building...');
try {
execSync('npm run build', { cwd: __dirname, stdio: 'pipe' });
console.log(' ✅ Build complete - hot-reload triggered\n');
} catch (err) {
console.log(' ❌ Build failed:', err.message);
process.exit(1);
}
// Step 3: Wait for hot-reload to process
console.log('3️⃣ Waiting 2 seconds for hot-reload to complete...');
await new Promise(resolve => setTimeout(resolve, 2000));
console.log(' ✅ Hot-reload should be complete\n');
// Step 4: Test if approval persisted
console.log('4️⃣ Testing if approval persisted through hot-reload');
console.log(' Run: claude -p "Use mcp__mcp-tool-factory__example-tool with action: write-file"');
console.log(' Expected: ✅ File write approved and executed');
console.log(' If fails: ❌ Approval required - STATE LOST\n');
// Step 5: Revert
console.log('5️⃣ Reverting changes');
writeFileSync(filePath, originalContent);
execSync('npm run build', { cwd: __dirname, stdio: 'pipe' });
console.log(' ✅ Reverted\n');
console.log('================================');
console.log('Test Setup Complete');
console.log('================================');
console.log('\nMANUAL TEST SEQUENCE:');
console.log('1. Run: approve:write-file');
console.log('2. Run: write-file (should succeed)');
console.log('3. While rapidly calling identity/greet, run this script');
console.log('4. Immediately after script, call: write-file');
console.log('5. Check if approval persisted\n');