/**
 * 问题五十一: Test-First / Constraint-Guided Implementation — 编码准备门与测试优先。
 *
 * 强制执行:
 * - coding 不能作为第一步，必须先通过 CodingReadinessGate 和 TestFirstPlan
 * - readiness_status=blocked/manual_required 时不得写实现代码
 * - 有测试框架且行为可断言时必须先写失败测试或回归测试
 * - VerificationPlan 必须从 RequirementGapAnalysis 生成
 */
/**
 * 创建编码准备契约。
 */
export function createCodingReadinessContract(overrides) {
    return {
        project_context_type: "empty_project",
        task_type: "feature",
        consumed_constraints: [],
        required_preconditions: [],
        test_first_plan: {
            test_first_status: "not_required",
            test_mode: "not_applicable_with_reason",
            task_type: "feature",
            test_targets: [],
            acceptance_cases: [],
            must_not_break_cases: [],
            expected_failures_before_implementation: [],
            test_files_to_create_or_update: [],
            verification_commands: [],
            manual_acceptance_steps: [],
            not_applicable_reason: "尚未评估",
        },
        implementation_scope: [],
        verification_plan_ref: "",
        coding_readiness_status: "blocked",
        ...overrides,
    };
}
/**
 * 创建测试优先计划。
 */
export function createTestFirstPlan(hasTestFramework, behaviorAssertable, taskType) {
    if (!hasTestFramework || !behaviorAssertable) {
        return {
            test_first_status: "not_required",
            test_mode: "not_applicable_with_reason",
            task_type: taskType,
            test_targets: [],
            acceptance_cases: [],
            must_not_break_cases: [],
            expected_failures_before_implementation: [],
            test_files_to_create_or_update: [],
            verification_commands: [],
            manual_acceptance_steps: ["手动验收"],
            not_applicable_reason: hasTestFramework ? "行为不可断言" : "无测试框架",
        };
    }
    return {
        test_first_status: "planned",
        test_mode: "failing_test_first",
        task_type: taskType,
        test_targets: [],
        acceptance_cases: [],
        must_not_break_cases: [],
        expected_failures_before_implementation: [],
        test_files_to_create_or_update: [],
        verification_commands: [],
        manual_acceptance_steps: [],
    };
}
/**
 * 评估编码准备状态。
 * - blocked / manual_required 时不得写实现代码
 */
export function evaluateCodingReadiness(contract, hasGapAnalysis, _hasArchitectureDesign, hasVerificationPlan) {
    const missing = [];
    // 编码不能作为第一步
    if (!hasVerificationPlan) {
        missing.push("VerificationPlan: 编码前必须有验证计划");
    }
    // VerificationPlan 必须从 RequirementGapAnalysis 生成
    if (!hasGapAnalysis && contract.project_context_type === "existing_system") {
        missing.push("RequirementGapAnalysis: 现有系统必须有差距分析");
    }
    // 检查约束消费
    for (const precondition of contract.required_preconditions) {
        if (!contract.consumed_constraints.includes(precondition)) {
            missing.push(`未消费前置条件: ${precondition}`);
        }
    }
    // 测试优先检查
    if (contract.test_first_plan.test_first_status === "not_required") {
        // 只有明确理由才能跳过测试优先
        if (!contract.test_first_plan.not_applicable_reason) {
            missing.push("测试优先计划被跳过但未说明理由");
        }
    }
    let status;
    if (missing.length > 0) {
        status = "blocked";
    }
    else if (contract.test_first_plan.test_first_status === "planned") {
        status = "ready_for_test_first";
    }
    else if (contract.test_first_plan.test_first_status === "written" || contract.test_first_plan.test_first_status === "expected_failure_recorded") {
        status = "ready_for_verification_first";
    }
    else {
        status = "blocked";
    }
    return {
        allowed: status === "ready_for_test_first" || status === "ready_for_verification_first",
        status,
        missing_preconditions: missing,
        reason: missing.length === 0 ? "编码准备就绪" : `编码被阻断: ${missing.join("; ")}`,
    };
}
/**
 * 创建约束消费报告。
 */
export function createConstraintConsumptionReport(constraints) {
    const consumed = constraints.map(c => ({
        constraint_id: c.id,
        source: c.source,
        consumption_status: "consumed",
        evidence_refs: [],
    }));
    return {
        consumed_constraints: consumed,
        blocking_missing_constraints: [],
    };
}
/**
 * 执行主线消费检查。
 */
export function performMainlineConsumptionCheck(newFiles, componentsRouted, apiBound) {
    const orphanFindings = [];
    for (const file of newFiles) {
        if (!componentsRouted.some(c => file.includes(c)) && !apiBound.some(a => file.includes(a))) {
            orphanFindings.push(`文件 ${file} 未被主线消费`);
        }
    }
    return {
        new_files_imported: newFiles,
        components_routed_or_rendered: componentsRouted,
        api_bound_to_client_or_router: apiBound,
        services_called_by_controller_or_job: [],
        dto_or_model_used_by_interface_or_data_layer: [],
        config_loaded: [],
        tests_cover_real_path: [],
        orphan_findings: orphanFindings,
    };
}
/**
 * 编码完成审查。
 */
export function reviewCodingCompletion(readinessContract, testFirstSatisfied, implementationDone, mainlineIntegrated, verificationExecuted) {
    const orphanFindings = [];
    if (!testFirstSatisfied && readinessContract.test_first_plan.test_first_status !== "not_required") {
        orphanFindings.push("测试优先计划未满足");
    }
    return {
        review_status: testFirstSatisfied && implementationDone && mainlineIntegrated && verificationExecuted
            ? "passed"
            : "failed",
        test_first_satisfied: testFirstSatisfied,
        implementation_done: implementationDone,
        mainline_integration_satisfied: mainlineIntegrated,
        verification_plan_executed: verificationExecuted,
        relevant_tests_passed_or_justified: testFirstSatisfied,
        manual_required_items_resolved: true,
        orphan_implementation_findings: orphanFindings,
        delivery_readiness_passed: testFirstSatisfied && implementationDone && mainlineIntegrated && verificationExecuted,
    };
}
//# sourceMappingURL=coding_readiness_gate.js.map