//! Wall-clock perf assertion for the drift detector. Generates a
//! synthetic 200-feature × 200-line repo and confirms `Registry::run_all`
//! finishes well under 2 s (the spec target). Not a statistical
//! benchmark — that lives in CI bench infra — but it catches
//! order-of-magnitude regressions in unit-test time.
//!
//! Lives under `benches/` per cargo convention but is a `tests/`-style
//! `#[test]` so `cargo test` picks it up. Adding the criterion
//! harness is a v1.x upgrade path that keeps this contract.

use koala_core::invariant::Context;
use koala_drift::Registry;
use std::fs;
use std::time::Instant;
use tempfile::TempDir;

fn build_fixture(root: &std::path::Path, features: usize) {
    let dir = root.join("wiki/features");
    fs::create_dir_all(&dir).unwrap();
    for i in 0..features {
        let body = format!(
            "---\nid: feat-{i:04}\nstatus: in-progress\nowner: crates/x/\n---\n\
             # Feature {i}\n\n## Acceptance criteria（机械可验证）\n\n\
             - [ ] does the thing\n  → crates/x/tests/missing.rs#none（待创建）\n"
        );
        fs::write(dir.join(format!("feat-{i:04}.md")), body).unwrap();
    }
}

#[test]
fn drift_p99_under_two_seconds() {
    let tmp = TempDir::new().unwrap();
    build_fixture(tmp.path(), 200);
    let ctx = Context::new(tmp.path().to_path_buf());
    let reg = Registry::builtin();

    // Warm one iteration to populate any caches (filesystem, walkdir).
    let _ = reg.run_all(&ctx);

    let mut samples = Vec::with_capacity(10);
    for _ in 0..10 {
        let start = Instant::now();
        let _ = reg.run_all(&ctx);
        samples.push(start.elapsed());
    }
    samples.sort();
    let p99 = samples[samples.len() - 1]; // 10 samples → max ≈ p99

    assert!(
        p99.as_secs() < 2,
        "drift run_all p99 = {p99:?}; spec target ≤ 2s on 200 features"
    );
}
