/**
 * MK2 Layout Regression Test Harness
 *
 * Tests layout computation at various terminal sizes to ensure:
 * - No rect exceeds bounds (x + w <= safeW - 1, y + h <= safeH - 1)
 * - No rect has w/h <= 0
 * - Layout computation succeeds for valid sizes
 */
import { computeLayout } from './computeLayout.js';
import { getSafeViewport } from '../safeViewport.js';
import { CMD_MIN_W, CMD_MIN_H, MIN_PANEL_W, MIN_PANEL_H } from '../safeViewport.js';
/**
 * Test layout at a specific terminal size
 */
async function testLayout(cols, rows) {
    const errors = [];
    const size = `${cols}x${rows}`;
    try {
        // Create mock screen for viewport calculation
        const mockScreen = { width: cols, height: rows };
        const viewport = getSafeViewport(mockScreen);
        // Compute layout
        const spec = await computeLayout(cols, rows);
        // Validate all rects
        for (const [name, rect] of Object.entries(spec.rects)) {
            // Skip zero-sized rects (hidden panels) - these are OK
            if (rect.w === 0 && rect.h === 0) {
                continue;
            }
            // Check for invalid dimensions
            if (rect.w <= 0) {
                errors.push(`${name}: w=${rect.w} <= 0 (invalid)`);
            }
            if (rect.h <= 0) {
                errors.push(`${name}: h=${rect.h} <= 0 (invalid)`);
            }
            // Check minimum dimensions
            const isCommand = name === 'command';
            const minW = isCommand ? CMD_MIN_W : MIN_PANEL_W;
            const minH = isCommand ? CMD_MIN_H : MIN_PANEL_H;
            if (rect.w < minW) {
                errors.push(`${name}: w=${rect.w} < min=${minW}`);
            }
            if (rect.h < minH) {
                errors.push(`${name}: h=${rect.h} < min=${minH}`);
            }
            // Check bounds: x + w <= safeW - 1, y + h <= safeH - 1
            const rightEdge = rect.x + rect.w;
            const bottomEdge = rect.y + rect.h;
            const maxRight = viewport.safe.w - 1;
            const maxBottom = viewport.safe.h - 1;
            if (rightEdge > maxRight) {
                errors.push(`${name}: rightEdge=${rightEdge} > maxRight=${maxRight} (x=${rect.x} w=${rect.w})`);
            }
            if (bottomEdge > maxBottom) {
                errors.push(`${name}: bottomEdge=${bottomEdge} > maxBottom=${maxBottom} (y=${rect.y} h=${rect.h})`);
            }
        }
        // Check max edges from layout spec
        if (spec.maxRightEdge > viewport.safe.w - 1) {
            errors.push(`maxRightEdge=${spec.maxRightEdge} > safeW-1=${viewport.safe.w - 1}`);
        }
        if (spec.maxBottomEdge > viewport.safe.h - 1) {
            errors.push(`maxBottomEdge=${spec.maxBottomEdge} > safeH-1=${viewport.safe.h - 1}`);
        }
        return {
            size,
            passed: errors.length === 0,
            errors,
        };
    }
    catch (error) {
        const errorMsg = error instanceof Error ? error.message : String(error);
        return {
            size,
            passed: false,
            errors: [`Layout computation failed: ${errorMsg}`],
        };
    }
}
/**
 * Run all regression tests
 */
export async function runLayoutTests() {
    const testSizes = [
        { cols: 120, rows: 30 },
        { cols: 160, rows: 40 },
        { cols: 200, rows: 50 },
        { cols: 282, rows: 61 }, // CloudShell size
        { cols: 90, rows: 22 }, // Minimum size
        { cols: 100, rows: 25 }, // Just above minimum
    ];
    console.log('Running MK2 layout regression tests...\n');
    const results = [];
    for (const size of testSizes) {
        const result = await testLayout(size.cols, size.rows);
        results.push(result);
        if (result.passed) {
            console.log(`✅ ${result.size}: PASSED`);
        }
        else {
            console.log(`❌ ${result.size}: FAILED`);
            for (const error of result.errors) {
                console.log(`   - ${error}`);
            }
        }
    }
    const allPassed = results.every(r => r.passed);
    console.log(`\n${results.length} tests run: ${results.filter(r => r.passed).length} passed, ${results.filter(r => !r.passed).length} failed`);
    return allPassed;
}
// Run tests if executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
    runLayoutTests()
        .then(passed => {
        process.exit(passed ? 0 : 1);
    })
        .catch(error => {
        console.error('Test harness error:', error);
        process.exit(1);
    });
}
//# sourceMappingURL=layoutTest.js.map