Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

14.9 KB
Raw Blame
//! Tests for [`laya::laya_presets::LayaPresets`] against `laya/presets.py`.

use laya::laya_presets::LayaPresets;
use laya::questions::QuestionType;
use serde_json::Value;

// ── Triage ──────────────────────────────────────────────────────────────────

#[test]
fn triage_question_ids_in_order() {
    let qs = LayaPresets::triage();
    let ids: Vec<&str> = qs.ids().collect();
    assert_eq!(
        vec![
            "intent",
            "is_urgent",
            "frustration",
            "refund_requested",
            "churn_risk"
        ],
        ids
    );
}

#[test]
fn triage_question_types() {
    let qs = LayaPresets::triage();
    assert_eq!(QuestionType::Choice, qs["intent"].question_type());
    assert_eq!(QuestionType::Noul, qs["is_urgent"].question_type());
    assert_eq!(QuestionType::Score, qs["frustration"].question_type());
    assert_eq!(QuestionType::Noul, qs["refund_requested"].question_type());
    assert_eq!(QuestionType::Noul, qs["churn_risk"].question_type());
}

#[test]
fn triage_intent_labels_in_order() {
    let qs = LayaPresets::triage();
    let labels: Vec<&str> = qs["intent"].as_choice().unwrap().labels().collect();
    assert_eq!(
        vec![
            "refund",
            "technical_help",
            "billing_question",
            "information",
            "cancellation",
            "other"
        ],
        labels
    );
}

#[test]
fn triage_intent_descriptions() {
    let qs = LayaPresets::triage();
    let options = qs["intent"].as_choice().unwrap().options();
    assert_eq!(
        Value::String("money returned or a duplicate charge reversed".to_string()),
        options[0].1
    );
    assert_eq!(
        Value::String("none of the other options fits".to_string()),
        options[5].1
    );
}

#[test]
fn triage_instructions_verbatim() {
    let qs = LayaPresets::triage();
    assert_eq!(
        &Value::String("What does the customer want in `message`?".to_string()),
        qs["intent"].instructions()
    );
    assert_eq!(
        &Value::String("Does `message` communicate time pressure or a deadline?".to_string()),
        qs["is_urgent"].instructions()
    );
    assert_eq!(
        &Value::String("How frustrated does the customer sound in `message`?".to_string()),
        qs["frustration"].instructions()
    );
    assert_eq!(
        &Value::String("Does the customer ask for money back?".to_string()),
        qs["refund_requested"].instructions()
    );
    assert_eq!(
        &Value::String(
            "Does `message` suggest the customer may leave for a competitor or cancel?".to_string()
        ),
        qs["churn_risk"].instructions()
    );
}

#[test]
fn triage_frustration_levels_in_order() {
    let qs = LayaPresets::triage();
    let levels = qs["frustration"].as_score().unwrap().levels();
    let expected = [
        "calm and neutral",
        "concerned but civil",
        "clearly annoyed",
        "very angry or using strong language",
    ]
    .map(|s| Value::String(s.to_string()));
    assert_eq!(expected.as_slice(), levels);
}

// ── Email ───────────────────────────────────────────────────────────────────

#[test]
fn email_default_question_ids_in_order() {
    let qs = LayaPresets::email(None::<Vec<(&str, &str)>>);
    let ids: Vec<&str> = qs.ids().collect();
    assert_eq!(
        vec![
            "category",
            "is_spam",
            "is_phishing",
            "urgency",
            "needs_reply"
        ],
        ids
    );
}

#[test]
fn email_default_question_types() {
    let qs = LayaPresets::email(None::<Vec<(&str, &str)>>);
    assert_eq!(QuestionType::Choice, qs["category"].question_type());
    assert_eq!(QuestionType::Noul, qs["is_spam"].question_type());
    assert_eq!(QuestionType::Noul, qs["is_phishing"].question_type());
    assert_eq!(QuestionType::Score, qs["urgency"].question_type());
    assert_eq!(QuestionType::Noul, qs["needs_reply"].question_type());
}

#[test]
fn email_default_category_labels_in_order() {
    let qs = LayaPresets::email(None::<Vec<(&str, &str)>>);
    let labels: Vec<&str> = qs["category"].as_choice().unwrap().labels().collect();
    assert_eq!(
        vec!["billing", "technical", "sales", "security", "hr", "other"],
        labels
    );
}

#[test]
fn email_default_category_descriptions() {
    let qs = LayaPresets::email(None::<Vec<(&str, &str)>>);
    let options = qs["category"].as_choice().unwrap().options();
    assert_eq!(
        Value::String("invoices, payments, refunds".to_string()),
        options[0].1
    );
    assert_eq!(Value::String("none of the above".to_string()), options[5].1);
}

