// tools/generate-tests/generators/concurrency-test-generator.ts — SPEC-034
// Generates concurrency, resilience, and Pact consumer contract test scaffolds.
// Delegates to sub-modules in ./concurrency-test-generator/
import { generateJsConcurrencyTest, generateJsPactTest, } from './concurrency-test-generator/js-templates.js';
import { generateGoConcurrencyTest } from './concurrency-test-generator/go-templates.js';
import { generateJavaConcurrencyTest, generateJavaPactTest, } from './concurrency-test-generator/java-templates.js';
import { generatePythonConcurrencyTest, generateRustConcurrencyTest, generatePythonPactTest, } from './concurrency-test-generator/python-rust-templates.js';
import { getConcurrencyModelForLanguage, getTestExt } from './concurrency-test-generator/utils.js';
/**
 * Returns true when the project has shared-state or external-call signals.
 */
export function isConcurrencyProject(knowledge) {
    const stackLower = knowledge.stack.map((s) => s.toLowerCase());
    return (stackLower.some((s) => ['redis', 'memcached', 'shared-state', 'mutex', 'concurrent'].some((kw) => s.includes(kw))) ||
        knowledge.language === 'go' ||
        knowledge.language === 'java' ||
        knowledge.language === 'rust');
}
/**
 * Returns true when the project has microservice/API signals for Pact contract testing.
 */
export function isMicroserviceProject(knowledge) {
    const stackLower = knowledge.stack.map((s) => s.toLowerCase());
    return (stackLower.some((s) => ['grpc', 'pact', 'openapi', 'protobuf', 'proto'].some((kw) => s.includes(kw))) || knowledge.isMicroservice === true);
}
/**
 * Generate test definitions for concurrency and resilience testing.
 */
export function generateConcurrencyTestDefs(title, testDir, language) {
    const defs = [];
    const model = getConcurrencyModelForLanguage(language);
    defs.push({
        name: `${title} — Race Condition: N concurrent writers produce correct final state`,
        type: 'integration',
        file: `${testDir}/concurrency/`,
        description: 'Launch N concurrent goroutines/threads/tasks performing the same write operation; verify final state equals expected value without lost updates',
        priority: 'critical',
        automatable: true,
    });
    if (model === 'threads-locks' || model === 'goroutines') {
        defs.push({
            name: `${title} — Deadlock: lock acquisition timeout does not hang test suite`,
            type: 'integration',
            file: `${testDir}/concurrency/`,
            description: 'Attempt to acquire locks in the order that could cause deadlock; if lock is not released within N seconds, the test fails with a clear message',
            priority: 'high',
            automatable: true,
        });
    }
    defs.push({
        name: `${title} — Starvation: all concurrent actors make progress within bounded time`,
        type: 'integration',
        file: `${testDir}/concurrency/`,
        description: 'One actor monopolizes the resource for K operations; verify other actors wait no more than T milliseconds on average',
        priority: 'high',
        automatable: true,
    });
    return defs;
}
/**
 * Generate test definitions for Pact consumer contract tests.
 */
export function generatePactTestDefs(title, testDir) {
    return [
        {
            name: `${title} — Pact Consumer: defines contract for provider interaction`,
            type: 'integration',
            file: `${testDir}/contracts/`,
            description: 'Define the Pact consumer contract: what endpoint is called, what request is sent, what response is expected',
            priority: 'high',
            automatable: true,
        },
        {
            name: `${title} — Pact Provider: verifies all consumer contracts are still satisfied`,
            type: 'integration',
            file: `${testDir}/contracts/`,
            description: 'Load all consumer pact files and replay each interaction against the real provider; fail if any contract is broken',
            priority: 'high',
            automatable: true,
        },
    ];
}
/**
 * Generate test file scaffolds for concurrency testing.
 */
export function generateConcurrencyTestFiles(spec, testDir, testFramework, language, _knowledge, autoGenerate) {
    const slug = spec.id.toLowerCase().replace(/[^a-z0-9]/g, '-');
    const ext = getTestExt(language);
    const model = getConcurrencyModelForLanguage(language);
    const content = language === 'go'
        ? generateGoConcurrencyTest(spec.title)
        : language === 'java' || language === 'kotlin'
            ? generateJavaConcurrencyTest(spec.title)
            : language === 'python'
                ? generatePythonConcurrencyTest(spec.title)
                : language === 'rust'
                    ? generateRustConcurrencyTest(spec.title)
                    : generateJsConcurrencyTest(spec.title, testFramework, model);
    return [
        {
            path: `${testDir}/concurrency/${slug}.concurrency.test.${ext}`,
            framework: testFramework,
            content,
            ready: autoGenerate,
        },
    ];
}
/**
 * Generate test file scaffolds for Pact consumer contract tests.
 */
export function generatePactTestFiles(spec, testDir, testFramework, language, autoGenerate) {
    const slug = spec.id.toLowerCase().replace(/[^a-z0-9]/g, '-');
    const ext = getTestExt(language);
    const content = language === 'java' || language === 'kotlin'
        ? generateJavaPactTest(spec.title)
        : language === 'python'
            ? generatePythonPactTest(spec.title)
            : generateJsPactTest(spec.title, testFramework);
    return [
        {
            path: `${testDir}/contracts/${slug}.pact.test.${ext}`,
            framework: testFramework,
            content,
            ready: autoGenerate,
        },
    ];
}
//# sourceMappingURL=concurrency-test-generator.js.map