import fs from 'fs';
import path from 'path';
import { spawn } from 'child_process';

const testsDir = path.resolve(process.cwd(), 'tests');

async function runAllTests() {
    console.log('🧪 Running all tests in tests/ directory...\n');

    const files = fs.readdirSync(testsDir).filter(f => f.endsWith('.ts'));
    let passed = 0;
    let failed = 0;

    for (const file of files) {
        console.log(`\n---------------------------------------------------------`);
        console.log(`▶️  Running ${file}...`);
        console.log(`---------------------------------------------------------`);

        await new Promise<void>((resolve) => {
            const proc = spawn('npx', ['tsx', path.join(testsDir, file)], {
                stdio: 'inherit',
                shell: true
            });

            proc.on('close', (code) => {
                if (code === 0) {
                    console.log(`✅ ${file} PASSED`);
                    passed++;
                } else {
                    console.log(`❌ ${file} FAILED (Exit Code: ${code})`);
                    failed++;
                }
                resolve();
            });
        });
    }

    console.log(`\n=========================================================`);
    console.log(`Summary: ${passed} Passed, ${failed} Failed`);
    console.log(`=========================================================\n`);
}

runAllTests();
