// tools/challenge-spec/architecture-challenge-scenarios.ts — DDD and XP challenges (SPEC-033)
// === DDD Aggregate Integrity Challenges ===
/**
 * Generates aggregate integrity challenges for DDD projects.
 * Analyzes the spec title and description to generate invariant violation scenarios.
 */
export function generateDddChallenges(spec, specContent) {
    const scenarios = [];
    // Check if the spec relates to a domain aggregate
    const hasAggregateContext = /aggregate|domain\s+event|invariant|bounded.context|value\s+object/i.test(specContent) ||
        /create|update|delete|command/i.test(spec.title);
    if (!hasAggregateContext) {
        return scenarios;
    }
    // Scenario 1: Aggregate invariant violation on concurrent modification
    scenarios.push({
        scenario: `[DDD] Two commands arrive simultaneously targeting the same aggregate in "${spec.title}". ` +
            'If the spec does not specify optimistic locking or version-based conflict detection, ' +
            'the last writer wins and invariants may be silently violated.',
        probability: 'medium',
        impact: 'high',
        currentHandling: /version|etag|lock|concurrent/i.test(specContent)
            ? 'Partially addressed'
            : 'Not addressed in spec',
        requiredHandling: 'Add explicit version/ETag to the aggregate. ' +
            'Spec must declare: "Concurrent modification → return Conflict (409) with current version".',
        dataConsistency: 'Aggregate invariants must hold under concurrent access',
        userExperience: 'User sees a conflict error with the option to reload and retry',
    });
    // Scenario 2: Resource constraint at zero/limit
    const hasStockOrBalance = /stock|balance|quantity|limit|capacity/i.test(specContent);
    if (hasStockOrBalance) {
        scenarios.push({
            scenario: `[DDD] The spec for "${spec.title}" does not specify the aggregate behavior ` +
                'when a resource constraint is at its limit (e.g., stock = 0, balance = 0, capacity full). ' +
                'What is the aggregate state after the rejection? Does it emit a domain event?',
            probability: 'high',
            impact: 'high',
            currentHandling: /reject|refuse|insufficient|not enough|unavailable/i.test(specContent)
                ? 'Partially addressed'
                : 'Not addressed in spec',
            requiredHandling: 'Add: "When [resource] is at limit, the aggregate rejects the command with [DomainError] ' +
                'and remains in [state]. No domain event is emitted."',
            dataConsistency: 'Aggregate must reject commands that would violate invariants',
            userExperience: 'User sees a specific error message explaining why the action failed',
        });
    }
    // Scenario 3: Cross-boundary domain event without ACL
    const hasCrossBoundary = /integration|external|downstream|consumer|publish|emit/i.test(specContent);
    if (hasCrossBoundary) {
        scenarios.push({
            scenario: `[DDD] The spec for "${spec.title}" implies integration with another bounded context ` +
                'but does not specify the ACL, Published Language, or Conformist relationship. ' +
                'The downstream context may adopt the upstream model without translation, creating coupling.',
            probability: 'medium',
            impact: 'medium',
            currentHandling: /acl|anti.corruption|published.language|conformist/i.test(specContent)
                ? 'Integration pattern specified'
                : 'Integration pattern not specified',
            requiredHandling: 'Spec must declare: "Integration with [DownstreamContext] uses [ACL/Published Language/Conformist]. ' +
                'Event schema: [schema]. ACL translation logic lives in [module]."',
            dataConsistency: 'Context boundaries must be respected to prevent model contamination',
            userExperience: 'Transparent to user — prevents future regressions from coupling',
        });
    }
    return scenarios;
}
// === XP Pair Programming Clarity Challenge ===
/**
 * Generates pair programming clarity challenge for XP projects.
 * Evaluates if two developers could implement the spec without additional clarification.
 */
export function generateXpPairClarityChallenge(spec, specContent) {
    const scenarios = [];
    const ambiguousTerms = [];
    // Detect potentially ambiguous terms
    const vaguePatterns = [
        [/\bappropriate\b/i, '"appropriate" (no definition)'],
        [/\bsoon\b|\bquickly\b|\bfast\b/i, '"soon/quickly/fast" (no time bound)'],
        [/\bsome\b/i, '"some" (no quantity)'],
        [/\bproperly\b|\bcorrectly\b/i, '"properly/correctly" (no definition of proper)'],
        [/\bif\s+needed\b|\bif\s+applicable\b/i, '"if needed/if applicable" (no condition)'],
        [/\betc\b|\band\s+so\s+on\b/i, '"etc./and so on" (incomplete list)'],
        [/\bmight\b|\bcould\b|\bshould\s+consider\b/i, 'weak modal (implies optional behavior)'],
    ];
    for (const [pattern, label] of vaguePatterns) {
        if (pattern.test(specContent)) {
            ambiguousTerms.push(label);
        }
    }
    // Check if acceptance criteria follow Given/When/Then
    const hasBehavioralCriteria = /given\s+.+when\s+.+then/is.test(specContent);
    if (!hasBehavioralCriteria) {
        ambiguousTerms.push('criteria not in Given/When/Then (XP requires executable acceptance tests)');
    }
    if (ambiguousTerms.length > 0) {
        scenarios.push({
            scenario: `[XP] Two developers starting a pairing session on "${spec.title}" without prior context ` +
                'may interpret these terms differently: ' +
                ambiguousTerms.join('; ') +
                '. This would require stopping the session to seek clarification.',
            probability: ambiguousTerms.length >= 3 ? 'high' : 'medium',
            impact: 'medium',
            currentHandling: 'Spec contains ambiguous or non-behavioral criteria',
            requiredHandling: 'Rewrite ambiguous criteria using Given/When/Then with explicit values. ' +
                'Each acceptance criterion should be independently verifiable as a test.',
            dataConsistency: 'Not applicable',
            userExperience: 'Prevents misimplementation and reduces mid-session interruptions',
        });
    }
    return scenarios;
}
//# sourceMappingURL=architecture-challenge-scenarios.js.map