#[test]
fn email_is_phishing_has_criteria() {
    let qs = LayaPresets::email(None::<Vec<(&str, &str)>>);
    let q = qs["is_phishing"].as_noul().unwrap();
    assert_eq!(
        &Value::String(
            "Is this email a phishing or scam attempt to steal money, credentials, or personal data?"
                .to_string()
        ),
        qs["is_phishing"].instructions()
    );
    assert_eq!(
        &Value::String("a legitimate email".to_string()),
        q.if_false()
    );
    assert_eq!(
        &Value::String("phishing, scam, or fraud".to_string()),
        q.if_true()
    );
}

#[test]
fn email_urgency_levels_in_order() {
    let qs = LayaPresets::email(None::<Vec<(&str, &str)>>);
    let levels = qs["urgency"].as_score().unwrap().levels();
    let expected = [
        "no time pressure",
        "needs attention soon",
        "blocking issue or hard deadline",
    ]
    .map(|s| Value::String(s.to_string()));
    assert_eq!(expected.as_slice(), levels);
}

#[test]
fn email_custom_categories_replaces_defaults() {
    let custom = vec![("support", "customer issues"), ("billing", "payments")];
    let qs = LayaPresets::email(Some(custom));
    let labels: Vec<&str> = qs["category"].as_choice().unwrap().labels().collect();
    assert_eq!(vec!["support", "billing"], labels);
}

#[test]
fn email_custom_categories_descriptions_preserved() {
    let custom = vec![("support", "customer issues"), ("billing", "payments")];
    let qs = LayaPresets::email(Some(custom));
    let options = qs["category"].as_choice().unwrap().options();
    assert_eq!(Value::String("customer issues".to_string()), options[0].1);
    assert_eq!(Value::String("payments".to_string()), options[1].1);
}

#[test]
fn email_custom_categories_other_questions_unchanged() {
    let custom = vec![("x", "y")];
    let qs = LayaPresets::email(Some(custom));
    assert_eq!(QuestionType::Noul, qs["is_spam"].question_type());
    assert_eq!(QuestionType::Noul, qs["is_phishing"].question_type());
    assert_eq!(QuestionType::Score, qs["urgency"].question_type());
    assert_eq!(QuestionType::Noul, qs["needs_reply"].question_type());
}

// ── Guard ───────────────────────────────────────────────────────────────────

#[test]
fn guard_question_ids_in_order() {
    let qs = LayaPresets::guard();
    let ids: Vec<&str> = qs.ids().collect();
    assert_eq!(
        vec![
            "jailbreak",
            "prompt_injection",
            "sensitive_data",
            "harm_severity",
            "topic"
        ],
        ids
    );
}

#[test]
fn guard_question_types() {
    let qs = LayaPresets::guard();
    assert_eq!(QuestionType::Noul, qs["jailbreak"].question_type());
    assert_eq!(QuestionType::Noul, qs["prompt_injection"].question_type());
    assert_eq!(QuestionType::Noul, qs["sensitive_data"].question_type());
    assert_eq!(QuestionType::Score, qs["harm_severity"].question_type());
    assert_eq!(QuestionType::Choice, qs["topic"].question_type());
}

#[test]
fn guard_topic_labels_in_order() {
    let qs = LayaPresets::guard();
    let labels: Vec<&str> = qs["topic"].as_choice().unwrap().labels().collect();
    assert_eq!(
        vec![
            "product_support",
            "coding",
            "general_knowledge",
            "personal_advice",
            "security_testing",
            "other"
        ],
        labels
    );
}

#[test]
fn guard_topic_options_have_null_descriptions() {
    // Python uses None for all topic descriptions — bare labels only.
    let qs = LayaPresets::guard();
    for (_, description) in qs["topic"].as_choice().unwrap().options() {
        assert_eq!(&Value::Null, description);
    }
}

#[test]
fn guard_harm_severity_levels_in_order() {
    let qs = LayaPresets::guard();
    let levels = qs["harm_severity"].as_score().unwrap().levels();
    let expected = [
        "none: ordinary request",
        "minor: mildly inappropriate",
        "serious: unsafe advice or abuse",
        "severe: dangerous or illegal",
    ]
    .map(|s| Value::String(s.to_string()));
    assert_eq!(expected.as_slice(), levels);
}

#[test]
fn guard_instructions_verbatim() {
    let qs = LayaPresets::guard();
    assert_eq!(
        &Value::String(
            "Does `prompt` try to make an AI assistant ignore its rules, policies or system instructions?"
                .to_string()
        ),
        qs["jailbreak"].instructions()
    );
    assert_eq!(
        &Value::String(
            "Does `prompt` contain instructions aimed at the AI system rather than a genuine user request?"
                .to_string()
        ),
        qs["prompt_injection"].instructions()
    );
    assert_eq!(
        &Value::String(
            "Does `prompt` contain credentials, personal data or other sensitive information?"
                .to_string()
        ),
        qs["sensitive_data"].instructions()
    );
    assert_eq!(
        &Value::String("How much harm would complying with `prompt` cause?".to_string()),
        qs["harm_severity"].instructions()
    );
    assert_eq!(
        &Value::String("What is `prompt` about?".to_string()),
        qs["topic"].instructions()
    );
}

