// Hours estimate per layer type
const LAYER_HOURS = {
    unit: 2,
    integration: 4,
    e2e: 6,
    contract: 4,
    visual: 3,
    load: 3,
    security: 4,
    a11y: 2,
};
// Priority order (lower index = higher priority for sort)
const PRIORITY_ORDER = {
    critical: 0,
    high: 1,
    medium: 2,
    low: 3,
};
function hasUiApps(knowledge) {
    return knowledge.apps.some((a) => a.type === 'frontend' ||
        a.type === 'mobile' ||
        a.type === 'ios' ||
        a.type === 'android' ||
        a.type === 'desktop');
}
function hasWebUiApps(knowledge) {
    return knowledge.apps.some((a) => a.type === 'frontend');
}
function hasBackendApps(knowledge) {
    return knowledge.apps.some((a) => a.type === 'backend' || a.type === 'worker');
}
function hasFullstackApps(knowledge) {
    return knowledge.apps.some((a) => a.type === 'frontend' || a.type === 'backend');
}
function isMicroservices(knowledge) {
    const arch = knowledge.architecture.primary;
    return (arch === 'microservices' ||
        knowledge.apps.filter((a) => a.type === 'backend' || a.type === 'worker').length > 1);
}
function hasAuth(knowledge) {
    const authTerms = ['auth', 'jwt', 'oauth', 'passport', 'keycloak', 'auth0', 'clerk', 'supabase'];
    return authTerms.some((term) => knowledge.stack.some((s) => s.toLowerCase().includes(term)));
}
function detectFrameworksForLayer(layerType, knowledge) {
    const lang = knowledge.language;
    const unitFrameworks = {
        typescript: ['vitest', 'jest'],
        javascript: ['vitest', 'jest'],
        python: ['pytest'],
        go: ['go test'],
        rust: ['cargo test'],
        java: ['JUnit 5'],
        kotlin: ['JUnit 5', 'Kotest'],
        swift: ['XCTest'],
        dart: ['flutter_test'],
        csharp: ['xUnit', 'NUnit'],
        php: ['PHPUnit'],
        ruby: ['RSpec'],
    };
    const e2eFrameworks = knowledge.stack.includes('playwright')
        ? ['Playwright']
        : knowledge.stack.includes('cypress')
            ? ['Cypress']
            : ['Playwright'];
    const frameworkMap = {
        unit: unitFrameworks[lang] ?? ['vitest'],
        integration: unitFrameworks[lang] ?? ['vitest'],
        e2e: e2eFrameworks,
        contract: ['Pact', 'Schemathesis'],
        visual: ['Playwright', 'Percy', 'Chromatic'],
        load: ['k6', 'Locust', 'Gatling'],
        security: ['OWASP ZAP', 'Trivy', 'Snyk'],
        a11y: ['axe-core', 'Playwright', 'pa11y'],
    };
    return frameworkMap[layerType] ?? [];
}
function detectToolsForLayer(layerType, knowledge) {
    const toolMap = {
        unit: ['Coverage reporter', 'Test runner'],
        integration: ['Supertest', 'Testcontainers', 'Docker Compose'],
        e2e: knowledge.stack.includes('playwright') ? ['Playwright', 'playwright MCP'] : ['Playwright'],
        contract: ['Pact Broker', 'Swagger Validator'],
        visual: ['Percy', 'Chromatic', 'BackstopJS'],
        load: ['k6', 'Artillery', 'autocannon'],
        security: ['OWASP ZAP', 'Semgrep', 'Snyk', 'Trivy'],
        a11y: ['axe DevTools', 'Lighthouse CI', 'chrome-devtools MCP'],
    };
    const tools = toolMap[layerType] ?? [];
    // Add language-specific tools
    if (layerType === 'unit' && knowledge.language === 'python') {
        return ['pytest-cov', 'pytest-mock'];
    }
    if (layerType === 'integration' && knowledge.database !== 'unknown') {
        return [...tools, 'Testcontainers'];
    }
    return tools;
}
function buildLayer(type, needed, reason, priority, knowledge) {
    return {
        type,
        needed,
        reason,
        priority,
        estimatedHours: needed ? LAYER_HOURS[type] : 0,
        tools: needed ? detectToolsForLayer(type, knowledge) : [],
        frameworks: needed ? detectFrameworksForLayer(type, knowledge) : [],
    };
}
function buildMcpRecommendations(knowledge) {
    const recs = [];
    if (hasWebUiApps(knowledge) || hasUiApps(knowledge)) {
        recs.push('playwright MCP — browser automation for e2e and visual tests');
    }
    if (hasUiApps(knowledge)) {
        recs.push('chrome-devtools MCP — accessibility audits and performance snapshots');
    }
    return recs;
}
export function generateTestPlan(knowledge) {
    const hasDatabase = knowledge.database !== 'unknown';
    const hasExternalServices = knowledge.externalServices.length > 0;
    const hasApiContracts = knowledge.apiContracts.length > 0;
    const hasMultipleLayers = knowledge.layers.length > 1 || knowledge.apps.length > 1;
    const noApps = knowledge.apps.length === 0;
    const layers = [
        buildLayer('unit', true, 'Every project needs unit tests for business logic', 'high', knowledge),
        buildLayer('integration', hasMultipleLayers || hasExternalServices || hasDatabase, hasDatabase
            ? 'Database detected — integration tests required'
            : hasExternalServices
                ? 'External services detected — integration tests required'
                : 'Multiple layers detected — integration tests required', 'high', knowledge),
        buildLayer('e2e', hasUiApps(knowledge) || hasFullstackApps(knowledge), hasUiApps(knowledge)
            ? 'UI application detected — e2e user flows required'
            : 'Fullstack app detected — e2e tests required', 'high', knowledge),
        buildLayer('contract', isMicroservices(knowledge) || hasApiContracts || knowledge.apps.length > 1, isMicroservices(knowledge)
            ? 'Microservices architecture — contract tests prevent integration drift'
            : hasApiContracts
                ? 'API contracts detected — consumer-driven contract tests recommended'
                : 'Multiple services detected — contract tests recommended', 'high', knowledge),
        buildLayer('visual', hasUiApps(knowledge), 'UI framework detected — visual regression tests prevent unintended style changes', 'medium', knowledge),
        buildLayer('load', hasBackendApps(knowledge) || hasFullstackApps(knowledge), 'API endpoints detected — load tests validate performance under traffic', 'medium', knowledge),
        buildLayer('security', hasAuth(knowledge) || knowledge.externalServices.length > 0, hasAuth(knowledge)
            ? 'Authentication detected — security tests required for auth flows'
            : 'External services detected — security scanning recommended', 'critical', knowledge),
        buildLayer('a11y', hasWebUiApps(knowledge), 'Web frontend detected — accessibility tests required for WCAG compliance', 'medium', knowledge),
    ];
    // Sort needed layers by priority, then keep all layers in result
    layers.sort((a, b) => {
        if (a.needed !== b.needed) {
            return a.needed ? -1 : 1;
        }
        return PRIORITY_ORDER[a.priority] - PRIORITY_ORDER[b.priority];
    });
    const totalEstimatedHours = layers
        .filter((l) => l.needed)
        .reduce((sum, l) => sum + l.estimatedHours, 0);
    return {
        projectId: knowledge.projectId,
        generatedAt: new Date().toISOString(),
        layers,
        totalEstimatedHours,
        mcpRecommendations: buildMcpRecommendations(knowledge),
        starterKit: noApps || knowledge.language === 'unknown',
    };
}
//# sourceMappingURL=test-plan-generator.js.map