from app.services.testing_pipeline.pipeline import run_testing_pipeline
from app.services.testing_pipeline.rules import REQUIREMENT_TYPES


def test_requirement_types_cover_all_docx_types() -> None:
    expected_types = {
        "功能测试",
        "性能测试",
        "外部接口测试",
        "人机交互界面测试",
        "强度测试",
        "余量测试",
        "可靠性测试",
        "安全性测试",
        "恢复性测试",
        "边界测试",
        "安装性测试",
        "互操作性测试",
        "敏感性测试",
        "测试充分性要求",
    }
    assert set(REQUIREMENT_TYPES) == expected_types


def test_identify_requirement_type_for_interface_requirement() -> None:
    result = run_testing_pipeline(
        user_requirement_text="请针对外部接口的入参格式、出参内容和异常返回进行测试分解",
        debug=True,
        use_model_generation=False,
    )

    assert result["requirement_type"] == "外部接口测试"
    assert len(result["test_items"]["normal"]) > 0
    assert len(result["test_items"]["abnormal"]) > 0


def test_unknown_type_fallback_still_generates_content() -> None:
    result = run_testing_pipeline(
        user_requirement_text="请给我一份跨域的综合验证策略，重点关注体系化和可追溯性。",
        debug=True,
        use_model_generation=False,
    )

    normal_item_ids = [item["id"] for item in result["test_items"]["normal"]]
    abnormal_item_ids = [item["id"] for item in result["test_items"]["abnormal"]]
    normal_case_item_ids = [case["item_id"] for case in result["test_cases"]["normal"]]
    abnormal_case_item_ids = [case["item_id"] for case in result["test_cases"]["abnormal"]]

    assert result["requirement_type"] in {"未知类型", "功能测试", "边界测试", "性能测试"}
    assert len(result["test_items"]["normal"]) > 0
    assert len(result["test_items"]["abnormal"]) > 0
    assert set(normal_item_ids).issubset(set(normal_case_item_ids))
    assert set(abnormal_item_ids).issubset(set(abnormal_case_item_ids))


def test_output_mapping_and_formatting() -> None:
    result = run_testing_pipeline(
        user_requirement_text="系统需要验证边界值、非法输入和状态转换端点处理能力",
        debug=True,
        use_model_generation=False,
    )

    normal_item_ids = {item["id"] for item in result["test_items"]["normal"]}
    normal_case_item_ids = [case["item_id"] for case in result["test_cases"]["normal"]]
    normal_case_ids = [case["id"] for case in result["test_cases"]["normal"]]
    normal_expected_ids = [expected["id"] for expected in result["expected_results"]["normal"]]
    placeholders = {case["expected_result_placeholder"] for case in result["test_cases"]["normal"]}
    step_lengths = [len(case["operation_steps"]) for case in result["test_cases"]["normal"]]
    item_case_counts = {item_id: 0 for item_id in normal_item_ids}
    for item_id in normal_case_item_ids:
        if item_id in item_case_counts:
            item_case_counts[item_id] += 1
    step_names = [log["step_name"] for log in result["step_logs"][:3]]

    assert set(normal_case_item_ids).issubset(normal_item_ids)
    assert normal_item_ids.issubset(set(normal_case_item_ids))
    assert normal_expected_ids == normal_case_ids
    assert all(case_id.startswith(f"{item_id}-C") for case_id, item_id in zip(normal_case_ids, normal_case_item_ids))
    assert all(length >= 5 for length in step_lengths)
    assert any(count >= 2 for count in item_case_counts.values())
    assert placeholders.issubset(
        {
            "{{return_value}}",
            "{{state_change}}",
            "{{error_message}}",
            "{{data_persistence}}",
            "{{ui_display}}",
        }
    )
    assert step_names == [
        "identify-requirement-type",
        "decompose-test-items",
        "generate-test-cases",
    ]
    assert "**测试项**" in result["formatted_output"]
    assert "\n\n**正常测试**：" in result["formatted_output"]
    assert "\n\n**异常测试**：" in result["formatted_output"]
    assert "**测试用例**" in result["formatted_output"]
    assert "**预期成果**" in result["formatted_output"]


def test_identify_adequacy_requirement_type() -> None:
    result = run_testing_pipeline(
        user_requirement_text="请根据测试充分性要求检查需求覆盖率、语句覆盖率和分支覆盖率是否达到100%",
        debug=True,
        use_model_generation=False,
    )

    assert result["requirement_type"] == "测试充分性要求"
    assert len(result["test_items"]["normal"]) > 0
    assert len(result["test_items"]["abnormal"]) > 0


def test_output_should_not_repeat_raw_requirement_context() -> None:
    result = run_testing_pipeline(
        user_requirement_text=(
            "为以下需求生成测试用例：根据作战需求，对电场综合防护设备的静电场防护进行启停8控制，"
            "轴频电场防护出航即开启，同时对外加电流阴极保护设备进行远程控制"
        ),
        debug=True,
        use_model_generation=False,
    )

    formatted = result["formatted_output"]
    assert "需求上下文" not in formatted
    assert "为以下需求生成测试用例" not in formatted
    assert "启停8控制" in formatted
