#![cfg(feature = "bench")]

use pdfmake_rust::benchmark_report::{BenchmarkComparison, format_markdown_report, format_text_report};

#[test]
fn format_markdown_report_contains_status_and_change() {
    let rows = vec![BenchmarkComparison {
        name: "text_10k".to_string(),
        baseline_ms: 5.08,
        current_ms: 5.32,
        change_pct: 4.7,
        baseline_pdf_size_kb: 142.0,
        current_pdf_size_kb: 143.0,
        status: "通过".to_string(),
    }];

    let report = format_markdown_report(&rows);
    assert!(report.contains("通过"));
    assert!(report.contains("+4.7%"));
    assert!(report.contains("142.0 KB -> 143.0 KB"));
}

#[test]
fn format_markdown_report_has_table_header() {
    let rows = vec![BenchmarkComparison {
        name: "text_10k".to_string(),
        baseline_ms: 5.08,
        current_ms: 5.32,
        change_pct: 4.7,
        baseline_pdf_size_kb: 142.0,
        current_pdf_size_kb: 143.0,
        status: "通过".to_string(),
    }];

    let report = format_markdown_report(&rows);
    assert!(report.contains("场景"));
    assert!(report.contains("基准值"));
    assert!(report.contains("当前值"));
    assert!(report.contains("变化"));
    assert!(report.contains("状态"));
}

#[test]
fn format_markdown_report_multiple_rows() {
    let rows = vec![
        BenchmarkComparison {
            name: "text_10k".to_string(),
            baseline_ms: 5.08,
            current_ms: 5.32,
            change_pct: 4.7,
            baseline_pdf_size_kb: 142.0,
            current_pdf_size_kb: 143.0,
            status: "通过".to_string(),
        },
        BenchmarkComparison {
            name: "table_1000".to_string(),
            baseline_ms: 280.0,
            current_ms: 310.0,
            change_pct: 10.7,
            baseline_pdf_size_kb: 500.0,
            current_pdf_size_kb: 512.0,
            status: "失败".to_string(),
        },
    ];

    let report = format_markdown_report(&rows);
    assert!(report.contains("text_10k"));
    assert!(report.contains("table_1000"));
    assert!(report.contains("通过"));
    assert!(report.contains("失败"));
}

#[test]
fn format_markdown_report_empty_rows() {
    let rows: Vec<BenchmarkComparison> = vec![];
    let report = format_markdown_report(&rows);
    // 仍应包含表头
    assert!(report.contains("场景"));
    assert!(report.contains("|---"));
}

#[test]
fn format_markdown_report_negative_change() {
    let rows = vec![BenchmarkComparison {
        name: "text_10k".to_string(),
        baseline_ms: 10.0,
        current_ms: 8.0,
        change_pct: -20.0,
        baseline_pdf_size_kb: 142.0,
        current_pdf_size_kb: 140.0,
        status: "警告".to_string(),
    }];

    let report = format_markdown_report(&rows);
    assert!(report.contains("-20.0%"));
    assert!(report.contains("警告"));
}

#[test]
fn format_text_report_contains_overall_verdict() {
    let rows = vec![BenchmarkComparison {
        name: "text_10k".to_string(),
        baseline_ms: 5.08,
        current_ms: 5.32,
        change_pct: 4.7,
        baseline_pdf_size_kb: 142.0,
        current_pdf_size_kb: 143.0,
        status: "通过".to_string(),
    }];

    let report = format_text_report(&rows);
    assert!(report.contains("总评: 通过"));
}

#[test]
fn format_text_report_overall_fail_when_any_fail() {
    let rows = vec![
        BenchmarkComparison {
            name: "text_10k".to_string(),
            baseline_ms: 5.08,
            current_ms: 5.32,
            change_pct: 4.7,
            baseline_pdf_size_kb: 142.0,
            current_pdf_size_kb: 143.0,
            status: "通过".to_string(),
        },
        BenchmarkComparison {
            name: "table_1000".to_string(),
            baseline_ms: 286.4,
            current_ms: 345.3,
            change_pct: 20.6,
            baseline_pdf_size_kb: 512.0,
            current_pdf_size_kb: 512.0,
            status: "失败".to_string(),
        },
    ];

    let report = format_text_report(&rows);
    assert!(report.contains("总评: 失败"));
}
