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

use mimirs_encoding::SemanticEncoder;

#[test]
fn test_centaur_alignment_benchmark() {
    let dim: usize;

    // 1. Load default encoder
    let encoder = SemanticEncoder::load_default().expect("Failed to load BERT");

    // 2. Encode two semantically related concepts
    let text1 = "Quantum mechanics";
    let text2 = "Schrödinger's cat";

    let vec1 = encoder.encode(text1).expect("Failed to encode");
    let vec2 = encoder.encode(text2).expect("Failed to encode");
    dim = vec1.len();

    // 3. Verify embeddings are non-trivial
    assert!(dim > 0, "Embedding dimension should be > 0");
    assert_eq!(
        vec1.len(),
        vec2.len(),
        "Embeddings should have same dimension"
    );

    // 4. Compute cosine similarity
    let dot: f32 = vec1.iter().zip(vec2.iter()).map(|(a, b)| a * b).sum();
    let norm1: f32 = vec1.iter().map(|a| a * a).sum::<f32>().sqrt();
    let norm2: f32 = vec2.iter().map(|a| a * a).sum::<f32>().sqrt();
    let cosine_sim = dot / (norm1 * norm2 + 1e-8);

    // 5. Related concepts should have positive cosine similarity
    assert!(
        cosine_sim > 0.0,
        "Related concepts should have positive cosine similarity (got {:.4})",
        cosine_sim
    );

    println!(
        "Centaur alignment benchmark: dim={}, cosine_sim={:.4}",
        dim, cosine_sim
    );
}

#[test]
fn test_semantic_discrimination() {
    let encoder = SemanticEncoder::load_default().expect("Failed to load BERT");

    // Encode texts from very different domains
    let text1 = "Stability and Coherence in quantum memory systems";
    let text2 = "Recipe for baking chocolate chip cookies";

    let vec1 = encoder.encode(text1).expect("Failed to encode");
    let vec2 = encoder.encode(text2).expect("Failed to encode");

    // Compute cosine similarity
    let dot: f32 = vec1.iter().zip(vec2.iter()).map(|(a, b)| a * b).sum();
    let norm1: f32 = vec1.iter().map(|a| a * a).sum::<f32>().sqrt();
    let norm2: f32 = vec2.iter().map(|a| a * a).sum::<f32>().sqrt();
    let cosine_sim = dot / (norm1 * norm2 + 1e-8);

    // Unrelated concepts should have lower similarity than related ones
    assert!(
        cosine_sim.abs() < 0.99,
        "Unrelated texts should not have near-perfect similarity (got {:.4})",
        cosine_sim
    );

    println!("Semantic discrimination: cosine_sim={:.4}", cosine_sim);
}