// ── Moderation ──────────────────────────────────────────────────────────────

#[test]
fn moderation_question_ids_in_order() {
    let qs = LayaPresets::moderation();
    let ids: Vec<&str> = qs.ids().collect();
    assert_eq!(
        vec!["toxic", "harassment", "threat", "spam", "severity"],
        ids
    );
}

#[test]
fn moderation_question_types() {
    let qs = LayaPresets::moderation();
    assert_eq!(QuestionType::Noul, qs["toxic"].question_type());
    assert_eq!(QuestionType::Noul, qs["harassment"].question_type());
    assert_eq!(QuestionType::Noul, qs["threat"].question_type());
    assert_eq!(QuestionType::Noul, qs["spam"].question_type());
    assert_eq!(QuestionType::Score, qs["severity"].question_type());
}

#[test]
fn moderation_severity_levels_in_order() {
    let qs = LayaPresets::moderation();
    let levels = qs["severity"].as_score().unwrap().levels();
    let expected = [
        "no rule-breaking: ordinary on-topic post",
        "mild: rude tone or off-topic, no target",
        "clear violation: insults, harassment or spam aimed at someone",
        "severe: threats, hate speech or calls for violence",
    ]
    .map(|s| Value::String(s.to_string()));
    assert_eq!(expected.as_slice(), levels);
}

#[test]
fn moderation_instructions_verbatim() {
    let qs = LayaPresets::moderation();
    assert_eq!(
        &Value::String(
            "Is `post` toxic: rude, disrespectful or likely to make someone leave the discussion?"
                .to_string()
        ),
        qs["toxic"].instructions()
    );
    assert_eq!(
        &Value::String("Does `post` target or harass a specific person?".to_string()),
        qs["harassment"].instructions()
    );
    assert_eq!(
        &Value::String("Does `post` threaten violence, harm or intimidation?".to_string()),
        qs["threat"].instructions()
    );
    assert_eq!(
        &Value::String("Is `post` spam or advertising?".to_string()),
        qs["spam"].instructions()
    );
    assert_eq!(
        &Value::String("How severe is any rule-breaking in `post`?".to_string()),
        qs["severity"].instructions()
    );
}

// ── Router ──────────────────────────────────────────────────────────────────

#[test]
fn router_question_ids_in_order() {
    let qs = LayaPresets::router();
    let ids: Vec<&str> = qs.ids().collect();
    assert_eq!(
        vec!["difficulty", "domain", "needs_tools", "is_sensitive"],
        ids
    );
}

#[test]
fn router_question_types() {
    let qs = LayaPresets::router();
    assert_eq!(QuestionType::Score, qs["difficulty"].question_type());
    assert_eq!(QuestionType::Choice, qs["domain"].question_type());
    assert_eq!(QuestionType::Noul, qs["needs_tools"].question_type());
    assert_eq!(QuestionType::Noul, qs["is_sensitive"].question_type());
}

#[test]
fn router_domain_labels_in_order() {
    let qs = LayaPresets::router();
    let labels: Vec<&str> = qs["domain"].as_choice().unwrap().labels().collect();
    assert_eq!(
        vec![
            "code",
            "math_or_logic",
            "writing",
            "factual_lookup",
            "data_analysis",
            "chitchat"
        ],
        labels
    );
}

#[test]
fn router_domain_descriptions() {
    let qs = LayaPresets::router();
    let options = qs["domain"].as_choice().unwrap().options();
    assert_eq!(
        Value::String(
            "software engineering, programming, refactoring, architecture, debugging".to_string()
        ),
        options[0].1
    );
    assert_eq!(
        Value::String("casual conversation, greetings, small talk".to_string()),
        options[5].1
    );
}

#[test]
fn router_difficulty_levels_in_order() {
    let qs = LayaPresets::router();
    let levels = qs["difficulty"].as_score().unwrap().levels();
    let expected = [
        "trivial: a lookup or one-liner",
        "easy: short answer, no reasoning",
        "moderate: several steps",
        "hard: long multi-step reasoning or specialist knowledge",
    ]
    .map(|s| Value::String(s.to_string()));
    assert_eq!(expected.as_slice(), levels);
}

#[test]
fn router_instructions_verbatim() {
    let qs = LayaPresets::router();
    assert_eq!(
        &Value::String("How hard is `request` for a language model?".to_string()),
        qs["difficulty"].instructions()
    );
    assert_eq!(
        &Value::String("What domain does `request` belong to?".to_string()),
        qs["domain"].instructions()
    );
    assert_eq!(
        &Value::String(
            "Does answering `request` require external tools, search or private data?".to_string()
        ),
        qs["needs_tools"].instructions()
    );
    assert_eq!(
        &Value::String(
            "Does `request` involve money, legal, medical or safety consequences?".to_string()
        ),
        qs["is_sensitive"].instructions()
    );
}