/*******************************************************************************
 *
 *    Copyright (c) 2025 - 2026 Haixing Hu.
 *
 *    SPDX-License-Identifier: Apache-2.0
 *
 *    Licensed under the Apache License, Version 2.0.
 *
 ******************************************************************************/
//! README consistency checks for `qubit-batch`.

const CARGO_TOML: &str = include_str!("../../Cargo.toml");
const README_EN: &str = include_str!("../../README.md");
const README_ZH: &str = include_str!("../../README.zh_CN.md");
const PARALLEL_BATCH_EXECUTOR: &str =
    include_str!("../../src/execute/impls/parallel_batch_executor.rs");
const PARALLEL_BATCH_PROCESSOR: &str =
    include_str!("../../src/process/impls/parallel_batch_processor.rs");

#[test]
/// Ensures README dependency snippets use the same major.minor line as `[package] version`.
fn test_readme_dependency_version_matches_cargo_toml() {
    let cargo_version =
        extract_package_version(CARGO_TOML).expect("Failed to extract version from Cargo.toml");
    let expected = minor_series(cargo_version).expect("Cargo.toml version must have major.minor");
    let readme_en_version = extract_readme_dependency_version(README_EN)
        .expect("Failed to extract version from README.md");
    let readme_zh_version = extract_readme_dependency_version(README_ZH)
        .expect("Failed to extract version from README.zh_CN.md");
    assert_eq!(readme_en_version, expected.as_str());
    assert_eq!(readme_zh_version, expected.as_str());
}

#[test]
/// Ensures both README files document the current executor types.
fn test_readme_mentions_current_executor_types() {
    assert!(README_EN.contains("SequentialBatchExecutor"));
    assert!(README_EN.contains("ParallelBatchExecutor"));
    assert!(README_ZH.contains("SequentialBatchExecutor"));
    assert!(README_ZH.contains("ParallelBatchExecutor"));
}

#[test]
/// Ensures parallel implementations use the shared scoped progress guard.
fn test_parallel_progress_reporting_uses_scoped_progress_guard() {
    assert!(PARALLEL_BATCH_EXECUTOR.contains("spawn_running_reporter"));
    assert!(PARALLEL_BATCH_PROCESSOR.contains("spawn_running_reporter"));
    assert!(!PARALLEL_BATCH_EXECUTOR.contains("RunningProgressLoop"));
    assert!(!PARALLEL_BATCH_PROCESSOR.contains("RunningProgressLoop"));
}

/// Returns `major.minor` from a semver string (e.g. `0.5.1` → `0.5`).
fn minor_series(version: &str) -> Option<String> {
    let mut parts = version.split('.');
    let major = parts.next()?;
    let minor = parts.next()?;
    Some(format!("{major}.{minor}"))
}

/// Extracts the first package version entry from Cargo.toml content.
fn extract_package_version(content: &str) -> Option<&str> {
    for line in content.lines() {
        if let Some(value) = line.strip_prefix("version = \"") {
            return value.strip_suffix('"');
        }
    }
    None
}

/// Extracts the `qubit-batch` dependency version from a README file.
fn extract_readme_dependency_version(content: &str) -> Option<&str> {
    for line in content.lines() {
        if let Some(value) = line.trim().strip_prefix("qubit-batch = \"") {
            return value.strip_suffix('"');
        }
    }
    None
}
