// tools/challenge-spec/scenarios-failure.ts — Network, database, and resource failure scenarios
import { contentMentions } from './scenarios-utils.js';
export function generateFailureScenarios(spec, content, _knowledge) {
    const scenarios = [];
    const lower = content.toLowerCase();
    // Network failures
    if (spec.target === 'fullstack' ||
        spec.target === 'frontend' ||
        lower.includes('api') ||
        lower.includes('fetch') ||
        lower.includes('request')) {
        scenarios.push({
            scenario: 'API endpoint becomes unreachable (network timeout)',
            probability: 'medium',
            impact: 'high',
            currentHandling: contentMentions(lower, ['timeout', 'retry', 'fallback'])
                ? 'Partially addressed in spec'
                : 'Not addressed in spec',
            requiredHandling: 'Implement request timeout (e.g. 10s), exponential backoff retry (3 attempts), and graceful fallback UI',
            dataConsistency: 'Ensure idempotent operations to prevent duplicate actions on retry',
            userExperience: 'Show offline indicator, queue actions for retry, display stale data if available',
        });
    }
    // Database failures
    if (spec.target === 'backend' ||
        spec.target === 'fullstack' ||
        spec.target === 'database' ||
        lower.includes('database') ||
        lower.includes('query') ||
        lower.includes('schema')) {
        scenarios.push({
            scenario: 'Database connection pool exhausted',
            probability: 'low',
            impact: 'critical',
            currentHandling: 'Not addressed in spec',
            requiredHandling: 'Configure connection pool limits, implement circuit breaker, add health check endpoint',
            dataConsistency: 'In-flight transactions will fail; ensure rollback handling',
            userExperience: 'Return 503 Service Unavailable with retry-after header',
        });
        scenarios.push({
            scenario: 'Migration fails halfway through',
            probability: 'medium',
            impact: 'high',
            currentHandling: contentMentions(lower, ['migration', 'rollback', 'reversible'])
                ? 'Partially addressed'
                : 'Not addressed',
            requiredHandling: 'Use transactional migrations. Every migration must have a reverse. Test migrations on staging first.',
            dataConsistency: 'Partial schema changes can leave database in inconsistent state',
            userExperience: 'Deploy downtime or maintenance mode during migration',
        });
    }
    // Input validation failures
    scenarios.push({
        scenario: 'Malformed or oversized input data',
        probability: 'high',
        impact: 'medium',
        currentHandling: contentMentions(lower, ['validat', 'sanitiz', 'schema', 'zod'])
            ? 'Validation mentioned in spec'
            : 'Not explicitly addressed',
        requiredHandling: 'Validate all inputs at API boundary. Use schema validation (Zod, Joi, etc.). Set payload size limits.',
        dataConsistency: 'Invalid data must never reach the database',
        userExperience: 'Clear validation error messages with field-level detail',
    });
    // Third-party service failures
    if (lower.includes('stripe') ||
        lower.includes('payment') ||
        lower.includes('email') ||
        lower.includes('auth') ||
        lower.includes('external') ||
        lower.includes('webhook')) {
        scenarios.push({
            scenario: 'Third-party service (payment/auth/email) goes down',
            probability: 'medium',
            impact: 'critical',
            currentHandling: contentMentions(lower, ['fallback', 'queue', 'retry', 'circuit'])
                ? 'Partially addressed'
                : 'Not addressed',
            requiredHandling: 'Implement circuit breaker pattern. Queue operations for retry. Have degraded-mode behavior.',
            dataConsistency: 'Use idempotency keys for payment operations. Log all attempts.',
            userExperience: 'Inform user of degraded state. Offer alternative or queue the action.',
        });
    }
    // Memory/resource exhaustion
    if (lower.includes('upload') ||
        lower.includes('file') ||
        lower.includes('stream') ||
        lower.includes('batch') ||
        lower.includes('bulk')) {
        scenarios.push({
            scenario: 'Memory exhaustion from large file upload or batch operation',
            probability: 'medium',
            impact: 'high',
            currentHandling: contentMentions(lower, ['limit', 'stream', 'chunk', 'pagination'])
                ? 'Partially addressed'
                : 'Not addressed',
            requiredHandling: 'Use streaming for large files. Implement file size limits. Process batches in chunks.',
            dataConsistency: 'Partial batch processing must be resumable. Track progress.',
            userExperience: 'Show progress indicator for long operations. Allow cancellation.',
        });
    }
    return scenarios;
}
//# sourceMappingURL=scenarios-failure.js.map