// Tests extracted from src/domain/chat/templates/parser/mod.rs

use kodegen_candle_agent::domain::chat::templates::TemplateAst;
use kodegen_candle_agent::domain::chat::templates::parser::{
    TemplateParser, validate_template,
};

#[test]
fn test_simple_variable_parsing() -> std::result::Result<(), Box<dyn std::error::Error>> {
    let parser = TemplateParser::new();
    let result = parser.parse("Hello {{name}}!")?;

    match result {
        TemplateAst::Block(nodes) => {
            assert_eq!(nodes.len(), 3);
        }
        _ => panic!("Expected block AST"),
    }
    Ok(())
}

#[test]
fn test_variable_extraction() -> std::result::Result<(), Box<dyn std::error::Error>> {
    let parser = TemplateParser::new();
    let variables = parser.extract_variables("Hello {{name}}, you have {{count}} messages.")?;

    assert_eq!(variables.len(), 2);
    assert!(variables.iter().any(|v| v.name.as_str() == "name"));
    assert!(variables.iter().any(|v| v.name.as_str() == "count"));
    Ok(())
}

#[test]
fn test_template_validation() {
    assert!(validate_template("Hello {{name}}!").is_ok());
    assert!(validate_template("{{unclosed").is_err()); // Parser correctly rejects unclosed expressions
}
