//! v5.0 C3 S3 — presence test for the cycle capstone + roadmap sync.
//!
//! Locks in two structural promises so future edits can't silently regress
//! the v5.0 cycle close:
//!   1. `docs/plan-history/v5.0-vs-maestro-report.md` exists with the 9
//!      H2 sections the cycle-close template requires.
//!   2. `docs/roadmap.md`'s v5.0 row is marked done (no `🔄 active`).
//!
//! Run:
//!   cargo test -p smix-sdk --test c3_capstone_presence

use std::path::PathBuf;

fn repo_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .and_then(|p| p.parent())
        .expect("smix-sdk lives under <repo>/crates")
        .to_path_buf()
}

#[test]
fn case1_v5_capstone_file_present() {
    let path = repo_root().join("docs/plan-history/v5.0-vs-maestro-report.md");
    assert!(
        path.is_file(),
        "expected v5.0 capstone at {path:?}; cycle close requires it"
    );
}

#[test]
fn case2_v5_capstone_has_nine_h2_sections() {
    let path = repo_root().join("docs/plan-history/v5.0-vs-maestro-report.md");
    let body = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {path:?}: {e}"));
    let h2_lines: Vec<&str> = body
        .lines()
        .filter(|line| line.starts_with("## "))
        .collect();
    assert!(
        h2_lines.len() >= 9,
        "capstone has {} H2 sections, expected ≥ 9. lines: {:?}",
        h2_lines.len(),
        h2_lines
    );
    // The schema (sub-string match, in order) — borrowed from v3.14
    // capstone with v5.0 adjusted naming (gate infra cycle, not new cap
    // surface):
    let required_substrings: [&[&str]; 9] = [
        &["概述"],                      // §0
        &["checkpoint", "进度"],        // §1
        &["原因"],                      // §2
        &["修法"],                      // §3
        &["验证", "出口"],              // §4
        &["capability", "矩阵"],        // §5
        &["maestro", "parity"],         // §6
        &["不变量"],                    // §7
        &["未做", "backlog", "下一段"], // §8
    ];
    let mut idx = 0;
    for expected in required_substrings.iter() {
        let found = (idx..h2_lines.len()).find(|i| {
            let lower = h2_lines[*i].to_lowercase();
            expected.iter().any(|s| lower.contains(&s.to_lowercase()))
        });
        match found {
            Some(i) => idx = i + 1,
            None => panic!(
                "capstone is missing an H2 matching any of {expected:?}; \
                 H2 lines from offset {idx}: {:?}",
                &h2_lines[idx..]
            ),
        }
    }
}

#[test]
fn case3_roadmap_v5_row_marked_done() {
    let path = repo_root().join("docs/roadmap.md");
    let body = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {path:?}: {e}"));
    // The v5.0 row starts with `| **v5.0** |` per the roadmap convention.
    let v5_row = body
        .lines()
        .find(|line| line.starts_with("| **v5.0** |"))
        .expect("roadmap.md should have a `| **v5.0** |` row");
    assert!(
        !v5_row.contains("🔄 active"),
        "roadmap.md v5.0 row is still marked `🔄 active`; cycle close \
         requires it flipped to `✅`. row: {v5_row}"
    );
    assert!(
        v5_row.contains("✅"),
        "roadmap.md v5.0 row missing `✅` mark. row: {v5_row}"
    );
}
