use super::*;
#[test]
fn test_estimate_content_tokens_non_zero() {
let tokens = estimate_content_tokens("hello world");
assert!(tokens > 0);
}
#[test]
fn test_estimate_tokens_with_overhead() {
let tokens = estimate_tokens_with_overhead("hello", 10);
assert!(tokens >= 11);
}
#[test]
fn test_estimate_content_tokens_code() {
let tokens = estimate_content_tokens("fn main() { println!(\"hi\"); }");
assert!(tokens > 0);
}
#[test]
fn test_cache_returns_consistent_results() {
let content = "The quick brown fox jumps over the lazy dog";
let first = estimate_content_tokens(content);
let second = estimate_content_tokens(content);
assert_eq!(first, second);
}
#[test]
fn test_hash_content_deterministic() {
let a = hash_content("hello");
let b = hash_content("hello");
assert_eq!(a, b);
let c = hash_content("world");
assert_ne!(a, c);
}
#[test]
fn test_hf_tokenizer_repo_known_families() {
assert!(hf_tokenizer_repo("Qwen/Qwen2.5-Coder-32B").is_some());
assert!(hf_tokenizer_repo("qwen2.5-7b").is_some());
assert!(hf_tokenizer_repo("z-ai/glm-5.2").is_some());
assert!(hf_tokenizer_repo("THUDM/glm-4-9b").is_some());
assert!(hf_tokenizer_repo("gpt-4o").is_some());
assert!(hf_tokenizer_repo("llama-3-8b").is_some());
assert!(hf_tokenizer_repo("mistral-7b").is_some());
}
#[test]
fn test_hf_tokenizer_repo_unknown_returns_none() {
assert!(hf_tokenizer_repo("some-random-model").is_none());
assert!(hf_tokenizer_repo("").is_none());
}
#[test]
fn test_for_model_falls_back_gracefully() {
let state = TokenizerState::for_model(Some("unknown-model-xyz"));
let count = state.count("fn main() { println!(\"hello\"); }");
assert!(
count > 0,
"fallback tokenizer must produce a positive count"
);
}
#[test]
fn test_for_model_none_falls_back_gracefully() {
let state = TokenizerState::for_model(None);
let count = state.count("hello world");
assert!(count > 0);
}
#[test]
fn test_configured_model_slot_used_when_env_absent() {
let _guard = crate::test_support::EnvGuard::clear_selfware_env();
set_configured_model("slot-model");
assert_eq!(configured_model_name().as_deref(), Some("slot-model"));
}
#[test]
fn test_env_var_beats_configured_model_slot() {
let _guard = crate::test_support::EnvGuard::clear_selfware_env();
set_configured_model("slot-model");
std::env::set_var("SELFWARE_MODEL", "env-model");
assert_eq!(configured_model_name().as_deref(), Some("env-model"));
}
#[test]
fn test_estimate_messages_tokens_simple() {
use crate::api::types::Message;
let messages = vec![
Message::system("You are a helpful assistant"),
Message::user("Hello, how are you?"),
Message::assistant("I'm doing well, thank you!"),
];
let estimate = estimate_messages_tokens(&messages);
assert!(estimate > 12);
}
#[test]
fn test_estimate_messages_tokens_with_tool_calls() {
use crate::api::types::{Message, ToolCall, ToolFunction};
let mut msg = Message::assistant("Let me read that file for you.");
msg.tool_calls = Some(vec![ToolCall {
id: "call_1".to_string(),
call_type: "function".to_string(),
function: ToolFunction {
name: "file_read".to_string(),
arguments: r#"{"path": "test.txt"}"#.to_string(),
},
}]);
let messages = vec![msg];
let estimate = estimate_messages_tokens(&messages);
assert!(estimate > 20);
}
#[test]
fn test_estimate_messages_tokens_empty() {
let messages: Vec<crate::api::types::Message> = vec![];
let estimate = estimate_messages_tokens(&messages);
assert_eq!(estimate, 0);
}