//! Tests for OWASP cheat sheet parsers.

use super::parsers::{create_slug, parse_cheatsheet, truncate_description};

#[test]
fn test_parse_authentication_sheet() {
    let content = r#"
        # Authentication Best Practices

        ## Multi-Factor Authentication

        Multi-factor authentication (MFA) or 2FA should be implemented.

        ## Password Requirements

        The minimum password length should be at least 8 characters.

        ## Account Lockout

        Account lockout should be enabled to prevent brute force attacks.

        Use bcrypt or Argon2 for password hashing.
    "#;

    let recs = parse_cheatsheet(content, "authentication");

    assert!(recs.iter().any(|r| r.subject.contains("mfa")), "Should find MFA recommendation");
    assert!(
        recs.iter().any(|r| r.subject.contains("password_length")),
        "Should find password length"
    );
    assert!(
        recs.iter().any(|r| r.subject.contains("account_lockout")),
        "Should find account lockout"
    );
}

#[test]
fn test_parse_jwt_sheet() {
    let content = r#"
        # JWT Security

        The algorithm header must be validated.
        The "none" algorithm must not be accepted.
        Verify the signature before trusting the claims.
        Check the expiration claim.
    "#;

    let recs = parse_cheatsheet(content, "jwt");

    assert!(
        recs.iter().any(|r| r.subject.contains("algorithm")),
        "Should find algorithm validation"
    );
    assert!(
        recs.iter().any(|r| r.subject.contains("none_algorithm")),
        "Should find none algorithm rejection"
    );
}

#[test]
fn test_parse_tls_sheet() {
    let content = r#"
        # TLS Configuration

        Use TLS 1.2 or TLS 1.3.
        Always verify the certificate chain.
        Configure strong cipher suites.
        Enable HSTS.
    "#;

    let recs = parse_cheatsheet(content, "tls");

    assert!(recs.iter().any(|r| r.subject.contains("min_version")), "Should find min version");
    assert!(
        recs.iter().any(|r| r.subject.contains("cert_verification")),
        "Should find cert verification"
    );
}

#[test]
fn test_parse_secrets_sheet() {
    let content = r#"
        # Secrets Management

        Never hardcode secrets in your code.
        Store secrets in environment variables or a vault.
        Rotate secrets regularly.
        Encrypt secrets at rest.
    "#;

    let recs = parse_cheatsheet(content, "secrets");

    assert!(recs.iter().any(|r| r.subject.contains("hardcoded")), "Should find hardcoded warning");
    assert!(
        recs.iter().any(|r| r.subject.contains("storage_method")),
        "Should find storage method"
    );
}

#[test]
fn test_create_slug() {
    assert_eq!(create_slug("Hello World!"), "hello_world");
    assert_eq!(create_slug("Use TLS 1.2"), "use_tls_1_2");
}

#[test]
fn test_truncate_description() {
    assert_eq!(truncate_description("short", 100), "short");
    assert_eq!(
        truncate_description("a".repeat(150).as_str(), 100),
        format!("{}...", "a".repeat(97))
    );
}
