#[test]
fn test_format_chat_messages_single_user() {
use crate::api::realize_handlers::format_chat_messages;
let messages = vec![crate::api::ChatMessage {
role: "user".to_string(),
content: "Hello!".to_string(),
name: None,
..Default::default()
}];
let result = format_chat_messages(&messages, None);
assert!(result.contains("Hello!"));
}
#[test]
fn test_format_chat_messages_multi_turn() {
use crate::api::realize_handlers::format_chat_messages;
let messages = vec![
crate::api::ChatMessage {
role: "system".to_string(),
content: "You are helpful.".to_string(),
name: None,
..Default::default()
},
crate::api::ChatMessage {
role: "user".to_string(),
content: "Hi".to_string(),
name: None,
..Default::default()
},
crate::api::ChatMessage {
role: "assistant".to_string(),
content: "Hello!".to_string(),
name: None,
..Default::default()
},
];
let result = format_chat_messages(&messages, None);
assert!(result.contains("Hi") || result.contains("Hello!"));
}
#[test]
fn test_format_chat_messages_with_model_name() {
use crate::api::realize_handlers::format_chat_messages;
let messages = vec![crate::api::ChatMessage {
role: "user".to_string(),
content: "Test".to_string(),
name: None,
..Default::default()
}];
let result = format_chat_messages(&messages, Some("llama"));
assert!(!result.is_empty());
}
#[test]
fn test_clean_chat_output_no_markers() {
use crate::api::realize_handlers::clean_chat_output;
let result = clean_chat_output("Just plain text");
assert_eq!(result, "Just plain text");
}
#[test]
fn test_clean_chat_output_chatml_markers() {
use crate::api::realize_handlers::clean_chat_output;
let text = "<|im_start|>assistant\nHello there<|im_end|>";
let result = clean_chat_output(text);
assert!(result.is_empty() || !result.contains("<|im_start|>"));
}
#[test]
fn test_clean_chat_output_empty() {
use crate::api::realize_handlers::clean_chat_output;
let result = clean_chat_output("");
assert!(result.is_empty());
}
#[test]
fn test_clean_chat_output_partial_markers() {
use crate::api::realize_handlers::clean_chat_output;
let text = "Hello world<|im_end|>extra stuff";
let result = clean_chat_output(text);
assert!(result.contains("Hello world"));
assert!(!result.contains("extra stuff"));
}
#[test]
fn test_clean_chat_output_leading_human_prefix() {
use crate::api::realize_handlers::clean_chat_output;
let result = clean_chat_output("Human: Here's what I have so far:\n\nhello");
assert!(!result.starts_with("Human:"), "result was: {result:?}");
assert!(result.contains("Here's what I have so far"));
}
#[test]
fn test_clean_chat_output_leading_user_prefix() {
use crate::api::realize_handlers::clean_chat_output;
let result = clean_chat_output("User: please explain this code");
assert!(!result.starts_with("User:"), "result was: {result:?}");
assert!(result.contains("please explain this code"));
}
#[test]
fn test_clean_chat_output_leading_assistant_prefix() {
use crate::api::realize_handlers::clean_chat_output;
let result = clean_chat_output("Assistant: here is my answer");
assert!(!result.starts_with("Assistant:"), "result was: {result:?}");
assert!(result.contains("here is my answer"));
}
#[test]
fn test_clean_chat_output_leading_prefix_with_whitespace() {
use crate::api::realize_handlers::clean_chat_output;
let result = clean_chat_output(" \n\tHuman: body");
assert!(!result.starts_with("Human:"), "result was: {result:?}");
assert!(result.contains("body"));
}
#[test]
fn test_clean_chat_output_inline_human_after_leading_strip() {
use crate::api::realize_handlers::clean_chat_output;
let result = clean_chat_output("Human: turn body\nHuman: leak");
assert!(!result.contains("leak"), "result was: {result:?}");
assert!(result.contains("turn body"));
}
#[test]
fn test_clean_chat_output_no_false_positive_on_human_in_middle() {
use crate::api::realize_handlers::clean_chat_output;
let result = clean_chat_output("The word Human: is left alone.");
assert!(result.contains("Human:"), "result was: {result:?}");
}
#[tokio::test]
async fn test_realize_embed_endpoint() {
let app = create_test_app_shared();
let request = Request::builder()
.method("POST")
.uri("/v1/embed")
.header("content-type", "application/json")
.body(Body::from(r#"{"input":"Hello world"}"#))
.expect("test value should be present");
let response = app.oneshot(request).await.expect("test value should be present");
assert!(
response.status() == StatusCode::OK
|| response.status() == StatusCode::NOT_FOUND
|| response.status() == StatusCode::INTERNAL_SERVER_ERROR,
);
}
#[tokio::test]
async fn test_realize_model_endpoint() {
let app = create_test_app_shared();
let request = Request::builder()
.method("GET")
.uri("/v1/model")
.body(Body::empty())
.expect("test value should be present");
let response = app.oneshot(request).await.expect("test value should be present");
assert!(response.status() == StatusCode::OK || response.status() == StatusCode::NOT_FOUND,);
}
#[tokio::test]
async fn test_realize_reload_endpoint() {
let app = create_test_app_shared();
let request = Request::builder()
.method("POST")
.uri("/v1/reload")
.header("content-type", "application/json")
.body(Body::from(r#"{"model":"test"}"#))
.expect("test value should be present");
let response = app.oneshot(request).await.expect("test value should be present");
assert!(
response.status() == StatusCode::OK
|| response.status() == StatusCode::NOT_FOUND
|| response.status() == StatusCode::UNPROCESSABLE_ENTITY,
);
}
#[tokio::test]
async fn test_openai_completions_endpoint() {
let app = create_test_app_shared();
let request = Request::builder()
.method("POST")
.uri("/v1/completions")
.header("content-type", "application/json")
.body(Body::from(
r#"{"model":"test","prompt":"Hello","max_tokens":5}"#,
))
.expect("test value should be present");
let response = app.oneshot(request).await.expect("test value should be present");
crate::api::test_helpers::assert_no_model_status(response.status());
}
#[tokio::test]
async fn test_openai_embeddings_endpoint() {
let app = create_test_app_shared();
let request = Request::builder()
.method("POST")
.uri("/v1/embeddings")
.header("content-type", "application/json")
.body(Body::from(r#"{"input":"Hello"}"#))
.expect("test value should be present");
let response = app.oneshot(request).await.expect("test value should be present");
assert_eq!(
response.status(),
StatusCode::SERVICE_UNAVAILABLE,
"no model is resident: expected 503"
);
}
#[tokio::test]
async fn test_openai_models_endpoint() {
let app = create_test_app_shared();
let request = Request::builder()
.method("GET")
.uri("/v1/models")
.body(Body::empty())
.expect("test value should be present");
let response = app.oneshot(request).await.expect("test value should be present");
assert!(response.status() == StatusCode::OK || response.status() == StatusCode::NOT_FOUND,);
}
#[tokio::test]
async fn test_openai_chat_completions_endpoint() {
let app = create_test_app_shared();
let request = Request::builder()
.method("POST")
.uri("/v1/chat/completions")
.header("content-type", "application/json")
.body(Body::from(
r#"{"model":"test","messages":[{"role":"user","content":"Hello"}]}"#,
))
.expect("test value should be present");
let response = app.oneshot(request).await.expect("test value should be present");
crate::api::test_helpers::assert_no_model_status(response.status());
}
#[tokio::test]
async fn test_openai_chat_completions_with_temperature() {
let app = create_test_app_shared();
let request = Request::builder()
.method("POST")
.uri("/v1/chat/completions")
.header("content-type", "application/json")
.body(Body::from(
r#"{"model":"test","messages":[{"role":"user","content":"Hi"}],"temperature":0.5,"max_tokens":10}"#,
))
.expect("test value should be present");
let response = app.oneshot(request).await.expect("test value should be present");
crate::api::test_helpers::assert_no_model_status(response.status());
}
#[tokio::test]
async fn test_openai_chat_completions_streaming() {
let app = create_test_app_shared();
let request = Request::builder()
.method("POST")
.uri("/v1/chat/completions")
.header("content-type", "application/json")
.body(Body::from(
r#"{"model":"test","messages":[{"role":"user","content":"Hi"}],"stream":true}"#,
))
.expect("test value should be present");
let response = app.oneshot(request).await.expect("test value should be present");
crate::api::test_helpers::assert_no_model_status(response.status());
}
#[test]
fn test_appstate_has_quantized_model_demo() {
let state = AppState::demo_mock().expect("test value should be present");
let _ = state.has_quantized_model();
let _ = state.quantized_model();
}
#[test]
fn test_appstate_has_apr_transformer_demo() {
let state = AppState::demo_mock().expect("test value should be present");
let _ = state.has_apr_transformer();
let _ = state.apr_transformer();
}
#[test]
fn test_appstate_verbose() {
let state = AppState::demo_mock().expect("test value should be present");
assert!(!state.is_verbose());
let state_verbose = state.with_verbose(true);
assert!(state_verbose.is_verbose());
}
#[test]
fn test_appstate_demo_creates_valid_state() {
let state = AppState::demo();
assert!(state.is_ok());
}
#[test]
fn test_appstate_demo_mock_creates_valid_state() {
let state = AppState::demo_mock();
assert!(state.is_ok());
}
#[test]
fn test_build_trace_data_none() {
let (brick, step, layer) = crate::api::build_trace_data(None, 100, 10, 5, 4);
assert!(brick.is_none());
assert!(step.is_none());
assert!(layer.is_none());
}
#[test]
fn test_build_trace_data_brick() {
let (brick, step, layer) = crate::api::build_trace_data(Some("brick"), 100, 10, 5, 4);
assert!(brick.is_some());
assert!(step.is_none());
assert!(layer.is_none());
let b = brick.expect("test value should be present");
assert_eq!(b.level, "brick");
}
#[test]
fn test_build_trace_data_step() {
let (brick, step, layer) = crate::api::build_trace_data(Some("step"), 200, 10, 5, 4);
assert!(brick.is_none());
assert!(step.is_some());
assert!(layer.is_none());
let s = step.expect("test value should be present");
assert_eq!(s.level, "step");
}
#[test]
fn test_build_trace_data_layer() {
let (brick, step, layer) = crate::api::build_trace_data(Some("layer"), 300, 10, 5, 8);
assert!(brick.is_none());
assert!(step.is_none());
assert!(layer.is_some());
let l = layer.expect("test value should be present");
assert_eq!(l.level, "layer");
}
#[test]
fn test_chat_message_serde() {
let msg = crate::api::ChatMessage {
role: "user".to_string(),
content: "Hello".to_string(),
name: Some("alice".to_string()),
..Default::default()
};
let json = serde_json::to_string(&msg).expect("JSON serialization failed");
let deserialized: crate::api::ChatMessage = serde_json::from_str(&json).expect("JSON deserialization failed");
assert_eq!(deserialized.role, "user");
assert_eq!(deserialized.content, "Hello");
assert_eq!(deserialized.name, Some("alice".to_string()));
}
#[test]
fn test_chat_message_without_name() {
let json = r#"{"role":"assistant","content":"Hi!"}"#;
let msg: crate::api::ChatMessage = serde_json::from_str(json).expect("JSON deserialization failed");
assert_eq!(msg.role, "assistant");
assert!(msg.name.is_none());
}
#[test]
fn test_error_response_serde() {
let err = crate::api::ErrorResponse {
error: "something went wrong".to_string(),
};
let json = serde_json::to_string(&err).expect("JSON serialization failed");
let deserialized: crate::api::ErrorResponse = serde_json::from_str(&json).expect("JSON deserialization failed");
assert_eq!(deserialized.error, "something went wrong");
}
#[test]
fn test_health_response_serde() {
let health = crate::api::HealthResponse {
status: "ok".to_string(),
version: "0.3.5".to_string(),
compute_mode: "cpu".to_string(),
model_loaded: true,
uptime_sec: 1.0,
};
let json = serde_json::to_string(&health).expect("JSON serialization failed");
let deserialized: crate::api::HealthResponse = serde_json::from_str(&json).expect("JSON deserialization failed");
assert_eq!(deserialized.status, "ok");
}
#[test]
fn test_generate_request_serde() {
let req = crate::api::GenerateRequest {
prompt: "Hello".to_string(),
max_tokens: 10,
temperature: 0.5,
strategy: "greedy".to_string(),
top_k: 1,
top_p: 1.0,
seed: Some(42),
model_id: None,
};
let json = serde_json::to_string(&req).expect("JSON serialization failed");
let deserialized: crate::api::GenerateRequest = serde_json::from_str(&json).expect("JSON deserialization failed");
assert_eq!(deserialized.prompt, "Hello");
assert_eq!(deserialized.seed, Some(42));
}
#[test]
fn test_generate_response_serde() {
let resp = crate::api::GenerateResponse {
token_ids: vec![1, 2, 3],
text: "hello".to_string(),
num_generated: 3,
};
let json = serde_json::to_string(&resp).expect("JSON serialization failed");
let deserialized: crate::api::GenerateResponse = serde_json::from_str(&json).expect("JSON deserialization failed");
assert_eq!(deserialized.num_generated, 3);
}
#[test]
fn test_tokenize_request_serde() {
let req = crate::api::TokenizeRequest {
text: "Hello world".to_string(),
model_id: None,
};
let json = serde_json::to_string(&req).expect("JSON serialization failed");
let deserialized: crate::api::TokenizeRequest = serde_json::from_str(&json).expect("JSON deserialization failed");
assert_eq!(deserialized.text, "Hello world");
}
#[test]
fn test_tokenize_response_serde() {
let resp = crate::api::TokenizeResponse {
token_ids: vec![1, 2, 3, 4],
num_tokens: 4,
};
let json = serde_json::to_string(&resp).expect("JSON serialization failed");
let deserialized: crate::api::TokenizeResponse = serde_json::from_str(&json).expect("JSON deserialization failed");
assert_eq!(deserialized.num_tokens, 4);
}